From 0dfc0f2872b1d4739cc262fb4372751214b71ca9 Mon Sep 17 00:00:00 2001
From: Konstantin Ritt <ritt.ks@gmail.com>
Date: Thu, 23 Jan 2014 21:39:01 +0200
Subject: Make CoreText backend support stretch factor

If x_scale differs from y_scale, the font is stretched and the
horizontal advance must be adjusted according to this stretch factor.
---
 src/hb-coretext.cc | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/src/hb-coretext.cc b/src/hb-coretext.cc
index 17a722d..7d78a78 100644
--- a/src/hb-coretext.cc
+++ b/src/hb-coretext.cc
@@ -120,7 +120,11 @@ _hb_coretext_shaper_font_data_create (hb_font_t *font)
   hb_face_t *face = font->face;
   hb_coretext_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face);
 
-  data->ct_font = CTFontCreateWithGraphicsFont (face_data->cg_font, font->y_scale, NULL, NULL);
+  CGAffineTransform transform = CGAffineTransformIdentity;
+  if (font->x_scale != font->y_scale)
+      transform = CGAffineTransformMakeScale ((double) font->x_scale / font->y_scale, 1.0);
+
+  data->ct_font = CTFontCreateWithGraphicsFont (face_data->cg_font, font->y_scale, &transform, NULL);
   if (unlikely (!data->ct_font)) {
     DEBUG_MSG (CORETEXT, font, "Font CTFontCreateWithGraphicsFont() failed");
     free (data);
@@ -679,7 +683,8 @@ _hb_coretext_shape (hb_shape_plan_t    *shape_plan,
       buffer->len += num_glyphs;
 
       CGGlyph notdef = 0;
-      double advance = CTFontGetAdvancesForGlyphs (font_data->ct_font, kCTFontHorizontalOrientation, &notdef, NULL, 1);
+      CGSize advance = CGSizeMake (CTFontGetAdvancesForGlyphs (font_data->ct_font, kCTFontHorizontalOrientation, &notdef, NULL, 1), 0);
+      advance = CGSizeApplyAffineTransform (advance, CTRunGetTextMatrix (run));
 
       for (CFIndex j = 0; j < num_glyphs; j++)
 	{
@@ -688,7 +693,7 @@ _hb_coretext_shape (hb_shape_plan_t    *shape_plan,
 	     * hb-uniscribe.cc for example. */
             info->cluster = range.location + j;
 
-            info->mask = advance;
+            info->mask = advance.width;
             info->var1.u32 = 0;
             info->var2.u32 = 0;
 
@@ -726,10 +731,13 @@ _hb_coretext_shape (hb_shape_plan_t    *shape_plan,
 
 #undef ALLOCATE_ARRAY
 
+    CGAffineTransform textMatrix = CTRunGetTextMatrix (run);
+
     double run_width = CTRunGetTypographicBounds (run, range_all, NULL, NULL, NULL);
 
     for (unsigned int j = 0; j < num_glyphs; j++) {
-      double advance = (j + 1 < num_glyphs ? positions[j + 1].x : positions[0].x + run_width) - positions[j].x;
+      CGSize advance = CGSizeMake((j + 1 < num_glyphs ? positions[j + 1].x : positions[0].x + run_width) - positions[j].x, 0);
+      advance = CGSizeApplyAffineTransform (advance, textMatrix);
 
       hb_glyph_info_t *info = &buffer->info[buffer->len];
 
@@ -737,7 +745,7 @@ _hb_coretext_shape (hb_shape_plan_t    *shape_plan,
       info->cluster = string_indices[j];
 
       /* Currently, we do all x-positioning by setting the advance, we never use x-offset. */
-      info->mask = advance;
+      info->mask = advance.width;
       info->var1.u32 = 0;
       info->var2.u32 = positions[j].y;
 
-- 
1.8.3.msysgit.0

