blob: 2a4817e418398f3bfe6abd2f3c90210abc0251c9 [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
Mike Reed16d91aa2018-11-02 21:25:57 -040021// exercise scale/linear
reed@android.com8a1c16f2008-12-17 15:59:43 +000022struct Setting {
reed@android.com8a1c16f2008-12-17 15:59:43 +000023 bool fLinearText;
reed@android.com8a1c16f2008-12-17 15:59:43 +000024};
25
reed@android.com8a1c16f2008-12-17 15:59:43 +000026static const Setting gSettings[] = {
Mike Reed16d91aa2018-11-02 21:25:57 -040027 { false },
28 { true },
reed@android.com8a1c16f2008-12-17 15:59:43 +000029};
30
reed@google.com81e3d7f2011-06-01 12:42:36 +000031static void doMeasure(SkCanvas* canvas, const SkPaint& paint, const char text[]) {
halcanary96fcdcc2015-08-27 07:41:13 -070032 SkScalar dy = paint.getFontMetrics(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +000033
34 size_t len = strlen(text);
35 SkAutoTMalloc<SkScalar> autoWidths(len);
36 SkScalar* widths = autoWidths.get();
37 SkAutoTMalloc<SkRect> autoRects(len);
38 SkRect* rects = autoRects.get();
39 SkRect bounds;
40
41 SkPaint p(paint);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000042 for (size_t i = 0; i < SK_ARRAY_COUNT(gSettings); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 p.setLinearText(gSettings[i].fLinearText);
rmistry@google.comae933ce2012-08-23 18:19:56 +000044
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 int n = p.getTextWidths(text, len, widths, rects);
reed99ae8812014-08-26 11:30:01 -070046 SkScalar w = p.measureText(text, len, &bounds);
rmistry@google.comae933ce2012-08-23 18:19:56 +000047
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 p.setStyle(SkPaint::kFill_Style);
49 p.setColor(0x8888FF88);
50 canvas->drawRect(bounds, p);
51 p.setColor(0xFF000000);
52 canvas->drawText(text, len, 0, 0, p);
53
54 p.setStyle(SkPaint::kStroke_Style);
55 p.setStrokeWidth(0);
56 p.setColor(0xFFFF0000);
57 SkScalar x = 0;
58 for (int j = 0; j < n; j++) {
59 SkRect r = rects[j];
60 r.offset(x, 0);
61 canvas->drawRect(r, p);
62 x += widths[j];
63 }
64
65 p.setColor(0xFF0000FF);
66 canvas->drawLine(0, 0, w, 0, p);
67 p.setStrokeWidth(SkIntToScalar(4));
68 canvas->drawPoint(x, 0, p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000069
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 canvas->translate(0, dy);
71 }
72}
73
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040074class MeasureView : public Sample {
reed@android.com8a1c16f2008-12-17 15:59:43 +000075public:
76 SkPaint fPaint;
77
rmistry@google.comae933ce2012-08-23 18:19:56 +000078 MeasureView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 fPaint.setAntiAlias(true);
80 fPaint.setTextSize(SkIntToScalar(64));
reed@google.com81e3d7f2011-06-01 12:42:36 +000081 this->setBGColor(0xFFDDDDDD);
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 }
83
84protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040085 virtual bool onQuery(Sample::Event* evt) {
86 if (Sample::TitleQ(*evt)) {
87 Sample::TitleR(evt, "Measure");
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 return true;
89 }
90 return this->INHERITED::onQuery(evt);
91 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000092
reed@google.com81e3d7f2011-06-01 12:42:36 +000093 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 canvas->translate(fPaint.getTextSize(), fPaint.getTextSize());
95 doMeasure(canvas, fPaint, "Hamburgefons");
96 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000097
reed@android.com8a1c16f2008-12-17 15:59:43 +000098private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040099 typedef Sample INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100};
101
102//////////////////////////////////////////////////////////////////////////////
103
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400104DEF_SAMPLE( return new MeasureView(); )