Class: Fontisan::Hints::TrueTypeInstructionAnalyzer
- Inherits:
-
Object
- Object
- Fontisan::Hints::TrueTypeInstructionAnalyzer
- Defined in:
- lib/fontisan/hints/truetype_instruction_analyzer.rb
Overview
Analyzes TrueType bytecode instructions to extract hint parameters
This analyzer parses fpgm (Font Program) and prep (Control Value Program) bytecode to extract semantic hint information that can be converted to PostScript Private dict parameters.
Key Extracted Parameters:
- Blue zones (alignment zones for baseline, x-height, cap-height)
- Stem widths (from CVT setup in prep)
- Delta base and shift values
- Twilight zone setup
Constant Summary collapse
- NPUSHB =
TrueType instruction opcodes relevant for hint extraction
0x40- NPUSHW =
Push N bytes
0x41- PUSHB =
Push N words
(0xB0..0xB7).to_a
- PUSHW =
Push 1-8 words
(0xB8..0xBF).to_a
- SVTCA_Y =
Set freedom and projection vectors to Y-axis
0x00- SVTCA_X =
Set freedom and projection vectors to X-axis
0x01- RCVT =
Read CVT
0x45- WCVTP =
Write CVT (in Pixels)
0x44- WCVTF =
Write CVT (in FUnits)
0x70- MDAP =
Move Direct Absolute Point
[0x2E, 0x2F].freeze
- SCVTCI =
Set Control Value Table Cut In
0x1D- SSWCI =
Set Single Width Cut In
0x1E- SSW =
Set Single Width
0x1F
Instance Method Summary collapse
-
#analyze_fpgm(fpgm) ⇒ Hash
Analyze Font Program (fpgm) for complexity indicators.
-
#analyze_prep(prep, cvt = []) ⇒ Hash
Analyze prep program to extract hint parameters.
-
#extract_blue_zones_from_cvt(cvt) ⇒ Hash
Extract blue zones from CVT values using heuristics.
Instance Method Details
#analyze_fpgm(fpgm) ⇒ Hash
Analyze Font Program (fpgm) for complexity indicators
The fpgm contains font-level function definitions. While we don't fully decompile it, we can extract useful metadata about hint complexity.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/fontisan/hints/truetype_instruction_analyzer.rb', line 155 def analyze_fpgm(fpgm) return {} if fpgm.nil? || fpgm.empty? size = fpgm.bytesize # Estimate complexity based on size complexity = if size < 100 :simple elsif size < 200 :moderate else :complex end { fpgm_size: size, has_functions: size.positive?, complexity: complexity, } rescue StandardError # Return empty hash on any error {} end |
#analyze_prep(prep, cvt = []) ⇒ Hash
Analyze prep program to extract hint parameters
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/fontisan/hints/truetype_instruction_analyzer.rb', line 42 def analyze_prep(prep, cvt = []) return {} if prep.nil? && (cvt.nil? || cvt.empty?) params = {} # Parse prep bytecode if present if prep && !prep.empty? bytes = prep.bytes stack = [] i = 0 while i < bytes.length opcode = bytes[i] case opcode when NPUSHB # Push N bytes i += 1 count = bytes[i] i += 1 count.times do stack.push(bytes[i]) i += 1 end next when NPUSHW # Push N words (16-bit values) i += 1 count = bytes[i] i += 1 count.times do value = (bytes[i] << 8) | bytes[i + 1] # Convert to signed value = value - 65536 if value > 32767 stack.push(value) i += 2 end next when *PUSHB # Push 1-8 bytes count = opcode - 0xB0 + 1 i += 1 count.times do stack.push(bytes[i]) i += 1 end next when *PUSHW # Push 1-8 words count = opcode - 0xB8 + 1 i += 1 count.times do value = (bytes[i] << 8) | bytes[i + 1] value = value - 65536 if value > 32767 stack.push(value) i += 2 end next when WCVTP, WCVTF # Write to CVT - this shows which CVT indices are being set up # Pattern: value cvt_index WCVTP (stack top to bottom) if stack.length >= 2 value = stack.pop stack.pop # Track CVT modifications (useful for understanding setup) end when SSW # Set Single Width - used for stem width control if stack.length >= 1 width = stack.pop params[:single_width] = width unless params[:single_width] end when SSWCI # Set Single Width Cut In if stack.length >= 1 params[:single_width_cut_in] = stack.pop end when SCVTCI # Set CVT Cut In if stack.length >= 1 params[:cvt_cut_in] = stack.pop end end i += 1 end end # Extract blue zones from CVT analysis (always do this if CVT is present) if cvt && !cvt.empty? params.merge!(extract_blue_zones_from_cvt(cvt)) end params rescue StandardError => e warn "Error analyzing prep program: #{e.}" {} end |
#extract_blue_zones_from_cvt(cvt) ⇒ Hash
Extract blue zones from CVT values using heuristics
Blue zones in PostScript define alignment constraints for baseline, x-height, cap-height, ascender, and descender. TrueType doesn't have explicit blue zones, but we can derive them from CVT values using common patterns.
Heuristics:
- Negative values near -250 to -200: Descender zones
- Values near 0: Baseline zones
- Values near 500-550: X-height zones
- Values near 700-750: Cap-height zones
- For large UPM (>2000): Scale thresholds proportionally
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 221 222 223 224 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 250 251 252 |
# File 'lib/fontisan/hints/truetype_instruction_analyzer.rb', line 195 def extract_blue_zones_from_cvt(cvt) return {} if cvt.nil? || cvt.empty? zones = {} # Detect scale from maximum absolute value max_value = cvt.map(&:abs).max scale_factor = max_value > 1000 ? (max_value / 1000.0) : 1.0 # Scaled thresholds descender_min = (-300 * scale_factor).to_i descender_max = (-150 * scale_factor).to_i baseline_range = (50 * scale_factor).to_i xheight_min = (450 * scale_factor).to_i xheight_max = (600 * scale_factor).to_i capheight_min = (650 * scale_factor).to_i capheight_max = (1500 * scale_factor).to_i # Wider range for large UPM # Group CVT values by typical alignment zones descender_values = cvt.select do |v| v < descender_max && v > descender_min end baseline_values = cvt.select do |v| v >= -baseline_range && v <= baseline_range end cvt.select { |v| v >= xheight_min && v <= xheight_max } capheight_values = cvt.select do |v| v >= capheight_min && v <= capheight_max end # Build blue_values (baseline and top zones) blue_values = [] # Add baseline zone if detected if baseline_values.any? min_baseline = baseline_values.min max_baseline = baseline_values.max blue_values << min_baseline << max_baseline end # Add cap-height zone if detected (or any top zone for large UPM) if capheight_values.any? min_cap = capheight_values.min max_cap = capheight_values.max blue_values << min_cap << max_cap end zones[:blue_values] = blue_values unless blue_values.empty? # Build other_blues (descender zones) if descender_values.any? min_desc = descender_values.min max_desc = descender_values.max zones[:other_blues] = [min_desc, max_desc] end zones end |