xref: /aoo42x/main/vcl/source/fontsubset/ttcr.cxx (revision 9f62ea84)
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 
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 */
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 
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 
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 
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 
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 
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 
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 
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 
217 static int TableEntryCompareF(const void *l, const void *r)
218 {
219     return ((const TableEntry *) l)->tag - ((const TableEntry *) r)->tag;
220 }
221 
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 
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 
250 _inline void *smalloc(sal_uInt32 size)
251 {
252     void *res = malloc(size);
253     assert(res != 0);
254     return res;
255 }
256 
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 
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 
280 int AddTable(TrueTypeCreator *_this, TrueTypeTable *table)
281 {
282     if (table != 0) {
283         listAppend(_this->tables, table);
284     }
285     return SF_OK;
286 }
287 
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 
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 
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) return SF_BADFILE;
398     if ((fd = fopen(fname, "wb")) == NULL) return SF_BADFILE;
399 
400     if (fwrite(ptr, 1, length, fd) != length) {
401         r = SF_FILEIO;
402     } else {
403         r = SF_OK;
404     }
405 
406     fclose(fd);
407     free(ptr);
408     return r;
409 }
410 
411 
412 
413 /*
414  * TrueTypeTable private methods
415  */
416 
417 #define TABLESIZE_head 54
418 #define TABLESIZE_hhea 36
419 #define TABLESIZE_maxp 32
420 
421 
422 
423 /*    Table         data points to
424  * --------------------------------------------
425  *    generic       tdata_generic struct
426  *    'head'        TABLESIZE_head bytes of memory
427  *    'hhea'        TABLESIZE_hhea bytes of memory
428  *    'loca'        tdata_loca struct
429  *    'maxp'        TABLESIZE_maxp bytes of memory
430  *    'glyf'        list of GlyphData structs (defined in sft.h)
431  *    'name'        list of NameRecord structs (defined in sft.h)
432  *    'post'        tdata_post struct
433  *
434  */
435 
436 
437 #define CMAP_SUBTABLE_INIT 10
438 #define CMAP_SUBTABLE_INCR 10
439 #define CMAP_PAIR_INIT 500
440 #define CMAP_PAIR_INCR 500
441 
442 typedef struct {
443     sal_uInt32  id;                         /* subtable ID (platform/encoding ID)    */
444     sal_uInt32  n;                          /* number of used translation pairs      */
445     sal_uInt32  m;                          /* number of allocated translation pairs */
446     sal_uInt32 *xc;                         /* character array                       */
447     sal_uInt32 *xg;                         /* glyph array                           */
448 } CmapSubTable;
449 
450 typedef struct {
451     sal_uInt32 n;                           /* number of used CMAP sub-tables       */
452     sal_uInt32 m;                           /* number of allocated CMAP sub-tables  */
453     CmapSubTable *s;                    /* sotred array of sub-tables           */
454 } table_cmap;
455 
456 typedef struct {
457     sal_uInt32 tag;
458     sal_uInt32 nbytes;
459     sal_uInt8 *ptr;
460 } tdata_generic;
461 
462 typedef struct {
463     sal_uInt32 nbytes;                      /* number of bytes in loca table */
464     sal_uInt8 *ptr;                          /* pointer to the data */
465 } tdata_loca;
466 
467 typedef struct {
468     sal_uInt32 format;
469     sal_uInt32 italicAngle;
470     sal_Int16  underlinePosition;
471     sal_Int16  underlineThickness;
472     sal_uInt32 isFixedPitch;
473     void   *ptr;                        /* format-specific pointer */
474 } tdata_post;
475 
476 
477 /* allocate memory for a TT table */
478 static sal_uInt8 *ttmalloc(sal_uInt32 nbytes)
479 {
480     sal_uInt32 n;
481 
482     n = (nbytes + 3) & (sal_uInt32) ~3;
483     sal_uInt8* res = (sal_uInt8*)malloc(n);
484     assert(res != 0);
485     memset(res, 0, n);
486 
487     return res;
488 }
489 
490 static void FreeGlyphData(void *ptr)
491 {
492     GlyphData *p = (GlyphData *) ptr;
493     if (p->ptr) free(p->ptr);
494     free(p);
495 }
496 
497 static void TrueTypeTableDispose_generic(TrueTypeTable *_this)
498 {
499     if (_this) {
500         if (_this->data) {
501             tdata_generic *pdata = (tdata_generic *) _this->data;
502             if (pdata->nbytes) free(pdata->ptr);
503             free(_this->data);
504         }
505         free(_this);
506     }
507 }
508 
509 static void TrueTypeTableDispose_head(TrueTypeTable *_this)
510 {
511     if (_this) {
512         if (_this->data) free(_this->data);
513         free(_this);
514     }
515 }
516 
517 static void TrueTypeTableDispose_hhea(TrueTypeTable *_this)
518 {
519     if (_this) {
520         if (_this->data) free(_this->data);
521         free(_this);
522     }
523 }
524 
525 static void TrueTypeTableDispose_loca(TrueTypeTable *_this)
526 {
527     if (_this) {
528         if (_this->data) {
529             tdata_loca *p = (tdata_loca *) _this->data;
530             if (p->ptr) free(p->ptr);
531             free(_this->data);
532         }
533         free(_this);
534     }
535 }
536 
537 static void TrueTypeTableDispose_maxp(TrueTypeTable *_this)
538 {
539     if (_this) {
540         if (_this->data) free(_this->data);
541         free(_this);
542     }
543 }
544 
545 static void TrueTypeTableDispose_glyf(TrueTypeTable *_this)
546 {
547     if (_this) {
548         if (_this->data) listDispose((list) _this->data);
549         free(_this);
550     }
551 }
552 
553 static void TrueTypeTableDispose_cmap(TrueTypeTable *_this)
554 {
555     table_cmap *t;
556     CmapSubTable *s;
557     sal_uInt32 i;
558 
559     if (_this) {
560         t = (table_cmap *) _this->data;
561         if (t) {
562             s = t->s;
563             if (s) {
564                 for (i = 0; i < t->m; i++) {
565                     if (s[i].xc) free(s[i].xc);
566                     if (s[i].xg) free(s[i].xg);
567                 }
568                 free(s);
569             }
570             free(t);
571         }
572         free(_this);
573     }
574 }
575 
576 static void TrueTypeTableDispose_name(TrueTypeTable *_this)
577 {
578     if (_this) {
579         if (_this->data) listDispose((list) _this->data);
580         free(_this);
581     }
582 }
583 
584 static void TrueTypeTableDispose_post(TrueTypeTable *_this)
585 {
586     if (_this) {
587         tdata_post *p = (tdata_post *) _this->data;
588         if (p) {
589             if (p->format == 0x00030000) {
590                 /* do nothing */
591             } else {
592                 fprintf(stderr, "Unsupported format of a 'post' table: %08X.\n", (int)p->format);
593             }
594             free(p);
595         }
596         free(_this);
597     }
598 }
599 
600 /* destructor vtable */
601 
602 static struct {
603     sal_uInt32 tag;
604     void (*f)(TrueTypeTable *);
605 } vtable1[] =
606 {
607     {0,      TrueTypeTableDispose_generic},
608     {T_head, TrueTypeTableDispose_head},
609     {T_hhea, TrueTypeTableDispose_hhea},
610     {T_loca, TrueTypeTableDispose_loca},
611     {T_maxp, TrueTypeTableDispose_maxp},
612     {T_glyf, TrueTypeTableDispose_glyf},
613     {T_cmap, TrueTypeTableDispose_cmap},
614     {T_name, TrueTypeTableDispose_name},
615     {T_post, TrueTypeTableDispose_post}
616 
617 };
618 
619 static int GetRawData_generic(TrueTypeTable *_this, sal_uInt8 **ptr, sal_uInt32 *len, sal_uInt32 *tag)
620 {
621     assert(_this != 0);
622     assert(_this->data != 0);
623 
624     *ptr = ((tdata_generic *) _this->data)->ptr;
625     *len = ((tdata_generic *) _this->data)->nbytes;
626     *tag = ((tdata_generic *) _this->data)->tag;
627 
628     return TTCR_OK;
629 }
630 
631 
632 static int GetRawData_head(TrueTypeTable *_this, sal_uInt8 **ptr, sal_uInt32 *len, sal_uInt32 *tag)
633 {
634     *len = TABLESIZE_head;
635     *ptr = (sal_uInt8 *) _this->data;
636     *tag = T_head;
637 
638     return TTCR_OK;
639 }
640 
641 static int GetRawData_hhea(TrueTypeTable *_this, sal_uInt8 **ptr, sal_uInt32 *len, sal_uInt32 *tag)
642 {
643     *len = TABLESIZE_hhea;
644     *ptr = (sal_uInt8 *) _this->data;
645     *tag = T_hhea;
646 
647     return TTCR_OK;
648 }
649 
650 static int GetRawData_loca(TrueTypeTable *_this, sal_uInt8 **ptr, sal_uInt32 *len, sal_uInt32 *tag)
651 {
652     tdata_loca *p;
653 
654     assert(_this->data != 0);
655 
656     p = (tdata_loca *) _this->data;
657 
658     if (p->nbytes == 0) return TTCR_ZEROGLYPHS;
659 
660     *ptr = p->ptr;
661     *len = p->nbytes;
662     *tag = T_loca;
663 
664     return TTCR_OK;
665 }
666 
667 static int GetRawData_maxp(TrueTypeTable *_this, sal_uInt8 **ptr, sal_uInt32 *len, sal_uInt32 *tag)
668 {
669     *len = TABLESIZE_maxp;
670     *ptr = (sal_uInt8 *) _this->data;
671     *tag = T_maxp;
672 
673     return TTCR_OK;
674 }
675 
676 static int GetRawData_glyf(TrueTypeTable *_this, sal_uInt8 **ptr, sal_uInt32 *len, sal_uInt32 *tag)
677 {
678     sal_uInt32 n, nbytes = 0;
679     list l = (list) _this->data;
680     /* sal_uInt16 curID = 0;    */               /* to check if glyph IDs are sequential and start from zero */
681     sal_uInt8 *p;
682 
683     *ptr = 0;
684     *len = 0;
685     *tag = 0;
686 
687     if (listCount(l) == 0) return TTCR_ZEROGLYPHS;
688 
689     listToFirst(l);
690     do {
691         /* if (((GlyphData *) listCurrent(l))->glyphID != curID++) return TTCR_GLYPHSEQ; */
692         nbytes += ((GlyphData *) listCurrent(l))->nbytes;
693     } while (listNext(l));
694 
695     p = _this->rawdata = ttmalloc(nbytes);
696 
697     listToFirst(l);
698     do {
699         n = ((GlyphData *) listCurrent(l))->nbytes;
700         if (n != 0) {
701             memcpy(p, ((GlyphData *) listCurrent(l))->ptr, n);
702             p += n;
703         }
704     } while (listNext(l));
705 
706     *len = nbytes;
707     *ptr = _this->rawdata;
708     *tag = T_glyf;
709 
710     return TTCR_OK;
711 }
712 
713 /* cmap packers */
714 static sal_uInt8 *PackCmapType0(CmapSubTable *s, sal_uInt32 *length)
715 {
716     sal_uInt8* ptr = (sal_uInt8*)smalloc(262);
717     sal_uInt8 *p = ptr + 6;
718     sal_uInt32 i, j;
719     sal_uInt16 g;
720 
721     PutUInt16(0, ptr, 0, 1);
722     PutUInt16(262, ptr, 2, 1);
723     PutUInt16(0, ptr, 4, 1);
724 
725     for (i = 0; i < 256; i++) {
726         g = 0;
727         for (j = 0; j < s->n; j++) {
728             if (s->xc[j] == i) {
729                 g = (sal_uInt16) s->xg[j];
730             }
731         }
732         p[i] = (sal_uInt8) g;
733     }
734     *length = 262;
735     return ptr;
736 }
737 
738 static sal_uInt8 *PackCmapType6(CmapSubTable *s, sal_uInt32 *length)
739 {
740     sal_uInt8* ptr = (sal_uInt8*)smalloc(s->n*2 + 10);
741     sal_uInt8 *p = ptr + 10;
742     sal_uInt32 i, j;
743     sal_uInt16 g;
744 
745     PutUInt16(6, ptr, 0, 1);
746     PutUInt16((sal_uInt16)(s->n*2+10), ptr, 2, 1);
747     PutUInt16(0, ptr, 4, 1);
748     PutUInt16(0, ptr, 6, 1);
749     PutUInt16((sal_uInt16)(s->n), ptr, 8, 1 );
750 
751     for (i = 0; i < s->n; i++) {
752         g = 0;
753         for (j = 0; j < s->n; j++) {
754             if (s->xc[j] == i) {
755                 g = (sal_uInt16) s->xg[j];
756             }
757         }
758         PutUInt16( g, p, 2*i, 1 );
759     }
760     *length = s->n*2+10;
761     return ptr;
762 }
763 
764 
765 
766 /* XXX it only handles Format 0 encoding tables */
767 static sal_uInt8 *PackCmap(CmapSubTable *s, sal_uInt32 *length)
768 {
769     if( s->xg[s->n-1] > 0xff )
770         return PackCmapType6(s, length);
771     else
772         return PackCmapType0(s, length);
773 }
774 
775 static int GetRawData_cmap(TrueTypeTable *_this, sal_uInt8 **ptr, sal_uInt32 *len, sal_uInt32 *tag)
776 {
777     table_cmap *t;
778     sal_uInt32 i;
779     sal_uInt32 tlen = 0;
780     sal_uInt32 l;
781     sal_uInt32 cmapsize;
782     sal_uInt8 *cmap;
783     sal_uInt32 coffset;
784 
785     assert(_this != 0);
786     t = (table_cmap *) _this->data;
787     assert(t != 0);
788     assert(t->n != 0);
789 
790     sal_uInt8** subtables = (sal_uInt8**)scalloc(t->n, sizeof(sal_uInt8 *));
791     sal_uInt32* sizes = (sal_uInt32*)scalloc(t->n, sizeof(sal_uInt32));
792 
793     for (i = 0; i < t->n; i++) {
794         subtables[i] = PackCmap(t->s+i, &l);
795         sizes[i] = l;
796         tlen += l;
797     }
798 
799     cmapsize = tlen + 4 + 8 * t->n;
800     _this->rawdata = cmap = ttmalloc(cmapsize);
801 
802     PutUInt16(0, cmap, 0, 1);
803     PutUInt16((sal_uInt16)t->n, cmap, 2, 1);
804     coffset = 4 + t->n * 8;
805 
806     for (i = 0; i < t->n; i++) {
807         PutUInt16((sal_uInt16)(t->s[i].id >> 16), cmap + 4, i * 8, 1);
808         PutUInt16((sal_uInt16)(t->s[i].id & 0xFF), cmap + 4, 2 + i * 8, 1);
809         PutUInt32(coffset, cmap + 4, 4 + i * 8, 1);
810         memcpy(cmap + coffset, subtables[i], sizes[i]);
811         free(subtables[i]);
812         coffset += sizes[i];
813     }
814 
815     free(subtables);
816     free(sizes);
817 
818     *ptr = cmap;
819     *len = cmapsize;
820     *tag = T_cmap;
821 
822     return TTCR_OK;
823 }
824 
825 
826 static int GetRawData_name(TrueTypeTable *_this, sal_uInt8 **ptr, sal_uInt32 *len, sal_uInt32 *tag)
827 {
828     list l;
829     sal_Int16 i=0, n;                          /* number of Name Records */
830     int stringLen = 0;
831     sal_uInt8 *p1, *p2;
832 
833     *ptr = 0;
834     *len = 0;
835     *tag = 0;
836 
837     assert(_this != 0);
838     l = (list) _this->data;
839     assert(l != 0);
840 
841     if ((n = (sal_Int16)listCount(l)) == 0) return TTCR_NONAMES;
842 
843     NameRecord* nr = (NameRecord*)scalloc(n, sizeof(NameRecord));
844 
845     listToFirst(l);
846 
847     do {
848         memcpy(nr+i, listCurrent(l), sizeof(NameRecord));
849         stringLen += nr[i].slen;
850         i++;
851     } while (listNext(l));
852 
853     if (stringLen > 65535) {
854         free(nr);
855         return TTCR_NAMETOOLONG;
856     }
857 
858     qsort(nr, n, sizeof(NameRecord), NameRecordCompareF);
859 
860     int nameLen = stringLen + 12 * n + 6;
861     sal_uInt8* name = (sal_uInt8*)ttmalloc(nameLen);
862 
863     PutUInt16(0, name, 0, 1);
864     PutUInt16(n, name, 2, 1);
865     PutUInt16((sal_uInt16)(6 + 12 * n), name, 4, 1);
866 
867     p1 = name + 6;
868     p2 = p1 + 12 * n;
869 
870     for (i = 0; i < n; i++) {
871         PutUInt16(nr[i].platformID, p1, 0, 1);
872         PutUInt16(nr[i].encodingID, p1, 2, 1);
873         PutUInt16(nr[i].languageID, p1, 4, 1);
874         PutUInt16(nr[i].nameID, p1, 6, 1);
875         PutUInt16(nr[i].slen, p1, 8, 1);
876         PutUInt16((sal_uInt16)(p2 - (name + 6 + 12 * n)), p1, 10, 1);
877         memcpy(p2, nr[i].sptr, nr[i].slen);
878         /* {int j; for(j=0; j<nr[i].slen; j++) printf("%c", nr[i].sptr[j]); printf("\n"); }; */
879         p2 += nr[i].slen;
880         p1 += 12;
881     }
882 
883     free(nr);
884     _this->rawdata = name;
885 
886     *ptr = name;
887     *len = (sal_uInt16)nameLen;
888     *tag = T_name;
889 
890     /*{int j; for(j=0; j<nameLen; j++) printf("%c", name[j]); }; */
891 
892     return TTCR_OK;
893 }
894 
895 static int GetRawData_post(TrueTypeTable *_this, sal_uInt8 **ptr, sal_uInt32 *len, sal_uInt32 *tag)
896 {
897     tdata_post *p = (tdata_post *) _this->data;
898     sal_uInt8 *post = 0;
899     sal_uInt32 postLen = 0;
900     int ret;
901 
902     if (_this->rawdata) free(_this->rawdata);
903 
904     if (p->format == 0x00030000) {
905         postLen = 32;
906         post = ttmalloc(postLen);
907         PutUInt32(0x00030000, post, 0, 1);
908         PutUInt32(p->italicAngle, post, 4, 1);
909         PutUInt16(p->underlinePosition, post, 8, 1);
910         PutUInt16(p->underlineThickness, post, 10, 1);
911         PutUInt16((sal_uInt16)p->isFixedPitch, post, 12, 1);
912         ret = TTCR_OK;
913     } else {
914         fprintf(stderr, "Unrecognized format of a post table: %08X.\n", (int)p->format);
915         ret = TTCR_POSTFORMAT;
916     }
917 
918     *ptr = _this->rawdata = post;
919     *len = postLen;
920     *tag = T_post;
921 
922     return ret;
923 }
924 
925 
926 
927 
928 
929 static struct {
930     sal_uInt32 tag;
931     int (*f)(TrueTypeTable *, sal_uInt8 **, sal_uInt32 *, sal_uInt32 *);
932 } vtable2[] =
933 {
934     {0,      GetRawData_generic},
935     {T_head, GetRawData_head},
936     {T_hhea, GetRawData_hhea},
937     {T_loca, GetRawData_loca},
938     {T_maxp, GetRawData_maxp},
939     {T_glyf, GetRawData_glyf},
940     {T_cmap, GetRawData_cmap},
941     {T_name, GetRawData_name},
942     {T_post, GetRawData_post}
943 
944 
945 };
946 
947 /*
948  * TrueTypeTable public methods
949  */
950 
951 /* Note: Type42 fonts only need these tables:
952  *        head, hhea, loca, maxp, cvt, prep, glyf, hmtx, fpgm
953  *
954  * Microsoft required tables
955  *        cmap, glyf, head, hhea, hmtx, loca, maxp, name, post, OS/2
956  *
957  * Apple required tables
958  *        cmap, glyf, head, hhea, hmtx, loca, maxp, name, post
959  *
960  */
961 
962 TrueTypeTable *TrueTypeTableNew(sal_uInt32 tag,
963                                 sal_uInt32 nbytes,
964                                 const sal_uInt8* ptr)
965 {
966     TrueTypeTable* table = (TrueTypeTable*)smalloc(sizeof(TrueTypeTable));
967     tdata_generic* pdata = (tdata_generic*)smalloc(sizeof(tdata_generic));
968     pdata->nbytes = nbytes;
969     pdata->tag = tag;
970     if (nbytes) {
971         pdata->ptr = ttmalloc(nbytes);
972         memcpy(pdata->ptr, ptr, nbytes);
973     } else {
974         pdata->ptr = 0;
975     }
976 
977     table->tag = 0;
978     table->data = pdata;
979     table->rawdata = 0;
980 
981     return table;
982 }
983 
984 TrueTypeTable *TrueTypeTableNew_head(sal_uInt32 fontRevision,
985                                      sal_uInt16 flags,
986                                      sal_uInt16 unitsPerEm,
987                                      const sal_uInt8* created,
988                                      sal_uInt16 macStyle,
989                                      sal_uInt16 lowestRecPPEM,
990                                      sal_Int16  fontDirectionHint)
991 {
992     assert(created != 0);
993 
994     TrueTypeTable* table  = (TrueTypeTable*)smalloc(sizeof(TrueTypeTable));
995     sal_uInt8* ptr = (sal_uInt8*)ttmalloc(TABLESIZE_head);
996 
997 
998     PutUInt32(0x00010000, ptr, 0, 1);             /* version */
999     PutUInt32(fontRevision, ptr, 4, 1);
1000     PutUInt32(0x5F0F3CF5, ptr, 12, 1);            /* magic number */
1001     PutUInt16(flags, ptr, 16, 1);
1002     PutUInt16(unitsPerEm, ptr, 18, 1);
1003     memcpy(ptr+20, created, 8);                   /* Created Long Date */
1004     memset(ptr+28, 0, 8);                         /* Modified Long Date */
1005     PutUInt16(macStyle, ptr, 44, 1);
1006     PutUInt16(lowestRecPPEM, ptr, 46, 1);
1007     PutUInt16(fontDirectionHint, ptr, 48, 1);
1008     PutUInt16(0, ptr, 52, 1);                     /* glyph data format: 0 */
1009 
1010     table->data = (void *) ptr;
1011     table->tag = T_head;
1012     table->rawdata = 0;
1013 
1014     return table;
1015 }
1016 
1017 TrueTypeTable *TrueTypeTableNew_hhea(sal_Int16  ascender,
1018                                      sal_Int16  descender,
1019                                      sal_Int16  linegap,
1020                                      sal_Int16  caretSlopeRise,
1021                                      sal_Int16  caretSlopeRun)
1022 {
1023     TrueTypeTable* table = (TrueTypeTable*)smalloc(sizeof(TrueTypeTable));
1024     sal_uInt8* ptr = (sal_uInt8*)ttmalloc(TABLESIZE_hhea);
1025 
1026     PutUInt32(0x00010000, ptr, 0, 1);             /* version */
1027     PutUInt16(ascender, ptr, 4, 1);
1028     PutUInt16(descender, ptr, 6, 1);
1029     PutUInt16(linegap, ptr, 8, 1);
1030     PutUInt16(caretSlopeRise, ptr, 18, 1);
1031     PutUInt16(caretSlopeRun, ptr, 20, 1);
1032     PutUInt16(0, ptr, 22, 1);                     /* reserved 1 */
1033     PutUInt16(0, ptr, 24, 1);                     /* reserved 2 */
1034     PutUInt16(0, ptr, 26, 1);                     /* reserved 3 */
1035     PutUInt16(0, ptr, 28, 1);                     /* reserved 4 */
1036     PutUInt16(0, ptr, 30, 1);                     /* reserved 5 */
1037     PutUInt16(0, ptr, 32, 1);                     /* metricDataFormat */
1038 
1039     table->data = (void *) ptr;
1040     table->tag = T_hhea;
1041     table->rawdata = 0;
1042 
1043     return table;
1044 }
1045 
1046 TrueTypeTable *TrueTypeTableNew_loca(void)
1047 {
1048     TrueTypeTable* table = (TrueTypeTable*)smalloc(sizeof(TrueTypeTable));
1049     table->data = smalloc(sizeof(tdata_loca));
1050 
1051     ((tdata_loca *)table->data)->nbytes = 0;
1052     ((tdata_loca *)table->data)->ptr = 0;
1053 
1054     table->tag = T_loca;
1055     table->rawdata = 0;
1056 
1057     return table;
1058 }
1059 
1060 TrueTypeTable *TrueTypeTableNew_maxp( const sal_uInt8* maxp, int size)
1061 {
1062     TrueTypeTable* table = (TrueTypeTable*)smalloc(sizeof(TrueTypeTable));
1063     table->data = ttmalloc(TABLESIZE_maxp);
1064 
1065     if (maxp && size == TABLESIZE_maxp) {
1066         memcpy(table->data, maxp, TABLESIZE_maxp);
1067     }
1068 
1069     table->tag = T_maxp;
1070     table->rawdata = 0;
1071 
1072     return table;
1073 }
1074 
1075 TrueTypeTable *TrueTypeTableNew_glyf(void)
1076 {
1077     TrueTypeTable* table = (TrueTypeTable*)smalloc(sizeof(TrueTypeTable));
1078     list l = listNewEmpty();
1079 
1080     assert(l != 0);
1081 
1082     listSetElementDtor(l, (list_destructor)FreeGlyphData);
1083 
1084     table->data = l;
1085     table->rawdata = 0;
1086     table->tag = T_glyf;
1087 
1088     return table;
1089 }
1090 
1091 TrueTypeTable *TrueTypeTableNew_cmap(void)
1092 {
1093     TrueTypeTable* table = (TrueTypeTable*)smalloc(sizeof(TrueTypeTable));
1094     table_cmap* cmap = (table_cmap*)smalloc(sizeof(table_cmap));
1095 
1096     cmap->n = 0;
1097     cmap->m = CMAP_SUBTABLE_INIT;
1098     cmap->s = (CmapSubTable *) scalloc(CMAP_SUBTABLE_INIT, sizeof(CmapSubTable));
1099     memset(cmap->s, 0, sizeof(CmapSubTable) * CMAP_SUBTABLE_INIT);
1100 
1101     table->data = (table_cmap *) cmap;
1102 
1103     table->rawdata = 0;
1104     table->tag = T_cmap;
1105 
1106     return table;
1107 }
1108 
1109 static void DisposeNameRecord(void *ptr)
1110 {
1111     if (ptr != 0) {
1112         NameRecord *nr = (NameRecord *) ptr;
1113         if (nr->sptr) free(nr->sptr);
1114         free(ptr);
1115     }
1116 }
1117 
1118 static NameRecord* NameRecordNewCopy(NameRecord *nr)
1119 {
1120     NameRecord* p = (NameRecord*)smalloc(sizeof(NameRecord));
1121 
1122     memcpy(p, nr, sizeof(NameRecord));
1123 
1124     if (p->slen) {
1125         p->sptr = (sal_uInt8*)smalloc(p->slen);
1126         memcpy(p->sptr, nr->sptr, p->slen);
1127     }
1128 
1129     return p;
1130 }
1131 
1132 TrueTypeTable *TrueTypeTableNew_name(int n, NameRecord *nr)
1133 {
1134     TrueTypeTable* table = (TrueTypeTable*)smalloc(sizeof(TrueTypeTable));
1135     list l = listNewEmpty();
1136 
1137     assert(l != 0);
1138 
1139     listSetElementDtor(l, (list_destructor)DisposeNameRecord);
1140 
1141     if (n != 0) {
1142         int i;
1143         for (i = 0; i < n; i++) {
1144             listAppend(l, NameRecordNewCopy(nr+i));
1145         }
1146     }
1147 
1148     table->data = l;
1149     table->rawdata = 0;
1150     table->tag = T_name;
1151 
1152     return table;
1153 }
1154 
1155 TrueTypeTable *TrueTypeTableNew_post(sal_uInt32 format,
1156                                      sal_uInt32 italicAngle,
1157                                      sal_Int16 underlinePosition,
1158                                      sal_Int16 underlineThickness,
1159                                      sal_uInt32 isFixedPitch)
1160 {
1161     assert(format == 0x00030000);                 /* Only format 3.0 is supported at this time */
1162     TrueTypeTable* table = (TrueTypeTable*)smalloc(sizeof(TrueTypeTable));
1163     tdata_post* post = (tdata_post*)smalloc(sizeof(tdata_post));
1164 
1165     post->format = format;
1166     post->italicAngle = italicAngle;
1167     post->underlinePosition = underlinePosition;
1168     post->underlineThickness = underlineThickness;
1169     post->isFixedPitch = isFixedPitch;
1170     post->ptr = 0;
1171 
1172     table->data = post;
1173     table->rawdata = 0;
1174     table->tag = T_post;
1175 
1176     return table;
1177 }
1178 
1179 int GetRawData(TrueTypeTable *_this, sal_uInt8 **ptr, sal_uInt32 *len, sal_uInt32 *tag)
1180 {
1181     /* XXX do a binary search */
1182     unsigned int i;
1183 
1184     assert(_this != 0);
1185     assert(ptr != 0);
1186     assert(len != 0);
1187     assert(tag != 0);
1188 
1189     *ptr = 0; *len = 0; *tag = 0;
1190 
1191     if (_this->rawdata) {
1192         free(_this->rawdata);
1193         _this->rawdata = 0;
1194     }
1195 
1196     for(i=0; i < sizeof(vtable2)/sizeof(*vtable2); i++) {
1197         if (_this->tag == vtable2[i].tag) {
1198             return vtable2[i].f(_this, ptr, len, tag);
1199         }
1200     }
1201 
1202     assert(!"Unknwon TrueType table.\n");
1203     return TTCR_UNKNOWN;
1204 }
1205 
1206 void cmapAdd(TrueTypeTable *table, sal_uInt32 id, sal_uInt32 c, sal_uInt32 g)
1207 {
1208     sal_uInt32 i, found;
1209     table_cmap *t;
1210     CmapSubTable *s;
1211 
1212     assert(table != 0);
1213     assert(table->tag == T_cmap);
1214     t = (table_cmap *) table->data; assert(t != 0);
1215     s = t->s; assert(s != 0);
1216 
1217     found = 0;
1218 
1219     for (i = 0; i < t->n; i++) {
1220         if (s[i].id == id) {
1221             found = 1;
1222             break;
1223         }
1224     }
1225 
1226     if (!found) {
1227         if (t->n == t->m) {
1228             CmapSubTable* tmp = (CmapSubTable*)scalloc(t->m + CMAP_SUBTABLE_INCR, sizeof(CmapSubTable));
1229             memset(tmp, 0, t->m + CMAP_SUBTABLE_INCR * sizeof(CmapSubTable));
1230             memcpy(tmp, s, sizeof(CmapSubTable) * t->m);
1231             t->m += CMAP_SUBTABLE_INCR;
1232             free(s);
1233             s = tmp;
1234             t->s = s;
1235         }
1236 
1237         for (i = 0; i < t->n; i++) {
1238             if (s[i].id > id) break;
1239         }
1240 
1241         if (i < t->n) {
1242             memmove(s+i+1, s+i, t->n-i);
1243         }
1244 
1245         t->n++;
1246 
1247         s[i].id = id;
1248         s[i].n = 0;
1249         s[i].m = CMAP_PAIR_INIT;
1250         s[i].xc = (sal_uInt32*)scalloc(CMAP_PAIR_INIT, sizeof(sal_uInt32));
1251         s[i].xg = (sal_uInt32*)scalloc(CMAP_PAIR_INIT, sizeof(sal_uInt32));
1252     }
1253 
1254     if (s[i].n == s[i].m) {
1255         sal_uInt32* tmp1 = (sal_uInt32*)scalloc(s[i].m + CMAP_PAIR_INCR, sizeof(sal_uInt32));
1256         sal_uInt32* tmp2 = (sal_uInt32*)scalloc(s[i].m + CMAP_PAIR_INCR, sizeof(sal_uInt32));
1257         assert(tmp1 != 0);
1258         assert(tmp2 != 0);
1259         memcpy(tmp1, s[i].xc, sizeof(sal_uInt32) * s[i].m);
1260         memcpy(tmp2, s[i].xg, sizeof(sal_uInt32) * s[i].m);
1261         s[i].m += CMAP_PAIR_INCR;
1262         free(s[i].xc);
1263         free(s[i].xg);
1264         s[i].xc = tmp1;
1265         s[i].xg = tmp2;
1266     }
1267 
1268     s[i].xc[s[i].n] = c;
1269     s[i].xg[s[i].n] = g;
1270     s[i].n++;
1271 }
1272 
1273 sal_uInt32 glyfAdd(TrueTypeTable *table, GlyphData *glyphdata, TrueTypeFont *fnt)
1274 {
1275     list l;
1276     sal_uInt32 currentID;
1277     int ret, n, ncomponents;
1278     GlyphData *gd;
1279 
1280     assert(table != 0);
1281     assert(table->tag == T_glyf);
1282 
1283     if (!glyphdata) return (sal_uInt32)~0;
1284 
1285     std::vector< sal_uInt32 > glyphlist;
1286 
1287     ncomponents = GetTTGlyphComponents(fnt, glyphdata->glyphID, glyphlist);
1288 
1289     l = (list) table->data;
1290     if (listCount(l) > 0) {
1291         listToLast(l);
1292         ret = n = ((GlyphData *) listCurrent(l))->newID + 1;
1293     } else {
1294         ret = n = 0;
1295     }
1296     glyphdata->newID = n++;
1297     listAppend(l, glyphdata);
1298 
1299     if (ncomponents > 1 && glyphlist.size() > 1 )
1300     {
1301         std::vector< sal_uInt32 >::const_iterator it = glyphlist.begin();
1302         ++it;
1303         /* glyphData->glyphID is always the first glyph on the list */
1304         do
1305         {
1306             int found = 0;
1307             currentID = *it;
1308             /* XXX expensive! should be rewritten with sorted arrays! */
1309             listToFirst(l);
1310             do {
1311                 if (((GlyphData *) listCurrent(l))->glyphID == currentID) {
1312                     found = 1;
1313                     break;
1314                 }
1315             } while (listNext(l));
1316 
1317             if (!found) {
1318                 gd = GetTTRawGlyphData(fnt, currentID);
1319                 gd->newID = n++;
1320                 listAppend(l, gd);
1321             }
1322         } while( ++it !=  glyphlist.end() );
1323     }
1324 
1325     return ret;
1326 }
1327 
1328 sal_uInt32 glyfCount(const TrueTypeTable *table)
1329 {
1330     assert(table != 0);
1331     assert(table->tag == T_glyf);
1332     return listCount((list) table->data);
1333 }
1334 
1335 
1336 void nameAdd(TrueTypeTable *table, NameRecord *nr)
1337 {
1338     list l;
1339 
1340     assert(table != 0);
1341     assert(table->tag == T_name);
1342 
1343     l = (list) table->data;
1344 
1345     listAppend(l, NameRecordNewCopy(nr));
1346 }
1347 
1348 static TrueTypeTable *FindTable(TrueTypeCreator *tt, sal_uInt32 tag)
1349 {
1350     if (listIsEmpty(tt->tables)) return 0;
1351 
1352     listToFirst(tt->tables);
1353 
1354     do {
1355         if (((TrueTypeTable *) listCurrent(tt->tables))->tag == tag) {
1356             return (TrueTypeTable*)listCurrent(tt->tables);
1357         }
1358     } while (listNext(tt->tables));
1359 
1360     return 0;
1361 }
1362 
1363 /* This function processes all the tables and synchronizes them before creating
1364  * the output TrueType stream.
1365  *
1366  * *** It adds two TrueType tables to the font: 'loca' and 'hmtx' ***
1367  *
1368  * It does:
1369  *
1370  * - Re-numbers glyph IDs and creates 'glyf', 'loca', and 'hmtx' tables.
1371  * - Calculates xMin, yMin, xMax, and yMax and stores values in 'head' table.
1372  * - Stores indexToLocFormat in 'head'
1373  * - updates 'maxp' table
1374  * - Calculates advanceWidthMax, minLSB, minRSB, xMaxExtent and numberOfHMetrics
1375  *   in 'hhea' table
1376  *
1377  */
1378 static void ProcessTables(TrueTypeCreator *tt)
1379 {
1380     TrueTypeTable *glyf, *loca, *head, *maxp, *hhea;
1381     list glyphlist;
1382     sal_uInt32 nGlyphs, locaLen = 0, glyfLen = 0;
1383     sal_Int16 xMin = 0, yMin = 0, xMax = 0, yMax = 0;
1384     sal_uInt32 i = 0;
1385     sal_Int16 indexToLocFormat;
1386     sal_uInt8 *hmtxPtr, *hheaPtr;
1387     sal_uInt32 hmtxSize;
1388     sal_uInt8 *p1, *p2;
1389     sal_uInt16 maxPoints = 0, maxContours = 0, maxCompositePoints = 0, maxCompositeContours = 0;
1390     int nlsb = 0;
1391     sal_uInt32 *gid;                        /* array of old glyphIDs */
1392 
1393     glyf = FindTable(tt, T_glyf);
1394     glyphlist = (list) glyf->data;
1395     nGlyphs = listCount(glyphlist);
1396     assert(nGlyphs != 0);
1397     gid = (sal_uInt32*)scalloc(nGlyphs, sizeof(sal_uInt32));
1398 
1399     RemoveTable(tt, T_loca);
1400     RemoveTable(tt, T_hmtx);
1401 
1402     /* XXX Need to make sure that composite glyphs do not break during glyph renumbering */
1403 
1404     listToFirst(glyphlist);
1405     do {
1406         GlyphData *gd = (GlyphData *) listCurrent(glyphlist);
1407         sal_Int16 z;
1408         glyfLen += gd->nbytes;
1409         /* XXX if (gd->nbytes & 1) glyfLen++; */
1410 
1411 
1412         assert(gd->newID == i);
1413         gid[i++] = gd->glyphID;
1414         /* gd->glyphID = i++; */
1415 
1416         /* printf("IDs: %d %d.\n", gd->glyphID, gd->newID); */
1417 
1418         if (gd->nbytes != 0) {
1419             z = GetInt16(gd->ptr, 2, 1);
1420             if (z < xMin) xMin = z;
1421 
1422             z = GetInt16(gd->ptr, 4, 1);
1423             if (z < yMin) yMin = z;
1424 
1425             z = GetInt16(gd->ptr, 6, 1);
1426             if (z > xMax) xMax = z;
1427 
1428             z = GetInt16(gd->ptr, 8, 1);
1429             if (z > yMax) yMax = z;
1430         }
1431 
1432         if (gd->compflag == 0) {                            /* non-composite glyph */
1433             if (gd->npoints > maxPoints) maxPoints = gd->npoints;
1434             if (gd->ncontours > maxContours) maxContours = gd->ncontours;
1435         } else {                                            /* composite glyph */
1436             if (gd->npoints > maxCompositePoints) maxCompositePoints = gd->npoints;
1437             if (gd->ncontours > maxCompositeContours) maxCompositeContours = gd->ncontours;
1438         }
1439 
1440     } while (listNext(glyphlist));
1441 
1442     indexToLocFormat = (glyfLen / 2 > 0xFFFF) ? 1 : 0;
1443     locaLen = indexToLocFormat ?  (nGlyphs + 1) << 2 : (nGlyphs + 1) << 1;
1444 
1445     sal_uInt8* glyfPtr = ttmalloc(glyfLen);
1446     sal_uInt8* locaPtr = ttmalloc(locaLen);
1447     TTSimpleGlyphMetrics* met = (TTSimpleGlyphMetrics*)scalloc(nGlyphs, sizeof(TTSimpleGlyphMetrics));
1448     i = 0;
1449 
1450     listToFirst(glyphlist);
1451     p1 = glyfPtr;
1452     p2 = locaPtr;
1453     do {
1454         GlyphData *gd = (GlyphData *) listCurrent(glyphlist);
1455 
1456         if (gd->compflag) {                       /* re-number all components */
1457             sal_uInt16 flags, index;
1458             sal_uInt8 *ptr = gd->ptr + 10;
1459             do {
1460                 sal_uInt32 j;
1461                 flags = GetUInt16(ptr, 0, 1);
1462                 index = GetUInt16(ptr, 2, 1);
1463                 /* XXX use the sorted array of old to new glyphID mapping and do a binary search */
1464                 for (j = 0; j < nGlyphs; j++) {
1465                     if (gid[j] == index) {
1466                         break;
1467                     }
1468                 }
1469                 /* printf("X: %d -> %d.\n", index, j); */
1470 
1471                 PutUInt16((sal_uInt16) j, ptr, 2, 1);
1472 
1473                 ptr += 4;
1474 
1475                 if (flags & ARG_1_AND_2_ARE_WORDS) {
1476                     ptr += 4;
1477                 } else {
1478                     ptr += 2;
1479                 }
1480 
1481                 if (flags & WE_HAVE_A_SCALE) {
1482                     ptr += 2;
1483                 } else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) {
1484                     ptr += 4;
1485                 } else if (flags & WE_HAVE_A_TWO_BY_TWO) {
1486                     ptr += 8;
1487                 }
1488             } while (flags & MORE_COMPONENTS);
1489         }
1490 
1491         if (gd->nbytes != 0) {
1492             memcpy(p1, gd->ptr, gd->nbytes);
1493         }
1494         if (indexToLocFormat == 1) {
1495             PutUInt32(p1 - glyfPtr, p2, 0, 1);
1496             p2 += 4;
1497         } else {
1498             PutUInt16((sal_uInt16)((p1 - glyfPtr) >> 1), p2, 0, 1);
1499             p2 += 2;
1500         }
1501         p1 += gd->nbytes;
1502 
1503         /* fill the array of metrics */
1504         met[i].adv = gd->aw;
1505         met[i].sb  = gd->lsb;
1506         i++;
1507     } while (listNext(glyphlist));
1508 
1509     free(gid);
1510 
1511     if (indexToLocFormat == 1) {
1512         PutUInt32(p1 - glyfPtr, p2, 0, 1);
1513     } else {
1514         PutUInt16((sal_uInt16)((p1 - glyfPtr) >> 1), p2, 0, 1);
1515     }
1516 
1517     glyf->rawdata = glyfPtr;
1518 
1519     loca = TrueTypeTableNew_loca(); assert(loca != 0);
1520     ((tdata_loca *) loca->data)->ptr = locaPtr;
1521     ((tdata_loca *) loca->data)->nbytes = locaLen;
1522 
1523     AddTable(tt, loca);
1524 
1525     head = FindTable(tt, T_head);
1526     sal_uInt8* const pHeadData = (sal_uInt8*)head->data;
1527     PutInt16(xMin, pHeadData, 36, 1);
1528     PutInt16(yMin, pHeadData, 38, 1);
1529     PutInt16(xMax, pHeadData, 40, 1);
1530     PutInt16(yMax, pHeadData, 42, 1);
1531     PutInt16(indexToLocFormat, pHeadData,  50, 1);
1532 
1533     maxp = FindTable(tt, T_maxp);
1534 
1535     sal_uInt8* const pMaxpData = (sal_uInt8*)maxp->data;
1536     PutUInt16((sal_uInt16)nGlyphs, pMaxpData, 4, 1);
1537     PutUInt16(maxPoints, pMaxpData, 6, 1);
1538     PutUInt16(maxContours, pMaxpData, 8, 1);
1539     PutUInt16(maxCompositePoints, pMaxpData, 10, 1);
1540     PutUInt16(maxCompositeContours, pMaxpData, 12, 1);
1541 
1542 #if 0
1543     /* XXX do not overwrite the existing data. Fix: re-calculate these numbers here */
1544     PutUInt16(2, maxp->data, 14, 1);                        /* maxZones is always 2       */
1545     PutUInt16(0, maxp->data, 16, 1);                        /* maxTwilightPoints          */
1546     PutUInt16(0, maxp->data, 18, 1);                        /* maxStorage                 */
1547     PutUInt16(0, maxp->data, 20, 1);                        /* maxFunctionDefs            */
1548     PutUint16(0, maxp->data, 22, 1);                        /* maxInstructionDefs         */
1549     PutUint16(0, maxp->data, 24, 1);                        /* maxStackElements           */
1550     PutUint16(0, maxp->data, 26, 1);                        /* maxSizeOfInstructions      */
1551     PutUint16(0, maxp->data, 28, 1);                        /* maxComponentElements       */
1552     PutUint16(0, maxp->data, 30, 1);                        /* maxComponentDepth          */
1553 #endif
1554 
1555     /*
1556      * Generate an htmx table and update hhea table
1557      */
1558     hhea = FindTable(tt, T_hhea); assert(hhea != 0);
1559     hheaPtr = (sal_uInt8 *) hhea->data;
1560     if (nGlyphs > 2) {
1561         for (i = nGlyphs - 1; i > 0; i--) {
1562             if (met[i].adv != met[i-1].adv) break;
1563         }
1564         nlsb = nGlyphs - 1 - i;
1565     }
1566     hmtxSize = (nGlyphs - nlsb) * 4 + nlsb * 2;
1567     hmtxPtr = ttmalloc(hmtxSize);
1568     p1 = hmtxPtr;
1569 
1570     for (i = 0; i < nGlyphs; i++) {
1571         if (i < nGlyphs - nlsb) {
1572             PutUInt16(met[i].adv, p1, 0, 1);
1573             PutUInt16(met[i].sb, p1, 2, 1);
1574             p1 += 4;
1575         } else {
1576             PutUInt16(met[i].sb, p1, 0, 1);
1577             p1 += 2;
1578         }
1579     }
1580 
1581     AddTable(tt, TrueTypeTableNew(T_hmtx, hmtxSize, hmtxPtr));
1582     PutUInt16((sal_uInt16)(nGlyphs - nlsb), hheaPtr, 34, 1);
1583     free(hmtxPtr);
1584     free(met);
1585 }
1586 
1587 } // namespace vcl
1588 
1589 extern "C"
1590 {
1591     /**
1592      * TrueTypeCreator destructor. It calls destructors for all TrueTypeTables added to it.
1593      */
1594      void TrueTypeCreatorDispose(vcl::TrueTypeCreator *_this)
1595     {
1596         listDispose(_this->tables);
1597         free(_this);
1598     }
1599 
1600 
1601     /**
1602      * Destructor for the TrueTypeTable object.
1603      */
1604      void TrueTypeTableDispose(vcl::TrueTypeTable *_this)
1605     {
1606         /* XXX do a binary search */
1607         unsigned int i;
1608 
1609         assert(_this != 0);
1610 
1611         if (_this->rawdata) free(_this->rawdata);
1612 
1613         for(i=0; i < sizeof(vcl::vtable1)/sizeof(*vcl::vtable1); i++) {
1614             if (_this->tag == vcl::vtable1[i].tag) {
1615                 vcl::vtable1[i].f(_this);
1616                 return;
1617             }
1618         }
1619         assert(!"Unknown TrueType table.\n");
1620     }
1621 }
1622 
1623 
1624 #ifdef TEST_TTCR
1625 int main(void)
1626 {
1627     TrueTypeCreator *ttcr;
1628     sal_uInt8 *t1, *t2, *t3, *t4, *t5, *t6, *t7;
1629 
1630     TrueTypeCreatorNewEmpty(mkTag('t','r','u','e'), &ttcr);
1631 
1632     t1 = malloc(1000); memset(t1, 'a', 1000);
1633     t2 = malloc(2000); memset(t2, 'b', 2000);
1634     t3 = malloc(3000); memset(t3, 'c', 3000);
1635     t4 = malloc(4000); memset(t4, 'd', 4000);
1636     t5 = malloc(5000); memset(t5, 'e', 5000);
1637     t6 = malloc(6000); memset(t6, 'f', 6000);
1638     t7 = malloc(7000); memset(t7, 'g', 7000);
1639 
1640     AddTable(ttcr, TrueTypeTableNew(0x6D617870, 1000, t1));
1641     AddTable(ttcr, TrueTypeTableNew(0x4F532F32, 2000, t2));
1642     AddTable(ttcr, TrueTypeTableNew(0x636D6170, 3000, t3));
1643     AddTable(ttcr, TrueTypeTableNew(0x6C6F6361, 4000, t4));
1644     AddTable(ttcr, TrueTypeTableNew(0x68686561, 5000, t5));
1645     AddTable(ttcr, TrueTypeTableNew(0x676C7966, 6000, t6));
1646     AddTable(ttcr, TrueTypeTableNew(0x6B65726E, 7000, t7));
1647 
1648     free(t1);
1649     free(t2);
1650     free(t3);
1651     free(t4);
1652     free(t5);
1653     free(t6);
1654     free(t7);
1655 
1656 
1657     StreamToFile(ttcr, "ttcrout.ttf");
1658 
1659     TrueTypeCreatorDispose(ttcr);
1660     return 0;
1661 }
1662 #endif
1663