Skip to content

Commit

Permalink
1.5.8
Browse files Browse the repository at this point in the history
  • Loading branch information
xErik committed Aug 15, 2023
1 parent f3e06ae commit 0429bea
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 5 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,8 @@

## 1.5.7

- Added service method for no-wrap and no-hyphenation calculation: Hyphenator.noWrapNoHyphen()
- Added service method for no-wrap and no-hyphenation calculation: Hyphenator.noWrapNoHyphen()

## 1.5.8

- Added multiple lines to: Hyphenator.noWrapNoHyphen()
82 changes: 81 additions & 1 deletion lib/token/linenowrappernohyphen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,50 @@ class LineNoWrapperNoHyphen {

while (true) {
var token = _tokenIter.current();
line.add(token);

// ------------------------------------------------------------
// NEWLINE
// ------------------------------------------------------------
// if (token is NewlineToken) {
// _lines.add(_cloneLineAndAddNewline(line));
// line.clear();
// } else
// ------------------------------------------------------------
// TABS AND SPACES
// ------------------------------------------------------------
if (token is TabsAndSpacesToken && line.isNotEmpty) {
if (_canAddNoHyphen([token], line)) {
line.add(token);
} else {
_lines.add(_cloneLineAndAddNewline(line));
line.clear();
}
} else
// ------------------------------------------------------------
// WORD
// ------------------------------------------------------------
if (token is WordToken) {
// print('ADING WORD TO LINE: $token');
bool trySuccess = _tryAddWordToLine(token, line);

if (trySuccess == false) {
if (line.isNotEmpty) {
_lines.add(_cloneLineAndAddNewline(line));
line.clear();
}
trySuccess = _tryAddWordToLine(token, line);
// print('ADING WORD TO EMPTY LINE: $token | $trySuccess | $line');

if (trySuccess == false) {
if (line.isNotEmpty) {
_lines.add(_cloneLineAndAddNewline(line));
line.clear();
}
// print('ADING WORD TO NEW EMPTY LINE -- FORCED: $token');
line.add(token);
}
}
}

// ------------------------------------------------------------
// NEXT TOKEN
Expand All @@ -90,6 +133,13 @@ class LineNoWrapperNoHyphen {
_lines.add(line);
}

final maxLines = _text.maxLines ?? 1;
// print('maxLines ${maxLines}');
// print('_lines.length ${_lines.length}');
if (_lines.length > maxLines) {
return WrapResult(_text, _style, _maxWidth, Size(0, 0), _lines);
}

final str =
_lines.map<String>((line) => line.map((e) => e.render()).join()).join();
_painter.text = TextSpan(text: str, style: _style);
Expand All @@ -109,4 +159,34 @@ class LineNoWrapperNoHyphen {
// }
return wrap;
}

bool _tryAddWordToLine(WordToken token, List<TextPartToken> line) {
if (_canAddNoHyphen(token.parts, line)) {
line.addAll(token.parts);
return true;
}
return false;
}

List<TextPartToken> _cloneLineAndAddNewline(List<TextPartToken> line) {
List<TextPartToken> clone = [...line];
while (clone.isNotEmpty && clone.last is TabsAndSpacesToken) {
clone.removeLast();
}
clone.add(NewlineToken());
return clone;
}

bool _canAddNoHyphen(
final List<TextPartToken> tokens, List<TextPartToken> line) {
double w = line.fold<double>(0, (sum, item) {
return sum + item.sizeCurrent!.width;
});

w += tokens.fold<double>(0, (sum, item) {
return sum + item.sizeCurrent!.width;
});

return w <= _maxWidth;
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: hyphenatorx
description: Implementation of an hyphenation algorithm for various languages, based on TeX definitions.
version: 1.5.7
version: 1.5.8
repository: https://github.com/xErik/hyphenatorx
issue_tracker: https://github.com/xErik/hyphenatorx/issues

Expand Down
5 changes: 3 additions & 2 deletions test/hyphenator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,12 @@ and disciplines.""";
TextStyle(fontWeight: FontWeight.normal, fontSize: 14).merge(sRoboto);
final text = Text("""A vast subdivision of
culture, composed of many creative
endeavors and disciplines.""");
endeavors and disciplines.""", maxLines: 1);
final expected =
"""A vast subdivision of culture, composed of many creative endeavors and disciplines.""";

WrapResult res = Hyphenator.noWrapNoHyphen(text, style, 523.0);
WrapResult res = Hyphenator.noWrapNoHyphen(text, style, 540.0);
// print(res);
expect(res.isSizeMatching, true);
expect(res.textStr, expected);
});
Expand Down

0 comments on commit 0429bea

Please sign in to comment.