2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2014-01-13 14:53:55 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
2014-01-30 18:58:24 +00:00
|
|
|
#include "SkReadBuffer.h"
|
|
|
|
#include "SkWriteBuffer.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkImageDecoder.h"
|
|
|
|
#include "SkKernel33MaskFilter.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkRandom.h"
|
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkUtils.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkColorFilter.h"
|
|
|
|
#include "SkTime.h"
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
#include "SkXfermode.h"
|
|
|
|
|
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkXMLParser.h"
|
|
|
|
|
|
|
|
class ReduceNoise : public SkKernel33ProcMaskFilter {
|
|
|
|
public:
|
|
|
|
ReduceNoise(int percent256) : SkKernel33ProcMaskFilter(percent256) {}
|
2012-12-18 19:18:39 +00:00
|
|
|
virtual uint8_t computeValue(uint8_t* const* srcRows) const {
|
2008-12-17 15:59:43 +00:00
|
|
|
int c = srcRows[1][1];
|
|
|
|
int min = 255, max = 0;
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
for (int j = 0; j < 3; j++)
|
|
|
|
if (i != 1 || j != 1)
|
|
|
|
{
|
|
|
|
int v = srcRows[i][j];
|
|
|
|
if (max < v)
|
|
|
|
max = v;
|
|
|
|
if (min > v)
|
|
|
|
min = v;
|
|
|
|
}
|
|
|
|
if (c > max) c = max;
|
|
|
|
// if (c < min) c = min;
|
|
|
|
return c;
|
|
|
|
}
|
2013-03-18 17:53:38 +00:00
|
|
|
|
2014-03-13 18:02:17 +00:00
|
|
|
#ifndef SK_IGNORE_TO_STRING
|
2013-03-18 17:53:38 +00:00
|
|
|
virtual void toString(SkString* str) const SK_OVERRIDE {
|
|
|
|
str->append("ReduceNoise: (");
|
|
|
|
this->INHERITED::toString(str);
|
|
|
|
str->append(")");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-03-26 17:57:35 +00:00
|
|
|
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(ReduceNoise)
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2014-01-30 18:58:24 +00:00
|
|
|
ReduceNoise(SkReadBuffer& rb) : SkKernel33ProcMaskFilter(rb) {}
|
2013-03-18 17:53:38 +00:00
|
|
|
|
|
|
|
typedef SkKernel33ProcMaskFilter INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Darken : public SkKernel33ProcMaskFilter {
|
|
|
|
public:
|
|
|
|
Darken(int percent256) : SkKernel33ProcMaskFilter(percent256) {}
|
2012-12-18 19:18:39 +00:00
|
|
|
virtual uint8_t computeValue(uint8_t* const* srcRows) const {
|
2008-12-17 15:59:43 +00:00
|
|
|
int c = srcRows[1][1];
|
|
|
|
float f = c / 255.f;
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2011-05-18 12:17:53 +00:00
|
|
|
if (c >= 0) {
|
2008-12-17 15:59:43 +00:00
|
|
|
f = sqrtf(f);
|
2011-05-18 12:17:53 +00:00
|
|
|
} else {
|
2008-12-17 15:59:43 +00:00
|
|
|
f *= f;
|
|
|
|
}
|
|
|
|
SkASSERT(f >= 0 && f <= 1);
|
|
|
|
return (int)(f * 255);
|
|
|
|
}
|
2013-03-18 17:53:38 +00:00
|
|
|
|
2014-03-13 18:02:17 +00:00
|
|
|
#ifndef SK_IGNORE_TO_STRING
|
2013-03-19 07:15:10 +00:00
|
|
|
virtual void toString(SkString* str) const SK_OVERRIDE {
|
|
|
|
str->append("Darken: (");
|
2013-03-18 17:53:38 +00:00
|
|
|
this->INHERITED::toString(str);
|
|
|
|
str->append(")");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-03-26 17:57:35 +00:00
|
|
|
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Darken)
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2014-01-30 18:58:24 +00:00
|
|
|
Darken(SkReadBuffer& rb) : SkKernel33ProcMaskFilter(rb) {}
|
2013-03-18 17:53:38 +00:00
|
|
|
|
|
|
|
typedef SkKernel33ProcMaskFilter INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static SkMaskFilter* makemf() { return new Darken(0x30); }
|
|
|
|
|
2011-05-18 12:17:53 +00:00
|
|
|
static void test_breakText() {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint paint;
|
|
|
|
const char* text = "sdfkljAKLDFJKEWkldfjlk#$%&sdfs.dsj";
|
|
|
|
size_t length = strlen(text);
|
|
|
|
SkScalar width = paint.measureText(text, length);
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkScalar mm = 0;
|
|
|
|
SkScalar nn = 0;
|
2011-05-18 12:17:53 +00:00
|
|
|
for (SkScalar w = 0; w <= width; w += SK_Scalar1) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkScalar m;
|
|
|
|
size_t n = paint.breakText(text, length, w, &m,
|
|
|
|
SkPaint::kBackward_TextBufferDirection);
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(n <= length);
|
|
|
|
SkASSERT(m <= width);
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2011-05-18 12:17:53 +00:00
|
|
|
if (n == 0) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(m == 0);
|
2011-05-18 12:17:53 +00:00
|
|
|
} else {
|
2008-12-17 15:59:43 +00:00
|
|
|
// now assert that we're monotonic
|
2011-05-18 12:17:53 +00:00
|
|
|
if (n == nn) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(m == mm);
|
2011-05-18 12:17:53 +00:00
|
|
|
} else {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(n > nn);
|
|
|
|
SkASSERT(m > mm);
|
|
|
|
}
|
|
|
|
}
|
2012-05-17 15:38:00 +00:00
|
|
|
nn = SkIntToScalar((unsigned int)n);
|
2008-12-17 15:59:43 +00:00
|
|
|
mm = m;
|
|
|
|
}
|
|
|
|
|
2011-05-19 19:58:58 +00:00
|
|
|
SkDEBUGCODE(size_t length2 =) paint.breakText(text, length, width, &mm);
|
2011-04-14 17:53:24 +00:00
|
|
|
SkASSERT(length2 == length);
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(mm == width);
|
|
|
|
}
|
|
|
|
|
2013-09-09 20:09:12 +00:00
|
|
|
static SkRandom gRand;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
class SkPowerMode : public SkXfermode {
|
|
|
|
public:
|
|
|
|
SkPowerMode(SkScalar exponent) { this->init(exponent); }
|
|
|
|
|
2011-05-18 12:17:53 +00:00
|
|
|
virtual void xfer16(uint16_t dst[], const SkPMColor src[], int count,
|
2012-12-24 18:26:43 +00:00
|
|
|
const SkAlpha aa[]) const SK_OVERRIDE;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2014-01-30 18:58:24 +00:00
|
|
|
typedef SkFlattenable* (*Factory)(SkReadBuffer&);
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2014-03-13 18:02:17 +00:00
|
|
|
SK_TO_STRING_OVERRIDE()
|
2012-03-26 17:57:35 +00:00
|
|
|
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPowerMode)
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
SkScalar fExp; // user's value
|
|
|
|
uint8_t fTable[256]; // cache
|
|
|
|
|
|
|
|
void init(SkScalar exponent);
|
2014-01-30 18:58:24 +00:00
|
|
|
SkPowerMode(SkReadBuffer& b) : INHERITED(b) {
|
2008-12-17 15:59:43 +00:00
|
|
|
// read the exponent
|
2012-08-07 15:54:32 +00:00
|
|
|
this->init(SkFixedToScalar(b.readFixed()));
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2014-01-30 18:58:24 +00:00
|
|
|
virtual void flatten(SkWriteBuffer& b) const SK_OVERRIDE {
|
2012-03-29 15:18:04 +00:00
|
|
|
this->INHERITED::flatten(b);
|
2012-08-07 15:54:32 +00:00
|
|
|
b.writeFixed(SkScalarToFixed(fExp));
|
2012-03-29 15:18:04 +00:00
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
typedef SkXfermode INHERITED;
|
|
|
|
};
|
|
|
|
|
2011-05-18 12:17:53 +00:00
|
|
|
void SkPowerMode::init(SkScalar e) {
|
2008-12-17 15:59:43 +00:00
|
|
|
fExp = e;
|
|
|
|
float ee = SkScalarToFloat(e);
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
printf("------ %g\n", ee);
|
2011-05-18 12:17:53 +00:00
|
|
|
for (int i = 0; i < 256; i++) {
|
2008-12-17 15:59:43 +00:00
|
|
|
float x = i / 255.f;
|
|
|
|
// printf(" %d %g", i, x);
|
|
|
|
x = powf(x, ee);
|
|
|
|
// printf(" %g", x);
|
2013-12-17 19:22:07 +00:00
|
|
|
int xx = SkScalarRoundToInt(x * 255);
|
2008-12-17 15:59:43 +00:00
|
|
|
// printf(" %d\n", xx);
|
|
|
|
fTable[i] = SkToU8(xx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-18 12:17:53 +00:00
|
|
|
void SkPowerMode::xfer16(uint16_t dst[], const SkPMColor src[], int count,
|
2012-12-24 18:26:43 +00:00
|
|
|
const SkAlpha aa[]) const {
|
2011-05-18 12:17:53 +00:00
|
|
|
for (int i = 0; i < count; i++) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPMColor c = src[i];
|
|
|
|
int r = SkGetPackedR32(c);
|
|
|
|
int g = SkGetPackedG32(c);
|
|
|
|
int b = SkGetPackedB32(c);
|
|
|
|
r = fTable[r];
|
|
|
|
g = fTable[g];
|
|
|
|
b = fTable[b];
|
|
|
|
dst[i] = SkPack888ToRGB16(r, g, b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-13 18:02:17 +00:00
|
|
|
#ifndef SK_IGNORE_TO_STRING
|
2013-01-22 14:32:09 +00:00
|
|
|
void SkPowerMode::toString(SkString* str) const {
|
|
|
|
str->append("SkPowerMode: exponent ");
|
|
|
|
str->appendScalar(fExp);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
static const struct {
|
|
|
|
const char* fName;
|
|
|
|
uint32_t fFlags;
|
|
|
|
bool fFlushCache;
|
|
|
|
} gHints[] = {
|
|
|
|
{ "Linear", SkPaint::kLinearText_Flag, false },
|
|
|
|
{ "Normal", 0, true },
|
|
|
|
{ "Subpixel", SkPaint::kSubpixelText_Flag, true }
|
|
|
|
};
|
|
|
|
|
|
|
|
static void DrawTheText(SkCanvas* canvas, const char text[], size_t length,
|
|
|
|
SkScalar x, SkScalar y, const SkPaint& paint,
|
2011-05-18 12:17:53 +00:00
|
|
|
SkScalar clickX, SkMaskFilter* mf) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint p(paint);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
canvas->drawText(text, length, x, y, paint);
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
SkPoint pts[1000];
|
|
|
|
SkScalar xpos = x;
|
|
|
|
SkASSERT(length <= SK_ARRAY_COUNT(pts));
|
2011-05-18 12:17:53 +00:00
|
|
|
for (size_t i = 0; i < length; i++) {
|
2008-12-17 15:59:43 +00:00
|
|
|
pts[i].set(xpos, y), xpos += paint.getTextSize();
|
2011-05-18 12:17:53 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->drawPosText(text, length, pts, paint);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
p.setSubpixelText(true);
|
|
|
|
x += SkIntToScalar(180);
|
|
|
|
canvas->drawText(text, length, x, y, p);
|
|
|
|
|
|
|
|
#ifdef SK_DEBUG
|
2011-05-18 12:17:53 +00:00
|
|
|
if (true) {
|
2008-12-17 15:59:43 +00:00
|
|
|
// p.setMaskFilter(mf);
|
|
|
|
p.setSubpixelText(false);
|
|
|
|
p.setLinearText(true);
|
|
|
|
x += SkIntToScalar(180);
|
|
|
|
canvas->drawText(text, length, x, y, p);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-05-18 12:17:53 +00:00
|
|
|
class TextSpeedView : public SampleView {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2012-08-23 18:19:56 +00:00
|
|
|
TextSpeedView() {
|
2008-12-17 15:59:43 +00:00
|
|
|
fMF = makemf();
|
|
|
|
|
|
|
|
fHints = 0;
|
|
|
|
fClickX = 0;
|
|
|
|
|
2011-02-07 15:30:46 +00:00
|
|
|
test_breakText();
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2011-05-18 12:17:53 +00:00
|
|
|
virtual ~TextSpeedView() {
|
2011-02-07 15:30:46 +00:00
|
|
|
SkSafeUnref(fMF);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
2011-05-18 12:17:53 +00:00
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SampleCode::TitleR(evt, "Text");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2011-05-18 12:17:53 +00:00
|
|
|
static void make_textstrip(SkBitmap* bm) {
|
2014-02-17 02:55:57 +00:00
|
|
|
bm->allocPixels(SkImageInfo::Make(200, 18, kRGB_565_SkColorType,
|
|
|
|
kOpaque_SkAlphaType));
|
2008-12-17 15:59:43 +00:00
|
|
|
bm->eraseColor(SK_ColorWHITE);
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkCanvas canvas(*bm);
|
|
|
|
SkPaint paint;
|
|
|
|
const char* s = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit";
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setFlags(paint.getFlags() | SkPaint::kAntiAlias_Flag
|
|
|
|
| SkPaint::kDevKernText_Flag);
|
|
|
|
paint.setTextSize(SkIntToScalar(14));
|
|
|
|
canvas.drawText(s, strlen(s), SkIntToScalar(8), SkIntToScalar(14), paint);
|
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2013-09-09 20:09:12 +00:00
|
|
|
static void fill_pts(SkPoint pts[], size_t n, SkRandom* rand) {
|
2008-12-17 15:59:43 +00:00
|
|
|
for (size_t i = 0; i < n; i++)
|
|
|
|
pts[i].set(rand->nextUScalar1() * 640, rand->nextUScalar1() * 480);
|
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2011-05-18 12:17:53 +00:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkAutoCanvasRestore restore(canvas, false);
|
|
|
|
{
|
|
|
|
SkRect r;
|
|
|
|
r.set(0, 0, SkIntToScalar(1000), SkIntToScalar(20));
|
|
|
|
// canvas->saveLayer(&r, NULL, SkCanvas::kHasAlphaLayer_SaveFlag);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
// const uint16_t glyphs[] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 };
|
|
|
|
int index = fHints % SK_ARRAY_COUNT(gHints);
|
|
|
|
index = 1;
|
|
|
|
// const char* style = gHints[index].fName;
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// canvas->translate(0, SkIntToScalar(50));
|
|
|
|
|
|
|
|
// canvas->drawText(style, strlen(style), SkIntToScalar(20), SkIntToScalar(20), paint);
|
|
|
|
|
2010-01-25 22:02:44 +00:00
|
|
|
SkSafeUnref(paint.setTypeface(SkTypeface::CreateFromFile("/skimages/samplefont.ttf")));
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setFlags(paint.getFlags() | gHints[index].fFlags);
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkRect clip;
|
|
|
|
clip.set(SkIntToScalar(25), SkIntToScalar(34), SkIntToScalar(88), SkIntToScalar(155));
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
const char* text = "Hamburgefons";
|
|
|
|
size_t length = strlen(text);
|
|
|
|
|
2011-05-02 17:33:45 +00:00
|
|
|
SkScalar y = SkIntToScalar(0);
|
|
|
|
for (int i = 9; i <= 24; i++) {
|
|
|
|
paint.setTextSize(SkIntToScalar(i) /*+ (gRand.nextU() & 0xFFFF)*/);
|
2011-05-18 12:17:53 +00:00
|
|
|
for (SkScalar dx = 0; dx <= SkIntToScalar(3)/4;
|
|
|
|
dx += SkIntToScalar(1) /* /4 */) {
|
2011-05-02 17:33:45 +00:00
|
|
|
y += paint.getFontSpacing();
|
2011-05-18 12:17:53 +00:00
|
|
|
DrawTheText(canvas, text, length, SkIntToScalar(20) + dx, y,
|
|
|
|
paint, fClickX, fMF);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-02 17:33:45 +00:00
|
|
|
if (gHints[index].fFlushCache) {
|
|
|
|
// SkGraphics::SetFontCacheUsed(0);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2013-01-08 16:17:50 +00:00
|
|
|
virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
|
|
|
|
unsigned modi) SK_OVERRIDE {
|
2008-12-17 15:59:43 +00:00
|
|
|
fClickX = x;
|
|
|
|
this->inval(NULL);
|
2013-01-08 16:17:50 +00:00
|
|
|
return this->INHERITED::onFindClickHandler(x, y, modi);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2011-05-18 12:17:53 +00:00
|
|
|
virtual bool onClick(Click* click) {
|
2008-12-17 15:59:43 +00:00
|
|
|
return this->INHERITED::onClick(click);
|
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
int fHints;
|
|
|
|
SkScalar fClickX;
|
|
|
|
SkMaskFilter* fMF;
|
|
|
|
|
2011-05-18 12:17:53 +00:00
|
|
|
typedef SampleView INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new TextSpeedView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|