1 #include "SampleCode.h"
2 #include "SkView.h"
3 #include "SkCanvas.h"
4 #include "SkTypeface.h"
5 #include "SkPath.h"
6 #include "SkRegion.h"
7 #include "SkShader.h"
8 #include "SkUtils.h"
9 #include "Sk1DPathEffect.h"
10 #include "SkCornerPathEffect.h"
11 #include "SkPathMeasure.h"
12 #include "SkRandom.h"
13 #include "SkColorPriv.h"
14 #include "SkColorFilter.h"
15 #include "SkDither.h"
16 #include "SkTypefaceCache.h"
17
dither_4444(int x)18 static int dither_4444(int x) {
19 return ((x << 1) - ((x >> 4 << 4) | (x >> 4))) >> 4;
20 }
21
22 /** Ensure that the max of the original and dithered value (for alpha) is always
23 >= any other dithered value. We apply this "max" in colorpriv.h when we
24 predither down to 4444, to be sure that we stay in legal premultiplied form
25 */
test_4444_dither()26 static void test_4444_dither() {
27 int buckets[16];
28 sk_bzero(buckets, sizeof(buckets));
29
30 for (int a = 0; a <= 0xFF; a++) {
31 int da = dither_4444(a);
32 int maxa = SkMax32(a >> 4, da);
33 // SkDebugf("--- %02X %X\n", a, da);
34 buckets[da] += 1;
35 for (int c = 0; c <= a; c++) {
36 int dc = dither_4444(c);
37 if (maxa < dc) {
38 SkDebugf("------------ error a=%d da=%d c=%d dc=%d\n", a, da,
39 c, dc);
40 }
41 }
42 }
43 for (int i = 0; i < 16; i++) {
44 // SkDebugf("[%d] = %d\n", i, buckets[i]);
45 }
46 }
47
48 static const struct {
49 const char* fName;
50 SkTypeface::Style fStyle;
51 } gFaces[] = {
52 { "sans-serif", SkTypeface::kNormal },
53 { "sans-serif", SkTypeface::kBold },
54 { "sans-serif", SkTypeface::kItalic },
55 { "sans-serif", SkTypeface::kBoldItalic },
56 { "serif", SkTypeface::kNormal },
57 { "serif", SkTypeface::kBold },
58 { "serif", SkTypeface::kItalic },
59 { "serif", SkTypeface::kBoldItalic },
60 { "monospace", SkTypeface::kNormal },
61 { "monospace", SkTypeface::kBold },
62 { "monospace", SkTypeface::kItalic },
63 { "monospace", SkTypeface::kBoldItalic },
64 };
65
66 static const int gFaceCount = SK_ARRAY_COUNT(gFaces);
67
68 class TypefaceView : public SampleView {
69 SkTypeface* fFaces[gFaceCount];
70
71 public:
TypefaceView()72 TypefaceView() {
73 test_4444_dither();
74 for (int i = 0; i < gFaceCount; i++) {
75 fFaces[i] = SkTypeface::CreateFromName(gFaces[i].fName,
76 gFaces[i].fStyle);
77 }
78
79 this->setBGColor(0xFFDDDDDD);
80 }
81
~TypefaceView()82 virtual ~TypefaceView() {
83 for (int i = 0; i < gFaceCount; i++) {
84 SkSafeUnref(fFaces[i]);
85 }
86
87 SkTypefaceCache::Dump();
88 }
89
90 protected:
91 // overrides from SkEventSink
onQuery(SkEvent * evt)92 virtual bool onQuery(SkEvent* evt) {
93 if (SampleCode::TitleQ(*evt)) {
94 SampleCode::TitleR(evt, "Typefaces");
95 return true;
96 }
97 return this->INHERITED::onQuery(evt);
98 }
99
onDrawContent(SkCanvas * canvas)100 virtual void onDrawContent(SkCanvas* canvas) {
101 SkPaint paint;
102 paint.setAntiAlias(true);
103 paint.setTextSize(SkIntToScalar(30));
104
105 const char* text = "Hamburgefons";
106 const size_t textLen = strlen(text);
107
108 SkScalar x = SkIntToScalar(10);
109 SkScalar dy = paint.getFontMetrics(NULL);
110 SkScalar y = dy;
111
112 paint.setLinearText(true);
113 for (int i = 0; i < gFaceCount; i++) {
114 paint.setTypeface(fFaces[i]);
115 canvas->drawText(text, textLen, x, y, paint);
116 y += dy;
117 }
118 }
119
120 private:
121 typedef SampleView INHERITED;
122 };
123
124 //////////////////////////////////////////////////////////////////////////////
125
MyFactory()126 static SkView* MyFactory() { return new TypefaceView; }
127 static SkViewRegister reg(MyFactory);
128
129