1 /*
2  Copyright (C) 2016-2017 Alexander Borisov
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 
18  Author: lex.borisov@gmail.com (Alexander Borisov)
19 */
20 
21 #include "myfont/os_2.h"
22 
myfont_load_table_os_2(myfont_font_t * mf,uint8_t * font_data,size_t data_size)23 mystatus_t myfont_load_table_os_2(myfont_font_t* mf, uint8_t* font_data, size_t data_size)
24 {
25     memset(&mf->table_os_2, 0, sizeof(myfont_table_os_2_t));
26 
27     if(mf->cache.tables_offset[MyFONT_TKEY_OS_2] == 0)
28         return MyFONT_STATUS_OK;
29 
30     myfont_table_os_2_t *tos_2 = &mf->table_os_2;
31     const uint32_t table_offset = mf->cache.tables_offset[MyFONT_TKEY_OS_2];
32 
33     /* get current data */
34     uint8_t *data = &font_data[table_offset];
35     uint32_t pos = table_offset + 32 + 10 + 16 + 4 + 16;
36 
37     if(pos > data_size)
38         return MyFONT_STATUS_ERROR_TABLE_UNEXPECTED_ENDING;
39 
40     tos_2->version = myfont_read_u16(&data);
41     tos_2->xAvgCharWidth = myfont_read_16(&data);
42     tos_2->usWeightClass = myfont_read_u16(&data);
43     tos_2->usWidthClass = myfont_read_u16(&data);
44     tos_2->fsType = myfont_read_16(&data);
45     tos_2->ySubscriptXSize = myfont_read_16(&data);
46     tos_2->ySubscriptYSize = myfont_read_16(&data);
47     tos_2->ySubscriptXOffset = myfont_read_16(&data);
48     tos_2->ySubscriptYOffset = myfont_read_16(&data);
49     tos_2->ySuperscriptXSize = myfont_read_16(&data);
50     tos_2->ySuperscriptYSize = myfont_read_16(&data);
51     tos_2->ySuperscriptXOffset = myfont_read_16(&data);
52     tos_2->ySuperscriptYOffset = myfont_read_16(&data);
53     tos_2->yStrikeoutSize = myfont_read_16(&data);
54     tos_2->yStrikeoutPosition = myfont_read_16(&data);
55     tos_2->sFamilyClass = myfont_read_16(&data);
56 
57     memcpy(tos_2->panose, data, 10);
58     data += 10;
59 
60     tos_2->ulUnicodeRange1 = myfont_read_u32(&data);
61     tos_2->ulUnicodeRange2 = myfont_read_u32(&data);
62     tos_2->ulUnicodeRange3 = myfont_read_u32(&data);
63     tos_2->ulUnicodeRange4 = myfont_read_u32(&data);
64 
65     memcpy(tos_2->achVendID, data, 4);
66     data += 4;
67 
68     tos_2->fsSelection = myfont_read_u16(&data);
69     tos_2->usFirstCharIndex = myfont_read_u16(&data);
70     tos_2->usLastCharIndex = myfont_read_u16(&data);
71     tos_2->sTypoAscender = myfont_read_16(&data);
72     tos_2->sTypoDescender = myfont_read_16(&data);
73     tos_2->sTypoLineGap = myfont_read_16(&data);
74     tos_2->usWinAscent = myfont_read_u16(&data);
75     tos_2->usWinDescent = myfont_read_u16(&data);
76 
77     switch (tos_2->version) {
78         case 1:
79             pos += 8;
80             if(pos > data_size)
81                 return MyFONT_STATUS_ERROR_TABLE_UNEXPECTED_ENDING;
82 
83             tos_2->ulCodePageRange1 = myfont_read_u32(&data);
84             tos_2->ulCodePageRange2 = myfont_read_u32(&data);
85 
86             break;
87         case 2:
88         case 3:
89         case 4:
90             pos += 18;
91             if(pos > data_size)
92                 return MyFONT_STATUS_ERROR_TABLE_UNEXPECTED_ENDING;
93 
94             tos_2->ulCodePageRange1 = myfont_read_u32(&data);
95             tos_2->ulCodePageRange2 = myfont_read_u32(&data);
96             tos_2->sxHeight = myfont_read_16(&data);
97             tos_2->sCapHeight = myfont_read_16(&data);
98             tos_2->usDefaultChar = myfont_read_u16(&data);
99             tos_2->usBreakChar = myfont_read_u16(&data);
100             tos_2->usMaxContext = myfont_read_u16(&data);
101 
102             break;
103         case 5:
104             pos += 22;
105             if(pos > data_size)
106                 return MyFONT_STATUS_ERROR_TABLE_UNEXPECTED_ENDING;
107 
108             tos_2->ulCodePageRange1 = myfont_read_u32(&data);
109             tos_2->ulCodePageRange2 = myfont_read_u32(&data);
110             tos_2->sxHeight = myfont_read_16(&data);
111             tos_2->sCapHeight = myfont_read_16(&data);
112             tos_2->usDefaultChar = myfont_read_u16(&data);
113             tos_2->usBreakChar = myfont_read_u16(&data);
114             tos_2->usMaxContext = myfont_read_u16(&data);
115             tos_2->usLowerOpticalPointSize = myfont_read_u16(&data);
116             tos_2->usUpperOpticalPointSize = myfont_read_u16(&data);
117 
118             break;
119         default:
120             break;
121     }
122 
123     return MyFONT_STATUS_OK;
124 }
125 
myfont_os_2_panose(myfont_font_t * mf,myfont_table_os_2_panose_t id)126 int8_t myfont_os_2_panose(myfont_font_t *mf, myfont_table_os_2_panose_t id)
127 {
128     return mf->table_os_2.panose[id];
129 }
130 
131 
132