PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/gfx/harfbuzz/src/hb-ot-layout.cc

http://github.com/zpao/v8monkey
C++ | 504 lines | 355 code | 89 blank | 60 comment | 47 complexity | 71feb06fc78ad6428e7c25cedc15eec1 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, AGPL-1.0, LGPL-2.1, BSD-3-Clause, GPL-2.0, JSON, Apache-2.0, 0BSD
  1. /*
  2. * Copyright © 1998-2004 David Turner and Werner Lemberg
  3. * Copyright © 2006 Behdad Esfahbod
  4. * Copyright © 2007,2008,2009 Red Hat, Inc.
  5. *
  6. * This is part of HarfBuzz, a text shaping library.
  7. *
  8. * Permission is hereby granted, without written agreement and without
  9. * license or royalty fees, to use, copy, modify, and distribute this
  10. * software and its documentation for any purpose, provided that the
  11. * above copyright notice and the following two paragraphs appear in
  12. * all copies of this software.
  13. *
  14. * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  15. * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  16. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  17. * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  18. * DAMAGE.
  19. *
  20. * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  21. * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  22. * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
  23. * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  24. * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  25. *
  26. * Red Hat Author(s): Behdad Esfahbod
  27. */
  28. #include "hb-ot-layout-private.hh"
  29. #include "hb-ot-layout-gdef-table.hh"
  30. #include "hb-ot-layout-gsub-table.hh"
  31. #include "hb-ot-layout-gpos-table.hh"
  32. #include "hb-ot-maxp-table.hh"
  33. #include <stdlib.h>
  34. #include <string.h>
  35. hb_ot_layout_t *
  36. _hb_ot_layout_create (hb_face_t *face)
  37. {
  38. /* TODO Remove this object altogether */
  39. hb_ot_layout_t *layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
  40. layout->gdef_blob = Sanitizer<GDEF>::sanitize (hb_face_reference_table (face, HB_OT_TAG_GDEF));
  41. layout->gdef = Sanitizer<GDEF>::lock_instance (layout->gdef_blob);
  42. layout->gsub_blob = Sanitizer<GSUB>::sanitize (hb_face_reference_table (face, HB_OT_TAG_GSUB));
  43. layout->gsub = Sanitizer<GSUB>::lock_instance (layout->gsub_blob);
  44. layout->gpos_blob = Sanitizer<GPOS>::sanitize (hb_face_reference_table (face, HB_OT_TAG_GPOS));
  45. layout->gpos = Sanitizer<GPOS>::lock_instance (layout->gpos_blob);
  46. return layout;
  47. }
  48. void
  49. _hb_ot_layout_destroy (hb_ot_layout_t *layout)
  50. {
  51. hb_blob_destroy (layout->gdef_blob);
  52. hb_blob_destroy (layout->gsub_blob);
  53. hb_blob_destroy (layout->gpos_blob);
  54. free (layout);
  55. }
  56. static inline const GDEF&
  57. _get_gdef (hb_face_t *face)
  58. {
  59. return likely (face->ot_layout && face->ot_layout->gdef) ? *face->ot_layout->gdef : Null(GDEF);
  60. }
  61. static inline const GSUB&
  62. _get_gsub (hb_face_t *face)
  63. {
  64. return likely (face->ot_layout && face->ot_layout->gsub) ? *face->ot_layout->gsub : Null(GSUB);
  65. }
  66. static inline const GPOS&
  67. _get_gpos (hb_face_t *face)
  68. {
  69. return likely (face->ot_layout && face->ot_layout->gpos) ? *face->ot_layout->gpos : Null(GPOS);
  70. }
  71. /*
  72. * GDEF
  73. */
  74. hb_bool_t
  75. hb_ot_layout_has_glyph_classes (hb_face_t *face)
  76. {
  77. return _get_gdef (face).has_glyph_classes ();
  78. }
  79. unsigned int
  80. _hb_ot_layout_get_glyph_property (hb_face_t *face,
  81. hb_glyph_info_t *info)
  82. {
  83. if (!info->props_cache())
  84. {
  85. const GDEF &gdef = _get_gdef (face);
  86. info->props_cache() = gdef.get_glyph_props (info->codepoint);
  87. }
  88. return info->props_cache();
  89. }
  90. static hb_bool_t
  91. _hb_ot_layout_match_properties (hb_face_t *face,
  92. hb_codepoint_t codepoint,
  93. unsigned int glyph_props,
  94. unsigned int lookup_props)
  95. {
  96. /* Not covered, if, for example, glyph class is ligature and
  97. * lookup_props includes LookupFlags::IgnoreLigatures
  98. */
  99. if (glyph_props & lookup_props & LookupFlag::IgnoreFlags)
  100. return false;
  101. if (glyph_props & HB_OT_LAYOUT_GLYPH_CLASS_MARK)
  102. {
  103. /* If using mark filtering sets, the high short of
  104. * lookup_props has the set index.
  105. */
  106. if (lookup_props & LookupFlag::UseMarkFilteringSet)
  107. return _get_gdef (face).mark_set_covers (lookup_props >> 16, codepoint);
  108. /* The second byte of lookup_props has the meaning
  109. * "ignore marks of attachment type different than
  110. * the attachment type specified."
  111. */
  112. if (lookup_props & LookupFlag::MarkAttachmentType && glyph_props & LookupFlag::MarkAttachmentType)
  113. return (lookup_props & LookupFlag::MarkAttachmentType) == (glyph_props & LookupFlag::MarkAttachmentType);
  114. }
  115. return true;
  116. }
  117. hb_bool_t
  118. _hb_ot_layout_check_glyph_property (hb_face_t *face,
  119. hb_glyph_info_t *ginfo,
  120. unsigned int lookup_props,
  121. unsigned int *property_out)
  122. {
  123. unsigned int property;
  124. property = _hb_ot_layout_get_glyph_property (face, ginfo);
  125. (void) (property_out && (*property_out = property));
  126. return _hb_ot_layout_match_properties (face, ginfo->codepoint, property, lookup_props);
  127. }
  128. hb_bool_t
  129. _hb_ot_layout_skip_mark (hb_face_t *face,
  130. hb_glyph_info_t *ginfo,
  131. unsigned int lookup_props,
  132. unsigned int *property_out)
  133. {
  134. unsigned int property;
  135. property = _hb_ot_layout_get_glyph_property (face, ginfo);
  136. (void) (property_out && (*property_out = property));
  137. /* If it's a mark, skip it we don't accept it. */
  138. if (unlikely (property & HB_OT_LAYOUT_GLYPH_CLASS_MARK))
  139. return !_hb_ot_layout_match_properties (face, ginfo->codepoint, property, lookup_props);
  140. /* If not a mark, don't skip. */
  141. return false;
  142. }
  143. unsigned int
  144. hb_ot_layout_get_attach_points (hb_face_t *face,
  145. hb_codepoint_t glyph,
  146. unsigned int start_offset,
  147. unsigned int *point_count /* IN/OUT */,
  148. unsigned int *point_array /* OUT */)
  149. {
  150. return _get_gdef (face).get_attach_points (glyph, start_offset, point_count, point_array);
  151. }
  152. unsigned int
  153. hb_ot_layout_get_ligature_carets (hb_font_t *font,
  154. hb_direction_t direction,
  155. hb_codepoint_t glyph,
  156. unsigned int start_offset,
  157. unsigned int *caret_count /* IN/OUT */,
  158. int *caret_array /* OUT */)
  159. {
  160. return _get_gdef (font->face).get_lig_carets (font, direction, glyph, start_offset, caret_count, caret_array);
  161. }
  162. /*
  163. * GSUB/GPOS
  164. */
  165. static const GSUBGPOS&
  166. get_gsubgpos_table (hb_face_t *face,
  167. hb_tag_t table_tag)
  168. {
  169. switch (table_tag) {
  170. case HB_OT_TAG_GSUB: return _get_gsub (face);
  171. case HB_OT_TAG_GPOS: return _get_gpos (face);
  172. default: return Null(GSUBGPOS);
  173. }
  174. }
  175. unsigned int
  176. hb_ot_layout_table_get_script_tags (hb_face_t *face,
  177. hb_tag_t table_tag,
  178. unsigned int start_offset,
  179. unsigned int *script_count /* IN/OUT */,
  180. hb_tag_t *script_tags /* OUT */)
  181. {
  182. const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
  183. return g.get_script_tags (start_offset, script_count, script_tags);
  184. }
  185. hb_bool_t
  186. hb_ot_layout_table_find_script (hb_face_t *face,
  187. hb_tag_t table_tag,
  188. hb_tag_t script_tag,
  189. unsigned int *script_index)
  190. {
  191. ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
  192. const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
  193. if (g.find_script_index (script_tag, script_index))
  194. return TRUE;
  195. /* try finding 'DFLT' */
  196. if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index))
  197. return FALSE;
  198. /* try with 'dflt'; MS site has had typos and many fonts use it now :(.
  199. * including many versions of DejaVu Sans Mono! */
  200. if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index))
  201. return FALSE;
  202. if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
  203. return FALSE;
  204. }
  205. hb_bool_t
  206. hb_ot_layout_table_choose_script (hb_face_t *face,
  207. hb_tag_t table_tag,
  208. const hb_tag_t *script_tags,
  209. unsigned int *script_index,
  210. hb_tag_t *chosen_script)
  211. {
  212. ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
  213. const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
  214. while (*script_tags)
  215. {
  216. if (g.find_script_index (*script_tags, script_index)) {
  217. if (chosen_script)
  218. *chosen_script = *script_tags;
  219. return TRUE;
  220. }
  221. script_tags++;
  222. }
  223. /* try finding 'DFLT' */
  224. if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index)) {
  225. if (chosen_script)
  226. *chosen_script = HB_OT_TAG_DEFAULT_SCRIPT;
  227. return FALSE;
  228. }
  229. /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
  230. if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index)) {
  231. if (chosen_script)
  232. *chosen_script = HB_OT_TAG_DEFAULT_LANGUAGE;
  233. return FALSE;
  234. }
  235. /* try with 'latn'; some old fonts put their features there even though
  236. they're really trying to support Thai, for example :( */
  237. #define HB_OT_TAG_LATIN_SCRIPT HB_TAG ('l', 'a', 't', 'n')
  238. if (g.find_script_index (HB_OT_TAG_LATIN_SCRIPT, script_index)) {
  239. if (chosen_script)
  240. *chosen_script = HB_OT_TAG_LATIN_SCRIPT;
  241. return FALSE;
  242. }
  243. if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
  244. if (chosen_script)
  245. *chosen_script = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
  246. return FALSE;
  247. }
  248. unsigned int
  249. hb_ot_layout_table_get_feature_tags (hb_face_t *face,
  250. hb_tag_t table_tag,
  251. unsigned int start_offset,
  252. unsigned int *feature_count /* IN/OUT */,
  253. hb_tag_t *feature_tags /* OUT */)
  254. {
  255. const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
  256. return g.get_feature_tags (start_offset, feature_count, feature_tags);
  257. }
  258. unsigned int
  259. hb_ot_layout_script_get_language_tags (hb_face_t *face,
  260. hb_tag_t table_tag,
  261. unsigned int script_index,
  262. unsigned int start_offset,
  263. unsigned int *language_count /* IN/OUT */,
  264. hb_tag_t *language_tags /* OUT */)
  265. {
  266. const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
  267. return s.get_lang_sys_tags (start_offset, language_count, language_tags);
  268. }
  269. hb_bool_t
  270. hb_ot_layout_script_find_language (hb_face_t *face,
  271. hb_tag_t table_tag,
  272. unsigned int script_index,
  273. hb_tag_t language_tag,
  274. unsigned int *language_index)
  275. {
  276. ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
  277. const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
  278. if (s.find_lang_sys_index (language_tag, language_index))
  279. return TRUE;
  280. /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
  281. if (s.find_lang_sys_index (HB_OT_TAG_DEFAULT_LANGUAGE, language_index))
  282. return FALSE;
  283. if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
  284. return FALSE;
  285. }
  286. hb_bool_t
  287. hb_ot_layout_language_get_required_feature_index (hb_face_t *face,
  288. hb_tag_t table_tag,
  289. unsigned int script_index,
  290. unsigned int language_index,
  291. unsigned int *feature_index)
  292. {
  293. const LangSys &l = get_gsubgpos_table (face, table_tag).get_script (script_index).get_lang_sys (language_index);
  294. if (feature_index) *feature_index = l.get_required_feature_index ();
  295. return l.has_required_feature ();
  296. }
  297. unsigned int
  298. hb_ot_layout_language_get_feature_indexes (hb_face_t *face,
  299. hb_tag_t table_tag,
  300. unsigned int script_index,
  301. unsigned int language_index,
  302. unsigned int start_offset,
  303. unsigned int *feature_count /* IN/OUT */,
  304. unsigned int *feature_indexes /* OUT */)
  305. {
  306. const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
  307. const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
  308. return l.get_feature_indexes (start_offset, feature_count, feature_indexes);
  309. }
  310. unsigned int
  311. hb_ot_layout_language_get_feature_tags (hb_face_t *face,
  312. hb_tag_t table_tag,
  313. unsigned int script_index,
  314. unsigned int language_index,
  315. unsigned int start_offset,
  316. unsigned int *feature_count /* IN/OUT */,
  317. hb_tag_t *feature_tags /* OUT */)
  318. {
  319. const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
  320. const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
  321. ASSERT_STATIC (sizeof (unsigned int) == sizeof (hb_tag_t));
  322. unsigned int ret = l.get_feature_indexes (start_offset, feature_count, (unsigned int *) feature_tags);
  323. if (feature_tags) {
  324. unsigned int count = *feature_count;
  325. for (unsigned int i = 0; i < count; i++)
  326. feature_tags[i] = g.get_feature_tag ((unsigned int) feature_tags[i]);
  327. }
  328. return ret;
  329. }
  330. hb_bool_t
  331. hb_ot_layout_language_find_feature (hb_face_t *face,
  332. hb_tag_t table_tag,
  333. unsigned int script_index,
  334. unsigned int language_index,
  335. hb_tag_t feature_tag,
  336. unsigned int *feature_index)
  337. {
  338. ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
  339. const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
  340. const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
  341. unsigned int num_features = l.get_feature_count ();
  342. for (unsigned int i = 0; i < num_features; i++) {
  343. unsigned int f_index = l.get_feature_index (i);
  344. if (feature_tag == g.get_feature_tag (f_index)) {
  345. if (feature_index) *feature_index = f_index;
  346. return TRUE;
  347. }
  348. }
  349. if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
  350. return FALSE;
  351. }
  352. unsigned int
  353. hb_ot_layout_feature_get_lookup_indexes (hb_face_t *face,
  354. hb_tag_t table_tag,
  355. unsigned int feature_index,
  356. unsigned int start_offset,
  357. unsigned int *lookup_count /* IN/OUT */,
  358. unsigned int *lookup_indexes /* OUT */)
  359. {
  360. const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
  361. const Feature &f = g.get_feature (feature_index);
  362. return f.get_lookup_indexes (start_offset, lookup_count, lookup_indexes);
  363. }
  364. /*
  365. * GSUB
  366. */
  367. hb_bool_t
  368. hb_ot_layout_has_substitution (hb_face_t *face)
  369. {
  370. return &_get_gsub (face) != &Null(GSUB);
  371. }
  372. void
  373. hb_ot_layout_substitute_start (hb_buffer_t *buffer)
  374. {
  375. GSUB::substitute_start (buffer);
  376. }
  377. hb_bool_t
  378. hb_ot_layout_substitute_lookup (hb_face_t *face,
  379. hb_buffer_t *buffer,
  380. unsigned int lookup_index,
  381. hb_mask_t mask)
  382. {
  383. return _get_gsub (face).substitute_lookup (face, buffer, lookup_index, mask);
  384. }
  385. void
  386. hb_ot_layout_substitute_finish (hb_buffer_t *buffer HB_UNUSED)
  387. {
  388. GSUB::substitute_finish (buffer);
  389. }
  390. /*
  391. * GPOS
  392. */
  393. hb_bool_t
  394. hb_ot_layout_has_positioning (hb_face_t *face)
  395. {
  396. return &_get_gpos (face) != &Null(GPOS);
  397. }
  398. void
  399. hb_ot_layout_position_start (hb_buffer_t *buffer)
  400. {
  401. GPOS::position_start (buffer);
  402. }
  403. hb_bool_t
  404. hb_ot_layout_position_lookup (hb_font_t *font,
  405. hb_buffer_t *buffer,
  406. unsigned int lookup_index,
  407. hb_mask_t mask)
  408. {
  409. return _get_gpos (font->face).position_lookup (font, buffer, lookup_index, mask);
  410. }
  411. void
  412. hb_ot_layout_position_finish (hb_buffer_t *buffer)
  413. {
  414. GPOS::position_finish (buffer);
  415. }