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

Side by Side Diff: gm/drawatlas.cpp

Issue 2137083005: center glyphs for text-on-path-via-rsxform (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 5 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2015 Google Inc. 2 * Copyright 2015 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 "gm.h" 8 #include "gm.h"
9 #include "SkCanvas.h" 9 #include "SkCanvas.h"
10 #include "SkRSXform.h" 10 #include "SkRSXform.h"
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 103
104 //////////////////////////////////////////////////////////////////////////////// /////////////////// 104 //////////////////////////////////////////////////////////////////////////////// ///////////////////
105 #include "SkPath.h" 105 #include "SkPath.h"
106 #include "SkPathMeasure.h" 106 #include "SkPathMeasure.h"
107 107
108 static void draw_text_on_path_rigid(SkCanvas* canvas, const void* text, size_t l ength, 108 static void draw_text_on_path_rigid(SkCanvas* canvas, const void* text, size_t l ength,
109 const SkPoint xy[], const SkPath& path, cons t SkPaint& paint) { 109 const SkPoint xy[], const SkPath& path, cons t SkPaint& paint) {
110 SkPathMeasure meas(path, false); 110 SkPathMeasure meas(path, false);
111 111
112 int count = paint.countText(text, length); 112 int count = paint.countText(text, length);
113 SkAutoSTArray<100, SkRSXform> xform(count); 113 size_t size = count * (sizeof(SkRSXform) + sizeof(SkScalar));
114 SkAutoSMalloc<512> storage(size);
115 SkRSXform* xform = (SkRSXform*)storage.get();
116 SkScalar* widths = (SkScalar*)(xform + count);
117
118 paint.getTextWidths(text, length, widths);
114 119
115 for (int i = 0; i < count; ++i) { 120 for (int i = 0; i < count; ++i) {
121 // we want to position each character on the center of its advance
122 const SkScalar offset = SkScalarHalf(widths[i]);
116 SkPoint pos; 123 SkPoint pos;
117 SkVector tan; 124 SkVector tan;
118 if (!meas.getPosTan(xy[i].x(), &pos, &tan)) { 125 if (!meas.getPosTan(xy[i].x() + offset, &pos, &tan)) {
119 pos = xy[i]; 126 pos = xy[i];
120 tan.set(1, 0); 127 tan.set(1, 0);
121 } 128 }
122 xform[i].fSCos = tan.x(); 129 xform[i].fSCos = tan.x();
123 xform[i].fSSin = tan.y(); 130 xform[i].fSSin = tan.y();
124 xform[i].fTx = pos.x() - tan.y() * xy[i].y(); 131 xform[i].fTx = pos.x() - tan.y() * xy[i].y() - tan.x() * offset;
125 xform[i].fTy = pos.y() + tan.x() * xy[i].y(); 132 xform[i].fTy = pos.y() + tan.x() * xy[i].y() - tan.y() * offset;
126 } 133 }
127 134
128 canvas->drawTextRSXform(text, length, &xform[0], nullptr, paint); 135 // Compute a conservative bounds so we can cull the draw
136 const SkRect font = paint.getFontBounds();
137 const SkScalar max = SkTMax(SkTMax(SkScalarAbs(font.fLeft), SkScalarAbs(font .fRight)),
138 SkTMax(SkScalarAbs(font.fTop), SkScalarAbs(font. fBottom)));
139 const SkRect bounds = path.getBounds().makeOutset(max, max);
140
141 canvas->drawTextRSXform(text, length, &xform[0], &bounds, paint);
142
143 if (true) {
144 SkPaint p;
145 p.setStyle(SkPaint::kStroke_Style);
146 canvas->drawRect(bounds, p);
147 }
129 } 148 }
130 149
131 DEF_SIMPLE_GM(drawTextRSXform, canvas, 510, 370) { 150 DEF_SIMPLE_GM(drawTextRSXform, canvas, 860, 860) {
132 const char text0[] = "ABCDFGHJKLMNOPQRSTUVWXYZ"; 151 const char text0[] = "ABCDFGHJKLMNOPQRSTUVWXYZ";
133 const char text1[] = "AAAAAAAAAAAAAAAAAAAAAAAAAA";
134 const int N = sizeof(text0) - 1; 152 const int N = sizeof(text0) - 1;
135 SkPoint pos[N]; 153 SkPoint pos[N];
136 SkRSXform xform[N];
137
138 canvas->translate(0, 30);
139
140 SkScalar x = 20;
141 SkScalar dx = 20;
142 SkScalar rad = 0;
143 SkScalar drad = 2 * SK_ScalarPI / (N - 1);
144 for (int i = 0; i < N; ++i) {
145 xform[i].fSCos = SkScalarCos(rad);
146 xform[i].fSSin = SkScalarSin(rad);
147 xform[i].fTx = x;
148 xform[i].fTy = 0;
149 pos[i].set(x, -10);
150 x += dx;
151 rad += drad;
152 }
153 154
154 SkPaint paint; 155 SkPaint paint;
155 paint.setAntiAlias(true); 156 paint.setAntiAlias(true);
156 paint.setTextSize(20); 157 paint.setTextSize(100);
157 canvas->drawTextRSXform(text0, N, xform, nullptr, paint); 158
159 SkScalar x = 0;
160 for (int i = 0; i < N; ++i) {
161 pos[i].set(x, 0);
162 x += paint.measureText(&text0[i], 1);
163 }
158 164
159 SkPath path; 165 SkPath path;
160 path.addOval(SkRect::MakeXYWH(150, 100, 200, 200)); 166 path.addOval(SkRect::MakeXYWH(160, 160, 540, 540));
161 167
162 draw_text_on_path_rigid(canvas, text1, N, pos, path, paint); 168 draw_text_on_path_rigid(canvas, text0, N, pos, path, paint);
163 169
164 paint.setStyle(SkPaint::kStroke_Style); 170 paint.setStyle(SkPaint::kStroke_Style);
165 canvas->drawPath(path, paint); 171 canvas->drawPath(path, paint);
166 } 172 }
167 173
168 174
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698