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

Side by Side Diff: gm/fontmgr.cpp

Issue 804903006: add paint::getFontBounds() (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years 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/SkPaint.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
25 // limit this just so we don't take too long to draw 18 // limit this just so we don't take too long to draw
26 #define MAX_FAMILIES 30 19 #define MAX_FAMILIES 30
27 20
28 static SkScalar drawString(SkCanvas* canvas, const SkString& text, SkScalar x, 21 static SkScalar drawString(SkCanvas* canvas, const SkString& text, SkScalar x,
29 SkScalar y, const SkPaint& paint) { 22 SkScalar y, const SkPaint& paint) {
30 canvas->drawText(text.c_str(), text.size(), x, y, paint); 23 canvas->drawText(text.c_str(), text.size(), x, y, paint);
31 return x + paint.measureText(text.c_str(), text.size()); 24 return x + paint.measureText(text.c_str(), text.size());
32 } 25 }
33 26
34 static SkScalar drawCharacter(SkCanvas* canvas, uint32_t character, SkScalar x, 27 static SkScalar drawCharacter(SkCanvas* canvas, uint32_t character, SkScalar x,
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 // these new styles, so skip tests that exercise that for now. 219 // these new styles, so skip tests that exercise that for now.
227 return kSkipPicture_Flag | kSkipPipe_Flag; 220 return kSkipPicture_Flag | kSkipPipe_Flag;
228 } 221 }
229 222
230 private: 223 private:
231 typedef GM INHERITED; 224 typedef GM INHERITED;
232 }; 225 };
233 226
234 class FontMgrBoundsGM : public skiagm::GM { 227 class FontMgrBoundsGM : public skiagm::GM {
235 public: 228 public:
236 FontMgrBoundsGM() { 229 FontMgrBoundsGM(double scale, double skew)
230 : fScaleX(SkDoubleToScalar(scale))
231 , fSkewX(SkDoubleToScalar(skew))
232 {
237 fName.set("fontmgr_bounds"); 233 fName.set("fontmgr_bounds");
234 if (scale != 1 || skew != 0) {
235 fName.appendf("_%g_%g", scale, skew);
236 }
238 fFM.reset(SkFontMgr::RefDefault()); 237 fFM.reset(SkFontMgr::RefDefault());
239 } 238 }
240 239
241 static void show_bounds(SkCanvas* canvas, const SkPaint& paint, SkScalar x, SkScalar y, 240 static void show_bounds(SkCanvas* canvas, const SkPaint& paint, SkScalar x, SkScalar y,
242 SkColor boundsColor) { 241 SkColor boundsColor) {
243 const char str[] = "jyHO[]{}@-_&%$"; 242 const char str[] = "jyHO[]{}@-_&%$";
244 243
245 const SkTypeface* tf = paint.getTypeface();
246 for (int i = 0; str[i]; ++i) { 244 for (int i = 0; str[i]; ++i) {
247 canvas->drawText(&str[i], 1, x, y, paint); 245 canvas->drawText(&str[i], 1, x, y, paint);
248 } 246 }
249 247
250 SkRect r = tf->getBounds(); 248 SkRect r = paint.getFontBounds();
251 scale(&r, paint.getTextSize());
252 r.offset(x, y); 249 r.offset(x, y);
253 SkPaint p(paint); 250 SkPaint p(paint);
254 p.setColor(boundsColor); 251 p.setColor(boundsColor);
255 canvas->drawRect(r, p); 252 canvas->drawRect(r, p);
256 } 253 }
257 254
258 protected: 255 protected:
259 virtual SkString onShortName() { 256 virtual SkString onShortName() {
260 return fName; 257 return fName;
261 } 258 }
262 259
263 virtual SkISize onISize() { 260 virtual SkISize onISize() {
264 return SkISize::Make(1024, 850); 261 return SkISize::Make(1024, 850);
265 } 262 }
266 263
267 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { 264 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
268 SkPaint paint; 265 SkPaint paint;
269 paint.setAntiAlias(true); 266 paint.setAntiAlias(true);
270 paint.setSubpixelText(true); 267 paint.setSubpixelText(true);
271 paint.setTextSize(100); 268 paint.setTextSize(100);
272 paint.setStyle(SkPaint::kStroke_Style); 269 paint.setStyle(SkPaint::kStroke_Style);
270 paint.setTextScaleX(fScaleX);
271 paint.setTextSkewX(fSkewX);
273 272
274 const SkColor boundsColors[2] = { SK_ColorRED, SK_ColorBLUE }; 273 const SkColor boundsColors[2] = { SK_ColorRED, SK_ColorBLUE };
275 274
276 SkFontMgr* fm = fFM; 275 SkFontMgr* fm = fFM;
277 int count = SkMin32(fm->countFamilies(), 32); 276 int count = SkMin32(fm->countFamilies(), 32);
278 277
279 int index = 0; 278 int index = 0;
280 SkScalar x = 0, y = 0; 279 SkScalar x = 0, y = 0;
281 280
282 canvas->translate(80, 120); 281 canvas->translate(80, 120);
(...skipping 24 matching lines...) Expand all
307 306
308 // If certain fonts are picked up (e.g. Microsoft Jhenghei 20MB for Regu lar, 12MB for Bold), 307 // If certain fonts are picked up (e.g. Microsoft Jhenghei 20MB for Regu lar, 12MB for Bold),
309 // the resulting pdf can be ~700MB and crashes Chrome's PDF viewer. 308 // the resulting pdf can be ~700MB and crashes Chrome's PDF viewer.
310 309
311 return kSkipPicture_Flag | kSkipPipe_Flag | kSkipPDF_Flag; 310 return kSkipPicture_Flag | kSkipPipe_Flag | kSkipPDF_Flag;
312 } 311 }
313 312
314 private: 313 private:
315 SkAutoTUnref<SkFontMgr> fFM; 314 SkAutoTUnref<SkFontMgr> fFM;
316 SkString fName; 315 SkString fName;
316 SkScalar fScaleX, fSkewX;
317 typedef GM INHERITED; 317 typedef GM INHERITED;
318 }; 318 };
319 319
320 ////////////////////////////////////////////////////////////////////////////// 320 //////////////////////////////////////////////////////////////////////////////
321 321
322 DEF_GM( return SkNEW(FontMgrGM); ) 322 DEF_GM( return SkNEW(FontMgrGM); )
323 DEF_GM( return SkNEW(FontMgrBoundsGM); )
324 DEF_GM( return SkNEW(FontMgrMatchGM); ) 323 DEF_GM( return SkNEW(FontMgrMatchGM); )
324 DEF_GM( return SkNEW(FontMgrBoundsGM(1.0, 0)); )
325 DEF_GM( return SkNEW(FontMgrBoundsGM(0.75, 0)); )
326 DEF_GM( return SkNEW(FontMgrBoundsGM(1.0, -0.25)); )
325 327
326 #ifdef SK_BUILD_FOR_WIN 328 #ifdef SK_BUILD_FOR_WIN
327 DEF_GM( return SkNEW_ARGS(FontMgrGM, (SkFontMgr_New_DirectWrite())); ) 329 DEF_GM( return SkNEW_ARGS(FontMgrGM, (SkFontMgr_New_DirectWrite())); )
328 #endif 330 #endif
OLDNEW
« no previous file with comments | « no previous file | include/core/SkPaint.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698