skia2/gm/mixedtextblobs.cpp
Brian Salomon 9fa47cc1c6 Make class members that are static constexpr also be inline.
This is in prep for compiling with -std=c++14 and -Wno-c++17-extensions
when building with clang. Chrome has encountered problems with
third_party headers that are included both in Skia and other Chrome
sources that produce different code based on whether preprocessor macros
indicate a C++14 or C++17 compilation.

In C++17 they are already inline implicitly. When compiling with C++14
we can get linker errors unless they're explicitly inlined or defined
outside the class. With -Wno-c++17-extensions we can explicitly inline
them in the C++14 build because the warning that would be generated
about using a C++17 language extension is suppressed.

We cannot do this in public headers because we support compiling with
C++14 without suppressing the C++17 language extension warnings.

Bug: chromium:1257145
Change-Id: Iaf5f4c62a398f98dd4ca9b7dfb86f2d5cab21d66
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/457498
Reviewed-by: Ben Wagner <bungeman@google.com>
Reviewed-by: Herb Derby <herb@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
2021-10-11 16:22:59 +00:00

170 lines
5.7 KiB
C++

/*
* Copyright 2013 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gm/gm.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkFont.h"
#include "include/core/SkFontTypes.h"
#include "include/core/SkPaint.h"
#include "include/core/SkRect.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkScalar.h"
#include "include/core/SkSize.h"
#include "include/core/SkString.h"
#include "include/core/SkTextBlob.h"
#include "include/core/SkTypeface.h"
#include "tools/Resources.h"
#include "tools/ToolUtils.h"
#include <string.h>
namespace skiagm {
static void draw_blob(SkCanvas* canvas, const SkTextBlob* blob, const SkPaint& skPaint,
const SkRect& clipRect) {
SkPaint clipHairline;
clipHairline.setColor(SK_ColorWHITE);
clipHairline.setStyle(SkPaint::kStroke_Style);
SkPaint paint(skPaint);
canvas->save();
canvas->drawRect(clipRect, clipHairline);
paint.setAlphaf(0.125f);
canvas->drawTextBlob(blob, 0, 0, paint);
canvas->clipRect(clipRect);
paint.setAlphaf(1.0f);
canvas->drawTextBlob(blob, 0, 0, paint);
canvas->restore();
}
class MixedTextBlobsGM : public GM {
public:
MixedTextBlobsGM() { }
protected:
void onOnceBeforeDraw() override {
fEmojiTypeface = ToolUtils::planet_typeface();
fEmojiText = "♁♃";
fReallyBigATypeface = MakeResourceAsTypeface("fonts/ReallyBigA.ttf");
SkTextBlobBuilder builder;
// make textblob
// Text so large we draw as paths
SkFont font(ToolUtils::create_portable_typeface(), 385);
font.setEdging(SkFont::Edging::kAlias);
const char* text = "O";
SkRect bounds;
font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
SkScalar yOffset = bounds.height();
ToolUtils::add_to_text_blob(&builder, text, font, 10, yOffset);
SkScalar corruptedAx = bounds.width();
SkScalar corruptedAy = yOffset;
const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf;
const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf;
SkScalar xOffset = boundsHalfWidth;
yOffset = boundsHalfHeight;
// LCD
font.setSize(32);
font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
font.setSubpixel(true);
text = "LCD!!!!!";
font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
ToolUtils::add_to_text_blob(&builder,
text,
font,
xOffset - bounds.width() * 0.25f,
yOffset - bounds.height() * 0.5f);
// color emoji font with large glyph
if (fEmojiTypeface) {
font.setEdging(SkFont::Edging::kAlias);
font.setSubpixel(false);
font.setTypeface(fEmojiTypeface);
font.measureText(fEmojiText, strlen(fEmojiText), SkTextEncoding::kUTF8, &bounds);
ToolUtils::add_to_text_blob(&builder, fEmojiText, font, xOffset, yOffset);
}
// outline font with large glyph
font.setSize(12);
text = "aA";
font.setTypeface(fReallyBigATypeface);
ToolUtils::add_to_text_blob(&builder, text, font, corruptedAx, corruptedAy);
fBlob = builder.make();
}
SkString onShortName() override {
return SkString("mixedtextblobs");
}
SkISize onISize() override {
return SkISize::Make(kWidth, kHeight);
}
void onDraw(SkCanvas* canvas) override {
canvas->drawColor(SK_ColorGRAY);
SkPaint paint;
// setup work needed to draw text with different clips
paint.setColor(SK_ColorBLACK);
canvas->translate(10, 40);
// compute the bounds of the text and setup some clips
SkRect bounds = fBlob->bounds();
const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf;
const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf;
const SkScalar boundsQuarterWidth = boundsHalfWidth * SK_ScalarHalf;
const SkScalar boundsQuarterHeight = boundsHalfHeight * SK_ScalarHalf;
SkRect upperLeftClip = SkRect::MakeXYWH(bounds.left(), bounds.top(),
boundsHalfWidth, boundsHalfHeight);
SkRect lowerRightClip = SkRect::MakeXYWH(bounds.centerX(), bounds.centerY(),
boundsHalfWidth, boundsHalfHeight);
SkRect interiorClip = bounds;
interiorClip.inset(boundsQuarterWidth, boundsQuarterHeight);
const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, interiorClip};
size_t count = sizeof(clipRects) / sizeof(SkRect);
for (size_t x = 0; x < count; ++x) {
draw_blob(canvas, fBlob.get(), paint, clipRects[x]);
if (x == (count >> 1) - 1) {
canvas->translate(SkScalarFloorToScalar(bounds.width() + SkIntToScalar(25)),
-(x * SkScalarFloorToScalar(bounds.height() +
SkIntToScalar(25))));
} else {
canvas->translate(0, SkScalarFloorToScalar(bounds.height() + SkIntToScalar(25)));
}
}
}
private:
sk_sp<SkTypeface> fEmojiTypeface;
sk_sp<SkTypeface> fReallyBigATypeface;
const char* fEmojiText;
sk_sp<SkTextBlob> fBlob;
inline static constexpr int kWidth = 1250;
inline static constexpr int kHeight = 700;
using INHERITED = GM;
};
//////////////////////////////////////////////////////////////////////////////
DEF_GM(return new MixedTextBlobsGM;)
} // namespace skiagm