1 2 /* 3 * Copyright 2011 Google Inc. 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 #include "SampleCode.h" 9 #include "SkView.h" 10 #include "SkCanvas.h" 11 #include "SkGradientShader.h" 12 #include "SkPath.h" 13 #include "SkRegion.h" 14 #include "SkShader.h" 15 #include "SkUtils.h" 16 #include "Sk1DPathEffect.h" 17 #include "SkCornerPathEffect.h" 18 #include "SkPathMeasure.h" 19 #include "SkRandom.h" 20 #include "SkColorPriv.h" 21 #include "SkColorFilter.h" 22 #include "SkDither.h" 23 24 // exercise scale/linear/devkern 25 struct Setting { 26 bool fLinearText; 27 bool fDevKernText; 28 }; 29 30 static const Setting gSettings[] = { 31 { false, false }, 32 { false, true }, 33 { true, false }, 34 { true, true }, 35 }; 36 37 static void doMeasure(SkCanvas* canvas, const SkPaint& paint, const char text[]) { 38 SkScalar dy = paint.getFontMetrics(NULL); 39 40 size_t len = strlen(text); 41 SkAutoTMalloc<SkScalar> autoWidths(len); 42 SkScalar* widths = autoWidths.get(); 43 SkAutoTMalloc<SkRect> autoRects(len); 44 SkRect* rects = autoRects.get(); 45 SkRect bounds; 46 47 SkPaint p(paint); 48 for (size_t i = 0; i < SK_ARRAY_COUNT(gSettings); i++) { 49 p.setLinearText(gSettings[i].fLinearText); 50 p.setDevKernText(gSettings[i].fDevKernText); 51 52 int n = p.getTextWidths(text, len, widths, rects); 53 SkScalar w = p.measureText(text, len, &bounds); 54 55 p.setStyle(SkPaint::kFill_Style); 56 p.setColor(0x8888FF88); 57 canvas->drawRect(bounds, p); 58 p.setColor(0xFF000000); 59 canvas->drawText(text, len, 0, 0, p); 60 61 p.setStyle(SkPaint::kStroke_Style); 62 p.setStrokeWidth(0); 63 p.setColor(0xFFFF0000); 64 SkScalar x = 0; 65 for (int j = 0; j < n; j++) { 66 SkRect r = rects[j]; 67 r.offset(x, 0); 68 canvas->drawRect(r, p); 69 x += widths[j]; 70 } 71 72 p.setColor(0xFF0000FF); 73 canvas->drawLine(0, 0, w, 0, p); 74 p.setStrokeWidth(SkIntToScalar(4)); 75 canvas->drawPoint(x, 0, p); 76 77 canvas->translate(0, dy); 78 } 79 } 80 81 class MeasureView : public SampleView { 82 public: 83 SkPaint fPaint; 84 85 MeasureView() { 86 fPaint.setAntiAlias(true); 87 fPaint.setTextSize(SkIntToScalar(64)); 88 this->setBGColor(0xFFDDDDDD); 89 } 90 91 protected: 92 // overrides from SkEventSink 93 virtual bool onQuery(SkEvent* evt) { 94 if (SampleCode::TitleQ(*evt)) { 95 SampleCode::TitleR(evt, "Measure"); 96 return true; 97 } 98 return this->INHERITED::onQuery(evt); 99 } 100 101 virtual void onDrawContent(SkCanvas* canvas) { 102 canvas->translate(fPaint.getTextSize(), fPaint.getTextSize()); 103 doMeasure(canvas, fPaint, "Hamburgefons"); 104 } 105 106 private: 107 typedef SampleView INHERITED; 108 }; 109 110 ////////////////////////////////////////////////////////////////////////////// 111 112 static SkView* MyFactory() { return new MeasureView; } 113 static SkViewRegister reg(MyFactory); 114