• 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 
26 namespace OHOS {
27 namespace Rosen {
28 namespace TextEngine {
29 struct StrutMetrics {
30     double ascent = 0;
31     double descent = 0;
32     double halfLeading = 0;
33 };
34 
35 struct CalcResult {
36     bool need = true;
37     double ascent = 0;
38     double descent = 0;
39 };
40 
41 class TypographyImpl : public Typography {
42 public:
43     TypographyImpl(TypographyStyle &ys, std::vector<VariantSpan> &spans,
44         std::shared_ptr<FontProviders> providers);
45 
46     double GetMaxWidth() const override;
47     double GetHeight() const override;
48     double GetActualWidth() const override;
49     double GetMinIntrinsicWidth() const override;
50     double GetMaxIntrinsicWidth() const override;
51     double GetAlphabeticBaseline() const override;
52     double GetIdeographicBaseline() const override;
53     bool DidExceedMaxLines() const override;
54     int GetLineCount() const override;
55     void SetIndents(const std::vector<float> &indents) override;
56     void Layout(double maxWidth) override;
57     void Paint(TexgineCanvas &canvas, double offsetX, double offsetY) override;
58     std::vector<TextRect> GetTextRectsByBoundary(Boundary boundary,
59                                                  TextRectHeightStyle heightStyle,
60                                                  TextRectWidthStyle widthStyle) const override;
61     std::vector<TextRect> GetTextRectsOfPlaceholders() const override;
62     IndexAndAffinity GetGlyphIndexByCoordinate(double x, double y) const override;
63     Boundary GetWordBoundaryByIndex(size_t index) const override;
64 
65 private:
66     void ReportMemoryUsage(const std::string &member, bool needThis) const override;
67 
68     void ComputeIntrinsicWidth();
69     void ConsiderEllipsis();
70     int ComputeStrut();
71     void DoLayout();
72     int UpdateMetrics();
73     int UpdateSpanMetrics(VariantSpan &span, double &coveredAscent);
74     int DoUpdateSpanMetrics(const VariantSpan &span, const TexgineFontMetrics &metrics,
75         const TextStyle &style, double &coveredAscent);
76     void UpadateAnySpanMetrics(std::shared_ptr<AnySpan> &span, double &coveredAscent, double &coveredDescent);
77     void ApplyAlignment();
78     size_t FindGlyphTargetLine(double y) const;
79     size_t FindGlyphTargetIndex(size_t line, double x, double &offsetX, std::vector<double> &widths) const;
80     std::vector<TextRect> MergeRects(const std::vector<TextRect> &boxes, Boundary boundary) const;
81     void ComputeWordBoundary() const;
82     void ComputeSpans(int lineIndex, double baseline, const CalcResult &calcResult,
83         std::vector<TextRect> &lineBoxes) const;
84 
85     TypographyStyle typographyStyle_;
86     std::vector<VariantSpan> spans_;
87     std::shared_ptr<FontProviders> fontProviders_;
88 
89     std::vector<LineMetrics> lineMetrics_;
90     mutable std::vector<Boundary> boundariesCache_;
91     bool didExceedMaxLines_ = false;
92     StrutMetrics strut_;
93     std::vector<double> baselines_;
94     std::vector<double> lineMaxAscent_;
95     std::vector<double> lineMaxCoveredAscent_;
96     std::vector<double> lineMaxCoveredDescent_;
97     double maxWidth_ = 0.0;
98     double maxLineWidth_ = 0.0;
99     double height_ = 0.0;
100     double maxIntrinsicWidth_ = 0.0;
101     double minIntrinsicWidth_ = 0.0;
102 };
103 } // namespace TextEngine
104 } // namespace Rosen
105 } // namespace OHOS
106 
107 #endif // ROSEN_MODULES_TEXGINE_SRC_TYPOGRAPHY_IMPL_H
108