Class: Fontisan::Models::GlyphOutline
- Inherits:
-
Object
- Object
- Fontisan::Models::GlyphOutline
- Defined in:
- lib/fontisan/models/glyph_outline.rb
Overview
Represents a glyph’s outline data with conversion capabilities
[‘GlyphOutline`](lib/fontisan/models/glyph_outline.rb) is a pure data model that stores glyph outline information extracted from font tables. It provides methods to convert the outline data to various formats (SVG paths, drawing commands) for rendering and manipulation.
The outline consists of:
-
Contours: Array of closed paths, each containing points
-
Points: All points from all contours (flattened for easy access)
-
Bounding box: The glyph’s bounding rectangle
-
Glyph ID: The identifier of this glyph
This class is immutable after construction to ensure data integrity.
Reference: [‘docs/GETTING_STARTED.md:66-121`](docs/GETTING_STARTED.md:66)
Instance Attribute Summary collapse
-
#bbox ⇒ Hash
readonly
Bounding box with keys: :x_min, :y_min, :x_max, :y_max.
-
#contours ⇒ Array<Array<Hash>>
readonly
Array of contours, each containing points Each point hash has keys: :x, :y, :on_curve.
-
#glyph_id ⇒ Integer
readonly
The glyph identifier.
-
#points ⇒ Array<Hash>
readonly
All points from all contours (flattened).
Instance Method Summary collapse
-
#contour_count ⇒ Integer
Number of contours in outline.
-
#empty? ⇒ Boolean
Check if outline is empty (e.g., space glyph).
-
#initialize(glyph_id:, contours:, bbox:) ⇒ GlyphOutline
constructor
Initialize a new glyph outline.
-
#point_count ⇒ Integer
Number of points in outline.
-
#to_commands ⇒ Array<Array>
Convert to drawing commands.
-
#to_s ⇒ String
(also: #inspect)
String representation for debugging.
-
#to_svg_path ⇒ String
Convert outline to SVG path data.
Constructor Details
#initialize(glyph_id:, contours:, bbox:) ⇒ GlyphOutline
Initialize a new glyph outline
63 64 65 66 67 68 69 70 |
# File 'lib/fontisan/models/glyph_outline.rb', line 63 def initialize(glyph_id:, contours:, bbox:) validate_parameters!(glyph_id, contours, bbox) @glyph_id = glyph_id.freeze @contours = deep_freeze(contours) @points = extract_all_points(contours).freeze @bbox = bbox.freeze end |
Instance Attribute Details
#bbox ⇒ Hash (readonly)
Returns Bounding box with keys: :x_min, :y_min, :x_max, :y_max.
54 55 56 |
# File 'lib/fontisan/models/glyph_outline.rb', line 54 def bbox @bbox end |
#contours ⇒ Array<Array<Hash>> (readonly)
Returns Array of contours, each containing points Each point hash has keys: :x, :y, :on_curve.
48 49 50 |
# File 'lib/fontisan/models/glyph_outline.rb', line 48 def contours @contours end |
#glyph_id ⇒ Integer (readonly)
Returns The glyph identifier.
44 45 46 |
# File 'lib/fontisan/models/glyph_outline.rb', line 44 def glyph_id @glyph_id end |
#points ⇒ Array<Hash> (readonly)
Returns All points from all contours (flattened).
51 52 53 |
# File 'lib/fontisan/models/glyph_outline.rb', line 51 def points @points end |
Instance Method Details
#contour_count ⇒ Integer
Number of contours in outline
138 139 140 |
# File 'lib/fontisan/models/glyph_outline.rb', line 138 def contour_count contours.length end |
#empty? ⇒ Boolean
Check if outline is empty (e.g., space glyph)
124 125 126 |
# File 'lib/fontisan/models/glyph_outline.rb', line 124 def empty? contours.empty? end |
#point_count ⇒ Integer
Number of points in outline
131 132 133 |
# File 'lib/fontisan/models/glyph_outline.rb', line 131 def point_count points.length end |
#to_commands ⇒ Array<Array>
Convert to drawing commands
Returns an array of drawing command arrays that can be used to render the glyph. Each command is an array with the command type as the first element and coordinates as subsequent elements.
Command types:
-
:move_to - Move to a point without drawing
-
:line_to - Draw a straight line to a point
-
:curve_to - Draw a quadratic Bézier curve (TrueType) or cubic curve (CFF)
-
:close_path - Close the current path
111 112 113 114 115 116 117 118 119 |
# File 'lib/fontisan/models/glyph_outline.rb', line 111 def to_commands return [] if empty? commands = [] contours.each do |contour| commands.concat(build_contour_commands(contour)) end commands end |
#to_s ⇒ String Also known as: inspect
String representation for debugging
145 146 147 148 149 |
# File 'lib/fontisan/models/glyph_outline.rb', line 145 def to_s "#<#{self.class.name} glyph_id=#{glyph_id} " \ "contours=#{contour_count} points=#{point_count} " \ "bbox=#{bbox.inspect}>" end |
#to_svg_path ⇒ String
Convert outline to SVG path data
Generates SVG path commands from the outline contours. Each contour becomes a closed path, with move_to for the first point, line_to or curve_to for subsequent points, and an explicit close path.
79 80 81 82 83 84 85 86 87 |
# File 'lib/fontisan/models/glyph_outline.rb', line 79 def to_svg_path return "" if empty? path_parts = contours.map do |contour| build_contour_path(contour) end path_parts.join(" ") end |