blob: a01de035563c005af19ff73bda87737b0cc00771 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com330578d2009-03-09 18:12:13 +00008#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
24static 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
37static const int gFaceCount = SK_ARRAY_COUNT(gFaces);
38
reed@google.com2f3dc9d2011-05-02 17:33:45 +000039class FontScalerTestView : public SampleView {
reed@android.com330578d2009-03-09 18:12:13 +000040 SkTypeface* fFaces[gFaceCount];
41
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++) {
45 fFaces[i] = SkTypeface::CreateFromName(gFaces[i].fName,
46 gFaces[i].fStyle);
47 }
reed@google.com00245c92011-08-23 15:30:44 +000048// this->setBGColor(0xFFDDDDDD);
reed@android.com330578d2009-03-09 18:12:13 +000049 }
reed@google.com82065d62011-02-07 15:30:46 +000050
reed@android.com330578d2009-03-09 18:12:13 +000051 virtual ~FontScalerTestView() {
52 for (int i = 0; i < gFaceCount; i++) {
reed@google.com82065d62011-02-07 15:30:46 +000053 SkSafeUnref(fFaces[i]);
reed@android.com330578d2009-03-09 18:12:13 +000054 }
55 }
56
57protected:
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.com82065d62011-02-07 15:30:46 +000066
reed@google.com2377b522011-07-13 14:06:19 +000067 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.com2f3dc9d2011-05-02 17:33:45 +000073 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com35555912009-03-16 18:46:55 +000074 SkPaint paint;
75
reed@android.com452f8442009-03-16 18:47:55 +000076 // test handling of obscene cubic values (currently broken)
77 if (false) {
reed@android.com35555912009-03-16 18:46:55 +000078 SkPoint pts[4];
reed@google.com261b8e22011-04-14 17:53:24 +000079 pts[0].set(1.61061274e+09f, 6291456);
rmistry@google.comae933ce2012-08-23 18:19:56 +000080 pts[1].set(SkFloatToScalar(-7.18397061e+15f),
robertphillips@google.com7460b372012-04-25 16:54:51 +000081 SkFloatToScalar(-1.53091184e+13f));
rmistry@google.comae933ce2012-08-23 18:19:56 +000082 pts[2].set(SkFloatToScalar(-1.30077315e+16f),
robertphillips@google.com7460b372012-04-25 16:54:51 +000083 SkFloatToScalar(-2.77196141e+13f));
rmistry@google.comae933ce2012-08-23 18:19:56 +000084 pts[3].set(SkFloatToScalar(-1.30077315e+16f),
robertphillips@google.com7460b372012-04-25 16:54:51 +000085 SkFloatToScalar(-2.77196162e+13f));
reed@google.com82065d62011-02-07 15:30:46 +000086
reed@android.com35555912009-03-16 18:46:55 +000087 SkPath path;
88 path.moveTo(pts[0]);
89 path.cubicTo(pts[1], pts[2], pts[3]);
90 canvas->drawPath(path, paint);
91 }
reed@google.com82065d62011-02-07 15:30:46 +000092
reed@google.com00245c92011-08-23 15:30:44 +000093// paint.setSubpixelText(true);
reed@android.com330578d2009-03-09 18:12:13 +000094 paint.setAntiAlias(true);
reed@google.com261b8e22011-04-14 17:53:24 +000095 paint.setLCDRenderText(true);
reed@google.com82065d62011-02-07 15:30:46 +000096 SkSafeUnref(paint.setTypeface(SkTypeface::CreateFromName("Times Roman", SkTypeface::kNormal)));
97
reed@android.com590ef3f2009-03-10 04:02:30 +000098// const char* text = "abcdefghijklmnopqrstuvwxyz";
reed@google.com00245c92011-08-23 15:30:44 +000099 const char* text = "Hamburgefons ooo mmm";
reed@google.com2377b522011-07-13 14:06:19 +0000100 const size_t textLen = strlen(text);
reed@android.com330578d2009-03-09 18:12:13 +0000101
reed@google.com2377b522011-07-13 14:06:19 +0000102 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.com330578d2009-03-09 18:12:13 +0000106
reed@google.com2377b522011-07-13 14:06:19 +0000107 SkAutoCanvasRestore acr(canvas, true);
bsalomon@google.com820e80a2011-10-24 21:09:40 +0000108 canvas->translate(SkIntToScalar(50 + i * 230),
109 SkIntToScalar(20));
robertphillips@google.com09042b82012-04-06 20:01:46 +0000110 rotate_about(canvas, SkIntToScalar(i * 5), x, y * 10);
reed@google.com82065d62011-02-07 15:30:46 +0000111
reed@google.com2377b522011-07-13 14:06:19 +0000112 {
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.com00245c92011-08-23 15:30:44 +0000121 for (int ps = 6; ps <= 22; ps++) {
reed@google.com2377b522011-07-13 14:06:19 +0000122 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.com00245c92011-08-23 15:30:44 +0000129 paint.setSubpixelText(true);
reed@android.com330578d2009-03-09 18:12:13 +0000130 }
131 }
reed@google.com82065d62011-02-07 15:30:46 +0000132
reed@android.com330578d2009-03-09 18:12:13 +0000133private:
134 typedef SkView INHERITED;
135};
136
137//////////////////////////////////////////////////////////////////////////////
138
139static SkView* MyFactory() { return new FontScalerTestView; }
140static SkViewRegister reg(MyFactory);