This is the code for the HexaPDF post "Simple Text Metrics": https://hexapdf.gettalong.org/news/2017/simple-text-metrics.html
Created
May 10, 2017 18:29
-
-
Save gettalong/a1b832733b23fe997d4ed5abb72a57fc to your computer and use it in GitHub Desktop.
Simple Text Metrics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
require 'hexapdf' | |
require 'hexapdf/content/text_fragment' | |
def draw_fragment(canvas, items, origin_baseline: false, **options) | |
fragment = HexaPDF::Content::TextFragment.new(font: canvas.font, font_size: 20, items: items, **options) | |
canvas.translate(0, origin_baseline ? -fragment.baseline_offset : 0) do | |
canvas.horizontal_scaling(fragment.horizontal_scaling). | |
character_spacing(fragment.character_spacing). | |
word_spacing(fragment.word_spacing). | |
text_rise(fragment.text_rise) | |
canvas.move_text_cursor(offset: [0, fragment.baseline_offset]) | |
canvas.show_glyphs_only(fragment.items) | |
canvas.line_width = 0.3 | |
canvas.fill_color(0, 128, 0).circle(0, 0, 1).fill.fill_color(0) | |
canvas.stroke_color(255, 0, 0) | |
canvas.rectangle(fragment.x_min, fragment.baseline_offset + fragment.y_min, | |
fragment.x_max - fragment.x_min, fragment.y_max - fragment.y_min).stroke | |
canvas.stroke_color(0, 255, 0) | |
canvas.rectangle(0, 0, fragment.width, fragment.height).stroke | |
canvas.stroke_color(0, 0, 255) | |
canvas.line(0, fragment.baseline_offset, fragment.width, fragment.baseline_offset).stroke | |
end | |
fragment | |
end | |
doc = HexaPDF::Document.new(config: {"font.map" => {"test" => {none: "LinLibertine_Rah.ttf"}}}) | |
canvas = doc.pages.add(doc.add(Type: :Page, MediaBox: [0, 0, 180, 130])).canvas | |
canvas.font("test", size: 20) | |
canvas.translate(10, 110) do | |
items = canvas.font.decode_utf8("Hallo") | |
draw_fragment(canvas, items) | |
end | |
canvas.translate(10, 90) do | |
items = canvas.font.decode_utf8("Hallo") | |
draw_fragment(canvas, items, horizontal_scaling: 200, character_spacing: 2) | |
end | |
canvas.translate(10, 70) do | |
items = canvas.font.decode_utf8("Hallo").insert(1, 120).insert(5, -200) | |
draw_fragment(canvas, items) | |
end | |
canvas.translate(70, 70) do | |
items = canvas.font.decode_utf8("Hallo").insert(0, 300).insert(-1, 300) | |
draw_fragment(canvas, items) | |
end | |
canvas.translate(120, 70) do | |
items = canvas.font.decode_utf8("Hallo").insert(0, -300).insert(-1, -300) | |
draw_fragment(canvas, items) | |
end | |
canvas.translate(10, 45) do | |
items = canvas.font.decode_utf8("Hallo") | |
draw_fragment(canvas, items, text_rise: 5) | |
end | |
canvas.translate(70, 45) do | |
items = canvas.font.decode_utf8("Hallo") | |
draw_fragment(canvas, items, text_rise: -5) | |
end | |
canvas.translate(120, 45) do | |
items = canvas.font.decode_utf8("Hallo") | |
draw_fragment(canvas, items, text_rise: -20) | |
end | |
canvas.translate(10, 10) | |
frag = draw_fragment(canvas, canvas.font.decode_utf8("This"), origin_baseline: true) | |
canvas.translate(frag.width, 0) | |
frag = draw_fragment(canvas, canvas.font.decode_utf8(" is"), origin_baseline: true, | |
character_spacing: 2, text_rise: -3) | |
canvas.translate(frag.width, 0) | |
frag = draw_fragment(canvas, canvas.font.decode_utf8("some"), origin_baseline: true) | |
canvas.translate(frag.width, 0) | |
frag = draw_fragment(canvas, canvas.font.decode_utf8("text"), origin_baseline: true, | |
text_rise: 3, horizontal_scaling: 200) | |
doc.write("test.pdf") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment