Minor
[framework/uifw/harfbuzz.git] / src / hb-ot-layout-gpos-private.hh
index 249fae2..59750ab 100644 (file)
@@ -1,5 +1,6 @@
 /*
- * Copyright (C) 2007,2008,2009,2010  Red Hat, Inc.
+ * Copyright © 2007,2008,2009,2010  Red Hat, Inc.
+ * Copyright © 2010  Google, Inc.
  *
  *  This is part of HarfBuzz, a text shaping library.
  *
@@ -22,6 +23,7 @@
  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  *
  * Red Hat Author(s): Behdad Esfahbod
+ * Google Author(s): Behdad Esfahbod
  */
 
 #ifndef HB_OT_LAYOUT_GPOS_PRIVATE_HH
 
 #include "hb-ot-layout-gsubgpos-private.hh"
 
+HB_BEGIN_DECLS
+
+
+/* buffer var allocations */
+#define attach_lookback() var.u16[0] /* number of glyphs to go back to attach this glyph to its base */
+#define cursive_chain() var.i16[1] /* character to which this connects, may be positive or negative */
 
-#define HB_OT_LAYOUT_GPOS_NO_LAST ((unsigned int) -1)
 
 /* Shared Tables: ValueRecord, Anchor Table, and MarkArray */
 
@@ -82,49 +89,53 @@ struct ValueFormat : USHORT
                                         * PosTable (may be NULL) */
 #endif
 
-  inline unsigned int get_len () const
+  inline unsigned int get_len (void) const
   { return _hb_popcount32 ((unsigned int) *this); }
-  inline unsigned int get_size () const
+  inline unsigned int get_size (void) const
   { return get_len () * Value::static_size; }
 
-  void apply_value (hb_ot_layout_context_t       *layout,
-                   const void                   *base,
-                   const Value                  *values,
-                   hb_internal_glyph_position_t &glyph_pos) const
+  void apply_value (hb_font_t            *font,
+                   hb_direction_t        direction,
+                   const void           *base,
+                   const Value          *values,
+                   hb_glyph_position_t  &glyph_pos) const
   {
     unsigned int x_ppem, y_ppem;
-    hb_16dot16_t x_scale, y_scale;
     unsigned int format = *this;
+    hb_bool_t horizontal = HB_DIRECTION_IS_HORIZONTAL (direction);
 
     if (!format) return;
 
-    x_scale = layout->font->x_scale;
-    y_scale = layout->font->y_scale;
-    /* design units -> fractional pixel */
-    if (format & xPlacement) glyph_pos.x_offset  += _hb_16dot16_mul_round (x_scale, get_short (values++));
-    if (format & yPlacement) glyph_pos.y_offset  += _hb_16dot16_mul_round (y_scale, get_short (values++));
-    if (format & xAdvance)   glyph_pos.x_advance += _hb_16dot16_mul_round (x_scale, get_short (values++));
-    if (format & yAdvance)   glyph_pos.y_advance += _hb_16dot16_mul_round (y_scale, get_short (values++));
+    if (format & xPlacement) glyph_pos.x_offset  += font->em_scale_x (get_short (values++));
+    if (format & yPlacement) glyph_pos.y_offset  += font->em_scale_y (get_short (values++));
+    if (format & xAdvance) {
+      if (likely (horizontal)) glyph_pos.x_advance += font->em_scale_x (get_short (values++)); else values++;
+    }
+    /* y_advance values grow downward but font-space grows upward, hence negation */
+    if (format & yAdvance) {
+      if (unlikely (!horizontal)) glyph_pos.y_advance -= font->em_scale_y (get_short (values++)); else values++;
+    }
 
     if (!has_device ()) return;
 
-    x_ppem = layout->font->x_ppem;
-    y_ppem = layout->font->y_ppem;
+    x_ppem = font->x_ppem;
+    y_ppem = font->y_ppem;
 
     if (!x_ppem && !y_ppem) return;
 
     /* pixel -> fractional pixel */
     if (format & xPlaDevice) {
-      if (x_ppem) glyph_pos.x_offset  += (base + get_device (values++)).get_delta (x_ppem) << 16; else values++;
+      if (x_ppem) glyph_pos.x_offset  += (base + get_device (values++)).get_x_delta (font); else values++;
     }
     if (format & yPlaDevice) {
-      if (y_ppem) glyph_pos.y_offset  += (base + get_device (values++)).get_delta (y_ppem) << 16; else values++;
+      if (y_ppem) glyph_pos.y_offset  += (base + get_device (values++)).get_y_delta (font); else values++;
     }
     if (format & xAdvDevice) {
-      if (x_ppem) glyph_pos.x_advance += (base + get_device (values++)).get_delta (x_ppem) << 16; else values++;
+      if (horizontal && x_ppem) glyph_pos.x_advance += (base + get_device (values++)).get_x_delta (font); else values++;
     }
     if (format & yAdvDevice) {
-      if (y_ppem) glyph_pos.y_advance += (base + get_device (values++)).get_delta (y_ppem) << 16; else values++;
+      /* y_advance values grow downward but font-space grows upward, hence negation */
+      if (!horizontal && y_ppem) glyph_pos.y_advance -= (base + get_device (values++)).get_y_delta (font); else values++;
     }
   }
 
