w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

aptex-opentype.c
Go to the documentation of this file.
1 /*
2  Copyright 2017, 2018 Clerk Ma
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful, but
10  WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301 USA.
18 */
19 
20 #include <stdio.h>
21 #include <stdint.h>
22 
23 #include "aptex-opentype.h"
24 
26 {
27  return (*s << 8) | (*(s + 1));
28 }
29 
31 {
32  return (*s << 24) | (*(s + 1) << 16) | (*(s + 2) << 8) | (*(s + 3));
33 }
34 
36 {
37  FT_ULong tbl_len = 0;
38  uint8_t * tbl_buf;
39  uint32_t offset_base_glyph;
40  uint32_t offset_layer;
41  ot_tbl_colr * colr;
42  uint32_t i;
43  FT_ULong tag = FT_MAKE_TAG('C', 'O', 'L', 'R');
44 
45  if (FT_Load_Sfnt_Table(face, tag, 0, NULL, &tbl_len))
46  return NULL;
47  tbl_buf = calloc(tbl_len, sizeof(uint8_t));
48  if (FT_Load_Sfnt_Table(face, tag, 0, tbl_buf, &tbl_len))
49  {
50  free(tbl_buf);
51  return NULL;
52  }
53  colr = calloc(1, sizeof(ot_tbl_colr));
54  colr->version = parse_u16(tbl_buf);
55  colr->numBaseGlyphRecords = parse_u16(tbl_buf + 2);
56  offset_base_glyph = parse_u32(tbl_buf + 4);
57  offset_layer = parse_u32(tbl_buf + 8);
58  colr->numLayerRecords = parse_u16(tbl_buf + 12);
59  colr->base_glyphs = malloc(sizeof(ot_base_glyph) * colr->numBaseGlyphRecords);
60  colr->layers = malloc(sizeof(ot_layer) * colr->numLayerRecords);
61 
62  for (i = 0; i < colr->numBaseGlyphRecords; i++)
63  {
64  colr->base_glyphs[i].GID
65  = parse_u16(tbl_buf + offset_base_glyph + i * 6);
67  = parse_u16(tbl_buf + offset_base_glyph + i * 6 + 2);
68  colr->base_glyphs[i].numLayers
69  = parse_u16(tbl_buf + offset_base_glyph + i * 6 + 4);
70  }
71 
72  for (i = 0; i < colr->numLayerRecords; i++)
73  {
74  colr->layers[i].GID
75  = parse_u16(tbl_buf + offset_layer + i * 4);
76  colr->layers[i].paletteIndex
77  = parse_u16(tbl_buf + offset_layer + i * 4 + 2);
78  }
79 
80  free(tbl_buf);
81 
82  return colr;
83 }
84 
86 {
87  if (colr != NULL)
88  {
89  if (colr->base_glyphs != NULL)
90  free(colr->base_glyphs);
91 
92  if (colr->layers != NULL)
93  free(colr->layers);
94 
95  free(colr);
96  }
97 }
98 
100 {
101  FT_ULong tbl_len = 0;
102  uint8_t * tbl_buf;
103  uint32_t offset_first_color;
104  uint32_t offset_palette_type;
105  uint32_t offset_palette_label;
106  uint32_t offset_palette_entry_label;
107  ot_tbl_cpal * cpal;
108  uint32_t i;
109  FT_ULong tag = FT_MAKE_TAG('C', 'P', 'A', 'L');
110 
111  if (FT_Load_Sfnt_Table(face, tag, 0, NULL, &tbl_len))
112  return NULL;
113  tbl_buf = calloc(tbl_len, sizeof(uint8_t));
114  if (FT_Load_Sfnt_Table(face, tag, 0, tbl_buf, &tbl_len))
115  {
116  free(tbl_buf);
117  return NULL;
118  }
119  cpal = calloc(1, sizeof(ot_tbl_cpal));
120  cpal->version = parse_u16(tbl_buf);
121  cpal->numPalettesEntries = parse_u16(tbl_buf + 2);
122  cpal->numPalette = parse_u16(tbl_buf + 4);
123  cpal->numColorRecords = parse_u16(tbl_buf + 6);
124  offset_first_color = parse_u32(tbl_buf + 8);
125  cpal->colorRecordIndices = malloc(sizeof(uint16_t) * cpal->numPalette);
126  cpal->colorRecords = malloc(sizeof(ot_color) * cpal->numColorRecords);
127 
128  for (i = 0; i < cpal->numPalette; i++)
129  cpal->colorRecordIndices[i] = parse_u16(tbl_buf + 12 + i * 2);
130 
131  for (i = 0; i < cpal->numColorRecords; i++)
132  {
133  cpal->colorRecords[i].blue
134  = *(tbl_buf + offset_first_color + i * 4);
135  cpal->colorRecords[i].green
136  = *(tbl_buf + offset_first_color + i * 4 + 1);
137  cpal->colorRecords[i].red
138  = *(tbl_buf + offset_first_color + i * 4 + 2);
139  cpal->colorRecords[i].alpha
140  = *(tbl_buf + offset_first_color + i * 4 + 3);
141  }
142 
143  if (cpal->version == 1)
144  {
145  offset_palette_type
146  = parse_u32(tbl_buf + 12 + 2 * cpal->numPalette);
147  offset_palette_label
148  = parse_u32(tbl_buf + 12 + 2 * cpal->numPalette + 2);
149  offset_palette_entry_label
150  = parse_u32(tbl_buf + 12 + 2 * cpal->numPalette + 4);
151 
152  cpal->paletteType
153  = malloc(sizeof(uint32_t) * cpal->numPalette);
154  cpal->paletteLabel
155  = malloc(sizeof(uint16_t) * cpal->numPalette);
156  cpal->paletteEntryLabel
157  = malloc(sizeof(uint16_t) * cpal->numPalettesEntries);
158 
159  for (i = 0; i < cpal->numPalette; i++)
160  cpal->paletteType[i] = parse_u32(tbl_buf + offset_palette_type + i * 4);
161 
162  for (i = 0; i < cpal->numPalette; i++)
163  cpal->paletteLabel[i] = parse_u16(tbl_buf + offset_palette_label + i * 2);
164 
165  for (i = 0; i < cpal->numPalettesEntries; i++)
166  cpal->paletteEntryLabel[i] = parse_u16(tbl_buf + offset_palette_entry_label + i * 2);
167  }
168  else
169  {
170  cpal->paletteType = NULL;
171  cpal->paletteLabel = NULL;
172  cpal->paletteEntryLabel = NULL;
173  }
174 
175  free(tbl_buf);
176 
177  return cpal;
178 }
179 
181 {
182  if (cpal != NULL)
183  {
184  if (cpal->colorRecordIndices != NULL)
185  free(cpal->colorRecordIndices);
186 
187  if (cpal->colorRecords != NULL)
188  free(cpal->colorRecords);
189 
190  if (cpal->paletteType != NULL)
191  free(cpal->paletteType);
192 
193  if (cpal->paletteLabel != NULL)
194  free(cpal->paletteLabel);
195 
196  if (cpal->paletteEntryLabel != NULL)
197  free(cpal->paletteEntryLabel);
198 
199  free(cpal);
200  }
201 }
ot_tbl_cpal * ot_parse_cpal(FT_Face face)
static uint32_t parse_u32(uint8_t *s)
static uint16_t parse_u16(uint8_t *s)
void ot_delete_cpal(ot_tbl_cpal *cpal)
void ot_delete_colr(ot_tbl_colr *colr)
ot_tbl_colr * ot_parse_colr(FT_Face face)
#define free(a)
Definition: decNumber.cpp:310
#define s
Definition: afcover.h:80
unsigned long FT_ULong
Definition: fttypes.h:253
#define FT_MAKE_TAG(_x1, _x2, _x3, _x4)
Definition: fttypes.h:488
#define NULL
Definition: ftobjs.h:61
FT_Load_Sfnt_Table(FT_Face face, FT_ULong tag, FT_Long offset, FT_Byte *buffer, FT_ULong *length)
Definition: ftobjs.c:4197
small capitals from c petite p scientific i
Definition: afcover.h:80
FT_Face face
Definition: cffdrivr.c:659
unsigned short uint16_t
Definition: stdint.h:79
unsigned int uint32_t
Definition: stdint.h:80
unsigned char uint8_t
Definition: stdint.h:78
voidp calloc()
#define malloc
Definition: alloca.c:91
uint16_t numLayers
uint16_t firstLayerIndex
uint8_t blue
uint8_t red
uint8_t green
uint8_t alpha
uint16_t GID
uint16_t paletteIndex
ot_base_glyph * base_glyphs
uint16_t numBaseGlyphRecords
uint16_t numLayerRecords
ot_layer * layers
uint16_t version
ot_color * colorRecords
uint16_t * paletteLabel
uint32_t * paletteType
uint16_t numPalettesEntries
uint16_t numColorRecords
uint16_t * colorRecordIndices
uint16_t * paletteEntryLabel
uint16_t numPalette
uint16_t version
Definition: xmlparse.c:179