blob: 9fc8530a69c080b88349ba1c5b97146b74e90b9e [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 */
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04007#include "Sample.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkCanvas.h"
9#include "SkGradientShader.h"
10#include "SkPath.h"
11#include "SkRegion.h"
12#include "SkShader.h"
Hal Canaryea60b952018-08-21 11:45:46 -040013#include "SkUTF.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "Sk1DPathEffect.h"
15#include "SkCornerPathEffect.h"
16#include "SkPathMeasure.h"
17#include "SkRandom.h"
18#include "SkColorPriv.h"
19#include "SkColorFilter.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020
21// exercise scale/linear/devkern
22struct Setting {
reed@android.com8a1c16f2008-12-17 15:59:43 +000023 bool fLinearText;
24 bool fDevKernText;
25};
26
reed@android.com8a1c16f2008-12-17 15:59:43 +000027static const Setting gSettings[] = {
reed99ae8812014-08-26 11:30:01 -070028 { false, false },
29 { false, true },
30 { true, false },
31 { true, true },
reed@android.com8a1c16f2008-12-17 15:59:43 +000032};
33
reed@google.com81e3d7f2011-06-01 12:42:36 +000034static void doMeasure(SkCanvas* canvas, const SkPaint& paint, const char text[]) {
halcanary96fcdcc2015-08-27 07:41:13 -070035 SkScalar dy = paint.getFontMetrics(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +000036
37 size_t len = strlen(text);
38 SkAutoTMalloc<SkScalar> autoWidths(len);
39 SkScalar* widths = autoWidths.get();
40 SkAutoTMalloc<SkRect> autoRects(len);
41 SkRect* rects = autoRects.get();
42 SkRect bounds;
43
44 SkPaint p(paint);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000045 for (size_t i = 0; i < SK_ARRAY_COUNT(gSettings); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000046 p.setLinearText(gSettings[i].fLinearText);
47 p.setDevKernText(gSettings[i].fDevKernText);
rmistry@google.comae933ce2012-08-23 18:19:56 +000048
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 int n = p.getTextWidths(text, len, widths, rects);
reed99ae8812014-08-26 11:30:01 -070050 SkScalar w = p.measureText(text, len, &bounds);
rmistry@google.comae933ce2012-08-23 18:19:56 +000051
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 p.setStyle(SkPaint::kFill_Style);
53 p.setColor(0x8888FF88);
54 canvas->drawRect(bounds, p);
55 p.setColor(0xFF000000);
56 canvas->drawText(text, len, 0, 0, p);
57
58 p.setStyle(SkPaint::kStroke_Style);
59 p.setStrokeWidth(0);
60 p.setColor(0xFFFF0000);
61 SkScalar x = 0;
62 for (int j = 0; j < n; j++) {
63 SkRect r = rects[j];
64 r.offset(x, 0);
65 canvas->drawRect(r, p);
66 x += widths[j];
67 }
68
69 p.setColor(0xFF0000FF);
70 canvas->drawLine(0, 0, w, 0, p);
71 p.setStrokeWidth(SkIntToScalar(4));
72 canvas->drawPoint(x, 0, p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000073
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 canvas->translate(0, dy);
75 }
76}
77
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040078class MeasureView : public Sample {
reed@android.com8a1c16f2008-12-17 15:59:43 +000079public:
80 SkPaint fPaint;
81
rmistry@google.comae933ce2012-08-23 18:19:56 +000082 MeasureView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 fPaint.setAntiAlias(true);
84 fPaint.setTextSize(SkIntToScalar(64));
reed@google.com81e3d7f2011-06-01 12:42:36 +000085 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +000086 }
87
88protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040089 virtual bool onQuery(Sample::Event* evt) {
90 if (Sample::TitleQ(*evt)) {
91 Sample::TitleR(evt, "Measure");
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 return true;
93 }
94 return this->INHERITED::onQuery(evt);
95 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000096
reed@google.com81e3d7f2011-06-01 12:42:36 +000097 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 canvas->translate(fPaint.getTextSize(), fPaint.getTextSize());
99 doMeasure(canvas, fPaint, "Hamburgefons");
100 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000101
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400103 typedef Sample INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104};
105
106//////////////////////////////////////////////////////////////////////////////
107
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400108DEF_SAMPLE( return new MeasureView(); )