epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 7 | #include "SampleCode.h" |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 8 | #include "Sk1DPathEffect.h" |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 9 | #include "SkCanvas.h" |
| 10 | #include "SkColorFilter.h" |
| 11 | #include "SkColorPriv.h" |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 12 | #include "SkCornerPathEffect.h" |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 13 | #include "SkDither.h" |
| 14 | #include "SkFontStyle.h" |
| 15 | #include "SkPath.h" |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 16 | #include "SkPathMeasure.h" |
| 17 | #include "SkRandom.h" |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 18 | #include "SkRegion.h" |
| 19 | #include "SkShader.h" |
| 20 | #include "SkTypeface.h" |
| 21 | #include "SkUtils.h" |
| 22 | #include "SkView.h" |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 23 | |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 24 | static constexpr struct { |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 25 | const char* fName; |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 26 | SkFontStyle fStyle; |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 27 | } gFaces[] = { |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 28 | { 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.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | static const int gFaceCount = SK_ARRAY_COUNT(gFaces); |
| 38 | |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 39 | class FontScalerTestView : public SampleView { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 40 | sk_sp<SkTypeface> fFaces[gFaceCount]; |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 41 | |
| 42 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 43 | FontScalerTestView() { |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 44 | for (int i = 0; i < gFaceCount; i++) { |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 45 | fFaces[i] = SkTypeface::MakeFromName(gFaces[i].fName, gFaces[i].fStyle); |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 46 | } |
| 47 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 48 | |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 49 | protected: |
| 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.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 58 | |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 59 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 3555591 | 2009-03-16 18:46:55 +0000 | [diff] [blame] | 60 | SkPaint paint; |
| 61 | |
reed@android.com | 452f844 | 2009-03-16 18:47:55 +0000 | [diff] [blame] | 62 | // test handling of obscene cubic values (currently broken) |
| 63 | if (false) { |
reed@android.com | 3555591 | 2009-03-16 18:46:55 +0000 | [diff] [blame] | 64 | SkPoint pts[4]; |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 65 | pts[0].set(1.61061274e+09f, 6291456); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 66 | 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.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 72 | |
reed@android.com | 3555591 | 2009-03-16 18:46:55 +0000 | [diff] [blame] | 73 | SkPath path; |
| 74 | path.moveTo(pts[0]); |
| 75 | path.cubicTo(pts[1], pts[2], pts[3]); |
| 76 | canvas->drawPath(path, paint); |
| 77 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 78 | |
reed@google.com | 00245c9 | 2011-08-23 15:30:44 +0000 | [diff] [blame] | 79 | // paint.setSubpixelText(true); |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 80 | paint.setAntiAlias(true); |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 81 | paint.setLCDRenderText(true); |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 82 | paint.setTypeface(SkTypeface::MakeFromName("Times Roman", SkFontStyle())); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 83 | |
reed@android.com | 590ef3f | 2009-03-10 04:02:30 +0000 | [diff] [blame] | 84 | // const char* text = "abcdefghijklmnopqrstuvwxyz"; |
reed@google.com | 00245c9 | 2011-08-23 15:30:44 +0000 | [diff] [blame] | 85 | const char* text = "Hamburgefons ooo mmm"; |
reed@google.com | 2377b52 | 2011-07-13 14:06:19 +0000 | [diff] [blame] | 86 | const size_t textLen = strlen(text); |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 87 | |
reed@google.com | 2377b52 | 2011-07-13 14:06:19 +0000 | [diff] [blame] | 88 | 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.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 92 | |
reed@google.com | 2377b52 | 2011-07-13 14:06:19 +0000 | [diff] [blame] | 93 | SkAutoCanvasRestore acr(canvas, true); |
bsalomon@google.com | 820e80a | 2011-10-24 21:09:40 +0000 | [diff] [blame] | 94 | canvas->translate(SkIntToScalar(50 + i * 230), |
| 95 | SkIntToScalar(20)); |
bungeman | 7438bfc | 2016-07-12 15:01:19 -0700 | [diff] [blame] | 96 | canvas->rotate(SkIntToScalar(i * 5), x, y * 10); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 97 | |
reed@google.com | 2377b52 | 2011-07-13 14:06:19 +0000 | [diff] [blame] | 98 | { |
| 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.com | 00245c9 | 2011-08-23 15:30:44 +0000 | [diff] [blame] | 107 | for (int ps = 6; ps <= 22; ps++) { |
reed@google.com | 2377b52 | 2011-07-13 14:06:19 +0000 | [diff] [blame] | 108 | paint.setTextSize(SkIntToScalar(ps)); |
| 109 | canvas->drawText(text, textLen, x, y, paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 110 | y += paint.getFontMetrics(nullptr); |
reed@google.com | 2377b52 | 2011-07-13 14:06:19 +0000 | [diff] [blame] | 111 | index += 1; |
| 112 | } |
| 113 | } |
| 114 | canvas->translate(0, 400); |
reed@google.com | 00245c9 | 2011-08-23 15:30:44 +0000 | [diff] [blame] | 115 | paint.setSubpixelText(true); |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 118 | |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 119 | private: |
| 120 | typedef SkView INHERITED; |
| 121 | }; |
| 122 | |
| 123 | ////////////////////////////////////////////////////////////////////////////// |
| 124 | |
| 125 | static SkView* MyFactory() { return new FontScalerTestView; } |
| 126 | static SkViewRegister reg(MyFactory); |