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" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #include "SkColorPriv.h" |
| 15 | #include "SkColorFilter.h" |
| 16 | #include "SkTime.h" |
| 17 | #include "SkTypeface.h" |
| 18 | #include "SkXfermode.h" |
| 19 | |
| 20 | static 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.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 32 | SkRect bounds = path.getBounds(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 58 | paint.setXfermodeMode(SkXfermode::kDstATop_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | |
| 66 | static 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.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 78 | SkRect bounds = path.getBounds(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 89 | paint.setXfermodeMode(SkXfermode::kDstATop_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | |
| 101 | class StrokeTextView : public SkView { |
| 102 | bool fAA; |
| 103 | public: |
| 104 | StrokeTextView() : fAA(false) {} |
| 105 | |
| 106 | protected: |
| 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 | |
| 136 | private: |
| 137 | |
| 138 | typedef SkView INHERITED; |
| 139 | }; |
| 140 | |
| 141 | ////////////////////////////////////////////////////////////////////////////// |
| 142 | |
| 143 | static SkView* MyFactory() { return new StrokeTextView; } |
| 144 | static SkViewRegister reg(MyFactory); |
| 145 | |