Class: Fontisan::Tables::GlyphBuilder
- Inherits:
-
Object
- Object
- Fontisan::Tables::GlyphBuilder
- Defined in:
- lib/fontisan/tables/glyf/glyph_builder.rb
Overview
Builds binary TrueType glyph data from universal outline representation
GlyphBuilder converts the format-agnostic
Outline model into binary TrueType glyph format.
It handles both simple and compound glyphs with proper encoding:
Simple Glyphs:
- Converts universal outline to TrueType contours
- Uses
CurveConverterfor cubic→quadratic conversion - Delta-encodes coordinates for compact storage
- Applies flag compression with run-length encoding
- Calculates accurate bounding box
Compound Glyphs:
- Encodes component references
- Supports transformation matrices
- Handles positioning via points or offsets
Constant Summary collapse
- ON_CURVE_POINT =
Flag constants (matching SimpleGlyph)
0x01- X_SHORT_VECTOR =
0x02- Y_SHORT_VECTOR =
0x04- REPEAT_FLAG =
0x08- X_IS_SAME_OR_POSITIVE_X_SHORT =
0x10- Y_IS_SAME_OR_POSITIVE_Y_SHORT =
0x20- ARG_1_AND_2_ARE_WORDS =
Component flag constants (matching CompoundGlyph)
0x0001- ARGS_ARE_XY_VALUES =
0x0002- ROUND_XY_TO_GRID =
0x0004- WE_HAVE_A_SCALE =
0x0008- MORE_COMPONENTS =
0x0020- WE_HAVE_AN_X_AND_Y_SCALE =
0x0040- WE_HAVE_A_TWO_BY_TWO =
0x0080- WE_HAVE_INSTRUCTIONS =
0x0100- USE_MY_METRICS =
0x0200- OVERLAP_COMPOUND =
0x0400
Class Method Summary collapse
-
.build_compound_glyph(components, bbox, instructions: "".b) ⇒ String
Build a compound TrueType glyph.
-
.build_simple_glyph(outline, instructions: "".b) ⇒ String
Build a simple TrueType glyph from universal outline.
Class Method Details
.build_compound_glyph(components, bbox, instructions: "".b) ⇒ String
Build a compound TrueType glyph
Creates a compound glyph by referencing other glyphs with optional transformations. Each component can specify positioning and scaling.
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/fontisan/tables/glyf/glyph_builder.rb', line 104 def self.build_compound_glyph(components, bbox, instructions: "".b) raise ArgumentError, "components cannot be nil" if components.nil? unless components.is_a?(Array) raise ArgumentError, "components must be Array" end raise ArgumentError, "components cannot be empty" if components.empty? validate_bbox!(bbox) build_compound_glyph_data(components, bbox, instructions) end |
.build_simple_glyph(outline, instructions: "".b) ⇒ String
Build a simple TrueType glyph from universal outline
Converts the universal outline to TrueType format with:
- Quadratic curves (cubic curves converted via
CurveConverter) - Delta-encoded coordinates
- Flag compression
- Accurate bounding box
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/fontisan/tables/glyf/glyph_builder.rb', line 67 def self.build_simple_glyph(outline, instructions: "".b) raise ArgumentError, "outline cannot be nil" if outline.nil? raise ArgumentError, "outline must be Outline" unless outline.is_a?(Fontisan::Models::Outline) raise ArgumentError, "outline cannot be empty" if outline.empty? # Convert outline to TrueType contours contours = outline.to_truetype_contours raise ArgumentError, "no contours in outline" if contours.empty? # Calculate bounding box from contours (on-curve points only) bbox = calculate_bounding_box(contours) # Build binary data build_simple_glyph_data(contours, bbox, instructions) end |