• 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 <string>
18 #include "array_mgr.h"
19 #include "drawing_rect.h"
20 #include "drawing_text_run.h"
21 #include "skia_txt/run_impl.h"
22 
23 using namespace OHOS::Rosen;
24 typedef AdapterTxt::RunImpl RunImpl;
25 
OH_Drawing_GetRunGlyphCount(OH_Drawing_Run * run)26 uint32_t OH_Drawing_GetRunGlyphCount(OH_Drawing_Run* run)
27 {
28     if (run == nullptr) {
29         return 0;
30     }
31     return reinterpret_cast<AdapterTxt::RunImpl*>(run)->GetGlyphCount();
32 }
33 
OH_Drawing_GetRunStringRange(OH_Drawing_Run * run,uint64_t * location,uint64_t * length)34 void OH_Drawing_GetRunStringRange(OH_Drawing_Run* run, uint64_t* location, uint64_t* length)
35 {
36     if (location == nullptr || length == nullptr) {
37         return;
38     } else if (run == nullptr) {
39         *location = 0;
40         *length = 0;
41         return;
42     }
43     reinterpret_cast<AdapterTxt::RunImpl*>(run)->GetStringRange(location, length);
44 }
45 
OH_Drawing_GetRunStringIndices(OH_Drawing_Run * run,int64_t start,int64_t length)46 OH_Drawing_Array* OH_Drawing_GetRunStringIndices(OH_Drawing_Run* run, int64_t start, int64_t length)
47 {
48     if (run == nullptr || start < 0 || length < 0) {
49         return nullptr;
50     }
51     auto stringIndices = reinterpret_cast<AdapterTxt::RunImpl*>(run)->GetStringIndices(start, length);
52     if (stringIndices.size() == 0) {
53         return nullptr;
54     }
55     ObjectArray* array = new (std::nothrow) ObjectArray;
56     if (array == nullptr) {
57         return nullptr;
58     }
59     uint64_t* stringIndicesArr = new (std::nothrow) uint64_t[stringIndices.size()];
60     if (stringIndicesArr == nullptr) {
61         delete array;
62         return nullptr;
63     }
64     for (size_t i = 0; i < stringIndices.size(); ++i) {
65         stringIndicesArr[i] = stringIndices[i];
66     }
67     array->addr = stringIndicesArr;
68     array->num = stringIndices.size();
69     array->type = ObjectType::TEXT_RUN;
70     return reinterpret_cast<OH_Drawing_Array*>(array);
71 }
72 
OH_Drawing_GetRunStringIndicesByIndex(OH_Drawing_Array * stringIndicesArry,size_t index)73 uint64_t OH_Drawing_GetRunStringIndicesByIndex(OH_Drawing_Array* stringIndicesArry, size_t index)
74 {
75     ObjectArray* stringIndices = reinterpret_cast<ObjectArray*>(stringIndicesArry);
76     if (stringIndices && stringIndices->type == ObjectType::TEXT_RUN &&
77         index < stringIndices->num && stringIndices->addr) {
78         return reinterpret_cast<uint64_t*>(stringIndices->addr)[index];
79     }
80     return 0;
81 }
82 
OH_Drawing_DestroyRunStringIndices(OH_Drawing_Array * stringIndicesArry)83 void OH_Drawing_DestroyRunStringIndices(OH_Drawing_Array* stringIndicesArry)
84 {
85     ObjectArray* stringIndices = reinterpret_cast<ObjectArray*>(stringIndicesArry);
86     if (stringIndices && stringIndices->type == ObjectType::TEXT_RUN) {
87         delete[] reinterpret_cast<uint64_t*>(stringIndices->addr);
88         stringIndices->addr = nullptr;
89         stringIndices->num = 0;
90         stringIndices->type = ObjectType::INVALID;
91         delete stringIndices;
92     }
93 }
94 
OH_Drawing_GetRunTypographicBounds(OH_Drawing_Run * run,float * ascent,float * descent,float * leading)95 float OH_Drawing_GetRunTypographicBounds(OH_Drawing_Run* run, float* ascent, float* descent, float* leading)
96 {
97     if (ascent == nullptr || descent == nullptr || leading == nullptr) {
98         return 0.0;
99     } else if (run == nullptr) {
100         *ascent = 0;
101         *descent = 0;
102         *leading = 0;
103         return 0.0;
104     }
105     return reinterpret_cast<AdapterTxt::RunImpl*>(run)->GetTypographicBounds(ascent, descent, leading);
106 }
107 
OH_Drawing_GetRunImageBounds(OH_Drawing_Run * run)108 OH_Drawing_Rect* OH_Drawing_GetRunImageBounds(OH_Drawing_Run* run)
109 {
110     if (run == nullptr) {
111         return nullptr;
112     }
113     auto skRect = reinterpret_cast<AdapterTxt::RunImpl*>(run)->GetImageBounds();
114     return OH_Drawing_RectCreate(skRect.GetLeft(), skRect.GetTop(), skRect.GetRight(), skRect.GetBottom());
115 }
116 
OH_Drawing_DestroyRunImageBounds(OH_Drawing_Rect * rect)117 void OH_Drawing_DestroyRunImageBounds(OH_Drawing_Rect* rect)
118 {
119     if (rect) {
120         OH_Drawing_RectDestroy(rect);
121     }
122 }
123 
OH_Drawing_GetRunGlyphs(OH_Drawing_Run * run,int64_t start,int64_t length)124 OH_Drawing_Array* OH_Drawing_GetRunGlyphs(OH_Drawing_Run* run, int64_t start, int64_t length)
125 {
126     if (run == nullptr || start < 0 || length < 0) {
127         return nullptr;
128     }
129     auto glyphs = reinterpret_cast<AdapterTxt::RunImpl*>(run)->GetGlyphs(start, length);
130     if (glyphs.size() == 0) {
131         return nullptr;
132     }
133     ObjectArray* array = new (std::nothrow) ObjectArray;
134     if (array == nullptr) {
135         return nullptr;
136     }
137     uint16_t* glyphsArr = new (std::nothrow) uint16_t[glyphs.size()];
138     if (glyphsArr == nullptr) {
139         delete array;
140         return nullptr;
141     }
142     for (size_t i = 0; i < glyphs.size() ; ++i) {
143         glyphsArr[i] = glyphs[i];
144     }
145     array->addr = glyphsArr;
146     array->num = glyphs.size();
147     array->type = ObjectType::TEXT_RUN;
148     return reinterpret_cast<OH_Drawing_Array*>(array);
149 }
150 
OH_Drawing_GetRunGlyphsByIndex(OH_Drawing_Array * glyphsArray,size_t index)151 uint16_t OH_Drawing_GetRunGlyphsByIndex(OH_Drawing_Array* glyphsArray, size_t index)
152 {
153     ObjectArray* glyphs = reinterpret_cast<ObjectArray*>(glyphsArray);
154     if (glyphs && glyphs->type == ObjectType::TEXT_RUN &&
155         index < glyphs->num && glyphs->addr) {
156         return reinterpret_cast<uint16_t*>(glyphs->addr)[index];
157     }
158     return 0;
159 }
160 
OH_Drawing_DestroyRunGlyphs(OH_Drawing_Array * glyphsArray)161 void OH_Drawing_DestroyRunGlyphs(OH_Drawing_Array* glyphsArray)
162 {
163     ObjectArray* glyphs = reinterpret_cast<ObjectArray*>(glyphsArray);
164     if (glyphs && glyphs->type == ObjectType::TEXT_RUN) {
165         delete[] reinterpret_cast<uint16_t*>(glyphs->addr);
166         glyphs->addr = nullptr;
167         glyphs->num = 0;
168         glyphs->type = ObjectType::INVALID;
169         delete glyphs;
170     }
171 }
172 
OH_Drawing_GetRunPositions(OH_Drawing_Run * run,int64_t start,int64_t length)173 OH_Drawing_Array* OH_Drawing_GetRunPositions(OH_Drawing_Run* run, int64_t start, int64_t length)
174 {
175     if (run == nullptr || start < 0 || length < 0) {
176         return nullptr;
177     }
178 
179     auto positions = reinterpret_cast<AdapterTxt::RunImpl*>(run)->GetPositions(start, length);
180     if (positions.size() == 0) {
181         return nullptr;
182     }
183     Drawing::Point* positionsArr = new (std::nothrow) Drawing::Point[positions.size()];
184     if (positionsArr == nullptr) {
185         return nullptr;
186     }
187     ObjectArray* array = new (std::nothrow) ObjectArray;
188     if (array == nullptr) {
189         delete[] positionsArr;
190         return nullptr;
191     }
192     for (size_t i = 0; i < positions.size(); ++i) {
193         positionsArr[i] = positions[i];
194     }
195     array->addr = positionsArr;
196     array->num = positions.size();
197     array->type = ObjectType::TEXT_RUN;
198     return reinterpret_cast<OH_Drawing_Array*>(array);
199 }
200 
OH_Drawing_GetRunPositionsByIndex(OH_Drawing_Array * positionsArr,size_t index)201 OH_Drawing_Point* OH_Drawing_GetRunPositionsByIndex(OH_Drawing_Array* positionsArr, size_t index)
202 {
203     ObjectArray* positions = reinterpret_cast<ObjectArray*>(positionsArr);
204     if (positions && positions->type == ObjectType::TEXT_RUN &&
205         index < positions->num && positions->addr) {
206         Drawing::Point* positionsArr =  reinterpret_cast<Drawing::Point*>(positions->addr);
207         return reinterpret_cast<OH_Drawing_Point*>(&positionsArr[index]);
208     }
209     return nullptr;
210 }
211 
OH_Drawing_DestroyRunPositions(OH_Drawing_Array * positionsArray)212 void OH_Drawing_DestroyRunPositions(OH_Drawing_Array* positionsArray)
213 {
214     ObjectArray* positions = reinterpret_cast<ObjectArray*>(positionsArray);
215     if (positions && positions->addr && positions->type == ObjectType::TEXT_RUN) {
216         delete[] reinterpret_cast<Drawing::Point*>(positions->addr);
217         positions->num = 0;
218         positions->type = ObjectType::INVALID;
219         positions->addr = nullptr;
220         delete positions;
221     }
222 }
223 
OH_Drawing_GetRunFont(OH_Drawing_Run * run)224 OH_Drawing_Font* OH_Drawing_GetRunFont(OH_Drawing_Run* run)
225 {
226     if (run == nullptr) {
227         return nullptr;
228     }
229 
230     Drawing::Font* font = new Drawing::Font(reinterpret_cast<AdapterTxt::RunImpl*>(run)->GetFont());
231     return reinterpret_cast<OH_Drawing_Font*>(font);
232 }
233 
OH_Drawing_GetRunTextDirection(OH_Drawing_Run * run)234 OH_Drawing_TextDirection OH_Drawing_GetRunTextDirection(OH_Drawing_Run* run)
235 {
236     if (run == nullptr) {
237         return TEXT_DIRECTION_LTR;
238     }
239 
240     auto textDirection = reinterpret_cast<AdapterTxt::RunImpl*>(run)->GetTextDirection();
241     if (textDirection == TextDirection::RTL) {
242         return TEXT_DIRECTION_RTL;
243     } else {
244         return TEXT_DIRECTION_LTR;
245     }
246 }
247 
OH_Drawing_GetRunGlyphAdvances(OH_Drawing_Run * run,uint32_t start,uint32_t length)248 OH_Drawing_Array* OH_Drawing_GetRunGlyphAdvances(OH_Drawing_Run* run, uint32_t start, uint32_t length)
249 {
250     if (run == nullptr) {
251         return nullptr;
252     }
253 
254     auto advances = reinterpret_cast<AdapterTxt::RunImpl*>(run)->GetAdvances(start, length);
255     if (advances.size() == 0) {
256         return nullptr;
257     }
258     Drawing::Point* advancesArr = new (std::nothrow) Drawing::Point[advances.size()];
259     if (advancesArr == nullptr) {
260         return nullptr;
261     }
262     ObjectArray* array = new (std::nothrow) ObjectArray;
263     if (array == nullptr) {
264         delete[] advancesArr;
265         return nullptr;
266     }
267     for (size_t i = 0; i < advances.size(); ++i) {
268         advancesArr[i] = advances[i];
269     }
270     array->addr = advancesArr;
271     array->num = advances.size();
272     array->type = ObjectType::TEXT_RUN;
273     return reinterpret_cast<OH_Drawing_Array*>(array);
274 }
275 
OH_Drawing_GetRunGlyphAdvanceByIndex(OH_Drawing_Array * advances,size_t index)276 OH_Drawing_Point* OH_Drawing_GetRunGlyphAdvanceByIndex(OH_Drawing_Array* advances, size_t index)
277 {
278     ObjectArray* advancesArray = reinterpret_cast<ObjectArray*>(advances);
279     if (advancesArray != nullptr && advancesArray->type == ObjectType::TEXT_RUN &&
280         index < advancesArray->num && advancesArray->addr) {
281         Drawing::Point* advancesArr =  reinterpret_cast<Drawing::Point*>(advancesArray->addr);
282         return reinterpret_cast<OH_Drawing_Point*>(&advancesArr[index]);
283     }
284     return nullptr;
285 }
286 
OH_Drawing_DestroyRunGlyphAdvances(OH_Drawing_Array * advances)287 void OH_Drawing_DestroyRunGlyphAdvances(OH_Drawing_Array* advances)
288 {
289     ObjectArray* advancesArray = reinterpret_cast<ObjectArray*>(advances);
290     if (advancesArray != nullptr && advancesArray->addr && advancesArray->type == ObjectType::TEXT_RUN) {
291         delete[] reinterpret_cast<Drawing::Point*>(advancesArray->addr);
292         advancesArray->num = 0;
293         advancesArray->type = ObjectType::INVALID;
294         advancesArray->addr = nullptr;
295         delete advancesArray;
296     }
297 }
298 
OH_Drawing_RunPaint(OH_Drawing_Canvas * canvas,OH_Drawing_Run * run,double x,double y)299 void OH_Drawing_RunPaint(OH_Drawing_Canvas* canvas, OH_Drawing_Run* run, double x, double y)
300 {
301     if (canvas == nullptr || run == nullptr) {
302         return;
303     }
304     reinterpret_cast<AdapterTxt::RunImpl*>(run)->Paint(reinterpret_cast<Drawing::Canvas*>(canvas), x, y);
305 }