1 /*
2 * Copyright (C) 2017 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 #include "FontUtils.h"
18
19 #include <cstdint>
20
21 #include <log/log.h>
22
23 namespace minikin {
24
readU16(const uint8_t * data,size_t offset)25 static uint16_t readU16(const uint8_t* data, size_t offset) {
26 return data[offset] << 8 | data[offset + 1];
27 }
28
readU32(const uint8_t * data,size_t offset)29 static uint32_t readU32(const uint8_t* data, size_t offset) {
30 return ((uint32_t)data[offset]) << 24 | ((uint32_t)data[offset + 1]) << 16 |
31 ((uint32_t)data[offset + 2]) << 8 | ((uint32_t)data[offset + 3]);
32 }
33
read1616Fixed(const uint8_t * data,size_t offset)34 static float read1616Fixed(const uint8_t* data, size_t offset) {
35 uint32_t bits = readU32(data, offset);
36 int32_t fixed = *reinterpret_cast<int32_t*>(&bits);
37 return fixed / float(0x10000);
38 }
39
analyzeStyle(const uint8_t * os2_data,size_t os2_size,int * weight,bool * italic)40 bool analyzeStyle(const uint8_t* os2_data, size_t os2_size, int* weight, bool* italic) {
41 const size_t kUsWeightClassOffset = 4;
42 const size_t kFsSelectionOffset = 62;
43 const uint16_t kItalicFlag = (1 << 0);
44 if (os2_size < kFsSelectionOffset + 2) {
45 return false;
46 }
47 uint16_t weightClass = readU16(os2_data, kUsWeightClassOffset);
48 *weight = weightClass;
49 uint16_t fsSelection = readU16(os2_data, kFsSelectionOffset);
50 *italic = (fsSelection & kItalicFlag) != 0;
51 return true;
52 }
53
analyzeAxes(const uint8_t * fvar_data,size_t fvar_size,std::unordered_set<uint32_t> * axes)54 bool analyzeAxes(const uint8_t* fvar_data, size_t fvar_size, std::unordered_set<uint32_t>* axes) {
55 const size_t kMajorVersionOffset = 0;
56 const size_t kMinorVersionOffset = 2;
57 const size_t kOffsetToAxesArrayOffset = 4;
58 const size_t kAxisCountOffset = 8;
59 const size_t kAxisSizeOffset = 10;
60
61 axes->clear();
62
63 if (fvar_size < kAxisSizeOffset + 2) {
64 return false;
65 }
66 const uint16_t majorVersion = readU16(fvar_data, kMajorVersionOffset);
67 const uint16_t minorVersion = readU16(fvar_data, kMinorVersionOffset);
68 const uint32_t axisOffset = readU16(fvar_data, kOffsetToAxesArrayOffset);
69 const uint32_t axisCount = readU16(fvar_data, kAxisCountOffset);
70 const uint32_t axisSize = readU16(fvar_data, kAxisSizeOffset);
71
72 if (majorVersion != 1 || minorVersion != 0 || axisOffset != 0x10 || axisSize != 0x14) {
73 return false; // Unsupported version.
74 }
75 if (fvar_size < axisOffset + axisSize * axisCount) {
76 if (axisOffset > axisSize) {
77 android_errorWriteLog(0x534e4554, "77822336");
78 }
79 return false; // Invalid table size.
80 }
81 for (uint32_t i = 0; i < axisCount; ++i) {
82 size_t axisRecordOffset = axisOffset + i * axisSize;
83 uint32_t tag = readU32(fvar_data, axisRecordOffset);
84 axes->insert(tag);
85 }
86 return true;
87 }
88
readFVarTable(const uint8_t * fvar_data,size_t fvar_size,FVarTable * out)89 bool readFVarTable(const uint8_t* fvar_data, size_t fvar_size, FVarTable* out) {
90 const size_t kMajorVersionOffset = 0;
91 const size_t kMinorVersionOffset = 2;
92 const size_t kOffsetToAxesArrayOffset = 4;
93 const size_t kAxisCountOffset = 8;
94 const size_t kAxisSizeOffset = 10;
95
96 out->clear();
97
98 if (fvar_size < kAxisSizeOffset + 2) {
99 return false;
100 }
101 const uint16_t majorVersion = readU16(fvar_data, kMajorVersionOffset);
102 const uint16_t minorVersion = readU16(fvar_data, kMinorVersionOffset);
103 const uint32_t axisOffset = readU16(fvar_data, kOffsetToAxesArrayOffset);
104 const uint32_t axisCount = readU16(fvar_data, kAxisCountOffset);
105 const uint32_t axisSize = readU16(fvar_data, kAxisSizeOffset);
106
107 if (majorVersion != 1 || minorVersion != 0 || axisOffset != 0x10 || axisSize != 0x14) {
108 return false; // Unsupported version.
109 }
110 if (fvar_size < axisOffset + axisSize * axisCount) {
111 return false; // Invalid table size.
112 }
113 for (uint32_t i = 0; i < axisCount; ++i) {
114 size_t axisRecordOffset = axisOffset + i * axisSize;
115 uint32_t tag = readU32(fvar_data, axisRecordOffset);
116 float minValue = read1616Fixed(fvar_data, axisRecordOffset + 4);
117 float defValue = read1616Fixed(fvar_data, axisRecordOffset + 8);
118 float maxValue = read1616Fixed(fvar_data, axisRecordOffset + 12);
119 FVarEntry entry = {minValue, maxValue, defValue};
120 out->emplace(tag, entry);
121 }
122 return true;
123 }
124 } // namespace minikin
125