kseg
Khmer Script Segmenter - A powerful Flutter package for detecting and styling mixed script text
π 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.
π§ How It Works
- Text Analysis:
kseg
analyzes the input text to identify Khmer and Latin characters using Unicode ranges. - Segmentation: The text is segmented into chunks of consistent script type.
- Styling: Each segment is styled appropriately based on its script type.
- Rendering: The styled segments are combined into a
Text.rich
widget for display.
π± Platform Support
Android | iOS | macOS | Web | Linux | Windows |
---|---|---|---|---|---|
β | β | β | β | β | β |
Libraries
- kseg
- Khmer Script Segmenter (kseg)