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 "ranges.h"
17
18 #include <iomanip>
19 #include "texgine/utils/exlog.h"
20
21 namespace OHOS {
22 namespace Rosen {
23 namespace TextEngine {
AddRange(const struct Range & range)24 void Ranges::AddRange(const struct Range &range)
25 {
26 if (range.end - range.start == 1) {
27 singles_[range.start] = range.gid;
28 } else {
29 ranges_.push_back(range);
30 }
31 }
32
GetGlyphId(uint32_t codepoint) const33 int32_t Ranges::GetGlyphId(uint32_t codepoint) const
34 {
35 if (const auto &it = singles_.find(codepoint); it != singles_.end()) {
36 return it->first + it->second;
37 }
38
39 for (const auto &[start, end, gid] : ranges_) {
40 if (codepoint > end) {
41 continue;
42 }
43
44 if (codepoint >= start) {
45 return codepoint + gid;
46 }
47 break;
48 }
49
50 return INVALID_GLYPH_ID;
51 }
52
Dump() const53 void Ranges::Dump() const
54 {
55 for (const auto &[start, end, gid] : ranges_) {
56 // 4 means output width,0 means fill with 0
57 LOGSO_FUNC_LINE(INFO) << "0x" << std::uppercase << std::hex << std::setw(4) << std::setfill('0') << start
58 << " ~ 0x" << std::uppercase << std::hex << std::setw(4) << std::setfill('0') << end
59 << ": offset " << std::dec << end;
60 }
61
62 for (const auto &[codepoint, gid] : singles_) {
63 // 4 means output width,1 means operand, 16 means offset, 1 << 16 means residual multiple
64 LOGSO_FUNC_LINE(INFO) << "0x" << std::uppercase << std::hex << std::setw(4) << std::setfill('0') << codepoint
65 << ": glyphid " << std::dec << (codepoint + gid) % (1 << 16);
66 }
67 }
68 } // namespace TextEngine
69 } // namespace Rosen
70 } // namespace OHOS
71