1/*
2 * Copyright (C) 2013 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 <minikin/AnalyzeStyle.h>
21
22namespace android {
23
24// should  we have a single FontAnalyzer class this stuff lives in, to avoid dup?
25static int32_t readU16(const uint8_t* data, size_t offset) {
26    return data[offset] << 8 | data[offset + 1];
27}
28
29bool analyzeStyle(const uint8_t* os2_data, size_t os2_size, int* weight, bool* italic) {
30    const size_t kUsWeightClassOffset = 4;
31    const size_t kFsSelectionOffset = 62;
32    const uint16_t kItalicFlag = (1 << 0);
33    if (os2_size < kFsSelectionOffset + 2) {
34        return false;
35    }
36    uint16_t weightClass = readU16(os2_data, kUsWeightClassOffset);
37    *weight = weightClass / 100;
38    uint16_t fsSelection = readU16(os2_data, kFsSelectionOffset);
39    *italic = (fsSelection & kItalicFlag) != 0;
40    return true;
41}
42
43}  // namespace android
44