1#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" 14#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 (size_t 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 = path.getBounds(); 33 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); 58 paint.setXfermodeMode(SkXfermode::kDstATop_Mode); 59 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 (size_t 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 } 78 SkRect bounds = path.getBounds(); 79 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); 89 paint.setXfermodeMode(SkXfermode::kDstATop_Mode); 90 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 SampleView { 102 bool fAA; 103public: 104 StrokeTextView() : fAA(false) { 105 this->setBGColor(0xFFCC8844); 106 } 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 virtual void onDrawContent(SkCanvas* canvas) { 119 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 133 typedef SampleView INHERITED; 134}; 135 136////////////////////////////////////////////////////////////////////////////// 137 138static SkView* MyFactory() { return new StrokeTextView; } 139static SkViewRegister reg(MyFactory); 140 141