blob: b41166d38ee9e5e6a716a9821bf844dc9995f4a5 [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.com330578d2009-03-09 18:12:13 +00007#include "SampleCode.h"
reed@android.com330578d2009-03-09 18:12:13 +00008#include "Sk1DPathEffect.h"
Ben Wagner71319502017-07-27 10:45:29 -04009#include "SkCanvas.h"
10#include "SkColorFilter.h"
11#include "SkColorPriv.h"
reed@android.com330578d2009-03-09 18:12:13 +000012#include "SkCornerPathEffect.h"
Ben Wagner71319502017-07-27 10:45:29 -040013#include "SkDither.h"
14#include "SkFontStyle.h"
15#include "SkPath.h"
reed@android.com330578d2009-03-09 18:12:13 +000016#include "SkPathMeasure.h"
17#include "SkRandom.h"
Ben Wagner71319502017-07-27 10:45:29 -040018#include "SkRegion.h"
19#include "SkShader.h"
20#include "SkTypeface.h"
21#include "SkUtils.h"
22#include "SkView.h"
reed@android.com330578d2009-03-09 18:12:13 +000023
Ben Wagner71319502017-07-27 10:45:29 -040024static constexpr struct {
reed@android.com330578d2009-03-09 18:12:13 +000025 const char* fName;
Ben Wagner71319502017-07-27 10:45:29 -040026 SkFontStyle fStyle;
reed@android.com330578d2009-03-09 18:12:13 +000027} gFaces[] = {
Ben Wagner71319502017-07-27 10:45:29 -040028 { nullptr, SkFontStyle::Normal() },
29 { nullptr, SkFontStyle::Bold() },
30 { "serif", SkFontStyle::Normal() },
31 { "serif", SkFontStyle::Bold() },
32 { "serif", SkFontStyle::Italic() },
33 { "serif", SkFontStyle::BoldItalic() },
34 { "monospace", SkFontStyle::Normal() }
reed@android.com330578d2009-03-09 18:12:13 +000035};
36
37static const int gFaceCount = SK_ARRAY_COUNT(gFaces);
38
reed@google.com2f3dc9d2011-05-02 17:33:45 +000039class FontScalerTestView : public SampleView {
bungeman13b9c952016-05-12 10:09:30 -070040 sk_sp<SkTypeface> fFaces[gFaceCount];
reed@android.com330578d2009-03-09 18:12:13 +000041
42public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000043 FontScalerTestView() {
reed@android.com330578d2009-03-09 18:12:13 +000044 for (int i = 0; i < gFaceCount; i++) {
Ben Wagner71319502017-07-27 10:45:29 -040045 fFaces[i] = SkTypeface::MakeFromName(gFaces[i].fName, gFaces[i].fStyle);
reed@android.com330578d2009-03-09 18:12:13 +000046 }
47 }
reed@google.com82065d62011-02-07 15:30:46 +000048
reed@android.com330578d2009-03-09 18:12:13 +000049protected:
50 // overrides from SkEventSink
51 virtual bool onQuery(SkEvent* evt) {
52 if (SampleCode::TitleQ(*evt)) {
53 SampleCode::TitleR(evt, "FontScaler Test");
54 return true;
55 }
56 return this->INHERITED::onQuery(evt);
57 }
reed@google.com82065d62011-02-07 15:30:46 +000058
reed@google.com2f3dc9d2011-05-02 17:33:45 +000059 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com35555912009-03-16 18:46:55 +000060 SkPaint paint;
61
reed@android.com452f8442009-03-16 18:47:55 +000062 // test handling of obscene cubic values (currently broken)
63 if (false) {
reed@android.com35555912009-03-16 18:46:55 +000064 SkPoint pts[4];
reed@google.com261b8e22011-04-14 17:53:24 +000065 pts[0].set(1.61061274e+09f, 6291456);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000066 pts[1].set(-7.18397061e+15f,
67 -1.53091184e+13f);
68 pts[2].set(-1.30077315e+16f,
69 -2.77196141e+13f);
70 pts[3].set(-1.30077315e+16f,
71 -2.77196162e+13f);
reed@google.com82065d62011-02-07 15:30:46 +000072
reed@android.com35555912009-03-16 18:46:55 +000073 SkPath path;
74 path.moveTo(pts[0]);
75 path.cubicTo(pts[1], pts[2], pts[3]);
76 canvas->drawPath(path, paint);
77 }
reed@google.com82065d62011-02-07 15:30:46 +000078
reed@google.com00245c92011-08-23 15:30:44 +000079// paint.setSubpixelText(true);
reed@android.com330578d2009-03-09 18:12:13 +000080 paint.setAntiAlias(true);
reed@google.com261b8e22011-04-14 17:53:24 +000081 paint.setLCDRenderText(true);
mbocee6a9912016-05-31 11:42:36 -070082 paint.setTypeface(SkTypeface::MakeFromName("Times Roman", SkFontStyle()));
reed@google.com82065d62011-02-07 15:30:46 +000083
reed@android.com590ef3f2009-03-10 04:02:30 +000084// const char* text = "abcdefghijklmnopqrstuvwxyz";
reed@google.com00245c92011-08-23 15:30:44 +000085 const char* text = "Hamburgefons ooo mmm";
reed@google.com2377b522011-07-13 14:06:19 +000086 const size_t textLen = strlen(text);
reed@android.com330578d2009-03-09 18:12:13 +000087
reed@google.com2377b522011-07-13 14:06:19 +000088 for (int j = 0; j < 2; ++j) {
89 for (int i = 0; i < 6; ++i) {
90 SkScalar x = SkIntToScalar(10);
91 SkScalar y = SkIntToScalar(20);
reed@android.com330578d2009-03-09 18:12:13 +000092
reed@google.com2377b522011-07-13 14:06:19 +000093 SkAutoCanvasRestore acr(canvas, true);
bsalomon@google.com820e80a2011-10-24 21:09:40 +000094 canvas->translate(SkIntToScalar(50 + i * 230),
95 SkIntToScalar(20));
bungeman7438bfc2016-07-12 15:01:19 -070096 canvas->rotate(SkIntToScalar(i * 5), x, y * 10);
reed@google.com82065d62011-02-07 15:30:46 +000097
reed@google.com2377b522011-07-13 14:06:19 +000098 {
99 SkPaint p;
100 p.setAntiAlias(true);
101 SkRect r;
102 r.set(x-3, 15, x-1, 280);
103 canvas->drawRect(r, p);
104 }
105
106 int index = 0;
reed@google.com00245c92011-08-23 15:30:44 +0000107 for (int ps = 6; ps <= 22; ps++) {
reed@google.com2377b522011-07-13 14:06:19 +0000108 paint.setTextSize(SkIntToScalar(ps));
109 canvas->drawText(text, textLen, x, y, paint);
halcanary96fcdcc2015-08-27 07:41:13 -0700110 y += paint.getFontMetrics(nullptr);
reed@google.com2377b522011-07-13 14:06:19 +0000111 index += 1;
112 }
113 }
114 canvas->translate(0, 400);
reed@google.com00245c92011-08-23 15:30:44 +0000115 paint.setSubpixelText(true);
reed@android.com330578d2009-03-09 18:12:13 +0000116 }
117 }
reed@google.com82065d62011-02-07 15:30:46 +0000118
reed@android.com330578d2009-03-09 18:12:13 +0000119private:
120 typedef SkView INHERITED;
121};
122
123//////////////////////////////////////////////////////////////////////////////
124
125static SkView* MyFactory() { return new FontScalerTestView; }
126static SkViewRegister reg(MyFactory);