QtBase  v6.3.1
hb-ot-shape-complex-khmer.cc
Go to the documentation of this file.
1 /*
2  * Copyright © 2011,2012 Google, Inc.
3  *
4  * This is part of HarfBuzz, a text shaping library.
5  *
6  * Permission is hereby granted, without written agreement and without
7  * license or royalty fees, to use, copy, modify, and distribute this
8  * software and its documentation for any purpose, provided that the
9  * above copyright notice and the following two paragraphs appear in
10  * all copies of this software.
11  *
12  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16  * DAMAGE.
17  *
18  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23  *
24  * Google Author(s): Behdad Esfahbod
25  */
26 
27 #include "hb.hh"
28 
29 #ifndef HB_NO_OT_SHAPE
30 
33 #include "hb-ot-layout.hh"
34 
35 
36 /*
37  * Khmer shaper.
38  */
39 
40 static const hb_ot_map_feature_t
41 khmer_features[] =
42 {
43  /*
44  * Basic features.
45  * These features are applied all at once, before reordering, constrained
46  * to the syllable.
47  */
48  {HB_TAG('p','r','e','f'), F_MANUAL_JOINERS | F_PER_SYLLABLE},
49  {HB_TAG('b','l','w','f'), F_MANUAL_JOINERS | F_PER_SYLLABLE},
50  {HB_TAG('a','b','v','f'), F_MANUAL_JOINERS | F_PER_SYLLABLE},
51  {HB_TAG('p','s','t','f'), F_MANUAL_JOINERS | F_PER_SYLLABLE},
52  {HB_TAG('c','f','a','r'), F_MANUAL_JOINERS | F_PER_SYLLABLE},
53  /*
54  * Other features.
55  * These features are applied all at once after clearing syllables.
56  */
57  {HB_TAG('p','r','e','s'), F_GLOBAL_MANUAL_JOINERS},
58  {HB_TAG('a','b','v','s'), F_GLOBAL_MANUAL_JOINERS},
59  {HB_TAG('b','l','w','s'), F_GLOBAL_MANUAL_JOINERS},
60  {HB_TAG('p','s','t','s'), F_GLOBAL_MANUAL_JOINERS},
61 };
62 
63 /*
64  * Must be in the same order as the khmer_features array.
65  */
66 enum {
72 
77 
79  KHMER_BASIC_FEATURES = _KHMER_PRES, /* Don't forget to update this! */
80 };
81 
82 static void
83 setup_syllables_khmer (const hb_ot_shape_plan_t *plan,
84  hb_font_t *font,
86 static void
87 reorder_khmer (const hb_ot_shape_plan_t *plan,
88  hb_font_t *font,
90 
91 static void
92 collect_features_khmer (hb_ot_shape_planner_t *plan)
93 {
94  hb_ot_map_builder_t *map = &plan->map;
95 
96  /* Do this before any lookups have been applied. */
97  map->add_gsub_pause (setup_syllables_khmer);
98  map->add_gsub_pause (reorder_khmer);
99 
100  /* Testing suggests that Uniscribe does NOT pause between basic
101  * features. Test with KhmerUI.ttf and the following three
102  * sequences:
103  *
104  * U+1789,U+17BC
105  * U+1789,U+17D2,U+1789
106  * U+1789,U+17D2,U+1789,U+17BC
107  *
108  * https://github.com/harfbuzz/harfbuzz/issues/974
109  */
110  map->enable_feature (HB_TAG('l','o','c','l'), F_PER_SYLLABLE);
111  map->enable_feature (HB_TAG('c','c','m','p'), F_PER_SYLLABLE);
112 
113  unsigned int i = 0;
114  for (; i < KHMER_BASIC_FEATURES; i++)
115  map->add_feature (khmer_features[i]);
116 
117  /* https://github.com/harfbuzz/harfbuzz/issues/3531 */
118  map->add_gsub_pause (nullptr);
119 
120  for (; i < KHMER_NUM_FEATURES; i++)
121  map->add_feature (khmer_features[i]);
122 }
123 
124 static void
125 override_features_khmer (hb_ot_shape_planner_t *plan)
126 {
127  hb_ot_map_builder_t *map = &plan->map;
128 
129  /* Khmer spec has 'clig' as part of required shaping features:
130  * "Apply feature 'clig' to form ligatures that are desired for
131  * typographical correctness.", hence in overrides... */
132  map->enable_feature (HB_TAG('c','l','i','g'));
133 
134  /* Uniscribe does not apply 'kern' in Khmer. */
135  if (hb_options ().uniscribe_bug_compatible)
136  {
137  map->disable_feature (HB_TAG('k','e','r','n'));
138  }
139 
140  map->disable_feature (HB_TAG('l','i','g','a'));
141 }
142 
143 
145 {
147 };
148 
149 static void *
150 data_create_khmer (const hb_ot_shape_plan_t *plan)
151 {
152  khmer_shape_plan_t *khmer_plan = (khmer_shape_plan_t *) hb_calloc (1, sizeof (khmer_shape_plan_t));
153  if (unlikely (!khmer_plan))
154  return nullptr;
155 
156  for (unsigned int i = 0; i < ARRAY_LENGTH (khmer_plan->mask_array); i++)
157  khmer_plan->mask_array[i] = (khmer_features[i].flags & F_GLOBAL) ?
158  0 : plan->map.get_1_mask (khmer_features[i].tag);
159 
160  return khmer_plan;
161 }
162 
163 static void
164 data_destroy_khmer (void *data)
165 {
166  hb_free (data);
167 }
168 
169 static void
170 setup_masks_khmer (const hb_ot_shape_plan_t *plan HB_UNUSED,
173 {
175 
176  /* We cannot setup masks here. We save information about characters
177  * and setup masks later on in a pause-callback. */
178 
179  unsigned int count = buffer->len;
180  hb_glyph_info_t *info = buffer->info;
181  for (unsigned int i = 0; i < count; i++)
182  set_khmer_properties (info[i]);
183 }
184 
185 static void
186 setup_syllables_khmer (const hb_ot_shape_plan_t *plan HB_UNUSED,
189 {
190  find_syllables_khmer (buffer);
192  buffer->unsafe_to_break (start, end);
193 }
194 
195 
196 /* Rules from:
197  * https://docs.microsoft.com/en-us/typography/script-development/devanagari */
198 
199 static void
200 reorder_consonant_syllable (const hb_ot_shape_plan_t *plan,
203  unsigned int start, unsigned int end)
204 {
205  const khmer_shape_plan_t *khmer_plan = (const khmer_shape_plan_t *) plan->data;
206  hb_glyph_info_t *info = buffer->info;
207 
208  /* Setup masks. */
209  {
210  /* Post-base */
211  hb_mask_t mask = khmer_plan->mask_array[KHMER_BLWF] |
212  khmer_plan->mask_array[KHMER_ABVF] |
213  khmer_plan->mask_array[KHMER_PSTF];
214  for (unsigned int i = start + 1; i < end; i++)
215  info[i].mask |= mask;
216  }
217 
218  unsigned int num_coengs = 0;
219  for (unsigned int i = start + 1; i < end; i++)
220  {
221  /* """
222  * When a COENG + (Cons | IndV) combination are found (and subscript count
223  * is less than two) the character combination is handled according to the
224  * subscript type of the character following the COENG.
225  *
226  * ...
227  *
228  * Subscript Type 2 - The COENG + RO characters are reordered to immediately
229  * before the base glyph. Then the COENG + RO characters are assigned to have
230  * the 'pref' OpenType feature applied to them.
231  * """
232  */
233  if (info[i].khmer_category() == OT_Coeng && num_coengs <= 2 && i + 1 < end)
234  {
235  num_coengs++;
236 
237  if (info[i + 1].khmer_category() == OT_Ra)
238  {
239  for (unsigned int j = 0; j < 2; j++)
240  info[i + j].mask |= khmer_plan->mask_array[KHMER_PREF];
241 
242  /* Move the Coeng,Ro sequence to the start. */
243  buffer->merge_clusters (start, i + 2);
245  hb_glyph_info_t t1 = info[i + 1];
246  memmove (&info[start + 2], &info[start], (i - start) * sizeof (info[0]));
247  info[start] = t0;
248  info[start + 1] = t1;
249 
250  /* Mark the subsequent stuff with 'cfar'. Used in Khmer.
251  * Read the feature spec.
252  * This allows distinguishing the following cases with MS Khmer fonts:
253  * U+1784,U+17D2,U+179A,U+17D2,U+1782
254  * U+1784,U+17D2,U+1782,U+17D2,U+179A
255  */
256  if (khmer_plan->mask_array[KHMER_CFAR])
257  for (unsigned int j = i + 2; j < end; j++)
258  info[j].mask |= khmer_plan->mask_array[KHMER_CFAR];
259 
260  num_coengs = 2; /* Done. */
261  }
262  }
263 
264  /* Reorder left matra piece. */
265  else if (info[i].khmer_category() == OT_VPre)
266  {
267  /* Move to the start. */
268  buffer->merge_clusters (start, i + 1);
269  hb_glyph_info_t t = info[i];
270  memmove (&info[start + 1], &info[start], (i - start) * sizeof (info[0]));
271  info[start] = t;
272  }
273  }
274 }
275 
276 static void
277 reorder_syllable_khmer (const hb_ot_shape_plan_t *plan,
278  hb_face_t *face,
280  unsigned int start, unsigned int end)
281 {
282  khmer_syllable_type_t syllable_type = (khmer_syllable_type_t) (buffer->info[start].syllable() & 0x0F);
283  switch (syllable_type)
284  {
285  case khmer_broken_cluster: /* We already inserted dotted-circles, so just call the consonant_syllable. */
287  reorder_consonant_syllable (plan, face, buffer, start, end);
288  break;
289 
291  break;
292  }
293 }
294 
295 static void
296 reorder_khmer (const hb_ot_shape_plan_t *plan,
297  hb_font_t *font,
299 {
300  if (buffer->message (font, "start reordering khmer"))
301  {
305  OT_Repha);
306 
308  reorder_syllable_khmer (plan, font->face, buffer, start, end);
309  (void) buffer->message (font, "end reordering khmer");
310  }
312 }
313 
314 
315 static bool
316 decompose_khmer (const hb_ot_shape_normalize_context_t *c,
317  hb_codepoint_t ab,
318  hb_codepoint_t *a,
319  hb_codepoint_t *b)
320 {
321  switch (ab)
322  {
323  /*
324  * Decompose split matras that don't have Unicode decompositions.
325  */
326 
327  /* Khmer */
328  case 0x17BEu : *a = 0x17C1u; *b= 0x17BEu; return true;
329  case 0x17BFu : *a = 0x17C1u; *b= 0x17BFu; return true;
330  case 0x17C0u : *a = 0x17C1u; *b= 0x17C0u; return true;
331  case 0x17C4u : *a = 0x17C1u; *b= 0x17C4u; return true;
332  case 0x17C5u : *a = 0x17C1u; *b= 0x17C5u; return true;
333  }
334 
335  return (bool) c->unicode->decompose (ab, a, b);
336 }
337 
338 static bool
339 compose_khmer (const hb_ot_shape_normalize_context_t *c,
342  hb_codepoint_t *ab)
343 {
344  /* Avoid recomposing split matras. */
345  if (HB_UNICODE_GENERAL_CATEGORY_IS_MARK (c->unicode->general_category (a)))
346  return false;
347 
348  return (bool) c->unicode->compose (a, b, ab);
349 }
350 
351 
353 {
354  collect_features_khmer,
355  override_features_khmer,
356  data_create_khmer,
357  data_destroy_khmer,
358  nullptr, /* preprocess_text */
359  nullptr, /* postprocess_glyphs */
361  decompose_khmer,
362  compose_khmer,
363  setup_masks_khmer,
364  HB_TAG_NONE, /* gpos_tag */
365  nullptr, /* reorder_marks */
367  false, /* fallback_position */
368 };
369 
370 
371 #endif
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
QMap< QString, QString > map
[6]
for(n=0;n< outline->n_points;n++)
Definition: ftbbox.c:494
#define HB_BUFFER_DEALLOCATE_VAR(b, var)
Definition: hb-buffer.hh:623
#define HB_BUFFER_ALLOCATE_VAR(b, var)
Definition: hb-buffer.hh:622
void const void *obj HB_UNUSED
Definition: hb-debug.hh:180
#define foreach_syllable(buffer, start, end)
@ F_GLOBAL_MANUAL_JOINERS
Definition: hb-ot-map.hh:185
@ F_MANUAL_JOINERS
Definition: hb-ot-map.hh:184
@ F_GLOBAL
Definition: hb-ot-map.hh:180
@ F_PER_SYLLABLE
Definition: hb-ot-map.hh:189
const hb_ot_complex_shaper_t _hb_ot_complex_shaper_khmer
@ KHMER_BASIC_FEATURES
#define khmer_category()
void hb_syllabic_insert_dotted_circles(hb_font_t *font, hb_buffer_t *buffer, unsigned int broken_syllable_type, unsigned int dottedcircle_category, int repha_category, int dottedcircle_position)
@ HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE
@ HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT
#define HB_UNICODE_GENERAL_CATEGORY_IS_MARK(gen_cat)
Definition: hb-unicode.hh:357
#define hb_calloc
Definition: hb.hh:236
#define unlikely(expr)
Definition: hb.hh:251
#define hb_free
Definition: hb.hh:238
backing_store_ptr info
[4]
Definition: jmemsys.h:161
void *PRIV() memmove(void *d, const void *s, size_t n)
void
Definition: png.h:1080
#define ARRAY_LENGTH(a)
Definition: qkmsdevice.cpp:52
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
[7]
GLuint GLuint end
GLenum GLenum GLsizei count
GLenum face
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t1
[4]
GLenum GLuint buffer
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t0
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLuint start
GLint GLint GLint GLint GLint GLint GLint GLbitfield mask
const GLubyte * c
Definition: qopenglext.h:12701
GLdouble GLdouble t
[9]
Definition: qopenglext.h:243
#define t0
#define t1
uint32_t hb_codepoint_t
Definition: hb-common.h:106
#define HB_TAG(c1, c2, c3, c4)
Definition: hb-common.h:169
uint32_t hb_mask_t
Definition: hb-common.h:122
#define HB_TAG_NONE
Definition: hb-common.h:187
hb_ot_map_feature_flags_t flags
Definition: hb-ot-map.hh:197
hb_mask_t get_1_mask(hb_tag_t feature_tag) const
Definition: hb-ot-map.hh:124
const void * data
Definition: hb-ot-shape.hh:67
hb_ot_map_t map
Definition: hb-ot-shape.hh:65
hb_ot_map_builder_t map
Definition: hb-ot-shape.hh:152
hb_mask_t mask_array[KHMER_NUM_FEATURES]
XmlOutput::xml_output tag(const QString &name)
Definition: xmloutput.h:154