Skip to content

Commit

Permalink
Test for wrong width of missing glyphs
Browse files Browse the repository at this point in the history
  • Loading branch information
mojavelinux authored and pointlessone committed Jan 22, 2024
1 parent a3054b7 commit 11c73c2
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions spec/prawn/font_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -670,4 +670,42 @@ def page_should_not_include_font(font)
font_name = text.font_settings.first[:name].to_s.sub(/\w+\+/, 'subset+')
expect(font_name).to eq 'subset+DustismoRoman'
end

it 'does not change width of unknown glyph' do
text_with_string_widths = Class.new(PDF::Inspector::Text) do
attr_reader :string_widths

def initialize(*)
super
@string_widths = []
end

def show_text text, kerned = false
super
@string_widths << ((@state.current_font.unpack text).reduce(0) do |width, code|
width + (@state.current_font.glyph_width code) * @font_settings[-1][:size] / 1000.0
end)
end
end

pdf = Prawn::Document.new do
font_families.update(
'DejaVu Sans' => {
normal: "#{Prawn::DATADIR}/fonts/DejaVuSans.ttf",
bold: "#{Prawn::DATADIR}/fonts/DejaVuSans-Bold.ttf"
},
)

# changing option to subset: false fixes issue (albeit using different behavior)
font 'DejaVu Sans', subset: true do
text '日本語<b>end</b>', inline_format: true
end
end

rendered_pdf = pdf.render
#File.write '/tmp/debug.pdf', rendered_pdf
analyzed_pdf = text_with_string_widths.analyze(rendered_pdf)
expect(analyzed_pdf.string_widths.length).to eql(2)
expect(analyzed_pdf.string_widths[0]).to be > 0.0
end
end

0 comments on commit 11c73c2

Please sign in to comment.