• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright (c) 2024 Huawei Device Co., Ltd.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "drawing_text_lineTypography.h"
18 #include "array_mgr.h"
19 #include "rosen_text/typography_create.h"
20 #include "skia_txt/text_line_base.h"
21 #include "skia/txt/paragraph_line_fetcher.h"
22 #include "utils/text_log.h"
23 
24 using namespace OHOS::Rosen;
25 typedef OHOS::Rosen::AdapterTxt::TextLineBaseImpl LineImpl;
26 
OH_Drawing_CreateLineTypography(OH_Drawing_TypographyCreate * handler)27 OH_Drawing_LineTypography* OH_Drawing_CreateLineTypography(OH_Drawing_TypographyCreate* handler)
28 {
29     if (handler == nullptr) {
30         TEXT_LOGE("Null handler");
31         return nullptr;
32     }
33     TypographyCreate* rosenHandler = reinterpret_cast<TypographyCreate*>(handler);
34     std::unique_ptr<LineTypography> lineTypography = rosenHandler->CreateLineTypography();
35     if (lineTypography == nullptr) {
36         TEXT_LOGE("Failed to create line typography");
37         return nullptr;
38     }
39     return reinterpret_cast<OH_Drawing_LineTypography*>(lineTypography.release());
40 }
41 
OH_Drawing_DestroyLineTypography(OH_Drawing_LineTypography * lineTypography)42 void OH_Drawing_DestroyLineTypography(OH_Drawing_LineTypography* lineTypography)
43 {
44     delete reinterpret_cast<LineTypography*>(lineTypography);
45 }
46 
OH_Drawing_LineTypographyGetLineBreak(OH_Drawing_LineTypography * lineTypograph,size_t startIndex,double width)47 size_t OH_Drawing_LineTypographyGetLineBreak(OH_Drawing_LineTypography *lineTypograph, size_t startIndex, double width)
48 {
49     LineTypography* innerlineTypography = reinterpret_cast<LineTypography*>(lineTypograph);
50     size_t limitSize = innerlineTypography->GetUnicodeSize();
51     if (startIndex >= limitSize || width <= 0) {
52         return 0;
53     }
54     return innerlineTypography->GetLineBreak(startIndex, width);
55 }
56 
OH_Drawing_LineTypographyCreateLine(OH_Drawing_LineTypography * lineTypograph,size_t startIndex,size_t count)57 OH_Drawing_TextLine* OH_Drawing_LineTypographyCreateLine(OH_Drawing_LineTypography *lineTypograph,
58                                                          size_t startIndex, size_t count)
59 {
60     LineTypography* innerlineTypography = reinterpret_cast<LineTypography*>(lineTypograph);
61     size_t limitSize = innerlineTypography->GetUnicodeSize();
62     if (startIndex >= limitSize || count + startIndex > limitSize) {
63         TEXT_LOGE("Invalid param");
64         return nullptr;
65     }
66     void* lineFetcher = innerlineTypography->GetLineFetcher();
67     if (lineFetcher == nullptr) {
68         TEXT_LOGE("Failed to get line fetcher");
69         return nullptr;
70     }
71     SPText::ParagraphLineFetcher* spLineFetcher = reinterpret_cast<SPText::ParagraphLineFetcher*>(lineFetcher);
72     auto line = spLineFetcher->CreateLine(startIndex, count);
73     if (line == nullptr) {
74         TEXT_LOGE("Failed to get line");
75         return nullptr;
76     }
77     LineImpl* lineImpl = new (std::nothrow) LineImpl(std::move(line));
78     if (lineImpl == nullptr) {
79         TEXT_LOGE("Failed to new text line");
80         return nullptr;
81     }
82     LineObject* lineObject = new (std::nothrow) LineObject();
83     if (lineObject == nullptr) {
84         TEXT_LOGE("Failed to new line object");
85         delete lineImpl;
86         return nullptr;
87     }
88     lineObject->line = reinterpret_cast<void*>(lineImpl);
89     lineObject->isArray = false;
90     return reinterpret_cast<OH_Drawing_TextLine*>(lineObject);
91 }
92