• 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 <stdint.h>
18 #include <stdlib.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,
34                   size_t os2_size,
35                   int* weight,
36                   bool* italic) {
37   const size_t kUsWeightClassOffset = 4;
38   const size_t kFsSelectionOffset = 62;
39   const uint16_t kItalicFlag = (1 << 0);
40   if (os2_size < kFsSelectionOffset + 2) {
41     return false;
42   }
43   uint16_t weightClass = readU16(os2_data, kUsWeightClassOffset);
44   *weight = weightClass / 100;
45   uint16_t fsSelection = readU16(os2_data, kFsSelectionOffset);
46   *italic = (fsSelection & kItalicFlag) != 0;
47   return true;
48 }
49 
analyzeAxes(const uint8_t * fvar_data,size_t fvar_size,std::unordered_set<uint32_t> * axes)50 void analyzeAxes(const uint8_t* fvar_data,
51                  size_t fvar_size,
52                  std::unordered_set<uint32_t>* axes) {
53   const size_t kMajorVersionOffset = 0;
54   const size_t kMinorVersionOffset = 2;
55   const size_t kOffsetToAxesArrayOffset = 4;
56   const size_t kAxisCountOffset = 8;
57   const size_t kAxisSizeOffset = 10;
58 
59   axes->clear();
60 
61   if (fvar_size < kAxisSizeOffset + 2) {
62     return;
63   }
64   const uint16_t majorVersion = readU16(fvar_data, kMajorVersionOffset);
65   const uint16_t minorVersion = readU16(fvar_data, kMinorVersionOffset);
66   const uint32_t axisOffset = readU16(fvar_data, kOffsetToAxesArrayOffset);
67   const uint32_t axisCount = readU16(fvar_data, kAxisCountOffset);
68   const uint32_t axisSize = readU16(fvar_data, kAxisSizeOffset);
69 
70   if (majorVersion != 1 || minorVersion != 0 || axisOffset != 0x10 ||
71       axisSize != 0x14) {
72     return;  // Unsupported version.
73   }
74   if (fvar_size < axisOffset + axisOffset * axisCount) {
75     return;  // Invalid table size.
76   }
77   for (uint32_t i = 0; i < axisCount; ++i) {
78     size_t axisRecordOffset = axisOffset + i * axisSize;
79     uint32_t tag = readU32(fvar_data, axisRecordOffset);
80     axes->insert(tag);
81   }
82 }
83 }  // namespace minikin
84