blob: 00943baf0586640d8305868a2cb6f6b14f1fe29b [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"
20#include "SkDither.h"
21
22// exercise scale/linear/devkern
23struct Setting {
reed@android.com8a1c16f2008-12-17 15:59:43 +000024 bool fLinearText;
25 bool fDevKernText;
26};
27
reed@android.com8a1c16f2008-12-17 15:59:43 +000028static const Setting gSettings[] = {
reed99ae8812014-08-26 11:30:01 -070029 { false, false },
30 { false, true },
31 { true, false },
32 { true, true },
reed@android.com8a1c16f2008-12-17 15:59:43 +000033};
34
reed@google.com81e3d7f2011-06-01 12:42:36 +000035static void doMeasure(SkCanvas* canvas, const SkPaint& paint, const char text[]) {
halcanary96fcdcc2015-08-27 07:41:13 -070036 SkScalar dy = paint.getFontMetrics(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +000037
38 size_t len = strlen(text);
39 SkAutoTMalloc<SkScalar> autoWidths(len);
40 SkScalar* widths = autoWidths.get();
41 SkAutoTMalloc<SkRect> autoRects(len);
42 SkRect* rects = autoRects.get();
43 SkRect bounds;
44
45 SkPaint p(paint);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000046 for (size_t i = 0; i < SK_ARRAY_COUNT(gSettings); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 p.setLinearText(gSettings[i].fLinearText);
48 p.setDevKernText(gSettings[i].fDevKernText);
rmistry@google.comae933ce2012-08-23 18:19:56 +000049
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 int n = p.getTextWidths(text, len, widths, rects);
reed99ae8812014-08-26 11:30:01 -070051 SkScalar w = p.measureText(text, len, &bounds);
rmistry@google.comae933ce2012-08-23 18:19:56 +000052
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 p.setStyle(SkPaint::kFill_Style);
54 p.setColor(0x8888FF88);
55 canvas->drawRect(bounds, p);
56 p.setColor(0xFF000000);
57 canvas->drawText(text, len, 0, 0, p);
58
59 p.setStyle(SkPaint::kStroke_Style);
60 p.setStrokeWidth(0);
61 p.setColor(0xFFFF0000);
62 SkScalar x = 0;
63 for (int j = 0; j < n; j++) {
64 SkRect r = rects[j];
65 r.offset(x, 0);
66 canvas->drawRect(r, p);
67 x += widths[j];
68 }
69
70 p.setColor(0xFF0000FF);
71 canvas->drawLine(0, 0, w, 0, p);
72 p.setStrokeWidth(SkIntToScalar(4));
73 canvas->drawPoint(x, 0, p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000074
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 canvas->translate(0, dy);
76 }
77}
78
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040079class MeasureView : public Sample {
reed@android.com8a1c16f2008-12-17 15:59:43 +000080public:
81 SkPaint fPaint;
82
rmistry@google.comae933ce2012-08-23 18:19:56 +000083 MeasureView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 fPaint.setAntiAlias(true);
85 fPaint.setTextSize(SkIntToScalar(64));
reed@google.com81e3d7f2011-06-01 12:42:36 +000086 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 }
88
89protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040090 virtual bool onQuery(Sample::Event* evt) {
91 if (Sample::TitleQ(*evt)) {
92 Sample::TitleR(evt, "Measure");
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 return true;
94 }
95 return this->INHERITED::onQuery(evt);
96 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000097
reed@google.com81e3d7f2011-06-01 12:42:36 +000098 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 canvas->translate(fPaint.getTextSize(), fPaint.getTextSize());
100 doMeasure(canvas, fPaint, "Hamburgefons");
101 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000102
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400104 typedef Sample INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105};
106
107//////////////////////////////////////////////////////////////////////////////
108
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400109DEF_SAMPLE( return new MeasureView(); )