blob: 4e9fe0d59fe09ca1bec3baebb4989f268858c1b8 [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.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "Sk64.h"
12#include "SkGradientShader.h"
13#include "SkGraphics.h"
14#include "SkImageDecoder.h"
15#include "SkKernel33MaskFilter.h"
16#include "SkPath.h"
17#include "SkRandom.h"
18#include "SkRegion.h"
19#include "SkShader.h"
20#include "SkUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000021#include "SkColorPriv.h"
22#include "SkColorFilter.h"
23#include "SkTime.h"
24#include "SkTypeface.h"
25#include "SkXfermode.h"
26
27static void lettersToBitmap(SkBitmap* dst, const char chars[],
28 const SkPaint& original, SkBitmap::Config config) {
29 SkPath path;
30 SkScalar x = 0;
31 SkScalar width;
32 SkPath p;
reed@google.com261b8e22011-04-14 17:53:24 +000033 for (size_t i = 0; i < strlen(chars); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000034 original.getTextPath(&chars[i], 1, x, 0, &p);
35 path.addPath(p);
36 original.getTextWidths(&chars[i], 1, &width);
37 x += width;
38 }
reed@android.comd252db02009-04-01 18:31:44 +000039 SkRect bounds = path.getBounds();
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 SkScalar sw = -original.getStrokeWidth();
41 bounds.inset(sw, sw);
42 path.offset(-bounds.fLeft, -bounds.fTop);
43 bounds.offset(-bounds.fLeft, -bounds.fTop);
rmistry@google.comae933ce2012-08-23 18:19:56 +000044
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 int w = SkScalarRound(bounds.width());
46 int h = SkScalarRound(bounds.height());
47 SkPaint paint(original);
48 SkBitmap src;
49 src.setConfig(config, w, h);
50 src.allocPixels();
junov@google.comdbfac8a2012-12-06 21:47:40 +000051 src.eraseColor(SK_ColorTRANSPARENT);
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 {
53 SkCanvas canvas(src);
54 paint.setAntiAlias(true);
55 paint.setColor(SK_ColorBLACK);
56 paint.setStyle(SkPaint::kFill_Style);
57 canvas.drawPath(path, paint);
58 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000059
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 dst->setConfig(config, w, h);
61 dst->allocPixels();
62 dst->eraseColor(SK_ColorWHITE);
63 {
64 SkCanvas canvas(*dst);
reed@android.com0baf1932009-06-24 12:41:42 +000065 paint.setXfermodeMode(SkXfermode::kDstATop_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 canvas.drawBitmap(src, 0, 0, &paint);
67 paint.setColor(original.getColor());
68 paint.setStyle(SkPaint::kStroke_Style);
69 canvas.drawPath(path, paint);
70 }
71}
72
73static void lettersToBitmap2(SkBitmap* dst, const char chars[],
74 const SkPaint& original, SkBitmap::Config config) {
75 SkPath path;
76 SkScalar x = 0;
77 SkScalar width;
78 SkPath p;
reed@google.com261b8e22011-04-14 17:53:24 +000079 for (size_t i = 0; i < strlen(chars); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 original.getTextPath(&chars[i], 1, x, 0, &p);
81 path.addPath(p);
82 original.getTextWidths(&chars[i], 1, &width);
83 x += width;
84 }
reed@android.comd252db02009-04-01 18:31:44 +000085 SkRect bounds = path.getBounds();
reed@android.com8a1c16f2008-12-17 15:59:43 +000086 SkScalar sw = -original.getStrokeWidth();
87 bounds.inset(sw, sw);
88 path.offset(-bounds.fLeft, -bounds.fTop);
89 bounds.offset(-bounds.fLeft, -bounds.fTop);
rmistry@google.comae933ce2012-08-23 18:19:56 +000090
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 int w = SkScalarRound(bounds.width());
92 int h = SkScalarRound(bounds.height());
93 SkPaint paint(original);
94
95 paint.setAntiAlias(true);
reed@android.com0baf1932009-06-24 12:41:42 +000096 paint.setXfermodeMode(SkXfermode::kDstATop_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 paint.setColor(original.getColor());
98 paint.setStyle(SkPaint::kStroke_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +000099
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 dst->setConfig(config, w, h);
101 dst->allocPixels();
102 dst->eraseColor(SK_ColorWHITE);
103
104 SkCanvas canvas(*dst);
105 canvas.drawPath(path, paint);
106}
107
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000108class StrokeTextView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 bool fAA;
110public:
rmistry@google.comae933ce2012-08-23 18:19:56 +0000111 StrokeTextView() : fAA(false) {
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000112 this->setBGColor(0xFFCC8844);
113 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000114
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115protected:
116 // overrides from SkEventSink
117 virtual bool onQuery(SkEvent* evt) {
118 if (SampleCode::TitleQ(*evt)) {
119 SampleCode::TitleR(evt, "StrokeText");
120 return true;
121 }
122 return this->INHERITED::onQuery(evt);
123 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000124
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000125 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 SkBitmap bm;
127 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000128
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 paint.setStrokeWidth(SkIntToScalar(6));
130 paint.setTextSize(SkIntToScalar(80));
131// paint.setTypeface(Typeface.DEFAULT_BOLD);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000132
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133 lettersToBitmap(&bm, "Test Case", paint, SkBitmap::kARGB_4444_Config);
caryclark@google.com02939ce2012-06-06 12:09:51 +0000134 if (false) { // avoid bit rot, suppress warning
135 lettersToBitmap2(&bm, "Test Case", paint, SkBitmap::kARGB_4444_Config);
136 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 canvas->drawBitmap(bm, 0, 0);
138 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000139
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140private:
rmistry@google.comae933ce2012-08-23 18:19:56 +0000141
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000142 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143};
144
145//////////////////////////////////////////////////////////////////////////////
146
147static SkView* MyFactory() { return new StrokeTextView; }
148static SkViewRegister reg(MyFactory);