Skip to content

Commit

Permalink
Add support for hyphenation
Browse files Browse the repository at this point in the history
  • Loading branch information
ilaurillard authored and DavBfr committed Jan 27, 2024
1 parent 77dd4fe commit 0a7acd8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions pdf/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Add Flutter's Logical Pixel constant
- Add support for existing reference objects
- Update barcode golden pdf
- Add support for hyphenation [ilja]

## 3.10.7

Expand Down
31 changes: 31 additions & 0 deletions pdf/lib/src/widgets/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,8 @@ class RichTextContext extends WidgetContext {
'$runtimeType Offset: $startOffset -> $endOffset Span: $spanStart -> $spanEnd';
}

typedef Hyphenation = List<String> Function(String word);

class RichText extends Widget with SpanningWidget {
RichText({
required this.text,
Expand All @@ -668,6 +670,7 @@ class RichText extends Widget with SpanningWidget {
this.textScaleFactor = 1.0,
this.maxLines,
this.overflow = TextOverflow.visible,
this.hyphenation,
});

static bool debug = false;
Expand Down Expand Up @@ -700,6 +703,8 @@ class RichText extends Widget with SpanningWidget {

List<InlineSpan>? _preprocessed;

final Hyphenation? hyphenation;

void _appendDecoration(bool append, _TextDecoration td) {
if (append && _decorations.isNotEmpty) {
final last = _decorations.last;
Expand Down Expand Up @@ -946,6 +951,32 @@ class RichText extends Widget with SpanningWidget {

if (_softWrap &&
offsetX + metrics.width > constraintWidth + 0.00001) {
if (hyphenation != null) {
final syllables = hyphenation!(word);
if (syllables.length > 1) {
var fits = '';
for (var syllable in syllables) {
if (offsetX +
((font.stringMetrics('$fits$syllable-',
letterSpacing: style.letterSpacing! /
(style.fontSize! *
textScaleFactor)) *
(style.fontSize! * textScaleFactor))
.width) >
constraintWidth + 0.00001) {
break;
}
fits += syllable;
}
if (fits.isNotEmpty) {
words[index] = '$fits-';
words.insert(index + 1, word.substring(fits.length));
index--;
continue;
}
}
}

if (spanCount > 0 && metrics.width <= constraintWidth) {
overflow = true;
lines.add(_Line(
Expand Down

0 comments on commit 0a7acd8

Please sign in to comment.