Skip to content

Commit

Permalink
Fix performance regression when encoding char codes for TrueType fonts
Browse files Browse the repository at this point in the history
The recent change for supporting multiple codepoint referencing the same
glyph lead to a performance regression because the char codes 13, 40, 41
and 92 were used again.
  • Loading branch information
gettalong committed Nov 5, 2024
1 parent 093b1e5 commit 8895738
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* Checksum calculation for TrueType tables
* Automatic wrapping of dictionary entry /CIDToGIDMap for CID fonts
* Performance regression when encoding char codes for TrueType fonts


## 1.0.1 - 2024-11-04
Expand Down
5 changes: 5 additions & 0 deletions lib/hexapdf/font/true_type_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ def encode(glyph)
raise HexaPDF::MissingGlyphError.new(glyph) if glyph.kind_of?(InvalidGlyph)
@subsetter.use_glyph(glyph.id) if @subsetter
@last_char_code += 1
# Handle codes for ASCII characters \r (13), (, ) (40, 41) and \ (92) specially so that
# they never appear in the output (PDF serialization would need to escape them)
if @last_char_code == 13 || @last_char_code == 40 || @last_char_code == 92
@last_char_code += (@last_char_code == 40 ? 2 : 1)
end
[[@last_char_code].pack('n'), @last_char_code]
end)[0]
end
Expand Down
5 changes: 5 additions & 0 deletions test/hexapdf/font/test_true_type_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@
assert_equal([3].pack('n'), code)
end

it "doesn't use char codes 13, 40, 41 and 92 because they would need to be escaped" do
codes = 1.upto(93).map {|i| @font_wrapper.encode(@font_wrapper.glyph(i)) }.join
assert_equal([1..12, 14..39, 42..91, 93..97].flat_map(&:to_a).pack('n*'), codes)
end

it "raises an error if an InvalidGlyph is encoded" do
exp = assert_raises(HexaPDF::MissingGlyphError) do
@font_wrapper.encode(@font_wrapper.decode_utf8("ö").first)
Expand Down

0 comments on commit 8895738

Please sign in to comment.