Skip to content

Commit

Permalink
Fix missing glyph width.
Browse files Browse the repository at this point in the history
Prawn correctly used width of missing glyphs to layout text but didn't
encode the widths correctly in subset fonts. The resulted in characters
with missing glyphs to have 0 width. And even though Prawn drawn
Replacement Character glyphs they were stacking on top of each other
because their widths were wrong.
  • Loading branch information
pointlessone committed Jan 25, 2024
1 parent 11c73c2 commit 927dcf6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
12 changes: 6 additions & 6 deletions lib/prawn/fonts/ttf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -393,18 +393,18 @@ def embed_simple_font(reference, font, unicode_mapping)
XHeight: x_height
)

first_char = font.cmap.tables.first.code_map.index { |gid| !gid.zero? }
last_char = font.cmap.tables.first.code_map.rindex { |gid| !gid.zero? }
first_char, last_char = unicode_mapping.keys.minmax
hmtx = font.horizontal_metrics
widths =
font.cmap.tables.first.code_map[first_char..last_char].map do |gid|
if gid.zero?
(first_char..last_char).map do |code|
if unicode_mapping.key?(code)
gid = font.cmap.tables.first.code_map[code]
Integer(hmtx.widths[gid] * scale_factor)
else
# These characters are not in the document so we don't ever use
# these values but we need to encode them so let's use as little
# sapce as possible.
0
else
Integer(hmtx.widths[gid] * scale_factor)
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/prawn_manual_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
MANUAL_HASH =
case RUBY_ENGINE
when 'ruby'
'8ace5f35f945e5994647cefc2cf7bc369d131e0646d91eb8aeb94e58f72de18d8e7bf82f58fc45406110c4adad239dcbe834059580d29fec2b2a039db67db04c'
'7991e4f72e944140840e1c26f0fff331029846eaab148de8483d06491c7808bc4963e8e7376a514e855037f1f1b4197877a31f2df44f511f4f7f5e0ce5df3170'
when 'jruby'
'b77a740d3290192360c4c083018ca61ccc88d42e7c6a152c7bc394d751f5f08d85aec613549f6660713644b00518561cc0f9c947701f01e8c25632c9db81201a'
'29b8f8cb00910426805ce226fb47c59d6409683f35f0d2c056a6cf837ba086ca5c763ff89266cfc8e11b1d92af60c9974822b12ad761cdbdf520adb005a98750'
end

RSpec.describe Prawn do
Expand Down

0 comments on commit 927dcf6

Please sign in to comment.