1 /*
2 * Copyright (c) 2024 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 #ifdef ENABLE_TEXT_ENHANCE
16 #include "ParagraphLineFetcherImpl.h"
17 namespace skia {
18 namespace textlayout {
ParagraphLineFetcherImpl(std::unique_ptr<Paragraph> && paragraph)19 ParagraphLineFetcherImpl::ParagraphLineFetcherImpl(
20 std::unique_ptr<Paragraph>&& paragraph) : fRootParagraph(move(paragraph)) {
21 fRootParagraph->initUnicodeText();
22 fUnicodeSize = fRootParagraph->unicodeText().size();
23 }
24
25 ParagraphLineFetcherImpl::~ParagraphLineFetcherImpl() = default;
26
getLineBreak(size_t startIndex,SkScalar width) const27 size_t ParagraphLineFetcherImpl::getLineBreak(size_t startIndex, SkScalar width) const {
28 if (startIndex >= fRootParagraph->unicodeText().size()) {
29 return 0;
30 }
31 std::unique_ptr<Paragraph> newParagraph = fRootParagraph->createCroppedCopy(startIndex);
32 if (newParagraph == nullptr) {
33 return 0;
34 }
35 newParagraph->layout(width);
36 TextRange textRange = newParagraph->getActualTextRange(0, true);
37 if (textRange == EMPTY_TEXT) {
38 return 0;
39 }
40 size_t count = newParagraph->getUnicodeIndex(textRange.end);
41 const std::vector<SkUnichar> unicodeText = newParagraph->unicodeText();
42 if (count < unicodeText.size() && unicodeText[count] == '\n') {
43 count++;
44 }
45 return count;
46 }
47
createLine(size_t startIndex,size_t count)48 std::unique_ptr<TextLineBase> ParagraphLineFetcherImpl::createLine(size_t startIndex, size_t count) {
49 size_t unicodeSize = fRootParagraph->unicodeText().size();
50 if (startIndex >= unicodeSize) {
51 return nullptr;
52 }
53 if (startIndex + count >= unicodeSize || count == 0) {
54 count = unicodeSize - startIndex;
55 }
56 fTempParagraph = fRootParagraph->createCroppedCopy(startIndex, count);
57 if (fTempParagraph == nullptr) {
58 return nullptr;
59 }
60 fTempParagraph->layout(std::numeric_limits<float>::max());
61 auto textLine = fTempParagraph->GetTextLines();
62 if (textLine.empty()) {
63 return nullptr;
64 }
65 return move(textLine.front());
66 }
67 } // namespace textlayout
68 } // namespace skia
69 #endif // ENABLE_TEXT_ENHANCE
70