2015-11-08 16:07:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColorFilter.h"
|
|
|
|
#include "include/core/SkColorPriv.h"
|
2020-04-06 17:57:30 +00:00
|
|
|
#include "include/core/SkFont.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkImage.h"
|
|
|
|
#include "include/core/SkTime.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "samplecode/Sample.h"
|
|
|
|
#include "src/utils/SkUTF.h"
|
2015-11-08 16:07:24 +00:00
|
|
|
|
2015-11-17 17:38:05 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2020-07-09 17:25:17 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2020-10-14 15:23:11 +00:00
|
|
|
#include "src/gpu/GrDirectContextPriv.h"
|
2015-11-17 17:38:05 +00:00
|
|
|
#endif
|
2015-11-08 16:07:24 +00:00
|
|
|
|
|
|
|
SkRandom gRand;
|
|
|
|
|
|
|
|
static void DrawTheText(SkCanvas* canvas, const char text[], size_t length, SkScalar x, SkScalar y,
|
2018-12-22 03:22:31 +00:00
|
|
|
const SkFont& font, const SkPaint& paint) {
|
|
|
|
SkFont f(font);
|
|
|
|
f.setSubpixel(true);
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText(text, length, SkTextEncoding::kUTF8, x, y, f, paint);
|
2015-11-08 16:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This sample demonstrates the cache behavior of bitmap vs. distance field text
|
|
|
|
// It renders variously sized text with an animated scale and rotation.
|
|
|
|
// Specifically one should:
|
|
|
|
// use 'D' to toggle between bitmap and distance field fonts
|
|
|
|
// use '2' to toggle between scaling the image by 2x
|
|
|
|
// -- this feature boosts the rendering out of the small point-size
|
|
|
|
// SDF-text special case (which falls back to bitmap fonts for small points)
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class AnimatedTextView : public Sample {
|
2019-07-17 13:08:11 +00:00
|
|
|
float fScale = 1;
|
|
|
|
float fScaleInc = 0.1f;
|
|
|
|
float fRotation = 0;
|
|
|
|
int fSizeScale = 1;
|
2015-11-08 16:07:24 +00:00
|
|
|
|
2019-07-03 14:55:44 +00:00
|
|
|
SkString name() override { return SkString("AnimatedText"); }
|
2015-11-08 16:07:24 +00:00
|
|
|
|
2019-07-03 19:53:04 +00:00
|
|
|
bool onChar(SkUnichar uni) override {
|
2015-11-08 16:07:24 +00:00
|
|
|
if ('2' == uni) {
|
|
|
|
if (fSizeScale == 2) {
|
|
|
|
fSizeScale = 1;
|
|
|
|
} else {
|
|
|
|
fSizeScale = 2;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-03 19:53:04 +00:00
|
|
|
return false;
|
2015-11-08 16:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2018-12-22 03:22:31 +00:00
|
|
|
SkFont font(SkTypeface::MakeFromFile("/skimages/samplefont.ttf"));
|
|
|
|
|
2015-11-08 16:07:24 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
2020-07-09 17:25:17 +00:00
|
|
|
auto direct = GrAsDirectContext(canvas->recordingContext());
|
|
|
|
if (direct) {
|
2020-12-19 20:14:06 +00:00
|
|
|
SkSamplingOptions sampling(SkFilterMode::kLinear, SkMipmapMode::kNearest);
|
2020-07-09 17:25:17 +00:00
|
|
|
sk_sp<SkImage> image = direct->priv().testingOnly_getFontAtlasImage(
|
2018-03-06 13:20:37 +00:00
|
|
|
GrMaskFormat::kA8_GrMaskFormat);
|
2020-12-19 20:14:06 +00:00
|
|
|
const SkRect rect = SkRect::MakeXYWH(512.0f, 10.0f, 512.0f, 512.0f);
|
|
|
|
canvas->drawImageRect(image.get(), rect, rect, sampling, &paint);
|
2015-11-08 16:07:24 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
canvas->translate(180, 180);
|
|
|
|
canvas->rotate(fRotation);
|
|
|
|
canvas->scale(fScale, fScale);
|
|
|
|
canvas->translate(-180, -180);
|
|
|
|
|
|
|
|
const char* text = "Hamburgefons";
|
|
|
|
size_t length = strlen(text);
|
|
|
|
|
|
|
|
SkScalar y = SkIntToScalar(0);
|
|
|
|
for (int i = 12; i <= 26; i++) {
|
2018-12-22 03:22:31 +00:00
|
|
|
font.setSize(SkIntToScalar(i*fSizeScale));
|
|
|
|
y += font.getSpacing();
|
|
|
|
DrawTheText(canvas, text, length, SkIntToScalar(110), y, font, paint);
|
2015-11-08 16:07:24 +00:00
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
|
2018-12-22 03:22:31 +00:00
|
|
|
font.setSize(16);
|
2015-11-08 16:07:24 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
bool onAnimate(double nanos) override {
|
2019-07-12 16:51:44 +00:00
|
|
|
// TODO: use nanos
|
2016-03-29 16:03:52 +00:00
|
|
|
// We add noise to the scale and rotation animations to
|
2015-11-08 16:07:24 +00:00
|
|
|
// keep the font atlas from falling into a steady state
|
|
|
|
fRotation += (1.0f + gRand.nextRangeF(-0.1f, 0.1f));
|
|
|
|
fScale += (fScaleInc + gRand.nextRangeF(-0.025f, 0.025f));
|
|
|
|
if (fScale >= 2.0f) {
|
|
|
|
fScaleInc = -0.1f;
|
|
|
|
} else if (fScale <= 1.0f) {
|
|
|
|
fScaleInc = 0.1f;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new AnimatedTextView(); )
|