1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 /*
25 * TrueTypeCreator method implementation
26 *
27 * @author: Alexander Gelfenbain
28 *
29 */
30
31 #if OSL_DEBUG_LEVEL == 0
32 # ifndef NDEBUG
33 # define NDEBUG
34 # endif
35 #endif
36 #include <assert.h>
37
38 #include "ttcr.hxx"
39 #include "list.h"
40 #include "string.h"
41
42
43
44 namespace vcl
45 {
46
47 /*
48 * Private Data Types
49 */
50
51 struct _TrueTypeCreator {
52 sal_uInt32 tag; /**< TrueType file tag */
53 list tables; /**< List of table tags and pointers */
54 };
55
56 /* These must be #defined so that they can be used in initializers */
57 #define T_maxp 0x6D617870
58 #define T_glyf 0x676C7966
59 #define T_head 0x68656164
60 #define T_loca 0x6C6F6361
61 #define T_name 0x6E616D65
62 #define T_hhea 0x68686561
63 #define T_hmtx 0x686D7478
64 #define T_cmap 0x636D6170
65 #define T_vhea 0x76686561
66 #define T_vmtx 0x766D7478
67 #define T_OS2 0x4F532F32
68 #define T_post 0x706F7374
69 #define T_kern 0x6B65726E
70 #define T_cvt 0x63767420
71
72 typedef struct {
73 sal_uInt32 tag;
74 sal_uInt32 length;
75 sal_uInt8 *data;
76 } TableEntry;
77
78 /*
79 * this is a duplicate code from sft.c but it is left here for performance reasons
80 */
81 #ifdef __GNUC__
82 #define _inline static __inline__
83 #else
84 #define _inline static
85 #endif
86
mkTag(sal_uInt8 a,sal_uInt8 b,sal_uInt8 c,sal_uInt8 d)87 _inline sal_uInt32 mkTag(sal_uInt8 a, sal_uInt8 b, sal_uInt8 c, sal_uInt8 d) {
88 return (a << 24) | (b << 16) | (c << 8) | d;
89 }
90
91 /*- Data access macros for data stored in big-endian or little-endian format */
GetInt16(const sal_uInt8 * ptr,sal_uInt32 offset,int bigendian)92 _inline sal_Int16 GetInt16( const sal_uInt8* ptr, sal_uInt32 offset, int bigendian)
93 {
94 sal_Int16 t;
95 assert(ptr != 0);
96
97 if (bigendian) {
98 t = (ptr+offset)[0] << 8 | (ptr+offset)[1];
99 } else {
100 t = (ptr+offset)[1] << 8 | (ptr+offset)[0];
101 }
102
103 return t;
104 }
105
GetUInt16(const sal_uInt8 * ptr,sal_uInt32 offset,int bigendian)106 _inline sal_uInt16 GetUInt16( const sal_uInt8* ptr, sal_uInt32 offset, int bigendian)
107 {
108 sal_uInt16 t;
109 assert(ptr != 0);
110
111 if (bigendian) {
112 t = (ptr+offset)[0] << 8 | (ptr+offset)[1];
113 } else {
114 t = (ptr+offset)[1] << 8 | (ptr+offset)[0];
115 }
116
117 return t;
118 }
119
GetInt32(const sal_uInt8 * ptr,sal_uInt32 offset,int bigendian)120 _inline sal_Int32 GetInt32( const sal_uInt8* ptr, sal_uInt32 offset, int bigendian)
121 {
122 sal_Int32 t;
123 assert(ptr != 0);
124
125 if (bigendian) {
126 t = (ptr+offset)[0] << 24 | (ptr+offset)[1] << 16 |
127 (ptr+offset)[2] << 8 | (ptr+offset)[3];
128 } else {
129 t = (ptr+offset)[3] << 24 | (ptr+offset)[2] << 16 |
130 (ptr+offset)[1] << 8 | (ptr+offset)[0];
131 }
132
133 return t;
134 }
135
GetUInt32(const sal_uInt8 * ptr,sal_uInt32 offset,int bigendian)136 _inline sal_uInt32 GetUInt32( const sal_uInt8* ptr, sal_uInt32 offset, int bigendian)
137 {
138 sal_uInt32 t;
139 assert(ptr != 0);
140
141
142 if (bigendian) {
143 t = (ptr+offset)[0] << 24 | (ptr+offset)[1] << 16 |
144 (ptr+offset)[2] << 8 | (ptr+offset)[3];
145 } else {
146 t = (ptr+offset)[3] << 24 | (ptr+offset)[2] << 16 |
147 (ptr+offset)[1] << 8 | (ptr+offset)[0];
148 }
149
150 return t;
151 }
152
153
PutInt16(sal_Int16 val,sal_uInt8 * ptr,sal_uInt32 offset,int bigendian)154 _inline void PutInt16(sal_Int16 val, sal_uInt8 *ptr, sal_uInt32 offset, int bigendian)
155 {
156 assert(ptr != 0);
157
158 if (bigendian) {
159 ptr[offset] = (sal_uInt8)((val >> 8) & 0xFF);
160 ptr[offset+1] = (sal_uInt8)(val & 0xFF);
161 } else {
162 ptr[offset+1] = (sal_uInt8)((val >> 8) & 0xFF);
163 ptr[offset] = (sal_uInt8)(val & 0xFF);
164 }
165 }
166
PutUInt16(sal_uInt16 val,sal_uInt8 * ptr,sal_uInt32 offset,int bigendian)167 _inline void PutUInt16(sal_uInt16 val, sal_uInt8 *ptr, sal_uInt32 offset, int bigendian)
168 {
169 assert(ptr != 0);
170
171 if (bigendian) {
172 ptr[offset] = (sal_uInt8)((val >> 8) & 0xFF);
173 ptr[offset+1] = (sal_uInt8)(val & 0xFF);
174 } else {
175 ptr[offset+1] = (sal_uInt8)((val >> 8) & 0xFF);
176 ptr[offset] = (sal_uInt8)(val & 0xFF);
177 }
178 }
179
PutUInt32(sal_uInt32 val,sal_uInt8 * ptr,sal_uInt32 offset,int bigendian)180 _inline void PutUInt32(sal_uInt32 val, sal_uInt8 *ptr, sal_uInt32 offset, int bigendian)
181 {
182 assert(ptr != 0);
183
184 if (bigendian) {
185 ptr[offset] = (sal_uInt8)((val >> 24) & 0xFF);
186 ptr[offset+1] = (sal_uInt8)((val >> 16) & 0xFF);
187 ptr[offset+2] = (sal_uInt8)((val >> 8) & 0xFF);
188 ptr[offset+3] = (sal_uInt8)(val & 0xFF);
189 } else {
190 ptr[offset+3] = (sal_uInt8)((val >> 24) & 0xFF);
191 ptr[offset+2] = (sal_uInt8)((val >> 16) & 0xFF);
192 ptr[offset+1] = (sal_uInt8)((val >> 8) & 0xFF);
193 ptr[offset] = (sal_uInt8)(val & 0xFF);
194 }
195
196 }
197
198
PutInt32(sal_Int32 val,sal_uInt8 * ptr,sal_uInt32 offset,int bigendian)199 _inline void PutInt32(sal_Int32 val, sal_uInt8 *ptr, sal_uInt32 offset, int bigendian)
200 {
201 assert(ptr != 0);
202
203 if (bigendian) {
204 ptr[offset] = (sal_uInt8)((val >> 24) & 0xFF);
205 ptr[offset+1] = (sal_uInt8)((val >> 16) & 0xFF);
206 ptr[offset+2] = (sal_uInt8)((val >> 8) & 0xFF);
207 ptr[offset+3] = (sal_uInt8)(val & 0xFF);
208 } else {
209 ptr[offset+3] = (sal_uInt8)((val >> 24) & 0xFF);
210 ptr[offset+2] = (sal_uInt8)((val >> 16) & 0xFF);
211 ptr[offset+1] = (sal_uInt8)((val >> 8) & 0xFF);
212 ptr[offset] = (sal_uInt8)(val & 0xFF);
213 }
214
215 }
216
TableEntryCompareF(const void * l,const void * r)217 static int TableEntryCompareF(const void *l, const void *r)
218 {
219 return ((const TableEntry *) l)->tag - ((const TableEntry *) r)->tag;
220 }
221
NameRecordCompareF(const void * l,const void * r)222 static int NameRecordCompareF(const void *l, const void *r)
223 {
224 NameRecord *ll = (NameRecord *) l;
225 NameRecord *rr = (NameRecord *) r;
226
227 if (ll->platformID != rr->platformID) {
228 return ll->platformID - rr->platformID;
229 } else if (ll->encodingID != rr->encodingID) {
230 return ll->encodingID - rr->encodingID;
231 } else if (ll->languageID != rr->languageID) {
232 return ll->languageID - rr->languageID;
233 } else if (ll->nameID != rr->nameID) {
234 return ll->nameID - rr->nameID;
235 }
236 return 0;
237 }
238
239
CheckSum(sal_uInt32 * ptr,sal_uInt32 length)240 static sal_uInt32 CheckSum(sal_uInt32 *ptr, sal_uInt32 length)
241 {
242 sal_uInt32 sum = 0;
243 sal_uInt32 *endptr = ptr + ((length + 3) & (sal_uInt32) ~3) / 4;
244
245 while (ptr < endptr) sum += *ptr++;
246
247 return sum;
248 }
249
smalloc(sal_uInt32 size)250 _inline void *smalloc(sal_uInt32 size)
251 {
252 void *res = malloc(size);
253 assert(res != 0);
254 return res;
255 }
256
scalloc(sal_uInt32 n,sal_uInt32 size)257 _inline void *scalloc(sal_uInt32 n, sal_uInt32 size)
258 {
259 void *res = calloc(n, size);
260 assert(res != 0);
261 return res;
262 }
263
264 /*
265 * Public functions
266 */
267
TrueTypeCreatorNewEmpty(sal_uInt32 tag,TrueTypeCreator ** _this)268 void TrueTypeCreatorNewEmpty(sal_uInt32 tag, TrueTypeCreator **_this)
269 {
270 TrueTypeCreator* ptr = (TrueTypeCreator*)smalloc(sizeof(TrueTypeCreator));
271
272 ptr->tables = listNewEmpty();
273 listSetElementDtor(ptr->tables, (list_destructor)TrueTypeTableDispose);
274
275 ptr->tag = tag;
276
277 *_this = ptr;
278 }
279
AddTable(TrueTypeCreator * _this,TrueTypeTable * table)280 int AddTable(TrueTypeCreator *_this, TrueTypeTable *table)
281 {
282 if (table != 0) {
283 listAppend(_this->tables, table);
284 }
285 return SF_OK;
286 }
287
RemoveTable(TrueTypeCreator * _this,sal_uInt32 tag)288 void RemoveTable(TrueTypeCreator *_this, sal_uInt32 tag)
289 {
290 int done = 0;
291
292 if (listCount(_this->tables)) {
293 listToFirst(_this->tables);
294 do {
295 if (((TrueTypeTable *) listCurrent(_this->tables))->tag == tag) {
296 listRemove(_this->tables);
297 } else {
298 if (listNext(_this->tables)) {
299 done = 1;
300 }
301 }
302 } while (!done);
303 }
304 }
305
306 static void ProcessTables(TrueTypeCreator *);
307
StreamToMemory(TrueTypeCreator * _this,sal_uInt8 ** ptr,sal_uInt32 * length)308 int StreamToMemory(TrueTypeCreator *_this, sal_uInt8 **ptr, sal_uInt32 *length)
309 {
310 sal_uInt16 numTables, searchRange=1, entrySelector=0, rangeShift;
311 sal_uInt32 s, offset, checkSumAdjustment = 0;
312 sal_uInt32 *p;
313 int i=0, n;
314 sal_uInt8 *head = NULL; /* saved pointer to the head table data for checkSumAdjustment calculation */
315
316 if ((n = listCount(_this->tables)) == 0) return SF_TTFORMAT;
317
318 ProcessTables(_this);
319
320 /* ProcessTables() adds 'loca' and 'hmtx' */
321
322 n = listCount(_this->tables);
323 numTables = (sal_uInt16) n;
324
325
326 TableEntry* te = (TableEntry*)scalloc(n, sizeof(TableEntry));
327
328 listToFirst(_this->tables);
329 for (i = 0; i < n; i++) {
330 GetRawData((TrueTypeTable *) listCurrent(_this->tables), &te[i].data, &te[i].length, &te[i].tag);
331 listNext(_this->tables);
332 }
333
334 qsort(te, n, sizeof(TableEntry), TableEntryCompareF);
335
336 do {
337 searchRange *= 2;
338 entrySelector++;
339 } while (searchRange <= numTables);
340
341 searchRange *= 8;
342 entrySelector--;
343 rangeShift = numTables * 16 - searchRange;
344
345 s = offset = 12 + 16 * n;
346
347 for (i = 0; i < n; i++) {
348 s += (te[i].length + 3) & (sal_uInt32) ~3;
349 /* if ((te[i].length & 3) != 0) s += (4 - (te[i].length & 3)) & 3; */
350 }
351
352 sal_uInt8* ttf = (sal_uInt8*)smalloc(s);
353
354 /* Offset Table */
355 PutUInt32(_this->tag, ttf, 0, 1);
356 PutUInt16(numTables, ttf, 4, 1);
357 PutUInt16(searchRange, ttf, 6, 1);
358 PutUInt16(entrySelector, ttf, 8, 1);
359 PutUInt16(rangeShift, ttf, 10, 1);
360
361 /* Table Directory */
362 for (i = 0; i < n; i++) {
363 PutUInt32(te[i].tag, ttf + 12, 16 * i, 1);
364 PutUInt32(CheckSum((sal_uInt32 *) te[i].data, te[i].length), ttf + 12, 16 * i + 4, 1);
365 PutUInt32(offset, ttf + 12, 16 * i + 8, 1);
366 PutUInt32(te[i].length, ttf + 12, 16 * i + 12, 1);
367
368 if (te[i].tag == T_head) {
369 head = ttf + offset;
370 }
371
372 memcpy(ttf+offset, te[i].data, (te[i].length + 3) & (sal_uInt32) ~3 );
373 offset += (te[i].length + 3) & (sal_uInt32) ~3;
374 /* if ((te[i].length & 3) != 0) offset += (4 - (te[i].length & 3)) & 3; */
375 }
376
377 free(te);
378
379 p = (sal_uInt32 *) ttf;
380 for (i = 0; i < (int)s / 4; i++) checkSumAdjustment += p[i];
381 PutUInt32(0xB1B0AFBA - checkSumAdjustment, head, 8, 1);
382
383 *ptr = ttf;
384 *length = s;
385
386 return SF_OK;
387 }
388
StreamToFile(TrueTypeCreator * _this,const char * fname)389 int StreamToFile(TrueTypeCreator *_this, const char* fname)
390 {
391 sal_uInt8 *ptr;
392 sal_uInt32 length;
393 int r;
394 FILE* fd;
395
396 if ((r = StreamToMemory(_this, &ptr, &length)) != SF_OK) return r;
397 if ((!fname) || ((fd = fopen(fname, "wb")) == NULL)) {
398 free(ptr);
399 return SF_BADFILE;
400 }
401 if (fwrite(ptr, 1, length, fd) != length) {
402 r = SF_FILEIO;
403 } else {
404 r = SF_OK;
405 }
406
407 fclose(fd);
408 free(ptr);
409 return r;
410 }
411
412
413
414 /*
415 * TrueTypeTable private methods
416 */
417
418 #define TABLESIZE_head 54
419 #define TABLESIZE_hhea 36
420 #define TABLESIZE_maxp 32
421
422
423
424 /* Table data points to
425 * --------------------------------------------
426 * generic tdata_generic struct
427 * 'head' TABLESIZE_head bytes of memory
428 * 'hhea' TABLESIZE_hhea bytes of memory
429 * 'loca' tdata_loca struct
430 * 'maxp' TABLESIZE_maxp bytes of memory
431 * 'glyf' list of GlyphData structs (defined in sft.h)
432 * 'name' list of NameRecord structs (defined in sft.h)
433 * 'post' tdata_post struct
434 *
435 */
436
437
438 #define CMAP_SUBTABLE_INIT 10
439 #define CMAP_SUBTABLE_INCR 10
440 #define CMAP_PAIR_INIT 500
441 #define CMAP_PAIR_INCR 500
442
443 typedef struct {
444 sal_uInt32 id; /* subtable ID (platform/encoding ID) */
445 sal_uInt32 n; /* number of used translation pairs */
446 sal_uInt32 m; /* number of allocated translation pairs */
447 sal_uInt32 *xc; /* character array */
448 sal_uInt32 *xg; /* glyph array */
449 } CmapSubTable;
450
451 typedef struct {
452 sal_uInt32 n; /* number of used CMAP sub-tables */
453 sal_uInt32 m; /* number of allocated CMAP sub-tables */
454 CmapSubTable *s; /* sorted array of sub-tables */
455 } table_cmap;
456
457 typedef struct {
458 sal_uInt32 tag;
459 sal_uInt32 nbytes;
460 sal_uInt8 *ptr;
461 } tdata_generic;
462
463 typedef struct {
464 sal_uInt32 nbytes; /* number of bytes in loca table */
465 sal_uInt8 *ptr; /* pointer to the data */
466 } tdata_loca;
467
468 typedef struct {
469 sal_uInt32 format;
470 sal_uInt32 italicAngle;
471 sal_Int16 underlinePosition;
472 sal_Int16 underlineThickness;
473 sal_uInt32 isFixedPitch;
474 void *ptr; /* format-specific pointer */
475 } tdata_post;
476
477
478 /* allocate memory for a TT table */
ttmalloc(sal_uInt32 nbytes)479 static sal_uInt8 *ttmalloc(sal_uInt32 nbytes)
480 {
481 sal_uInt32 n;
482
483 n = (nbytes + 3) & (sal_uInt32) ~3;
484 sal_uInt8* res = (sal_uInt8*)malloc(n);
485 assert(res != 0);
486 memset(res, 0, n);
487
488 return res;
489 }
490
FreeGlyphData(void * ptr)491 static void FreeGlyphData(void *ptr)
492 {
493 GlyphData *p = (GlyphData *) ptr;
494 if (p->ptr) free(p->ptr);
495 free(p);
496 }
497
TrueTypeTableDispose_generic(TrueTypeTable * _this)498 static void TrueTypeTableDispose_generic(TrueTypeTable *_this)
499 {
500 if (_this) {
501 if (_this->data) {
502 tdata_generic *pdata = (tdata_generic *) _this->data;
503 if (pdata->nbytes) free(pdata->ptr);
504 free(_this->data);
505 }
506 free(_this);
507 }
508 }
509
TrueTypeTableDispose_head(TrueTypeTable * _this)510 static void TrueTypeTableDispose_head(TrueTypeTable *_this)
511 {
512 if (_this) {
513 if (_this->data) free(_this->data);
514 free(_this);
515 }
516 }
517
TrueTypeTableDispose_hhea(TrueTypeTable * _this)518 static void TrueTypeTableDispose_hhea(TrueTypeTable *_this)
519 {
520 if (_this) {
521 if (_this->data) free(_this->data);
522 free(_this);
523 }
524 }
525
TrueTypeTableDispose_loca(TrueTypeTable * _this)526 static void TrueTypeTableDispose_loca(TrueTypeTable *_this)
527 {
528 if (_this) {
529 if (_this->data) {
530 tdata_loca *p = (tdata_loca *) _this->data;
531 if (p->ptr) free(p->ptr);
532 free(_this->data);
533 }
534 free(_this);
535 }
536 }
537
TrueTypeTableDispose_maxp(TrueTypeTable * _this)538 static void TrueTypeTableDispose_maxp(TrueTypeTable *_this)
539 {
540 if (_this) {
541 if (_this->data) free(_this->data);
542 free(_this);
543 }
544 }
545
TrueTypeTableDispose_glyf(TrueTypeTable * _this)546 static void TrueTypeTableDispose_glyf(TrueTypeTable *_this)
547 {
548 if (_this) {
549 if (_this->data) listDispose((list) _this->data);
550 free(_this);
551 }
552 }
553
TrueTypeTableDispose_cmap(TrueTypeTable * _this)554 static void TrueTypeTableDispose_cmap(TrueTypeTable *_this)
555 {
556 table_cmap *t;
557 CmapSubTable *s;
558 sal_uInt32 i;
559
560 if (_this) {
561 t = (table_cmap *) _this->data;
562 if (t) {
563 s = t->s;
564 if (s) {
565 for (i = 0; i < t->m; i++) {
566 if (s[i].xc) free(s[i].xc);
567 if (s[i].xg) free(s[i].xg);
568 }
569 free(s);
570 }
571 free(t);
572 }
573 free(_this);
574 }
575 }
576
TrueTypeTableDispose_name(TrueTypeTable * _this)577 static void TrueTypeTableDispose_name(TrueTypeTable *_this)
578 {
579 if (_this) {
580 if (_this->data) listDispose((list) _this->data);
581 free(_this);
582 }
583 }
584
TrueTypeTableDispose_post(TrueTypeTable * _this)585 static void TrueTypeTableDispose_post(TrueTypeTable *_this)
586 {
587 if (_this) {
588 tdata_post *p = (tdata_post *) _this->data;
589 if (p) {
590 if (p->format == 0x00030000) {
591 /* do nothing */
592 } else {
593 fprintf(stderr, "Unsupported format of a 'post' table: %08X.\n", (int)p->format);
594 }
595 free(p);
596 }
597 free(_this);
598 }
599 }
600
601 /* destructor vtable */
602
603 static struct {
604 sal_uInt32 tag;
605 void (*f)(TrueTypeTable *);
606 } vtable1[] =
607 {
608 {0, TrueTypeTableDispose_generic},
609 {T_head, TrueTypeTableDispose_head},
610 {T_hhea, TrueTypeTableDispose_hhea},
611 {T_loca, TrueTypeTableDispose_loca},
612 {T_maxp, TrueTypeTableDispose_maxp},
613 {T_glyf, TrueTypeTableDispose_glyf},
614 {T_cmap, TrueTypeTableDispose_cmap},
615 {T_name, TrueTypeTableDispose_name},
616 {T_post, TrueTypeTableDispose_post}
617
618 };
619
GetRawData_generic(TrueTypeTable * _this,sal_uInt8 ** ptr,sal_uInt32 * len,sal_uInt32 * tag)620 static int GetRawData_generic(TrueTypeTable *_this, sal_uInt8 **ptr, sal_uInt32 *len, sal_uInt32 *tag)
621 {
622 assert(_this != 0);
623 assert(_this->data != 0);
624
625 *ptr = ((tdata_generic *) _this->data)->ptr;
626 *len = ((tdata_generic *) _this->data)->nbytes;
627 *tag = ((tdata_generic *) _this->data)->tag;
628
629 return TTCR_OK;
630 }
631
632
GetRawData_head(TrueTypeTable * _this,sal_uInt8 ** ptr,sal_uInt32 * len,sal_uInt32 * tag)633 static int GetRawData_head(TrueTypeTable *_this, sal_uInt8 **ptr, sal_uInt32 *len, sal_uInt32 *tag)
634 {
635 *len = TABLESIZE_head;
636 *ptr = (sal_uInt8 *) _this->data;
637 *tag = T_head;
638
639 return TTCR_OK;
640 }
641
GetRawData_hhea(TrueTypeTable * _this,sal_uInt8 ** ptr,sal_uInt32 * len,sal_uInt32 * tag)642 static int GetRawData_hhea(TrueTypeTable *_this, sal_uInt8 **ptr, sal_uInt32 *len, sal_uInt32 *tag)
643 {
644 *len = TABLESIZE_hhea;
645 *ptr = (sal_uInt8 *) _this->data;
646 *tag = T_hhea;
647
648 return TTCR_OK;
649 }
650
GetRawData_loca(TrueTypeTable * _this,sal_uInt8 ** ptr,sal_uInt32 * len,sal_uInt32 * tag)651 static int GetRawData_loca(TrueTypeTable *_this, sal_uInt8 **ptr, sal_uInt32 *len, sal_uInt32 *tag)
652 {
653 tdata_loca *p;
654
655 assert(_this->data != 0);
656
657 p = (tdata_loca *) _this->data;
658
659 if (p->nbytes == 0) return TTCR_ZEROGLYPHS;
660
661 *ptr = p->ptr;
662 *len = p->nbytes;
663 *tag = T_loca;
664
665 return TTCR_OK;
666 }
667
GetRawData_maxp(TrueTypeTable * _this,sal_uInt8 ** ptr,sal_uInt32 * len,sal_uInt32 * tag)668 static int GetRawData_maxp(TrueTypeTable *_this, sal_uInt8 **ptr, sal_uInt32 *len, sal_uInt32 *tag)
669 {
670 *len = TABLESIZE_maxp;
671 *ptr = (sal_uInt8 *) _this->data;
672 *tag = T_maxp;
673
674 return TTCR_OK;
675 }
676
GetRawData_glyf(TrueTypeTable * _this,sal_uInt8 ** ptr,sal_uInt32 * len,sal_uInt32 * tag)677 static int GetRawData_glyf(TrueTypeTable *_this, sal_uInt8 **ptr, sal_uInt32 *len, sal_uInt32 *tag)
678 {
679 sal_uInt32 n, nbytes = 0;
680 list l = (list) _this->data;
681 /* sal_uInt16 curID = 0; */ /* to check if glyph IDs are sequential and start from zero */
682 sal_uInt8 *p;
683
684 *ptr = 0;
685 *len = 0;
686 *tag = 0;
687
688 if (listCount(l) == 0) return TTCR_ZEROGLYPHS;
689
690 listToFirst(l);
691 do {
692 /* if (((GlyphData *) listCurrent(l))->glyphID != curID++) return TTCR_GLYPHSEQ; */
693 nbytes += ((GlyphData *) listCurrent(l))->nbytes;
694 } while (listNext(l));
695
696 p = _this->rawdata = ttmalloc(nbytes);
697
698 listToFirst(l);
699 do {
700 n = ((GlyphData *) listCurrent(l))->nbytes;
701 if (n != 0) {
702 memcpy(p, ((GlyphData *) listCurrent(l))->ptr, n);
703 p += n;
704 }
705 } while (listNext(l));
706
707 *len = nbytes;
708 *ptr = _this->rawdata;
709 *tag = T_glyf;
710
711 return TTCR_OK;
712 }
713
714 /* cmap packers */
PackCmapType0(CmapSubTable * s,sal_uInt32 * length)715 static sal_uInt8 *PackCmapType0(CmapSubTable *s, sal_uInt32 *length)
716 {
717 sal_uInt8* ptr = (sal_uInt8*)smalloc(262);
718 sal_uInt8 *p = ptr + 6;
719 sal_uInt32 i, j;
720 sal_uInt16 g;
721
722 PutUInt16(0, ptr, 0, 1);
723 PutUInt16(262, ptr, 2, 1);
724 PutUInt16(0, ptr, 4, 1);
725
726 for (i = 0; i < 256; i++) {
727 g = 0;
728 for (j = 0; j < s->n; j++) {
729 if (s->xc[j] == i) {
730 g = (sal_uInt16) s->xg[j];
731 }
732 }
733 p[i] = (sal_uInt8) g;
734 }
735 *length = 262;
736 return ptr;
737 }
738
PackCmapType6(CmapSubTable * s,sal_uInt32 * length)739 static sal_uInt8 *PackCmapType6(CmapSubTable *s, sal_uInt32 *length)
740 {
741 sal_uInt8* ptr = (sal_uInt8*)smalloc(s->n*2 + 10);
742 sal_uInt8 *p = ptr + 10;
743 sal_uInt32 i, j;
744 sal_uInt16 g;
745
746 PutUInt16(6, ptr, 0, 1);
747 PutUInt16((sal_uInt16)(s->n*2+10), ptr, 2, 1);
748 PutUInt16(0, ptr, 4, 1);
749 PutUInt16(0, ptr, 6, 1);
750 PutUInt16((sal_uInt16)(s->n), ptr, 8, 1 );
751
752 for (i = 0; i < s->n; i++) {
753 g = 0;
754 for (j = 0; j < s->n; j++) {
755 if (s->xc[j] == i) {
756 g = (sal_uInt16) s->xg[j];
757 }
758 }
759 PutUInt16( g, p, 2*i, 1 );
760 }
761 *length = s->n*2+10;
762 return ptr;
763 }
764
765
766
767 /* XXX it only handles Format 0 encoding tables */
PackCmap(CmapSubTable * s,sal_uInt32 * length)768 static sal_uInt8 *PackCmap(CmapSubTable *s, sal_uInt32 *length)
769 {
770 if( s->xg[s->n-1] > 0xff )
771 return PackCmapType6(s, length);
772 else
773 return PackCmapType0(s, length);
774 }
775
GetRawData_cmap(TrueTypeTable * _this,sal_uInt8 ** ptr,sal_uInt32 * len,sal_uInt32 * tag)776 static int GetRawData_cmap(TrueTypeTable *_this, sal_uInt8 **ptr, sal_uInt32 *len, sal_uInt32 *tag)
777 {
778 table_cmap *t;
779 sal_uInt32 i;
780 sal_uInt32 tlen = 0;
781 sal_uInt32 l;
782 sal_uInt32 cmapsize;
783 sal_uInt8 *cmap;
784 sal_uInt32 coffset;
785
786 assert(_this != 0);
787 t = (table_cmap *) _this->data;
788 assert(t != 0);
789 assert(t->n != 0);
790
791 sal_uInt8** subtables = (sal_uInt8**)scalloc(t->n, sizeof(sal_uInt8 *));
792 sal_uInt32* sizes = (sal_uInt32*)scalloc(t->n, sizeof(sal_uInt32));
793
794 for (i = 0; i < t->n; i++) {
795 subtables[i] = PackCmap(t->s+i, &l);
796 sizes[i] = l;
797 tlen += l;
798 }
799
800 cmapsize = tlen + 4 + 8 * t->n;
801 _this->rawdata = cmap = ttmalloc(cmapsize);
802
803 PutUInt16(0, cmap, 0, 1);
804 PutUInt16((sal_uInt16)t->n, cmap, 2, 1);
805 coffset = 4 + t->n * 8;
806
807 for (i = 0; i < t->n; i++) {
808 PutUInt16((sal_uInt16)(t->s[i].id >> 16), cmap + 4, i * 8, 1);
809 PutUInt16((sal_uInt16)(t->s[i].id & 0xFF), cmap + 4, 2 + i * 8, 1);
810 PutUInt32(coffset, cmap + 4, 4 + i * 8, 1);
811 memcpy(cmap + coffset, subtables[i], sizes[i]);
812 free(subtables[i]);
813 coffset += sizes[i];
814 }
815
816 free(subtables);
817 free(sizes);
818
819 *ptr = cmap;
820 *len = cmapsize;
821 *tag = T_cmap;
822
823 return TTCR_OK;
824 }
825
826
GetRawData_name(TrueTypeTable * _this,sal_uInt8 ** ptr,sal_uInt32 * len,sal_uInt32 * tag)827 static int GetRawData_name(TrueTypeTable *_this, sal_uInt8 **ptr, sal_uInt32 *len, sal_uInt32 *tag)
828 {
829 list l;
830 sal_Int16 i=0, n; /* number of Name Records */
831 int stringLen = 0;
832 sal_uInt8 *p1, *p2;
833
834 *ptr = 0;
835 *len = 0;
836 *tag = 0;
837
838 assert(_this != 0);
839 l = (list) _this->data;
840 assert(l != 0);
841
842 if ((n = (sal_Int16)listCount(l)) == 0) return TTCR_NONAMES;
843
844 NameRecord* nr = (NameRecord*)scalloc(n, sizeof(NameRecord));
845
846 listToFirst(l);
847
848 do {
849 memcpy(nr+i, listCurrent(l), sizeof(NameRecord));
850 stringLen += nr[i].slen;
851 i++;
852 } while (listNext(l));
853
854 if (stringLen > 65535) {
855 free(nr);
856 return TTCR_NAMETOOLONG;
857 }
858
859 qsort(nr, n, sizeof(NameRecord), NameRecordCompareF);
860
861 int nameLen = stringLen + 12 * n + 6;
862 sal_uInt8* name = (sal_uInt8*)ttmalloc(nameLen);
863
864 PutUInt16(0, name, 0, 1);
865 PutUInt16(n, name, 2, 1);
866 PutUInt16((sal_uInt16)(6 + 12 * n), name, 4, 1);
867
868 p1 = name + 6;
869 p2 = p1 + 12 * n;
870
871 for (i = 0; i < n; i++) {
872 PutUInt16(nr[i].platformID, p1, 0, 1);
873 PutUInt16(nr[i].encodingID, p1, 2, 1);
874 PutUInt16(nr[i].languageID, p1, 4, 1);
875 PutUInt16(nr[i].nameID, p1, 6, 1);
876 PutUInt16(nr[i].slen, p1, 8, 1);
877 PutUInt16((sal_uInt16)(p2 - (name + 6 + 12 * n)), p1, 10, 1);
878 memcpy(p2, nr[i].sptr, nr[i].slen);
879 /* {int j; for(j=0; j<nr[i].slen; j++) printf("%c", nr[i].sptr[j]); printf("\n"); }; */
880 p2 += nr[i].slen;
881 p1 += 12;
882 }
883
884 free(nr);
885 _this->rawdata = name;
886
887 *ptr = name;
888 *len = (sal_uInt16)nameLen;
889 *tag = T_name;
890
891 /*{int j; for(j=0; j<nameLen; j++) printf("%c", name[j]); }; */
892
893 return TTCR_OK;
894 }
895
GetRawData_post(TrueTypeTable * _this,sal_uInt8 ** ptr,sal_uInt32 * len,sal_uInt32 * tag)896 static int GetRawData_post(TrueTypeTable *_this, sal_uInt8 **ptr, sal_uInt32 *len, sal_uInt32 *tag)
897 {
898 tdata_post *p = (tdata_post *) _this->data;
899 sal_uInt8 *post = 0;
900 sal_uInt32 postLen = 0;
901 int ret;
902
903 if (_this->rawdata) free(_this->rawdata);
904
905 if (p->format == 0x00030000) {
906 postLen = 32;
907 post = ttmalloc(postLen);
908 PutUInt32(0x00030000, post, 0, 1);
909 PutUInt32(p->italicAngle, post, 4, 1);
910 PutUInt16(p->underlinePosition, post, 8, 1);
911 PutUInt16(p->underlineThickness, post, 10, 1);
912 PutUInt16((sal_uInt16)p->isFixedPitch, post, 12, 1);
913 ret = TTCR_OK;
914 } else {
915 fprintf(stderr, "Unrecognized format of a post table: %08X.\n", (int)p->format);
916 ret = TTCR_POSTFORMAT;
917 }
918
919 *ptr = _this->rawdata = post;
920 *len = postLen;
921 *tag = T_post;
922
923 return ret;
924 }
925
926
927
928
929
930 static struct {
931 sal_uInt32 tag;
932 int (*f)(TrueTypeTable *, sal_uInt8 **, sal_uInt32 *, sal_uInt32 *);
933 } vtable2[] =
934 {
935 {0, GetRawData_generic},
936 {T_head, GetRawData_head},
937 {T_hhea, GetRawData_hhea},
938 {T_loca, GetRawData_loca},
939 {T_maxp, GetRawData_maxp},
940 {T_glyf, GetRawData_glyf},
941 {T_cmap, GetRawData_cmap},
942 {T_name, GetRawData_name},
943 {T_post, GetRawData_post}
944
945
946 };
947
948 /*
949 * TrueTypeTable public methods
950 */
951
952 /* Note: Type42 fonts only need these tables:
953 * head, hhea, loca, maxp, cvt, prep, glyf, hmtx, fpgm
954 *
955 * Microsoft required tables
956 * cmap, glyf, head, hhea, hmtx, loca, maxp, name, post, OS/2
957 *
958 * Apple required tables
959 * cmap, glyf, head, hhea, hmtx, loca, maxp, name, post
960 *
961 */
962
TrueTypeTableNew(sal_uInt32 tag,sal_uInt32 nbytes,const sal_uInt8 * ptr)963 TrueTypeTable *TrueTypeTableNew(sal_uInt32 tag,
964 sal_uInt32 nbytes,
965 const sal_uInt8* ptr)
966 {
967 TrueTypeTable* table = (TrueTypeTable*)smalloc(sizeof(TrueTypeTable));
968 tdata_generic* pdata = (tdata_generic*)smalloc(sizeof(tdata_generic));
969 pdata->nbytes = nbytes;
970 pdata->tag = tag;
971 if (nbytes) {
972 pdata->ptr = ttmalloc(nbytes);
973 memcpy(pdata->ptr, ptr, nbytes);
974 } else {
975 pdata->ptr = 0;
976 }
977
978 table->tag = 0;
979 table->data = pdata;
980 table->rawdata = 0;
981
982 return table;
983 }
984
TrueTypeTableNew_head(sal_uInt32 fontRevision,sal_uInt16 flags,sal_uInt16 unitsPerEm,const sal_uInt8 * created,sal_uInt16 macStyle,sal_uInt16 lowestRecPPEM,sal_Int16 fontDirectionHint)985 TrueTypeTable *TrueTypeTableNew_head(sal_uInt32 fontRevision,
986 sal_uInt16 flags,
987 sal_uInt16 unitsPerEm,
988 const sal_uInt8* created,
989 sal_uInt16 macStyle,
990 sal_uInt16 lowestRecPPEM,
991 sal_Int16 fontDirectionHint)
992 {
993 assert(created != 0);
994
995 TrueTypeTable* table = (TrueTypeTable*)smalloc(sizeof(TrueTypeTable));
996 sal_uInt8* ptr = (sal_uInt8*)ttmalloc(TABLESIZE_head);
997
998
999 PutUInt32(0x00010000, ptr, 0, 1); /* version */
1000 PutUInt32(fontRevision, ptr, 4, 1);
1001 PutUInt32(0x5F0F3CF5, ptr, 12, 1); /* magic number */
1002 PutUInt16(flags, ptr, 16, 1);
1003 PutUInt16(unitsPerEm, ptr, 18, 1);
1004 memcpy(ptr+20, created, 8); /* Created Long Date */
1005 memset(ptr+28, 0, 8); /* Modified Long Date */
1006 PutUInt16(macStyle, ptr, 44, 1);
1007 PutUInt16(lowestRecPPEM, ptr, 46, 1);
1008 PutUInt16(fontDirectionHint, ptr, 48, 1);
1009 PutUInt16(0, ptr, 52, 1); /* glyph data format: 0 */
1010
1011 table->data = (void *) ptr;
1012 table->tag = T_head;
1013 table->rawdata = 0;
1014
1015 return table;
1016 }
1017
TrueTypeTableNew_hhea(sal_Int16 ascender,sal_Int16 descender,sal_Int16 linegap,sal_Int16 caretSlopeRise,sal_Int16 caretSlopeRun)1018 TrueTypeTable *TrueTypeTableNew_hhea(sal_Int16 ascender,
1019 sal_Int16 descender,
1020 sal_Int16 linegap,
1021 sal_Int16 caretSlopeRise,
1022 sal_Int16 caretSlopeRun)
1023 {
1024 TrueTypeTable* table = (TrueTypeTable*)smalloc(sizeof(TrueTypeTable));
1025 sal_uInt8* ptr = (sal_uInt8*)ttmalloc(TABLESIZE_hhea);
1026
1027 PutUInt32(0x00010000, ptr, 0, 1); /* version */
1028 PutUInt16(ascender, ptr, 4, 1);
1029 PutUInt16(descender, ptr, 6, 1);
1030 PutUInt16(linegap, ptr, 8, 1);
1031 PutUInt16(caretSlopeRise, ptr, 18, 1);
1032 PutUInt16(caretSlopeRun, ptr, 20, 1);
1033 PutUInt16(0, ptr, 22, 1); /* reserved 1 */
1034 PutUInt16(0, ptr, 24, 1); /* reserved 2 */
1035 PutUInt16(0, ptr, 26, 1); /* reserved 3 */
1036 PutUInt16(0, ptr, 28, 1); /* reserved 4 */
1037 PutUInt16(0, ptr, 30, 1); /* reserved 5 */
1038 PutUInt16(0, ptr, 32, 1); /* metricDataFormat */
1039
1040 table->data = (void *) ptr;
1041 table->tag = T_hhea;
1042 table->rawdata = 0;
1043
1044 return table;
1045 }
1046
TrueTypeTableNew_loca(void)1047 TrueTypeTable *TrueTypeTableNew_loca(void)
1048 {
1049 TrueTypeTable* table = (TrueTypeTable*)smalloc(sizeof(TrueTypeTable));
1050 table->data = smalloc(sizeof(tdata_loca));
1051
1052 ((tdata_loca *)table->data)->nbytes = 0;
1053 ((tdata_loca *)table->data)->ptr = 0;
1054
1055 table->tag = T_loca;
1056 table->rawdata = 0;
1057
1058 return table;
1059 }
1060
TrueTypeTableNew_maxp(const sal_uInt8 * maxp,int size)1061 TrueTypeTable *TrueTypeTableNew_maxp( const sal_uInt8* maxp, int size)
1062 {
1063 TrueTypeTable* table = (TrueTypeTable*)smalloc(sizeof(TrueTypeTable));
1064 table->data = ttmalloc(TABLESIZE_maxp);
1065
1066 if (maxp && size == TABLESIZE_maxp) {
1067 memcpy(table->data, maxp, TABLESIZE_maxp);
1068 }
1069
1070 table->tag = T_maxp;
1071 table->rawdata = 0;
1072
1073 return table;
1074 }
1075
TrueTypeTableNew_glyf(void)1076 TrueTypeTable *TrueTypeTableNew_glyf(void)
1077 {
1078 TrueTypeTable* table = (TrueTypeTable*)smalloc(sizeof(TrueTypeTable));
1079 list l = listNewEmpty();
1080
1081 assert(l != 0);
1082
1083 listSetElementDtor(l, (list_destructor)FreeGlyphData);
1084
1085 table->data = l;
1086 table->rawdata = 0;
1087 table->tag = T_glyf;
1088
1089 return table;
1090 }
1091
TrueTypeTableNew_cmap(void)1092 TrueTypeTable *TrueTypeTableNew_cmap(void)
1093 {
1094 TrueTypeTable* table = (TrueTypeTable*)smalloc(sizeof(TrueTypeTable));
1095 table_cmap* cmap = (table_cmap*)smalloc(sizeof(table_cmap));
1096
1097 cmap->n = 0;
1098 cmap->m = CMAP_SUBTABLE_INIT;
1099 cmap->s = (CmapSubTable *) scalloc(CMAP_SUBTABLE_INIT, sizeof(CmapSubTable));
1100 memset(cmap->s, 0, sizeof(CmapSubTable) * CMAP_SUBTABLE_INIT);
1101
1102 table->data = (table_cmap *) cmap;
1103
1104 table->rawdata = 0;
1105 table->tag = T_cmap;
1106
1107 return table;
1108 }
1109
DisposeNameRecord(void * ptr)1110 static void DisposeNameRecord(void *ptr)
1111 {
1112 if (ptr != 0) {
1113 NameRecord *nr = (NameRecord *) ptr;
1114 if (nr->sptr) free(nr->sptr);
1115 free(ptr);
1116 }
1117 }
1118
NameRecordNewCopy(NameRecord * nr)1119 static NameRecord* NameRecordNewCopy(NameRecord *nr)
1120 {
1121 NameRecord* p = (NameRecord*)smalloc(sizeof(NameRecord));
1122
1123 memcpy(p, nr, sizeof(NameRecord));
1124
1125 if (p->slen) {
1126 p->sptr = (sal_uInt8*)smalloc(p->slen);
1127 memcpy(p->sptr, nr->sptr, p->slen);
1128 }
1129
1130 return p;
1131 }
1132
TrueTypeTableNew_name(int n,NameRecord * nr)1133 TrueTypeTable *TrueTypeTableNew_name(int n, NameRecord *nr)
1134 {
1135 TrueTypeTable* table = (TrueTypeTable*)smalloc(sizeof(TrueTypeTable));
1136 list l = listNewEmpty();
1137
1138 assert(l != 0);
1139
1140 listSetElementDtor(l, (list_destructor)DisposeNameRecord);
1141
1142 if (n != 0) {
1143 int i;
1144 for (i = 0; i < n; i++) {
1145 listAppend(l, NameRecordNewCopy(nr+i));
1146 }
1147 }
1148
1149 table->data = l;
1150 table->rawdata = 0;
1151 table->tag = T_name;
1152
1153 return table;
1154 }
1155
TrueTypeTableNew_post(sal_uInt32 format,sal_uInt32 italicAngle,sal_Int16 underlinePosition,sal_Int16 underlineThickness,sal_uInt32 isFixedPitch)1156 TrueTypeTable *TrueTypeTableNew_post(sal_uInt32 format,
1157 sal_uInt32 italicAngle,
1158 sal_Int16 underlinePosition,
1159 sal_Int16 underlineThickness,
1160 sal_uInt32 isFixedPitch)
1161 {
1162 assert(format == 0x00030000); /* Only format 3.0 is supported at this time */
1163 TrueTypeTable* table = (TrueTypeTable*)smalloc(sizeof(TrueTypeTable));
1164 tdata_post* post = (tdata_post*)smalloc(sizeof(tdata_post));
1165
1166 post->format = format;
1167 post->italicAngle = italicAngle;
1168 post->underlinePosition = underlinePosition;
1169 post->underlineThickness = underlineThickness;
1170 post->isFixedPitch = isFixedPitch;
1171 post->ptr = 0;
1172
1173 table->data = post;
1174 table->rawdata = 0;
1175 table->tag = T_post;
1176
1177 return table;
1178 }
1179
GetRawData(TrueTypeTable * _this,sal_uInt8 ** ptr,sal_uInt32 * len,sal_uInt32 * tag)1180 int GetRawData(TrueTypeTable *_this, sal_uInt8 **ptr, sal_uInt32 *len, sal_uInt32 *tag)
1181 {
1182 /* XXX do a binary search */
1183 unsigned int i;
1184
1185 assert(_this != 0);
1186 assert(ptr != 0);
1187 assert(len != 0);
1188 assert(tag != 0);
1189
1190 *ptr = 0; *len = 0; *tag = 0;
1191
1192 if (_this->rawdata) {
1193 free(_this->rawdata);
1194 _this->rawdata = 0;
1195 }
1196
1197 for(i=0; i < sizeof(vtable2)/sizeof(*vtable2); i++) {
1198 if (_this->tag == vtable2[i].tag) {
1199 return vtable2[i].f(_this, ptr, len, tag);
1200 }
1201 }
1202
1203 assert(!"Unknwon TrueType table.\n");
1204 return TTCR_UNKNOWN;
1205 }
1206
cmapAdd(TrueTypeTable * table,sal_uInt32 id,sal_uInt32 c,sal_uInt32 g)1207 void cmapAdd(TrueTypeTable *table, sal_uInt32 id, sal_uInt32 c, sal_uInt32 g)
1208 {
1209 sal_uInt32 i, found;
1210 table_cmap *t;
1211 CmapSubTable *s;
1212
1213 assert(table != 0);
1214 assert(table->tag == T_cmap);
1215 t = (table_cmap *) table->data; assert(t != 0);
1216 s = t->s; assert(s != 0);
1217
1218 found = 0;
1219
1220 for (i = 0; i < t->n; i++) {
1221 if (s[i].id == id) {
1222 found = 1;
1223 break;
1224 }
1225 }
1226
1227 if (!found) {
1228 if (t->n == t->m) {
1229 CmapSubTable* tmp = (CmapSubTable*)scalloc(t->m + CMAP_SUBTABLE_INCR, sizeof(CmapSubTable));
1230 memset(tmp, 0, t->m + CMAP_SUBTABLE_INCR * sizeof(CmapSubTable));
1231 memcpy(tmp, s, sizeof(CmapSubTable) * t->m);
1232 t->m += CMAP_SUBTABLE_INCR;
1233 free(s);
1234 s = tmp;
1235 t->s = s;
1236 }
1237
1238 for (i = 0; i < t->n; i++) {
1239 if (s[i].id > id) break;
1240 }
1241
1242 if (i < t->n) {
1243 memmove(s+i+1, s+i, t->n-i);
1244 }
1245
1246 t->n++;
1247
1248 s[i].id = id;
1249 s[i].n = 0;
1250 s[i].m = CMAP_PAIR_INIT;
1251 s[i].xc = (sal_uInt32*)scalloc(CMAP_PAIR_INIT, sizeof(sal_uInt32));
1252 s[i].xg = (sal_uInt32*)scalloc(CMAP_PAIR_INIT, sizeof(sal_uInt32));
1253 }
1254
1255 if (s[i].n == s[i].m) {
1256 sal_uInt32* tmp1 = (sal_uInt32*)scalloc(s[i].m + CMAP_PAIR_INCR, sizeof(sal_uInt32));
1257 sal_uInt32* tmp2 = (sal_uInt32*)scalloc(s[i].m + CMAP_PAIR_INCR, sizeof(sal_uInt32));
1258 assert(tmp1 != 0);
1259 assert(tmp2 != 0);
1260 memcpy(tmp1, s[i].xc, sizeof(sal_uInt32) * s[i].m);
1261 memcpy(tmp2, s[i].xg, sizeof(sal_uInt32) * s[i].m);
1262 s[i].m += CMAP_PAIR_INCR;
1263 free(s[i].xc);
1264 free(s[i].xg);
1265 s[i].xc = tmp1;
1266 s[i].xg = tmp2;
1267 }
1268
1269 s[i].xc[s[i].n] = c;
1270 s[i].xg[s[i].n] = g;
1271 s[i].n++;
1272 }
1273
glyfAdd(TrueTypeTable * table,GlyphData * glyphdata,TrueTypeFont * fnt)1274 sal_uInt32 glyfAdd(TrueTypeTable *table, GlyphData *glyphdata, TrueTypeFont *fnt)
1275 {
1276 list l;
1277 sal_uInt32 currentID;
1278 int ret, n, ncomponents;
1279 GlyphData *gd;
1280
1281 assert(table != 0);
1282 assert(table->tag == T_glyf);
1283
1284 if (!glyphdata) return (sal_uInt32)~0;
1285
1286 std::vector< sal_uInt32 > glyphlist;
1287
1288 ncomponents = GetTTGlyphComponents(fnt, glyphdata->glyphID, glyphlist);
1289
1290 l = (list) table->data;
1291 if (listCount(l) > 0) {
1292 listToLast(l);
1293 ret = n = ((GlyphData *) listCurrent(l))->newID + 1;
1294 } else {
1295 ret = n = 0;
1296 }
1297 glyphdata->newID = n++;
1298 listAppend(l, glyphdata);
1299
1300 if (ncomponents > 1 && glyphlist.size() > 1 )
1301 {
1302 std::vector< sal_uInt32 >::const_iterator it = glyphlist.begin();
1303 ++it;
1304 /* glyphData->glyphID is always the first glyph on the list */
1305 do
1306 {
1307 int found = 0;
1308 currentID = *it;
1309 /* XXX expensive! should be rewritten with sorted arrays! */
1310 listToFirst(l);
1311 do {
1312 if (((GlyphData *) listCurrent(l))->glyphID == currentID) {
1313 found = 1;
1314 break;
1315 }
1316 } while (listNext(l));
1317
1318 if (!found) {
1319 gd = GetTTRawGlyphData(fnt, currentID);
1320 gd->newID = n++;
1321 listAppend(l, gd);
1322 }
1323 } while( ++it != glyphlist.end() );
1324 }
1325
1326 return ret;
1327 }
1328
glyfCount(const TrueTypeTable * table)1329 sal_uInt32 glyfCount(const TrueTypeTable *table)
1330 {
1331 assert(table != 0);
1332 assert(table->tag == T_glyf);
1333 return listCount((list) table->data);
1334 }
1335
1336
nameAdd(TrueTypeTable * table,NameRecord * nr)1337 void nameAdd(TrueTypeTable *table, NameRecord *nr)
1338 {
1339 list l;
1340
1341 assert(table != 0);
1342 assert(table->tag == T_name);
1343
1344 l = (list) table->data;
1345
1346 listAppend(l, NameRecordNewCopy(nr));
1347 }
1348
FindTable(TrueTypeCreator * tt,sal_uInt32 tag)1349 static TrueTypeTable *FindTable(TrueTypeCreator *tt, sal_uInt32 tag)
1350 {
1351 if (listIsEmpty(tt->tables)) return 0;
1352
1353 listToFirst(tt->tables);
1354
1355 do {
1356 if (((TrueTypeTable *) listCurrent(tt->tables))->tag == tag) {
1357 return (TrueTypeTable*)listCurrent(tt->tables);
1358 }
1359 } while (listNext(tt->tables));
1360
1361 return 0;
1362 }
1363
1364 /* This function processes all the tables and synchronizes them before creating
1365 * the output TrueType stream.
1366 *
1367 * *** It adds two TrueType tables to the font: 'loca' and 'hmtx' ***
1368 *
1369 * It does:
1370 *
1371 * - Re-numbers glyph IDs and creates 'glyf', 'loca', and 'hmtx' tables.
1372 * - Calculates xMin, yMin, xMax, and yMax and stores values in 'head' table.
1373 * - Stores indexToLocFormat in 'head'
1374 * - updates 'maxp' table
1375 * - Calculates advanceWidthMax, minLSB, minRSB, xMaxExtent and numberOfHMetrics
1376 * in 'hhea' table
1377 *
1378 */
ProcessTables(TrueTypeCreator * tt)1379 static void ProcessTables(TrueTypeCreator *tt)
1380 {
1381 TrueTypeTable *glyf, *loca, *head, *maxp, *hhea;
1382 list glyphlist;
1383 sal_uInt32 nGlyphs, locaLen = 0, glyfLen = 0;
1384 sal_Int16 xMin = 0, yMin = 0, xMax = 0, yMax = 0;
1385 sal_uInt32 i = 0;
1386 sal_Int16 indexToLocFormat;
1387 sal_uInt8 *hmtxPtr, *hheaPtr;
1388 sal_uInt32 hmtxSize;
1389 sal_uInt8 *p1, *p2;
1390 sal_uInt16 maxPoints = 0, maxContours = 0, maxCompositePoints = 0, maxCompositeContours = 0;
1391 int nlsb = 0;
1392 sal_uInt32 *gid; /* array of old glyphIDs */
1393
1394 glyf = FindTable(tt, T_glyf);
1395 glyphlist = (list) glyf->data;
1396 nGlyphs = listCount(glyphlist);
1397 assert(nGlyphs != 0);
1398 gid = (sal_uInt32*)scalloc(nGlyphs, sizeof(sal_uInt32));
1399
1400 RemoveTable(tt, T_loca);
1401 RemoveTable(tt, T_hmtx);
1402
1403 /* XXX Need to make sure that composite glyphs do not break during glyph renumbering */
1404
1405 listToFirst(glyphlist);
1406 do {
1407 GlyphData *gd = (GlyphData *) listCurrent(glyphlist);
1408 sal_Int16 z;
1409 glyfLen += gd->nbytes;
1410 /* XXX if (gd->nbytes & 1) glyfLen++; */
1411
1412
1413 assert(gd->newID == i);
1414 gid[i++] = gd->glyphID;
1415 /* gd->glyphID = i++; */
1416
1417 /* printf("IDs: %d %d.\n", gd->glyphID, gd->newID); */
1418
1419 if (gd->nbytes != 0) {
1420 z = GetInt16(gd->ptr, 2, 1);
1421 if (z < xMin) xMin = z;
1422
1423 z = GetInt16(gd->ptr, 4, 1);
1424 if (z < yMin) yMin = z;
1425
1426 z = GetInt16(gd->ptr, 6, 1);
1427 if (z > xMax) xMax = z;
1428
1429 z = GetInt16(gd->ptr, 8, 1);
1430 if (z > yMax) yMax = z;
1431 }
1432
1433 if (gd->compflag == 0) { /* non-composite glyph */
1434 if (gd->npoints > maxPoints) maxPoints = gd->npoints;
1435 if (gd->ncontours > maxContours) maxContours = gd->ncontours;
1436 } else { /* composite glyph */
1437 if (gd->npoints > maxCompositePoints) maxCompositePoints = gd->npoints;
1438 if (gd->ncontours > maxCompositeContours) maxCompositeContours = gd->ncontours;
1439 }
1440
1441 } while (listNext(glyphlist));
1442
1443 indexToLocFormat = (glyfLen / 2 > 0xFFFF) ? 1 : 0;
1444 locaLen = indexToLocFormat ? (nGlyphs + 1) << 2 : (nGlyphs + 1) << 1;
1445
1446 sal_uInt8* glyfPtr = ttmalloc(glyfLen);
1447 sal_uInt8* locaPtr = ttmalloc(locaLen);
1448 TTSimpleGlyphMetrics* met = (TTSimpleGlyphMetrics*)scalloc(nGlyphs, sizeof(TTSimpleGlyphMetrics));
1449 i = 0;
1450
1451 listToFirst(glyphlist);
1452 p1 = glyfPtr;
1453 p2 = locaPtr;
1454 do {
1455 GlyphData *gd = (GlyphData *) listCurrent(glyphlist);
1456
1457 if (gd->compflag) { /* re-number all components */
1458 sal_uInt16 flags, index;
1459 sal_uInt8 *ptr = gd->ptr + 10;
1460 do {
1461 sal_uInt32 j;
1462 flags = GetUInt16(ptr, 0, 1);
1463 index = GetUInt16(ptr, 2, 1);
1464 /* XXX use the sorted array of old to new glyphID mapping and do a binary search */
1465 for (j = 0; j < nGlyphs; j++) {
1466 if (gid[j] == index) {
1467 break;
1468 }
1469 }
1470 /* printf("X: %d -> %d.\n", index, j); */
1471
1472 PutUInt16((sal_uInt16) j, ptr, 2, 1);
1473
1474 ptr += 4;
1475
1476 if (flags & ARG_1_AND_2_ARE_WORDS) {
1477 ptr += 4;
1478 } else {
1479 ptr += 2;
1480 }
1481
1482 if (flags & WE_HAVE_A_SCALE) {
1483 ptr += 2;
1484 } else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) {
1485 ptr += 4;
1486 } else if (flags & WE_HAVE_A_TWO_BY_TWO) {
1487 ptr += 8;
1488 }
1489 } while (flags & MORE_COMPONENTS);
1490 }
1491
1492 if (gd->nbytes != 0) {
1493 memcpy(p1, gd->ptr, gd->nbytes);
1494 }
1495 if (indexToLocFormat == 1) {
1496 PutUInt32(p1 - glyfPtr, p2, 0, 1);
1497 p2 += 4;
1498 } else {
1499 PutUInt16((sal_uInt16)((p1 - glyfPtr) >> 1), p2, 0, 1);
1500 p2 += 2;
1501 }
1502 p1 += gd->nbytes;
1503
1504 /* fill the array of metrics */
1505 met[i].adv = gd->aw;
1506 met[i].sb = gd->lsb;
1507 i++;
1508 } while (listNext(glyphlist));
1509
1510 free(gid);
1511
1512 if (indexToLocFormat == 1) {
1513 PutUInt32(p1 - glyfPtr, p2, 0, 1);
1514 } else {
1515 PutUInt16((sal_uInt16)((p1 - glyfPtr) >> 1), p2, 0, 1);
1516 }
1517
1518 glyf->rawdata = glyfPtr;
1519
1520 loca = TrueTypeTableNew_loca(); assert(loca != 0);
1521 ((tdata_loca *) loca->data)->ptr = locaPtr;
1522 ((tdata_loca *) loca->data)->nbytes = locaLen;
1523
1524 AddTable(tt, loca);
1525
1526 head = FindTable(tt, T_head);
1527 sal_uInt8* const pHeadData = (sal_uInt8*)head->data;
1528 PutInt16(xMin, pHeadData, 36, 1);
1529 PutInt16(yMin, pHeadData, 38, 1);
1530 PutInt16(xMax, pHeadData, 40, 1);
1531 PutInt16(yMax, pHeadData, 42, 1);
1532 PutInt16(indexToLocFormat, pHeadData, 50, 1);
1533
1534 maxp = FindTable(tt, T_maxp);
1535
1536 sal_uInt8* const pMaxpData = (sal_uInt8*)maxp->data;
1537 PutUInt16((sal_uInt16)nGlyphs, pMaxpData, 4, 1);
1538 PutUInt16(maxPoints, pMaxpData, 6, 1);
1539 PutUInt16(maxContours, pMaxpData, 8, 1);
1540 PutUInt16(maxCompositePoints, pMaxpData, 10, 1);
1541 PutUInt16(maxCompositeContours, pMaxpData, 12, 1);
1542
1543 #if 0
1544 /* XXX do not overwrite the existing data. Fix: re-calculate these numbers here */
1545 PutUInt16(2, maxp->data, 14, 1); /* maxZones is always 2 */
1546 PutUInt16(0, maxp->data, 16, 1); /* maxTwilightPoints */
1547 PutUInt16(0, maxp->data, 18, 1); /* maxStorage */
1548 PutUInt16(0, maxp->data, 20, 1); /* maxFunctionDefs */
1549 PutUint16(0, maxp->data, 22, 1); /* maxInstructionDefs */
1550 PutUint16(0, maxp->data, 24, 1); /* maxStackElements */
1551 PutUint16(0, maxp->data, 26, 1); /* maxSizeOfInstructions */
1552 PutUint16(0, maxp->data, 28, 1); /* maxComponentElements */
1553 PutUint16(0, maxp->data, 30, 1); /* maxComponentDepth */
1554 #endif
1555
1556 /*
1557 * Generate an htmx table and update hhea table
1558 */
1559 hhea = FindTable(tt, T_hhea); assert(hhea != 0);
1560 hheaPtr = (sal_uInt8 *) hhea->data;
1561 if (nGlyphs > 2) {
1562 for (i = nGlyphs - 1; i > 0; i--) {
1563 if (met[i].adv != met[i-1].adv) break;
1564 }
1565 nlsb = nGlyphs - 1 - i;
1566 }
1567 hmtxSize = (nGlyphs - nlsb) * 4 + nlsb * 2;
1568 hmtxPtr = ttmalloc(hmtxSize);
1569 p1 = hmtxPtr;
1570
1571 for (i = 0; i < nGlyphs; i++) {
1572 if (i < nGlyphs - nlsb) {
1573 PutUInt16(met[i].adv, p1, 0, 1);
1574 PutUInt16(met[i].sb, p1, 2, 1);
1575 p1 += 4;
1576 } else {
1577 PutUInt16(met[i].sb, p1, 0, 1);
1578 p1 += 2;
1579 }
1580 }
1581
1582 AddTable(tt, TrueTypeTableNew(T_hmtx, hmtxSize, hmtxPtr));
1583 PutUInt16((sal_uInt16)(nGlyphs - nlsb), hheaPtr, 34, 1);
1584 free(hmtxPtr);
1585 free(met);
1586 }
1587
1588 } // namespace vcl
1589
1590 extern "C"
1591 {
1592 /**
1593 * TrueTypeCreator destructor. It calls destructors for all TrueTypeTables added to it.
1594 */
TrueTypeCreatorDispose(vcl::TrueTypeCreator * _this)1595 void TrueTypeCreatorDispose(vcl::TrueTypeCreator *_this)
1596 {
1597 listDispose(_this->tables);
1598 free(_this);
1599 }
1600
1601
1602 /**
1603 * Destructor for the TrueTypeTable object.
1604 */
TrueTypeTableDispose(vcl::TrueTypeTable * _this)1605 void TrueTypeTableDispose(vcl::TrueTypeTable *_this)
1606 {
1607 /* XXX do a binary search */
1608 unsigned int i;
1609
1610 assert(_this != 0);
1611
1612 if (_this->rawdata) free(_this->rawdata);
1613
1614 for(i=0; i < sizeof(vcl::vtable1)/sizeof(*vcl::vtable1); i++) {
1615 if (_this->tag == vcl::vtable1[i].tag) {
1616 vcl::vtable1[i].f(_this);
1617 return;
1618 }
1619 }
1620 assert(!"Unknown TrueType table.\n");
1621 }
1622 }
1623
1624
1625 #ifdef TEST_TTCR
main(void)1626 int main(void)
1627 {
1628 TrueTypeCreator *ttcr;
1629 sal_uInt8 *t1, *t2, *t3, *t4, *t5, *t6, *t7;
1630
1631 TrueTypeCreatorNewEmpty(mkTag('t','r','u','e'), &ttcr);
1632
1633 t1 = malloc(1000); memset(t1, 'a', 1000);
1634 t2 = malloc(2000); memset(t2, 'b', 2000);
1635 t3 = malloc(3000); memset(t3, 'c', 3000);
1636 t4 = malloc(4000); memset(t4, 'd', 4000);
1637 t5 = malloc(5000); memset(t5, 'e', 5000);
1638 t6 = malloc(6000); memset(t6, 'f', 6000);
1639 t7 = malloc(7000); memset(t7, 'g', 7000);
1640
1641 AddTable(ttcr, TrueTypeTableNew(0x6D617870, 1000, t1));
1642 AddTable(ttcr, TrueTypeTableNew(0x4F532F32, 2000, t2));
1643 AddTable(ttcr, TrueTypeTableNew(0x636D6170, 3000, t3));
1644 AddTable(ttcr, TrueTypeTableNew(0x6C6F6361, 4000, t4));
1645 AddTable(ttcr, TrueTypeTableNew(0x68686561, 5000, t5));
1646 AddTable(ttcr, TrueTypeTableNew(0x676C7966, 6000, t6));
1647 AddTable(ttcr, TrueTypeTableNew(0x6B65726E, 7000, t7));
1648
1649 free(t1);
1650 free(t2);
1651 free(t3);
1652 free(t4);
1653 free(t5);
1654 free(t6);
1655 free(t7);
1656
1657
1658 StreamToFile(ttcr, "ttcrout.ttf");
1659
1660 TrueTypeCreatorDispose(ttcr);
1661 return 0;
1662 }
1663 #endif
1664
1665 /* vim: set noet sw=4 ts=4: */
1666