SampleText.cpp revision c73dd5c6880739f26216f198c757028fd28df1a4
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2011 Google Inc.
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
7ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SampleCode.h"
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkView.h"
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkCanvas.h"
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "Sk64.h"
12c73dd5c6880739f26216f198c757028fd28df1a4djsollen@google.com#include "SkFlattenableBuffers.h"
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkGradientShader.h"
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkGraphics.h"
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkImageDecoder.h"
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkKernel33MaskFilter.h"
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPath.h"
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkRandom.h"
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkRegion.h"
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkShader.h"
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkUtils.h"
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkColorPriv.h"
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkColorFilter.h"
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkTime.h"
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkTypeface.h"
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkXfermode.h"
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkStream.h"
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkXMLParser.h"
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass ReduceNoise : public SkKernel33ProcMaskFilter {
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ReduceNoise(int percent256) : SkKernel33ProcMaskFilter(percent256) {}
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual uint8_t computeValue(uint8_t* const* srcRows)
358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int c = srcRows[1][1];
378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int min = 255, max = 0;
388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        for (int i = 0; i < 3; i++)
398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            for (int j = 0; j < 3; j++)
408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                if (i != 1 || j != 1)
418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                {
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    int v = srcRows[i][j];
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    if (max < v)
448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        max = v;
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                    if  (min > v)
468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        min = v;
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                }
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (c > max) c = max;
498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    //    if (c < min) c = min;
508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return c;
518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
52ba28d03e94dc221d6a803bf2a84a420b9159255cdjsollen@google.com    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(ReduceNoise)
53ba28d03e94dc221d6a803bf2a84a420b9159255cdjsollen@google.com
548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ReduceNoise(SkFlattenableReadBuffer& rb) : SkKernel33ProcMaskFilter(rb) {}
568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass Darken : public SkKernel33ProcMaskFilter {
598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Darken(int percent256) : SkKernel33ProcMaskFilter(percent256) {}
618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual uint8_t computeValue(uint8_t* const* srcRows)
628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int c = srcRows[1][1];
648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        float f = c / 255.f;
6582065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
669e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com        if (c >= 0) {
678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            f = sqrtf(f);
689e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com        } else {
698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            f *= f;
708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkASSERT(f >= 0 && f <= 1);
728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return (int)(f * 255);
738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
74ba28d03e94dc221d6a803bf2a84a420b9159255cdjsollen@google.com    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Darken)
75ba28d03e94dc221d6a803bf2a84a420b9159255cdjsollen@google.com
768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Darken(SkFlattenableReadBuffer& rb) : SkKernel33ProcMaskFilter(rb) {}
788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkMaskFilter* makemf() { return new Darken(0x30); }
818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
829e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.comstatic void test_breakText() {
838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPaint paint;
848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const char* text = "sdfkljAKLDFJKEWkldfjlk#$%&sdfs.dsj";
858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    size_t length = strlen(text);
868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar width = paint.measureText(text, length);
8782065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar mm = 0;
898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar nn = 0;
909e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com    for (SkScalar w = 0; w <= width; w += SK_Scalar1) {
918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar m;
928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        size_t n = paint.breakText(text, length, w, &m,
938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                    SkPaint::kBackward_TextBufferDirection);
9482065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkASSERT(n <= length);
968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkASSERT(m <= width);
9782065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
989e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com        if (n == 0) {
998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkASSERT(m == 0);
1009e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com        } else {
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            // now assert that we're monotonic
1029e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com            if (n == nn) {
1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                SkASSERT(m == mm);
1049e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com            } else {
1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                SkASSERT(n > nn);
1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                SkASSERT(m > mm);
1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
109ffe39bd3b66eb5090684959e7f2409346ab72d93tomhudson@google.com        nn = SkIntToScalar((unsigned int)n);
1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        mm = m;
1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
11364cc579efa7e416c7298ed159d76b074b283c0f9senorblanco@chromium.org    SkDEBUGCODE(size_t length2 =) paint.breakText(text, length, width, &mm);
114261b8e2ca1cf22303ad95267f0bdc6e87e1bbe70reed@google.com    SkASSERT(length2 == length);
1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(mm == width);
1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkRandom gRand;
1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkPowerMode : public SkXfermode {
1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPowerMode(SkScalar exponent) { this->init(exponent); }
1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1249e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com    virtual void xfer16(uint16_t dst[], const SkPMColor src[], int count,
1259e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com                        const SkAlpha aa[]);
1268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef SkFlattenable* (*Factory)(SkFlattenableReadBuffer&);
12882065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
129ba28d03e94dc221d6a803bf2a84a420b9159255cdjsollen@google.com    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPowerMode)
13082065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
1318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
1328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar fExp;          // user's value
1338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint8_t fTable[256];    // cache
1348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void init(SkScalar exponent);
13654924243c1b65b3ee6d8fa064b50a9b1bb2a19a5djsollen@google.com    SkPowerMode(SkFlattenableReadBuffer& b) : INHERITED(b) {
1378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // read the exponent
138c73dd5c6880739f26216f198c757028fd28df1a4djsollen@google.com        this->init(SkFixedToScalar(b.readFixed()));
1398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
14054924243c1b65b3ee6d8fa064b50a9b1bb2a19a5djsollen@google.com    virtual void flatten(SkFlattenableWriteBuffer& b) const SK_OVERRIDE {
14154924243c1b65b3ee6d8fa064b50a9b1bb2a19a5djsollen@google.com        this->INHERITED::flatten(b);
142c73dd5c6880739f26216f198c757028fd28df1a4djsollen@google.com        b.writeFixed(SkScalarToFixed(fExp));
14354924243c1b65b3ee6d8fa064b50a9b1bb2a19a5djsollen@google.com    }
14482065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
1458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef SkXfermode INHERITED;
1468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
1478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1489e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.comvoid SkPowerMode::init(SkScalar e) {
1498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fExp = e;
1508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    float ee = SkScalarToFloat(e);
15182065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
1528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    printf("------ %g\n", ee);
1539e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com    for (int i = 0; i < 256; i++) {
1548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        float x = i / 255.f;
1558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     //   printf(" %d %g", i, x);
1568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        x = powf(x, ee);
1578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     //   printf(" %g", x);
1588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int xx = SkScalarRound(SkFloatToScalar(x * 255));
1598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     //   printf(" %d\n", xx);
1608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fTable[i] = SkToU8(xx);
1618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1649e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.comvoid SkPowerMode::xfer16(uint16_t dst[], const SkPMColor src[], int count,
1659e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com                         const SkAlpha aa[]) {
1669e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com    for (int i = 0; i < count; i++) {
1678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPMColor c = src[i];
1688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int r = SkGetPackedR32(c);
1698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int g = SkGetPackedG32(c);
1708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int b = SkGetPackedB32(c);
1718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        r = fTable[r];
1728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        g = fTable[g];
1738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        b = fTable[b];
1748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        dst[i] = SkPack888ToRGB16(r, g, b);
1758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic const struct {
1798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    const char* fName;
1808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    uint32_t    fFlags;
1818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    bool        fFlushCache;
1828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} gHints[] = {
1838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    { "Linear", SkPaint::kLinearText_Flag,     false },
1848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    { "Normal",   0,                           true },
1858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    { "Subpixel", SkPaint::kSubpixelText_Flag, true }
1868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
1878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void DrawTheText(SkCanvas* canvas, const char text[], size_t length,
1898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                        SkScalar x, SkScalar y, const SkPaint& paint,
1909e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com                        SkScalar clickX, SkMaskFilter* mf) {
1918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPaint p(paint);
1928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#if 0
1948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    canvas->drawText(text, length, x, y, paint);
1958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
1968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
1978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPoint pts[1000];
1988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar xpos = x;
1998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkASSERT(length <= SK_ARRAY_COUNT(pts));
2009e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com        for (size_t i = 0; i < length; i++) {
2018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            pts[i].set(xpos, y), xpos += paint.getTextSize();
2029e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com        }
2038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        canvas->drawPosText(text, length, pts, paint);
2048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
2068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    p.setSubpixelText(true);
2088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    x += SkIntToScalar(180);
2098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    canvas->drawText(text, length, x, y, p);
2108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG
2129e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com    if (true) {
2138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    //    p.setMaskFilter(mf);
2148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        p.setSubpixelText(false);
2158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        p.setLinearText(true);
2168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        x += SkIntToScalar(180);
2178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        canvas->drawText(text, length, x, y, p);
2188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
2208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2229e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.comclass TextSpeedView : public SampleView {
2238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
2249e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com	TextSpeedView() {
2258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fMF = makemf();
2268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fHints = 0;
2288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fClickX = 0;
2298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
23082065d667f64e232bcde2ad849756a6096fcbe6freed@google.com        test_breakText();
2318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
23282065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
2339e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com    virtual ~TextSpeedView() {
23482065d667f64e232bcde2ad849756a6096fcbe6freed@google.com        SkSafeUnref(fMF);
2358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprotected:
2388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // overrides from SkEventSink
2399e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com    virtual bool onQuery(SkEvent* evt) {
2409e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com        if (SampleCode::TitleQ(*evt)) {
2418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SampleCode::TitleR(evt, "Text");
2428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            return true;
2438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
2448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return this->INHERITED::onQuery(evt);
2458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
24682065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
2479e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com    static void make_textstrip(SkBitmap* bm) {
2488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        bm->setConfig(SkBitmap::kRGB_565_Config, 200, 18);
2498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        bm->allocPixels();
2508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        bm->eraseColor(SK_ColorWHITE);
25182065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
2528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkCanvas    canvas(*bm);
2538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPaint     paint;
2548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const char* s = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit";
25582065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
2568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        paint.setFlags(paint.getFlags() | SkPaint::kAntiAlias_Flag
2578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                        | SkPaint::kDevKernText_Flag);
2588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        paint.setTextSize(SkIntToScalar(14));
2598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        canvas.drawText(s, strlen(s), SkIntToScalar(8), SkIntToScalar(14), paint);
2608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
26182065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
2629e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com    static void fill_pts(SkPoint pts[], size_t n, SkRandom* rand) {
2638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        for (size_t i = 0; i < n; i++)
2648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            pts[i].set(rand->nextUScalar1() * 640, rand->nextUScalar1() * 480);
2658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
26682065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
2679e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com    virtual void onDrawContent(SkCanvas* canvas) {
2688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkAutoCanvasRestore restore(canvas, false);
2698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        {
2708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkRect r;
2718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            r.set(0, 0, SkIntToScalar(1000), SkIntToScalar(20));
2728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com       //     canvas->saveLayer(&r, NULL, SkCanvas::kHasAlphaLayer_SaveFlag);
2738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
2748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPaint paint;
2768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//        const uint16_t glyphs[] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 };
2778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int         index = fHints % SK_ARRAY_COUNT(gHints);
2788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        index = 1;
2798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//        const char* style = gHints[index].fName;
28082065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
2818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//        canvas->translate(0, SkIntToScalar(50));
2828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com  //      canvas->drawText(style, strlen(style), SkIntToScalar(20), SkIntToScalar(20), paint);
2848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
28504d86c6a6b8eb8631752b3680f1292fa0a2c7119reed@android.com        SkSafeUnref(paint.setTypeface(SkTypeface::CreateFromFile("/skimages/samplefont.ttf")));
2868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        paint.setAntiAlias(true);
2878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        paint.setFlags(paint.getFlags() | gHints[index].fFlags);
28882065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
2898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkRect clip;
2908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        clip.set(SkIntToScalar(25), SkIntToScalar(34), SkIntToScalar(88), SkIntToScalar(155));
29182065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
2928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const char* text = "Hamburgefons";
2938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        size_t length = strlen(text);
2948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2952f3dc9dc4c970bd066be329a842a791d91f524e2reed@google.com        SkScalar y = SkIntToScalar(0);
2962f3dc9dc4c970bd066be329a842a791d91f524e2reed@google.com        for (int i = 9; i <= 24; i++) {
2972f3dc9dc4c970bd066be329a842a791d91f524e2reed@google.com            paint.setTextSize(SkIntToScalar(i) /*+ (gRand.nextU() & 0xFFFF)*/);
2989e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com            for (SkScalar dx = 0; dx <= SkIntToScalar(3)/4;
2999e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com                                            dx += SkIntToScalar(1) /* /4 */) {
3002f3dc9dc4c970bd066be329a842a791d91f524e2reed@google.com                y += paint.getFontSpacing();
3019e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com                DrawTheText(canvas, text, length, SkIntToScalar(20) + dx, y,
3029e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com                            paint, fClickX, fMF);
3038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
3048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
3052f3dc9dc4c970bd066be329a842a791d91f524e2reed@google.com        if (gHints[index].fFlushCache) {
3062f3dc9dc4c970bd066be329a842a791d91f524e2reed@google.com//                SkGraphics::SetFontCacheUsed(0);
3078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
3088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
30982065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
3109e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com    virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
3118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fClickX = x;
3128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->inval(NULL);
3138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return this->INHERITED::onFindClickHandler(x, y);
3148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
31582065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
3169e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com    virtual bool onClick(Click* click) {
3178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return this->INHERITED::onClick(click);
3188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
31982065d667f64e232bcde2ad849756a6096fcbe6freed@google.com
3208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
3218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int fHints;
3228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkScalar fClickX;
3238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkMaskFilter* fMF;
3248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3259e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com    typedef SampleView INHERITED;
3268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
3278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////
3298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkView* MyFactory() { return new TextSpeedView; }
3318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkViewRegister reg(MyFactory);
3328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
333