blob: a527bd26ad11d54e7b673ae0409784e0f44b3d38 [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 }
reed@android.comd252db02009-04-01 18:31:44 +000032 SkRect bounds = path.getBounds();
reed@android.com8a1c16f2008-12-17 15:59:43 +000033 SkScalar sw = -original.getStrokeWidth();
34 bounds.inset(sw, sw);
35 path.offset(-bounds.fLeft, -bounds.fTop);
36 bounds.offset(-bounds.fLeft, -bounds.fTop);
37
38 int w = SkScalarRound(bounds.width());
39 int h = SkScalarRound(bounds.height());
40 SkPaint paint(original);
41 SkBitmap src;
42 src.setConfig(config, w, h);
43 src.allocPixels();
44 src.eraseColor(0);
45 {
46 SkCanvas canvas(src);
47 paint.setAntiAlias(true);
48 paint.setColor(SK_ColorBLACK);
49 paint.setStyle(SkPaint::kFill_Style);
50 canvas.drawPath(path, paint);
51 }
52
53 dst->setConfig(config, w, h);
54 dst->allocPixels();
55 dst->eraseColor(SK_ColorWHITE);
56 {
57 SkCanvas canvas(*dst);
reed@android.com0baf1932009-06-24 12:41:42 +000058 paint.setXfermodeMode(SkXfermode::kDstATop_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 canvas.drawBitmap(src, 0, 0, &paint);
60 paint.setColor(original.getColor());
61 paint.setStyle(SkPaint::kStroke_Style);
62 canvas.drawPath(path, paint);
63 }
64}
65
66static void lettersToBitmap2(SkBitmap* dst, const char chars[],
67 const SkPaint& original, SkBitmap::Config config) {
68 SkPath path;
69 SkScalar x = 0;
70 SkScalar width;
71 SkPath p;
72 for (int i = 0; i < strlen(chars); i++) {
73 original.getTextPath(&chars[i], 1, x, 0, &p);
74 path.addPath(p);
75 original.getTextWidths(&chars[i], 1, &width);
76 x += width;
77 }
reed@android.comd252db02009-04-01 18:31:44 +000078 SkRect bounds = path.getBounds();
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 SkScalar sw = -original.getStrokeWidth();
80 bounds.inset(sw, sw);
81 path.offset(-bounds.fLeft, -bounds.fTop);
82 bounds.offset(-bounds.fLeft, -bounds.fTop);
83
84 int w = SkScalarRound(bounds.width());
85 int h = SkScalarRound(bounds.height());
86 SkPaint paint(original);
87
88 paint.setAntiAlias(true);
reed@android.com0baf1932009-06-24 12:41:42 +000089 paint.setXfermodeMode(SkXfermode::kDstATop_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 paint.setColor(original.getColor());
91 paint.setStyle(SkPaint::kStroke_Style);
92
93 dst->setConfig(config, w, h);
94 dst->allocPixels();
95 dst->eraseColor(SK_ColorWHITE);
96
97 SkCanvas canvas(*dst);
98 canvas.drawPath(path, paint);
99}
100
101class StrokeTextView : public SkView {
102 bool fAA;
103public:
104 StrokeTextView() : fAA(false) {}
105
106protected:
107 // overrides from SkEventSink
108 virtual bool onQuery(SkEvent* evt) {
109 if (SampleCode::TitleQ(*evt)) {
110 SampleCode::TitleR(evt, "StrokeText");
111 return true;
112 }
113 return this->INHERITED::onQuery(evt);
114 }
115
116 void drawBG(SkCanvas* canvas) {
117 canvas->drawColor(0xFF333333);
118 canvas->drawColor(0xFFCC8844);
119 }
120
121 virtual void onDraw(SkCanvas* canvas) {
122 this->drawBG(canvas);
123
124 SkBitmap bm;
125 SkPaint paint;
126
127 paint.setStrokeWidth(SkIntToScalar(6));
128 paint.setTextSize(SkIntToScalar(80));
129// paint.setTypeface(Typeface.DEFAULT_BOLD);
130
131 lettersToBitmap(&bm, "Test Case", paint, SkBitmap::kARGB_4444_Config);
132
133 canvas->drawBitmap(bm, 0, 0);
134 }
135
136private:
137
138 typedef SkView INHERITED;
139};
140
141//////////////////////////////////////////////////////////////////////////////
142
143static SkView* MyFactory() { return new StrokeTextView; }
144static SkViewRegister reg(MyFactory);
145