support vertical text and typeface-by-name on Leopard

M    src/ports/SkFontHost_mac_coretext.cpp
M    gyp/gmslides.gypi
A    gm/verttext2.cpp



git-svn-id: http://skia.googlecode.com/svn/trunk@2920 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gm/verttext2.cpp b/gm/verttext2.cpp
new file mode 100644
index 0000000..3bfb471
--- /dev/null
+++ b/gm/verttext2.cpp
@@ -0,0 +1,92 @@
+
+/*
+ * Copyright 2011 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+
+/* Tests text vertical text rendering with different fonts and centering.
+ */
+
+#include "gm.h"
+#include "SkCanvas.h"
+#include "SkTypeface.h"
+
+namespace skiagm {
+
+class VertText2GM : public GM {
+public:
+    VertText2GM() {
+        const int pointSize = 24;
+        textHeight = SkIntToScalar(pointSize);
+        prop = SkTypeface::CreateFromName("Helvetica", SkTypeface::kNormal);
+        mono = SkTypeface::CreateFromName("Courier New", SkTypeface::kNormal);
+    }
+
+protected:
+
+    SkString onShortName() {
+        return SkString("verttext2");
+    }
+
+    SkISize onISize() { return make_isize(640, 480); }
+
+    virtual void onDraw(SkCanvas* canvas) {
+        
+        for (int i = 0; i < 3; ++i) {
+            SkPaint paint;
+            paint.setColor(SK_ColorRED);
+            paint.setAntiAlias(true);
+            y = textHeight;
+            canvas->drawLine(0, SkIntToScalar(10),
+                    SkIntToScalar(110), SkIntToScalar(10), paint);
+            canvas->drawLine(0, SkIntToScalar(240),
+                    SkIntToScalar(110), SkIntToScalar(240), paint);
+            canvas->drawLine(0, SkIntToScalar(470),
+                    SkIntToScalar(110), SkIntToScalar(470), paint);
+            drawText(canvas, SkString("Proportional / Top Aligned"),
+                     prop,  SkPaint::kLeft_Align);
+            drawText(canvas, SkString("<   Proportional / Centered   >"),
+                     prop,  SkPaint::kCenter_Align);
+            drawText(canvas, SkString("Monospaced / Top Aligned"),
+                     mono, SkPaint::kLeft_Align);
+            drawText(canvas, SkString("<    Monospaced / Centered    >"),
+                     mono, SkPaint::kCenter_Align);
+            canvas->rotate(SkIntToScalar(-15));
+            canvas->translate(textHeight * 4, SkIntToScalar(50));
+            if (i > 0) {
+                canvas->translate(0, SkIntToScalar(50));
+            }
+        }
+    }
+
+    void drawText(SkCanvas* canvas, const SkString& string,
+                  SkTypeface* family, SkPaint::Align alignment) {
+        SkPaint paint;
+        paint.setColor(SK_ColorBLACK);
+        paint.setAntiAlias(true);
+        paint.setVerticalText(true);
+        paint.setTextAlign(alignment);
+        paint.setTypeface(family);
+        paint.setTextSize(textHeight);
+
+        canvas->drawText(string.c_str(), string.size(), y, 
+                alignment == SkPaint::kLeft_Align ? 10 : 240, paint);
+        y += textHeight;
+    }
+
+private:
+    typedef GM INHERITED;
+    SkScalar y, textHeight;
+    SkTypeface* prop;
+    SkTypeface* mono;
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
+static GM* MyFactory(void*) { return new VertText2GM; }
+static GMRegistry reg(MyFactory);
+
+}
diff --git a/gyp/gmslides.gypi b/gyp/gmslides.gypi
index 44bc7de..b85a701 100644
--- a/gyp/gmslides.gypi
+++ b/gyp/gmslides.gypi
@@ -43,6 +43,7 @@
     '../gm/tilemodes.cpp',
     '../gm/tinybitmap.cpp',
     '../gm/verttext.cpp',
+    '../gm/verttext2.cpp',
     '../gm/xfermodes.cpp',
   ],
 }
