PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/MeeGoAdmin/mozilla-central/
C++ | 196 lines | 123 code | 37 blank | 36 comment | 29 complexity | 8539e1c5e1cf29fa07b287412f492aa9 MD5 | raw file
Possible License(s): AGPL-1.0, MIT, BSD-3-Clause, Apache-2.0, LGPL-2.1, 0BSD, LGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-2.0, JSON
  1. /*
  2. * Copyright (C) 2009,2010 Red Hat, Inc.
  3. * Copyright (C) 2010 Google, Inc.
  4. *
  5. * This is part of HarfBuzz, a text shaping library.
  6. *
  7. * Permission is hereby granted, without written agreement and without
  8. * license or royalty fees, to use, copy, modify, and distribute this
  9. * software and its documentation for any purpose, provided that the
  10. * above copyright notice and the following two paragraphs appear in
  11. * all copies of this software.
  12. *
  13. * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  14. * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  15. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  16. * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  17. * DAMAGE.
  18. *
  19. * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  20. * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  21. * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
  22. * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  23. * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  24. *
  25. * Red Hat Author(s): Behdad Esfahbod
  26. * Google Author(s): Behdad Esfahbod
  27. */
  28. #include "hb-ot-map-private.hh"
  29. #include "hb-ot-shape-private.hh"
  30. HB_BEGIN_DECLS
  31. void
  32. hb_ot_map_t::add_lookups (hb_face_t *face,
  33. unsigned int table_index,
  34. unsigned int feature_index,
  35. unsigned short priority,
  36. hb_mask_t mask)
  37. {
  38. unsigned int i = MAX_LOOKUPS - lookup_count[table_index];
  39. lookup_map_t *lookups = lookup_maps[table_index] + lookup_count[table_index];
  40. unsigned int *lookup_indices = (unsigned int *) lookups;
  41. hb_ot_layout_feature_get_lookup_indexes (face,
  42. table_tags[table_index],
  43. feature_index,
  44. 0, &i,
  45. lookup_indices);
  46. lookup_count[table_index] += i;
  47. while (i--) {
  48. lookups[i].mask = mask;
  49. lookups[i].index = lookup_indices[i];
  50. lookups[i].priority = priority;
  51. }
  52. }
  53. void
  54. hb_ot_map_t::compile (hb_face_t *face,
  55. const hb_segment_properties_t *props)
  56. {
  57. global_mask = 1;
  58. lookup_count[0] = lookup_count[1] = 0;
  59. if (!feature_count)
  60. return;
  61. /* Fetch script/language indices for GSUB/GPOS. We need these later to skip
  62. * features not available in either table and not waste precious bits for them. */
  63. const hb_tag_t *script_tags;
  64. hb_tag_t language_tag;
  65. script_tags = hb_ot_tags_from_script (props->script);
  66. language_tag = hb_ot_tag_from_language (props->language);
  67. unsigned int script_index[2], language_index[2];
  68. for (unsigned int table_index = 0; table_index < 2; table_index++) {
  69. hb_tag_t table_tag = table_tags[table_index];
  70. hb_ot_layout_table_choose_script (face, table_tag, script_tags, &script_index[table_index]);
  71. hb_ot_layout_script_find_language (face, table_tag, script_index[table_index], language_tag, &language_index[table_index]);
  72. }
  73. /* Sort features and merge duplicates */
  74. qsort (feature_infos, feature_count, sizeof (feature_infos[0]), (hb_compare_func_t) feature_info_t::cmp);
  75. unsigned int j = 0;
  76. for (unsigned int i = 1; i < feature_count; i++)
  77. if (feature_infos[i].tag != feature_infos[j].tag)
  78. feature_infos[++j] = feature_infos[i];
  79. else {
  80. if (feature_infos[i].global)
  81. feature_infos[j] = feature_infos[i];
  82. else {
  83. feature_infos[j].global = false;
  84. feature_infos[j].max_value = MAX (feature_infos[j].max_value, feature_infos[i].max_value);
  85. /* Inherit default_value from j */
  86. }
  87. }
  88. feature_count = j + 1;
  89. /* Allocate bits now */
  90. unsigned int next_bit = 1;
  91. j = 0;
  92. for (unsigned int i = 0; i < feature_count; i++) {
  93. const feature_info_t *info = &feature_infos[i];
  94. unsigned int bits_needed;
  95. if (info->global && info->max_value == 1)
  96. /* Uses the global bit */
  97. bits_needed = 0;
  98. else
  99. bits_needed = _hb_bit_storage (info->max_value);
  100. if (!info->max_value || next_bit + bits_needed > 8 * sizeof (hb_mask_t))
  101. continue; /* Feature disabled, or not enough bits. */
  102. bool found = false;
  103. unsigned int feature_index[2];
  104. for (unsigned int table_index = 0; table_index < 2; table_index++)
  105. found |= hb_ot_layout_language_find_feature (face,
  106. table_tags[table_index],
  107. script_index[table_index],
  108. language_index[table_index],
  109. info->tag,
  110. &feature_index[table_index]);
  111. if (!found)
  112. continue;
  113. feature_map_t *map = &feature_maps[j++];
  114. map->tag = info->tag;
  115. map->index[0] = feature_index[0];
  116. map->index[1] = feature_index[1];
  117. if (info->global && info->max_value == 1) {
  118. /* Uses the global bit */
  119. map->shift = 0;
  120. map->mask = 1;
  121. } else {
  122. map->shift = next_bit;
  123. map->mask = (1 << (next_bit + bits_needed)) - (1 << next_bit);
  124. next_bit += bits_needed;
  125. if (info->global)
  126. global_mask |= (info->default_value << map->shift) & map->mask;
  127. }
  128. map->_1_mask = (1 << map->shift) & map->mask;
  129. map->priority = info->priority;
  130. }
  131. feature_count = j;
  132. for (unsigned int table_index = 0; table_index < 2; table_index++) {
  133. hb_tag_t table_tag = table_tags[table_index];
  134. /* Collect lookup indices for features */
  135. unsigned int required_feature_index;
  136. if (hb_ot_layout_language_get_required_feature_index (face,
  137. table_tag,
  138. script_index[table_index],
  139. language_index[table_index],
  140. &required_feature_index))
  141. add_lookups (face, table_index, required_feature_index, DEFAULT_PRIORITY, 1);
  142. for (unsigned i = 0; i < feature_count; i++)
  143. add_lookups (face, table_index, feature_maps[i].index[table_index], feature_maps[i].priority, feature_maps[i].mask);
  144. /* Sort lookups and merge duplicates */
  145. qsort (lookup_maps[table_index], lookup_count[table_index], sizeof (lookup_maps[table_index][0]), (hb_compare_func_t) lookup_map_t::cmp);
  146. if (lookup_count[table_index])
  147. {
  148. unsigned int j = 0;
  149. for (unsigned int i = 1; i < lookup_count[table_index]; i++)
  150. if (lookup_maps[table_index][i].index != lookup_maps[table_index][j].index)
  151. lookup_maps[table_index][++j] = lookup_maps[table_index][i];
  152. else
  153. lookup_maps[table_index][j].mask |= lookup_maps[table_index][i].mask;
  154. j++;
  155. lookup_count[table_index] = j;
  156. }
  157. }
  158. }
  159. HB_END_DECLS