Skip to content

Commit

Permalink
Handle zero-width spaces in text-boxes
Browse files Browse the repository at this point in the history
  • Loading branch information
dwarring committed Dec 15, 2024
1 parent e8431a1 commit 1103a60
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{{$NEXT}}
- Handle zero width spaces.

0.8.12 2024-12-09T07:07:11+13:00
- Tweak current-point() for #28 and also to more faithfully follow
Expand Down
3 changes: 2 additions & 1 deletion lib/PDF/Content/Text/Box.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ method content-height returns Numeric { @!lines».height.sum * $.leading; }
my grammar Text {
token nbsp { <[ \c[NO-BREAK SPACE] \c[NARROW NO-BREAK SPACE] \c[WORD JOINER] ]> }
token space { [\s <!after <nbsp> >]+ }
token space { [\s <!after <nbsp> > | "\c[ZERO WIDTH SPACE]"]+ }
token word { [ <![ - \c[HYPHENATION POINT] ]> <!before <space>> . ]+ '-'? | <[ - \c[HYPHENATION POINT] ]> }
}

Expand Down Expand Up @@ -369,6 +369,7 @@ method !flush-spaces(@words is raw, $i is rw) returns UInt {
else {
$i++;
$n = 1 if $!squish;
$n = 0 if $_ eq "\c[ZERO WIDTH SPACE]";
}
}
}
Expand Down
Binary file modified t/text-box.pdf
Binary file not shown.
27 changes: 25 additions & 2 deletions t/text-box.t
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use v6;
use Test;
plan 13;
plan 14;
use lib 't';
use PDF::Grammar::Test :is-json-equiv;
use PDF::Content::Text::Box;
Expand Down Expand Up @@ -114,7 +114,7 @@ subtest 'overflow', {
my $width = 200;
$height = 50;
$text-box .= new( :$text, :$font, :$font-size, :$width, :$height );
.text-position = 100, 500;
.text-position = 100, 650;
.say: $text-box;
is $text-box.lines[0].text, 'Lorem ipsum dolor sit';
is $text-box.lines[1].text, 'amet, consectetur';
Expand All @@ -128,6 +128,29 @@ subtest 'overflow', {
}
}

subtest 'zero width spaces', {
$gfx.text: {
$text = q:to<END>.chomp;
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
END
$text ~~ s:g/' '/\c[ZERO WIDTH SPACE]/;
my $width = 200;
$height = 50;
$text-box .= new( :$text, :$font, :$font-size, :$width, :$height );
.text-position = 100, 500;
.say: $text-box;
is $text-box.lines[0].text, 'Loremipsumdolorsitamet,';
is $text-box.lines[1].text, 'consecteturadipiscingelit,';
is-deeply $text-box.Str.lines, ('Loremipsumdolorsitamet,', 'consecteturadipiscingelit,');
todo "fix tests", 2;
is-deeply $text-box.overflow.join, qw<do eiusmod tempor incididunt ut labore et dolore magna aliqua.>.join: "\c[ZERO WIDTH SPACE]";
$text = '...' ~ $text-box.overflow.join;
$text-box .= clone: :$text;
.say: $text-box;
is $text-box.overflow.join, "magna\c[ZERO WIDTH SPACE]aliqua.";
.say: '...' ~ $text-box.overflow.join;
}
}

subtest 'font loading from content stream', {
if (try require PDF::Font::Loader) === Nil {
Expand Down

0 comments on commit 1103a60

Please sign in to comment.