diff --git a/src/ports/SkFontHost_mac_coretext.cpp b/src/ports/SkFontHost_mac_coretext.cpp
index 4c322bc..ec9e68a 100644
--- a/src/ports/SkFontHost_mac_coretext.cpp
+++ b/src/ports/SkFontHost_mac_coretext.cpp
@@ -423,7 +423,15 @@
 
         ctFontDesc = CTFontDescriptorCreateWithAttributes(cfAttributes);
         if (ctFontDesc != NULL) {
-            ctFont = CTFontCreateWithFontDescriptor(ctFontDesc, 0, NULL);
+            if (isLeopard()) {
+                // CTFontCreateWithFontDescriptor on Leopard ignores the name
+                CTFontRef ctNamed = CTFontCreateWithName(cfFontName, 1, NULL);
+                ctFont = CTFontCreateCopyWithAttributes(ctNamed, 1, NULL,
+                                                        ctFontDesc);
+                CFSafeRelease(ctNamed);
+            } else {
+                ctFont = CTFontCreateWithFontDescriptor(ctFontDesc, 0, NULL);
+            }
         }
     }
 
@@ -792,9 +800,11 @@
                            SkFloatToScalar(vertOffset.height)};
     SkPoint floatOffset;
     fVerticalMatrix.mapPoints(&floatOffset, &trans, 1);
-    if (isLion()) {
-    // Lion changed functionality from Snow Leopard, though it's not clear why
-    // this is required here; it was found through trial and error.
+    if (!isSnowLeopard()) {
+    // SnowLeopard fails to apply the font's matrix to the vertical metrics,
+    // but Lion and Leopard do. The unit matrix describes the font's matrix at
+    // point size 1. There may be some way to avoid mapping here by setting up
+    // fVerticalMatrix differently, but this works for now.
         fUnitMatrix.mapPoints(&floatOffset, 1);
     }
     offset->fX = SkScalarRound(floatOffset.fX);
@@ -938,15 +948,14 @@
     cgGlyph = (CGGlyph) glyph->getGlyphID(fBaseGlyphCount);
 
     if (fVertical) {
-        if (isLion()) {
-        // Lion correctly supports returning the bounding box for vertical text.
+        if (!isSnowLeopard()) {
+        // Lion and Leopard respect the vertical font metrics.
             CTFontGetBoundingRectsForGlyphs(fCTVerticalFont,
                                             kCTFontVerticalOrientation, 
                                             &cgGlyph, &theBounds,  1);
         } else {
-        /* Snow Leopard and earlier respect the vertical font metrics for
-           advances, but not bounds, so use the default box and adjust it below.
-         */
+        // Snow Leopard and earlier respect the vertical font metrics for
+        // advances, but not bounds, so use the default box and adjust it below.
             CTFontGetBoundingRectsForGlyphs(fCTFont, kCTFontDefaultOrientation,
                                             &cgGlyph, &theBounds,  1);
         }
@@ -982,10 +991,12 @@
         return;
     }
     
-    if (isLeopard()) {
+    if (isLeopard() && !fVertical) {
         // Leopard does not consider the matrix skew in its bounds.
         // Run the bounding rectangle through the skew matrix to determine
-        // the true bounds.
+        // the true bounds. However, this doesn't work if the font is vertical.
+        // FIXME (Leopard): If the font has synthetic italic (e.g., matrix skew)
+        // and the font is vertical, the bounds need to be recomputed.
         SkRect glyphBounds = SkRect::MakeXYWH(
                 theBounds.origin.x, theBounds.origin.y,
                 theBounds.size.width, theBounds.size.height);
@@ -1026,7 +1037,9 @@
     glyph->fTop      = -sk_float_round2int(CGRectGetMaxY_inline(theBounds));
     glyph->fLeft     =  sk_float_round2int(CGRectGetMinX_inline(theBounds));
     SkIPoint offset;
-    if (fVertical && (!isLion() || lionAdjustedMetrics)) {
+    if (fVertical && (isSnowLeopard() || lionAdjustedMetrics)) {
+    // SnowLeopard doesn't respect vertical metrics, so compute them manually.
+    // Also compute them for Lion when the metrics were computed by hand.
         getVerticalOffset(cgGlyph, &offset);
         glyph->fLeft += offset.fX;
         glyph->fTop += offset.fY;