epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkView.h" |
| 10 | #include "SkBlurMaskFilter.h" |
| 11 | #include "SkCanvas.h" |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 12 | #include "SkDevice.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | #include "SkGradientShader.h" |
| 14 | #include "SkGraphics.h" |
| 15 | #include "SkImageDecoder.h" |
| 16 | #include "SkPath.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | #include "SkRandom.h" |
| 18 | #include "SkRegion.h" |
| 19 | #include "SkShader.h" |
| 20 | #include "SkUtils.h" |
| 21 | #include "SkXfermode.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | #include "SkColorPriv.h" |
| 23 | #include "SkColorFilter.h" |
| 24 | #include "SkTime.h" |
| 25 | #include "SkTypeface.h" |
| 26 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | #include "SkOSFile.h" |
| 28 | #include "SkStream.h" |
| 29 | |
| 30 | static void check_for_nonwhite(const SkBitmap& bm, int alpha) { |
| 31 | if (bm.config() != SkBitmap::kRGB_565_Config) { |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | for (int y = 0; y < bm.height(); y++) { |
| 36 | for (int x = 0; x < bm.width(); x++) { |
| 37 | uint16_t c = *bm.getAddr16(x, y); |
| 38 | if (c != 0xFFFF) { |
| 39 | SkDebugf("------ nonwhite alpha=%x [%d %d] %x\n", alpha, x, y, c); |
| 40 | return; |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 46 | class TextAlphaView : public SampleView { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | public: |
| 48 | TextAlphaView() { |
| 49 | fByte = 0xFF; |
| 50 | } |
| 51 | |
| 52 | protected: |
| 53 | // overrides from SkEventSink |
| 54 | virtual bool onQuery(SkEvent* evt) { |
| 55 | if (SampleCode::TitleQ(*evt)) { |
| 56 | SkString str("TextAlpha"); |
| 57 | SampleCode::TitleR(evt, str.c_str()); |
| 58 | return true; |
| 59 | } |
| 60 | return this->INHERITED::onQuery(evt); |
| 61 | } |
| 62 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 63 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | const char* str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 65 | SkPaint paint; |
| 66 | SkScalar x = SkIntToScalar(10); |
| 67 | SkScalar y = SkIntToScalar(20); |
| 68 | |
| 69 | paint.setFlags(0x105); |
| 70 | |
| 71 | paint.setARGB(fByte, 0xFF, 0xFF, 0xFF); |
| 72 | |
| 73 | paint.setMaskFilter(SkBlurMaskFilter::Create(SkIntToScalar(3), |
| 74 | SkBlurMaskFilter::kNormal_BlurStyle)); |
| 75 | paint.getMaskFilter()->unref(); |
| 76 | |
| 77 | SkRandom rand; |
| 78 | |
| 79 | for (int ps = 6; ps <= 35; ps++) { |
| 80 | paint.setColor(rand.nextU() | (0xFF << 24)); |
| 81 | paint.setTextSize(SkIntToScalar(ps)); |
| 82 | paint.setTextSize(SkIntToScalar(24)); |
| 83 | canvas->drawText(str, strlen(str), x, y, paint); |
| 84 | y += paint.getFontMetrics(NULL); |
| 85 | } |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 86 | if (false) { // avoid bit rot, suppress warning |
| 87 | check_for_nonwhite(canvas->getDevice()->accessBitmap(false), fByte); |
| 88 | SkDebugf("------ byte %x\n", fByte); |
| 89 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | |
| 91 | if (false) { |
| 92 | fByte += 1; |
| 93 | fByte &= 0xFF; |
| 94 | this->inval(NULL); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { |
| 99 | return new Click(this); |
| 100 | } |
| 101 | |
| 102 | virtual bool onClick(Click* click) { |
| 103 | int y = click->fICurr.fY; |
| 104 | if (y < 0) { |
| 105 | y = 0; |
| 106 | } else if (y > 255) { |
| 107 | y = 255; |
| 108 | } |
| 109 | fByte = y; |
| 110 | this->inval(NULL); |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | private: |
| 115 | int fByte; |
| 116 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 117 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | ////////////////////////////////////////////////////////////////////////////// |
| 121 | |
| 122 | static SkView* MyFactory() { return new TextAlphaView; } |
| 123 | static SkViewRegister reg(MyFactory); |
| 124 | |