Class: Fontisan::Tables::GlyphBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/fontisan/tables/glyf/glyph_builder.rb

Overview

Builds binary TrueType glyph data from universal outline representation

[‘GlyphBuilder`](lib/fontisan/tables/glyf/glyph_builder.rb) converts the format-agnostic [`Outline`](lib/fontisan/models/outline.rb) 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 [‘CurveConverter`](lib/fontisan/tables/glyf/curve_converter.rb) for 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

Examples:

Building a simple glyph from outline

outline = Fontisan::Models::Outline.new(...)
binary_data = Fontisan::Tables::GlyphBuilder.build_simple_glyph(outline)

Building a compound glyph

components = [
  { glyph_index: 10, x_offset: 100, y_offset: 0 },
  { glyph_index: 20, x_offset: 300, y_offset: 0 }
]
bbox = { x_min: 0, y_min: 0, x_max: 500, y_max: 700 }
binary_data = Fontisan::Tables::GlyphBuilder.build_compound_glyph(components, bbox)

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

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.

Parameters:

  • components (Array<Hash>)

    Component descriptions Each component hash can contain:

    • ‘:glyph_index` (Integer, required): Referenced glyph ID

    • ‘:x_offset` (Integer): X offset (default: 0)

    • ‘:y_offset` (Integer): Y offset (default: 0)

    • ‘:scale` (Float): Uniform scale (optional)

    • ‘:scale_x` (Float): X-axis scale (optional)

    • ‘:scale_y` (Float): Y-axis scale (optional)

    • ‘:scale_01` (Float): Matrix element (0,1) (optional)

    • ‘:scale_10` (Float): Matrix element (1,0) (optional)

    • ‘:use_my_metrics` (Boolean): Use component’s metrics (default: false)

    • ‘:overlap` (Boolean): Mark as overlapping (default: false)

  • bbox (Hash)

    Bounding box :y_min, :x_max, :y_max

  • instructions (String) (defaults to: "".b)

    Optional TrueType instructions (default: empty)

Returns:

  • (String)

    Binary glyph data

Raises:

  • (ArgumentError)

    If parameters are invalid



107
108
109
110
111
112
113
114
115
# File 'lib/fontisan/tables/glyf/glyph_builder.rb', line 107

def self.build_compound_glyph(components, bbox, instructions: "".b)
  raise ArgumentError, "components cannot be nil" if components.nil?
  raise ArgumentError, "components must be Array" unless components.is_a?(Array)
  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`](lib/fontisan/tables/glyf/curve_converter.rb))

  • Delta-encoded coordinates

  • Flag compression

  • Accurate bounding box

Parameters:

  • outline (Fontisan::Models::Outline)

    Universal outline

  • instructions (String) (defaults to: "".b)

    Optional TrueType instructions (default: empty)

Returns:

  • (String)

    Binary glyph data

Raises:

  • (ArgumentError)

    If outline is invalid or empty



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fontisan/tables/glyf/glyph_builder.rb', line 70

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
  bbox = calculate_bounding_box(contours)

  # Build binary data
  build_simple_glyph_data(contours, bbox, instructions)
end