blob: dc771db7d688dd975e079a838fc6e647c2ac01c1 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00007#include "SampleCode.h"
8#include "SkView.h"
9#include "SkCanvas.h"
10#include "SkGradientShader.h"
11#include "SkPath.h"
12#include "SkRegion.h"
13#include "SkShader.h"
14#include "SkUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "Sk1DPathEffect.h"
16#include "SkCornerPathEffect.h"
17#include "SkPathMeasure.h"
18#include "SkRandom.h"
19#include "SkColorPriv.h"
20#include "SkColorFilter.h"
21#include "SkDither.h"
22
23// exercise scale/linear/devkern
24struct Setting {
reed@android.com8a1c16f2008-12-17 15:59:43 +000025 bool fLinearText;
26 bool fDevKernText;
27};
28
reed@android.com8a1c16f2008-12-17 15:59:43 +000029static const Setting gSettings[] = {
reed99ae8812014-08-26 11:30:01 -070030 { false, false },
31 { false, true },
32 { true, false },
33 { true, true },
reed@android.com8a1c16f2008-12-17 15:59:43 +000034};
35
reed@google.com81e3d7f2011-06-01 12:42:36 +000036static void doMeasure(SkCanvas* canvas, const SkPaint& paint, const char text[]) {
halcanary96fcdcc2015-08-27 07:41:13 -070037 SkScalar dy = paint.getFontMetrics(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +000038
39 size_t len = strlen(text);
40 SkAutoTMalloc<SkScalar> autoWidths(len);
41 SkScalar* widths = autoWidths.get();
42 SkAutoTMalloc<SkRect> autoRects(len);
43 SkRect* rects = autoRects.get();
44 SkRect bounds;
45
46 SkPaint p(paint);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000047 for (size_t i = 0; i < SK_ARRAY_COUNT(gSettings); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 p.setLinearText(gSettings[i].fLinearText);
49 p.setDevKernText(gSettings[i].fDevKernText);
rmistry@google.comae933ce2012-08-23 18:19:56 +000050
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 int n = p.getTextWidths(text, len, widths, rects);
reed99ae8812014-08-26 11:30:01 -070052 SkScalar w = p.measureText(text, len, &bounds);
rmistry@google.comae933ce2012-08-23 18:19:56 +000053
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 p.setStyle(SkPaint::kFill_Style);
55 p.setColor(0x8888FF88);
56 canvas->drawRect(bounds, p);
57 p.setColor(0xFF000000);
58 canvas->drawText(text, len, 0, 0, p);
59
60 p.setStyle(SkPaint::kStroke_Style);
61 p.setStrokeWidth(0);
62 p.setColor(0xFFFF0000);
63 SkScalar x = 0;
64 for (int j = 0; j < n; j++) {
65 SkRect r = rects[j];
66 r.offset(x, 0);
67 canvas->drawRect(r, p);
68 x += widths[j];
69 }
70
71 p.setColor(0xFF0000FF);
72 canvas->drawLine(0, 0, w, 0, p);
73 p.setStrokeWidth(SkIntToScalar(4));
74 canvas->drawPoint(x, 0, p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000075
reed@android.com8a1c16f2008-12-17 15:59:43 +000076 canvas->translate(0, dy);
77 }
78}
79
reed@google.com81e3d7f2011-06-01 12:42:36 +000080class MeasureView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000081public:
82 SkPaint fPaint;
83
rmistry@google.comae933ce2012-08-23 18:19:56 +000084 MeasureView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 fPaint.setAntiAlias(true);
86 fPaint.setTextSize(SkIntToScalar(64));
reed@google.com81e3d7f2011-06-01 12:42:36 +000087 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 }
89
90protected:
91 // overrides from SkEventSink
reed@google.com81e3d7f2011-06-01 12:42:36 +000092 virtual bool onQuery(SkEvent* evt) {
93 if (SampleCode::TitleQ(*evt)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 SampleCode::TitleR(evt, "Measure");
95 return true;
96 }
97 return this->INHERITED::onQuery(evt);
98 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000099
reed@google.com81e3d7f2011-06-01 12:42:36 +0000100 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 canvas->translate(fPaint.getTextSize(), fPaint.getTextSize());
102 doMeasure(canvas, fPaint, "Hamburgefons");
103 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000104
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105private:
reed@google.com81e3d7f2011-06-01 12:42:36 +0000106 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107};
108
109//////////////////////////////////////////////////////////////////////////////
110
111static SkView* MyFactory() { return new MeasureView; }
112static SkViewRegister reg(MyFactory);