blob: bcb9e4fca02dcc965e06eb022bebc5e0d391e16d [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;
reed@google.com261b8e22011-04-14 17:53:24 +000026 for (size_t i = 0; i < strlen(chars); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000027 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;
reed@google.com261b8e22011-04-14 17:53:24 +000072 for (size_t i = 0; i < strlen(chars); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 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
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000101class StrokeTextView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 bool fAA;
103public:
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000104 StrokeTextView() : fAA(false) {
105 this->setBGColor(0xFFCC8844);
106 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107
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
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000118 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 SkBitmap bm;
120 SkPaint paint;
121
122 paint.setStrokeWidth(SkIntToScalar(6));
123 paint.setTextSize(SkIntToScalar(80));
124// paint.setTypeface(Typeface.DEFAULT_BOLD);
125
126 lettersToBitmap(&bm, "Test Case", paint, SkBitmap::kARGB_4444_Config);
127
128 canvas->drawBitmap(bm, 0, 0);
129 }
130
131private:
132
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000133 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134};
135
136//////////////////////////////////////////////////////////////////////////////
137
138static SkView* MyFactory() { return new StrokeTextView; }
139static SkViewRegister reg(MyFactory);
140