blob: 83616f42602ceac91fc44df3d8d607cac6b94cd1 [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"
robertphillips@google.comb7061172013-09-06 14:16:12 +00009#include "SkBlurMask.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkBlurMaskFilter.h"
11#include "SkCanvas.h"
caryclark@google.com02939ce2012-06-06 12:09:51 +000012#include "SkDevice.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkGradientShader.h"
14#include "SkGraphics.h"
15#include "SkImageDecoder.h"
16#include "SkPath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017#include "SkRandom.h"
18#include "SkRegion.h"
19#include "SkShader.h"
20#include "SkUtils.h"
21#include "SkXfermode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022#include "SkColorPriv.h"
23#include "SkColorFilter.h"
24#include "SkTime.h"
25#include "SkTypeface.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000026#include "SkView.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000027
reed@android.com8a1c16f2008-12-17 15:59:43 +000028#include "SkOSFile.h"
29#include "SkStream.h"
30
31static void check_for_nonwhite(const SkBitmap& bm, int alpha) {
32 if (bm.config() != SkBitmap::kRGB_565_Config) {
33 return;
34 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000035
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 for (int y = 0; y < bm.height(); y++) {
37 for (int x = 0; x < bm.width(); x++) {
38 uint16_t c = *bm.getAddr16(x, y);
39 if (c != 0xFFFF) {
40 SkDebugf("------ nonwhite alpha=%x [%d %d] %x\n", alpha, x, y, c);
41 return;
42 }
43 }
44 }
45}
46
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000047class TextAlphaView : public SampleView {
rmistry@google.comae933ce2012-08-23 18:19:56 +000048public:
49 TextAlphaView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 fByte = 0xFF;
51 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000052
reed@android.com8a1c16f2008-12-17 15:59:43 +000053protected:
54 // overrides from SkEventSink
55 virtual bool onQuery(SkEvent* evt) {
56 if (SampleCode::TitleQ(*evt)) {
tfarina@chromium.org45369a32012-09-30 11:30:01 +000057 SampleCode::TitleR(evt, "TextAlpha");
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 return true;
59 }
60 return this->INHERITED::onQuery(evt);
61 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000062
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000063 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 const char* str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
65 SkPaint paint;
66 SkScalar x = SkIntToScalar(10);
67 SkScalar y = SkIntToScalar(20);
rmistry@google.comae933ce2012-08-23 18:19:56 +000068
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 paint.setFlags(0x105);
rmistry@google.comae933ce2012-08-23 18:19:56 +000070
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 paint.setARGB(fByte, 0xFF, 0xFF, 0xFF);
rmistry@google.comae933ce2012-08-23 18:19:56 +000072
robertphillips@google.comb7061172013-09-06 14:16:12 +000073 paint.setMaskFilter(SkBlurMaskFilter::Create(SkBlurMaskFilter::kNormal_BlurStyle,
74 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(3))));
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 paint.getMaskFilter()->unref();
rmistry@google.comae933ce2012-08-23 18:19:56 +000076
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000077 SkRandom rand;
rmistry@google.comae933ce2012-08-23 18:19:56 +000078
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 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.com02939ce2012-06-06 12:09:51 +000086 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.com8a1c16f2008-12-17 15:59:43 +000090
91 if (false) {
92 fByte += 1;
93 fByte &= 0xFF;
94 this->inval(NULL);
95 }
96 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000097
sugoi@google.com9c55f802013-03-07 20:52:59 +000098 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 return new Click(this);
100 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000101
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 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 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000113
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114private:
115 int fByte;
116
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000117 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118};
119
120//////////////////////////////////////////////////////////////////////////////
121
122static SkView* MyFactory() { return new TextAlphaView; }
123static SkViewRegister reg(MyFactory);