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 #include "shaper.h"
17
18 #include <queue>
19 #include <variant>
20
21 #include "bidi_processer.h"
22 #include "line_breaker.h"
23 #include "measurer.h"
24 #include "texgine/any_span.h"
25 #include "texgine_exception.h"
26 #include "texgine/utils/exlog.h"
27 #include "texgine/utils/trace.h"
28 #include "text_breaker.h"
29 #include "text_merger.h"
30 #include "text_reverser.h"
31 #include "text_shaper.h"
32
33 namespace OHOS {
34 namespace Rosen {
35 namespace TextEngine {
36 namespace {
DumpLineMetrics(const std::vector<LineMetrics> & lineMetrics)37 void DumpLineMetrics(const std::vector<LineMetrics> &lineMetrics)
38 {
39 LOGSCOPED(sl, LOGEX_FUNC_LINE_DEBUG(), "DumpLineMetrics");
40 for (const auto &metric : lineMetrics) {
41 for (const auto &span : metric.lineSpans) {
42 span.Dump();
43 }
44 }
45 }
46 } // namespace
47
DoShape(std::vector<VariantSpan> spans,const TypographyStyle & tstyle,const std::shared_ptr<FontProviders> & fontProviders,const double widthLimit)48 std::vector<LineMetrics> Shaper::DoShape(std::vector<VariantSpan> spans, const TypographyStyle &tstyle,
49 const std::shared_ptr<FontProviders> &fontProviders, const double widthLimit)
50 {
51 ScopedTrace scope("Shaper::DoShape");
52 TextBreaker tb;
53 auto ret = tb.WordBreak(spans, tstyle, fontProviders);
54 if (ret) {
55 LOGEX_FUNC_LINE(ERROR) << "word break failed";
56 return {};
57 }
58
59 BidiProcesser bp;
60 auto newSpans = bp.ProcessBidiText(spans, tstyle.direction);
61 if (newSpans.empty()) {
62 LOGEX_FUNC_LINE(ERROR) << "Process BidiText failed";
63 return {};
64 }
65
66 LineBreaker lb;
67 auto lineMetrics = lb.BreakLines(newSpans, tstyle, widthLimit);
68
69 TextMerger tm;
70 for (auto &metric : lineMetrics) {
71 auto res = tm.MergeSpans(metric.lineSpans);
72 std::swap(res, metric.lineSpans);
73 }
74
75 TextReverser tr;
76 for (auto &metric : lineMetrics) {
77 tr.ReverseRTLText(metric.lineSpans);
78 tr.ProcessTypoDirection(metric.lineSpans, tstyle.direction);
79 }
80
81 TextShaper textShaper;
82 for (const auto &metric : lineMetrics) {
83 for (const auto &span : metric.lineSpans) {
84 textShaper.Shape(span, tstyle, fontProviders);
85 }
86 }
87 DumpLineMetrics(lineMetrics);
88 return lineMetrics;
89 }
90 } // namespace TextEngine
91 } // namespace Rosen
92 } // namespace OHOS
93