kseg 1.0.0 copy "kseg: ^1.0.0" to clipboard
kseg: ^1.0.0 copied to clipboard

A Flutter package that detects and segments different scripts within a string (e.g., Khmer and Latin), enabling distinct styling for each script.

kseg #

Khmer Script Segmenter - A powerful Flutter package for detecting and styling mixed script text

Pub.dev Badge License: MIT Platform GitHub stars

πŸ“‹ Overview #

kseg is a specialized Flutter package that elegantly solves the challenge of displaying mixed Khmer and Latin script text with proper styling. It automatically detects and segments different scripts within a string, allowing you to apply distinct styles (font family, size, weight, color, etc.) to each script type.

✨ Key Features #

  • πŸ” Smart Script Detection: Precisely identifies Khmer and Latin scripts based on Unicode character ranges
  • βœ‚οΈ Automatic Segmentation: Seamlessly divides text into segments by script type
  • 🎨 Independent Styling: Apply different styling to each script segment (font, size, weight, color, etc.)
  • πŸ“± Flutter Integration: Works perfectly with Flutter's Text and RichText widgets
  • πŸ“¦ Lightweight: No bundled fonts, integrates with Google Fonts to keep package size small
  • 🧩 Simple API: Clean, widget-based API that follows Flutter conventions
  • πŸ“„ Comprehensive Documentation: Detailed documentation and examples for easy implementation

πŸ“₯ Installation #

Add kseg to your pubspec.yaml file:

dependencies:
  flutter:
    sdk: flutter
  kseg: ^1.0.0
  google_fonts:  # Required for Google Fonts support

Then run:

flutter pub get

πŸš€ Usage #

Basic Usage #

Simply wrap your text in a Kseg widget and specify the fonts and styles for each script:

import 'package:flutter/material.dart';
import 'package:kseg/kseg.dart';

Kseg(
  text: "αžαŸαžŸαŸ’αžTest αž’αž€αŸ’αžŸαžšαžαŸ’αž˜αŸ‚αžš Latin script",
  khFont: "Battambang",
  latinFont: "Roboto",
  khFontSize: 18,
  latinFontSize: 16,
)

Google Fonts Integration #

By default, kseg uses Google Fonts for the specified font names:

Kseg(
  text: "αžαŸαžŸαŸ’αžTest αž’αž€αŸ’αžŸαžšαžαŸ’αž˜αŸ‚αžš Latin script",
  khFont: "Battambang",    // Uses Google Fonts
  latinFont: "Lato",       // Uses Google Fonts
  khFontSize: 20,
  latinFontSize: 16,
)

Using System Fonts #

If you want to use system fonts instead, set the appropriate flags:

Kseg(
  text: "αžαŸαžŸαŸ’αžTest αž’αž€αŸ’αžŸαžšαžαŸ’αž˜αŸ‚αžš Latin script",
  khFont: "Battambang",
  latinFont: "Roboto",
  khUseGoogleFont: false,    // Use system font
  latinUseGoogleFont: false, // Use system font
)

πŸ“ Examples #

Different Font Sizes #

Kseg(
  text: "αžαŸαžŸαŸ’αžTest αž’αž€αŸ’αžŸαžšαžαŸ’αž˜αŸ‚αžš Latin script",
  khFont: "Battambang",
  latinFont: "Roboto",
  khFontSize: 24,             // Larger Khmer text
  latinFontSize: 16,          // Smaller Latin text
)

Color and Weight Styling #

Kseg(
  text: "αžαŸαžŸαŸ’αžTest αž’αž€αŸ’αžŸαžšαžαŸ’αž˜αŸ‚αžš Latin script",
  khFont: "Moul",
  latinFont: "Roboto",
  khColor: Colors.blue,                // Blue Khmer text
  latinColor: Colors.deepOrange,       // Orange Latin text
  khFontWeight: FontWeight.bold,       // Bold Khmer text
  latinFontWeight: FontWeight.normal,  // Normal Latin text
)

Advanced Styling #

Kseg(
  text: "αžαŸαžŸαŸ’αžTest αž’αž€αŸ’αžŸαžšαžαŸ’αž˜αŸ‚αžš Latin script",
  khFont: "Battambang",
  latinFont: "Roboto Mono",
  khFontSize: 20,
  latinFontSize: 16,
  khColor: Colors.deepPurple,
  latinColor: Colors.teal,
  khLetterSpacing: 1.2,               // Wider letter spacing for Khmer
  latinLetterSpacing: 0.5,            // Normal letter spacing for Latin
  khHeight: 1.5,                      // Taller line height for Khmer
  latinHeight: 1.3,                   // Normal line height for Latin
  khBackgroundColor: Colors.yellow.shade100,  // Light yellow background for Khmer
  textAlign: TextAlign.center,        // Center align all text
  maxLines: 2,                        // Limit to 2 lines
  overflow: TextOverflow.ellipsis,    // Show ellipsis when text overflows
)

Text Decoration #

