Fix scale issues
[framework/uifw/harfbuzz.git] / src / hb-ot-layout.cc
1 /*
2  * Copyright (C) 1998-2004  David Turner and Werner Lemberg
3  * Copyright (C) 2006  Behdad Esfahbod
4  * Copyright (C) 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
29 #define HB_OT_LAYOUT_CC
30
31 #include "hb-ot-layout-private.hh"
32
33 #include "hb-ot-layout-gdef-private.hh"
34 #include "hb-ot-layout-gsub-private.hh"
35 #include "hb-ot-layout-gpos-private.hh"
36
37
38 #include <stdlib.h>
39 #include <string.h>
40
41
42 hb_ot_layout_t *
43 _hb_ot_layout_new (hb_face_t *face)
44 {
45   /* Remove this object altogether */
46   hb_ot_layout_t *layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
47
48   layout->gdef_blob = Sanitizer<GDEF>::sanitize (hb_face_get_table (face, HB_OT_TAG_GDEF));
49   layout->gdef = Sanitizer<GDEF>::lock_instance (layout->gdef_blob);
50
51   layout->gsub_blob = Sanitizer<GSUB>::sanitize (hb_face_get_table (face, HB_OT_TAG_GSUB));
52   layout->gsub = Sanitizer<GSUB>::lock_instance (layout->gsub_blob);
53
54   layout->gpos_blob = Sanitizer<GPOS>::sanitize (hb_face_get_table (face, HB_OT_TAG_GPOS));
55   layout->gpos = Sanitizer<GPOS>::lock_instance (layout->gpos_blob);
56
57   return layout;
58 }
59
60 void
61 _hb_ot_layout_free (hb_ot_layout_t *layout)
62 {
63   hb_blob_unlock (layout->gdef_blob);
64   hb_blob_unlock (layout->gsub_blob);
65   hb_blob_unlock (layout->gpos_blob);
66
67   hb_blob_destroy (layout->gdef_blob);
68   hb_blob_destroy (layout->gsub_blob);
69   hb_blob_destroy (layout->gpos_blob);
70
71   free (layout->new_gdef.klasses);
72 }
73
74 static const GDEF&
75 _get_gdef (hb_face_t *face)
76 {
77   return likely (face->ot_layout->gdef) ? *face->ot_layout->gdef : Null(GDEF);
78 }
79
80 static const GSUB&
81 _get_gsub (hb_face_t *face)
82 {
83   return likely (face->ot_layout->gsub) ? *face->ot_layout->gsub : Null(GSUB);
84 }
85
86 static const GPOS&
87 _get_gpos (hb_face_t *face)
88 {
89   return likely (face->ot_layout->gpos) ? *face->ot_layout->gpos : Null(GPOS);
90 }
91
92
93 /*
94  * GDEF
95  */
96
97 /* TODO the public class_t is a mess */
98
99 hb_bool_t
100 hb_ot_layout_has_glyph_classes (hb_face_t *face)
101 {
102   return _get_gdef (face).has_glyph_classes ();
103 }
104
105 hb_bool_t
106 _hb_ot_layout_has_new_glyph_classes (hb_face_t *face)
107 {
108   return face->ot_layout->new_gdef.len > 0;
109 }
110
111 static unsigned int
112 _hb_ot_layout_get_glyph_property (hb_face_t      *face,
113                                   hb_codepoint_t  glyph)
114 {
115   hb_ot_layout_class_t klass;
116   const GDEF &gdef = _get_gdef (face);
117
118   klass = gdef.get_glyph_class (glyph);
119
120   if (!klass && glyph < face->ot_layout->new_gdef.len)
121     klass = face->ot_layout->new_gdef.klasses[glyph];
122
123   switch (klass) {
124   default:
125   case GDEF::UnclassifiedGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED;
126   case GDEF::BaseGlyph:         return HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;
127   case GDEF::LigatureGlyph:     return HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE;
128   case GDEF::ComponentGlyph:    return HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT;
129   case GDEF::MarkGlyph:
130         klass = gdef.get_mark_attachment_type (glyph);
131         return HB_OT_LAYOUT_GLYPH_CLASS_MARK + (klass << 8);
132   }
133 }
134
135 hb_bool_t
136 _hb_ot_layout_check_glyph_property (hb_face_t    *face,
137                                     hb_internal_glyph_info_t *ginfo,
138                                     unsigned int  lookup_flags,
139                                     unsigned int *property_out)
140 {
141   unsigned int property;
142
143   if (ginfo->gproperty == HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN)
144     ginfo->gproperty = _hb_ot_layout_get_glyph_property (face, ginfo->codepoint);
145   property = ginfo->gproperty;
146   if (property_out)
147     *property_out = property;
148
149   /* Not covered, if, for example, glyph class is ligature and
150    * lookup_flags includes LookupFlags::IgnoreLigatures
151    */
152   if (property & lookup_flags & LookupFlag::IgnoreFlags)
153     return false;
154
155   if (property & HB_OT_LAYOUT_GLYPH_CLASS_MARK)
156   {
157     /* If using mark filtering sets, the high short of
158      * lookup_flags has the set index.
159      */
160     if (lookup_flags & LookupFlag::UseMarkFilteringSet)
161       return _get_gdef (face).mark_set_covers (lookup_flags >> 16, ginfo->codepoint);
162
163     /* The second byte of lookup_flags has the meaning
164      * "ignore marks of attachment type different than
165      * the attachment type specified."
166      */
167     if (lookup_flags & LookupFlag::MarkAttachmentType && property & LookupFlag::MarkAttachmentType)
168       return (lookup_flags & LookupFlag::MarkAttachmentType) == (property & LookupFlag::MarkAttachmentType);
169   }
170
171   return true;
172 }
173
174 hb_bool_t
175 _hb_ot_layout_skip_mark (hb_face_t    *face,
176                          hb_internal_glyph_info_t *ginfo,
177                          unsigned int  lookup_flags,
178                          unsigned int *property_out)
179 {
180   unsigned int property;
181
182   if (ginfo->gproperty == HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN)
183     ginfo->gproperty = _hb_ot_layout_get_glyph_property (face, ginfo->codepoint);
184   property = ginfo->gproperty;
185   if (property_out)
186     *property_out = property;
187
188   if (property & HB_OT_LAYOUT_GLYPH_CLASS_MARK)
189   {
190     /* Skip mark if lookup_flags includes LookupFlags::IgnoreMarks */
191     if (lookup_flags & LookupFlag::IgnoreMarks)
192       return true;
193
194     /* If using mark filtering sets, the high short of lookup_flags has the set index. */
195     if (lookup_flags & LookupFlag::UseMarkFilteringSet)
196       return !_get_gdef (face).mark_set_covers (lookup_flags >> 16, ginfo->codepoint);
197
198     /* The second byte of lookup_flags has the meaning "ignore marks of attachment type
199      * different than the attachment type specified." */
200     if (lookup_flags & LookupFlag::MarkAttachmentType && property & LookupFlag::MarkAttachmentType)
201       return (lookup_flags & LookupFlag::MarkAttachmentType) != (property & LookupFlag::MarkAttachmentType);
202   }
203
204   return false;
205 }
206
207 void
208 _hb_ot_layout_set_glyph_class (hb_face_t                  *face,
209                                hb_codepoint_t              glyph,
210                                hb_ot_layout_glyph_class_t  klass)
211 {
212   if (HB_OBJECT_IS_INERT (face))
213     return;
214
215   /* TODO optimize this? similar to old harfbuzz code for example */
216
217   hb_ot_layout_t *layout = face->ot_layout;
218   hb_ot_layout_class_t gdef_klass;
219   unsigned int len = layout->new_gdef.len;
220
221   if (unlikely (glyph > 65535))
222     return;
223
224   /* XXX this is not threadsafe */
225   if (glyph >= len) {
226     unsigned int new_len;
227     unsigned char *new_klasses;
228
229     new_len = len == 0 ? 120 : 2 * len;
230     while (new_len <= glyph)
231       new_len *= 2;
232
233     if (new_len > 65536)
234       new_len = 65536;
235     new_klasses = (unsigned char *) realloc (layout->new_gdef.klasses, new_len * sizeof (unsigned char));
236
237     if (unlikely (!new_klasses))
238       return;
239
240     memset (new_klasses + len, 0, new_len - len);
241
242     layout->new_gdef.klasses = new_klasses;
243     layout->new_gdef.len = new_len;
244   }
245
246   switch (klass) {
247   default:
248   case HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED:   gdef_klass = GDEF::UnclassifiedGlyph;   break;
249   case HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH:     gdef_klass = GDEF::BaseGlyph;           break;
250   case HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE:       gdef_klass = GDEF::LigatureGlyph;       break;
251   case HB_OT_LAYOUT_GLYPH_CLASS_MARK:           gdef_klass = GDEF::MarkGlyph;           break;
252   case HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT:      gdef_klass = GDEF::ComponentGlyph;      break;
253   }
254
255   layout->new_gdef.klasses[glyph] = gdef_klass;
256   return;
257 }
258
259 void
260 _hb_ot_layout_set_glyph_property (hb_face_t      *face,
261                                   hb_codepoint_t  glyph,
262                                   unsigned int    property)
263 { _hb_ot_layout_set_glyph_class (face, glyph, (hb_ot_layout_glyph_class_t) (property & 0xff)); }
264
265
266 hb_ot_layout_glyph_class_t
267 hb_ot_layout_get_glyph_class (hb_face_t      *face,
268                               hb_codepoint_t  glyph)
269 {
270   return (hb_ot_layout_glyph_class_t) (_hb_ot_layout_get_glyph_property (face, glyph) & 0xff);
271 }
272
273 void
274 hb_ot_layout_set_glyph_class (hb_face_t                 *face,
275                               hb_codepoint_t             glyph,
276                               hb_ot_layout_glyph_class_t klass)
277 {
278   _hb_ot_layout_set_glyph_class (face, glyph, klass);
279 }
280
281 void
282 hb_ot_layout_build_glyph_classes (hb_face_t      *face,
283                                   hb_codepoint_t *glyphs,
284                                   unsigned char  *klasses,
285                                   uint16_t        count)
286 {
287   if (HB_OBJECT_IS_INERT (face))
288     return;
289
290   hb_ot_layout_t *layout = face->ot_layout;
291
292   if (unlikely (!count || !glyphs || !klasses))
293     return;
294
295   if (layout->new_gdef.len == 0) {
296     layout->new_gdef.klasses = (unsigned char *) calloc (count, sizeof (unsigned char));
297     layout->new_gdef.len = count;
298   }
299
300   for (unsigned int i = 0; i < count; i++)
301     _hb_ot_layout_set_glyph_class (face, glyphs[i], (hb_ot_layout_glyph_class_t) klasses[i]);
302 }
303
304 unsigned int
305 hb_ot_layout_get_attach_points (hb_face_t      *face,
306                                 hb_codepoint_t  glyph,
307                                 unsigned int    start_offset,
308                                 unsigned int   *point_count /* IN/OUT */,
309                                 unsigned int   *point_array /* OUT */)
310 {
311   return _get_gdef (face).get_attach_points (glyph, start_offset, point_count, point_array);
312 }
313
314 unsigned int
315 hb_ot_layout_get_lig_carets (hb_font_t      *font,
316                              hb_face_t      *face,
317                              hb_codepoint_t  glyph,
318                              unsigned int    start_offset,
319                              unsigned int   *caret_count /* IN/OUT */,
320                              int            *caret_array /* OUT */)
321 {
322   hb_ot_layout_context_t c;
323   c.font = font;
324   c.face = face;
325   return _get_gdef (face).get_lig_carets (&c, glyph, start_offset, caret_count, caret_array);
326 }
327
328 /*
329  * GSUB/GPOS
330  */
331
332 static const GSUBGPOS&
333 get_gsubgpos_table (hb_face_t *face,
334                     hb_tag_t   table_tag)
335 {
336   switch (table_tag) {
337     case HB_OT_TAG_GSUB: return _get_gsub (face);
338     case HB_OT_TAG_GPOS: return _get_gpos (face);
339     default:             return Null(GSUBGPOS);
340   }
341 }
342
343
344 unsigned int
345 hb_ot_layout_table_get_script_tags (hb_face_t    *face,
346                                     hb_tag_t      table_tag,
347                                     unsigned int  start_offset,
348                                     unsigned int *script_count /* IN/OUT */,
349                                     hb_tag_t     *script_tags /* OUT */)
350 {
351   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
352
353   return g.get_script_tags (start_offset, script_count, script_tags);
354 }
355
356 hb_bool_t
357 hb_ot_layout_table_find_script (hb_face_t    *face,
358                                 hb_tag_t      table_tag,
359                                 hb_tag_t      script_tag,
360                                 unsigned int *script_index)
361 {
362   ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
363   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
364
365   if (g.find_script_index (script_tag, script_index))
366     return TRUE;
367
368   /* try finding 'DFLT' */
369   if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index))
370     return FALSE;
371
372   /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
373   if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index))
374     return FALSE;
375
376   if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
377   return FALSE;
378 }
379
380 hb_bool_t
381 hb_ot_layout_table_choose_script (hb_face_t      *face,
382                                   hb_tag_t        table_tag,
383                                   const hb_tag_t *script_tags,
384                                   unsigned int   *script_index)
385 {
386   ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
387   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
388
389   while (*script_tags)
390   {
391     if (g.find_script_index (*script_tags, script_index))
392       return TRUE;
393     script_tags++;
394   }
395
396   /* try finding 'DFLT' */
397   if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index))
398     return FALSE;
399
400   /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
401   if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index))
402     return FALSE;
403
404   if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
405   return FALSE;
406 }
407
408 unsigned int
409 hb_ot_layout_table_get_feature_tags (hb_face_t    *face,
410                                      hb_tag_t      table_tag,
411                                      unsigned int  start_offset,
412                                      unsigned int *feature_count /* IN/OUT */,
413                                      hb_tag_t     *feature_tags /* OUT */)
414 {
415   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
416
417   return g.get_feature_tags (start_offset, feature_count, feature_tags);
418 }
419
420
421 unsigned int
422 hb_ot_layout_script_get_language_tags (hb_face_t    *face,
423                                        hb_tag_t      table_tag,
424                                        unsigned int  script_index,
425                                        unsigned int  start_offset,
426                                        unsigned int *language_count /* IN/OUT */,
427                                        hb_tag_t     *language_tags /* OUT */)
428 {
429   const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
430
431   return s.get_lang_sys_tags (start_offset, language_count, language_tags);
432 }
433
434 hb_bool_t
435 hb_ot_layout_script_find_language (hb_face_t    *face,
436                                    hb_tag_t      table_tag,
437                                    unsigned int  script_index,
438                                    hb_tag_t      language_tag,
439                                    unsigned int *language_index)
440 {
441   ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
442   const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
443
444   if (s.find_lang_sys_index (language_tag, language_index))
445     return TRUE;
446
447   /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
448   if (s.find_lang_sys_index (HB_OT_TAG_DEFAULT_LANGUAGE, language_index))
449     return FALSE;
450
451   if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
452   return FALSE;
453 }
454
455 hb_bool_t
456 hb_ot_layout_language_get_required_feature_index (hb_face_t    *face,
457                                                   hb_tag_t      table_tag,
458                                                   unsigned int  script_index,
459                                                   unsigned int  language_index,
460                                                   unsigned int *feature_index)
461 {
462   const LangSys &l = get_gsubgpos_table (face, table_tag).get_script (script_index).get_lang_sys (language_index);
463
464   if (feature_index) *feature_index = l.get_required_feature_index ();
465
466   return l.has_required_feature ();
467 }
468
469 unsigned int
470 hb_ot_layout_language_get_feature_indexes (hb_face_t    *face,
471                                            hb_tag_t      table_tag,
472                                            unsigned int  script_index,
473                                            unsigned int  language_index,
474                                            unsigned int  start_offset,
475                                            unsigned int *feature_count /* IN/OUT */,
476                                            unsigned int *feature_indexes /* OUT */)
477 {
478   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
479   const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
480
481   return l.get_feature_indexes (start_offset, feature_count, feature_indexes);
482 }
483
484 unsigned int
485 hb_ot_layout_language_get_feature_tags (hb_face_t    *face,
486                                         hb_tag_t      table_tag,
487                                         unsigned int  script_index,
488                                         unsigned int  language_index,
489                                         unsigned int  start_offset,
490                                         unsigned int *feature_count /* IN/OUT */,
491                                         hb_tag_t     *feature_tags /* OUT */)
492 {
493   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
494   const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
495
496   ASSERT_STATIC (sizeof (unsigned int) == sizeof (hb_tag_t));
497   unsigned int ret = l.get_feature_indexes (start_offset, feature_count, (unsigned int *) feature_tags);
498
499   if (feature_tags) {
500     unsigned int count = *feature_count;
501     for (unsigned int i = 0; i < count; i++)
502       feature_tags[i] = g.get_feature_tag ((unsigned int) feature_tags[i]);
503   }
504
505   return ret;
506 }
507
508
509 hb_bool_t
510 hb_ot_layout_language_find_feature (hb_face_t    *face,
511                                     hb_tag_t      table_tag,
512                                     unsigned int  script_index,
513                                     unsigned int  language_index,
514                                     hb_tag_t      feature_tag,
515                                     unsigned int *feature_index)
516 {
517   ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
518   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
519   const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
520
521   unsigned int num_features = l.get_feature_count ();
522   for (unsigned int i = 0; i < num_features; i++) {
523     unsigned int f_index = l.get_feature_index (i);
524
525     if (feature_tag == g.get_feature_tag (f_index)) {
526       if (feature_index) *feature_index = f_index;
527       return TRUE;
528     }
529   }
530
531   if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
532   return FALSE;
533 }
534
535 unsigned int
536 hb_ot_layout_feature_get_lookup_indexes (hb_face_t    *face,
537                                          hb_tag_t      table_tag,
538                                          unsigned int  feature_index,
539                                          unsigned int  start_offset,
540                                          unsigned int *lookup_count /* IN/OUT */,
541                                          unsigned int *lookup_indexes /* OUT */)
542 {
543   const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
544   const Feature &f = g.get_feature (feature_index);
545
546   return f.get_lookup_indexes (start_offset, lookup_count, lookup_indexes);
547 }
548
549
550 /*
551  * GSUB
552  */
553
554 hb_bool_t
555 hb_ot_layout_has_substitution (hb_face_t *face)
556 {
557   return &_get_gsub (face) != &Null(GSUB);
558 }
559
560 hb_bool_t
561 hb_ot_layout_substitute_lookup (hb_face_t    *face,
562                                 hb_buffer_t  *buffer,
563                                 unsigned int  lookup_index,
564                                 hb_mask_t     mask)
565 {
566   hb_ot_layout_context_t c;
567   c.font = NULL;
568   c.face = face;
569   return _get_gsub (face).substitute_lookup (&c, buffer, lookup_index, mask);
570 }
571
572
573 /*
574  * GPOS
575  */
576
577 hb_bool_t
578 hb_ot_layout_has_positioning (hb_face_t *face)
579 {
580   return &_get_gpos (face) != &Null(GPOS);
581 }
582
583 hb_bool_t
584 hb_ot_layout_position_lookup   (hb_font_t    *font,
585                                 hb_face_t    *face,
586                                 hb_buffer_t  *buffer,
587                                 unsigned int  lookup_index,
588                                 hb_mask_t     mask)
589 {
590   hb_ot_layout_context_t c;
591   c.font = font;
592   c.face = face;
593   return _get_gpos (face).position_lookup (&c, buffer, lookup_index, mask);
594 }
595
596 void
597 hb_ot_layout_position_finish (hb_font_t    *font HB_UNUSED,
598                               hb_face_t    *face HB_UNUSED,
599                               hb_buffer_t  *buffer)
600 {
601   unsigned int i, j;
602   unsigned int len = hb_buffer_get_length (buffer);
603   hb_internal_glyph_position_t *pos = (hb_internal_glyph_position_t *) hb_buffer_get_glyph_positions (buffer);
604
605   /* TODO: Vertical */
606
607   /* Handle cursive connections */
608   /* First handle all left-to-right connections */
609   for (j = 0; j < len; j++) {
610     if (pos[j].cursive_chain > 0)
611     {
612       pos[j].y_offset += pos[j - pos[j].cursive_chain].y_offset;
613       pos[j].cursive_chain = 0;
614     }
615   }
616   /* Then handle all right-to-left connections */
617   for (i = len; i > 0; i--) {
618     j = i - 1;
619     if (pos[j].cursive_chain < 0)
620     {
621       pos[j].y_offset += pos[j - pos[j].cursive_chain].y_offset;
622       pos[j].cursive_chain = 0;
623     }
624   }
625
626   /* Handle attachments */
627   for (i = 0; i < len; i++)
628     if (pos[i].back)
629     {
630       unsigned int back = i - pos[i].back;
631       pos[i].back = 0;
632       pos[i].x_offset += pos[back].x_offset;
633       pos[i].y_offset += pos[back].y_offset;
634
635       if (buffer->direction == HB_DIRECTION_RTL)
636         for (j = back + 1; j < i + 1; j++) {
637           pos[i].x_offset += pos[j].x_advance;
638           pos[i].y_offset += pos[j].y_advance;
639         }
640       else
641         for (j = back; j < i; j++) {
642           pos[i].x_offset -= pos[j].x_advance;
643           pos[i].y_offset -= pos[j].y_advance;
644         }
645     }
646 }