epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkView.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkTypeface.h" |
| 12 | #include "SkPath.h" |
| 13 | #include "SkRegion.h" |
| 14 | #include "SkShader.h" |
| 15 | #include "SkUtils.h" |
| 16 | #include "Sk1DPathEffect.h" |
| 17 | #include "SkCornerPathEffect.h" |
| 18 | #include "SkPathMeasure.h" |
| 19 | #include "SkRandom.h" |
| 20 | #include "SkColorPriv.h" |
| 21 | #include "SkColorFilter.h" |
| 22 | #include "SkDither.h" |
| 23 | |
| 24 | static const struct { |
| 25 | const char* fName; |
| 26 | SkTypeface::Style fStyle; |
| 27 | } gFaces[] = { |
| 28 | { NULL, SkTypeface::kNormal }, |
| 29 | { NULL, SkTypeface::kBold }, |
| 30 | { "serif", SkTypeface::kNormal }, |
| 31 | { "serif", SkTypeface::kBold }, |
| 32 | { "serif", SkTypeface::kItalic }, |
| 33 | { "serif", SkTypeface::kBoldItalic }, |
| 34 | { "monospace", SkTypeface::kNormal } |
| 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 { |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 40 | SkTypeface* fFaces[gFaceCount]; |
| 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++) { |
| 45 | fFaces[i] = SkTypeface::CreateFromName(gFaces[i].fName, |
| 46 | gFaces[i].fStyle); |
| 47 | } |
reed@google.com | 00245c9 | 2011-08-23 15:30:44 +0000 | [diff] [blame] | 48 | // this->setBGColor(0xFFDDDDDD); |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 49 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 50 | |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 51 | virtual ~FontScalerTestView() { |
| 52 | for (int i = 0; i < gFaceCount; i++) { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 53 | SkSafeUnref(fFaces[i]); |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
| 57 | protected: |
| 58 | // overrides from SkEventSink |
| 59 | virtual bool onQuery(SkEvent* evt) { |
| 60 | if (SampleCode::TitleQ(*evt)) { |
| 61 | SampleCode::TitleR(evt, "FontScaler Test"); |
| 62 | return true; |
| 63 | } |
| 64 | return this->INHERITED::onQuery(evt); |
| 65 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 66 | |
reed@google.com | 2377b52 | 2011-07-13 14:06:19 +0000 | [diff] [blame] | 67 | static void rotate_about(SkCanvas* canvas, SkScalar degrees, SkScalar px, SkScalar py) { |
| 68 | canvas->translate(px, py); |
| 69 | canvas->rotate(degrees); |
| 70 | canvas->translate(-px, -py); |
| 71 | } |
| 72 | |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 73 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 3555591 | 2009-03-16 18:46:55 +0000 | [diff] [blame] | 74 | SkPaint paint; |
| 75 | |
reed@android.com | 452f844 | 2009-03-16 18:47:55 +0000 | [diff] [blame] | 76 | // test handling of obscene cubic values (currently broken) |
| 77 | if (false) { |
reed@android.com | 3555591 | 2009-03-16 18:46:55 +0000 | [diff] [blame] | 78 | SkPoint pts[4]; |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 79 | pts[0].set(1.61061274e+09f, 6291456); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 80 | pts[1].set(SkFloatToScalar(-7.18397061e+15f), |
robertphillips@google.com | 7460b37 | 2012-04-25 16:54:51 +0000 | [diff] [blame] | 81 | SkFloatToScalar(-1.53091184e+13f)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 82 | pts[2].set(SkFloatToScalar(-1.30077315e+16f), |
robertphillips@google.com | 7460b37 | 2012-04-25 16:54:51 +0000 | [diff] [blame] | 83 | SkFloatToScalar(-2.77196141e+13f)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 84 | pts[3].set(SkFloatToScalar(-1.30077315e+16f), |
robertphillips@google.com | 7460b37 | 2012-04-25 16:54:51 +0000 | [diff] [blame] | 85 | SkFloatToScalar(-2.77196162e+13f)); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 86 | |
reed@android.com | 3555591 | 2009-03-16 18:46:55 +0000 | [diff] [blame] | 87 | SkPath path; |
| 88 | path.moveTo(pts[0]); |
| 89 | path.cubicTo(pts[1], pts[2], pts[3]); |
| 90 | canvas->drawPath(path, paint); |
| 91 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 92 | |
reed@google.com | 00245c9 | 2011-08-23 15:30:44 +0000 | [diff] [blame] | 93 | // paint.setSubpixelText(true); |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 94 | paint.setAntiAlias(true); |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 95 | paint.setLCDRenderText(true); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 96 | SkSafeUnref(paint.setTypeface(SkTypeface::CreateFromName("Times Roman", SkTypeface::kNormal))); |
| 97 | |
reed@android.com | 590ef3f | 2009-03-10 04:02:30 +0000 | [diff] [blame] | 98 | // const char* text = "abcdefghijklmnopqrstuvwxyz"; |
reed@google.com | 00245c9 | 2011-08-23 15:30:44 +0000 | [diff] [blame] | 99 | const char* text = "Hamburgefons ooo mmm"; |
reed@google.com | 2377b52 | 2011-07-13 14:06:19 +0000 | [diff] [blame] | 100 | const size_t textLen = strlen(text); |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 101 | |
reed@google.com | 2377b52 | 2011-07-13 14:06:19 +0000 | [diff] [blame] | 102 | for (int j = 0; j < 2; ++j) { |
| 103 | for (int i = 0; i < 6; ++i) { |
| 104 | SkScalar x = SkIntToScalar(10); |
| 105 | SkScalar y = SkIntToScalar(20); |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 106 | |
reed@google.com | 2377b52 | 2011-07-13 14:06:19 +0000 | [diff] [blame] | 107 | SkAutoCanvasRestore acr(canvas, true); |
bsalomon@google.com | 820e80a | 2011-10-24 21:09:40 +0000 | [diff] [blame] | 108 | canvas->translate(SkIntToScalar(50 + i * 230), |
| 109 | SkIntToScalar(20)); |
robertphillips@google.com | 09042b8 | 2012-04-06 20:01:46 +0000 | [diff] [blame] | 110 | rotate_about(canvas, SkIntToScalar(i * 5), x, y * 10); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 111 | |
reed@google.com | 2377b52 | 2011-07-13 14:06:19 +0000 | [diff] [blame] | 112 | { |
| 113 | SkPaint p; |
| 114 | p.setAntiAlias(true); |
| 115 | SkRect r; |
| 116 | r.set(x-3, 15, x-1, 280); |
| 117 | canvas->drawRect(r, p); |
| 118 | } |
| 119 | |
| 120 | int index = 0; |
reed@google.com | 00245c9 | 2011-08-23 15:30:44 +0000 | [diff] [blame] | 121 | for (int ps = 6; ps <= 22; ps++) { |
reed@google.com | 2377b52 | 2011-07-13 14:06:19 +0000 | [diff] [blame] | 122 | paint.setTextSize(SkIntToScalar(ps)); |
| 123 | canvas->drawText(text, textLen, x, y, paint); |
| 124 | y += paint.getFontMetrics(NULL); |
| 125 | index += 1; |
| 126 | } |
| 127 | } |
| 128 | canvas->translate(0, 400); |
reed@google.com | 00245c9 | 2011-08-23 15:30:44 +0000 | [diff] [blame] | 129 | paint.setSubpixelText(true); |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 132 | |
reed@android.com | 330578d | 2009-03-09 18:12:13 +0000 | [diff] [blame] | 133 | private: |
| 134 | typedef SkView INHERITED; |
| 135 | }; |
| 136 | |
| 137 | ////////////////////////////////////////////////////////////////////////////// |
| 138 | |
| 139 | static SkView* MyFactory() { return new FontScalerTestView; } |
| 140 | static SkViewRegister reg(MyFactory); |