@@ -155,7 +166,7 @@ struct ValueFormat : USHORT
 
   public:
 
-  inline bool has_device () const {
+  inline bool has_device (void) const {
     unsigned int format = *this;
     return (format & devices) != 0;
   }
@@ -205,11 +216,11 @@ struct AnchorFormat1
   friend struct Anchor;
 
   private:
-  inline void get_anchor (hb_ot_layout_context_t *layout, hb_codepoint_t glyph_id HB_UNUSED,
+  inline void get_anchor (hb_font_t *font, hb_codepoint_t glyph_id HB_UNUSED,
                          hb_position_t *x, hb_position_t *y) const
   {
-      *x = _hb_16dot16_mul_round (layout->font->x_scale, xCoordinate);
-      *y = _hb_16dot16_mul_round (layout->font->y_scale, yCoordinate);
+      *x = font->em_scale_x (xCoordinate);
+      *y = font->em_scale_y (yCoordinate);
   }
 
   inline bool sanitize (hb_sanitize_context_t *c) {
@@ -230,18 +241,18 @@ struct AnchorFormat2
   friend struct Anchor;
 
   private:
-  inline void get_anchor (hb_ot_layout_context_t *layout, hb_codepoint_t glyph_id,
+  inline void get_anchor (hb_font_t *font, hb_codepoint_t glyph_id,
                          hb_position_t *x, hb_position_t *y) const
   {
-      unsigned int x_ppem = layout->font->x_ppem;
-      unsigned int y_ppem = layout->font->y_ppem;
+      unsigned int x_ppem = font->x_ppem;
+      unsigned int y_ppem = font->y_ppem;
       hb_position_t cx, cy;
-      hb_bool_t ret;
+      hb_bool_t ret = false;
 
       if (x_ppem || y_ppem)
-       ret = hb_font_get_contour_point (layout->font, layout->face, anchorPoint, glyph_id, &cx, &cy);
-      *x = x_ppem && ret ? cx : _hb_16dot16_mul_round (layout->font->x_scale, xCoordinate);
-      *y = y_ppem && ret ? cy : _hb_16dot16_mul_round (layout->font->y_scale, yCoordinate);
+       ret = hb_font_get_glyph_contour_point_for_origin (font, glyph_id, anchorPoint, HB_DIRECTION_LTR, &cx, &cy);
+      *x = x_ppem && ret ? cx : font->em_scale_x (xCoordinate);
+      *y = y_ppem && ret ? cy : font->em_scale_y (yCoordinate);
   }
 
   inline bool sanitize (hb_sanitize_context_t *c) {
@@ -263,17 +274,16 @@ struct AnchorFormat3
   friend struct Anchor;
 
   private:
-  inline void get_anchor (hb_ot_layout_context_t *layout, hb_codepoint_t glyph_id HB_UNUSED,
+  inline void get_anchor (hb_font_t *font, hb_codepoint_t glyph_id HB_UNUSED,
                          hb_position_t *x, hb_position_t *y) const
   {
-      *x = _hb_16dot16_mul_round (layout->font->x_scale, xCoordinate);
-      *y = _hb_16dot16_mul_round (layout->font->y_scale, yCoordinate);
-
-      /* pixel -> fractional pixel */
-      if (layout->font->x_ppem)
-       *x += (this+xDeviceTable).get_delta (layout->font->x_ppem) << 16;
-      if (layout->font->y_ppem)
-       *y += (this+yDeviceTable).get_delta (layout->font->y_ppem) << 16;
+      *x = font->em_scale_x (xCoordinate);
+      *y = font->em_scale_y (yCoordinate);
+
+      if (font->x_ppem)
+       *x += (this+xDeviceTable).get_x_delta (font);
+      if (font->y_ppem)
+       *y += (this+yDeviceTable).get_x_delta (font);
   }
 
   inline bool sanitize (hb_sanitize_context_t *c) {
@@ -301,15 +311,15 @@ struct AnchorFormat3
 
 struct Anchor
 {
-  inline void get_anchor (hb_ot_layout_context_t *layout, hb_codepoint_t glyph_id,
+  inline void get_anchor (hb_font_t *font, hb_codepoint_t glyph_id,
                          hb_position_t *x, hb_position_t *y) const
   {
     *x = *y = 0;
     switch (u.format) {
-    case 1: u.format1.get_anchor (layout, glyph_id, x, y); return;
-    case 2: u.format2.get_anchor (layout, glyph_id, x, y); return;
-    case 3: u.format3.get_anchor (layout, glyph_id, x, y); return;
-    default:                                               return;
+    case 1: u.format1.get_anchor (font, glyph_id, x, y); return;
+    case 2: u.format2.get_anchor (font, glyph_id, x, y); return;
+    case 3: u.format3.get_anchor (font, glyph_id, x, y); return;
+    default:                                            return;
     }
   }
 
@@ -346,7 +356,7 @@ struct AnchorMatrix
   inline bool sanitize (hb_sanitize_context_t *c, unsigned int cols) {
     TRACE_SANITIZE ();
     if (!c->check_struct (this)) return false;
-    if (unlikely (cols >= ((unsigned int) -1) / rows)) return false;
+    if (unlikely (rows > 0 && cols >= ((unsigned int) -1) / rows)) return false;
     unsigned int count = rows * cols;
     if (!c->check_array (matrix, matrix[0].static_size, count)) return false;
     for (unsigned int i = 0; i < count; i++)
@@ -399,17 +409,15 @@ struct MarkArray : ArrayOf<MarkRecord>    /* Array of MarkRecords--in Coverage orde
 
     hb_position_t mark_x, mark_y, base_x, base_y;
 
-    mark_anchor.get_anchor (c->layout, c->buffer->info[c->buffer->in_pos].codepoint, &mark_x, &mark_y);
-    glyph_anchor.get_anchor (c->layout, c->buffer->info[glyph_pos].codepoint, &base_x, &base_y);
+    mark_anchor.get_anchor (c->font, c->buffer->info[c->buffer->i].codepoint, &mark_x, &mark_y);
+    glyph_anchor.get_anchor (c->font, c->buffer->info[glyph_pos].codepoint, &base_x, &base_y);
 
-    hb_internal_glyph_position_t &o = c->buffer->positions[c->buffer->in_pos];
-    o.x_advance = 0;
-    o.y_advance = 0;
-    o.x_offset  = base_x - mark_x;
-    o.y_offset  = base_y - mark_y;
-    o.back      = c->buffer->in_pos - glyph_pos;
+    hb_glyph_position_t &o = c->buffer->pos[c->buffer->i];
+    o.x_offset = base_x - mark_x;
+    o.y_offset = base_y - mark_y;
+    o.attach_lookback() = c->buffer->i - glyph_pos;
 
-    c->buffer->in_pos++;
+    c->buffer->i++;
     return true;
   }
 
@@ -430,13 +438,14 @@ struct SinglePosFormat1
   inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
-    unsigned int index = (this+coverage) (c->buffer->info[c->buffer->in_pos].codepoint);
+    unsigned int index = (this+coverage) (c->buffer->info[c->buffer->i].codepoint);
     if (likely (index == NOT_COVERED))
       return false;
 
-    valueFormat.apply_value (c->layout, this, values, c->buffer->positions[c->buffer->in_pos]);
+    valueFormat.apply_value (c->font, c->direction, this,
+                            values, c->buffer->pos[c->buffer->i]);
 
-    c->buffer->in_pos++;
+    c->buffer->i++;
     return true;
   }
 
@@ -469,18 +478,18 @@ struct SinglePosFormat2
   inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
-    unsigned int index = (this+coverage) (c->buffer->info[c->buffer->in_pos].codepoint);
+    unsigned int index = (this+coverage) (c->buffer->info[c->buffer->i].codepoint);
     if (likely (index == NOT_COVERED))
       return false;
 
     if (likely (index >= valueCount))
       return false;
 
-    valueFormat.apply_value (c->layout, this,
+    valueFormat.apply_value (c->font, c->direction, this,
                             &values[index * valueFormat.get_len ()],
-                            c->buffer->positions[c->buffer->in_pos]);
+                            c->buffer->pos[c->buffer->i]);
 
-    c->buffer->in_pos++;
+    c->buffer->i++;
     return true;
   }
 
@@ -572,11 +581,13 @@ struct PairSet
     {
       if (c->buffer->info[pos].codepoint == record->secondGlyph)
       {
-       valueFormats[0].apply_value (c->layout, this, &record->values[0], c->buffer->positions[c->buffer->in_pos]);
-       valueFormats[1].apply_value (c->layout, this, &record->values[len1], c->buffer->positions[pos]);
+       valueFormats[0].apply_value (c->font, c->direction, this,
+                                    &record->values[0], c->buffer->pos[c->buffer->i]);
+       valueFormats[1].apply_value (c->font, c->direction, this,
+                                    &record->values[len1], c->buffer->pos[pos]);
        if (len2)
          pos++;
-       c->buffer->in_pos = pos;
+       c->buffer->i = pos;
        return true;
       }
       record = &StructAtOffset<PairValueRecord> (record, record_size);
@@ -619,16 +630,16 @@ struct PairPosFormat1
   inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
-    unsigned int end = MIN (c->buffer->in_length, c->buffer->in_pos + c->context_length);
-    if (unlikely (c->buffer->in_pos + 2 > end))
+    unsigned int end = MIN (c->buffer->len, c->buffer->i + c->context_length);
+    if (unlikely (c->buffer->i + 2 > end))
       return false;
 
-    unsigned int index = (this+coverage) (c->buffer->info[c->buffer->in_pos].codepoint);
+    unsigned int index = (this+coverage) (c->buffer->info[c->buffer->i].codepoint);
     if (likely (index == NOT_COVERED))
       return false;
 
-    unsigned int j = c->buffer->in_pos + 1;
-    while (_hb_ot_layout_skip_mark (c->layout->face, &c->buffer->info[j], c->lookup_flag, NULL))
+    unsigned int j = c->buffer->i + 1;
+    while (_hb_ot_layout_skip_mark (c->face, &c->buffer->info[j], c->lookup_props, NULL))
     {
       if (unlikely (j == end))
        return false;
@@ -681,16 +692,16 @@ struct PairPosFormat2
   inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
-    unsigned int end = MIN (c->buffer->in_length, c->buffer->in_pos + c->context_length);
-    if (unlikely (c->buffer->in_pos + 2 > end))
+    unsigned int end = MIN (c->buffer->len, c->buffer->i + c->context_length);
+    if (unlikely (c->buffer->i + 2 > end))
       return false;
 
-    unsigned int index = (this+coverage) (c->buffer->info[c->buffer->in_pos].codepoint);
+    unsigned int index = (this+coverage) (c->buffer->info[c->buffer->i].codepoint);
     if (likely (index == NOT_COVERED))
       return false;
 
-    unsigned int j = c->buffer->in_pos + 1;
-    while (_hb_ot_layout_skip_mark (c->layout->face, &c->buffer->info[j], c->lookup_flag, NULL))
+    unsigned int j = c->buffer->i + 1;
+    while (_hb_ot_layout_skip_mark (c->face, &c->buffer->info[j], c->lookup_props, NULL))
     {
       if (unlikely (j == end))
        return false;
@@ -701,18 +712,20 @@ struct PairPosFormat2
     unsigned int len2 = valueFormat2.get_len ();
     unsigned int record_len = len1 + len2;
 
-    unsigned int klass1 = (this+classDef1) (c->buffer->info[c->buffer->in_pos].codepoint);
+    unsigned int klass1 = (this+classDef1) (c->buffer->info[c->buffer->i].codepoint);
     unsigned int klass2 = (this+classDef2) (c->buffer->info[j].codepoint);
     if (unlikely (klass1 >= class1Count || klass2 >= class2Count))
       return false;
 
     const Value *v = &values[record_len * (klass1 * class2Count + klass2)];
-    valueFormat1.apply_value (c->layout, this, v, c->buffer->positions[c->buffer->in_pos]);
-    valueFormat2.apply_value (c->layout, this, v + len1, c->buffer->positions[j]);
+    valueFormat1.apply_value (c->font, c->direction, this,
+                             v, c->buffer->pos[c->buffer->i]);
+    valueFormat2.apply_value (c->font, c->direction, this,
+                             v + len1, c->buffer->pos[j]);
 
     if (len2)
       j++;
-    c->buffer->in_pos = j;
+    c->buffer->i = j;
 
     return true;
   }
@@ -829,175 +842,91 @@ struct CursivePosFormat1
   inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
-    /* Now comes the messiest part of the whole OpenType
-       specification.  At first glance, cursive connections seem easy
-       to understand, but there are pitfalls!  The reason is that
-       the specs don't mention how to compute the advance values
-       resp. glyph offsets.  I was told it would be an omission, to
-       be fixed in the next OpenType version...  Again many thanks to
-       Andrei Burago <andreib@microsoft.com> for clarifications.
-
-       Consider the following example:
-
-                       |  xadv1    |
-                        +---------+
-                        |         |
-                  +-----+--+ 1    |
-                  |     | .|      |
-                  |    0+--+------+
-                  |   2    |
-                  |        |
-                 0+--------+
-                 |  xadv2   |
-
-        glyph1: advance width = 12
-                anchor point = (3,1)
-
-        glyph2: advance width = 11
-                anchor point = (9,4)
-
-        LSB is 1 for both glyphs (so the boxes drawn above are glyph
-        bboxes).  Writing direction is R2L; `0' denotes the glyph's
-        coordinate origin.
-
-       Now the surprising part: The advance width of the *left* glyph
-       (resp. of the *bottom* glyph) will be modified, no matter
-       whether the writing direction is L2R or R2L (resp. T2B or
-       B2T)!  This assymetry is caused by the fact that the glyph's
-       coordinate origin is always the lower left corner for all
-       writing directions.
-
-       Continuing the above example, we can compute the new
-       (horizontal) advance width of glyph2 as
-
-        9 - 3 = 6  ,
-
-       and the new vertical offset of glyph2 as
-
-        1 - 4 = -3  .
-
-
-       Vertical writing direction is far more complicated:
-
-       a) Assuming that we recompute the advance height of the lower glyph:
-
-                                   --
-                        +---------+
-               --       |         |
-                  +-----+--+ 1    | yadv1
-                  |     | .|      |
-            yadv2 |    0+--+------+        -- BSB1  --
-                  |   2    |       --      --        y_offset
-                  |        |
-     BSB2 --      0+--------+                        --
-         --    --
-
-        glyph1: advance height = 6
-                anchor point = (3,1)
-
-        glyph2: advance height = 7
-                anchor point = (9,4)
-
-        TSB is 1 for both glyphs; writing direction is T2B.
-
-
-          BSB1     = yadv1 - (TSB1 + ymax1)
-          BSB2     = yadv2 - (TSB2 + ymax2)
-          y_offset = y2 - y1
-
-        vertical advance width of glyph2
-          = y_offset + BSB2 - BSB1
-          = (y2 - y1) + (yadv2 - (TSB2 + ymax2)) - (yadv1 - (TSB1 + ymax1))
-          = y2 - y1 + yadv2 - TSB2 - ymax2 - (yadv1 - TSB1 - ymax1)
-          = y2 - y1 + yadv2 - TSB2 - ymax2 - yadv1 + TSB1 + ymax1
-
-
-       b) Assuming that we recompute the advance height of the upper glyph:
-
-                                   --      --
-                        +---------+        -- TSB1
-         --    --       |         |
-     TSB2 --       +-----+--+ 1    | yadv1   ymax1
-                  |     | .|      |
-            yadv2 |    0+--+------+        --       --
-      ymax2        |   2    |       --                y_offset
-                  |        |
-         --      0+--------+                        --
-               --
-
-        glyph1: advance height = 6
-                anchor point = (3,1)
-
-        glyph2: advance height = 7
-                anchor point = (9,4)
-
-        TSB is 1 for both glyphs; writing direction is T2B.
-
-        y_offset = y2 - y1
-
-        vertical advance width of glyph2
-          = TSB1 + ymax1 + y_offset - (TSB2 + ymax2)
-          = TSB1 + ymax1 + y2 - y1 - TSB2 - ymax2
-
-
-       Comparing a) with b) shows that b) is easier to compute.  I'll wait
-       for a reply from Andrei to see what should really be implemented...
-
-       Since horizontal advance widths or vertical advance heights
-       can be used alone but not together, no ambiguity occurs.        */
-
-    struct hb_ot_layout_context_t::info_t::gpos_t *gpi = &c->layout->info.gpos;
-    hb_codepoint_t last_pos = gpi->last;
-    gpi->last = HB_OT_LAYOUT_GPOS_NO_LAST;
 
     /* We don't handle mark glyphs here. */
-    if (c->property == HB_OT_LAYOUT_GLYPH_CLASS_MARK)
+    if (c->property & HB_OT_LAYOUT_GLYPH_CLASS_MARK)
       return false;
 
-    unsigned int index = (this+coverage) (c->buffer->info[c->buffer->in_pos].codepoint);
-    if (likely (index == NOT_COVERED))
+    unsigned int end = MIN (c->buffer->len, c->buffer->i + c->context_length);
+    if (unlikely (c->buffer->i + 2 > end))
       return false;
 
-    const EntryExitRecord &record = entryExitRecord[index];
-
-    if (last_pos == HB_OT_LAYOUT_GPOS_NO_LAST || !record.entryAnchor)
-      goto end;
-
-    hb_position_t entry_x, entry_y;
-    (this+record.entryAnchor).get_anchor (c->layout, c->buffer->info[c->buffer->in_pos].codepoint, &entry_x, &entry_y);
-
-    /* TODO vertical */
+    const EntryExitRecord &this_record = entryExitRecord[(this+coverage) (c->buffer->info[c->buffer->i].codepoint)];
+    if (!this_record.exitAnchor)
+      return false;
 
-    if (c->buffer->direction == HB_DIRECTION_RTL)
+    unsigned int j = c->buffer->i + 1;
+    while (_hb_ot_layout_skip_mark (c->face, &c->buffer->info[j], c->lookup_props, NULL))
     {
-      /* advance is absolute, not relative */
-      c->buffer->positions[c->buffer->in_pos].x_advance = entry_x - gpi->anchor_x;
-    }
-    else
-    {
-      /* advance is absolute, not relative */
-      c->buffer->positions[last_pos].x_advance = gpi->anchor_x - entry_x;
+      if (unlikely (j == end))
+       return false;
+      j++;
     }
 
-    if  (c->lookup_flag & LookupFlag::RightToLeft)
-    {
-      c->buffer->positions[last_pos].cursive_chain = last_pos - c->buffer->in_pos;
-      c->buffer->positions[last_pos].y_offset = entry_y - gpi->anchor_y;
-    }
-    else
-    {
-      c->buffer->positions[c->buffer->in_pos].cursive_chain = c->buffer->in_pos - last_pos;
-      c->buffer->positions[c->buffer->in_pos].y_offset = gpi->anchor_y - entry_y;
+    const EntryExitRecord &next_record = entryExitRecord[(this+coverage) (c->buffer->info[j].codepoint)];
+    if (!next_record.entryAnchor)
+      return false;
+
+    unsigned int i = c->buffer->i;
+
+    hb_position_t entry_x, entry_y, exit_x, exit_y;
+    (this+this_record.exitAnchor).get_anchor (c->font, c->buffer->info[i].codepoint, &exit_x, &exit_y);
+    (this+next_record.entryAnchor).get_anchor (c->font, c->buffer->info[j].codepoint, &entry_x, &entry_y);
+
+    hb_glyph_position_t *pos = c->buffer->pos;
+
+    hb_position_t d;
+    /* Main-direction adjustment */
+    switch (c->direction) {
+      case HB_DIRECTION_LTR:
+       pos[i].x_advance  =  exit_x + pos[i].x_offset;
+
+       d = entry_x + pos[j].x_offset;
+       pos[j].x_advance -= d;
+       pos[j].x_offset  -= d;
+       break;
+      case HB_DIRECTION_RTL:
+       d = exit_x + pos[i].x_offset;
+       pos[i].x_advance -= d;
+       pos[i].x_offset  -= d;
+
+       pos[j].x_advance  =  entry_x + pos[j].x_offset;
+       break;
+      case HB_DIRECTION_TTB:
+       pos[i].y_advance  =  exit_y + pos[i].y_offset;
+
+       d = entry_y + pos[j].y_offset;
+       pos[j].y_advance -= d;
+       pos[j].y_offset  -= d;
+       break;
+      case HB_DIRECTION_BTT:
+       d = exit_y + pos[i].y_offset;
+       pos[i].y_advance -= d;
+       pos[i].y_offset  -= d;
+
+       pos[j].y_advance  =  entry_y;
+       break;
+      case HB_DIRECTION_INVALID:
+      default:
+       break;
     }
 
-  end:
-    if (record.exitAnchor)
-    {
-      gpi->last = c->buffer->in_pos;
-      (this+record.exitAnchor).get_anchor (c->layout, c->buffer->info[c->buffer->in_pos].codepoint, &gpi->anchor_x, &gpi->anchor_y);
+    /* Cross-direction adjustment */
+    if  (c->lookup_props & LookupFlag::RightToLeft) {
+      pos[i].cursive_chain() = j - i;
+      if (likely (HB_DIRECTION_IS_HORIZONTAL (c->direction)))
+       pos[i].y_offset = entry_y - exit_y;
+      else
+       pos[i].x_offset = entry_x - exit_x;
+    } else {
+      pos[j].cursive_chain() = i - j;
+      if (likely (HB_DIRECTION_IS_HORIZONTAL (c->direction)))
+       pos[j].y_offset = exit_y - entry_y;
+      else
+       pos[j].x_offset = exit_x - entry_x;
     }
 
-    c->buffer->in_pos++;
+    c->buffer->i = j;
     return true;
   }
 
@@ -1063,23 +992,23 @@ struct MarkBasePosFormat1
   inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
-    unsigned int mark_index = (this+markCoverage) (c->buffer->info[c->buffer->in_pos].codepoint);
+    unsigned int mark_index = (this+markCoverage) (c->buffer->info[c->buffer->i].codepoint);
     if (likely (mark_index == NOT_COVERED))
       return false;
 
     /* now we search backwards for a non-mark glyph */
     unsigned int property;
-    unsigned int j = c->buffer->in_pos;
+    unsigned int j = c->buffer->i;
     do
     {
       if (unlikely (!j))
        return false;
       j--;
-    } while (_hb_ot_layout_skip_mark (c->layout->face, &c->buffer->info[j], LookupFlag::IgnoreMarks, &property));
+    } while (_hb_ot_layout_skip_mark (c->face, &c->buffer->info[j], LookupFlag::IgnoreMarks, &property));
 
     /* The following assertion is too strong, so we've disabled it. */
-    if (false && !(property & HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH))
-      return false;
+    if (!(property & HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH))
+    {/*return false;*/}
 
     unsigned int base_index = (this+baseCoverage) (c->buffer->info[j].codepoint);
     if (base_index == NOT_COVERED)
@@ -1165,23 +1094,23 @@ struct MarkLigPosFormat1
   inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
-    unsigned int mark_index = (this+markCoverage) (c->buffer->info[c->buffer->in_pos].codepoint);
+    unsigned int mark_index = (this+markCoverage) (c->buffer->info[c->buffer->i].codepoint);
     if (likely (mark_index == NOT_COVERED))
       return false;
 
     /* now we search backwards for a non-mark glyph */
     unsigned int property;
-    unsigned int j = c->buffer->in_pos;
+    unsigned int j = c->buffer->i;
     do
     {
       if (unlikely (!j))
        return false;
       j--;
-    } while (_hb_ot_layout_skip_mark (c->layout->face, &c->buffer->info[j], LookupFlag::IgnoreMarks, &property));
+    } while (_hb_ot_layout_skip_mark (c->face, &c->buffer->info[j], LookupFlag::IgnoreMarks, &property));
 
     /* The following assertion is too strong, so we've disabled it. */
-    if (false && !(property & HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE))
-      return false;
+    if (!(property & HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE))
+    {/*return false;*/}
 
     unsigned int lig_index = (this+ligatureCoverage) (c->buffer->info[j].codepoint);
     if (lig_index == NOT_COVERED)
@@ -1199,9 +1128,9 @@ struct MarkLigPosFormat1
      * is identical to the ligature ID of the found ligature.  If yes, we
      * can directly use the component index.  If not, we attach the mark
      * glyph to the last component of the ligature. */
-    if (c->buffer->info[j].lig_id && c->buffer->info[j].lig_id == c->buffer->info[c->buffer->in_pos].lig_id && c->buffer->info[c->buffer->in_pos].component)
+    if (c->buffer->info[j].lig_id() && c->buffer->info[j].lig_id() == c->buffer->info[c->buffer->i].lig_id() && c->buffer->info[c->buffer->i].lig_comp())
     {
-      comp_index = c->buffer->info[c->buffer->in_pos].component - 1;
+      comp_index = c->buffer->info[c->buffer->i].lig_comp() - 1;
       if (comp_index >= comp_count)
        comp_index = comp_count - 1;
     }
@@ -1284,19 +1213,19 @@ struct MarkMarkPosFormat1
   inline bool apply (hb_apply_context_t *c) const
   {
     TRACE_APPLY ();
-    unsigned int mark1_index = (this+mark1Coverage) (c->buffer->info[c->buffer->in_pos].codepoint);
+    unsigned int mark1_index = (this+mark1Coverage) (c->buffer->info[c->buffer->i].codepoint);
     if (likely (mark1_index == NOT_COVERED))
       return false;
 
     /* now we search backwards for a suitable mark glyph until a non-mark glyph */
     unsigned int property;
-    unsigned int j = c->buffer->in_pos;
+    unsigned int j = c->buffer->i;
     do
     {
       if (unlikely (!j))
        return false;
       j--;
-    } while (_hb_ot_layout_skip_mark (c->layout->face, &c->buffer->info[j], c->lookup_flag, &property));
+    } while (_hb_ot_layout_skip_mark (c->face, &c->buffer->info[j], c->lookup_props, &property));
 
     if (!(property & HB_OT_LAYOUT_GLYPH_CLASS_MARK))
       return false;
@@ -1304,8 +1233,8 @@ struct MarkMarkPosFormat1
     /* Two marks match only if they belong to the same base, or same component
      * of the same ligature.  That is, the component numbers must match, and
      * if those are non-zero, the ligid number should also match. */
-    if ((c->buffer->info[j].component != c->buffer->info[c->buffer->in_pos].component) ||
-       (c->buffer->info[j].component && c->buffer->info[j].lig_id != c->buffer->info[c->buffer->in_pos].lig_id))
+    if ((c->buffer->info[j].lig_comp() != c->buffer->info[c->buffer->i].lig_comp()) ||
+       (c->buffer->info[j].lig_comp() && c->buffer->info[j].lig_id() != c->buffer->info[c->buffer->i].lig_id()))
       return false;
 
     unsigned int mark2_index = (this+mark2Coverage) (c->buffer->info[j].codepoint);
@@ -1376,7 +1305,9 @@ struct MarkMarkPos
 };
 
 
+HB_BEGIN_DECLS
 static inline bool position_lookup (hb_apply_context_t *c, unsigned int lookup_index);
+HB_END_DECLS
 
 struct ContextPos : Context
 {
@@ -1499,21 +1430,25 @@ struct PosLookup : Lookup
   inline const PosLookupSubTable& get_subtable (unsigned int i) const
   { return this+CastR<OffsetArrayOf<PosLookupSubTable> > (subTable)[i]; }
 
-  inline bool apply_once (hb_ot_layout_context_t *layout,
-                         hb_buffer_t    *buffer,
-                         unsigned int    context_length,
-                         unsigned int    nesting_level_left) const
+  inline bool apply_once (hb_font_t *font,
+                         hb_buffer_t *buffer,
+                         hb_mask_t lookup_mask,
+                         unsigned int context_length,
+                         unsigned int nesting_level_left) const
   {
     unsigned int lookup_type = get_type ();
     hb_apply_context_t c[1] = {{0}};
 
-    c->layout = layout;
+    c->font = font;
+    c->face = font->face;
     c->buffer = buffer;
+    c->direction = buffer->props.direction;
+    c->lookup_mask = lookup_mask;
     c->context_length = context_length;
     c->nesting_level_left = nesting_level_left;
-    c->lookup_flag = get_flag ();
+    c->lookup_props = get_props ();
 
-    if (!_hb_ot_layout_check_glyph_property (c->layout->face, &c->buffer->info[c->buffer->in_pos], c->lookup_flag, &c->property))
+    if (!_hb_ot_layout_check_glyph_property (c->face, &c->buffer->info[c->buffer->i], c->lookup_props, &c->property))
       return false;
 
     for (unsigned int i = 0; i < get_subtable_count (); i++)
@@ -1523,36 +1458,23 @@ struct PosLookup : Lookup
     return false;
   }
 
-   inline bool apply_string (hb_ot_layout_context_t *layout,
+   inline bool apply_string (hb_font_t   *font,
                             hb_buffer_t *buffer,
                             hb_mask_t    mask) const
   {
     bool ret = false;
 
-    if (unlikely (!buffer->in_length))
+    if (unlikely (!buffer->len))
       return false;
 
-    layout->info.gpos.last = HB_OT_LAYOUT_GPOS_NO_LAST; /* no last valid glyph for cursive pos. */
-
-    buffer->in_pos = 0;
-    while (buffer->in_pos < buffer->in_length)
+    buffer->i = 0;
+    while (buffer->i < buffer->len)
     {
-      bool done;
-      if (~buffer->info[buffer->in_pos].mask & mask)
-      {
-         done = apply_once (layout, buffer, NO_CONTEXT, MAX_NESTING_LEVEL);
-         ret |= done;
-      }
+      if ((buffer->info[buffer->i].mask & mask) &&
+         apply_once (font, buffer, mask, NO_CONTEXT, MAX_NESTING_LEVEL))
+       ret = true;
       else
-      {
-          done = false;
-         /* Contrary to properties defined in GDEF, user-defined properties
-            will always stop a possible cursive positioning.                */
-         layout->info.gpos.last = HB_OT_LAYOUT_GPOS_NO_LAST;
-      }
-
-      if (!done)
-       buffer->in_pos++;
+       buffer->i++;
     }
 
     return ret;
@@ -1579,11 +1501,13 @@ struct GPOS : GSUBGPOS
   inline const PosLookup& get_lookup (unsigned int i) const
   { return CastR<PosLookup> (GSUBGPOS::get_lookup (i)); }
 
-  inline bool position_lookup (hb_ot_layout_context_t *layout,
+  inline bool position_lookup (hb_font_t    *font,
                               hb_buffer_t  *buffer,
                               unsigned int  lookup_index,
                               hb_mask_t     mask) const
-  { return get_lookup (lookup_index).apply_string (layout, buffer, mask); }
+  { return get_lookup (lookup_index).apply_string (font, buffer, mask); }
+
+  static inline void position_finish (hb_buffer_t *buffer);
 
   inline bool sanitize (hb_sanitize_context_t *c) {
     TRACE_SANITIZE ();
@@ -1596,6 +1520,71 @@ struct GPOS : GSUBGPOS
 };
 
 
+static void
+fix_cursive_minor_offset (hb_glyph_position_t *pos, unsigned int i, hb_direction_t direction)
+{
+    unsigned int j = pos[i].cursive_chain();
+    if (likely (!j))
+      return;
+
+    j += i;
+
+    pos[i].cursive_chain() = 0;
+
+    fix_cursive_minor_offset (pos, j, direction);
+
+    if (HB_DIRECTION_IS_HORIZONTAL (direction))
+      pos[i].y_offset += pos[j].y_offset;
+    else
+      pos[i].x_offset += pos[j].x_offset;
+}
+
+static void
+fix_mark_attachment (hb_glyph_position_t *pos, unsigned int i, hb_direction_t direction)
+{
+  if (likely (!(pos[i].attach_lookback())))
+    return;
+
+  unsigned int j = i - pos[i].attach_lookback();
+
+  pos[i].x_advance = 0;
+  pos[i].y_advance = 0;
+  pos[i].x_offset += pos[j].x_offset;
+  pos[i].y_offset += pos[j].y_offset;
+
+  if (HB_DIRECTION_IS_FORWARD (direction))
+    for (unsigned int k = j; k < i; k++) {
+      pos[i].x_offset -= pos[k].x_advance;
+      pos[i].y_offset -= pos[k].y_advance;
+    }
+  else
+    for (unsigned int k = j + 1; k < i + 1; k++) {
+      pos[i].x_offset += pos[k].x_advance;
+      pos[i].y_offset += pos[k].y_advance;
+    }
+}
+
+void
+GPOS::position_finish (hb_buffer_t *buffer)
+{
+  unsigned int len;
+  hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, &len);
+  hb_direction_t direction = buffer->props.direction;
+
+  /* Handle cursive connections */
+  for (unsigned int i = 0; i < len; i++)
+  {
+    fix_cursive_minor_offset (pos, i, direction);
+  }
+
+  /* Handle attachments */
+  for (unsigned int i = 0; i < len; i++)
+  {
+    fix_mark_attachment (pos, i, direction);
+  }
+}
+
+
 /* Out-of-class implementation for methods recursing */
 
 inline bool ExtensionPos::apply (hb_apply_context_t *c) const
@@ -1615,7 +1604,7 @@ inline bool ExtensionPos::sanitize (hb_sanitize_context_t *c)
 
 static inline bool position_lookup (hb_apply_context_t *c, unsigned int lookup_index)
 {
-  const GPOS &gpos = *(c->layout->face->ot_layout.gpos);
+  const GPOS &gpos = *(c->face->ot_layout->gpos);
   const PosLookup &l = gpos.get_lookup (lookup_index);
 
   if (unlikely (c->nesting_level_left == 0))
@@ -1624,8 +1613,14 @@ static inline bool position_lookup (hb_apply_context_t *c, unsigned int lookup_i
   if (unlikely (c->context_length < 1))
     return false;
 
-  return l.apply_once (c->layout, c->buffer, c->context_length, c->nesting_level_left - 1);
+  return l.apply_once (c->font, c->buffer, c->lookup_mask, c->context_length, c->nesting_level_left - 1);
 }
 
 
+#undef attach_lookback
+#undef cursive_chain
+
+
+HB_END_DECLS
+
 #endif /* HB_OT_LAYOUT_GPOS_PRIVATE_HH */