fontisan 0.4.49-x86_64-linux

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (792) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/.rubocop.yml +141 -0
  4. data/.rubocop_todo.yml +433 -0
  5. data/BENCHMARKS.md +56 -0
  6. data/BUG-stitcher-drops-isolated-cps.md +58 -0
  7. data/BUG-stitcher-drops-plane1-codepoints.md +310 -0
  8. data/BUG-stitcher-gid-cap-65535.md +110 -0
  9. data/CHANGELOG.md +293 -0
  10. data/Gemfile +40 -0
  11. data/LICENSE +28 -0
  12. data/README.adoc +2811 -0
  13. data/Rakefile +156 -0
  14. data/TODO.bug-fixes/01-cff-expert-charsets.md +27 -0
  15. data/TODO.bug-fixes/02-cff2-subroutine-stubs.md +32 -0
  16. data/TODO.bug-fixes/03-variation-subsetter-placeholders.md +42 -0
  17. data/TODO.bug-fixes/04-ufo-image-compile-roundtrip.md +28 -0
  18. data/TODO.bug-fixes/05-stale-cff-comment.md +18 -0
  19. data/TODO.bug-fixes/06-instance-woff2-output.md +24 -0
  20. data/TODO.bug-fixes/07-encapsulation-violations.md +38 -0
  21. data/TODO.bug-fixes/08-respond-to-violations.md +25 -0
  22. data/TODO.bug-fixes/09-type1-cff-seac-expansion.md +22 -0
  23. data/TODO.bug-fixes/10-ttf-type1-composite-glyphs.md +23 -0
  24. data/TODO.bug-fixes/11-remaining-respond-to.md +25 -0
  25. data/TODO.bug-fixes/12-type1-generators-duck-typing.md +80 -0
  26. data/TODO.bug-fixes/13-hint-extractor-duck-typing.md +30 -0
  27. data/TODO.bug-fixes/14-font-interface-type-checks.md +82 -0
  28. data/TODO.bug-fixes/15-glyph-type-checks-and-simple-glyph-points-bug.md +49 -0
  29. data/TODO.bug-fixes/16-variation-table-checks.md +31 -0
  30. data/TODO.bug-fixes/17-ufo-and-stitcher-cleanup.md +32 -0
  31. data/TODO.bug-fixes/18-generic-value-and-io-checks.md +56 -0
  32. data/TODO.bug-fixes/19-serialization-migration.md +52 -0
  33. data/TODO.bug-fixes/20-variation-validator-type-checks.md +29 -0
  34. data/TODO.bug-fixes/21-xml-builder-send.md +24 -0
  35. data/TODO.bug-fixes/22-type1-converter-font-info.md +25 -0
  36. data/TODO.bug-fixes/23-boundary-type-checks.md +31 -0
  37. data/TODO.bug-fixes/24-validator-dynamic-field-key.md +25 -0
  38. data/TODO.bug-fixes/25-serialization-migration.md +49 -0
  39. data/TODO.bug-fixes/26-variation-preserver-spec-migration.md +25 -0
  40. data/TODO.bug-fixes/27-table-class-registry.md +50 -0
  41. data/TODO.bug-fixes/28-spec-doubles-cleanup.md +40 -0
  42. data/TODO.bug-fixes/29-strategy-registries.md +30 -0
  43. data/TODO.bug-fixes/30-eliminate-send-in-specs.md +36 -0
  44. data/TODO.bug-fixes/README.md +52 -0
  45. data/TODO.improvements/01-cbdt-cblc-gid-stable-propagation.md +73 -0
  46. data/TODO.improvements/02-collection-outline-priority.md +48 -0
  47. data/TODO.improvements/03-fontisan-audit-command.md +219 -0
  48. data/TODO.improvements/04-ufo-composite-glyph-encoding.md +46 -0
  49. data/TODO.improvements/05-otf-compiler-real-cff.md +63 -0
  50. data/TODO.improvements/06-cff2-blend-vsindex-operators.md +51 -0
  51. data/TODO.improvements/07-cpal-v1-header-fields.md +71 -0
  52. data/TODO.improvements/08-cff-standard-string-table.md +52 -0
  53. data/TODO.improvements/09-type1-seac-expansion.md +57 -0
  54. data/TODO.improvements/10-ufo-image-set-feature-writers.md +71 -0
  55. data/TODO.improvements/11-kern-groups-plist.md +49 -0
  56. data/TODO.improvements/12-cbdt-fixture-bindata-conversion.md +68 -0
  57. data/TODO.improvements/13-split-octokit-fetcher.md +45 -0
  58. data/TODO.improvements/14-rubocop-baseline-chip.md +62 -0
  59. data/TODO.improvements/15-cff-cff2-subsetter-strategy.md +78 -0
  60. data/TODO.improvements/README.md +58 -0
  61. data/benchmark/baseline.json +5 -0
  62. data/benchmark/ci_gate.rb +217 -0
  63. data/benchmark/compile_benchmark.rb +78 -0
  64. data/benchmark/corpus/harness.rb +175 -0
  65. data/benchmark/corpus/ops.rb +150 -0
  66. data/benchmark/corpus/run.rb +182 -0
  67. data/benchmark/corpus/scan_child.rb +8 -0
  68. data/benchmark/corpus/tiers.rb +176 -0
  69. data/benchmark/tier_baseline.json +12 -0
  70. data/benchmark/variation_quick_bench.rb +47 -0
  71. data/build_mingw32_stub.rb +15 -0
  72. data/docs/.gitignore +17 -0
  73. data/docs/.vitepress/config.ts +373 -0
  74. data/docs/.vitepress/theme/components/ApiMethod.vue +127 -0
  75. data/docs/.vitepress/theme/components/Badge.vue +51 -0
  76. data/docs/.vitepress/theme/components/FeatureComparison.vue +87 -0
  77. data/docs/.vitepress/theme/components/HeroCodeBlock.vue +98 -0
  78. data/docs/.vitepress/theme/components/WithinHero.vue +30 -0
  79. data/docs/.vitepress/theme/index.ts +22 -0
  80. data/docs/.vitepress/theme/style.css +628 -0
  81. data/docs/AFDKO_MIGRATION.adoc +175 -0
  82. data/docs/APPLE_LEGACY_FONTS.adoc +173 -0
  83. data/docs/CFF2_SUPPORT.adoc +184 -0
  84. data/docs/COLLECTION_VALIDATION.adoc +204 -0
  85. data/docs/COLOR_FONTS.adoc +127 -0
  86. data/docs/CONVERSION_GUIDE.adoc +656 -0
  87. data/docs/EXTRACT_TTC_MIGRATION.md +549 -0
  88. data/docs/FEATURE_PARITY.adoc +263 -0
  89. data/docs/FONT_HINTING.adoc +570 -0
  90. data/docs/STITCHER_GUIDE.adoc +436 -0
  91. data/docs/SVG_TO_GLYF.adoc +118 -0
  92. data/docs/TTX.adoc +176 -0
  93. data/docs/TYPE1_FONTS.adoc +445 -0
  94. data/docs/UFO_COMPILATION.adoc +397 -0
  95. data/docs/VALIDATION.adoc +254 -0
  96. data/docs/VARIABLE_FONT_OPERATIONS.adoc +599 -0
  97. data/docs/WOFF_WOFF2_FORMATS.adoc +220 -0
  98. data/docs/api/conversion-options.md +164 -0
  99. data/docs/api/converters/curve-converter.md +34 -0
  100. data/docs/api/converters/hint-converter.md +34 -0
  101. data/docs/api/converters/outline-converter.md +27 -0
  102. data/docs/api/font-loader.md +117 -0
  103. data/docs/api/font-writer.md +103 -0
  104. data/docs/api/index.md +79 -0
  105. data/docs/api/layer.md +89 -0
  106. data/docs/api/models/glyph-accessor.md +43 -0
  107. data/docs/api/models/glyph.md +40 -0
  108. data/docs/api/models/table-analyzer.md +35 -0
  109. data/docs/api/sfnt-font.md +53 -0
  110. data/docs/api/type1-font.md +43 -0
  111. data/docs/api/validators/font-validator.md +31 -0
  112. data/docs/api/validators/helper.md +36 -0
  113. data/docs/api/validators/profile.md +39 -0
  114. data/docs/cli/convert.md +148 -0
  115. data/docs/cli/dump-table.md +110 -0
  116. data/docs/cli/export.md +176 -0
  117. data/docs/cli/features.md +124 -0
  118. data/docs/cli/glyphs.md +90 -0
  119. data/docs/cli/index.md +232 -0
  120. data/docs/cli/info.md +254 -0
  121. data/docs/cli/instance.md +122 -0
  122. data/docs/cli/ls.md +95 -0
  123. data/docs/cli/optical-size.md +94 -0
  124. data/docs/cli/pack.md +125 -0
  125. data/docs/cli/scripts.md +105 -0
  126. data/docs/cli/subset.md +58 -0
  127. data/docs/cli/tables.md +84 -0
  128. data/docs/cli/unicode.md +101 -0
  129. data/docs/cli/validate-collection.md +95 -0
  130. data/docs/cli/validate.md +56 -0
  131. data/docs/cli/variable.md +126 -0
  132. data/docs/cli/version.md +46 -0
  133. data/docs/guide/cli/convert.md +108 -0
  134. data/docs/guide/cli/export.md +138 -0
  135. data/docs/guide/cli/index.md +99 -0
  136. data/docs/guide/cli/info.md +144 -0
  137. data/docs/guide/cli/pack.md +155 -0
  138. data/docs/guide/cli/subset.md +149 -0
  139. data/docs/guide/cli/validate.md +139 -0
  140. data/docs/guide/color-fonts/bitmaps.md +194 -0
  141. data/docs/guide/color-fonts/colr-cpal.md +175 -0
  142. data/docs/guide/color-fonts/index.md +140 -0
  143. data/docs/guide/color-fonts/svg.md +154 -0
  144. data/docs/guide/color.md +51 -0
  145. data/docs/guide/comparisons/font-validator.md +222 -0
  146. data/docs/guide/comparisons/fonttools.md +200 -0
  147. data/docs/guide/comparisons/index.md +83 -0
  148. data/docs/guide/comparisons/lcdf-typetools.md +205 -0
  149. data/docs/guide/contributing.md +279 -0
  150. data/docs/guide/conversion/collections.md +251 -0
  151. data/docs/guide/conversion/curves.md +246 -0
  152. data/docs/guide/conversion/index.md +157 -0
  153. data/docs/guide/conversion/options.md +280 -0
  154. data/docs/guide/conversion/ttf-otf.md +184 -0
  155. data/docs/guide/conversion/type1.md +208 -0
  156. data/docs/guide/conversion/web.md +299 -0
  157. data/docs/guide/conversion.md +40 -0
  158. data/docs/guide/engines.md +117 -0
  159. data/docs/guide/formats/collections.md +147 -0
  160. data/docs/guide/formats/dfont.md +99 -0
  161. data/docs/guide/formats/index.md +65 -0
  162. data/docs/guide/formats/otf.md +103 -0
  163. data/docs/guide/formats/svg.md +97 -0
  164. data/docs/guide/formats/ttf.md +105 -0
  165. data/docs/guide/formats/type1.md +118 -0
  166. data/docs/guide/formats/woff.md +139 -0
  167. data/docs/guide/hinting/autohint.md +141 -0
  168. data/docs/guide/hinting/conversion.md +161 -0
  169. data/docs/guide/hinting/index.md +86 -0
  170. data/docs/guide/hinting/postscript.md +149 -0
  171. data/docs/guide/hinting/truetype.md +135 -0
  172. data/docs/guide/hinting.md +44 -0
  173. data/docs/guide/index.md +152 -0
  174. data/docs/guide/installation.md +116 -0
  175. data/docs/guide/migrations/extract-ttc.md +549 -0
  176. data/docs/guide/migrations/font-validator.md +260 -0
  177. data/docs/guide/migrations/fonttools.md +208 -0
  178. data/docs/guide/migrations/index.md +64 -0
  179. data/docs/guide/migrations/otfinfo.md +197 -0
  180. data/docs/guide/quick-start.md +204 -0
  181. data/docs/guide/type1.md +58 -0
  182. data/docs/guide/universal-outline.md +151 -0
  183. data/docs/guide/validation/custom.md +195 -0
  184. data/docs/guide/validation/helpers.md +188 -0
  185. data/docs/guide/validation/index.md +132 -0
  186. data/docs/guide/validation/profiles.md +156 -0
  187. data/docs/guide/validation.md +47 -0
  188. data/docs/guide/variable-fonts/advanced.md +231 -0
  189. data/docs/guide/variable-fonts/axes.md +209 -0
  190. data/docs/guide/variable-fonts/conversion.md +197 -0
  191. data/docs/guide/variable-fonts/index.md +84 -0
  192. data/docs/guide/variable-fonts/instances.md +187 -0
  193. data/docs/guide/variable-fonts/named-instances.md +194 -0
  194. data/docs/guide/variable-fonts/static.md +168 -0
  195. data/docs/guide/variable.md +58 -0
  196. data/docs/guide/woff.md +61 -0
  197. data/docs/index.md +136 -0
  198. data/docs/lychee.toml +41 -0
  199. data/docs/package-lock.json +2560 -0
  200. data/docs/package.json +15 -0
  201. data/docs/public/apple-touch-icon.png +0 -0
  202. data/docs/public/favicon-96x96.png +0 -0
  203. data/docs/public/favicon.ico +0 -0
  204. data/docs/public/favicon.svg +1 -0
  205. data/docs/public/logo-full.svg +1 -0
  206. data/docs/public/logo.svg +1 -0
  207. data/docs/public/robots.txt +4 -0
  208. data/docs/public/site.webmanifest +21 -0
  209. data/docs/public/web-app-manifest-192x192.png +0 -0
  210. data/docs/public/web-app-manifest-512x512.png +0 -0
  211. data/docs/reference-facade.md +130 -0
  212. data/docs/scripts/post-build.mjs +81 -0
  213. data/exe/fontisan +7 -0
  214. data/lib/fontisan/audit/check.rb +47 -0
  215. data/lib/fontisan/audit/check_registry.rb +77 -0
  216. data/lib/fontisan/audit/checks/cff_table_check.rb +87 -0
  217. data/lib/fontisan/audit/checks/cmap_check.rb +256 -0
  218. data/lib/fontisan/audit/checks/collection_integrity_check.rb +102 -0
  219. data/lib/fontisan/audit/checks/format_round_trip_check.rb +150 -0
  220. data/lib/fontisan/audit/checks/glyf_table_check.rb +115 -0
  221. data/lib/fontisan/audit/checks/glyph_name_check.rb +107 -0
  222. data/lib/fontisan/audit/checks/head_check.rb +96 -0
  223. data/lib/fontisan/audit/checks/hhea_check.rb +68 -0
  224. data/lib/fontisan/audit/checks/hinting_check.rb +163 -0
  225. data/lib/fontisan/audit/checks/kern_check.rb +47 -0
  226. data/lib/fontisan/audit/checks/layout_table_check.rb +70 -0
  227. data/lib/fontisan/audit/checks/maxp_check.rb +85 -0
  228. data/lib/fontisan/audit/checks/name_table_check.rb +78 -0
  229. data/lib/fontisan/audit/checks/naming_consistency_check.rb +102 -0
  230. data/lib/fontisan/audit/checks/opentype_conformance_check.rb +108 -0
  231. data/lib/fontisan/audit/checks/os2_check.rb +117 -0
  232. data/lib/fontisan/audit/checks/ots_compatibility_check.rb +191 -0
  233. data/lib/fontisan/audit/checks/post_check.rb +75 -0
  234. data/lib/fontisan/audit/checks/table_directory_check.rb +157 -0
  235. data/lib/fontisan/audit/checks/variable_font_check.rb +151 -0
  236. data/lib/fontisan/audit/checks/woff2_validation_check.rb +140 -0
  237. data/lib/fontisan/audit/checks.rb +42 -0
  238. data/lib/fontisan/audit.rb +20 -0
  239. data/lib/fontisan/base_collection.rb +312 -0
  240. data/lib/fontisan/binary/base_record.rb +82 -0
  241. data/lib/fontisan/binary/structures.rb +82 -0
  242. data/lib/fontisan/binary.rb +11 -0
  243. data/lib/fontisan/byte_ops.rb +314 -0
  244. data/lib/fontisan/cli.rb +1111 -0
  245. data/lib/fontisan/collection/builder.rb +336 -0
  246. data/lib/fontisan/collection/dfont_builder.rb +305 -0
  247. data/lib/fontisan/collection/offset_calculator.rb +229 -0
  248. data/lib/fontisan/collection/reader/stats.rb +23 -0
  249. data/lib/fontisan/collection/reader.rb +90 -0
  250. data/lib/fontisan/collection/shared_logic.rb +52 -0
  251. data/lib/fontisan/collection/table_analyzer.rb +289 -0
  252. data/lib/fontisan/collection/table_deduplicator.rb +317 -0
  253. data/lib/fontisan/collection/writer.rb +289 -0
  254. data/lib/fontisan/collection.rb +16 -0
  255. data/lib/fontisan/commands/audit_command.rb +288 -0
  256. data/lib/fontisan/commands/base_command.rb +88 -0
  257. data/lib/fontisan/commands/convert_command.rb +506 -0
  258. data/lib/fontisan/commands/dump_table_command.rb +68 -0
  259. data/lib/fontisan/commands/export_command.rb +157 -0
  260. data/lib/fontisan/commands/features_command.rb +90 -0
  261. data/lib/fontisan/commands/glyphs_command.rb +50 -0
  262. data/lib/fontisan/commands/info_command.rb +447 -0
  263. data/lib/fontisan/commands/instance_command.rb +357 -0
  264. data/lib/fontisan/commands/ls_command.rb +107 -0
  265. data/lib/fontisan/commands/multi_format_output.rb +64 -0
  266. data/lib/fontisan/commands/optical_size_command.rb +38 -0
  267. data/lib/fontisan/commands/pack_command.rb +344 -0
  268. data/lib/fontisan/commands/scripts_command.rb +56 -0
  269. data/lib/fontisan/commands/subset_command.rb +285 -0
  270. data/lib/fontisan/commands/tables_command.rb +52 -0
  271. data/lib/fontisan/commands/unicode_command.rb +73 -0
  272. data/lib/fontisan/commands/unpack_command.rb +387 -0
  273. data/lib/fontisan/commands/validate_collection_command.rb +127 -0
  274. data/lib/fontisan/commands/validate_command.rb +305 -0
  275. data/lib/fontisan/commands/variable_command.rb +87 -0
  276. data/lib/fontisan/commands.rb +30 -0
  277. data/lib/fontisan/config/collection_settings.yml +56 -0
  278. data/lib/fontisan/config/conversion_matrix.yml +626 -0
  279. data/lib/fontisan/config/export_settings.yml +66 -0
  280. data/lib/fontisan/config/features.yml +143 -0
  281. data/lib/fontisan/config/scripts.yml +42 -0
  282. data/lib/fontisan/config/subset_profiles.yml +106 -0
  283. data/lib/fontisan/config/svg_settings.yml +60 -0
  284. data/lib/fontisan/config/variable_settings.yml +99 -0
  285. data/lib/fontisan/config/woff2_settings.yml +77 -0
  286. data/lib/fontisan/constants.rb +219 -0
  287. data/lib/fontisan/conversion_options.rb +389 -0
  288. data/lib/fontisan/converters/cff_table_builder.rb +195 -0
  289. data/lib/fontisan/converters/collection_converter.rb +520 -0
  290. data/lib/fontisan/converters/conversion_strategy.rb +211 -0
  291. data/lib/fontisan/converters/format_converter.rb +536 -0
  292. data/lib/fontisan/converters/glyf_table_builder.rb +61 -0
  293. data/lib/fontisan/converters/outline_converter.rb +720 -0
  294. data/lib/fontisan/converters/outline_extraction.rb +88 -0
  295. data/lib/fontisan/converters/outline_optimizer.rb +84 -0
  296. data/lib/fontisan/converters/svg_generator.rb +236 -0
  297. data/lib/fontisan/converters/table_copier.rb +107 -0
  298. data/lib/fontisan/converters/type1_converter.rb +1201 -0
  299. data/lib/fontisan/converters/woff2_encoder.rb +520 -0
  300. data/lib/fontisan/converters/woff_writer.rb +358 -0
  301. data/lib/fontisan/converters.rb +21 -0
  302. data/lib/fontisan/dfont_collection.rb +288 -0
  303. data/lib/fontisan/engine.rb +393 -0
  304. data/lib/fontisan/error.rb +281 -0
  305. data/lib/fontisan/export/exporter.rb +255 -0
  306. data/lib/fontisan/export/table_serializer.rb +140 -0
  307. data/lib/fontisan/export/transformers/font_to_ttx.rb +163 -0
  308. data/lib/fontisan/export/transformers/head_transformer.rb +94 -0
  309. data/lib/fontisan/export/transformers/hhea_transformer.rb +57 -0
  310. data/lib/fontisan/export/transformers/maxp_transformer.rb +61 -0
  311. data/lib/fontisan/export/transformers/name_transformer.rb +61 -0
  312. data/lib/fontisan/export/transformers/os2_transformer.rb +119 -0
  313. data/lib/fontisan/export/transformers/post_transformer.rb +49 -0
  314. data/lib/fontisan/export/transformers.rb +17 -0
  315. data/lib/fontisan/export/ttx_generator.rb +526 -0
  316. data/lib/fontisan/export/ttx_parser.rb +300 -0
  317. data/lib/fontisan/export.rb +13 -0
  318. data/lib/fontisan/ffi/client.rb +123 -0
  319. data/lib/fontisan/ffi/libfontisan_cdylib.so +0 -0
  320. data/lib/fontisan/ffi.rb +387 -0
  321. data/lib/fontisan/ffi_platform.rb +24 -0
  322. data/lib/fontisan/font_loader.rb +315 -0
  323. data/lib/fontisan/font_writer.rb +309 -0
  324. data/lib/fontisan/formatters/text_formatter.rb +517 -0
  325. data/lib/fontisan/formatters.rb +9 -0
  326. data/lib/fontisan/glyph_accessor.rb +525 -0
  327. data/lib/fontisan/hints/hint_converter.rb +309 -0
  328. data/lib/fontisan/hints/hint_validator.rb +287 -0
  329. data/lib/fontisan/hints/postscript_hint_applier.rb +255 -0
  330. data/lib/fontisan/hints/postscript_hint_extractor.rb +347 -0
  331. data/lib/fontisan/hints/truetype_hint_applier.rb +117 -0
  332. data/lib/fontisan/hints/truetype_hint_extractor.rb +283 -0
  333. data/lib/fontisan/hints/truetype_instruction_analyzer.rb +268 -0
  334. data/lib/fontisan/hints/truetype_instruction_generator.rb +266 -0
  335. data/lib/fontisan/hints.rb +19 -0
  336. data/lib/fontisan/loading_modes.rb +113 -0
  337. data/lib/fontisan/metrics_calculator.rb +275 -0
  338. data/lib/fontisan/models/all_scripts_features_info.rb +20 -0
  339. data/lib/fontisan/models/audit_report.rb +196 -0
  340. data/lib/fontisan/models/bitmap_glyph.rb +123 -0
  341. data/lib/fontisan/models/bitmap_strike.rb +94 -0
  342. data/lib/fontisan/models/collection_brief_info.rb +36 -0
  343. data/lib/fontisan/models/collection_font_summary.rb +52 -0
  344. data/lib/fontisan/models/collection_info.rb +79 -0
  345. data/lib/fontisan/models/collection_list_info.rb +36 -0
  346. data/lib/fontisan/models/collection_validation_report.rb +102 -0
  347. data/lib/fontisan/models/color_glyph.rb +56 -0
  348. data/lib/fontisan/models/color_layer.rb +53 -0
  349. data/lib/fontisan/models/color_palette.rb +60 -0
  350. data/lib/fontisan/models/features_info.rb +42 -0
  351. data/lib/fontisan/models/font_export.rb +158 -0
  352. data/lib/fontisan/models/font_info.rb +98 -0
  353. data/lib/fontisan/models/font_report.rb +23 -0
  354. data/lib/fontisan/models/font_summary.rb +48 -0
  355. data/lib/fontisan/models/glyph_info.rb +26 -0
  356. data/lib/fontisan/models/glyph_outline.rb +343 -0
  357. data/lib/fontisan/models/hint.rb +404 -0
  358. data/lib/fontisan/models/optical_size_info.rb +33 -0
  359. data/lib/fontisan/models/outline.rb +667 -0
  360. data/lib/fontisan/models/scripts_info.rb +39 -0
  361. data/lib/fontisan/models/svg_glyph.rb +89 -0
  362. data/lib/fontisan/models/table_info.rb +55 -0
  363. data/lib/fontisan/models/table_sharing_info.rb +40 -0
  364. data/lib/fontisan/models/ttx/glyph_order.rb +31 -0
  365. data/lib/fontisan/models/ttx/tables/binary_table.rb +67 -0
  366. data/lib/fontisan/models/ttx/tables/head_table.rb +74 -0
  367. data/lib/fontisan/models/ttx/tables/hhea_table.rb +74 -0
  368. data/lib/fontisan/models/ttx/tables/maxp_table.rb +55 -0
  369. data/lib/fontisan/models/ttx/tables/name_table.rb +45 -0
  370. data/lib/fontisan/models/ttx/tables/os2_table.rb +157 -0
  371. data/lib/fontisan/models/ttx/tables/post_table.rb +50 -0
  372. data/lib/fontisan/models/ttx/tables.rb +21 -0
  373. data/lib/fontisan/models/ttx/ttfont.rb +41 -0
  374. data/lib/fontisan/models/ttx.rb +14 -0
  375. data/lib/fontisan/models/unicode_mappings.rb +42 -0
  376. data/lib/fontisan/models/validation_report.rb +435 -0
  377. data/lib/fontisan/models/variable_font_info.rb +82 -0
  378. data/lib/fontisan/models.rb +49 -0
  379. data/lib/fontisan/native/3.2/fontisan_native.so +0 -0
  380. data/lib/fontisan/native/3.3/fontisan_native.so +0 -0
  381. data/lib/fontisan/native/3.4/fontisan_native.so +0 -0
  382. data/lib/fontisan/native/4.0/fontisan_native.so +0 -0
  383. data/lib/fontisan/native.rb +235 -0
  384. data/lib/fontisan/open_type_collection.rb +49 -0
  385. data/lib/fontisan/open_type_font.rb +161 -0
  386. data/lib/fontisan/open_type_font_extensions.rb +54 -0
  387. data/lib/fontisan/optimizers/charstring_rewriter.rb +172 -0
  388. data/lib/fontisan/optimizers/pattern_analyzer.rb +310 -0
  389. data/lib/fontisan/optimizers/stack_tracker.rb +246 -0
  390. data/lib/fontisan/optimizers/subroutine_builder.rb +135 -0
  391. data/lib/fontisan/optimizers/subroutine_generator.rb +207 -0
  392. data/lib/fontisan/optimizers/subroutine_optimizer.rb +114 -0
  393. data/lib/fontisan/optimizers.rb +14 -0
  394. data/lib/fontisan/outline_extractor.rb +417 -0
  395. data/lib/fontisan/output_guard.rb +40 -0
  396. data/lib/fontisan/parsers/dfont_parser.rb +191 -0
  397. data/lib/fontisan/parsers/tag.rb +77 -0
  398. data/lib/fontisan/parsers.rb +10 -0
  399. data/lib/fontisan/pipeline/format_detector.rb +176 -0
  400. data/lib/fontisan/pipeline/output_writer.rb +161 -0
  401. data/lib/fontisan/pipeline/strategies/base_strategy.rb +75 -0
  402. data/lib/fontisan/pipeline/strategies/instance_strategy.rb +89 -0
  403. data/lib/fontisan/pipeline/strategies/named_strategy.rb +114 -0
  404. data/lib/fontisan/pipeline/strategies/preserve_strategy.rb +54 -0
  405. data/lib/fontisan/pipeline/strategies.rb +16 -0
  406. data/lib/fontisan/pipeline/transformation_pipeline.rb +407 -0
  407. data/lib/fontisan/pipeline/variation_resolver.rb +158 -0
  408. data/lib/fontisan/pipeline.rb +14 -0
  409. data/lib/fontisan/reference/table_digests.rb +251 -0
  410. data/lib/fontisan/reference.rb +663 -0
  411. data/lib/fontisan/scan.rb +372 -0
  412. data/lib/fontisan/sfnt_builder.rb +66 -0
  413. data/lib/fontisan/sfnt_directory.rb +41 -0
  414. data/lib/fontisan/sfnt_font.rb +749 -0
  415. data/lib/fontisan/sfnt_source.rb +28 -0
  416. data/lib/fontisan/sfnt_table.rb +260 -0
  417. data/lib/fontisan/stitcher/cbdt_propagator.rb +212 -0
  418. data/lib/fontisan/stitcher/collection_result.rb +19 -0
  419. data/lib/fontisan/stitcher/deduplicator.rb +47 -0
  420. data/lib/fontisan/stitcher/format_metadata.rb +51 -0
  421. data/lib/fontisan/stitcher/glyph_cloner.rb +41 -0
  422. data/lib/fontisan/stitcher/glyph_copier.rb +110 -0
  423. data/lib/fontisan/stitcher/glyph_limit.rb +53 -0
  424. data/lib/fontisan/stitcher/glyph_signature.rb +51 -0
  425. data/lib/fontisan/stitcher/partition_strategy/base.rb +52 -0
  426. data/lib/fontisan/stitcher/partition_strategy/blueprint.rb +24 -0
  427. data/lib/fontisan/stitcher/partition_strategy/by_block.rb +430 -0
  428. data/lib/fontisan/stitcher/partition_strategy/by_plane.rb +161 -0
  429. data/lib/fontisan/stitcher/partition_strategy/by_script.rb +309 -0
  430. data/lib/fontisan/stitcher/partition_strategy/partition.rb +24 -0
  431. data/lib/fontisan/stitcher/partition_strategy.rb +24 -0
  432. data/lib/fontisan/stitcher/selector/codepoints.rb +29 -0
  433. data/lib/fontisan/stitcher/selector/gid.rb +25 -0
  434. data/lib/fontisan/stitcher/selector/range.rb +30 -0
  435. data/lib/fontisan/stitcher/selector.rb +26 -0
  436. data/lib/fontisan/stitcher/source.rb +459 -0
  437. data/lib/fontisan/stitcher/unique_glyph_name.rb +46 -0
  438. data/lib/fontisan/stitcher.rb +185 -0
  439. data/lib/fontisan/stitcher_cli.rb +69 -0
  440. data/lib/fontisan/subset/builder.rb +263 -0
  441. data/lib/fontisan/subset/glyph_mapping.rb +222 -0
  442. data/lib/fontisan/subset/options.rb +142 -0
  443. data/lib/fontisan/subset/profile.rb +152 -0
  444. data/lib/fontisan/subset/shared_state.rb +42 -0
  445. data/lib/fontisan/subset/subset_context.rb +20 -0
  446. data/lib/fontisan/subset/table_strategy/cbdt.rb +30 -0
  447. data/lib/fontisan/subset/table_strategy/cblc.rb +27 -0
  448. data/lib/fontisan/subset/table_strategy/cff.rb +92 -0
  449. data/lib/fontisan/subset/table_strategy/cff2.rb +391 -0
  450. data/lib/fontisan/subset/table_strategy/cmap.rb +146 -0
  451. data/lib/fontisan/subset/table_strategy/color_bitmap_placement.rb +20 -0
  452. data/lib/fontisan/subset/table_strategy/color_bitmap_strike_plan.rb +45 -0
  453. data/lib/fontisan/subset/table_strategy/color_bitmap_subset_plan.rb +62 -0
  454. data/lib/fontisan/subset/table_strategy/color_bitmap_subsetter.rb +182 -0
  455. data/lib/fontisan/subset/table_strategy/color_bitmap_subtable_plan.rb +59 -0
  456. data/lib/fontisan/subset/table_strategy/glyf.rb +29 -0
  457. data/lib/fontisan/subset/table_strategy/glyf_loca_builder.rb +146 -0
  458. data/lib/fontisan/subset/table_strategy/head.rb +43 -0
  459. data/lib/fontisan/subset/table_strategy/hhea.rb +67 -0
  460. data/lib/fontisan/subset/table_strategy/hmtx.rb +52 -0
  461. data/lib/fontisan/subset/table_strategy/loca.rb +41 -0
  462. data/lib/fontisan/subset/table_strategy/maxp.rb +23 -0
  463. data/lib/fontisan/subset/table_strategy/name.rb +19 -0
  464. data/lib/fontisan/subset/table_strategy/os2.rb +21 -0
  465. data/lib/fontisan/subset/table_strategy/pass_through.rb +20 -0
  466. data/lib/fontisan/subset/table_strategy/post.rb +37 -0
  467. data/lib/fontisan/subset/table_strategy.rb +79 -0
  468. data/lib/fontisan/subset/table_subsetter.rb +110 -0
  469. data/lib/fontisan/subset.rb +16 -0
  470. data/lib/fontisan/svg/font_face_generator.rb +274 -0
  471. data/lib/fontisan/svg/font_generator.rb +256 -0
  472. data/lib/fontisan/svg/glyph_generator.rb +201 -0
  473. data/lib/fontisan/svg/standalone_glyph.rb +132 -0
  474. data/lib/fontisan/svg/view_box_calculator.rb +137 -0
  475. data/lib/fontisan/svg.rb +13 -0
  476. data/lib/fontisan/svg_to_glyf/assembler.rb +174 -0
  477. data/lib/fontisan/svg_to_glyf/document.rb +102 -0
  478. data/lib/fontisan/svg_to_glyf/geometry/affine_transform.rb +112 -0
  479. data/lib/fontisan/svg_to_glyf/geometry/normalizer.rb +62 -0
  480. data/lib/fontisan/svg_to_glyf/geometry/transform_parser.rb +98 -0
  481. data/lib/fontisan/svg_to_glyf/geometry.rb +15 -0
  482. data/lib/fontisan/svg_to_glyf/path/command.rb +18 -0
  483. data/lib/fontisan/svg_to_glyf/path/contour_builder.rb +140 -0
  484. data/lib/fontisan/svg_to_glyf/path/parser.rb +98 -0
  485. data/lib/fontisan/svg_to_glyf/path/state.rb +79 -0
  486. data/lib/fontisan/svg_to_glyf/path.rb +14 -0
  487. data/lib/fontisan/svg_to_glyf.rb +62 -0
  488. data/lib/fontisan/tables/cbdt.rb +113 -0
  489. data/lib/fontisan/tables/cblc.rb +172 -0
  490. data/lib/fontisan/tables/cblc_big_glyph_metrics.rb +28 -0
  491. data/lib/fontisan/tables/cblc_bitmap_size.rb +67 -0
  492. data/lib/fontisan/tables/cblc_glyph_bitmap_location.rb +28 -0
  493. data/lib/fontisan/tables/cblc_index_subtable.rb +104 -0
  494. data/lib/fontisan/tables/cblc_index_subtable_array_entry.rb +20 -0
  495. data/lib/fontisan/tables/cblc_index_subtable_format_parser.rb +150 -0
  496. data/lib/fontisan/tables/cblc_index_subtable_header.rb +18 -0
  497. data/lib/fontisan/tables/cblc_sbit_line_metrics.rb +28 -0
  498. data/lib/fontisan/tables/cff/cff2_charstring_builder.rb +225 -0
  499. data/lib/fontisan/tables/cff/cff_glyph.rb +176 -0
  500. data/lib/fontisan/tables/cff/charset.rb +276 -0
  501. data/lib/fontisan/tables/cff/charstring.rb +978 -0
  502. data/lib/fontisan/tables/cff/charstring_builder.rb +356 -0
  503. data/lib/fontisan/tables/cff/charstring_parser.rb +249 -0
  504. data/lib/fontisan/tables/cff/charstring_rebuilder.rb +168 -0
  505. data/lib/fontisan/tables/cff/charstrings_index.rb +159 -0
  506. data/lib/fontisan/tables/cff/dict.rb +353 -0
  507. data/lib/fontisan/tables/cff/dict_builder.rb +260 -0
  508. data/lib/fontisan/tables/cff/encoding.rb +273 -0
  509. data/lib/fontisan/tables/cff/expert_charsets.rb +85 -0
  510. data/lib/fontisan/tables/cff/header.rb +100 -0
  511. data/lib/fontisan/tables/cff/hint_operation_injector.rb +207 -0
  512. data/lib/fontisan/tables/cff/index.rb +241 -0
  513. data/lib/fontisan/tables/cff/index_builder.rb +170 -0
  514. data/lib/fontisan/tables/cff/offset_recalculator.rb +70 -0
  515. data/lib/fontisan/tables/cff/private_dict.rb +282 -0
  516. data/lib/fontisan/tables/cff/private_dict_writer.rb +129 -0
  517. data/lib/fontisan/tables/cff/standard_strings_data.rb +83 -0
  518. data/lib/fontisan/tables/cff/table_builder.rb +215 -0
  519. data/lib/fontisan/tables/cff/top_dict.rb +234 -0
  520. data/lib/fontisan/tables/cff.rb +497 -0
  521. data/lib/fontisan/tables/cff2/blend_operator.rb +240 -0
  522. data/lib/fontisan/tables/cff2/charstring_parser.rb +624 -0
  523. data/lib/fontisan/tables/cff2/dict_encoder.rb +94 -0
  524. data/lib/fontisan/tables/cff2/fd_select.rb +142 -0
  525. data/lib/fontisan/tables/cff2/header.rb +34 -0
  526. data/lib/fontisan/tables/cff2/index.rb +155 -0
  527. data/lib/fontisan/tables/cff2/index_builder.rb +80 -0
  528. data/lib/fontisan/tables/cff2/operand_stack.rb +232 -0
  529. data/lib/fontisan/tables/cff2/private_dict_blend_handler.rb +247 -0
  530. data/lib/fontisan/tables/cff2/region_matcher.rb +200 -0
  531. data/lib/fontisan/tables/cff2/table_builder.rb +567 -0
  532. data/lib/fontisan/tables/cff2/table_reader.rb +418 -0
  533. data/lib/fontisan/tables/cff2/variation_data_extractor.rb +212 -0
  534. data/lib/fontisan/tables/cff2.rb +353 -0
  535. data/lib/fontisan/tables/cmap.rb +399 -0
  536. data/lib/fontisan/tables/cmap_table.rb +228 -0
  537. data/lib/fontisan/tables/colr.rb +290 -0
  538. data/lib/fontisan/tables/cpal.rb +381 -0
  539. data/lib/fontisan/tables/cvar.rb +204 -0
  540. data/lib/fontisan/tables/fvar.rb +159 -0
  541. data/lib/fontisan/tables/glyf/compound_glyph.rb +482 -0
  542. data/lib/fontisan/tables/glyf/compound_glyph_resolver.rb +135 -0
  543. data/lib/fontisan/tables/glyf/curve_converter.rb +349 -0
  544. data/lib/fontisan/tables/glyf/glyph_builder.rb +468 -0
  545. data/lib/fontisan/tables/glyf/simple_glyph.rb +391 -0
  546. data/lib/fontisan/tables/glyf.rb +347 -0
  547. data/lib/fontisan/tables/glyf_table.rb +252 -0
  548. data/lib/fontisan/tables/gpos/glyph_filter.rb +87 -0
  549. data/lib/fontisan/tables/gpos/kerning_pairs.rb +215 -0
  550. data/lib/fontisan/tables/gpos/pair_position.rb +274 -0
  551. data/lib/fontisan/tables/gpos.rb +139 -0
  552. data/lib/fontisan/tables/gsub.rb +116 -0
  553. data/lib/fontisan/tables/gvar.rb +232 -0
  554. data/lib/fontisan/tables/head.rb +208 -0
  555. data/lib/fontisan/tables/head_table.rb +108 -0
  556. data/lib/fontisan/tables/hhea.rb +199 -0
  557. data/lib/fontisan/tables/hhea_table.rb +252 -0
  558. data/lib/fontisan/tables/hmtx.rb +281 -0
  559. data/lib/fontisan/tables/hmtx_table.rb +188 -0
  560. data/lib/fontisan/tables/hvar.rb +192 -0
  561. data/lib/fontisan/tables/layout_common.rb +245 -0
  562. data/lib/fontisan/tables/loca.rb +323 -0
  563. data/lib/fontisan/tables/loca_table.rb +209 -0
  564. data/lib/fontisan/tables/maxp.rb +253 -0
  565. data/lib/fontisan/tables/maxp_table.rb +255 -0
  566. data/lib/fontisan/tables/mvar.rb +185 -0
  567. data/lib/fontisan/tables/name.rb +340 -0
  568. data/lib/fontisan/tables/name_table.rb +173 -0
  569. data/lib/fontisan/tables/os2.rb +291 -0
  570. data/lib/fontisan/tables/os2_table.rb +326 -0
  571. data/lib/fontisan/tables/post.rb +206 -0
  572. data/lib/fontisan/tables/post_table.rb +180 -0
  573. data/lib/fontisan/tables/registry.rb +71 -0
  574. data/lib/fontisan/tables/sbix.rb +379 -0
  575. data/lib/fontisan/tables/svg.rb +300 -0
  576. data/lib/fontisan/tables/variation_common.rb +345 -0
  577. data/lib/fontisan/tables/vvar.rb +234 -0
  578. data/lib/fontisan/tables.rb +69 -0
  579. data/lib/fontisan/true_type_collection.rb +160 -0
  580. data/lib/fontisan/true_type_font.rb +80 -0
  581. data/lib/fontisan/true_type_font_extensions.rb +54 -0
  582. data/lib/fontisan/type1/afm_generator.rb +379 -0
  583. data/lib/fontisan/type1/afm_parser.rb +299 -0
  584. data/lib/fontisan/type1/agl.rb +456 -0
  585. data/lib/fontisan/type1/cff_to_type1_converter.rb +307 -0
  586. data/lib/fontisan/type1/charstring_converter.rb +238 -0
  587. data/lib/fontisan/type1/charstrings.rb +441 -0
  588. data/lib/fontisan/type1/conversion_options.rb +242 -0
  589. data/lib/fontisan/type1/decryptor.rb +183 -0
  590. data/lib/fontisan/type1/encodings.rb +695 -0
  591. data/lib/fontisan/type1/font_dictionary.rb +576 -0
  592. data/lib/fontisan/type1/generator.rb +217 -0
  593. data/lib/fontisan/type1/inf_generator.rb +307 -0
  594. data/lib/fontisan/type1/pfa_generator.rb +376 -0
  595. data/lib/fontisan/type1/pfa_parser.rb +159 -0
  596. data/lib/fontisan/type1/pfb_generator.rb +321 -0
  597. data/lib/fontisan/type1/pfb_parser.rb +166 -0
  598. data/lib/fontisan/type1/pfm_generator.rb +589 -0
  599. data/lib/fontisan/type1/pfm_parser.rb +433 -0
  600. data/lib/fontisan/type1/private_dict.rb +363 -0
  601. data/lib/fontisan/type1/seac_expander.rb +517 -0
  602. data/lib/fontisan/type1/ttf_to_type1_converter.rb +389 -0
  603. data/lib/fontisan/type1/upm_scaler.rb +118 -0
  604. data/lib/fontisan/type1.rb +48 -0
  605. data/lib/fontisan/type1_font.rb +347 -0
  606. data/lib/fontisan/ufo/anchor.rb +17 -0
  607. data/lib/fontisan/ufo/bounds.rb +91 -0
  608. data/lib/fontisan/ufo/cli.rb +105 -0
  609. data/lib/fontisan/ufo/compile/avar.rb +46 -0
  610. data/lib/fontisan/ufo/compile/base_compiler.rb +162 -0
  611. data/lib/fontisan/ufo/compile/cbdt_cblc.rb +104 -0
  612. data/lib/fontisan/ufo/compile/cff.rb +229 -0
  613. data/lib/fontisan/ufo/compile/cff2.rb +323 -0
  614. data/lib/fontisan/ufo/compile/cff2_subroutines.rb +39 -0
  615. data/lib/fontisan/ufo/compile/cmap.rb +143 -0
  616. data/lib/fontisan/ufo/compile/colr.rb +80 -0
  617. data/lib/fontisan/ufo/compile/cpal.rb +62 -0
  618. data/lib/fontisan/ufo/compile/feature_compiler.rb +161 -0
  619. data/lib/fontisan/ufo/compile/feature_writers/base.rb +56 -0
  620. data/lib/fontisan/ufo/compile/feature_writers/curs.rb +72 -0
  621. data/lib/fontisan/ufo/compile/feature_writers/gdef.rb +92 -0
  622. data/lib/fontisan/ufo/compile/feature_writers/kern.rb +50 -0
  623. data/lib/fontisan/ufo/compile/feature_writers/kern2.rb +57 -0
  624. data/lib/fontisan/ufo/compile/feature_writers/mark.rb +45 -0
  625. data/lib/fontisan/ufo/compile/feature_writers/mark_family_base.rb +124 -0
  626. data/lib/fontisan/ufo/compile/feature_writers/mkmk.rb +53 -0
  627. data/lib/fontisan/ufo/compile/feature_writers.rb +38 -0
  628. data/lib/fontisan/ufo/compile/filters/cubic_to_quadratic.rb +176 -0
  629. data/lib/fontisan/ufo/compile/filters/decompose_components.rb +33 -0
  630. data/lib/fontisan/ufo/compile/filters/flatten_components.rb +22 -0
  631. data/lib/fontisan/ufo/compile/filters/propagate_anchors.rb +102 -0
  632. data/lib/fontisan/ufo/compile/filters/remove_overlaps.rb +95 -0
  633. data/lib/fontisan/ufo/compile/filters/reverse_contour_direction.rb +27 -0
  634. data/lib/fontisan/ufo/compile/filters/sort_contours.rb +69 -0
  635. data/lib/fontisan/ufo/compile/filters/transformations.rb +113 -0
  636. data/lib/fontisan/ufo/compile/filters.rb +69 -0
  637. data/lib/fontisan/ufo/compile/fvar.rb +113 -0
  638. data/lib/fontisan/ufo/compile/glyf_loca.rb +206 -0
  639. data/lib/fontisan/ufo/compile/gpos.rb +248 -0
  640. data/lib/fontisan/ufo/compile/gsub.rb +267 -0
  641. data/lib/fontisan/ufo/compile/gvar.rb +225 -0
  642. data/lib/fontisan/ufo/compile/head.rb +97 -0
  643. data/lib/fontisan/ufo/compile/hhea.rb +57 -0
  644. data/lib/fontisan/ufo/compile/hmtx.rb +38 -0
  645. data/lib/fontisan/ufo/compile/hvar.rb +42 -0
  646. data/lib/fontisan/ufo/compile/item_variation_store.rb +100 -0
  647. data/lib/fontisan/ufo/compile/math.rb +143 -0
  648. data/lib/fontisan/ufo/compile/maxp.rb +57 -0
  649. data/lib/fontisan/ufo/compile/meta.rb +51 -0
  650. data/lib/fontisan/ufo/compile/mvar.rb +59 -0
  651. data/lib/fontisan/ufo/compile/name.rb +81 -0
  652. data/lib/fontisan/ufo/compile/os2.rb +81 -0
  653. data/lib/fontisan/ufo/compile/otf2_compiler.rb +40 -0
  654. data/lib/fontisan/ufo/compile/otf_compiler.rb +34 -0
  655. data/lib/fontisan/ufo/compile/post.rb +32 -0
  656. data/lib/fontisan/ufo/compile/sbix.rb +100 -0
  657. data/lib/fontisan/ufo/compile/stat.rb +106 -0
  658. data/lib/fontisan/ufo/compile/svg_table.rb +60 -0
  659. data/lib/fontisan/ufo/compile/ttf_compiler.rb +75 -0
  660. data/lib/fontisan/ufo/compile/variable_otf.rb +84 -0
  661. data/lib/fontisan/ufo/compile/variable_ttf.rb +161 -0
  662. data/lib/fontisan/ufo/compile.rb +72 -0
  663. data/lib/fontisan/ufo/component.rb +18 -0
  664. data/lib/fontisan/ufo/contour.rb +34 -0
  665. data/lib/fontisan/ufo/convert/from_bin_data.rb +366 -0
  666. data/lib/fontisan/ufo/convert/to_dfont.rb +45 -0
  667. data/lib/fontisan/ufo/convert/to_otc.rb +40 -0
  668. data/lib/fontisan/ufo/convert/to_otf.rb +18 -0
  669. data/lib/fontisan/ufo/convert/to_otf2.rb +20 -0
  670. data/lib/fontisan/ufo/convert/to_postscript.rb +64 -0
  671. data/lib/fontisan/ufo/convert/to_ttc.rb +35 -0
  672. data/lib/fontisan/ufo/convert/to_ttf.rb +21 -0
  673. data/lib/fontisan/ufo/convert/to_woff.rb +61 -0
  674. data/lib/fontisan/ufo/convert/to_woff2.rb +50 -0
  675. data/lib/fontisan/ufo/convert.rb +84 -0
  676. data/lib/fontisan/ufo/data_set.rb +21 -0
  677. data/lib/fontisan/ufo/features.rb +17 -0
  678. data/lib/fontisan/ufo/font.rb +63 -0
  679. data/lib/fontisan/ufo/glyph.rb +445 -0
  680. data/lib/fontisan/ufo/glyph_exists_error.rb +23 -0
  681. data/lib/fontisan/ufo/groups.rb +45 -0
  682. data/lib/fontisan/ufo/guideline.rb +19 -0
  683. data/lib/fontisan/ufo/image.rb +29 -0
  684. data/lib/fontisan/ufo/image_set.rb +99 -0
  685. data/lib/fontisan/ufo/info.rb +133 -0
  686. data/lib/fontisan/ufo/kerning.rb +32 -0
  687. data/lib/fontisan/ufo/layer.rb +78 -0
  688. data/lib/fontisan/ufo/layer_set.rb +37 -0
  689. data/lib/fontisan/ufo/lib.rb +24 -0
  690. data/lib/fontisan/ufo/plist.rb +123 -0
  691. data/lib/fontisan/ufo/point.rb +32 -0
  692. data/lib/fontisan/ufo/reader.rb +163 -0
  693. data/lib/fontisan/ufo/transformation.rb +50 -0
  694. data/lib/fontisan/ufo/writer.rb +123 -0
  695. data/lib/fontisan/ufo.rb +47 -0
  696. data/lib/fontisan/unicode/plane.rb +44 -0
  697. data/lib/fontisan/unicode.rb +17 -0
  698. data/lib/fontisan/utilities/brotli_wrapper.rb +177 -0
  699. data/lib/fontisan/utilities/checksum_calculator.rb +104 -0
  700. data/lib/fontisan/utilities/padding.rb +61 -0
  701. data/lib/fontisan/utilities.rb +11 -0
  702. data/lib/fontisan/utils/thread_pool.rb +134 -0
  703. data/lib/fontisan/utils.rb +10 -0
  704. data/lib/fontisan/validation/collection_validator.rb +265 -0
  705. data/lib/fontisan/validation.rb +9 -0
  706. data/lib/fontisan/validators/basic_validator.rb +73 -0
  707. data/lib/fontisan/validators/font_book_validator.rb +107 -0
  708. data/lib/fontisan/validators/opentype_validator.rb +118 -0
  709. data/lib/fontisan/validators/profile_loader.rb +134 -0
  710. data/lib/fontisan/validators/validator.rb +494 -0
  711. data/lib/fontisan/validators/web_font_validator.rb +95 -0
  712. data/lib/fontisan/validators.rb +14 -0
  713. data/lib/fontisan/variable/axis_normalizer.rb +215 -0
  714. data/lib/fontisan/variable/delta_applicator.rb +302 -0
  715. data/lib/fontisan/variable/glyph_delta_processor.rb +218 -0
  716. data/lib/fontisan/variable/instancer.rb +172 -0
  717. data/lib/fontisan/variable/metric_delta_processor.rb +282 -0
  718. data/lib/fontisan/variable/region_matcher.rb +208 -0
  719. data/lib/fontisan/variable/static_font_builder.rb +209 -0
  720. data/lib/fontisan/variable/table_updater.rb +219 -0
  721. data/lib/fontisan/variable.rb +16 -0
  722. data/lib/fontisan/variation/blend_applier.rb +197 -0
  723. data/lib/fontisan/variation/cache.rb +299 -0
  724. data/lib/fontisan/variation/cache_key_builder.rb +162 -0
  725. data/lib/fontisan/variation/converter.rb +373 -0
  726. data/lib/fontisan/variation/data_extractor.rb +84 -0
  727. data/lib/fontisan/variation/delta_applier.rb +262 -0
  728. data/lib/fontisan/variation/delta_parser.rb +228 -0
  729. data/lib/fontisan/variation/inspector.rb +275 -0
  730. data/lib/fontisan/variation/instance_font_wrapper.rb +41 -0
  731. data/lib/fontisan/variation/instance_generator.rb +122 -0
  732. data/lib/fontisan/variation/instance_writer.rb +360 -0
  733. data/lib/fontisan/variation/instancing/axis_space.rb +136 -0
  734. data/lib/fontisan/variation/instancing/glyph_outline.rb +204 -0
  735. data/lib/fontisan/variation/instancing/gvar_reader.rb +146 -0
  736. data/lib/fontisan/variation/instancing/item_variation_store.rb +92 -0
  737. data/lib/fontisan/variation/instancing/name_rewriter.rb +90 -0
  738. data/lib/fontisan/variation/instancing/true_type_instancer.rb +326 -0
  739. data/lib/fontisan/variation/instancing.rb +20 -0
  740. data/lib/fontisan/variation/interpolator.rb +231 -0
  741. data/lib/fontisan/variation/metrics_adjuster.rb +309 -0
  742. data/lib/fontisan/variation/optimizer.rb +417 -0
  743. data/lib/fontisan/variation/parallel_generator.rb +147 -0
  744. data/lib/fontisan/variation/region_matcher.rb +221 -0
  745. data/lib/fontisan/variation/subsetter.rb +400 -0
  746. data/lib/fontisan/variation/table_accessor.rb +105 -0
  747. data/lib/fontisan/variation/tuple_variation_header.rb +49 -0
  748. data/lib/fontisan/variation/validator.rb +336 -0
  749. data/lib/fontisan/variation/variable_svg_generator.rb +235 -0
  750. data/lib/fontisan/variation/variation_context.rb +208 -0
  751. data/lib/fontisan/variation/variation_preserver.rb +287 -0
  752. data/lib/fontisan/variation.rb +33 -0
  753. data/lib/fontisan/version.rb +5 -0
  754. data/lib/fontisan/version.rb.orig +9 -0
  755. data/lib/fontisan/woff2/collection_decoder.rb +223 -0
  756. data/lib/fontisan/woff2/collection_encoder.rb +302 -0
  757. data/lib/fontisan/woff2/directory.rb +255 -0
  758. data/lib/fontisan/woff2/encoder_rules.rb +114 -0
  759. data/lib/fontisan/woff2/glyf_canonicalizer.rb +90 -0
  760. data/lib/fontisan/woff2/glyf_loca_reconstruct.rb +401 -0
  761. data/lib/fontisan/woff2/glyf_loca_transform.rb +319 -0
  762. data/lib/fontisan/woff2/header.rb +101 -0
  763. data/lib/fontisan/woff2/hmtx_transformer.rb +140 -0
  764. data/lib/fontisan/woff2/sfnt_checksum.rb +107 -0
  765. data/lib/fontisan/woff2/table_transformer.rb +575 -0
  766. data/lib/fontisan/woff2/triplet_codec.rb +181 -0
  767. data/lib/fontisan/woff2/uint255.rb +101 -0
  768. data/lib/fontisan/woff2.rb +21 -0
  769. data/lib/fontisan/woff2_collection.rb +127 -0
  770. data/lib/fontisan/woff2_font.rb +754 -0
  771. data/lib/fontisan/woff_container.rb +83 -0
  772. data/lib/fontisan/woff_font.rb +554 -0
  773. data/lib/fontisan.rb +299 -0
  774. data/rakelib/bench.rake +9 -0
  775. data/rakelib/cdylib.rake +39 -0
  776. data/rakelib/corpus.rake +272 -0
  777. data/rakelib/corpus_convert.rake +43 -0
  778. data/rakelib/corpus_goldens.rake +22 -0
  779. data/rakelib/corpus_malformed.rake +20 -0
  780. data/rakelib/corpus_scan.rake +24 -0
  781. data/rakelib/java.rake +61 -0
  782. data/rakelib/native.rake +43 -0
  783. data/scripts/build-woff2.sh +57 -0
  784. data/scripts/compare_stack_aware.rb +187 -0
  785. data/scripts/corpus/generate_convert_goldens.rb +478 -0
  786. data/scripts/corpus/generate_goldens.rb +282 -0
  787. data/scripts/corpus/generate_malformed_goldens.rb +254 -0
  788. data/scripts/corpus/generate_scan_goldens.rb +98 -0
  789. data/scripts/measure_optimization.rb +148 -0
  790. data/scripts/tier_fallback.rb +228 -0
  791. data/scripts/tier_smoke.rb +72 -0
  792. metadata +945 -0
