1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
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 // Definitions internal to Minikin
18 
19 #ifndef MINIKIN_INTERNAL_H
20 #define MINIKIN_INTERNAL_H
21 
22 #include <hb.h>
23 #include <utils/Log.h>
24 #include <utils/Mutex.h>
25 
26 #include <algorithm>
27 #include <memory>
28 #include <unordered_set>
29 
30 #include "minikin/FontVariation.h"
31 #include "minikin/HbUtils.h"
32 #include "minikin/MinikinFont.h"
33 
34 namespace minikin {
35 
36 #ifdef ENABLE_ASSERTION
37 #define MINIKIN_ASSERT(cond, ...) LOG_ALWAYS_FATAL_IF(!(cond), __VA_ARGS__)
38 #else
39 #define MINIKIN_ASSERT(cond, ...) ((void)0)
40 #endif
41 
42 #define MINIKIN_NOT_REACHED(...) MINIKIN_ASSERT(false, __VA_ARGS__);
43 
44 constexpr uint32_t MAX_UNICODE_CODE_POINT = 0x10FFFF;
45 
46 constexpr uint32_t VS1 = 0xFE00;
47 constexpr uint32_t VS16 = 0xFE0F;
48 constexpr uint32_t VS17 = 0xE0100;
49 constexpr uint32_t VS256 = 0xE01EF;
50 
51 // Returns variation selector index. This is one unit less than the variation selector number. For
52 // example, VARIATION SELECTOR-25 maps to 24.
53 // [0x00-0x0F] for U+FE00..U+FE0F
54 // [0x10-0xFF] for U+E0100..U+E01EF
55 // INVALID_VS_INDEX for other input.
56 constexpr uint16_t INVALID_VS_INDEX = 0xFFFF;
57 uint16_t getVsIndex(uint32_t codePoint);
58 
59 // Returns true if the code point is a variation selector.
60 // Note that this function returns false for Mongolian free variation selectors.
61 bool isVariationSelector(uint32_t codePoint);
62 
63 // An RAII accessor for hb_blob_t
64 class HbBlob {
65 public:
HbBlob(const HbFaceUniquePtr & face,uint32_t tag)66     HbBlob(const HbFaceUniquePtr& face, uint32_t tag)
67             : mBlob(hb_face_reference_table(face.get(), tag)) {}
HbBlob(const HbFontUniquePtr & font,uint32_t tag)68     HbBlob(const HbFontUniquePtr& font, uint32_t tag)
69             : mBlob(hb_face_reference_table(hb_font_get_face(font.get()), tag)) {}
70 
get()71     inline const uint8_t* get() const {
72         return reinterpret_cast<const uint8_t*>(hb_blob_get_data(mBlob.get(), nullptr));
73     }
74 
size()75     inline size_t size() const { return (size_t)hb_blob_get_length(mBlob.get()); }
76 
77     inline operator bool() const { return size() > 0; }
78 
79 private:
80     HbBlobUniquePtr mBlob;
81 };
82 
83 template <typename T>
sortedArrayFromSet(const std::unordered_set<T> & set)84 std::unique_ptr<T[]> sortedArrayFromSet(const std::unordered_set<T>& set) {
85     std::unique_ptr<T[]> array(new T[set.size()]);
86     std::copy(set.begin(), set.end(), array.get());
87     std::sort(array.get(), array.get() + set.size());
88     return array;
89 }
90 
91 }  // namespace minikin
92 
93 #endif  // MINIKIN_INTERNAL_H
94