aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/utils/SkSfntUtils.cpp
blob: 54f7cc33aaf65752ef24af7dcb4dc1e2e2c99368 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

/*
 * Copyright 2011 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#include "SkEndian.h"
#include "SkSfntUtils.h"

static uint16_t parse_be16(const uint8_t*& p) {
    uint16_t value = (p[0] << 8) | p[1];
    p += 2;
    return value;
}

static uint32_t parse_be32(const uint8_t*& p) {
    uint32_t value = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
    p += 4;
    return value;
}

static Sk64 parse_be64(const uint8_t*& p) {
    Sk64 value;
    value.fHi = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
    value.fLo = (p[4] << 24) | (p[5] << 16) | (p[6] << 8) | p[7];
    p += 8;
    return value;
}

///////////////////////////////////////////////////////////////////////////////

bool SkSfntUtils::ReadTable_head(SkFontID fontID, SkSfntTable_head* head) {
    static const uint32_t gTag = SkSetFourByteTag('h', 'e', 'a', 'd');
    static const size_t gSize = 54;
    
    uint8_t storage[gSize];
    size_t size = SkFontHost::GetTableData(fontID, gTag, 0, gSize, storage);
    if (size != gSize) {
        return false;
    }
    
    const uint8_t* p = storage;
    head->fVersion = parse_be32(p);
    head->fRevision = parse_be32(p);
    head->fCheckSumAdjustment = parse_be32(p);
    head->fMagicNumber = parse_be32(p);
    head->fFlags = parse_be16(p);
    head->fUnitsPerEm = parse_be16(p);
    head->fDateCreated = parse_be64(p);
    head->fDateModified = parse_be64(p);
    head->fXMin = parse_be16(p);
    head->fXMin = parse_be16(p);
    head->fXMin = parse_be16(p);
    head->fXMin = parse_be16(p);
    head->fMacStyle = parse_be16(p);
    head->fLowestPPEM = parse_be16(p);
    head->fFontDirectionHint = parse_be16(p);
    head->fIndexToLocFormat = parse_be16(p);
    head->fGlyphDataFormat = parse_be16(p);
    SkASSERT(p - storage == (long)size);
    return true;
}

bool SkSfntUtils::ReadTable_maxp(SkFontID fontID, SkSfntTable_maxp* maxp) {
    static const uint32_t gTag = SkSetFourByteTag('m', 'a', 'x', 'p');
    static const size_t gSize = 32;
    
    uint8_t storage[gSize];
    size_t size = SkFontHost::GetTableData(fontID, gTag, 0, gSize, storage);
    if (size != gSize) {
        return false;
    }
    
    const uint8_t* p = storage;
    maxp->fVersion = parse_be32(p);
    maxp->fNumGlyphs = parse_be16(p);
    maxp->fMaxPoints = parse_be16(p);
    maxp->fMaxContours = parse_be16(p);
    maxp->fMaxComponentPoints = parse_be16(p);
    maxp->fMaxComponentContours = parse_be16(p);
    maxp->fMaxZones = parse_be16(p);
    maxp->fMaxTwilightPoints = parse_be16(p);
    maxp->fMaxStorage = parse_be16(p);
    maxp->fMaxFunctionDefs = parse_be16(p);
    maxp->fMaxInstructionDefs = parse_be16(p);
    maxp->fMaxStackElements = parse_be16(p);
    maxp->fMaxSizeOfInstructions = parse_be16(p);
    maxp->fMaxComponentElements = parse_be16(p);
    maxp->fMaxComponentDepth = parse_be16(p);
    SkASSERT(p - storage == (long)size);
    return true;
}