Kseg(
  text: "αžαŸαžŸαŸ’αžTest αž’αž€αŸ’αžŸαžšαžαŸ’αž˜αŸ‚αžš Latin script",
  khFont: "Battambang",
  latinFont: "Lato",
  khDecoration: TextDecoration.underline,       // Underline Khmer text
  khDecorationColor: Colors.red,                // Red underline
  latinDecoration: TextDecoration.lineThrough,  // Strikethrough Latin text
  latinDecorationStyle: TextDecorationStyle.wavy, // Wavy line style
)

πŸ“š API Reference #

Kseg Widget #

The main widget for displaying mixed script text with different styles.

Kseg({
  // Required
  required String text,
  
  // Khmer script styling
  String? khFont,                        // Font name for Khmer text
  double? khFontSize,                    // Font size for Khmer text
  FontWeight? khFontWeight,              // Font weight for Khmer text
  FontStyle? khFontStyle,                // Font style for Khmer text
  Color? khColor,                        // Text color for Khmer text
  Color? khBackgroundColor,              // Background color for Khmer text
  double? khLetterSpacing,               // Letter spacing for Khmer text
  double? khWordSpacing,                 // Word spacing for Khmer text
  double? khHeight,                      // Line height for Khmer text
  TextBaseline? khTextBaseline,          // Text baseline for Khmer text
  List<Shadow>? khShadows,               // Text shadows for Khmer text
  TextDecoration? khDecoration,          // Text decoration for Khmer text
  Color? khDecorationColor,              // Text decoration color for Khmer text
  TextDecorationStyle? khDecorationStyle, // Text decoration style for Khmer text
  double? khDecorationThickness,         // Text decoration thickness for Khmer text
  bool khUseGoogleFont = true,           // Whether to use Google Fonts for Khmer
  
  // Latin script styling
  String? latinFont,                      // Font name for Latin text
  double? latinFontSize,                  // Font size for Latin text
  FontWeight? latinFontWeight,            // Font weight for Latin text
  FontStyle? latinFontStyle,              // Font style for Latin text
  Color? latinColor,                      // Text color for Latin text
  Color? latinBackgroundColor,            // Background color for Latin text
  double? latinLetterSpacing,             // Letter spacing for Latin text
  double? latinWordSpacing,               // Word spacing for Latin text
  double? latinHeight,                    // Line height for Latin text
  TextBaseline? latinTextBaseline,        // Text baseline for Latin text
  List<Shadow>? latinShadows,             // Text shadows for Latin text
  TextDecoration? latinDecoration,        // Text decoration for Latin text
  Color? latinDecorationColor,            // Text decoration color for Latin text
  TextDecorationStyle? latinDecorationStyle, // Text decoration style for Latin text
  double? latinDecorationThickness,       // Text decoration thickness for Latin text
  bool latinUseGoogleFont = true,         // Whether to use Google Fonts for Latin
  
  // General text properties
  TextAlign? textAlign,                   // Text alignment
  TextDirection? textDirection,           // Text direction
  Locale? locale,                         // Locale
  bool? softWrap,                         // Whether text should soft wrap
  TextOverflow? overflow,                 // Overflow behavior
  int? maxLines,                          // Maximum number of lines
  TextWidthBasis? textWidthBasis,         // Text width basis
  TextHeightBehavior? textHeightBehavior, // Text height behavior
})

Utility Classes #

The package also provides these utility classes for advanced usage:

ScriptDetector

// Detects the script type of a character
static ScriptType detectCharScript(String char)

// Analyzes the overall script composition of a string
static ScriptType analyzeString(String text)

// Segments text into parts based on script changes
static List<ScriptDetectionResult> segmentByScript(String text)

// Checks if a string contains Khmer script
static bool containsKhmer(String text)

// Checks if a string contains Latin script
static bool containsLatin(String text)

// Checks if a string contains mixed scripts
static bool containsMixedScripts(String text)

TextSegmenter

// Segments a string into different script segments
static SegmentedString segment(String text)

// Extracts only Khmer text from a string
static SegmentedString extractKhmer(String text)

// Extracts only Latin text from a string
static SegmentedString extractLatin(String text)

🎬 Demo #

Check out our example project for a complete demonstration of all features. Kseg Demo

πŸ”§ How It Works #

  1. Text Analysis: kseg analyzes the input text to identify Khmer and Latin characters using Unicode ranges.
  2. Segmentation: The text is segmented into chunks of consistent script type.
  3. Styling: Each segment is styled appropriately based on its script type.
  4. Rendering: The styled segments are combined into a Text.rich widget for display.

πŸ“± Platform Support #

Android iOS macOS Web Linux Windows
βœ… βœ… βœ… βœ… βœ… βœ…
2
likes
150
points
122
downloads

Publisher

verified publishersovanken.space

Weekly Downloads

A Flutter package that detects and segments different scripts within a string (e.g., Khmer and Latin), enabling distinct styling for each script.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter, google_fonts

More

Packages that depend on kseg