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



26
27
28
# File 'lib/fontisan/tables/glyf_table.rb', line 26

def head_table
  @head_table
end

#loca_tableObject (readonly)

Cache for context tables



26
27
28
# File 'lib/fontisan/tables/glyf_table.rb', line 26

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



156
157
158
159
160
161
# File 'lib/fontisan/tables/glyf_table.rb', line 156

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



137
138
139
140
141
# File 'lib/fontisan/tables/glyf_table.rb', line 137

def cache_size
  return 0 unless parsed

  parsed.cache_size
end

#clear_cachevoid

This method returns an undefined value.

Clear glyph cache



146
147
148
149
150
# File 'lib/fontisan/tables/glyf_table.rb', line 146

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



86
87
88
89
90
91
# File 'lib/fontisan/tables/glyf_table.rb', line 86

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



97
98
99
# File 'lib/fontisan/tables/glyf_table.rb', line 97

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



105
106
107
108
109
110
# File 'lib/fontisan/tables/glyf_table.rb', line 105

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



116
117
118
119
120
121
# File 'lib/fontisan/tables/glyf_table.rb', line 116

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



64
65
66
67
68
69
# File 'lib/fontisan/tables/glyf_table.rb', line 64

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



127
128
129
130
131
132
# File 'lib/fontisan/tables/glyf_table.rb', line 127

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



55
56
57
# File 'lib/fontisan/tables/glyf_table.rb', line 55

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



189
190
191
192
193
194
# File 'lib/fontisan/tables/glyf_table.rb', line 189

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



167
168
169
170
171
172
# File 'lib/fontisan/tables/glyf_table.rb', line 167

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fontisan/tables/glyf_table.rb', line 37

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



75
76
77
78
79
80
# File 'lib/fontisan/tables/glyf_table.rb', line 75

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



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

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



178
179
180
181
182
183
# File 'lib/fontisan/tables/glyf_table.rb', line 178

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

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