Class: Fontisan::Tables::GlyfTable

Inherits:
SfntTable
  • Object
show all
Defined in:
lib/fontisan/tables/glyf_table.rb

Overview

OOP representation of the ‘glyf’ (Glyph Data) table

The glyf table contains TrueType glyph outline data. Each glyph is described by either a simple glyph (with contours and points) or a compound glyph (composed of other glyphs with transformations).

This class extends SfntTable to provide glyf-specific convenience methods for accessing glyph data. The glyf table requires context from the loca and head tables to function properly.

Examples:

Accessing glyphs

glyf = font.sfnt_table("glyf")
loca = font.table("loca")
head = font.table("head")

glyf.parse_with_context(loca, head)
glyph = glyf.glyph_for(42)  # Get glyph by ID
glyph.simple?  # => true or false
glyph.bounding_box  # => [xMin, yMin, xMax, yMax]

Instance Attribute Summary collapse

Attributes inherited from SfntTable

#data, #entry, #font, #parsed

Instance Method Summary collapse

Methods inherited from SfntTable

#available?, #calculate_checksum, #checksum, #data_loaded?, #human_name, #initialize, #inspect, #length, #load_data!, #offset, #parse, #parsed?, #required?, #tag, #to_s, #validate!

Constructor Details

This class inherits a constructor from Fontisan::SfntTable

Instance Attribute Details

#head_tableObject (readonly)

Cache for context tables



29
30
31
# File 'lib/fontisan/tables/glyf_table.rb', line 29

def head_table
  @head_table
end

#loca_tableObject (readonly)

Cache for context tables



29
30
31
# File 'lib/fontisan/tables/glyf_table.rb', line 29

def loca_table
  @loca_table
end

Instance Method Details

#all_glyphs_accessible?(num_glyphs) ⇒ Boolean

Validate all glyphs are accessible

Parameters:

  • num_glyphs (Integer)

    Total number of glyphs to check

Returns:

  • (Boolean)

    true if all glyphs can be accessed



159
160
161
162
163
164
# File 'lib/fontisan/tables/glyf_table.rb', line 159

def all_glyphs_accessible?(num_glyphs)
  ensure_context!
  return false unless parsed

  parsed.all_glyphs_accessible?(@loca_table, @head_table, num_glyphs)
end

#cache_sizeInteger

Get glyph cache size

Returns:

  • (Integer)

    Number of cached glyphs



140
141
142
143
144
# File 'lib/fontisan/tables/glyf_table.rb', line 140

def cache_size
  return 0 unless parsed

  parsed.cache_size
end

#clear_cachevoid

This method returns an undefined value.

Clear glyph cache



149
150
151
152
153
# File 'lib/fontisan/tables/glyf_table.rb', line 149

def clear_cache
  return unless parsed

  parsed.clear_cache
end

#compound_glyph?(glyph_id) ⇒ Boolean

Check if a glyph is compound

Parameters:

  • glyph_id (Integer)

    Glyph ID

Returns:

  • (Boolean)

    true if glyph is compound



89
90
91
92
93
94
# File 'lib/fontisan/tables/glyf_table.rb', line 89

def compound_glyph?(glyph_id)
  glyph = glyph_for(glyph_id)
  return false if glyph.nil?

  glyph.respond_to?(:num_contours) && glyph.num_contours == -1
end

#empty_glyph?(glyph_id) ⇒ Boolean

Check if a glyph is empty

Parameters:

  • glyph_id (Integer)

    Glyph ID

Returns:

  • (Boolean)

    true if glyph has no data



100
101
102
# File 'lib/fontisan/tables/glyf_table.rb', line 100

def empty_glyph?(glyph_id)
  glyph_for(glyph_id).nil?
end

#glyph_bounding_box(glyph_id) ⇒ Array<Integer>?

Get glyph bounding box

Parameters:

  • glyph_id (Integer)

    Glyph ID

Returns:

  • (Array<Integer>, nil)
    xMin, yMin, xMax, yMax

    or nil



108
109
110
111
112
113
# File 'lib/fontisan/tables/glyf_table.rb', line 108

def glyph_bounding_box(glyph_id)
  glyph = glyph_for(glyph_id)
  return nil if glyph.nil?

  [glyph.x_min, glyph.y_min, glyph.x_max, glyph.y_max]
end

#glyph_contour_count(glyph_id) ⇒ Integer?

Get number of contours for a glyph

Parameters:

  • glyph_id (Integer)

    Glyph ID

Returns:

  • (Integer, nil)

    Number of contours, or nil



119
120
121
122
123
124
# File 'lib/fontisan/tables/glyf_table.rb', line 119

def glyph_contour_count(glyph_id)
  glyph = glyph_for(glyph_id)
  return nil if glyph.nil?

  glyph.num_contours if glyph.respond_to?(:num_contours)
end

