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)



50
51
52
53
54
# File 'lib/fontisan/models/hint.rb', line 50

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



40
41
42
# File 'lib/fontisan/models/hint.rb', line 40

def data
  @data
end

#source_formatSymbol (readonly)

Returns Source format (:truetype or :postscript).

Returns:

  • (Symbol)

    Source format (:truetype or :postscript)



43
44
45
# File 'lib/fontisan/models/hint.rb', line 43

def source_format
  @source_format
end

#typeSymbol (readonly)

Returns Hint type.

Returns:

  • (Symbol)

    Hint type



37
38
39
# File 'lib/fontisan/models/hint.rb', line 37

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



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/fontisan/models/hint.rb', line 115

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



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/fontisan/models/hint.rb', line 88

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
end

#to_truetypeArray<Integer>

Convert hint to TrueType instruction format

Returns:

  • (Array<Integer>)

    TrueType instruction bytes



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fontisan/models/hint.rb', line 59

def to_truetype
  case type
  when :stem
    convert_stem_to_truetype
  when :flex
    convert_flex_to_truetype
  when :counter
    convert_counter_to_truetype
  when :delta
    # Already in TrueType format
    data[:instructions] || []
  when :interpolate
    # IUP instruction
    [0x30] # IUP[y], or [0x31] for IUP[x]
  when :shift
    # SHP instruction
    data[:instructions] || []
  when :align
    # ALIGNRP instruction
    [0x3C]
  else
    # Unknown hint type - return empty
    []
  end
end