tTFData.ml 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. (*
  2. * Copyright (C)2005-2014 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. *)
  22. type header = {
  23. hd_major_version : int;
  24. hd_minor_version : int;
  25. hd_num_tables : int;
  26. hd_search_range : int;
  27. hd_entry_selector : int;
  28. hd_range_shift : int;
  29. }
  30. type entry = {
  31. entry_table_name : string;
  32. entry_checksum : int32;
  33. entry_offset : int32;
  34. entry_length: int32;
  35. }
  36. (* GLYF *)
  37. type glyf_header = {
  38. gh_num_contours : int;
  39. gh_xmin : int;
  40. gh_ymin : int;
  41. gh_xmax : int;
  42. gh_ymax : int;
  43. }
  44. type glyf_simple = {
  45. gs_end_pts_of_contours : int array;
  46. gs_instruction_length : int;
  47. gs_instructions : char array;
  48. gs_flags : int array;
  49. gs_x_coordinates : int array;
  50. gs_y_coordinates : int array;
  51. }
  52. type transformation_option =
  53. | NoScale
  54. | Scale of float
  55. | ScaleXY of float * float
  56. | ScaleMatrix of float * float * float * float
  57. type glyf_component = {
  58. gc_flags : int;
  59. gc_glyf_index : int;
  60. gc_arg1 : int;
  61. gc_arg2 : int;
  62. gc_transformation : transformation_option;
  63. }
  64. type glyf =
  65. | TGlyfSimple of glyf_header * glyf_simple
  66. | TGlyfComposite of glyf_header * glyf_component list
  67. | TGlyfNull
  68. (* HMTX *)
  69. type hmtx = {
  70. advance_width : int;
  71. left_side_bearing : int;
  72. }
  73. (* CMAP *)
  74. type cmap_subtable_header = {
  75. csh_platform_id : int;
  76. csh_platform_specific_id : int;
  77. csh_offset : int32;
  78. }
  79. type cmap_format_0 = {
  80. c0_format : int;
  81. c0_length : int;
  82. c0_language : int;
  83. c0_glyph_index_array : char array;
  84. }
  85. type cmap_format_4 = {
  86. c4_format : int;
  87. c4_length : int;
  88. c4_language : int;
  89. c4_seg_count_x2 : int;
  90. c4_search_range : int;
  91. c4_entry_selector : int;
  92. c4_range_shift : int;
  93. c4_end_code : int array;
  94. c4_reserved_pad : int;
  95. c4_start_code : int array;
  96. c4_id_delta : int array;
  97. c4_id_range_offset : int array;
  98. c4_glyph_index_array : int array;
  99. }
  100. type cmap_format_6 = {
  101. c6_format : int;
  102. c6_length : int;
  103. c6_language : int;
  104. c6_first_code : int;
  105. c6_entry_count : int;
  106. c6_glyph_index_array : int array;
  107. }
  108. type cmap_format_12_group = {
  109. c12g_start_char_code : int32;
  110. c12g_end_char_code : int32;
  111. c12g_start_glyph_code : int32;
  112. }
  113. type cmap_format_12 = {
  114. c12_format : int32;
  115. c12_length : int32;
  116. c12_language : int32;
  117. c12_num_groups : int32;
  118. c12_groups : cmap_format_12_group list;
  119. }
  120. type cmap_subtable_def =
  121. | Cmap0 of cmap_format_0
  122. | Cmap4 of cmap_format_4
  123. | Cmap6 of cmap_format_6
  124. | Cmap12 of cmap_format_12
  125. | CmapUnk of string
  126. type cmap_subtable = {
  127. cs_header : cmap_subtable_header;
  128. cs_def : cmap_subtable_def;
  129. }
  130. type cmap = {
  131. cmap_version : int;
  132. cmap_num_subtables : int;
  133. cmap_subtables : cmap_subtable list;
  134. }
  135. (* KERN *)
  136. type kern_subtable_header = {
  137. ksh_length : int32;
  138. ksh_coverage : int;
  139. ksh_tuple_index : int;
  140. }
  141. type kern_pair = {
  142. kern_left : int;
  143. kern_right : int;
  144. kern_value : int;
  145. }
  146. type kern_format_0 = {
  147. k0_num_pairs : int;
  148. k0_search_range : int;
  149. k0_entry_selector : int;
  150. k0_range_shift : int;
  151. k0_pairs : kern_pair list;
  152. }
  153. type kern_format_2 = {
  154. k2_row_width : int;
  155. k2_left_offset_table : int;
  156. k2_right_offset_table : int;
  157. k2_array : int;
  158. k2_first_glyph : int;
  159. k2_num_glyphs : int;
  160. k2_offsets : int list;
  161. }
  162. type kern_subtable_def =
  163. | Kern0 of kern_format_0
  164. | Kern2 of kern_format_2
  165. type kern_subtable = {
  166. ks_header : kern_subtable_header;
  167. ks_def : kern_subtable_def;
  168. }
  169. type kern = {
  170. kern_version : int32;
  171. kern_num_tables : int32;
  172. kern_subtables : kern_subtable list;
  173. }
  174. (* NAME *)
  175. type name_record = {
  176. nr_platform_id : int;
  177. nr_platform_specific_id : int;
  178. nr_language_id : int;
  179. nr_name_id : int;
  180. nr_length : int;
  181. nr_offset : int;
  182. mutable nr_value : string;
  183. }
  184. type name = {
  185. name_format : int;
  186. name_num_records : int;
  187. name_offset : int;
  188. name_records : name_record array;
  189. }
  190. (* HEAD *)
  191. type head = {
  192. hd_version : int32;
  193. hd_font_revision : int32;
  194. hd_checksum_adjustment : int32;
  195. hd_magic_number : int32;
  196. hd_flags : int;
  197. hd_units_per_em : int;
  198. hd_created : float;
  199. hd_modified : float;
  200. hd_xmin : int;
  201. hd_ymin : int;
  202. hd_xmax : int;
  203. hd_ymax : int;
  204. hd_mac_style : int;
  205. hd_lowest_rec_ppem : int;
  206. hd_font_direction_hint : int;
  207. hd_index_to_loc_format : int;
  208. hd_glyph_data_format : int;
  209. }
  210. (* HHEA *)
  211. type hhea = {
  212. hhea_version : int32;
  213. hhea_ascent : int;
  214. hhea_descent : int;
  215. hhea_line_gap : int;
  216. hhea_advance_width_max : int;
  217. hhea_min_left_side_bearing : int;
  218. hhea_min_right_side_bearing : int;
  219. hhea_x_max_extent : int;
  220. hhea_caret_slope_rise : int;
  221. hhea_caret_slope_run : int;
  222. hhea_caret_offset : int;
  223. hhea_reserved : string;
  224. hhea_metric_data_format : int;
  225. hhea_number_of_hmetrics :int;
  226. }
  227. (* LOCA *)
  228. type loca = int32 array
  229. (* MAXP *)
  230. type maxp = {
  231. maxp_version_number : int32;
  232. maxp_num_glyphs : int;
  233. maxp_max_points : int;
  234. maxp_max_contours : int;
  235. maxp_max_component_points : int;
  236. maxp_max_component_contours : int;
  237. maxp_max_zones : int;
  238. maxp_max_twilight_points : int;
  239. maxp_max_storage : int;
  240. maxp_max_function_defs : int;
  241. maxp_max_instruction_defs :int;
  242. maxp_max_stack_elements : int;
  243. maxp_max_size_of_instructions :int;
  244. maxp_max_component_elements :int;
  245. maxp_max_component_depth :int;
  246. }
  247. (* OS2 *)
  248. type os2 = {
  249. os2_version : int;
  250. os2_x_avg_char_width : int;
  251. os2_us_weight_class : int;
  252. os2_us_width_class : int;
  253. os2_fs_type : int;
  254. os2_y_subscript_x_size : int;
  255. os2_y_subscript_y_size : int;
  256. os2_y_subscript_x_offset : int;
  257. os2_y_subscript_y_offset : int;
  258. os2_y_superscript_x_size : int;
  259. os2_y_superscript_y_size : int;
  260. os2_y_superscript_x_offset : int;
  261. os2_y_superscript_y_offset : int;
  262. os2_y_strikeout_size : int;
  263. os2_y_strikeout_position : int;
  264. os2_s_family_class : int;
  265. os2_b_family_type : int;
  266. os2_b_serif_style : int;
  267. os2_b_weight : int;
  268. os2_b_proportion : int;
  269. os2_b_contrast : int;
  270. os2_b_stroke_variation : int;
  271. os2_b_arm_style : int;
  272. os2_b_letterform : int;
  273. os2_b_midline : int;
  274. os2_b_x_height : int;
  275. os2_ul_unicode_range_1 : int32;
  276. os2_ul_unicode_range_2 : int32;
  277. os2_ul_unicode_range_3 : int32;
  278. os2_ul_unicode_range_4 : int32;
  279. os2_ach_vendor_id : int32;
  280. os2_fs_selection : int;
  281. os2_us_first_char_index : int;
  282. os2_us_last_char_index : int;
  283. os2_s_typo_ascender : int;
  284. os2_s_typo_descender : int;
  285. os2_s_typo_line_gap : int;
  286. os2_us_win_ascent : int;
  287. os2_us_win_descent : int;
  288. }
  289. type ttf = {
  290. ttf_header : header;
  291. ttf_font_name : string;
  292. ttf_directory: (string,entry) Hashtbl.t;
  293. ttf_glyfs : glyf array;
  294. ttf_hmtx : hmtx array;
  295. ttf_cmap : cmap;
  296. ttf_head : head;
  297. ttf_loca : loca;
  298. ttf_hhea : hhea;
  299. ttf_maxp : maxp;
  300. ttf_name : name;
  301. ttf_os2 : os2;
  302. ttf_kern : kern option;
  303. }
  304. type ttf_font_weight =
  305. | TFWRegular
  306. | TFWBold
  307. type ttf_font_posture =
  308. | TFPNormal
  309. | TFPItalic
  310. type ttf_config = {
  311. mutable ttfc_range_str : string;
  312. mutable ttfc_font_name : string option;
  313. mutable ttfc_font_weight : ttf_font_weight;
  314. mutable ttfc_font_posture : ttf_font_posture;
  315. }