add SkPaint::getPosTextPath(), with gm to test it
authorreed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 20 Jul 2012 11:20:32 +0000 (11:20 +0000)
committerreed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 20 Jul 2012 11:20:32 +0000 (11:20 +0000)
Review URL: https://codereview.appspot.com/6427055

git-svn-id: http://skia.googlecode.com/svn/trunk@4684 2bbb7eff-a529-9590-31e7-b0007b416f81

gm/getpostextpath.cpp [new file with mode: 0644]
gyp/gmslides.gypi
include/core/SkPaint.h
src/core/SkPaint.cpp

diff --git a/gm/getpostextpath.cpp b/gm/getpostextpath.cpp
new file mode 100644 (file)
index 0000000..6ef37ca
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "gm.h"
+#include "SkCanvas.h"
+#include "SkPaint.h"
+#include "SkRandom.h"
+
+class GetPosTextPathGM : public skiagm::GM {
+public:
+    GetPosTextPathGM() {}
+
+protected:
+    SkString onShortName() {
+        return SkString("getpostextpath");
+    }
+
+    SkISize onISize() { return skiagm::make_isize(480, 780); }
+
+    virtual void onDraw(SkCanvas* canvas) {
+        const char* text = "Hamburgefons";
+        size_t len = strlen(text);
+
+        SkPaint paint;
+        paint.setAntiAlias(true);
+        paint.setTextSize(SkIntToScalar(48));
+        
+        SkAutoTArray<SkPoint>  pos(len);
+        SkAutoTArray<SkScalar> widths(len);
+        paint.getTextWidths(text, len, &widths[0]);
+        
+        SkRandom rand;
+        SkScalar x = SkIntToScalar(20);
+        SkScalar y = SkIntToScalar(100);
+        for (size_t i = 0; i < len; ++i) {
+            pos[i].set(x, y + rand.nextSScalar1() * 24);
+            x += widths[i];
+        }
+        
+        canvas->drawPosText(text, len, &pos[0], paint);
+        
+        SkPath path;
+        paint.setColor(SK_ColorRED);
+        paint.setStyle(SkPaint::kStroke_Style);
+        paint.getPosTextPath(text, len, &pos[0], &path);
+        canvas->drawPath(path, paint);
+    }
+};
+
+//////////////////////////////////////////////////////////////////////////////
+
+static skiagm::GM* F(void*) { return new GetPosTextPathGM; }
+
+static skiagm::GMRegistry gR(F);
+
index fa4d2bb..6a032a1 100644 (file)
@@ -27,6 +27,7 @@
     '../gm/filltypespersp.cpp',
     '../gm/fontscaler.cpp',
     '../gm/gammatext.cpp',
+    '../gm/getpostextpath.cpp',
     '../gm/giantbitmap.cpp',
     '../gm/gradients.cpp',
     '../gm/gradtext.cpp',
index 684bc45..4defe8b 100644 (file)
@@ -825,6 +825,9 @@ public:
     void getTextPath(const void* text, size_t length, SkScalar x, SkScalar y,
                      SkPath* path) const;
 
+    void getPosTextPath(const void* text, size_t length, 
+                        const SkPoint pos[], SkPath* path) const;
+
 #ifdef SK_BUILD_FOR_ANDROID
     const SkGlyph& getUnicharMetrics(SkUnichar);
     const SkGlyph& getGlyphMetrics(uint16_t);
index 19abb4b..f240737 100644 (file)
@@ -1358,6 +1358,33 @@ void SkPaint::getTextPath(const void* textData, size_t length,
     }
 }
 
+void SkPaint::getPosTextPath(const void* textData, size_t length,
+                             const SkPoint pos[], SkPath* path) const {
+    SkASSERT(length == 0 || textData != NULL);
+
+    const char* text = (const char*)textData;
+    if (text == NULL || length == 0 || path == NULL) {
+        return;
+    }
+
+    SkTextToPathIter    iter(text, length, *this, false);
+    SkMatrix            matrix;
+    SkPoint             prevPos;
+    prevPos.set(0, 0);
+
+    matrix.setScale(iter.getPathScale(), iter.getPathScale());
+    path->reset();
+
+    unsigned int    i = 0;
+    const SkPath*   iterPath;
+    while ((iterPath = iter.next(NULL)) != NULL) {
+        matrix.postTranslate(pos[i].fX - prevPos.fX, pos[i].fY - prevPos.fY);
+        path->addPath(*iterPath, matrix);
+        prevPos = pos[i];
+        i++;
+    }
+}
+
 static void add_flattenable(SkDescriptor* desc, uint32_t tag,
                             SkFlattenableWriteBuffer* buffer) {
     buffer->flatten(desc->addEntry(tag, buffer->size(), NULL));