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 #include "ParagraphLineFetcherImpl.h"
16 namespace skia {
17 namespace textlayout {
ParagraphLineFetcherImpl(std::unique_ptr<Paragraph>&& paragraph)18 ParagraphLineFetcherImpl::ParagraphLineFetcherImpl(std::unique_ptr<Paragraph>&& paragraph)
19         : fRootParagraph(move(paragraph)) {
20     fRootParagraph->initUnicodeText();
21     fUnicodeSize = fRootParagraph->unicodeText().size();
22 }
23 
24 ParagraphLineFetcherImpl::~ParagraphLineFetcherImpl() = default;
25 
getLineBreak(size_t startIndex, SkScalar width) const26 size_t ParagraphLineFetcherImpl::getLineBreak(size_t startIndex, SkScalar width) const {
27     if (startIndex >= fRootParagraph->unicodeText().size()) {
28         return 0;
29     }
30     auto newParagraph = fRootParagraph->createCroppedCopy(startIndex);
31     if (newParagraph == nullptr) {
32         return 0;
33     }
34     newParagraph->layout(width);
35     auto textRange = newParagraph->getActualTextRange(0, true);
36     if (textRange == EMPTY_TEXT) {
37         return 0;
38     }
39     auto count = newParagraph->getUnicodeIndex(textRange.end);
40     auto unicodeText = newParagraph->unicodeText();
41     if (count < unicodeText.size() && unicodeText[count] == '\n') {
42         count++;
43     }
44     return count;
45 }
46 
createLine(size_t startIndex, size_t count)47 std::unique_ptr<TextLineBase> ParagraphLineFetcherImpl::createLine(size_t startIndex,
48                                                                    size_t count) {
49     auto 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(SkFloatBits_GetFiniteFloat());
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