Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Side by Side Diff: samplecode/SampleText.cpp

Issue 473633002: SkTextBlob (Closed) Base URL: https://chromium.googlesource.com/skia.git@master
Patch Set: Alloc API + review comments Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SampleCode.h" 8 #include "SampleCode.h"
9 #include "SkView.h" 9 #include "SkView.h"
10 #include "SkCanvas.h" 10 #include "SkCanvas.h"
11 #include "SkReadBuffer.h" 11 #include "SkReadBuffer.h"
12 #include "SkWriteBuffer.h" 12 #include "SkWriteBuffer.h"
13 #include "SkGradientShader.h" 13 #include "SkGradientShader.h"
14 #include "SkGraphics.h" 14 #include "SkGraphics.h"
15 #include "SkImageDecoder.h" 15 #include "SkImageDecoder.h"
16 #include "SkPath.h" 16 #include "SkPath.h"
17 #include "SkRandom.h" 17 #include "SkRandom.h"
18 #include "SkRegion.h" 18 #include "SkRegion.h"
19 #include "SkShader.h" 19 #include "SkShader.h"
20 #include "SkUtils.h" 20 #include "SkUtils.h"
21 #include "SkColorPriv.h" 21 #include "SkColorPriv.h"
22 #include "SkColorFilter.h" 22 #include "SkColorFilter.h"
23 #include "SkTextBlob.h"
23 #include "SkTime.h" 24 #include "SkTime.h"
24 #include "SkTypeface.h" 25 #include "SkTypeface.h"
25 #include "SkXfermode.h" 26 #include "SkXfermode.h"
26 27
27 #include "SkStream.h" 28 #include "SkStream.h"
28 #include "SkXMLParser.h" 29 #include "SkXMLParser.h"
29 30
30 static void test_breakText() { 31 static void test_breakText() {
31 SkPaint paint; 32 SkPaint paint;
32 const char* text = "sdfkljAKLDFJKEWkldfjlk#$%&sdfs.dsj"; 33 const char* text = "sdfkljAKLDFJKEWkldfjlk#$%&sdfs.dsj";
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 bool fFlushCache; 70 bool fFlushCache;
70 } gHints[] = { 71 } gHints[] = {
71 { "Linear", SkPaint::kLinearText_Flag, false }, 72 { "Linear", SkPaint::kLinearText_Flag, false },
72 { "Normal", 0, true }, 73 { "Normal", 0, true },
73 { "Subpixel", SkPaint::kSubpixelText_Flag, true } 74 { "Subpixel", SkPaint::kSubpixelText_Flag, true }
74 }; 75 };
75 76
76 static void DrawTheText(SkCanvas* canvas, const char text[], size_t length, SkSc alar x, SkScalar y, 77 static void DrawTheText(SkCanvas* canvas, const char text[], size_t length, SkSc alar x, SkScalar y,
77 const SkPaint& paint, SkScalar clickX) { 78 const SkPaint& paint, SkScalar clickX) {
78 SkPaint p(paint); 79 SkPaint p(paint);
80 SkPoint pts[1000];
81 SkScalar xPos[1000];
82 SkASSERT(length <= SK_ARRAY_COUNT(pts));
79 83
80 #if 0 84 #if 0
81 canvas->drawText(text, length, x, y, paint); 85 canvas->drawText(text, length, x, y, paint);
82 #else 86 #else
83 { 87 {
84 SkPoint pts[1000];
85 SkScalar xpos = x; 88 SkScalar xpos = x;
86 SkASSERT(length <= SK_ARRAY_COUNT(pts));
87 for (size_t i = 0; i < length; i++) { 89 for (size_t i = 0; i < length; i++) {
88 pts[i].set(xpos, y), xpos += paint.getTextSize(); 90 pts[i].set(xpos, y), xpos += paint.getTextSize();
91 xPos[i] = pts[i].x();
89 } 92 }
90 canvas->drawPosText(text, length, pts, paint); 93 canvas->drawPosText(text, length, pts, paint);
91 } 94 }
92 #endif 95 #endif
93 96
94 p.setSubpixelText(true); 97 p.setSubpixelText(true);
95 x += SkIntToScalar(180); 98 x += SkIntToScalar(180);
96 canvas->drawText(text, length, x, y, p); 99 canvas->drawText(text, length, x, y, p);
97 100
98 #ifdef SK_DEBUG 101 #ifdef SK_DEBUG
99 if (true) { 102 if (true) {
100 p.setSubpixelText(false); 103 p.setSubpixelText(false);
101 p.setLinearText(true); 104 p.setLinearText(true);
102 x += SkIntToScalar(180); 105 x += SkIntToScalar(180);
103 canvas->drawText(text, length, x, y, p); 106 canvas->drawText(text, length, x, y, p);
104 } 107 }
105 #endif 108 #endif
109
110
111 uint16_t glyphs[1000];
112 SkASSERT(length <= SK_ARRAY_COUNT(glyphs));
113 int glyphCount = paint.textToGlyphs(text, length, glyphs);
114 SkPaint glyphPaint = paint;
115 glyphPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
116
117 SkTextBlobBuilder blobBuilder;
118 SkTextBlobBuilder::RunBuffer posRun = blobBuilder.allocRun(glyphPaint, glyph Count);
119 memcpy(posRun.glyphs, glyphs, glyphCount * sizeof(uint16_t));
120 memcpy(posRun.pos, pts, glyphCount * sizeof(SkScalar) * 2);
121 SkAutoTUnref<const SkTextBlob> posBlob(blobBuilder.build());
122 canvas->drawTextBlob(posBlob, 0, 400, glyphPaint);
123
124 glyphPaint.setSubpixelText(true);
125 uint16_t* defaultRun = blobBuilder.allocRun(glyphPaint, glyphCount,
126 SkPoint::Make(0, 0));
127 memcpy(defaultRun, glyphs, glyphCount * sizeof(uint16_t));
128 SkAutoTUnref<const SkTextBlob> blob(blobBuilder.build());
129 canvas->drawTextBlob(blob, 180, 400 + y, glyphPaint);
130
131 glyphPaint.setSubpixelText(false);
132 glyphPaint.setLinearText(true);
133 SkTextBlobBuilder::RunBuffer hPosRun = blobBuilder.allocRun(glyphPaint, glyp hCount, 0, NULL);
134 memcpy(hPosRun.glyphs, glyphs, glyphCount * sizeof(uint16_t));
135 memcpy(hPosRun.pos, xPos, glyphCount * sizeof(SkScalar));
136 SkAutoTUnref<const SkTextBlob> hPosBlob(blobBuilder.build());
137 canvas->drawTextBlob(hPosBlob, 360, 400 + y, glyphPaint);
106 } 138 }
107 139
108 class TextSpeedView : public SampleView { 140 class TextSpeedView : public SampleView {
109 public: 141 public:
110 TextSpeedView() { 142 TextSpeedView() {
111 fHints = 0; 143 fHints = 0;
112 fClickX = 0; 144 fClickX = 0;
113 145
114 test_breakText(); 146 test_breakText();
115 } 147 }
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 int fHints; 233 int fHints;
202 SkScalar fClickX; 234 SkScalar fClickX;
203 235
204 typedef SampleView INHERITED; 236 typedef SampleView INHERITED;
205 }; 237 };
206 238
207 ////////////////////////////////////////////////////////////////////////////// 239 //////////////////////////////////////////////////////////////////////////////
208 240
209 static SkView* MyFactory() { return new TextSpeedView; } 241 static SkView* MyFactory() { return new TextSpeedView; }
210 static SkViewRegister reg(MyFactory); 242 static SkViewRegister reg(MyFactory);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698