blob: 18b3720997f2d617af1804abe5cbbbac59023123 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#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"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#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
25struct Setting {
26 SkScalar fScale;
27 bool fLinearText;
28 bool fDevKernText;
29};
30
31static const SkScalar ONE = SkIntToScalar(9999)/10000;
32
33static const Setting gSettings[] = {
34 { 0, false, false },
35 { 0, false, true },
36 { 0, true, false },
37 { 0, true, true },
38 { ONE, false, false },
39 { ONE, false, true },
40 { ONE, true, false },
41 { ONE, true, true }
42};
43
reed@google.com81e3d7f2011-06-01 12:42:36 +000044static void doMeasure(SkCanvas* canvas, const SkPaint& paint, const char text[]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 SkScalar dy = paint.getFontMetrics(NULL);
46
47 size_t len = strlen(text);
48 SkAutoTMalloc<SkScalar> autoWidths(len);
49 SkScalar* widths = autoWidths.get();
50 SkAutoTMalloc<SkRect> autoRects(len);
51 SkRect* rects = autoRects.get();
52 SkRect bounds;
53
54 SkPaint p(paint);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000055 for (size_t i = 0; i < SK_ARRAY_COUNT(gSettings); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 p.setLinearText(gSettings[i].fLinearText);
57 p.setDevKernText(gSettings[i].fDevKernText);
58 SkScalar scale = gSettings[i].fScale;
rmistry@google.comae933ce2012-08-23 18:19:56 +000059
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 int n = p.getTextWidths(text, len, widths, rects);
61 SkScalar w = p.measureText(text, len, &bounds, scale);
rmistry@google.comae933ce2012-08-23 18:19:56 +000062
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 p.setStyle(SkPaint::kFill_Style);
64 p.setColor(0x8888FF88);
65 canvas->drawRect(bounds, p);
66 p.setColor(0xFF000000);
67 canvas->drawText(text, len, 0, 0, p);
68
69 p.setStyle(SkPaint::kStroke_Style);
70 p.setStrokeWidth(0);
71 p.setColor(0xFFFF0000);
72 SkScalar x = 0;
73 for (int j = 0; j < n; j++) {
74 SkRect r = rects[j];
75 r.offset(x, 0);
76 canvas->drawRect(r, p);
77 x += widths[j];
78 }
79
80 p.setColor(0xFF0000FF);
81 canvas->drawLine(0, 0, w, 0, p);
82 p.setStrokeWidth(SkIntToScalar(4));
83 canvas->drawPoint(x, 0, p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000084
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 canvas->translate(0, dy);
86 }
87}
88
reed@google.com81e3d7f2011-06-01 12:42:36 +000089class MeasureView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000090public:
91 SkPaint fPaint;
92
rmistry@google.comae933ce2012-08-23 18:19:56 +000093 MeasureView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 fPaint.setAntiAlias(true);
95 fPaint.setTextSize(SkIntToScalar(64));
reed@google.com81e3d7f2011-06-01 12:42:36 +000096 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 }
98
99protected:
100 // overrides from SkEventSink
reed@google.com81e3d7f2011-06-01 12:42:36 +0000101 virtual bool onQuery(SkEvent* evt) {
102 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 SampleCode::TitleR(evt, "Measure");
104 return true;
105 }
106 return this->INHERITED::onQuery(evt);
107 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000108
reed@google.com81e3d7f2011-06-01 12:42:36 +0000109 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 canvas->translate(fPaint.getTextSize(), fPaint.getTextSize());
111 doMeasure(canvas, fPaint, "Hamburgefons");
112 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000113
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114private:
reed@google.com81e3d7f2011-06-01 12:42:36 +0000115 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116};
117
118//////////////////////////////////////////////////////////////////////////////
119
120static SkView* MyFactory() { return new MeasureView; }
121static SkViewRegister reg(MyFactory);
122