#glyph_for(glyph_id) ⇒ SimpleGlyph, ...

Get a glyph by ID

Parameters:

  • glyph_id (Integer)

    Glyph ID (0-based, 0 is .notdef)

Returns:

Raises:

  • (ArgumentError)

    if context not set



67
68
69
70
71
72
# File 'lib/fontisan/tables/glyf_table.rb', line 67

def glyph_for(glyph_id)
  ensure_context!
  return nil unless parsed

  parsed.glyph_for(glyph_id, @loca_table, @head_table)
end

#glyph_point_count(glyph_id) ⇒ Integer?

Get number of points for a simple glyph

Parameters:

  • glyph_id (Integer)

    Glyph ID

Returns:

  • (Integer, nil)

    Number of points, or nil



130
131
132
133
134
135
# File 'lib/fontisan/tables/glyf_table.rb', line 130

def glyph_point_count(glyph_id)
  glyph = glyph_for(glyph_id)
  return nil if glyph.nil? || !glyph.respond_to?(:num_points)

  glyph.num_points
end

#has_context?Boolean

Check if table has been parsed with context

Returns:

  • (Boolean)

    true if context is available



58
59
60
# File 'lib/fontisan/tables/glyf_table.rb', line 58

def has_context?
  !@loca_table.nil? && !@head_table.nil?
end

#instructions_sound?(num_glyphs) ⇒ Boolean

Validate glyphs have sound instructions

Parameters:

  • num_glyphs (Integer)

    Total number of glyphs to check

Returns:

  • (Boolean)

    true if all instructions are valid



192
193
194
195
196
197
# File 'lib/fontisan/tables/glyf_table.rb', line 192

def instructions_sound?(num_glyphs)
  ensure_context!
  return false unless parsed

  parsed.instructions_sound?(@loca_table, @head_table, num_glyphs)
end

#no_clipped_glyphs?(num_glyphs) ⇒ Boolean

Validate no glyphs are clipped

Parameters:

  • num_glyphs (Integer)

    Total number of glyphs to check

Returns:

  • (Boolean)

    true if no glyphs exceed font bounds



170
171
172
173
174
175
# File 'lib/fontisan/tables/glyf_table.rb', line 170

def no_clipped_glyphs?(num_glyphs)
  ensure_context!
  return false unless parsed

  parsed.no_clipped_glyphs?(@loca_table, @head_table, num_glyphs)
end

#parse_with_context(loca, head) ⇒ self

Parse the glyf table with required context

The glyf table cannot be used without context from loca and head tables. This method must be called before accessing glyph data.

Parameters:

Returns:

  • (self)

    Returns self for chaining

Raises:

  • (ArgumentError)

    if context is invalid



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fontisan/tables/glyf_table.rb', line 40

def parse_with_context(loca, head)
  unless loca && head
    raise ArgumentError,
          "glyf table requires both loca and head tables as context"
  end

  @loca_table = loca
  @head_table = head

  # Ensure parsed data is loaded
  parse

  self
end

#simple_glyph?(glyph_id) ⇒ Boolean

Check if a glyph is simple (has contours)

Parameters:

  • glyph_id (Integer)

    Glyph ID

Returns:

  • (Boolean)

    true if glyph is simple



78
79
80
81
82
83
# File 'lib/fontisan/tables/glyf_table.rb', line 78

def simple_glyph?(glyph_id)
  glyph = glyph_for(glyph_id)
  return false if glyph.nil?

  glyph.respond_to?(:num_contours) && glyph.num_contours >= 0
end

#statistics(num_glyphs) ⇒ Hash

Get glyph statistics

Parameters:

  • num_glyphs (Integer)

    Total number of glyphs

Returns:

  • (Hash)

    Statistics about the glyphs



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/fontisan/tables/glyf_table.rb', line 203

def statistics(num_glyphs)
  ensure_context!
  return {} unless parsed

  simple_count = 0
  compound_count = 0
  empty_count = 0

  (0...num_glyphs).each do |gid|
    if empty_glyph?(gid)
      empty_count += 1
    elsif simple_glyph?(gid)
      simple_count += 1
    elsif compound_glyph?(gid)
      compound_count += 1
    end
  end

  {
    total_glyphs: num_glyphs,
    simple_glyphs: simple_count,
    compound_glyphs: compound_count,
    empty_glyphs: empty_count,
    cached_glyphs: cache_size,
  }
end

#valid_contour_count?(glyph_id) ⇒ Boolean

Validate all glyphs have valid contour counts

Parameters:

  • glyph_id (Integer)

    Glyph ID to check

Returns:

  • (Boolean)

    true if contour count is valid



181
182
183
184
185
186
# File 'lib/fontisan/tables/glyf_table.rb', line 181

def valid_contour_count?(glyph_id)
  ensure_context!
  return false unless parsed

  parsed.valid_contour_count?(glyph_id, @loca_table, @head_table)
end