fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkColor.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 11 | #include "include/core/SkFont.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkFontStyle.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 13 | #include "include/core/SkFontTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/core/SkPaint.h" |
| 15 | #include "include/core/SkPoint.h" |
| 16 | #include "include/core/SkRect.h" |
| 17 | #include "include/core/SkRefCnt.h" |
| 18 | #include "include/core/SkScalar.h" |
| 19 | #include "include/core/SkSize.h" |
| 20 | #include "include/core/SkString.h" |
| 21 | #include "include/core/SkTextBlob.h" |
| 22 | #include "include/core/SkTypeface.h" |
| 23 | #include "include/core/SkTypes.h" |
| 24 | #include "include/private/SkTDArray.h" |
| 25 | #include "tools/ToolUtils.h" |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 26 | |
Ben Wagner | cb3d49c | 2018-03-14 15:07:43 -0400 | [diff] [blame] | 27 | #include <cstring> |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 28 | |
| 29 | namespace { |
| 30 | |
| 31 | enum Pos { |
| 32 | kDefault_Pos = 0, |
| 33 | kScalar_Pos = 1, |
| 34 | kPoint_Pos = 2, |
| 35 | }; |
| 36 | |
| 37 | const struct BlobCfg { |
| 38 | unsigned count; |
| 39 | Pos pos; |
| 40 | SkScalar scale; |
| 41 | } blobConfigs[][3][3] = { |
| 42 | { |
| 43 | { { 1024, kDefault_Pos, 1 }, { 0, kDefault_Pos, 0 }, { 0, kDefault_Pos, 0 } }, |
| 44 | { { 1024, kScalar_Pos, 1 }, { 0, kScalar_Pos, 0 }, { 0, kScalar_Pos, 0 } }, |
| 45 | { { 1024, kPoint_Pos, 1 }, { 0, kPoint_Pos, 0 }, { 0, kPoint_Pos, 0 } }, |
| 46 | }, |
| 47 | { |
fmalita | 37ecbaf | 2014-08-22 09:01:19 -0700 | [diff] [blame] | 48 | { { 4, kDefault_Pos, 1 }, { 4, kDefault_Pos, 1 }, { 4, kDefault_Pos, 1 } }, |
| 49 | { { 4, kScalar_Pos, 1 }, { 4, kScalar_Pos, 1 }, { 4, kScalar_Pos, 1 } }, |
| 50 | { { 4, kPoint_Pos, 1 }, { 4, kPoint_Pos, 1 }, { 4, kPoint_Pos, 1 } }, |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 51 | }, |
| 52 | |
| 53 | { |
fmalita | 37ecbaf | 2014-08-22 09:01:19 -0700 | [diff] [blame] | 54 | { { 4, kDefault_Pos, 1 }, { 4, kDefault_Pos, 1 }, { 4, kScalar_Pos, 1 } }, |
| 55 | { { 4, kScalar_Pos, 1 }, { 4, kScalar_Pos, 1 }, { 4, kPoint_Pos, 1 } }, |
| 56 | { { 4, kPoint_Pos, 1 }, { 4, kPoint_Pos, 1 }, { 4, kDefault_Pos, 1 } }, |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 57 | }, |
| 58 | |
| 59 | { |
fmalita | 37ecbaf | 2014-08-22 09:01:19 -0700 | [diff] [blame] | 60 | { { 4, kDefault_Pos, 1 }, { 4, kScalar_Pos, 1 }, { 4, kPoint_Pos, 1 } }, |
| 61 | { { 4, kScalar_Pos, 1 }, { 4, kPoint_Pos, 1 }, { 4, kDefault_Pos, 1 } }, |
| 62 | { { 4, kPoint_Pos, 1 }, { 4, kDefault_Pos, 1 }, { 4, kScalar_Pos, 1 } }, |
| 63 | }, |
| 64 | |
| 65 | { |
| 66 | { { 4, kDefault_Pos, .75f }, { 4, kDefault_Pos, 1 }, { 4, kScalar_Pos, 1.25f } }, |
| 67 | { { 4, kScalar_Pos, .75f }, { 4, kScalar_Pos, 1 }, { 4, kPoint_Pos, 1.25f } }, |
| 68 | { { 4, kPoint_Pos, .75f }, { 4, kPoint_Pos, 1 }, { 4, kDefault_Pos, 1.25f } }, |
| 69 | }, |
| 70 | |
| 71 | { |
| 72 | { { 4, kDefault_Pos, 1 }, { 4, kScalar_Pos, .75f }, { 4, kPoint_Pos, 1.25f } }, |
| 73 | { { 4, kScalar_Pos, 1 }, { 4, kPoint_Pos, .75f }, { 4, kDefault_Pos, 1.25f } }, |
| 74 | { { 4, kPoint_Pos, 1 }, { 4, kDefault_Pos, .75f }, { 4, kScalar_Pos, 1.25f } }, |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 75 | }, |
| 76 | }; |
| 77 | |
| 78 | const SkScalar kFontSize = 16; |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 79 | } // namespace |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 80 | |
| 81 | class TextBlobGM : public skiagm::GM { |
| 82 | public: |
fmalita | acb882c | 2014-09-16 17:58:34 -0700 | [diff] [blame] | 83 | TextBlobGM(const char* txt) |
kkinnunen | b4a797f | 2015-05-21 06:15:28 -0700 | [diff] [blame] | 84 | : fText(txt) { |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | protected: |
kkinnunen | b4a797f | 2015-05-21 06:15:28 -0700 | [diff] [blame] | 88 | void onOnceBeforeDraw() override { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 89 | fTypeface = ToolUtils::create_portable_typeface("serif", SkFontStyle()); |
Mike Reed | f78b7ea | 2018-12-25 22:06:17 -0500 | [diff] [blame] | 90 | SkFont font(fTypeface); |
kkinnunen | b4a797f | 2015-05-21 06:15:28 -0700 | [diff] [blame] | 91 | size_t txtLen = strlen(fText); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 92 | int glyphCount = font.countText(fText, txtLen, SkTextEncoding::kUTF8); |
kkinnunen | b4a797f | 2015-05-21 06:15:28 -0700 | [diff] [blame] | 93 | |
| 94 | fGlyphs.append(glyphCount); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 95 | font.textToGlyphs(fText, txtLen, SkTextEncoding::kUTF8, fGlyphs.begin(), glyphCount); |
kkinnunen | b4a797f | 2015-05-21 06:15:28 -0700 | [diff] [blame] | 96 | } |
| 97 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 98 | SkString onShortName() override { |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 99 | return SkString("textblob"); |
| 100 | } |
| 101 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 102 | SkISize onISize() override { |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 103 | return SkISize::Make(640, 480); |
| 104 | } |
| 105 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 106 | void onDraw(SkCanvas* canvas) override { |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 107 | for (unsigned b = 0; b < SK_ARRAY_COUNT(blobConfigs); ++b) { |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 108 | sk_sp<SkTextBlob> blob(this->makeBlob(b)); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 109 | |
| 110 | SkPaint p; |
Mike Reed | 3185f90 | 2018-10-26 16:33:00 -0400 | [diff] [blame] | 111 | p.setAntiAlias(true); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 112 | SkPoint offset = SkPoint::Make(SkIntToScalar(10 + 300 * (b % 2)), |
| 113 | SkIntToScalar(20 + 150 * (b / 2))); |
| 114 | |
| 115 | canvas->drawTextBlob(blob, offset.x(), offset.y(), p); |
| 116 | |
| 117 | p.setColor(SK_ColorBLUE); |
| 118 | p.setStyle(SkPaint::kStroke_Style); |
| 119 | SkRect box = blob->bounds(); |
| 120 | box.offset(offset); |
Mike Reed | 3185f90 | 2018-10-26 16:33:00 -0400 | [diff] [blame] | 121 | p.setAntiAlias(false); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 122 | canvas->drawRect(box, p); |
| 123 | |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | private: |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 128 | sk_sp<SkTextBlob> makeBlob(unsigned blobIndex) { |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 129 | SkTextBlobBuilder builder; |
| 130 | |
Mike Reed | 3185f90 | 2018-10-26 16:33:00 -0400 | [diff] [blame] | 131 | SkFont font; |
Mike Reed | 5f50f57 | 2018-11-12 13:19:37 -0500 | [diff] [blame] | 132 | font.setSubpixel(true); |
| 133 | font.setEdging(SkFont::Edging::kAntiAlias); |
fmalita | acb882c | 2014-09-16 17:58:34 -0700 | [diff] [blame] | 134 | font.setTypeface(fTypeface); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 135 | |
| 136 | for (unsigned l = 0; l < SK_ARRAY_COUNT(blobConfigs[blobIndex]); ++l) { |
| 137 | unsigned currentGlyph = 0; |
| 138 | |
| 139 | for (unsigned c = 0; c < SK_ARRAY_COUNT(blobConfigs[blobIndex][l]); ++c) { |
| 140 | const BlobCfg* cfg = &blobConfigs[blobIndex][l][c]; |
| 141 | unsigned count = cfg->count; |
| 142 | |
| 143 | if (count > fGlyphs.count() - currentGlyph) { |
| 144 | count = fGlyphs.count() - currentGlyph; |
| 145 | } |
| 146 | if (0 == count) { |
| 147 | break; |
| 148 | } |
| 149 | |
Mike Reed | 3185f90 | 2018-10-26 16:33:00 -0400 | [diff] [blame] | 150 | font.setSize(kFontSize * cfg->scale); |
| 151 | const SkScalar advanceX = font.getSize() * 0.85f; |
| 152 | const SkScalar advanceY = font.getSize() * 1.5f; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 153 | |
| 154 | SkPoint offset = SkPoint::Make(currentGlyph * advanceX + c * advanceX, |
| 155 | advanceY * l); |
| 156 | switch (cfg->pos) { |
| 157 | case kDefault_Pos: { |
| 158 | const SkTextBlobBuilder::RunBuffer& buf = builder.allocRun(font, count, |
| 159 | offset.x(), |
| 160 | offset.y()); |
| 161 | memcpy(buf.glyphs, fGlyphs.begin() + currentGlyph, count * sizeof(uint16_t)); |
| 162 | } break; |
| 163 | case kScalar_Pos: { |
| 164 | const SkTextBlobBuilder::RunBuffer& buf = builder.allocRunPosH(font, count, |
| 165 | offset.y()); |
| 166 | SkTDArray<SkScalar> pos; |
| 167 | for (unsigned i = 0; i < count; ++i) { |
| 168 | *pos.append() = offset.x() + i * advanceX; |
| 169 | } |
| 170 | |
| 171 | memcpy(buf.glyphs, fGlyphs.begin() + currentGlyph, count * sizeof(uint16_t)); |
| 172 | memcpy(buf.pos, pos.begin(), count * sizeof(SkScalar)); |
| 173 | } break; |
| 174 | case kPoint_Pos: { |
| 175 | const SkTextBlobBuilder::RunBuffer& buf = builder.allocRunPos(font, count); |
| 176 | |
| 177 | SkTDArray<SkScalar> pos; |
| 178 | for (unsigned i = 0; i < count; ++i) { |
| 179 | *pos.append() = offset.x() + i * advanceX; |
| 180 | *pos.append() = offset.y() + i * (advanceY / count); |
| 181 | } |
| 182 | |
| 183 | memcpy(buf.glyphs, fGlyphs.begin() + currentGlyph, count * sizeof(uint16_t)); |
| 184 | memcpy(buf.pos, pos.begin(), count * sizeof(SkScalar) * 2); |
| 185 | } break; |
| 186 | default: |
djsollen | f2b340f | 2016-01-29 08:51:04 -0800 | [diff] [blame] | 187 | SK_ABORT("unhandled pos value"); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | currentGlyph += count; |
| 191 | } |
| 192 | } |
| 193 | |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 194 | return builder.make(); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 195 | } |
| 196 | |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 197 | SkTDArray<uint16_t> fGlyphs; |
| 198 | sk_sp<SkTypeface> fTypeface; |
| 199 | const char* fText; |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 200 | using INHERITED = skiagm::GM; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 201 | }; |
| 202 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 203 | DEF_GM(return new TextBlobGM("hamburgefons");) |