// Copyright 2019 Google LLC. #ifndef ParagraphImpl_DEFINED #define ParagraphImpl_DEFINED #include "include/core/SkFont.h" #include "include/core/SkPaint.h" #include "include/core/SkPicture.h" #include "include/core/SkPoint.h" #include "include/core/SkRect.h" #include "include/core/SkRefCnt.h" #include "include/core/SkScalar.h" #include "include/core/SkSpan.h" #include "include/core/SkString.h" #include "include/core/SkTypes.h" #include "include/private/base/SkOnce.h" #include "include/private/base/SkTArray.h" #include "include/private/base/SkTemplates.h" #include "modules/skparagraph/include/DartTypes.h" #include "modules/skparagraph/include/FontCollection.h" #include "modules/skparagraph/include/Paragraph.h" #include "modules/skparagraph/include/ParagraphCache.h" #include "modules/skparagraph/include/ParagraphStyle.h" #include "modules/skparagraph/include/TextShadow.h" #include "modules/skparagraph/include/TextStyle.h" #include "modules/skparagraph/src/Run.h" #include "modules/skparagraph/src/TextLine.h" #include "modules/skunicode/include/SkUnicode.h" #include "src/base/SkBitmaskEnum.h" #include "src/core/SkTHash.h" #include #include #include class SkCanvas; namespace skia { namespace textlayout { class LineMetrics; class TextLine; template bool operator==(const SkSpan& a, const SkSpan& b) { return a.size() == b.size() && a.begin() == b.begin(); } template bool operator<=(const SkSpan& a, const SkSpan& b) { return a.begin() >= b.begin() && a.end() <= b.end(); } template struct StyleBlock { StyleBlock() : fRange(EMPTY_RANGE), fStyle() { } StyleBlock(size_t start, size_t end, const TStyle& style) : fRange(start, end), fStyle(style) {} StyleBlock(TextRange textRange, const TStyle& style) : fRange(textRange), fStyle(style) {} void add(TextRange tail) { SkASSERT(fRange.end == tail.start); fRange = TextRange(fRange.start, fRange.start + fRange.width() + tail.width()); } TextRange fRange; TStyle fStyle; }; struct ResolvedFontDescriptor { ResolvedFontDescriptor(TextIndex index, SkFont font) : fFont(std::move(font)), fTextStart(index) {} SkFont fFont; TextIndex fTextStart; }; enum InternalState { kUnknown = 0, kIndexed = 1, // Text is indexed kShaped = 2, // Text is shaped kLineBroken = 5, kFormatted = 6, kDrawn = 7 }; /* struct BidiRegion { BidiRegion(size_t start, size_t end, uint8_t dir) : text(start, end), direction(dir) { } TextRange text; uint8_t direction; }; */ class ParagraphImpl final : public Paragraph { public: ParagraphImpl(const SkString& text, ParagraphStyle style, skia_private::TArray blocks, skia_private::TArray placeholders, sk_sp fonts, sk_sp unicode); ParagraphImpl(const std::u16string& utf16text, ParagraphStyle style, skia_private::TArray blocks, skia_private::TArray placeholders, sk_sp fonts, sk_sp unicode); ~ParagraphImpl() override; void layout(SkScalar width) override; void paint(SkCanvas* canvas, SkScalar x, SkScalar y) override; void paint(ParagraphPainter* canvas, SkScalar x, SkScalar y) override; std::vector getRectsForRange(unsigned start, unsigned end, RectHeightStyle rectHeightStyle, RectWidthStyle rectWidthStyle) override; std::vector getRectsForPlaceholders() override; void getLineMetrics(std::vector&) override; PositionWithAffinity getGlyphPositionAtCoordinate(SkScalar dx, SkScalar dy) override; SkRange getWordBoundary(unsigned offset) override; bool getApplyRoundingHack() const { return fParagraphStyle.getApplyRoundingHack(); } size_t lineNumber() override { return fLines.size(); } TextLine& addLine(SkVector offset, SkVector advance, TextRange textExcludingSpaces, TextRange text, TextRange textIncludingNewlines, ClusterRange clusters, ClusterRange clustersWithGhosts, SkScalar widthWithSpaces, InternalLineMetrics sizes); SkSpan text() const { return SkSpan(fText.c_str(), fText.size()); } InternalState state() const { return fState; } SkSpan runs() { return SkSpan(fRuns.data(), fRuns.size()); } SkSpan styles() { return SkSpan(fTextStyles.data(), fTextStyles.size()); } SkSpan placeholders() { return SkSpan(fPlaceholders.data(), fPlaceholders.size()); } SkSpan lines() { return SkSpan(fLines.data(), fLines.size()); } const ParagraphStyle& paragraphStyle() const { return fParagraphStyle; } SkSpan clusters() { return SkSpan(fClusters.begin(), fClusters.size()); } sk_sp fontCollection() const { return fFontCollection; } void formatLines(SkScalar maxWidth); void ensureUTF16Mapping(); skia_private::TArray countSurroundingGraphemes(TextRange textRange) const; TextIndex findNextGraphemeBoundary(TextIndex utf8) const; TextIndex findPreviousGraphemeBoundary(TextIndex utf8) const; TextIndex findNextGlyphClusterBoundary(TextIndex utf8) const; TextIndex findPreviousGlyphClusterBoundary(TextIndex utf8) const; size_t getUTF16Index(TextIndex index) const { return fUTF16IndexForUTF8Index[index]; } bool strutEnabled() const { return paragraphStyle().getStrutStyle().getStrutEnabled(); } bool strutForceHeight() const { return paragraphStyle().getStrutStyle().getForceStrutHeight(); } bool strutHeightOverride() const { return paragraphStyle().getStrutStyle().getHeightOverride(); } InternalLineMetrics strutMetrics() const { return fStrutMetrics; } SkString getEllipsis() const; SkSpan text(TextRange textRange); SkSpan clusters(ClusterRange clusterRange); Cluster& cluster(ClusterIndex clusterIndex); ClusterIndex clusterIndex(TextIndex textIndex) { auto clusterIndex = this->fClustersIndexFromCodeUnit[textIndex]; SkASSERT(clusterIndex != EMPTY_INDEX); return clusterIndex; } Run& run(RunIndex runIndex) { SkASSERT(runIndex < SkToSizeT(fRuns.size())); return fRuns[runIndex]; } Run& runByCluster(ClusterIndex clusterIndex); SkSpan blocks(BlockRange blockRange); Block& block(BlockIndex blockIndex); skia_private::TArray resolvedFonts() const { return fFontSwitches; } void markDirty() override { if (fState > kIndexed) { fState = kIndexed; } } int32_t unresolvedGlyphs() override; std::unordered_set unresolvedCodepoints() override; void addUnresolvedCodepoints(TextRange textRange); void setState(InternalState state); sk_sp getPicture() { return fPicture; } SkScalar widthWithTrailingSpaces() { return fMaxWidthWithTrailingSpaces; } void resetContext(); void resolveStrut(); bool computeCodeUnitProperties(); void applySpacingAndBuildClusterTable(); void buildClusterTable(); bool shapeTextIntoEndlessLine(); void breakShapedTextIntoLines(SkScalar maxWidth); void updateTextAlign(TextAlign textAlign) override; void updateFontSize(size_t from, size_t to, SkScalar fontSize) override; void updateForegroundPaint(size_t from, size_t to, SkPaint paint) override; void updateBackgroundPaint(size_t from, size_t to, SkPaint paint) override; void visit(const Visitor&) override; void extendedVisit(const ExtendedVisitor&) override; int getPath(int lineNumber, SkPath* dest) override; bool containsColorFontOrBitmap(SkTextBlob* textBlob) override; bool containsEmoji(SkTextBlob* textBlob) override; int getLineNumberAt(TextIndex codeUnitIndex) const override; int getLineNumberAtUTF16Offset(size_t codeUnitIndex) override; bool getLineMetricsAt(int lineNumber, LineMetrics* lineMetrics) const override; TextRange getActualTextRange(int lineNumber, bool includeSpaces) const override; bool getGlyphClusterAt(TextIndex codeUnitIndex, GlyphClusterInfo* glyphInfo) override; bool getClosestGlyphClusterAt(SkScalar dx, SkScalar dy, GlyphClusterInfo* glyphInfo) override; bool getGlyphInfoAtUTF16Offset(size_t codeUnitIndex, GlyphInfo* graphemeInfo) override; bool getClosestUTF16GlyphInfoAt(SkScalar dx, SkScalar dy, GlyphInfo* graphemeInfo) override; SkFont getFontAt(TextIndex codeUnitIndex) const override; SkFont getFontAtUTF16Offset(size_t codeUnitIndex) override; std::vector getFonts() const override; InternalLineMetrics getEmptyMetrics() const { return fEmptyMetrics; } InternalLineMetrics getStrutMetrics() const { return fStrutMetrics; } BlockRange findAllBlocks(TextRange textRange); void resetShifts() { for (auto& run : fRuns) { run.resetJustificationShifts(); } } bool codeUnitHasProperty(size_t index, SkUnicode::CodeUnitFlags property) const { return (fCodeUnitProperties[index] & property) == property; } sk_sp getUnicode() { return fUnicode; } private: friend class ParagraphBuilder; friend class ParagraphCacheKey; friend class ParagraphCacheValue; friend class ParagraphCache; friend class TextWrapper; friend class OneLineShaper; void computeEmptyMetrics(); // Input skia_private::TArray> fLetterSpaceStyles; skia_private::TArray> fWordSpaceStyles; skia_private::TArray> fBackgroundStyles; skia_private::TArray> fForegroundStyles; skia_private::TArray>> fShadowStyles; skia_private::TArray> fDecorationStyles; skia_private::TArray fTextStyles; // TODO: take out only the font stuff skia_private::TArray fPlaceholders; SkString fText; // Internal structures InternalState fState; skia_private::TArray fRuns; // kShaped skia_private::TArray fClusters; // kClusterized (cached: text, word spacing, letter spacing, resolved fonts) skia_private::TArray fCodeUnitProperties; skia_private::TArray fClustersIndexFromCodeUnit; std::vector fWords; std::vector fBidiRegions; // These two arrays are used in measuring methods (getRectsForRange, getGlyphPositionAtCoordinate) // They are filled lazily whenever they need and cached skia_private::TArray fUTF8IndexForUTF16Index; skia_private::TArray fUTF16IndexForUTF8Index; SkOnce fillUTF16MappingOnce; size_t fUnresolvedGlyphs; std::unordered_set fUnresolvedCodepoints; skia_private::TArray fLines; // kFormatted (cached: width, max lines, ellipsis, text align) sk_sp fPicture; // kRecorded (cached: text styles) skia_private::TArray fFontSwitches; InternalLineMetrics fEmptyMetrics; InternalLineMetrics fStrutMetrics; SkScalar fOldWidth; SkScalar fOldHeight; SkScalar fMaxWidthWithTrailingSpaces; sk_sp fUnicode; bool fHasLineBreaks; bool fHasWhitespacesInside; TextIndex fTrailingSpaces; }; } // namespace textlayout } // namespace skia #endif // ParagraphImpl_DEFINED