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 >= start && codepoint < end) {
41             return codepoint + gid;
42         }
43     }
44 
45     return INVALID_GLYPH_ID;
46 }
47 
Dump() const48 void Ranges::Dump() const
49 {
50     for (const auto &[start, end, gid] : ranges_) {
51         LOGSO_FUNC_LINE(INFO) << "0x" << std::uppercase << std::hex
52             << std::setw(4) << std::setfill('0') << start  // 4 means output width
53             << " ~ 0x" << std::uppercase << std::hex
54             << std::setw(4) << std::setfill('0') << end  // 4 means output width
55             << ": offset " << std::dec << end;
56     }
57 
58     for (const auto &[codepoint, gid] : singles_) {
59         LOGSO_FUNC_LINE(INFO) << "0x" << std::uppercase << std::hex
60             << std::setw(4) << std::setfill('0') << codepoint  // 4 means output width
61             << ": glyphid " << std::dec
62             << (codepoint + gid) % (1 << 16); // 16 means offset; 1 << 16 means residual multiple
63     }
64 }
65 } // namespace TextEngine
66 } // namespace Rosen
67 } // namespace OHOS
68