@@ -0,0 +1,1201 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fontisan
4
+ module Converters
5
+ # Converter for Adobe Type 1 fonts to/from SFNT formats.
6
+ #
7
+ # [`Type1Converter`](lib/fontisan/converters/type1_converter.rb) handles
8
+ # bidirectional conversion between Type 1 fonts (PFB/PFA) and SFNT-based
9
+ # formats (TTF, OTF, WOFF, WOFF2).
10
+ #
11
+ # == Conversion Strategy
12
+ #
13
+ # Type 1 fonts use PostScript CharStrings that are similar to CFF CharStrings
14
+ # used in OpenType fonts. The conversion uses CharStringConverter for the
15
+ # CharString translation.
16
+ #
17
+ # * Type 1 → OTF: Convert Type 1 CharStrings to CFF format, build CFF table
18
+ # * OTF → Type 1: Convert CFF CharStrings to Type 1 format, build PFB/PFA
19
+ # * Type 1 → TTF: Type 1 → OTF → TTF (via OutlineConverter)
20
+ # * TTF → Type 1: TTF → OTF → Type 1
21
+ #
22
+ # == Conversion Options
23
+ #
24
+ # The converter accepts [`ConversionOptions`](../conversion_options) with
25
+ # opening and generating options:
26
+ #
27
+ # * Opening options: decompose_composites, generate_unicode, read_all_records
28
+ # * Generating options: decompose_on_output, hinting_mode, write_pfm, write_afm
29
+ #
30
+ # @example Convert Type 1 to OTF with options
31
+ # font = FontLoader.load("font.pfb")
32
+ # options = ConversionOptions.recommended(from: :type1, to: :otf)
33
+ # converter = Type1Converter.new
34
+ # tables = converter.convert(font, options: options)
35
+ #
36
+ # @example Convert with preset
37
+ # options = ConversionOptions.from_preset(:type1_to_modern)
38
+ # tables = converter.convert(font, options: options)
39
+ #
40
+ # @see https://www.adobe.com/devnet/font/pdfs/Type1.pdf
41
+ # @see CharStringConverter
42
+ class Type1Converter
43
+ include ConversionStrategy
44
+ include CffTableBuilder
45
+
46
+ # Initialize a new Type1Converter
47
+ #
48
+ # @param options [Hash] Converter options
49
+ # @option options [Boolean] :optimize_cff Enable CFF optimization (default: false)
50
+ # @option options [Boolean] :preserve_hints Preserve hinting (default: true)
51
+ # @option options [Symbol] :target_format Target format for conversion
52
+ def initialize(options = {})
53
+ @optimize_cff = options.fetch(:optimize_cff, false)
54
+ @preserve_hints = options.fetch(:preserve_hints, true)
55
+ @target_format = options[:target_format]
56
+ end
57
+
58
+ # Convert font to target format
59
+ #
60
+ # @param font [Type1Font, OpenTypeFont, TrueTypeFont] Source font
61
+ # @param options [Hash, ConversionOptions] Conversion options
62
+ # @option options [Symbol] :target_format Target format override
63
+ # @option options [ConversionOptions] :options ConversionOptions object
64
+ # @return [Hash<String, String>] Map of table tags to binary data
65
+ def convert(font, options = {})
66
+ # Extract ConversionOptions if provided
67
+ conv_options = extract_conversion_options(options)
68
+
69
+ target_format = options[:target_format] || conv_options&.to || @target_format ||
70
+ detect_target_format(font)
71
+ validate(font, target_format)
72
+
73
+ # Apply opening options to source font
74
+ apply_opening_options(font, conv_options) if conv_options
75
+
76
+ source_format = detect_format(font)
77
+
78
+ case [source_format, target_format]
79
+ when %i[type1 otf]
80
+ convert_type1_to_otf(font, conv_options)
81
+ when %i[otf type1]
82
+ convert_otf_to_type1(font, conv_options)
83
+ when %i[type1 ttf]
84
+ convert_type1_to_ttf(font, conv_options)
85
+ when %i[ttf type1]
86
+ convert_ttf_to_type1(font, conv_options)
87
+ else
88
+ raise Fontisan::Error,
89
+ "Unsupported conversion: #{source_format} → #{target_format}"
90
+ end
91
+ end
92
+
93
+ # Get supported conversions
94
+ #
95
+ # @return [Array<Array<Symbol>>] Supported conversion pairs
96
+ def supported_conversions
97
+ [
98
+ %i[type1 otf],
99
+ %i[otf type1],
100
+ %i[type1 ttf],
101
+ %i[ttf type1],
102
+ ]
103
+ end
104
+
105
+ # Validate font for conversion
106
+ #
107
+ # @param font [Type1Font, OpenTypeFont, TrueTypeFont] Font to validate
108
+ # @param target_format [Symbol] Target format
109
+ # @return [Boolean] True if valid
110
+ # @raise [ArgumentError] If font is invalid
111
+ # @raise [Error] If conversion is not supported
112
+ def validate(font, target_format)
113
+ raise ArgumentError, "Font cannot be nil" if font.nil?
114
+
115
+ unless font.is_a?(Type1Font) || font.is_a?(SfntSource)
116
+ raise ArgumentError,
117
+ "Font must be a Type1Font or SfntSource instance"
118
+ end
119
+
120
+ source_format = detect_format(font)
121
+ unless supports?(source_format, target_format)
122
+ raise Fontisan::Error,
123
+ "Conversion #{source_format} → #{target_format} not supported"
124
+ end
125
+
126
+ true
127
+ end
128
+
129
+ # Methods below were private; made public for testability (TODO 30)
130
+
131
+ # Extract ConversionOptions from options hash
132
+ #
133
+ # @param options [Hash, ConversionOptions] Options or hash containing :options key
134
+ # @return [ConversionOptions, nil] Extracted ConversionOptions or nil
135
+ def extract_conversion_options(options)
136
+ return options if options.is_a?(ConversionOptions)
137
+
138
+ options[:options] if options.is_a?(Hash)
139
+ end
140
+
141
+ # Apply opening options to source font
142
+ #
143
+ # @param font [Type1Font] Source font
144
+ # @param conv_options [ConversionOptions] Conversion options with opening options
145
+ def apply_opening_options(font, conv_options)
146
+ return unless font.is_a?(Type1Font)
147
+ return unless conv_options
148
+
149
+ # Generate Unicode codepoints if requested
150
+ if conv_options.opening_option?(:generate_unicode)
151
+ generate_unicode_mappings(font)
152
+ end
153
+
154
+ # Decompose seac composites if requested
155
+ if conv_options.opening_option?(:decompose_composites)
156
+ decompose_seac_glyphs(font)
157
+ end
158
+
159
+ # Read all font dictionary records if requested.
160
+ if conv_options.opening_option?(:read_all_records) && font.font_dictionary
161
+ font.font_dictionary.parse(font.font_dictionary.raw_data)
162
+ end
163
+ end
164
+
165
+ # Generate Unicode codepoints from glyph names/encoding
166
+ #
167
+ # @param font [Type1Font] Source Type 1 font
168
+ def generate_unicode_mappings(_font)
169
+ # Placeholder: Generate Unicode mappings from glyph names
170
+ # A full implementation would:
171
+ # 1. Parse the Adobe Glyph List
172
+ # 2. Map glyph names to Unicode codepoints
173
+ # 3. Update the charstrings encoding
174
+ #
175
+ # For now, this is a no-op placeholder
176
+ nil
177
+ end
178
+
179
+ # Decompose seac composite glyphs to base glyphs
180
+ #
181
+ # @param font [Type1Font] Source Type 1 font
182
+ def decompose_seac_glyphs(font)
183
+ return unless font.charstrings
184
+
185
+ # Create SeacExpander to decompose composite glyphs
186
+ expander = Type1::SeacExpander.new(font.charstrings, font.private_dict)
187
+
188
+ # Get all composite glyphs
189
+ composites = expander.composite_glyphs
190
+ return if composites.empty?
191
+
192
+ # Decompose each composite glyph
193
+ composites.each do |glyph_name|
194
+ decomposed = expander.decompose(glyph_name)
195
+ next if decomposed.nil? || decomposed.empty?
196
+
197
+ # Update the CharString with decomposed version
198
+ # Access the charstrings hash directly and update
199
+ charstrings_hash = font.charstrings.charstrings
200
+ charstrings_hash[glyph_name] = decomposed
201
+
202
+ # Mark as decomposed (no longer a seac composite)
203
+ # The decomposed CharString no longer contains the seac operator
204
+ end
205
+ end
206
+
207
+ # Detect font format
208
+ #
209
+ # @param font [Type1Font, OpenTypeFont, TrueTypeFont] Font to detect
210
+ # @return [Symbol] Font format (:type1, :ttf, :otf)
211
+ def detect_format(font)
212
+ case font
213
+ when Type1Font
214
+ :type1
215
+ when TrueTypeFont
216
+ :ttf
217
+ when OpenTypeFont
218
+ :otf
219
+ else
220
+ # Try to detect from tables
221
+ if font.is_a?(SfntSource)
222
+ if font.tables.key?("glyf")
223
+ :ttf
224
+ elsif font.tables.key?("CFF ") || font.tables.key?("CFF2")
225
+ :otf
226
+ else
227
+ raise Fontisan::Error, "Cannot detect font format"
228
+ end
229
+ else
230
+ raise Fontisan::Error, "Unknown font type: #{font.class}"
231
+ end
232
+ end
233
+ end
234
+
235
+ # Detect target format from font class or options
236
+ #
237
+ # @param font [Type1Font, OpenTypeFont, TrueTypeFont] Source font
238
+ # @return [Symbol] Target format
239
+ def detect_target_format(font)
240
+ case font
241
+ when Type1Font
242
+ :otf # Default: Type 1 → OTF
243
+ when TrueTypeFont
244
+ :type1 # TTF → Type 1
245
+ when OpenTypeFont
246
+ :type1 # OTF → Type 1
247
+ else
248
+ :otf
249
+ end
250
+ end
251
+
252
+ # Convert Type 1 font to OpenType/CFF
253
+ #
254
+ # @param font [Type1Font] Source Type 1 font
255
+ # @param options [Hash] Conversion options
256
+ # @return [Hash<String, String>] Target tables including CFF table
257
+ def convert_type1_to_otf(font, _options = {})
258
+ # Convert Type 1 CharStrings to CFF format
259
+ converter = Type1::CharStringConverter.new(font.charstrings)
260
+ cff_charstrings = {}
261
+
262
+ font.charstrings.each_charstring do |glyph_name, charstring|
263
+ cff_charstrings[glyph_name] = converter.convert(charstring)
264
+ end
265
+
266
+ # Build font dictionary for CFF
267
+ font_dict = build_cff_font_dict(font)
268
+
269
+ # Build private dictionary for CFF
270
+ private_dict = build_cff_private_dict(font)
271
+
272
+ # Build CFF table
273
+ # Note: This is a simplified implementation
274
+ # A full implementation would build proper CFF INDEX structures
275
+ cff_data = build_cff_table_data(font, cff_charstrings, font_dict,
276
+ private_dict)
277
+
278
+ # Build other required SFNT tables
279
+ tables = {}
280
+
281
+ # Build head table
282
+ tables["head"] = build_head_table(font)
283
+
284
+ # Build hhea table
285
+ tables["hhea"] = build_hhea_table(font)
286
+
287
+ # Build maxp table
288
+ tables["maxp"] = build_maxp_table(font)
289
+
290
+ # Build name table
291
+ tables["name"] = build_name_table(font)
292
+
293
+ # Build OS/2 table
294
+ tables["OS/2"] = build_os2_table(font)
295
+
296
+ # Build post table
297
+ tables["post"] = build_post_table(font)
298
+
299
+ # Build cmap table
300
+ tables["cmap"] = build_cmap_table(font)
301
+
302
+ # Add CFF table
303
+ tables["CFF "] = cff_data
304
+
305
+ tables
306
+ end
307
+
308
+ # Convert OpenType/CFF font to Type 1
309
+ #
310
+ # @param font [OpenTypeFont] Source OpenType font
311
+ # @param options [Hash] Conversion options
312
+ # @return [Hash<String, String>] Type 1 font data as PFB
313
+ def convert_otf_to_type1(font, _options = {})
314
+ # Extract CFF table
315
+ cff_table = font.table("CFF ")
316
+ raise Fontisan::Error, "CFF table not found" unless cff_table
317
+
318
+ # Get CharStrings INDEX from CFF
319
+ charstrings_index = cff_table.charstrings_index(0)
320
+ unless charstrings_index
321
+ raise Fontisan::Error,
322
+ "CharStrings INDEX not found"
323
+ end
324
+
325
+ # Get Private DICT for context
326
+ private_dict = cff_table.private_dict(0)
327
+
328
+ # Create CFF to Type 1 converter
329
+ converter = Type1::CffToType1Converter.new(
330
+ nominal_width: private_dict&.nominal_width || 0,
331
+ default_width: private_dict&.default_width || 0,
332
+ )
333
+
334
+ # Convert each CFF CharString to Type 1 format
335
+ type1_charstrings = {}
336
+ glyph_count = charstrings_index.count
337
+
338
+ glyph_count.times do |glyph_index|
339
+ # Get raw CFF CharString data
340
+ cff_charstring = charstrings_index[glyph_index]
341
+ next unless cff_charstring
342
+
343
+ # Get glyph name
344
+ glyph_name = font.glyph_name(glyph_index) || "glyph#{glyph_index}"
345
+
346
+ # Convert CFF CharString to Type 1 format
347
+ private_dict_hash = build_private_dict_hash(private_dict)
348
+ type1_charstrings[glyph_name] = converter.convert(
349
+ cff_charstring,
350
+ private_dict: private_dict_hash,
351
+ )
352
+ end
353
+
354
+ # Build Type 1 font data
355
+ build_type1_data(font, type1_charstrings, cff_table)
356
+ end
357
+
358
+ # Convert Type 1 font to TrueType (via OTF)
359
+ #
360
+ # @param font [Type1Font] Source Type 1 font
361
+ # @param options [Hash] Conversion options
362
+ # @return [Hash<String, String>] Target tables including glyf table
363
+ def convert_type1_to_ttf(font, options = {})
364
+ # First convert to OTF
365
+ otf_tables = convert_type1_to_otf(font, options)
366
+
367
+ # Then use OutlineConverter to convert OTF to TTF
368
+ # Create a temporary OTF font object
369
+ temp_otf = OpenTypeFont.new
370
+ otf_tables.each do |tag, data|
371
+ temp_otf.tables[tag] = data
372
+ end
373
+
374
+ # Use OutlineConverter for OTF → TTF
375
+ outline_converter = OutlineConverter.new(
376
+ optimize_cff: @optimize_cff,
377
+ preserve_hints: @preserve_hints,
378
+ target_format: :ttf,
379
+ )
380
+
381
+ outline_converter.convert(temp_otf, target_format: :ttf)
382
+ end
383
+
384
+ # Convert TrueType font to Type 1 (via OTF)
385
+ #
386
+ # @param font [TrueTypeFont] Source TrueType font
387
+ # @return [Hash<String, String>] Type 1 font data as PFB
388
+ def convert_ttf_to_type1(font)
389
+ # First use OutlineConverter to convert TTF to OTF
390
+ outline_converter = OutlineConverter.new(
391
+ optimize_cff: @optimize_cff,
392
+ preserve_hints: @preserve_hints,
393
+ target_format: :otf,
394
+ )
395
+
396
+ otf_tables = outline_converter.convert(font, target_format: :otf)
397
+
398
+ # Create a temporary OTF font object
399
+ temp_otf = OpenTypeFont.new
400
+ otf_tables.each do |tag, data|
401
+ temp_otf.tables[tag] = data
402
+ end
403
+
404
+ # Then convert OTF to Type 1
405
+ convert_otf_to_type1(temp_otf)
406
+ end
407
+
408
+ # Build CFF font dictionary from Type 1 font
409
+ #
410
+ # @param font [Type1Font] Source Type 1 font
411
+ # @return [Hash] CFF font dictionary data
412
+ def build_cff_font_dict(font)
413
+ {
414
+ version: font.font_dictionary.version || "001.000",
415
+ notice: font.font_dictionary.notice || "",
416
+ copyright: font.font_dictionary.copyright || "",
417
+ full_name: font.font_dictionary.full_name || font.font_name,
418
+ family_name: font.font_dictionary.family_name || font.font_name,
419
+ weight: font.font_dictionary.weight || "Medium",
420
+ font_b_box: font.font_dictionary.font_bbox || [0, 0, 0, 0],
421
+ font_matrix: font.font_dictionary.font_matrix || [0.001, 0, 0, 0.001,
422
+ 0, 0],
423
+ charset: font.charstrings.encoding.keys,
424
+ encoding: font.charstrings.encoding,
425
+ }
426
+ end
427
+
428
+ # Build CFF private dictionary from Type 1 font
429
+ #
430
+ # @param font [Type1Font] Source Type 1 font
431
+ # @return [Hash] CFF private dictionary data
432
+ def build_cff_private_dict(font)
433
+ private_dict = font.private_dict
434
+ {
435
+ blue_values: private_dict.blue_values || [],
436
+ other_blues: private_dict.other_blues || [],
437
+ family_blues: private_dict.family_blues || [],
438
+ family_other_blues: private_dict.family_other_blues || [],
439
+ blue_scale: private_dict.blue_scale || 0.039625,
440
+ blue_shift: private_dict.blue_shift || 7,
441
+ blue_fuzz: private_dict.blue_fuzz || 1,
442
+ std_hw: private_dict.std_hw || 0,
443
+ std_vw: private_dict.std_vw || 0,
444
+ stem_snap_h: private_dict.stem_snap_h || [],
445
+ stem_snap_v: private_dict.stem_snap_v || [],
446
+ force_bold: private_dict.force_bold || false,
447
+ language_group: private_dict.language_group || 0,
448
+ expansion_factor: private_dict.expansion_factor || 0.06,
449
+ initial_random_seed: private_dict.initial_random_seed || 0,
450
+ }
451
+ end
452
+
453
+ # Build CFF table data
454
+ #
455
+ # @param font [Type1Font] Source Type 1 font
456
+ # @param charstrings [Hash] CFF CharStrings (glyph_name => data)
457
+ # @param font_dict [Hash] CFF font dictionary (not used, kept for compatibility)
458
+ # @param private_dict [Hash] CFF private dictionary (not used, kept for compatibility)
459
+ # @return [String] CFF table binary data
460
+ def build_cff_table_data(font, charstrings, _font_dict, _private_dict)
461
+ # Convert charstrings hash to array (build_cff_table expects array)
462
+ charstrings_array = charstrings.values
463
+
464
+ # Build CFF table using CffTableBuilder
465
+ # We need to pass the Type1Font as-is for metadata extraction
466
+ build_cff_table(charstrings_array, [], font)
467
+ end
468
+
469
+ # Override extract_font_name to handle Type1Font
470
+ #
471
+ # @param font [Type1Font, TrueTypeFont, OpenTypeFont] Font
472
+ # @return [String] Font name
473
+ def extract_font_name(font)
474
+ if font.is_a?(Type1Font)
475
+ # Get font name from Type1Font
476
+ name = font.font_name || font.font_dictionary&.font_name
477
+ return name.dup.force_encoding("ASCII-8BIT") if name
478
+ end
479
+
480
+ # Fall back to original implementation for TrueTypeFont/OpenTypeFont
481
+ super
482
+ end
483
+
484
+ # Build Type 1 Private dictionary hash from CFF Private dict
485
+ #
486
+ # @param private_dict [Tables::Cff::PrivateDict] CFF Private dict
487
+ # @return [Hash] Private dictionary as hash for Type 1
488
+ def build_private_dict_hash(private_dict)
489
+ return {} unless private_dict
490
+
491
+ {
492
+ nominal_width: private_dict.nominal_width,
493
+ default_width: private_dict.default_width,
494
+ blue_values: private_dict.blue_values || [],
495
+ other_blues: private_dict.other_blues || [],
496
+ family_blues: private_dict.family_blues || [],
497
+ family_other_blues: private_dict.family_other_blues || [],
498
+ blue_scale: private_dict.blue_scale || 0.039625,
499
+ blue_shift: private_dict.blue_shift || 7,
500
+ blue_fuzz: private_dict.blue_fuzz || 1,
501
+ std_hw: private_dict.std_hw || 0,
502
+ std_vw: private_dict.std_vw || 0,
503
+ stem_snap_h: private_dict.stem_snap_h || [],
504
+ stem_snap_v: private_dict.stem_snap_v || [],
505
+ force_bold: private_dict.force_bold || false,
506
+ language_group: private_dict.language_group || 0,
507
+ expansion_factor: private_dict.expansion_factor || 0.06,
508
+ initial_random_seed: private_dict.initial_random_seed || 0,
509
+ }
510
+ end
511
+
512
+ # Build Type 1 font data
513
+ #
514
+ # @param font [OpenTypeFont] Source OpenType font
515
+ # @param charstrings [Hash] Type 1 CharStrings
516
+ # @param cff_table [Tables::Cff] CFF table for metadata
517
+ # @return [Hash] Type 1 font data with :pfb key
518
+ def build_type1_data(_font, _charstrings, _cff_table)
519
+ # Build PFB format
520
+ # This is a placeholder implementation
521
+ # Full implementation requires:
522
+ # 1. Build Font Dictionary
523
+ # 2. Build Private Dictionary
524
+ # 3. Build CharStrings
525
+ # 4. Encrypt with eexec
526
+ # 5. Format as PFB chunks
527
+
528
+ pfb_data = String.new(encoding: Encoding::ASCII_8BIT)
529
+
530
+ { pfb: pfb_data }
531
+ end
532
+
533
+ # Build head table from Type 1 font
534
+ #
535
+ # @param font [Type1Font] Source Type 1 font
536
+ # @return [String] head table binary data
537
+ def build_head_table(font)
538
+ data = (+"").b
539
+
540
+ # Get font metadata from Type1Font
541
+ font_bbox = font.font_dictionary&.font_bbox || [0, 0, 1000, 1000]
542
+ version_str = font.version || "001.000"
543
+
544
+ # Parse version (e.g., "001.000" => 1.0)
545
+ version_parts = version_str.split(".")
546
+ major = version_parts[0].to_i
547
+ minor = version_parts[1].to_i
548
+ version = major + (minor / 1000.0)
549
+
550
+ # Version (Fixed 16.16) - stored as int32
551
+ integer_part = version.to_i
552
+ fractional_part = ((version - integer_part) * 65_536).to_i
553
+ version_raw = (integer_part << 16) | fractional_part
554
+ data << [version_raw].pack("N")
555
+
556
+ # Font Revision (Fixed 16.16) - default to 1.0
557
+ font_revision_raw = 0x00010000
558
+ data << [font_revision_raw].pack("N")
559
+
560
+ # Checksum Adjustment (uint32) - will be calculated later
561
+ data << [0].pack("N")
562
+
563
+ # Magic Number (uint32)
564
+ data << [0x5F0F3CF5].pack("N")
565
+
566
+ # Flags (uint16) - bit 0 indicates y direction (0 = mixed)
567
+ data << [0].pack("n")
568
+
569
+ # Units Per Em (uint16) - Type 1 standard is 1000
570
+ data << [1000].pack("n")
571
+
572
+ # Created (LONGDATETIME) - use current time
573
+ data << [Tables::Head.now_longdatetime].pack("Q>")
574
+
575
+ # Modified (LONGDATETIME) - use current time
576
+ data << [Tables::Head.now_longdatetime].pack("Q>")
577
+
578
+ # Bounding box (int16 each)
579
+ data << [font_bbox[0]].pack("s>") # x_min
580
+ data << [font_bbox[1]].pack("s>") # y_min
581
+ data << [font_bbox[2]].pack("s>") # x_max
582
+ data << [font_bbox[3]].pack("s>") # y_max
583
+
584
+ # Mac Style (uint16) - no style bits set
585
+ data << [0].pack("n")
586
+
587
+ # Lowest Rec PPEM (uint16) - readable size
588
+ data << [8].pack("n")
589
+
590
+ # Font Direction Hint (int16)
591
+ # 2 = Left to right, mixed glyphs
592
+ data << [2].pack("s>")
593
+
594
+ # Index To Loc Format (int16)
595
+ # 0 = short offsets (for CFF fonts we use this)
596
+ data << [0].pack("s>")
597
+
598
+ # Glyph Data Format (int16)
599
+ data << [0].pack("s>")
600
+
601
+ data
602
+ end
603
+
604
+ # Build hhea table from Type 1 font
605
+ #
606
+ # @param font [Type1Font] Source Type 1 font
607
+ # @return [String] hhea table binary data
608
+ def build_hhea_table(font)
609
+ data = (+"").b
610
+
611
+ # Get font metrics from Type1Font
612
+ font_bbox = font.font_dictionary&.font_bbox || [0, 0, 1000, 1000]
613
+ blue_values = font.private_dict&.blue_values || []
614
+
615
+ # Version (Fixed 16.16) - 0x00010000 (1.0)
616
+ data << [0x00010000].pack("N")
617
+
618
+ # Ascent (int16) - Distance from baseline to highest ascender
619
+ # Use BlueValues[2] or [3] if available, otherwise font_bbox[3]
620
+ ascent = if blue_values.length >= 4
621
+ blue_values[3] # Top zone top
622
+ elsif blue_values.length >= 3
623
+ blue_values[2] # Top zone bottom
624
+ else
625
+ font_bbox[3] # y_max
626
+ end
627
+ data << [ascent].pack("s>")
628
+
629
+ # Descent (int16) - Distance from baseline to lowest descender (negative)
630
+ # Use BlueValues[0] or [1] if available, otherwise font_bbox[1]
631
+ descent = if blue_values.length >= 2
632
+ blue_values[0] # Bottom zone bottom (negative)
633
+ elsif blue_values.length >= 1
634
+ blue_values[0]
635
+ else
636
+ font_bbox[1] # y_min (should be negative)
637
+ end
638
+ data << [descent].pack("s>")
639
+
640
+ # Line Gap (int16) - Additional space between lines
641
+ # Use typical value of 0 for Type 1 fonts
642
+ data << [0].pack("s>")
643
+
644
+ # Advance Width Max (uint16)
645
+ # Type 1 standard is typically 1000, use font_bbox width + padding
646
+ advance_max = (font_bbox[2] - font_bbox[0]) + 100
647
+ data << [advance_max].pack("n")
648
+
649
+ # Min Left Side Bearing (int16)
650
+ # Use font_bbox[0] (x_min) as reasonable default
651
+ data << [font_bbox[0]].pack("s>")
652
+
653
+ # Min Right Side Bearing (int16)
654
+ # Estimate as 0 (will be updated if actual metrics available)
655
+ data << [0].pack("s>")
656
+
657
+ # x Max Extent (int16) - Max(lsb + xMax)
658
+ # Use font_bbox[2] (x_max) as reasonable default
659
+ data << [font_bbox[2]].pack("s>")
660
+
661
+ # Caret Slope Rise (int16)
662
+ # 1 for upright fonts (not italic)
663
+ data << [1].pack("s>")
664
+
665
+ # Caret Slope Run (int16)
666
+ # 0 for upright fonts
667
+ data << [0].pack("s>")
668
+
669
+ # Caret Offset (int16)
670
+ # Set to 0 for standard fonts
671
+ data << [0].pack("s>")
672
+
673
+ # Reserved (int64) - 8 bytes of zeros
674
+ data << [0, 0].pack("Q>")
675
+
676
+ # Metric Data Format (int16)
677
+ # 0 for current format
678
+ data << [0].pack("s>")
679
+
680
+ # Number of HMetrics (uint16)
681
+ # Number of glyphs with explicit metrics (typically all glyphs)
682
+ num_glyphs = font.charstrings&.count || 1
683
+ data << [[num_glyphs, 1].max].pack("n")
684
+
685
+ data
686
+ end
687
+
688
+ # Build maxp table from Type 1 font
689
+ #
690
+ # @param font [Type1Font] Source Type 1 font
691
+ # @return [String] maxp table binary data
692
+ def build_maxp_table(font)
693
+ data = (+"").b
694
+
695
+ # Get number of glyphs from Type1Font
696
+ num_glyphs = font.charstrings&.count || 1
697
+
698
+ # Version (Fixed 16.16)
699
+ # For CFF fonts (OTF output), use version 0.5 (0x00005000)
700
+ # For TrueType fonts (TTF output), would use version 1.0 (0x00010000)
701
+ # Type 1 fonts convert to CFF-based OTF, so use version 0.5
702
+ data << [0x00005000].pack("N")
703
+
704
+ # Number of Glyphs (uint16)
705
+ # Must be >= 1 (at minimum, .notdef must be present)
706
+ data << [[num_glyphs, 1].max].pack("n")
707
+
708
+ data
709
+ end
710
+
711
+ # Build name table from Type 1 font
712
+ #
713
+ # @param font [Type1Font] Source Type 1 font
714
+ # @return [String] name table binary data
715
+ def build_name_table(font)
716
+ # Get font metadata from Type1Font
717
+ font_dict = font.font_dictionary
718
+ font_info = font_dict&.font_info
719
+
720
+ # Extract font names with fallbacks
721
+ font_name = font.font_name || font_dict&.font_name || "Unnamed"
722
+ family_name = font_info&.family_name || font_dict&.family_name || font_name
723
+ full_name = font_info&.full_name || font_dict&.full_name || family_name
724
+ version = font_info&.version || font.version || "001.000"
725
+ copyright = font_info&.copyright || font_dict&.raw_data&.dig(:copyright) || ""
726
+ postscript_name = font_name
727
+ weight = font_info&.weight || "Regular"
728
+ notice = font_info&.notice || ""
729
+
730
+ # Build name records (Windows Unicode, English US)
731
+ # Platform ID 3 (Windows), Encoding ID 1 (Unicode BMP), Language ID 0x0409 (US English)
732
+ name_records = [
733
+ # Copyright (name ID 0)
734
+ { name_id: 0, string: copyright },
735
+ # Family Name (name ID 1)
736
+ { name_id: 1, string: family_name },
737
+ # Subfamily Name (name ID 2) - derive from weight or default to Regular
738
+ { name_id: 2, string: weight || "Regular" },
739
+ # Unique ID (name ID 3) - format: version;copyright;postscript_name
740
+ { name_id: 3, string: "#{version};#{copyright};#{postscript_name}" },
741
+ # Full Name (name ID 4)
742
+ { name_id: 4, string: full_name },
743
+ # Version (name ID 5)
744
+ { name_id: 5, string: version },
745
+ # PostScript Name (name ID 6)
746
+ { name_id: 6, string: postscript_name },
747
+ # Trademark (name ID 7) - use notice if available
748
+ { name_id: 7, string: notice || "" },
749
+ ]
750
+
751
+ # Filter out empty strings and build string storage
752
+ name_records = name_records.select do |r|
753
+ !r[:string].nil? && !r[:string].empty?
754
+ end
755
+
756
+ # Build string storage (UTF-16BE encoded for Windows platform)
757
+ string_storage = (+"").b
758
+ name_records.each do |record|
759
+ encoded_string = record[:string].encode("UTF-16BE").force_encoding("ASCII-8BIT")
760
+ record[:encoded] = encoded_string
761
+ record[:offset] = string_storage.bytesize
762
+ string_storage << encoded_string
763
+ end
764
+
765
+ # Build name table
766
+ data = (+"").b
767
+
768
+ # Format selector (uint16) - 0 for basic
769
+ data << [0].pack("n")
770
+
771
+ # Count (uint16) - number of name records
772
+ data << [name_records.size].pack("n")
773
+
774
+ # String offset (uint16) - offset to string storage from start of table
775
+ # Header is 6 bytes, each name record is 12 bytes
776
+ string_data_offset = 6 + (name_records.size * 12)
777
+ data << [string_data_offset].pack("n")
778
+
779
+ # Write name records
780
+ platform_id = 3 # Windows
781
+ encoding_id = 1 # Unicode BMP
782
+ language_id = 0x0409 # US English
783
+
784
+ name_records.each do |record|
785
+ data << [platform_id].pack("n") # platform ID
786
+ data << [encoding_id].pack("n") # encoding ID
787
+ data << [language_id].pack("n") # language ID
788
+ data << [record[:name_id]].pack("n") # name ID
789
+ data << [record[:encoded].bytesize].pack("n") # string length
790
+ data << [record[:offset]].pack("n") # string offset
791
+ end
792
+
793
+ # Write string storage
794
+ data << string_storage
795
+
796
+ data
797
+ end
798
+
799
+ # Build OS/2 table from Type 1 font
800
+ #
801
+ # @param font [Type1Font] Source Type 1 font
802
+ # @return [String] OS/2 table binary data
803
+ def build_os2_table(font)
804
+ data = (+"").b
805
+
806
+ # Get font metadata from Type1Font
807
+ font_bbox = font.font_dictionary&.font_bbox || [0, 0, 1000, 1000]
808
+ blue_values = font.private_dict&.blue_values || []
809
+ font_info = font.font_dictionary&.font_info || {}
810
+ weight = font_info.weight || "Medium"
811
+
812
+ # Determine weight class (100-900)
813
+ # Order matters - more specific patterns must come first
814
+ weight_class = case weight.to_s.downcase
815
+ when /thin/ then 100
816
+ when /extralight/ then 200
817
+ when /light/ then 300
818
+ when /regular|normal/ then 400
819
+ when /medium/ then 400
820
+ when /semibold|semib/ then 600
821
+ when /extrabold/ then 800
822
+ when /bold/ then 700
823
+ when /black|heavy/ then 900
824
+ else 400
825
+ end
826
+
827
+ # Version (uint16) - Use version 4 for modern fonts
828
+ data << [4].pack("n")
829
+
830
+ # xAvgCharWidth (int16) - Average character width
831
+ # Use font width estimate
832
+ avg_width = ((font_bbox[2] - font_bbox[0]) * 0.5).to_i
833
+ data << [avg_width].pack("s>")
834
+
835
+ # usWeightClass (uint16)
836
+ data << [weight_class].pack("n")
837
+
838
+ # usWidthClass (uint16) - 1 = Ultra-condensed to 9 = Ultra-expanded
839
+ # Default to 5 (Medium)
840
+ data << [5].pack("n")
841
+
842
+ # fsType (uint16) - Embedding permissions
843
+ # 0 = Installable embedding, 8 = Restricted (use 0 as default)
844
+ data << [0].pack("n")
845
+
846
+ # ySubscriptXSize (int16)
847
+ data << [650].pack("s>")
848
+
849
+ # ySubscriptYSize (int16)
850
+ data << [600].pack("s>")
851
+
852
+ # ySubscriptXOffset (int16)
853
+ data << [0].pack("s>")
854
+
855
+ # ySubscriptYOffset (int16)
856
+ data << [75].pack("s>")
857
+
858
+ # ySuperscriptXSize (int16)
859
+ data << [650].pack("s>")
860
+
861
+ # ySuperscriptYSize (int16)
862
+ data << [600].pack("s>")
863
+
864
+ # ySuperscriptXOffset (int16)
865
+ data << [0].pack("s>")
866
+
867
+ # ySuperscriptYOffset (int16)
868
+ data << [350].pack("s>")
869
+
870
+ # yStrikeoutSize (int16)
871
+ data << [50].pack("s>")
872
+
873
+ # yStrikeoutPosition (int16)
874
+ data << [300].pack("s>")
875
+
876
+ # sFamilyClass (int16) - Family class and subclass
877
+ # 0 = No classification
878
+ data << [0].pack("s>")
879
+
880
+ # PANOSE (10 bytes) - Use default Latin Text family
881
+ # Family: 2 (Text and Display), Serif Style: 11 (Normal Sans)
882
+ panose = [
883
+ 2, # Family kind: Latin Text
884
+ 11, # Serif style: Normal Sans
885
+ 5, # Weight: Medium
886
+ 5, # Proportion: Modern
887
+ 2, # Contrast: Medium Low
888
+ 5, # Stroke variation: Medium
889
+ 5, # Arm style: Straight arms/serifs
890
+ 5, # Letter form: Normal
891
+ 4, # Midline: Standard
892
+ 3, # X-height: Medium
893
+ ]
894
+ data << panose.pack("C*")
895
+
896
+ # Unicode ranges (4 x uint32) - Basic Latin + Latin-1
897
+ # Bits 0-31: Basic Latin, Latin-1, Latin Extended-A/B, etc.
898
+ data << [0x00000001].pack("N") # Basic Latin (0-7F)
899
+ data << [0x00000000].pack("N")
900
+ data << [0x00000000].pack("N")
901
+ data << [0x00000000].pack("N")
902
+
903
+ # achVendID (4 bytes) - Vendor ID
904
+ data << "UKWN" # Unknown
905
+
906
+ # fsSelection (uint16) - Font selection flags
907
+ # Bit 6 (0x40) = Regular weight if 400-500
908
+ fs_selection = if weight_class >= 400 && weight_class <= 500
909
+ 0x40 # REGULAR
910
+ elsif weight_class >= 700
911
+ 0x20 # BOLD
912
+ else
913
+ 0
914
+ end
915
+ data << [fs_selection].pack("n")
916
+
917
+ # usFirstCharIndex (uint16) - First Unicode character
918
+ data << [32].pack("n") # Space
919
+
920
+ # usLastCharIndex (uint16) - Last Unicode character
921
+ data << [0xFFFD].pack("n") # Replacement character
922
+
923
+ # sTypoAscender (int16) - Use BlueValues or font bbox
924
+ typo_ascender = if blue_values.length >= 4
925
+ blue_values[3]
926
+ else
927
+ font_bbox[3]
928
+ end
929
+ data << [typo_ascender].pack("s>")
930
+
931
+ # sTypoDescender (int16) - Use BlueValues or font bbox (negative)
932
+ typo_descender = if blue_values.length >= 2
933
+ blue_values[0]
934
+ else
935
+ font_bbox[1]
936
+ end
937
+ data << [typo_descender].pack("s>")
938
+
939
+ # sTypoLineGap (int16)
940
+ data << [0].pack("s>")
941
+
942
+ # usWinAscent (uint16)
943
+ data << [[font_bbox[3], 1000].max].pack("n")
944
+
945
+ # usWinDescent (uint16)
946
+ data << [[-font_bbox[1], 200].max].pack("n")
947
+
948
+ # ulCodePageRange1 (uint32) - Latin 1
949
+ data << [0x00000001].pack("N")
950
+
951
+ # ulCodePageRange2 (uint32)
952
+ data << [0x00000000].pack("N")
953
+
954
+ # sxHeight (int16) - x-height, approximate as 500 for 1000 UPM
955
+ data << [500].pack("s>")
956
+
957
+ # sCapHeight (int16) - Cap height, approximate as 700 for 1000 UPM
958
+ data << [700].pack("s>")
959
+
960
+ # usDefaultChar (uint16)
961
+ data << [0].pack("n")
962
+
963
+ # usBreakChar (uint16) - Space
964
+ data << [32].pack("n")
965
+
966
+ # usMaxContext (uint16)
967
+ data << [0].pack("n")
968
+
969
+ data
970
+ end
971
+
972
+ # Build post table from Type 1 font
973
+ #
974
+ # @param font [Type1Font] Source Type 1 font
975
+ # @return [String] post table binary data
976
+ def build_post_table(font)
977
+ data = (+"").b
978
+
979
+ # Get font metadata from Type1Font
980
+ font_info = font.font_dictionary&.font_info || {}
981
+
982
+ # Version (Fixed 16.16) - Use version 3.0 for CFF fonts (no glyph names)
983
+ # Version 2.0 would include glyph names, but for OTF output version 3.0 is fine
984
+ # since CFF table contains the glyph names
985
+ data << [0x00030000].pack("N") # Version 3.0
986
+
987
+ # Italic Angle (Fixed 16.16)
988
+ # Get from FontInfo if available, otherwise default to 0
989
+ italic_angle = font_info.italic_angle || 0
990
+ angle_raw = (italic_angle * 65_536).to_i
991
+ data << [angle_raw].pack("N")
992
+
993
+ # Underline Position (int16)
994
+ underline_position = font_info.underline_position || -100
995
+ data << [underline_position].pack("s>")
996
+
997
+ # Underline Thickness (int16)
998
+ underline_thickness = font_info.underline_thickness || 50
999
+ data << [underline_thickness].pack("s>")
1000
+
1001
+ # Fixed Pitch (uint32) - Boolean for monospace
1002
+ is_fixed_pitch = font_info.is_fixed_pitch || false ? 1 : 0
1003
+ data << [is_fixed_pitch].pack("N")
1004
+
1005
+ # Min/Max Memory for Type 42 (uint32 each) - Not used for CFF, set to 0
1006
+ data << [0].pack("N") # min_mem_type42
1007
+ data << [0].pack("N") # max_mem_type42
1008
+
1009
+ # Min/Max Memory for Type 1 (uint32 each) - Not used for CFF, set to 0
1010
+ data << [0].pack("N") # min_mem_type1
1011
+ data << [0].pack("N") # max_mem_type1
1012
+
1013
+ data
1014
+ end
1015
+
1016
+ # Build cmap table from Type 1 font
1017
+ #
1018
+ # @param font [Type1Font] Source Type 1 font
1019
+ # @return [String] cmap table binary data
1020
+ def build_cmap_table(font)
1021
+ data = (+"").b
1022
+
1023
+ # Get encoding from Type1Font
1024
+ encoding = font.charstrings&.encoding || {}
1025
+ glyph_names = font.charstrings&.glyph_names || encoding.keys
1026
+
1027
+ # Build Unicode mapping from glyph names using AGL
1028
+ unicode_to_glyph = {}
1029
+ glyph_index = 0
1030
+
1031
+ glyph_names.each do |glyph_name|
1032
+ # Get Unicode code point from AGL
1033
+ unicode = Type1::AGL.unicode_for_glyph_name(glyph_name)
1034
+
1035
+ # If no Unicode mapping, try to derive from encoding position
1036
+ if unicode.nil? && (glyph_index < 128)
1037
+ # For standard encoding, try to map from position
1038
+ # This is a simplified approach - real implementation would be more robust
1039
+ unicode = glyph_index
1040
+ end
1041
+
1042
+ # Map Unicode to glyph index
1043
+ if unicode && unicode <= 0xFFFF
1044
+ unicode_to_glyph[unicode] ||= glyph_index
1045
+ end
1046
+
1047
+ glyph_index += 1
1048
+ end
1049
+
1050
+ # Ensure at least .notdef (glyph 0) maps to something
1051
+ unicode_to_glyph[0x0000] ||= 0
1052
+
1053
+ # Build Format 4 subtable (Segment mapping to delta values)
1054
+ # This is the most common format for BMP Unicode fonts
1055
+ subtable_data = build_cmap_format_4(unicode_to_glyph)
1056
+
1057
+ # Calculate offsets
1058
+ encoding_records_offset = 4 # After version (2) + num_tables (2)
1059
+ subtable_offset = encoding_records_offset + 8 # After one encoding record (8 bytes)
1060
+
1061
+ # Build cmap table header
1062
+ # Version (uint16)
1063
+ data << [0].pack("n")
1064
+
1065
+ # Number of encoding records (uint16)
1066
+ data << [1].pack("n") # One encoding record
1067
+
1068
+ # Encoding record: Platform ID (uint16), Encoding ID (uint16), Subtable offset (uint32)
1069
+ # Platform 3 (Windows), Encoding 1 (Unicode BMP)
1070
+ data << [3].pack("n") # Platform ID: Windows
1071
+ data << [1].pack("n") # Encoding ID: Unicode BMP
1072
+ data << [subtable_offset].pack("N") # Subtable offset
1073
+
1074
+ # Append subtable data
1075
+ data << subtable_data
1076
+
1077
+ data
1078
+ end
1079
+
1080
+ # Build cmap format 4 subtable
1081
+ #
1082
+ # @param unicode_to_glyph [Hash<Integer, Integer>] Unicode to glyph index mapping
1083
+ # @return [String] Format 4 subtable binary data
1084
+ def build_cmap_format_4(unicode_to_glyph)
1085
+ data = (+"").b
1086
+
1087
+ # Get sorted Unicode values
1088
+ unicode_values = unicode_to_glyph.keys.sort
1089
+ return data if unicode_values.empty?
1090
+
1091
+ # For simplicity, create segments for continuous ranges
1092
+ # A more sophisticated implementation would optimize this
1093
+ segments = []
1094
+ current_segment = nil
1095
+
1096
+ unicode_values.each do |unicode|
1097
+ glyph_id = unicode_to_glyph[unicode]
1098
+
1099
+ if current_segment.nil?
1100
+ current_segment = {
1101
+ start: unicode,
1102
+ end: unicode,
1103
+ start_glyph: glyph_id,
1104
+ glyphs: [glyph_id],
1105
+ }
1106
+ elsif unicode == current_segment[:end] + 1 && glyph_id == current_segment[:glyphs].last + 1
1107
+ # Continue current segment (sequential)
1108
+ current_segment[:end] = unicode
1109
+ current_segment[:glyphs] << glyph_id
1110
+ else
1111
+ # Start new segment
1112
+ segments << current_segment
1113
+ current_segment = {
1114
+ start: unicode,
1115
+ end: unicode,
1116
+ start_glyph: glyph_id,
1117
+ glyphs: [glyph_id],
1118
+ }
1119
+ end
1120
+ end
1121
+
1122
+ segments << current_segment if current_segment
1123
+
1124
+ # Add end segment marker (0xFFFF)
1125
+ segments << { start: 0xFFFF, end: 0xFFFF, start_glyph: 0, glyphs: [0] }
1126
+
1127
+ # Calculate segment count and related values
1128
+ seg_count = segments.length
1129
+ seg_count_x2 = seg_count * 2
1130
+ search_range = 2**Math.log2(seg_count).to_i * 2
1131
+ entry_selector = Math.log2(search_range / 2).to_i
1132
+ range_shift = (seg_count - search_range / 2) * 2
1133
+
1134
+ # Build format 4 subtable header (14 bytes)
1135
+ data << [4].pack("n") # Format
1136
+ data << [calculate_cmap4_length(segments)].pack("n") # Length (placeholder)
1137
+ data << [0].pack("n") # Language (0 = independent)
1138
+ data << [seg_count_x2].pack("n") # segCountX2
1139
+ data << [search_range].pack("n") # searchRange
1140
+ data << [entry_selector].pack("n") # entrySelector
1141
+ data << [range_shift].pack("n") # rangeShift
1142
+
1143
+ # Build segment arrays
1144
+ end_codes = []
1145
+ start_codes = []
1146
+ id_deltas = []
1147
+ id_range_offsets = []
1148
+ glyph_id_array = []
1149
+
1150
+ segments.each do |seg|
1151
+ end_codes << seg[:end]
1152
+ start_codes << seg[:start]
1153
+
1154
+ # For sequential glyphs, use delta
1155
+ if seg[:start] == 0xFFFF
1156
+ # End segment marker
1157
+ id_deltas << 1
1158
+ id_range_offsets << 0
1159
+ elsif seg[:end] - seg[:start] == seg[:glyphs].length - 1
1160
+ # Sequential: use delta
1161
+ id_deltas << (seg[:start_glyph] - seg[:start])
1162
+ id_range_offsets << 0
1163
+ else
1164
+ # Non-sequential: use glyph ID array
1165
+ id_deltas << 0
1166
+ id_range_offsets << (glyph_id_array.length * 2 + 2)
1167
+ glyph_id_array.concat(seg[:glyphs])
1168
+ end
1169
+ end
1170
+
1171
+ # Write arrays (padded to even length)
1172
+ end_codes.each { |code| data << [code].pack("n") }
1173
+ data << [0].pack("n") # Reserved padding
1174
+ start_codes.each { |code| data << [code].pack("n") }
1175
+ id_deltas.each { |delta| data << [delta].pack("s>") } # Signed
1176
+ id_range_offsets.each { |offset| data << [offset].pack("n") }
1177
+ glyph_id_array.each { |gid| data << [gid].pack("n") }
1178
+
1179
+ # Update length in header
1180
+ length = data.bytesize
1181
+ data[2..3] = [length].pack("n")
1182
+
1183
+ data
1184
+ end
1185
+
1186
+ # Calculate length for format 4 subtable
1187
+ #
1188
+ # @param segments [Array<Hash>] Segment definitions
1189
+ # @return [Integer] Estimated length
1190
+ def calculate_cmap4_length(segments)
1191
+ # Header: 14 bytes
1192
+ # Arrays: seg_count * 2 bytes each
1193
+ # Glyph ID array: variable
1194
+ seg_count = segments.length
1195
+
1196
+ # Rough estimate (actual calculation done during construction)
1197
+ 14 + (seg_count * 8) + (seg_count * 2) + 100 # 100 for glyph ID array estimate
1198
+ end
1199
+ end
1200
+ end
1201
+ end