Skip to content

height_of returns same height for every character #1180

Answered by smithtim
smithtim asked this question in Q&A
Discussion options

You must be logged in to vote

I got it working! Here is the code (using a font that is available on my system). Thank you for your help.

def center_at pdf, character, x, y
  character_code = character.unpack1 'U*'
  glyph_id = pdf.font.ttf.cmap.unicode.first[character_code]
  outlines = pdf.font.ttf.glyph_outlines.for glyph_id
  units_per_em = pdf.font.ttf.header.units_per_em.to_f

  left = outlines.x_min / units_per_em * pdf.font_size
  right = outlines.x_max / units_per_em * pdf.font_size
  bottom = outlines.y_min / units_per_em * pdf.font_size
  top = outlines.y_max / units_per_em * pdf.font_size

  [x-(left+right)/2.0, y-(top+bottom)/2.0]
end

pdf = Prawn::Document.new
pdf.font "/run/current-system/sw/share/X11-fo…

Replies: 5 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by smithtim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #1180 on October 24, 2020 14:03.