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