SampleText.cpp revision 4d5c26de0a24f86c37c1da8b0e30d11a550ea67b
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) {} 3430711b764be6bbb58caa30a0ac5d1474c894efe7reed@google.com virtual uint8_t computeValue(uint8_t* const* srcRows) const { 358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int c = srcRows[1][1]; 368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int min = 255, max = 0; 378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com for (int i = 0; i < 3; i++) 388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com for (int j = 0; j < 3; j++) 398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (i != 1 || j != 1) 408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int v = srcRows[i][j]; 428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (max < v) 438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com max = v; 448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (min > v) 458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com min = v; 468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (c > max) c = max; 488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // if (c < min) c = min; 498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return c; 508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 51ba28d03e94dc221d6a803bf2a84a420b9159255cdjsollen@google.com SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(ReduceNoise) 52ba28d03e94dc221d6a803bf2a84a420b9159255cdjsollen@google.com 538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ReduceNoise(SkFlattenableReadBuffer& rb) : SkKernel33ProcMaskFilter(rb) {} 558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass Darken : public SkKernel33ProcMaskFilter { 588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com Darken(int percent256) : SkKernel33ProcMaskFilter(percent256) {} 6030711b764be6bbb58caa30a0ac5d1474c894efe7reed@google.com virtual uint8_t computeValue(uint8_t* const* srcRows) const { 618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int c = srcRows[1][1]; 628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com float f = c / 255.f; 6382065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 649e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com if (c >= 0) { 658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com f = sqrtf(f); 669e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com } else { 678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com f *= f; 688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(f >= 0 && f <= 1); 708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return (int)(f * 255); 718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 72ba28d03e94dc221d6a803bf2a84a420b9159255cdjsollen@google.com SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Darken) 73ba28d03e94dc221d6a803bf2a84a420b9159255cdjsollen@google.com 748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com Darken(SkFlattenableReadBuffer& rb) : SkKernel33ProcMaskFilter(rb) {} 768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkMaskFilter* makemf() { return new Darken(0x30); } 798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 809e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.comstatic void test_breakText() { 818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkPaint paint; 828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com const char* text = "sdfkljAKLDFJKEWkldfjlk#$%&sdfs.dsj"; 838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com size_t length = strlen(text); 848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar width = paint.measureText(text, length); 8582065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar mm = 0; 878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar nn = 0; 889e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com for (SkScalar w = 0; w <= width; w += SK_Scalar1) { 898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar m; 908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com size_t n = paint.breakText(text, length, w, &m, 918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkPaint::kBackward_TextBufferDirection); 9282065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(n <= length); 948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(m <= width); 9582065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 969e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com if (n == 0) { 978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(m == 0); 989e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com } else { 998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // now assert that we're monotonic 1009e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com if (n == nn) { 1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(m == mm); 1029e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com } else { 1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(n > nn); 1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(m > mm); 1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 107ffe39bd3b66eb5090684959e7f2409346ab72d93tomhudson@google.com nn = SkIntToScalar((unsigned int)n); 1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com mm = m; 1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 11164cc579efa7e416c7298ed159d76b074b283c0f9senorblanco@chromium.org SkDEBUGCODE(size_t length2 =) paint.breakText(text, length, width, &mm); 112261b8e2ca1cf22303ad95267f0bdc6e87e1bbe70reed@google.com SkASSERT(length2 == length); 1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(mm == width); 1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkRandom gRand; 1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkPowerMode : public SkXfermode { 1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkPowerMode(SkScalar exponent) { this->init(exponent); } 1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1229e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com virtual void xfer16(uint16_t dst[], const SkPMColor src[], int count, 1233af2b8def636eb36b69b1b334013320646dc7465mike@reedtribe.org const SkAlpha aa[]) const SK_OVERRIDE; 1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com typedef SkFlattenable* (*Factory)(SkFlattenableReadBuffer&); 12682065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 127ba28d03e94dc221d6a803bf2a84a420b9159255cdjsollen@google.com SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPowerMode) 12882065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 1298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 1308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar fExp; // user's value 1318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com uint8_t fTable[256]; // cache 1328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void init(SkScalar exponent); 13454924243c1b65b3ee6d8fa064b50a9b1bb2a19a5djsollen@google.com SkPowerMode(SkFlattenableReadBuffer& b) : INHERITED(b) { 1358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // read the exponent 136c73dd5c6880739f26216f198c757028fd28df1a4djsollen@google.com this->init(SkFixedToScalar(b.readFixed())); 1378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 13854924243c1b65b3ee6d8fa064b50a9b1bb2a19a5djsollen@google.com virtual void flatten(SkFlattenableWriteBuffer& b) const SK_OVERRIDE { 13954924243c1b65b3ee6d8fa064b50a9b1bb2a19a5djsollen@google.com this->INHERITED::flatten(b); 140c73dd5c6880739f26216f198c757028fd28df1a4djsollen@google.com b.writeFixed(SkScalarToFixed(fExp)); 14154924243c1b65b3ee6d8fa064b50a9b1bb2a19a5djsollen@google.com } 14282065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 1438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com typedef SkXfermode INHERITED; 1448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 1458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1469e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.comvoid SkPowerMode::init(SkScalar e) { 1478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fExp = e; 1488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com float ee = SkScalarToFloat(e); 14982065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 1508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com printf("------ %g\n", ee); 1519e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com for (int i = 0; i < 256; i++) { 1528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com float x = i / 255.f; 1538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // printf(" %d %g", i, x); 1548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com x = powf(x, ee); 1558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // printf(" %g", x); 1568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int xx = SkScalarRound(SkFloatToScalar(x * 255)); 1578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // printf(" %d\n", xx); 1588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fTable[i] = SkToU8(xx); 1598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 1608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 1618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1629e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.comvoid SkPowerMode::xfer16(uint16_t dst[], const SkPMColor src[], int count, 1633af2b8def636eb36b69b1b334013320646dc7465mike@reedtribe.org const SkAlpha aa[]) const { 1649e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com for (int i = 0; i < count; i++) { 1658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkPMColor c = src[i]; 1668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int r = SkGetPackedR32(c); 1678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int g = SkGetPackedG32(c); 1688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int b = SkGetPackedB32(c); 1698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com r = fTable[r]; 1708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com g = fTable[g]; 1718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com b = fTable[b]; 1728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com dst[i] = SkPack888ToRGB16(r, g, b); 1738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 1748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 1758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic const struct { 1778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com const char* fName; 1788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com uint32_t fFlags; 1798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com bool fFlushCache; 1808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} gHints[] = { 1818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { "Linear", SkPaint::kLinearText_Flag, false }, 1828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { "Normal", 0, true }, 1838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { "Subpixel", SkPaint::kSubpixelText_Flag, true } 1848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 1858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void DrawTheText(SkCanvas* canvas, const char text[], size_t length, 1878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar x, SkScalar y, const SkPaint& paint, 1889e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com SkScalar clickX, SkMaskFilter* mf) { 1898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkPaint p(paint); 1908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#if 0 1928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com canvas->drawText(text, length, x, y, paint); 1938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else 1948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 1958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkPoint pts[1000]; 1968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar xpos = x; 1978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(length <= SK_ARRAY_COUNT(pts)); 1989e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com for (size_t i = 0; i < length; i++) { 1998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com pts[i].set(xpos, y), xpos += paint.getTextSize(); 2009e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com } 2018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com canvas->drawPosText(text, length, pts, paint); 2028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 2038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 2048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com p.setSubpixelText(true); 2068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com x += SkIntToScalar(180); 2078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com canvas->drawText(text, length, x, y, p); 2088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG 2109e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com if (true) { 2118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // p.setMaskFilter(mf); 2128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com p.setSubpixelText(false); 2138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com p.setLinearText(true); 2148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com x += SkIntToScalar(180); 2158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com canvas->drawText(text, length, x, y, p); 2168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 2178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 2188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2209e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.comclass TextSpeedView : public SampleView { 2218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 222ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com TextSpeedView() { 2238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fMF = makemf(); 2248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fHints = 0; 2268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fClickX = 0; 2278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 22882065d667f64e232bcde2ad849756a6096fcbe6freed@google.com test_breakText(); 2298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 23082065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 2319e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com virtual ~TextSpeedView() { 23282065d667f64e232bcde2ad849756a6096fcbe6freed@google.com SkSafeUnref(fMF); 2338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 2348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprotected: 2368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // overrides from SkEventSink 2379e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com virtual bool onQuery(SkEvent* evt) { 2389e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com if (SampleCode::TitleQ(*evt)) { 2398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SampleCode::TitleR(evt, "Text"); 2408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return true; 2418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 2428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return this->INHERITED::onQuery(evt); 2438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 24482065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 2459e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com static void make_textstrip(SkBitmap* bm) { 2468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com bm->setConfig(SkBitmap::kRGB_565_Config, 200, 18); 2478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com bm->allocPixels(); 2488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com bm->eraseColor(SK_ColorWHITE); 24982065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 2508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkCanvas canvas(*bm); 2518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkPaint paint; 2528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com const char* s = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit"; 25382065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 2548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com paint.setFlags(paint.getFlags() | SkPaint::kAntiAlias_Flag 2558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com | SkPaint::kDevKernText_Flag); 2568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com paint.setTextSize(SkIntToScalar(14)); 2578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com canvas.drawText(s, strlen(s), SkIntToScalar(8), SkIntToScalar(14), paint); 2588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 25982065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 2609e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com static void fill_pts(SkPoint pts[], size_t n, SkRandom* rand) { 2618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com for (size_t i = 0; i < n; i++) 2628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com pts[i].set(rand->nextUScalar1() * 640, rand->nextUScalar1() * 480); 2638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 26482065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 2659e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com virtual void onDrawContent(SkCanvas* canvas) { 2668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoCanvasRestore restore(canvas, false); 2678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 2688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkRect r; 2698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com r.set(0, 0, SkIntToScalar(1000), SkIntToScalar(20)); 2708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // canvas->saveLayer(&r, NULL, SkCanvas::kHasAlphaLayer_SaveFlag); 2718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 2728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkPaint paint; 2748a1c16ff38322f0210116fa7293eb8817c7e477ereed@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 }; 2758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int index = fHints % SK_ARRAY_COUNT(gHints); 2768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com index = 1; 2778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// const char* style = gHints[index].fName; 27882065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 2798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// canvas->translate(0, SkIntToScalar(50)); 2808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // canvas->drawText(style, strlen(style), SkIntToScalar(20), SkIntToScalar(20), paint); 2828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 28304d86c6a6b8eb8631752b3680f1292fa0a2c7119reed@android.com SkSafeUnref(paint.setTypeface(SkTypeface::CreateFromFile("/skimages/samplefont.ttf"))); 2848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com paint.setAntiAlias(true); 2858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com paint.setFlags(paint.getFlags() | gHints[index].fFlags); 28682065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 2878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkRect clip; 2888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com clip.set(SkIntToScalar(25), SkIntToScalar(34), SkIntToScalar(88), SkIntToScalar(155)); 28982065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 2908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com const char* text = "Hamburgefons"; 2918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com size_t length = strlen(text); 2928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2932f3dc9dc4c970bd066be329a842a791d91f524e2reed@google.com SkScalar y = SkIntToScalar(0); 2942f3dc9dc4c970bd066be329a842a791d91f524e2reed@google.com for (int i = 9; i <= 24; i++) { 2952f3dc9dc4c970bd066be329a842a791d91f524e2reed@google.com paint.setTextSize(SkIntToScalar(i) /*+ (gRand.nextU() & 0xFFFF)*/); 2969e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com for (SkScalar dx = 0; dx <= SkIntToScalar(3)/4; 2979e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com dx += SkIntToScalar(1) /* /4 */) { 2982f3dc9dc4c970bd066be329a842a791d91f524e2reed@google.com y += paint.getFontSpacing(); 2999e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com DrawTheText(canvas, text, length, SkIntToScalar(20) + dx, y, 3009e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com paint, fClickX, fMF); 3018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 3028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 3032f3dc9dc4c970bd066be329a842a791d91f524e2reed@google.com if (gHints[index].fFlushCache) { 3042f3dc9dc4c970bd066be329a842a791d91f524e2reed@google.com// SkGraphics::SetFontCacheUsed(0); 3058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 3068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 30782065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 3084d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, 3094d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com unsigned modi) SK_OVERRIDE { 3108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fClickX = x; 3118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com this->inval(NULL); 3124d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com return this->INHERITED::onFindClickHandler(x, y, modi); 3138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 31482065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 3159e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com virtual bool onClick(Click* click) { 3168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return this->INHERITED::onClick(click); 3178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 31882065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 3198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 3208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int fHints; 3218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar fClickX; 3228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkMaskFilter* fMF; 3238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3249e39bb3603ee33e7753ee66eb76ebcfc412396f1reed@google.com typedef SampleView INHERITED; 3258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 3268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com////////////////////////////////////////////////////////////////////////////// 3288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkView* MyFactory() { return new TextSpeedView; } 3308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkViewRegister reg(MyFactory); 3318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 332