blob: 2b4a46d00c100839c75a21a90e662c01c4bd058a [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"
8#include "SkView.h"
9#include "SkCanvas.h"
10#include "SkTypeface.h"
11#include "SkPath.h"
12#include "SkRegion.h"
13#include "SkShader.h"
14#include "SkUtils.h"
15#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
23static const struct {
24 const char* fName;
25 SkTypeface::Style fStyle;
26} gFaces[] = {
halcanary96fcdcc2015-08-27 07:41:13 -070027 { nullptr, SkTypeface::kNormal },
28 { nullptr, SkTypeface::kBold },
reed@android.com330578d2009-03-09 18:12:13 +000029 { "serif", SkTypeface::kNormal },
30 { "serif", SkTypeface::kBold },
31 { "serif", SkTypeface::kItalic },
32 { "serif", SkTypeface::kBoldItalic },
33 { "monospace", SkTypeface::kNormal }
34};
35
36static const int gFaceCount = SK_ARRAY_COUNT(gFaces);
37
reed@google.com2f3dc9d2011-05-02 17:33:45 +000038class FontScalerTestView : public SampleView {
bungeman13b9c952016-05-12 10:09:30 -070039 sk_sp<SkTypeface> fFaces[gFaceCount];
reed@android.com330578d2009-03-09 18:12:13 +000040
41public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000042 FontScalerTestView() {
reed@android.com330578d2009-03-09 18:12:13 +000043 for (int i = 0; i < gFaceCount; i++) {
mbocee6a9912016-05-31 11:42:36 -070044 fFaces[i] = SkTypeface::MakeFromName(
45 gFaces[i].fName, SkFontStyle::FromOldStyle(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);