|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 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/SkColor.h" |
| 11 | +#include "include/core/SkFont.h" |
| 12 | +#include "include/core/SkFontMgr.h" |
| 13 | +#include "include/core/SkPaint.h" |
| 14 | +#include "include/core/SkRect.h" |
| 15 | +#include "include/core/SkString.h" |
| 16 | +#include "modules/skparagraph/include/FontCollection.h" |
| 17 | +#include "modules/skparagraph/include/Paragraph.h" |
| 18 | +#include "modules/skparagraph/include/ParagraphStyle.h" |
| 19 | +#include "modules/skparagraph/include/TextStyle.h" |
| 20 | +#include "modules/skparagraph/src/ParagraphBuilderImpl.h" |
| 21 | +#include "modules/skshaper/utils/FactoryHelpers.h" |
| 22 | +#include "tools/fonts/FontToolUtils.h" |
| 23 | + |
| 24 | +// Visual coverage for ellipsis shaping across bidi contexts: LTR and RTL |
| 25 | +// paragraphs with the default "…" and with custom ellipses. The custom ellipses |
| 26 | +// avoid neutral characters, which can need a second font (macOS's Arabic font |
| 27 | +// has no "!") and ShapeHandler supports only one run. The neutral-character |
| 28 | +// cases are covered by unit tests that use fixed test fonts. |
| 29 | + |
| 30 | +namespace { |
| 31 | + |
| 32 | +constexpr SkScalar kRowHeight = 80; |
| 33 | +// Narrower than every row's text, so each row truncates and shows its ellipsis. |
| 34 | +constexpr SkScalar kLayoutWidth = 180; |
| 35 | +constexpr SkScalar kCanvasWidth = 640; |
| 36 | +constexpr int kRowCount = 4; |
| 37 | +// Visual gap between row baselines and the debug border so descenders aren't |
| 38 | +// clipped by the border rectangle drawn around each paragraph's layout box. |
| 39 | +constexpr SkScalar kRowBorderInset = 10; |
| 40 | + |
| 41 | +} // namespace |
| 42 | + |
| 43 | +class EllipsisBidiGM : public skiagm::GM { |
| 44 | +public: |
| 45 | + EllipsisBidiGM() = default; |
| 46 | + |
| 47 | +protected: |
| 48 | + SkString getName() const override { return SkString("paragraph_ellipsis_bidi"); } |
| 49 | + |
| 50 | + SkISize getISize() override { |
| 51 | + return SkISize::Make(static_cast<int>(kCanvasWidth), |
| 52 | + static_cast<int>(kRowHeight * kRowCount + 20)); |
| 53 | + } |
| 54 | + |
| 55 | + DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { |
| 56 | + sk_sp<SkFontMgr> fontmgr = ToolUtils::TestFontMgr(); |
| 57 | + if (fontmgr->countFamilies() == 0) { |
| 58 | + *errorMsg = "No fonts available."; |
| 59 | + return DrawResult::kSkip; |
| 60 | + } |
| 61 | + auto collection = sk_make_sp<skia::textlayout::FontCollection>(); |
| 62 | + collection->setDefaultFontManager(fontmgr); |
| 63 | + collection->enableFontFallback(); |
| 64 | + |
| 65 | + auto unicode = SkShapers::BestAvailableUnicode(); |
| 66 | + if (!unicode) { |
| 67 | + *errorMsg = "No SkUnicode implementation available."; |
| 68 | + return DrawResult::kSkip; |
| 69 | + } |
| 70 | + |
| 71 | + canvas->clear(SK_ColorWHITE); |
| 72 | + |
| 73 | + SkScalar y = 10; |
| 74 | + |
| 75 | + // Row 1: LTR paragraph, default "…" ellipsis (ON-class, regression guard). |
| 76 | + drawRow(canvas, 10, y, collection, unicode, |
| 77 | + skia::textlayout::TextDirection::kLtr, |
| 78 | + u"The quick brown fox jumps over the lazy dog", |
| 79 | + u"\u2026"); |
| 80 | + y += kRowHeight; |
| 81 | + |
| 82 | + // Row 2: LTR paragraph, custom ellipsis containing strong-LTR letters. |
| 83 | + drawRow(canvas, 10, y, collection, unicode, |
| 84 | + skia::textlayout::TextDirection::kLtr, |
| 85 | + u"The quick brown fox jumps over the lazy dog", |
| 86 | + u"[more]"); |
| 87 | + y += kRowHeight; |
| 88 | + |
| 89 | + // Row 3: RTL paragraph, default "…" ellipsis. |
| 90 | + drawRow(canvas, 10, y, collection, unicode, |
| 91 | + skia::textlayout::TextDirection::kRtl, |
| 92 | + u"\u062A\u0638\u0627\u0647\u0631\u0627\u062A \u0648 " |
| 93 | + u"\u062A\u062C\u0645\u0639\u0627\u062A", |
| 94 | + u"\u2026"); |
| 95 | + y += kRowHeight; |
| 96 | + |
| 97 | + // Row 4: RTL paragraph, custom Arabic ellipsis "النهاية". |
| 98 | + drawRow(canvas, 10, y, collection, unicode, |
| 99 | + skia::textlayout::TextDirection::kRtl, |
| 100 | + u"\u062A\u0638\u0627\u0647\u0631\u0627\u062A \u0648 " |
| 101 | + u"\u062A\u062C\u0645\u0639\u0627\u062A", |
| 102 | + u"\u0627\u0644\u0646\u0647\u0627\u064A\u0629"); |
| 103 | + |
| 104 | + return DrawResult::kOk; |
| 105 | + } |
| 106 | + |
| 107 | +private: |
| 108 | + static void drawRow(SkCanvas* canvas, |
| 109 | + SkScalar x, |
| 110 | + SkScalar y, |
| 111 | + const sk_sp<skia::textlayout::FontCollection>& collection, |
| 112 | + const sk_sp<SkUnicode>& unicode, |
| 113 | + skia::textlayout::TextDirection direction, |
| 114 | + const char16_t* text, |
| 115 | + const char16_t* ellipsis) { |
| 116 | + skia::textlayout::TextStyle textStyle; |
| 117 | + textStyle.setFontFamilies({SkString("Noto Naskh Arabic"), SkString("Roboto")}); |
| 118 | + textStyle.setFontSize(30); |
| 119 | + textStyle.setColor(SK_ColorBLACK); |
| 120 | + |
| 121 | + skia::textlayout::ParagraphStyle paragraphStyle; |
| 122 | + paragraphStyle.setTextStyle(textStyle); |
| 123 | + paragraphStyle.setTextDirection(direction); |
| 124 | + paragraphStyle.setEllipsis(std::u16string(ellipsis)); |
| 125 | + paragraphStyle.setMaxLines(1); |
| 126 | + |
| 127 | + auto builder = skia::textlayout::ParagraphBuilderImpl::make( |
| 128 | + paragraphStyle, collection, unicode); |
| 129 | + if (!builder) { |
| 130 | + return; |
| 131 | + } |
| 132 | + builder->addText(std::u16string(text)); |
| 133 | + auto paragraph = builder->Build(); |
| 134 | + paragraph->layout(kLayoutWidth); |
| 135 | + paragraph->paint(canvas, x, y); |
| 136 | + |
| 137 | + SkPaint border; |
| 138 | + border.setStroke(true); |
| 139 | + border.setColor(0xFFCCCCCC); |
| 140 | + canvas->drawRect(SkRect::MakeXYWH(x, y, kLayoutWidth, kRowHeight - kRowBorderInset), |
| 141 | + border); |
| 142 | + } |
| 143 | +}; |
| 144 | + |
| 145 | +DEF_GM(return new EllipsisBidiGM();) |
0 commit comments