Class: Fontisan::Models::Hint

Inherits:
Object
  • Object
show all
Defined in:
lib/fontisan/models/hint.rb

Overview

Universal hint representation supporting both TrueType and PostScript hints

Hints are instructions that improve font rendering at small sizes by providing information about how to align features to the pixel grid. This model provides a format-agnostic representation that can be converted between TrueType instructions and PostScript hint operators.

**Hint Types:**

  • ‘:stem` - Vertical or horizontal stem hints (PostScript hstem/vstem)

  • ‘:stem3` - Multiple stem hints (PostScript hstem3/vstem3)

  • ‘:flex` - Flex hints for smooth curves (PostScript flex)

  • ‘:counter` - Counter control hints (PostScript counter)

  • ‘:hint_replacement` - Hint replacement (PostScript hintmask)

  • ‘:delta` - Delta hints for pixel-level adjustments (TrueType DELTA)

  • ‘:interpolate` - Interpolation hints (TrueType IUP)

  • ‘:shift` - Shift hints (TrueType SHP)

  • ‘:align` - Alignment hints (TrueType ALIGNRP)

Examples:

Creating a stem hint

hint = Fontisan::Models::Hint.new(
  type: :stem,
  data: { position: 100, width: 50, orientation: :vertical }
)

Converting to TrueType

tt_instructions = hint.to_truetype

Converting to PostScript

ps_operators = hint.to_postscript

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, data:, source_format: nil) ⇒ Hint

Initialize a new hint

Parameters:

  • type (Symbol)

    Hint type

  • data (Hash)

    Hint-specific data

  • source_format (Symbol) (defaults to: nil)

    Source format (optional)



179
180
181
182
183
# File 'lib/fontisan/models/hint.rb', line 179

def initialize(type:, data:, source_format: nil)
  @type = type
  @data = data
  @source_format = source_format
end

Instance Attribute Details

#dataHash (readonly)

Returns Hint-specific data.

Returns:

  • (Hash)

    Hint-specific data



169
170
171
# File 'lib/fontisan/models/hint.rb', line 169

def data
  @data
end

#source_formatSymbol (readonly)

Returns Source format (:truetype or :postscript).

Returns:

  • (Symbol)

    Source format (:truetype or :postscript)



172
173
174
# File 'lib/fontisan/models/hint.rb', line 172

def source_format
  @source_format
end

#typeSymbol (readonly)

Returns Hint type.

Returns:

  • (Symbol)

    Hint type



166
167
168
# File 'lib/fontisan/models/hint.rb', line 166

def type
  @type
end

Instance Method Details

#compatible_with?(format) ⇒ Boolean

Check if hint is compatible with target format

Parameters:

  • format (Symbol)

    Target format (:truetype or :postscript)

Returns:

  • (Boolean)

    True if compatible



255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/fontisan/models/hint.rb', line 255

def compatible_with?(format)
  case format
  when :truetype
    # Most PostScript hints can be converted to TrueType
    %i[stem flex counter delta interpolate shift align].include?(type)
  when :postscript
    # Most TrueType hints can be approximated in PostScript
    %i[stem stem3 flex counter hint_replacement].include?(type)
  else
    false
  end
end

#to_postscriptHash

Convert hint to PostScript hint format

Returns:

  • (Hash)

    PostScript hint operators and arguments



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/fontisan/models/hint.rb', line 225

def to_postscript
  case type
  when :stem
    convert_stem_to_postscript
  when :stem3
    convert_stem3_to_postscript
  when :flex
    convert_flex_to_postscript
  when :counter
    convert_counter_to_postscript
  when :hint_replacement
    # Hintmask operator
    { operator: :hintmask, args: data[:mask] || [] }
  when :delta, :interpolate, :shift, :align
    # TrueType-specific hints don't have direct PS equivalents
    # Return approximation using stem hints
    approximate_as_postscript
  else
    # Unknown hint type
    {}
  end
rescue StandardError => e
  warn "Error converting hint type #{type} to PostScript: #{e.message}"
  {}
end

#to_truetypeArray<Integer>

Convert hint to TrueType instruction format

Returns:

  • (Array<Integer>)

    TrueType instruction bytes



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/fontisan/models/hint.rb', line 188

def to_truetype
  case type
  when :stem
    convert_stem_to_truetype
  when :stem3
    convert_stem3_to_truetype
  when :flex
    convert_flex_to_truetype
  when :counter
    convert_counter_to_truetype
  when :hint_replacement
    convert_hintmask_to_truetype
  when :delta
    # Already in TrueType format
    data[:instructions] || []
  when :interpolate
    # IUP instruction
    axis = data[:axis] || :y
    axis == :x ? [0x31] : [0x30] # IUP[x] or IUP[y]
  when :shift
    # SHP instruction
    data[:instructions] || []
  when :align
    # ALIGNRP instruction
    [0x3C]
  else
    # Unknown hint type - return empty
    []
  end
rescue StandardError => e
  warn "Error converting hint type #{type} to TrueType: #{e.message}"
  []
end