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

Side by Side Diff: gm/fontmgr.cpp

Issue 676523002: Add SkTypeface::getBounds() (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years, 1 month 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 | include/core/SkLazyPtr.h » ('j') | 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 2013 Google Inc. 2 * Copyright 2013 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 "SkFontMgr.h" 10 #include "SkFontMgr.h"
11 #include "SkGraphics.h" 11 #include "SkGraphics.h"
12 #include "SkTypeface.h" 12 #include "SkTypeface.h"
13 13
14 #ifdef SK_BUILD_FOR_WIN 14 #ifdef SK_BUILD_FOR_WIN
15 #include "SkTypeface_win.h" 15 #include "SkTypeface_win.h"
16 #endif 16 #endif
17 17
18 static void scale(SkRect* rect, SkScalar scale) {
19 rect->fLeft *= scale;
20 rect->fTop *= scale;
21 rect->fRight *= scale;
22 rect->fBottom *= scale;
23 }
24
18 // limit this just so we don't take too long to draw 25 // limit this just so we don't take too long to draw
19 #define MAX_FAMILIES 30 26 #define MAX_FAMILIES 30
20 27
21 static SkScalar drawString(SkCanvas* canvas, const SkString& text, SkScalar x, 28 static SkScalar drawString(SkCanvas* canvas, const SkString& text, SkScalar x,
22 SkScalar y, const SkPaint& paint) { 29 SkScalar y, const SkPaint& paint) {
23 canvas->drawText(text.c_str(), text.size(), x, y, paint); 30 canvas->drawText(text.c_str(), text.size(), x, y, paint);
24 return x + paint.measureText(text.c_str(), text.size()); 31 return x + paint.measureText(text.c_str(), text.size());
25 } 32 }
26 33
27 static SkScalar drawCharacter(SkCanvas* canvas, uint32_t character, SkScalar x, 34 static SkScalar drawCharacter(SkCanvas* canvas, uint32_t character, SkScalar x,
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 virtual uint32_t onGetFlags() const SK_OVERRIDE { 220 virtual uint32_t onGetFlags() const SK_OVERRIDE {
214 // fontdescriptors (and therefore serialization) don't yet understand 221 // fontdescriptors (and therefore serialization) don't yet understand
215 // these new styles, so skip tests that exercise that for now. 222 // these new styles, so skip tests that exercise that for now.
216 return kSkipPicture_Flag | kSkipPipe_Flag; 223 return kSkipPicture_Flag | kSkipPipe_Flag;
217 } 224 }
218 225
219 private: 226 private:
220 typedef GM INHERITED; 227 typedef GM INHERITED;
221 }; 228 };
222 229
230 class FontMgrBoundsGM : public skiagm::GM {
231 public:
232 FontMgrBoundsGM() {
233 fName.set("fontmgr_bounds");
234 fFM.reset(SkFontMgr::RefDefault());
235 }
236
237 static void show_bounds(SkCanvas* canvas, const SkPaint& paint, SkScalar x, SkScalar y,
238 SkColor boundsColor) {
239 const char str[] = "jyHO[]{}@-_&%$";
240
241 const SkTypeface* tf = paint.getTypeface();
242 for (int i = 0; str[i]; ++i) {
243 canvas->drawText(&str[i], 1, x, y, paint);
244 }
245
246 SkRect r = tf->getBounds();
247 scale(&r, paint.getTextSize());
248 r.offset(x, y);
249 SkPaint p(paint);
250 p.setColor(boundsColor);
251 canvas->drawRect(r, p);
252 }
253
254 protected:
255 virtual SkString onShortName() {
256 return fName;
257 }
258
259 virtual SkISize onISize() {
260 return SkISize::Make(1024, 850);
261 }
262
263 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
264 SkPaint paint;
265 paint.setAntiAlias(true);
266 paint.setSubpixelText(true);
267 paint.setTextSize(100);
268 paint.setStyle(SkPaint::kStroke_Style);
269
270 const SkColor boundsColors[2] = { SK_ColorRED, SK_ColorBLUE };
271
272 SkFontMgr* fm = fFM;
273 int count = SkMin32(fm->countFamilies(), 32);
274
275 int index = 0;
276 SkScalar x = 0, y = 0;
277
278 canvas->translate(80, 120);
279
280 for (int i = 0; i < count; ++i) {
281 SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i));
282 for (int j = 0; j < set->count(); ++j) {
283 SkSafeUnref(paint.setTypeface(set->createTypeface(j)));
284 if (paint.getTypeface()) {
285 show_bounds(canvas, paint, x, y, boundsColors[index & 1]);
286 index += 1;
287 x += 160;
288 if (0 == (index % 6)) {
289 x = 0;
290 y += 160;
291 }
292 if (index >= 30) {
293 return;
294 }
295 }
296 }
297 }
298 }
299
300 virtual uint32_t onGetFlags() const SK_OVERRIDE {
301 // fontdescriptors (and therefore serialization) don't yet understand
302 // these new styles, so skip tests that exercise that for now.
303
304 // If certain fonts are picked up (e.g. Microsoft Jhenghei 20MB for Regu lar, 12MB for Bold),
305 // the resulting pdf can be ~700MB and crashes Chrome's PDF viewer.
306
307 return kSkipPicture_Flag | kSkipPipe_Flag | kSkipPDF_Flag;
308 }
309
310 private:
311 SkAutoTUnref<SkFontMgr> fFM;
312 SkString fName;
313 typedef GM INHERITED;
314 };
315
223 ////////////////////////////////////////////////////////////////////////////// 316 //////////////////////////////////////////////////////////////////////////////
224 317
225 DEF_GM( return SkNEW(FontMgrGM); ) 318 DEF_GM( return SkNEW(FontMgrGM); )
319 DEF_GM( return SkNEW(FontMgrBoundsGM); )
226 DEF_GM( return SkNEW(FontMgrMatchGM); ) 320 DEF_GM( return SkNEW(FontMgrMatchGM); )
227 321
228 #ifdef SK_BUILD_FOR_WIN 322 #ifdef SK_BUILD_FOR_WIN
229 DEF_GM( return SkNEW_ARGS(FontMgrGM, (SkFontMgr_New_DirectWrite())); ) 323 DEF_GM( return SkNEW_ARGS(FontMgrGM, (SkFontMgr_New_DirectWrite())); )
230 #endif 324 #endif
OLDNEW
« no previous file with comments | « no previous file | include/core/SkLazyPtr.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698