blob: 0627d51e97e0b06fa82e6f8ccad28ed8ed22a7db [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001#include "SampleCode.h"
2#include "SkView.h"
3#include "SkCanvas.h"
4#include "Sk64.h"
5#include "SkGradientShader.h"
6#include "SkGraphics.h"
7#include "SkImageDecoder.h"
8#include "SkKernel33MaskFilter.h"
9#include "SkPath.h"
10#include "SkRandom.h"
11#include "SkRegion.h"
12#include "SkShader.h"
13#include "SkUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkColorPriv.h"
15#include "SkColorFilter.h"
16#include "SkTime.h"
17#include "SkTypeface.h"
18#include "SkXfermode.h"
19
20static void lettersToBitmap(SkBitmap* dst, const char chars[],
21 const SkPaint& original, SkBitmap::Config config) {
22 SkPath path;
23 SkScalar x = 0;
24 SkScalar width;
25 SkPath p;
26 for (int i = 0; i < strlen(chars); i++) {
27 original.getTextPath(&chars[i], 1, x, 0, &p);
28 path.addPath(p);
29 original.getTextWidths(&chars[i], 1, &width);
30 x += width;
31 }
32 SkRect bounds;
33 path.computeBounds(&bounds, SkPath::kExact_BoundsType);
34 SkScalar sw = -original.getStrokeWidth();
35 bounds.inset(sw, sw);
36 path.offset(-bounds.fLeft, -bounds.fTop);
37 bounds.offset(-bounds.fLeft, -bounds.fTop);
38
39 int w = SkScalarRound(bounds.width());
40 int h = SkScalarRound(bounds.height());
41 SkPaint paint(original);
42 SkBitmap src;
43 src.setConfig(config, w, h);
44 src.allocPixels();
45 src.eraseColor(0);
46 {
47 SkCanvas canvas(src);
48 paint.setAntiAlias(true);
49 paint.setColor(SK_ColorBLACK);
50 paint.setStyle(SkPaint::kFill_Style);
51 canvas.drawPath(path, paint);
52 }
53
54 dst->setConfig(config, w, h);
55 dst->allocPixels();
56 dst->eraseColor(SK_ColorWHITE);
57 {
58 SkCanvas canvas(*dst);
59 paint.setPorterDuffXfermode(SkPorterDuff::kDstATop_Mode);
60 canvas.drawBitmap(src, 0, 0, &paint);
61 paint.setColor(original.getColor());
62 paint.setStyle(SkPaint::kStroke_Style);
63 canvas.drawPath(path, paint);
64 }
65}
66
67static void lettersToBitmap2(SkBitmap* dst, const char chars[],
68 const SkPaint& original, SkBitmap::Config config) {
69 SkPath path;
70 SkScalar x = 0;
71 SkScalar width;
72 SkPath p;
73 for (int i = 0; i < strlen(chars); i++) {
74 original.getTextPath(&chars[i], 1, x, 0, &p);
75 path.addPath(p);
76 original.getTextWidths(&chars[i], 1, &width);
77 x += width;
78 }
79 SkRect bounds;
80 path.computeBounds(&bounds, SkPath::kExact_BoundsType);
81 SkScalar sw = -original.getStrokeWidth();
82 bounds.inset(sw, sw);
83 path.offset(-bounds.fLeft, -bounds.fTop);
84 bounds.offset(-bounds.fLeft, -bounds.fTop);
85
86 int w = SkScalarRound(bounds.width());
87 int h = SkScalarRound(bounds.height());
88 SkPaint paint(original);
89
90 paint.setAntiAlias(true);
91 paint.setPorterDuffXfermode(SkPorterDuff::kDstATop_Mode);
92 paint.setColor(original.getColor());
93 paint.setStyle(SkPaint::kStroke_Style);
94
95 dst->setConfig(config, w, h);
96 dst->allocPixels();
97 dst->eraseColor(SK_ColorWHITE);
98
99 SkCanvas canvas(*dst);
100 canvas.drawPath(path, paint);
101}
102
103class StrokeTextView : public SkView {
104 bool fAA;
105public:
106 StrokeTextView() : fAA(false) {}
107
108protected:
109 // overrides from SkEventSink
110 virtual bool onQuery(SkEvent* evt) {
111 if (SampleCode::TitleQ(*evt)) {
112 SampleCode::TitleR(evt, "StrokeText");
113 return true;
114 }
115 return this->INHERITED::onQuery(evt);
116 }
117
118 void drawBG(SkCanvas* canvas) {
119 canvas->drawColor(0xFF333333);
120 canvas->drawColor(0xFFCC8844);
121 }
122
123 virtual void onDraw(SkCanvas* canvas) {
124 this->drawBG(canvas);
125
126 SkBitmap bm;
127 SkPaint paint;
128
129 paint.setStrokeWidth(SkIntToScalar(6));
130 paint.setTextSize(SkIntToScalar(80));
131// paint.setTypeface(Typeface.DEFAULT_BOLD);
132
133 lettersToBitmap(&bm, "Test Case", paint, SkBitmap::kARGB_4444_Config);
134
135 canvas->drawBitmap(bm, 0, 0);
136 }
137
138private:
139
140 typedef SkView INHERITED;
141};
142
143//////////////////////////////////////////////////////////////////////////////
144
145static SkView* MyFactory() { return new StrokeTextView; }
146static SkViewRegister reg(MyFactory);
147