123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- /*
- * Copyright 2018 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/SkFontMetrics.h"
- #include "include/core/SkFontTypes.h"
- #include "include/core/SkPaint.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/ToolUtils.h"
- #include <string.h>
- #include <initializer_list>
- static sk_sp<SkTextBlob> make_hpos_test_blob_utf8(const char* text, const SkFont& font) {
- constexpr SkTextEncoding enc = SkTextEncoding::kUTF8;
- SkTextBlobBuilder builder;
- size_t len = strlen(text);
- int glyphCount = font.countText(text, len, enc);
- const auto& buffer = builder.allocRunPosH(font, glyphCount, 0);
- (void)font.textToGlyphs(text, len, enc, buffer.glyphs, glyphCount);
- font.getXPos(buffer.glyphs, glyphCount, buffer.pos);
- return builder.make();
- }
- namespace skiagm {
- class ScaledEmojiGM : public GM {
- public:
- ScaledEmojiGM() { }
- protected:
- struct EmojiFont {
- sk_sp<SkTypeface> fTypeface;
- const char* fText;
- } fEmojiFont;
- void onOnceBeforeDraw() override {
- fEmojiFont.fTypeface = ToolUtils::emoji_typeface();
- fEmojiFont.fText = ToolUtils::emoji_sample_text();
- }
- SkString onShortName() override {
- return SkString("scaledemoji");
- }
- SkISize onISize() override { return SkISize::Make(1200, 1200); }
- void onDraw(SkCanvas* canvas) override {
- canvas->drawColor(SK_ColorGRAY);
- SkPaint paint;
- SkFont font(fEmojiFont.fTypeface);
- font.setEdging(SkFont::Edging::kAlias);
- const char* text = fEmojiFont.fText;
- // draw text at different point sizes
- // Testing GPU bitmap path, SDF path with no scaling,
- // SDF path with scaling, path rendering with scaling
- SkFontMetrics metrics;
- SkScalar y = 0;
- for (SkScalar textSize : { 70, 180, 270, 340 }) {
- font.setSize(textSize);
- font.getMetrics(&metrics);
- y += -metrics.fAscent;
- canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, 10, y, font, paint);
- y += metrics.fDescent + metrics.fLeading;
- }
- }
- private:
- typedef GM INHERITED;
- };
- class ScaledEmojiPosGM : public GM {
- public:
- ScaledEmojiPosGM() {}
- protected:
- struct EmojiFont {
- sk_sp<SkTypeface> fTypeface;
- const char* fText;
- } fEmojiFont;
- void onOnceBeforeDraw() override {
- fEmojiFont.fTypeface = ToolUtils::emoji_typeface();
- fEmojiFont.fText = ToolUtils::emoji_sample_text();
- }
- SkString onShortName() override {
- return SkString("scaledemojipos");
- }
- SkISize onISize() override { return SkISize::Make(1200, 1200); }
- void onDraw(SkCanvas* canvas) override {
- canvas->drawColor(SK_ColorGRAY);
- SkPaint paint;
- SkFont font;
- font.setTypeface(fEmojiFont.fTypeface);
- const char* text = fEmojiFont.fText;
- // draw text at different point sizes
- // Testing GPU bitmap path, SDF path with no scaling,
- // SDF path with scaling, path rendering with scaling
- SkFontMetrics metrics;
- SkScalar y = 0;
- for (SkScalar textSize : { 70, 180, 270, 340 }) {
- font.setSize(textSize);
- font.getMetrics(&metrics);
- y += -metrics.fAscent;
- sk_sp<SkTextBlob> blob = make_hpos_test_blob_utf8(text, font);
- // Draw with an origin.
- canvas->drawTextBlob(blob, 10, y, paint);
- // Draw with shifted canvas.
- canvas->save();
- canvas->translate(750, 0);
- canvas->drawTextBlob(blob, 10, y, paint);
- canvas->restore();
- y += metrics.fDescent + metrics.fLeading;
- }
- }
- private:
- typedef GM INHERITED;
- };
- //////////////////////////////////////////////////////////////////////////////
- DEF_GM(return new ScaledEmojiGM;)
- DEF_GM(return new ScaledEmojiPosGM;)
- }
|