• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stdlib.h>
18 #include <stdint.h>
19 
20 #include "FontUtils.h"
21 
22 namespace minikin {
23 
readU16(const uint8_t * data,size_t offset)24 static uint16_t readU16(const uint8_t* data, size_t offset) {
25     return data[offset] << 8 | data[offset + 1];
26 }
27 
readU32(const uint8_t * data,size_t offset)28 static uint32_t readU32(const uint8_t* data, size_t offset) {
29     return ((uint32_t)data[offset]) << 24 | ((uint32_t)data[offset + 1]) << 16 |
30             ((uint32_t)data[offset + 2]) << 8 | ((uint32_t)data[offset + 3]);
31 }
32 
analyzeStyle(const uint8_t * os2_data,size_t os2_size,int * weight,bool * italic)33 bool analyzeStyle(const uint8_t* os2_data, size_t os2_size, int* weight, bool* italic) {
34     const size_t kUsWeightClassOffset = 4;
35     const size_t kFsSelectionOffset = 62;
36     const uint16_t kItalicFlag = (1 << 0);
37     if (os2_size < kFsSelectionOffset + 2) {
38         return false;
39     }
40     uint16_t weightClass = readU16(os2_data, kUsWeightClassOffset);
41     *weight = weightClass / 100;
42     uint16_t fsSelection = readU16(os2_data, kFsSelectionOffset);
43     *italic = (fsSelection & kItalicFlag) != 0;
44     return true;
45 }
46 
analyzeAxes(const uint8_t * fvar_data,size_t fvar_size,std::unordered_set<uint32_t> * axes)47 void analyzeAxes(const uint8_t* fvar_data, size_t fvar_size, std::unordered_set<uint32_t>* axes) {
48     const size_t kMajorVersionOffset = 0;
49     const size_t kMinorVersionOffset = 2;
50     const size_t kOffsetToAxesArrayOffset = 4;
51     const size_t kAxisCountOffset = 8;
52     const size_t kAxisSizeOffset = 10;
53 
54     axes->clear();
55 
56     if (fvar_size < kAxisSizeOffset + 2) {
57         return;
58     }
59     const uint16_t majorVersion = readU16(fvar_data, kMajorVersionOffset);
60     const uint16_t minorVersion = readU16(fvar_data, kMinorVersionOffset);
61     const uint32_t axisOffset = readU16(fvar_data, kOffsetToAxesArrayOffset);
62     const uint32_t axisCount = readU16(fvar_data, kAxisCountOffset);
63     const uint32_t axisSize = readU16(fvar_data, kAxisSizeOffset);
64 
65     if (majorVersion != 1 || minorVersion != 0 || axisOffset != 0x10 || axisSize != 0x14) {
66         return;  // Unsupported version.
67     }
68     if (fvar_size < axisOffset + axisOffset * axisCount) {
69         return;  // Invalid table size.
70     }
71     for (uint32_t i = 0; i < axisCount; ++i) {
72         size_t axisRecordOffset = axisOffset + i * axisSize;
73         uint32_t tag = readU32(fvar_data, axisRecordOffset);
74         axes->insert(tag);
75     }
76 }
77 }  // namespace minikin
78