Class: ZplRender::TrueType
- Inherits:
-
Object
- Object
- ZplRender::TrueType
- Defined in:
- lib/zpl_render/truetype.rb
Overview
Minimal pure-Ruby TrueType font loader and glyph rasterizer.
Parses just enough of a .ttf (head, maxp, cmap format 4, loca, glyf, hhea, hmtx, OS/2) to rasterize glyph outlines: quadratic contours are flattened to polygons and filled scanline-wise with the non-zero winding rule, sampling at pixel centers. Output is 1-bit, matching a thermal print head.
Defined Under Namespace
Classes: Glyph
Constant Summary collapse
- ON_CURVE =
0x01- X_SHORT =
0x02- Y_SHORT =
0x04- REPEAT =
0x08- X_SAME_OR_POS =
0x10- Y_SAME_OR_POS =
0x20- CURVE_SEGMENTS =
polyline segments per quadratic bezier
8
Instance Attribute Summary collapse
-
#cap_height ⇒ Object
readonly
Returns the value of attribute cap_height.
-
#units_per_em ⇒ Object
readonly
Returns the value of attribute units_per_em.
Class Method Summary collapse
Instance Method Summary collapse
-
#advance(code) ⇒ Object
Advance width in font units for a unicode codepoint.
-
#glyph_bitmap(code, scale_x, scale_y) ⇒ Object
Rasterize a glyph at the given x/y scales (pixels per font unit).
- #glyph_id(code) ⇒ Object
-
#initialize(path) ⇒ TrueType
constructor
A new instance of TrueType.
Constructor Details
#initialize(path) ⇒ TrueType
Returns a new instance of TrueType.
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/zpl_render/truetype.rb', line 33 def initialize(path) @data = File.binread(path) @tables = parse_table_directory parse_head parse_maxp parse_hhea_hmtx parse_cmap parse_os2 @glyph_cache = {} @contour_cache = {} end |
Instance Attribute Details
#cap_height ⇒ Object (readonly)
Returns the value of attribute cap_height.
23 24 25 |
# File 'lib/zpl_render/truetype.rb', line 23 def cap_height @cap_height end |
#units_per_em ⇒ Object (readonly)
Returns the value of attribute units_per_em.
23 24 25 |
# File 'lib/zpl_render/truetype.rb', line 23 def units_per_em @units_per_em end |
Class Method Details
.default ⇒ Object
25 26 27 |
# File 'lib/zpl_render/truetype.rb', line 25 def self.default @default ||= new(File.("fonts/LiberationSans-Bold.ttf", __dir__)) end |
.mono ⇒ Object
29 30 31 |
# File 'lib/zpl_render/truetype.rb', line 29 def self.mono @mono ||= new(File.("fonts/LiberationMono-Regular.ttf", __dir__)) end |
Instance Method Details
#advance(code) ⇒ Object
Advance width in font units for a unicode codepoint.
46 47 48 49 |
# File 'lib/zpl_render/truetype.rb', line 46 def advance(code) gid = glyph_id(code) @advances[gid] || @advances.last || @units_per_em / 2 end |
#glyph_bitmap(code, scale_x, scale_y) ⇒ Object
Rasterize a glyph at the given x/y scales (pixels per font unit). Returns a Glyph whose canvas is stamped at (pen_x + left, baseline_y - top). Returns nil for blank glyphs.
58 59 60 61 62 63 |
# File 'lib/zpl_render/truetype.rb', line 58 def glyph_bitmap(code, scale_x, scale_y) key = [code, (scale_x * 10_000).round, (scale_y * 10_000).round] return @glyph_cache[key] if @glyph_cache.key?(key) @glyph_cache[key] = rasterize(glyph_id(code), scale_x, scale_y) end |
#glyph_id(code) ⇒ Object
51 52 53 |
# File 'lib/zpl_render/truetype.rb', line 51 def glyph_id(code) @cmap_cache[code] ||= lookup_cmap(code) end |