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