• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
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_MODULES_TEXGINE_SRC_TYPOGRAPHY_IMPL_H
17 #define ROSEN_MODULES_TEXGINE_SRC_TYPOGRAPHY_IMPL_H
18 
19 #include <string>
20 #include <vector>
21 
22 #include "line_metrics.h"
23 #include "texgine/typography.h"
24 #include "texgine/typography_types.h"
25 #include "symbol_animation_config.h"
26 
27 namespace OHOS {
28 namespace Rosen {
29 namespace TextEngine {
30 struct StrutMetrics {
31     double ascent = 0;
32     double descent = 0;
33     double halfLeading = 0;
34 };
35 
36 struct CalcResult {
37     bool need = true;
38     double ascent = 0;
39     double descent = 0;
40 };
41 
42 class TypographyImpl : public Typography {
43 public:
44     TypographyImpl(TypographyStyle &ys, std::vector<VariantSpan> &spans,
45         std::shared_ptr<FontProviders> providers);
46 
47     double GetMaxWidth() const override;
48     double GetHeight() const override;
49     double GetActualWidth() const override;
50     double GetMinIntrinsicWidth() const override;
51     double GetMaxIntrinsicWidth() const override;
52     double GetAlphabeticBaseline() const override;
53     double GetIdeographicBaseline() const override;
54     bool DidExceedMaxLines() const override;
55     int GetLineCount() const override;
56     void SetIndents(const std::vector<float> &indents) override;
57     void Layout(double maxWidth) override;
58 
59     void Paint(TexgineCanvas &canvas, double offsetX, double offsetY) override;
60 
61     std::vector<TextRect> GetTextRectsByBoundary(Boundary boundary,
62                                                  TextRectHeightStyle heightStyle,
63                                                  TextRectWidthStyle widthStyle) const override;
64     std::vector<TextRect> GetTextRectsOfPlaceholders() const override;
65     IndexAndAffinity GetGlyphIndexByCoordinate(double x, double y) const override;
66     Boundary GetWordBoundaryByIndex(size_t index) const override;
67     Boundary GetActualTextRange(int lineNumber, bool includeSpaces) const override;
68     double GetLineHeight(int lineNumber);
69     double GetLineWidth(int lineNumber);
70 
SetAnimation(std::function<bool (const std::shared_ptr<TextEngine::SymbolAnimationConfig> &)> & animationFunc)71     void SetAnimation(
72         std::function<bool(
73             const std::shared_ptr<TextEngine::SymbolAnimationConfig>&)>& animationFunc) override
74     {
75         if (animationFunc) {
76             animationFunc_ = animationFunc;
77         }
78     }
79 private:
80 
81     std::function<bool(const std::shared_ptr<SymbolAnimationConfig>&)> animationFunc_ = nullptr;
82 
83     void ReportMemoryUsage(const std::string &member, bool needThis) const override;
84 
85     int ComputeStrut();
86     void DoLayout();
87     int UpdateMetrics();
88     int UpdateSpanMetrics(VariantSpan &span, double &coveredAscent);
89     int DoUpdateSpanMetrics(const VariantSpan &span, const std::shared_ptr<TexgineFontMetrics> metrics,
90         const TextStyle &style, double &coveredAscent);
91     void UpadateAnySpanMetrics(std::shared_ptr<AnySpan> &span, double &coveredAscent, double &coveredDescent);
92     void ApplyAlignment();
93     size_t FindGlyphTargetLine(double y) const;
94     size_t FindGlyphTargetIndex(size_t line, double x, double &offsetX, std::vector<double> &widths) const;
95     std::vector<TextRect> MergeRects(const std::vector<TextRect> &boxes, Boundary boundary) const;
96     void ComputeWordBoundary() const;
97     void ComputeSpans(int lineIndex, double baseline, const CalcResult &calcResult,
98         std::vector<TextRect> &lineBoxes) const;
99     std::vector<TextRect> GenTextRects(std::shared_ptr<TextSpan> &ts, double offsetX, double offsetY,
100         double spanGapWidth) const;
101     void ComputeRoundRect(VariantSpan& span, int& index, int& preIndex, LineMetrics& metric,
102         std::vector<VariantSpan>& groupSpans);
103     TypographyStyle typographyStyle_;
104     std::vector<VariantSpan> spans_;
105     std::shared_ptr<FontProviders> fontProviders_;
106 
107     std::vector<LineMetrics> lineMetrics_;
108     mutable std::vector<Boundary> boundariesCache_;
109     bool didExceedMaxLines_ = false;
110     StrutMetrics strut_;
111     std::vector<double> baselines_;
112     std::vector<double> lineMaxAscent_;
113     std::vector<double> lineMaxCoveredAscent_;
114     std::vector<double> lineMaxCoveredDescent_;
115     double maxWidth_ = 0.0;
116     double maxLineWidth_ = 0.0;
117     float descent_ = 0.0;
118     double height_ = 0.0;
119     std::vector<double> yOffsets_ = {};
120     double maxIntrinsicWidth_ = 0.0;
121     double minIntrinsicWidth_ = 0.0;
122     std::vector<float> indents_;
123 };
124 } // namespace TextEngine
125 } // namespace Rosen
126 } // namespace OHOS
127 
128 #endif // ROSEN_MODULES_TEXGINE_SRC_TYPOGRAPHY_IMPL_H
129