1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ROSEN_TEXT_EXPORT_ROSEN_TEXT_TYPOGRAPHY_H
17 #define ROSEN_TEXT_EXPORT_ROSEN_TEXT_TYPOGRAPHY_H
18 
19 #include <cstddef>
20 #include <vector>
21 
22 #include "common/rs_macros.h"
23 #include "draw/canvas.h"
24 #include "include/core/SkCanvas.h" // SKIA
25 #include "text/font_metrics.h"
26 #include "utils/rect.h"
27 
28 #include "text_style.h"
29 #include "text_line_base.h"
30 #include "typography_types.h"
31 #include "symbol_animation_config.h"
32 
33 namespace OHOS {
34 namespace Rosen {
35 enum class TextRectWidthStyle {
36     TIGHT,
37     MAX,
38 };
39 
40 enum class TextRectHeightStyle {
41     TIGHT,
42     COVER_TOP_AND_BOTTOM,
43     COVER_HALF_TOP_AND_BOTTOM,
44     COVER_TOP,
45     COVER_BOTTOM,
46     FOLLOW_BY_STRUT,
47 };
48 
49 struct RS_EXPORT TextRect {
50     Drawing::RectF rect;
51     TextDirection direction;
52     TextRect(Drawing::RectF rec, TextDirection dir);
53 };
54 
55 enum class Affinity {
56     PREV,
57     NEXT,
58 };
59 
60 struct IndexAndAffinity {
61     size_t index;
62     Affinity affinity;
63     IndexAndAffinity(size_t charIndex, Affinity charAffinity);
64 };
65 
66 class RunMetrics {
67 public:
RunMetrics(const TextStyle * style)68     explicit RunMetrics(const TextStyle* style) : textStyle(style) {}
69 
RunMetrics(const TextStyle * style,const Drawing::FontMetrics & metrics)70     RunMetrics(const TextStyle* style, const Drawing::FontMetrics& metrics)
71         : textStyle(style), fontMetrics(metrics) {}
72 
73     const TextStyle* textStyle;
74     Drawing::FontMetrics fontMetrics;
75 };
76 
77 struct LineMetrics {
78     /** Text ascender height */
79     double ascender;
80     /** Tex descender height */
81     double descender;
82     /** The height of a capital letter */
83     double capHeight;
84     /** The height of a lowercase letter */
85     double xHeight;
86     /** Text width */
87     double width;
88     /** Line height */
89     double height;
90     /**
91      * The distance from the left end of the text to the left end of the container,
92      * aligned to 0, is the width of the container minus the width of the line of text
93      */
94     double x;
95     /**
96      * The height from the top of the text to the top of the container, the first line is 0,
97      *  and the second line is the height of the first line
98      */
99     double y;
100     /** Start Index */
101     size_t startIndex;
102     /** End Index */
103     size_t endIndex;
104 
105     Drawing::FontMetrics firstCharMetrics;
106     /** The y position of the baseline for this line from the top of the paragraph */
107     double baseline;
108     /** Zero indexed line number */
109     size_t lineNumber;
110 
111     std::map<size_t, RunMetrics> runMetrics;
112 };
113 
114 class Typography {
115 public:
116     virtual ~Typography() = default;
117 
118     virtual double GetMaxWidth() const = 0;
119     virtual double GetHeight() const = 0;
120     virtual double GetActualWidth() const = 0;
121     virtual double GetMinIntrinsicWidth() = 0;
122     virtual double GetMaxIntrinsicWidth() = 0;
123     virtual double GetAlphabeticBaseline() = 0;
124     virtual double GetIdeographicBaseline() = 0;
125     virtual double GetGlyphsBoundsTop() = 0;
126     virtual double GetGlyphsBoundsBottom() = 0;
127     virtual double GetGlyphsBoundsLeft() = 0;
128     virtual double GetGlyphsBoundsRight() = 0;
129     virtual bool DidExceedMaxLines() const = 0;
130     virtual int GetLineCount() const = 0;
131     virtual void MarkDirty() = 0;
132     virtual int32_t GetUnresolvedGlyphsCount() = 0;
133     virtual void UpdateFontSize(size_t from, size_t to, float fontSize) = 0;
134 
135     virtual void SetIndents(const std::vector<float>& indents) = 0;
136     virtual float DetectIndents(size_t index) = 0;
137     virtual void Layout(double width) = 0;
138     virtual void Paint(SkCanvas *canvas, double x, double y) = 0; // SKIA
139     virtual void Paint(Drawing::Canvas *canvas, double x, double y) = 0; // DRAWING
140     virtual void Paint(Drawing::Canvas* canvas, Drawing::Path* path, double hOffset, double vOffset) = 0; // DRAWING
141 
142     virtual std::vector<TextRect> GetTextRectsByBoundary(size_t left, size_t right,
143         TextRectHeightStyle heightStyle, TextRectWidthStyle widthStyle) = 0;
144     virtual std::vector<TextRect> GetTextRectsOfPlaceholders() = 0;
145     virtual IndexAndAffinity GetGlyphIndexByCoordinate(double x, double y) = 0;
146     virtual Boundary GetWordBoundaryByIndex(size_t index) = 0;
147     virtual Boundary GetActualTextRange(int lineNumber, bool includeSpaces) = 0;
148     virtual double GetLineHeight(int lineNumber) = 0;
149     virtual double GetLineWidth(int lineNumber) = 0;
150     virtual void SetAnimation(
151         std::function<bool(const std::shared_ptr<TextEngine::SymbolAnimationConfig>&)>& animationFunc)= 0;
152     virtual void SetParagraghId(uint32_t id) = 0;
153     virtual Drawing::FontMetrics MeasureText() = 0;
154     virtual bool GetLineInfo(int lineNumber, bool oneLine, bool includeWhitespace, LineMetrics* lineMetrics) = 0;
155     virtual std::vector<LineMetrics> GetLineMetrics() = 0;
156     virtual bool GetLineMetricsAt(int lineNumber, LineMetrics* lineMetrics) = 0;
157     virtual Drawing::FontMetrics GetFontMetrics(const OHOS::Rosen::TextStyle& textStyle) = 0;
158     virtual bool GetLineFontMetrics(const size_t lineNumber,
159         size_t& charNumber, std::vector<Drawing::FontMetrics>& fontMetrics) = 0;
160     virtual std::vector<std::unique_ptr<TextLineBase>> GetTextLines() const = 0;
161     virtual std::unique_ptr<Typography> CloneSelf() = 0;
162     virtual double GetLongestLineWithIndent() const = 0;
163     virtual void UpdateColor(size_t from, size_t to, const Drawing::Color& color) = 0;
164     virtual Drawing::RectI GeneratePaintRegion(double x, double y) const = 0;
165 };
166 } // namespace Rosen
167 } // namespace OHOS
168 
169 #endif // ROSEN_TEXT_EXPORT_ROSEN_TEXT_TYPOGRAPHY_H
170