blob: 86f9b52516adfd49aaa7a11ac7560c66e065c68d [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 {
reed@android.com8a1c16f2008-12-17 15:59:43 +000026 bool fLinearText;
27 bool fDevKernText;
28};
29
reed@android.com8a1c16f2008-12-17 15:59:43 +000030static const Setting gSettings[] = {
reed99ae8812014-08-26 11:30:01 -070031 { false, false },
32 { false, true },
33 { true, false },
34 { true, true },
reed@android.com8a1c16f2008-12-17 15:59:43 +000035};
36
reed@google.com81e3d7f2011-06-01 12:42:36 +000037static void doMeasure(SkCanvas* canvas, const SkPaint& paint, const char text[]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000038 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);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000048 for (size_t i = 0; i < SK_ARRAY_COUNT(gSettings); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 p.setLinearText(gSettings[i].fLinearText);
50 p.setDevKernText(gSettings[i].fDevKernText);
rmistry@google.comae933ce2012-08-23 18:19:56 +000051
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 int n = p.getTextWidths(text, len, widths, rects);
reed99ae8812014-08-26 11:30:01 -070053 SkScalar w = p.measureText(text, len, &bounds);
rmistry@google.comae933ce2012-08-23 18:19:56 +000054
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 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);
rmistry@google.comae933ce2012-08-23 18:19:56 +000076
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 canvas->translate(0, dy);
78 }
79}
80
reed@google.com81e3d7f2011-06-01 12:42:36 +000081class MeasureView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000082public:
83 SkPaint fPaint;
84
rmistry@google.comae933ce2012-08-23 18:19:56 +000085 MeasureView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000086 fPaint.setAntiAlias(true);
87 fPaint.setTextSize(SkIntToScalar(64));
reed@google.com81e3d7f2011-06-01 12:42:36 +000088 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 }
90
91protected:
92 // overrides from SkEventSink
reed@google.com81e3d7f2011-06-01 12:42:36 +000093 virtual bool onQuery(SkEvent* evt) {
94 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000095 SampleCode::TitleR(evt, "Measure");
96 return true;
97 }
98 return this->INHERITED::onQuery(evt);
99 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000100
reed@google.com81e3d7f2011-06-01 12:42:36 +0000101 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 canvas->translate(fPaint.getTextSize(), fPaint.getTextSize());
103 doMeasure(canvas, fPaint, "Hamburgefons");
104 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000105
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106private:
reed@google.com81e3d7f2011-06-01 12:42:36 +0000107 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108};
109
110//////////////////////////////////////////////////////////////////////////////
111
112static SkView* MyFactory() { return new MeasureView; }
113static SkViewRegister reg(MyFactory);