Herb Derby | e90a295 | 2021-04-16 11:31:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 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 | |
| 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkFont.h" |
| 11 | #include "include/core/SkPaint.h" |
Herb Derby | f9cf1aa | 2021-04-21 10:57:25 -0400 | [diff] [blame] | 12 | #include "include/core/SkRSXform.h" |
Brian Osman | a5842bc | 2021-05-11 13:41:46 -0400 | [diff] [blame] | 13 | #include "include/core/SkSpan.h" |
Kevin Lubick | dc6cc02 | 2023-01-13 11:24:27 -0500 | [diff] [blame] | 14 | #include "include/private/base/SkTDArray.h" |
Kevin Lubick | ed37743 | 2023-01-25 13:34:36 -0500 | [diff] [blame] | 15 | #include "src/base/SkZip.h" |
Herb Derby | e90a295 | 2021-04-16 11:31:39 -0400 | [diff] [blame] | 16 | #include "tools/ToolUtils.h" |
Kevin Lubick | e836c3a | 2023-10-20 06:55:35 -0400 | [diff] [blame] | 17 | #include "tools/fonts/FontToolUtils.h" |
Herb Derby | e90a295 | 2021-04-16 11:31:39 -0400 | [diff] [blame] | 18 | |
| 19 | static const char gText[] = "Call me Ishmael. Some years ago—never mind how long precisely"; |
| 20 | |
| 21 | class DrawGlyphsGM : public skiagm::GM { |
| 22 | public: |
| 23 | void onOnceBeforeDraw() override { |
Kevin Lubick | e836c3a | 2023-10-20 06:55:35 -0400 | [diff] [blame] | 24 | fTypeface = ToolUtils::CreatePortableTypeface("serif", SkFontStyle()); |
Herb Derby | e90a295 | 2021-04-16 11:31:39 -0400 | [diff] [blame] | 25 | fFont = SkFont(fTypeface); |
| 26 | fFont.setSubpixel(true); |
| 27 | fFont.setSize(18); |
Herb Derby | f9cf1aa | 2021-04-21 10:57:25 -0400 | [diff] [blame] | 28 | const size_t txtLen = strlen(gText); |
Herb Derby | e90a295 | 2021-04-16 11:31:39 -0400 | [diff] [blame] | 29 | fGlyphCount = fFont.countText(gText, txtLen, SkTextEncoding::kUTF8); |
| 30 | |
| 31 | fGlyphs.append(fGlyphCount); |
| 32 | fFont.textToGlyphs(gText, txtLen, SkTextEncoding::kUTF8, fGlyphs.begin(), fGlyphCount); |
| 33 | |
| 34 | fPositions.append(fGlyphCount); |
| 35 | fFont.getPos(fGlyphs.begin(), fGlyphCount, fPositions.begin()); |
Herb Derby | 8ccbeee | 2022-06-14 12:09:25 -0400 | [diff] [blame] | 36 | auto positions = SkSpan(fPositions.begin(), fGlyphCount); |
Herb Derby | f9cf1aa | 2021-04-21 10:57:25 -0400 | [diff] [blame] | 37 | |
| 38 | fLength = positions.back().x() - positions.front().x(); |
| 39 | fRadius = fLength / SK_FloatPI; |
| 40 | fXforms.append(fGlyphCount); |
| 41 | |
| 42 | for (auto [xform, pos] : SkMakeZip(fXforms.begin(), positions)) { |
| 43 | const SkScalar lengthToGlyph = pos.x() - positions.front().x(); |
| 44 | const SkScalar angle = SK_FloatPI * (fLength - lengthToGlyph) / fLength; |
| 45 | const SkScalar cos = std::cos(angle); |
| 46 | const SkScalar sin = std::sin(angle); |
| 47 | xform = SkRSXform::Make(sin, cos, fRadius*cos, -fRadius*sin); |
| 48 | } |
Herb Derby | e90a295 | 2021-04-16 11:31:39 -0400 | [diff] [blame] | 49 | } |
| 50 | |
Leandro Lovisolo | 24fa211 | 2023-08-15 19:05:17 +0000 | [diff] [blame] | 51 | SkString getName() const override { return SkString("drawglyphs"); } |
Herb Derby | e90a295 | 2021-04-16 11:31:39 -0400 | [diff] [blame] | 52 | |
Leandro Lovisolo | 8f02388 | 2023-08-15 21:13:52 +0000 | [diff] [blame] | 53 | SkISize getISize() override { return SkISize::Make(640, 480); } |
Herb Derby | e90a295 | 2021-04-16 11:31:39 -0400 | [diff] [blame] | 54 | |
| 55 | void onDraw(SkCanvas* canvas) override { |
| 56 | canvas->drawGlyphs(fGlyphCount, fGlyphs.begin(), fPositions.begin(), {50, 100}, fFont, |
| 57 | SkPaint{}); |
| 58 | |
| 59 | canvas->drawGlyphs(fGlyphCount, fGlyphs.begin(), fPositions.begin(), {50, 120}, fFont, |
| 60 | SkPaint{}); |
| 61 | |
| 62 | // Check bounding box calculation. |
| 63 | for (auto& pos : fPositions) { |
| 64 | pos += {0, -500}; |
| 65 | } |
| 66 | canvas->drawGlyphs(fGlyphCount, fGlyphs.begin(), fPositions.begin(), {50, 640}, fFont, |
| 67 | SkPaint{}); |
| 68 | |
Herb Derby | f9cf1aa | 2021-04-21 10:57:25 -0400 | [diff] [blame] | 69 | canvas->drawGlyphs(fGlyphCount, fGlyphs.begin(), fXforms.begin(), |
| 70 | {50 + fLength / 2, 160 + fRadius}, fFont, SkPaint{}); |
| 71 | |
Herb Derby | e90a295 | 2021-04-16 11:31:39 -0400 | [diff] [blame] | 72 | // TODO: add tests for cluster versions of drawGlyphs. |
| 73 | } |
| 74 | |
| 75 | private: |
| 76 | sk_sp<SkTypeface> fTypeface; |
| 77 | SkFont fFont; |
| 78 | SkTDArray<SkGlyphID> fGlyphs; |
Herb Derby | f9cf1aa | 2021-04-21 10:57:25 -0400 | [diff] [blame] | 79 | SkTDArray<SkPoint> fPositions; |
| 80 | SkTDArray<SkRSXform> fXforms; |
Herb Derby | e90a295 | 2021-04-16 11:31:39 -0400 | [diff] [blame] | 81 | int fGlyphCount; |
Herb Derby | f9cf1aa | 2021-04-21 10:57:25 -0400 | [diff] [blame] | 82 | SkScalar fRadius; |
| 83 | SkScalar fLength; |
Herb Derby | e90a295 | 2021-04-16 11:31:39 -0400 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | DEF_GM(return new DrawGlyphsGM{};) |