Debian Codegraph @

texk/ttfdump/libttf/cmap.c

raw
1 /* cmap.c -- Load and Print the content of cmap table
2 * Copyright (C) 1996 Li-Da Lho, All right reserved
3 *
4 * The structure of a cmap is really complex, it depends on which format the
5 * cmap is and loading a cmap is a kind of confusing thing.
6 * The structure of CMAP:
7 * A cmap begins with a general description of the table
8 * USHORT version indicates the version of this cmap.
9 * USHORT numEncodings is the number of encodings in the cmap table.
10 * Then follow 'numEncodings' of encoding tables, each of which contains
11 * the following informations for each subtable. The subtable contains the
12 * true data to map character code to glyph index
13 * USHORT PlatformID shows which platform the subtable is to be used
14 * (e.g. Mac, PC)
15 * USHORT Platfrom specific EncodingID shows what kind of encoding scheme
16 * this table uses (e.g. Big5, iso8859, Unicode etc.)
17 * ULONG offset is the offset from the beginning of the whole cmap table
18 * to the beginning of the corresponding subtable
19 * ----------------------- --- ---
20 * | USHORT version | ^ ^
21 * ----------------------- | |
22 * | USHORT numEncodings | | |
23 * ----------------------- | |
24 * | Encoding Table 1 | | |
25 * | USHORT PlatformID | | |
26 * | USHORT EncodingID | | |
27 * | ULONG offset 1 |------ | |
28 * ----------------------- | | offset 1 | offset 2
29 * | Encoding Table 2 | | | |
30 * | USHORT PlatformID | | | |
31 * | USHORT EncodingID | | | |
32 * | ULONG offset 2 |--- | v |
33 * ----------------------- | | --- |
34 * | Subtable 1 |<----- |
35 * | with format 0 | | v
36 * ----------------------- | ---
37 * | Subtable 2 |<--
38 * | with format 2 |
39 * ----------------------|
40 *
41 * Problem with Character Code vs. Byte Endianess.
42 *
43 */
44 #ifdef HAVE_CONFIG_H
45 #include <config.h>
46 #endif
47
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include "ttf.h"
51 #include "ttfutil.h"
52
53 /* $Id: cmap.c,v 1.1.1.1 1998/06/05 07:47:52 robert Exp $ */
54
55 typedef union _TwoBytes {
56 USHORT u;
57 unsigned char c[2];
58 } TwoBytes;
59
60 static void ttfLoadCMAP(FILE *fp, CMAPPtr cmap, ULONG offset);
61
62 static void ttfLoadSubTable(FILE *fp, SubTablePtr subTable, ULONG offset);
63 static void ttfPrintSubTable(FILE* fp, SubTablePtr subTable);
64 static void ttfFreeSubTable(SubTablePtr subTable);
65
66 static void ttfLoadCMAP0(FILE *fp, MapPtr *map);
67 static void ttfPrintCMAP0(FILE *fp, MapPtr map);
68 static USHORT ttfLookUpCMAP0(MapPtr map, USHORT cc);
69 static void ttfFreeCMAP0(MapPtr map);
70
71 static void ttfLoadCMAP2(FILE *fp, MapPtr *map);
72 static void ttfPrintCMAP2(FILE *fp, MapPtr map);
73 static USHORT ttfLookUpCMAP2(MapPtr map, USHORT cc);
74 static void ttfFreeCMAP2(MapPtr map);
75
76 static void ttfLoadCMAP4(FILE *fp, MapPtr *map);
77 static void ttfPrintCMAP4(FILE *fp, MapPtr map);
78 static USHORT ttfLookUpCMAP4(MapPtr map, USHORT cc);
79 static void ttfFreeCMAP4(MapPtr map);
80
81 static void ttfLoadCMAP6(FILE *fp, MapPtr *map);
82 static void ttfPrintCMAP6(FILE *fp, MapPtr map);
83 static USHORT ttfLookUpCMAP6(MapPtr map, USHORT cc);
84 static void ttfFreeCMAP6(MapPtr map);
85
86 static void ttfLoadCMAP8(FILE *fp, MapPtr *map);
87 static void ttfPrintCMAP8(FILE *fp, MapPtr map);
88 static USHORT ttfLookUpCMAP8(MapPtr map, ULONG cc);
89 static void ttfFreeCMAP8(MapPtr map);
90
91 static void ttfLoadCMAP10(FILE *fp, MapPtr *map);
92 static void ttfPrintCMAP10(FILE *fp, MapPtr map);
93 static USHORT ttfLookUpCMAP10(MapPtr map, ULONG cc);
94 static void ttfFreeCMAP10(MapPtr map);
95
96 static void ttfLoadCMAP12(FILE *fp, MapPtr *map);
97 static void ttfPrintCMAP12(FILE *fp, MapPtr map);
98 static USHORT ttfLookUpCMAP12(MapPtr map, ULONG cc);
99 static void ttfFreeCMAP12(MapPtr map);
100
101 static void ttfLoadCMAP13(FILE *fp, MapPtr *map);
102 static void ttfPrintCMAP13(FILE *fp, MapPtr map);
103 static USHORT ttfLookUpCMAP13(MapPtr map, ULONG cc);
104 static void ttfFreeCMAP13(MapPtr map);
105
106 static void ttfLoadCMAP14(FILE *fp, MapPtr *map);
107 static void ttfPrintCMAP14(FILE *fp, MapPtr map);
108 static USHORT ttfLookUpCMAP14(MapPtr map, ULONG cc);
109 static void ttfFreeCMAP14(MapPtr map);
110
111 void ttfInitCMAP(TTFontPtr font)
112 {
113 ULONG tag = FT_MAKE_TAG ('c', 'm', 'a', 'p');
114 TableDirPtr ptd;
115
116 if ((ptd = ttfLookUpTableDir(tag,font)) != NULL)
117 {
118 font->cmap = XCALLOC1 (CMAP);
119 ttfLoadCMAP(font->fp,font->cmap,ptd->offset);
120 }
121 }
122
123 static void ttfLoadCMAP(FILE *fp,CMAPPtr cmap,ULONG offset)
124 {
125 USHORT i,n;
126 int k = 0;
127
128 xfseek(fp, offset, SEEK_SET, "ttfLoadCMAP");
129
130 cmap->version = ttfGetUSHORT(fp);
131 cmap->numberOfEncodings = n = ttfGetUSHORT(fp);
132 cmap->encoding = XCALLOC (cmap->numberOfEncodings, Encoding);
133
134 for (i = 0; i < n; i++)
135 {
136 int j;
137
138 cmap->encoding[i].PlatformID = ttfGetUSHORT(fp);
139 cmap->encoding[i].EncodingID = ttfGetUSHORT(fp);
140 cmap->encoding[i].offset = ttfGetULONG(fp);
141 cmap->encoding[i].mapindex = k++;
142 for (j = 0; j < i; j++) {
143 if (cmap->encoding[i].offset == cmap->encoding[j].offset)
144 {
145 cmap->encoding[i].mapindex = cmap->encoding[j].mapindex;
146 k--;
147 break;
148
149 }
150 }
151 }
152 cmap->numberOfMaps = k;
153 cmap->subTable = XCALLOC (cmap->numberOfMaps, SubTable);
154 for (i = 0; i < n; i++)
155 cmap->subTable[cmap->encoding[i].mapindex].offset = cmap->encoding[i].offset;
156 for (i = 0; i < k; i++)
157 ttfLoadSubTable(fp, cmap->subTable+i, offset);
158 }
159 void ttfPrintCMAP(FILE *fp,CMAPPtr cmap)
160 {
161 USHORT i;
162
163 fprintf(fp,"'cmap' Table - Character to Glyph Index Mapping Table\n");
164 fprintf(fp,"-----------------------------------------------------\n");
165 fprintf(fp,"\t 'cmap' version: %d\n", cmap->version);
166 fprintf(fp,"\t number of encodings: %d\n", cmap->numberOfEncodings);
167 fprintf(fp,"\t number of subtables: %d\n\n", cmap->numberOfMaps);
168
169 for (i = 0; i < cmap->numberOfEncodings; i++)
170 {
171 fprintf(fp, "Encoding %3d.\t PlatformID: %2d\n", i,
172 cmap->encoding[i].PlatformID);
173 fprintf(fp, "\t\t EcodingID: %2d\n",
174 cmap->encoding[i].EncodingID);
175 fprintf(fp, "\t\t SubTable: %d, Offset: 0x%08x\n\n",
176 cmap->encoding[i].mapindex, cmap->encoding[i].offset);
177 }
178 for (i = 0; i < cmap->numberOfMaps; i++)
179 {
180 fprintf(fp,"SubTable %3d.\t",i);
181 ttfPrintSubTable(fp, cmap->subTable+i);
182 fprintf(fp,"\n");
183 }
184 }
185 USHORT ttfLookUpCMAP(MapPtr map, USHORT cc)
186 {
187 USHORT idx,format = *map.format;
188
189 switch (format)
190 {
191 case 0:
192 idx = ttfLookUpCMAP0(map, cc);
193 break;
194 case 2:
195 idx = ttfLookUpCMAP2(map, cc);
196 break;
197 case 4:
198 idx = ttfLookUpCMAP4(map, cc);
199 break;
200 case 6:
201 idx = ttfLookUpCMAP6(map, cc);
202 break;
203 case 8:
204 idx = ttfLookUpCMAP8(map, cc);
205 break;
206 case 10:
207 idx = ttfLookUpCMAP10(map, cc);
208 break;
209 case 12:
210 idx = ttfLookUpCMAP12(map, cc);
211 break;
212 case 13:
213 idx = ttfLookUpCMAP13(map, cc);
214 break;
215 case 14:
216 idx = ttfLookUpCMAP14(map, cc);
217 break;
218 default:
219 ttfError("Unrecognized CMAP format\n");
220 return 0;
221 }
222 return idx;
223 }
224 void ttfFreeCMAP(CMAPPtr cmap)
225 {
226 USHORT i;
227
228 if (!cmap)
229 return;
230
231 free(cmap->encoding);
232
233 for (i = 0; i < cmap->numberOfMaps; i++)
234 ttfFreeSubTable(cmap->subTable+i);
235 free(cmap->subTable);
236
237 free(cmap);
238 }
239
240 static void ttfLoadSubTable(FILE *fp, SubTablePtr subTable, ULONG offset)
241 {
242 ULONG pos;
243 USHORT format;
244
245 /* seek to the actuall position for this subtable
246 * base: beginning of cmap
247 * offset: offset field of each encoding table */
248 pos = offset + subTable->offset;
249 xfseek(fp, pos, SEEK_SET, "ttfLoadEncoding");
250
251 format = ttfGetUSHORT(fp);
252
253 switch(format)
254 {
255 case 0:
256 ttfLoadCMAP0(fp, &subTable->map);
257 break;
258 case 2:
259 ttfLoadCMAP2(fp, &subTable->map);
260 break;
261 case 4:
262 ttfLoadCMAP4(fp, &subTable->map);
263 break;
264 case 6:
265 ttfLoadCMAP6(fp, &subTable->map);
266 break;
267 case 8:
268 ttfLoadCMAP8(fp, &subTable->map);
269 break;
270 case 10:
271 ttfLoadCMAP10(fp, &subTable->map);
272 break;
273 case 12:
274 ttfLoadCMAP12(fp, &subTable->map);
275 break;
276 case 13:
277 ttfLoadCMAP13(fp, &subTable->map);
278 break;
279 case 14:
280 ttfLoadCMAP14(fp, &subTable->map);
281 break;
282 default:
283 ttfError("Unrecognized CMAP format\n");
284 }
285
286 *subTable->map.format = format;
287 }
288 static void ttfPrintSubTable(FILE* fp, SubTablePtr subTable)
289 {
290 USHORT format = *subTable->map.format;
291
292 switch(format)
293 {
294 case 0:
295 ttfPrintCMAP0(fp, subTable->map);
296 break;
297 case 2:
298 ttfPrintCMAP2(fp, subTable->map);
299 break;
300 case 4:
301 ttfPrintCMAP4(fp, subTable->map);
302 break;
303 case 6:
304 ttfPrintCMAP6(fp, subTable->map);
305 break;
306 case 8:
307 ttfPrintCMAP8(fp, subTable->map);
308 break;
309 case 10:
310 ttfPrintCMAP10(fp, subTable->map);
311 break;
312 case 12:
313 ttfPrintCMAP12(fp, subTable->map);
314 break;
315 case 13:
316 ttfPrintCMAP13(fp, subTable->map);
317 break;
318 case 14:
319 ttfPrintCMAP14(fp, subTable->map);
320 break;
321 default:
322 ttfError("Unrecognized CMAP format\n");
323 }
324 }
325 static void ttfFreeSubTable(SubTablePtr subTable)
326 {
327 USHORT format = *subTable->map.format;
328
329 switch(format)
330 {
331 case 0:
332 ttfFreeCMAP0(subTable->map);
333 break;
334 case 2:
335 ttfFreeCMAP2(subTable->map);
336 break;
337 case 4:
338 ttfFreeCMAP4(subTable->map);
339 break;
340 case 6:
341 ttfFreeCMAP6(subTable->map);
342 break;
343 case 8:
344 ttfFreeCMAP8(subTable->map);
345 break;
346 case 10:
347 ttfFreeCMAP10(subTable->map);
348 break;
349 case 12:
350 ttfFreeCMAP12(subTable->map);
351 break;
352 case 13:
353 ttfFreeCMAP13(subTable->map);
354 break;
355 case 14:
356 ttfFreeCMAP14(subTable->map);
357 break;
358 }
359 }
360
361 static void ttfLoadCMAP0(FILE *fp, MapPtr *map)
362 {
363 BYTE * array;
364
365 map->cmap0 = XCALLOC1 (CMAP0);
366 map->cmap0->length = ttfGetUSHORT(fp);
367 map->cmap0->version = ttfGetUSHORT(fp);
368 array = map->cmap0->glyphIndexArray;
369
370 /* Attention: we get lots of bytes at once as a work around of the
371 * usual ttfGet*, this cause byte sex trouble as will be seen
372 * in the following procedures */
373 if (fread(array,sizeof(BYTE),256,fp) != 256)
374 ttfError("Error when getting glyphIndexArray\n");
375 }
376 static void ttfPrintCMAP0(FILE *fp, MapPtr map)
377 {
378 USHORT index;
379 int i;
380
381 fprintf(fp, " Format 0 - Byte encoding table\n");
382 fprintf(fp,"\t\t Length: %6d\n", map.cmap0->length);
383 fprintf(fp,"\t\t Version: %6d\n", map.cmap0->version);
384 for (i=0;i<256;i++)
385 {
386 index = ttfLookUpCMAP(map, i);
387 fprintf(fp,"\t\t Char %3d -> Index %4d\n",i,index);
388 }
389 }
390 static USHORT ttfLookUpCMAP0(MapPtr map, USHORT cc)
391 {
392 return map.cmap0->glyphIndexArray[cc & 0x00ff];
393 }
394 static void ttfFreeCMAP0(MapPtr map)
395 {
396 free(map.cmap0);
397 }
398
399 static void ttfLoadCMAP2(FILE *fp, MapPtr *map)
400 {
401 USHORT * array,i,n = 0;
402 USHORT numGlyphId;
403 SubHeaderPtr header;
404
405 map->cmap2 = XCALLOC1 (CMAP2);
406 map->cmap2->length = ttfGetUSHORT(fp);
407 map->cmap2->version = ttfGetUSHORT(fp);
408 array = map->cmap2->subHeaderKeys;
409
410 ttfReadUSHORT (array, 256, fp);
411
412 for (i=0;i<256;i++)
413 {
414 array[i] /= 8;
415 if (n< array[i])
416 n = array[i]; /* find the max of subHeaderKeys */
417 }
418 n += 1; /* the number of subHeaders is one plus the max of subHeaderKeys */
419
420 map->cmap2->subHeaders = header = XCALLOC (n, SubHeader);
421 for (i=0;i<n;i++)
422 {
423 (header+i)->firstCode = ttfGetUSHORT(fp);
424 (header+i)->entryCount = ttfGetUSHORT(fp);
425 (header+i)->idDelta = ttfGetSHORT(fp);
426 (header+i)->idRangeOffset = ttfGetUSHORT(fp);
427
428 /* it makes things easier to let the offset starts from
429 * the beginning of glyphIndexArray */
430 if ((header+i)->idRangeOffset != 0)
431 (header+i)->idRangeOffset -= (sizeof(USHORT) +
432 (n-i-1) * sizeof(SubHeader)) ;
433 }
434
435 /* caculate the length of glyphIndexArray, this is ugly, there should be
436 * a better way to get this information. */
437 numGlyphId =
438 map->cmap2->length - (256 + 3) * sizeof(USHORT) - n * sizeof(SubHeader);
439 numGlyphId /= sizeof(USHORT);
440 map->cmap2->glyphIndexArray = XCALLOC (numGlyphId, USHORT);
441 for (i=0;i<numGlyphId;i++)
442 {
443 map->cmap2->glyphIndexArray[i] = ttfGetUSHORT(fp);
444 }
445 }
446 static void ttfPrintCMAP2(FILE *fp, MapPtr map)
447 {
448 USHORT i,j,numGlyphId;
449 USHORT *array,n=0,index;
450 SubHeaderPtr header;
451 TwoBytes tb;
452
453 fprintf(fp, " Format 2 - High-byte mapping through table\n");
454 fprintf(fp,"\t\t Length: %6d\n", map.cmap2->length);
455 fprintf(fp,"\t\t Version: %6d\n", map.cmap2->version);
456 array = map.cmap2->subHeaderKeys;
457 header = map.cmap2->subHeaders;
458
459 for (i=0;i<256;i++)
460 {
461 /* find the number of subHeader */
462 if (n< array[i])
463 n = array[i];
464 fprintf(fp,"\t\t subHeaderKeys[%d] = %d\n",i,array[i]);
465 }
466 n += 1; /* the number of subHeaders is one plus the max of subHeaderKeys */
467 fprintf(fp,"\t\t Number of SubHeaders is %d\n",n);
468
469 for (i=0;i<=n;i++)
470 {
471 fprintf(fp,"\t\t SubHeader[%d]\n",i);
472 fprintf(fp,"\t\t firstCode \t 0x%04x\n",(header+i)->firstCode);
473 fprintf(fp,"\t\t entryCount \t %d\n",(header+i)->entryCount);
474 fprintf(fp,"\t\t idDelta \t %d\n",(header+i)->idDelta);
475 fprintf(fp,"\t\t idRangeOffset \t 0x%04x\n\n",
476 (header+i)->idRangeOffset);
477 }
478
479 /* caculate the length of glyphIndexArray, this is ugly, there should be
480 * a better way to get this information. */
481 numGlyphId =
482 map.cmap2->length - (256 + 3) * sizeof(USHORT) - n * sizeof(SubHeader);
483 numGlyphId /= sizeof(USHORT);
484 fprintf(fp,"Number of glyphIndex: %d\n", numGlyphId);
485 for (i=0;i<numGlyphId;i++)
486 {
487 fprintf(fp,"\t\t glyphIdArray[%d] = %4d\n",i,
488 map.cmap2->glyphIndexArray[i]);
489 }
490
491 i = 0;
492 fprintf(fp,"\t\t First Byte:\t %2x\n",i);
493 tb.c[1] = i;
494 for(j=0;j<=255;j++)
495 {
496 tb.c[0] = j;
497 index = ttfLookUpCMAP2(map, tb.u);
498 fprintf(fp,"\t\t Char %2x -> Index %d\n",j,index);
499 }
500 for (i=128;i<=255;i++)
501 {
502 fprintf(fp,"\t\t First Byte:\t %2x\n",i);
503 tb.c[1] = i;
504 for(j=0;j<=255;j++)
505 {
506 tb.c[0] = j;
507 index = ttfLookUpCMAP2(map, tb.u);
508 fprintf(fp,"\t\t Char %2x -> Index %d\n",j,index);
509 }
510 }
511 }
512 static USHORT ttfLookUpCMAP2(MapPtr map, USHORT cc)
513 {
514 USHORT index,idx = 0;
515 USHORT *array = map.cmap2->subHeaderKeys;
516 SubHeaderPtr headers = map.cmap2->subHeaders;
517 SHORT idDelta;
518 USHORT firstCode, entryCount, idRangeOffset;
519 TwoBytes tb;
520 unsigned char first,second;
521
522
523 /* On little endian platforms "low byte" is the "first byte".
524 * It determines if it is necessary to use the second byte to
525 * fully interprete the character code; for example, if the first byte
526 * is obviously an ASCII character then it is not necessary to
527 * interpret the second byte, on the other hand, if the first byte is
528 * zero then the second byte is a ASCII char, when the first byte
529 * is not an ASCII char nor zero, those two bytes together determine
530 * the meanning of the character code
531 */
532 tb.u = cc;
533 first = tb.c[1];
534 second = tb.c[0];
535
536 /* select which subHeader to use */
537 idx = array[first];
538
539 firstCode = (headers+idx)->firstCode;
540 entryCount = (headers+idx)->entryCount;
541 idDelta = (headers+idx)->idDelta;
542 idRangeOffset = (headers+idx)->idRangeOffset / sizeof (USHORT);
543
544 if (second >= firstCode && second < firstCode+entryCount)
545 {
546 /* use idRangeOffset to find where in the glyphIndexArray
547 * the correspinding index is */
548 idRangeOffset += (second - firstCode);
549 index = map.cmap2->glyphIndexArray[idRangeOffset];
550 if (index != 0)
551 /* if the character is not a missing character then
552 * add idDelta to it */
553 index += idDelta;
554 }
555 else
556 /* The second code is out ranged then return the
557 * missing glyph character */
558 index = 0;
559
560 return index;
561 }
562 static void ttfFreeCMAP2(MapPtr map)
563 {
564 free(map.cmap2->subHeaders);
565 free(map.cmap2->glyphIndexArray);
566 free(map.cmap2);
567 }
568
569 static void ttfLoadCMAP4(FILE *fp, MapPtr *map)
570 {
571 USHORT segCount;
572 USHORT len;
573
574 map->cmap4 = XCALLOC1 (CMAP4);
575 map->cmap4->length = ttfGetUSHORT(fp);
576 map->cmap4->version = ttfGetUSHORT(fp);
577
578 map->cmap4->segCountX2 = segCount = ttfGetUSHORT(fp);
579 map->cmap4->searchRange = ttfGetUSHORT(fp);
580 map->cmap4->entrySelector = ttfGetUSHORT(fp);
581 map->cmap4->rangeShift = ttfGetUSHORT(fp);
582
583 segCount /= 2;
584 map->cmap4->endCount = ttfMakeUSHORT (segCount, fp);
585
586 map->cmap4->reservedPad = ttfGetUSHORT(fp);
587
588 map->cmap4->startCount = ttfMakeUSHORT (segCount, fp);
589
590 map->cmap4->idDelta = ttfMakeUSHORT (segCount, fp);
591
592 map->cmap4->idRangeOffset = ttfMakeUSHORT (segCount, fp);
593
594 /* caculate the length of glyphIndexArray, this is ugly, there should be
595 * a better way to get this information. */
596 len = map->cmap4->length - 8*sizeof(USHORT) - 4*segCount*sizeof(USHORT);
597 len /= sizeof(USHORT);
598 map->cmap4->glyphIndexArray = ttfMakeUSHORT (len, fp);
599 }
600 static void ttfPrintCMAP4(FILE *fp, MapPtr map)
601 {
602 USHORT i;
603 USHORT segCount,len;
604
605 fprintf(fp, " Format 4 - Segment mapping to delta values\n");
606 fprintf(fp,"\t\t Length: %6d\n", map.cmap4->length);
607 fprintf(fp,"\t\t Version: %6d\n", map.cmap4->version);
608 segCount = map.cmap4->segCountX2/2;
609 fprintf(fp, "\t\t segCount:\t %d\n", segCount);
610 fprintf(fp, "\t\t searchRange:\t %d\n", map.cmap4->searchRange);
611 fprintf(fp, "\t\t entrySelector:\t %d\n", map.cmap4->entrySelector);
612 fprintf(fp, "\t\t rangeShift:\t %d\n", map.cmap4->rangeShift);
613
614 for (i=0;i<segCount;i++)
615 {
616 fprintf(fp, "\t\t Seg %3d :", i);
617 fprintf(fp, " St = %04x,", map.cmap4->startCount[i]);
618 fprintf(fp, " En = %04x,", map.cmap4->endCount[i]);
619 /* should this filed be SHORT or USHORT ?? */
620 fprintf(fp, " D = %6d," ,
621 (map.cmap4->idDelta[i]));
622 fprintf(fp, " RO = %6d," , map.cmap4->idRangeOffset[i]);
623
624 /* find the glyphIndex correpsonding to this segment */
625 if (map.cmap4->idRangeOffset[i] != 0)
626 {
627 USHORT j;
628 j = map.cmap4->segCountX2/2 - i;
629 j = map.cmap4->idRangeOffset[i] -
630 j*sizeof(USHORT);
631 fprintf(fp, " gId# = %d\n", (int) (j/sizeof(USHORT)));
632 }
633 else
634 fprintf(fp, " gId# = N/A\n");
635 }
636
637 /* caculate the length of glyphIndexArray, this is ugly, there should be
638 * a better way to get this information. */
639 len = map.cmap4->length - 8*sizeof(USHORT) - 4*segCount*sizeof(USHORT);
640 len /= sizeof(USHORT);
641 fprintf(fp,"\t\t Number of glyphIndex %d\n",len);
642 for (i=0;i<len;i++)
643 {
644 fprintf(fp,"\t\t glyphIdArray[%d] = %d\n",i,
645 map.cmap4->glyphIndexArray[i]);
646 }
647
648 for (i=0;i<segCount;i++)
649 {
650 int j,index;
651 fprintf(fp,"Segment %d:\n",i);
652 for (j=map.cmap4->startCount[i];
653 j<=map.cmap4->endCount[i];j++)
654 {
655 index = ttfLookUpCMAP4(map, j);
656 fprintf(fp,"\t\tChar 0x%04x -> Index %d\n",j,index);
657 }
658 }
659 }
660 static USHORT ttfLookUpCMAP4(MapPtr map, USHORT cc)
661 {
662 USHORT i;
663 USHORT index=0, segCount = map.cmap4->segCountX2/2;
664
665 for (i=0;i<segCount;i++)
666 {
667 if (cc <= map.cmap4->endCount[i] &&
668 cc >= map.cmap4->startCount[i])
669 {
670 USHORT j;
671 if (map.cmap4->idRangeOffset[i] != 0)
672 {
673 j = map.cmap4->idRangeOffset[i] -
674 (segCount - i)*sizeof(USHORT);
675 j = cc - map.cmap4->startCount[i] + j/2;
676 index = map.cmap4->glyphIndexArray[j];
677 if (index != 0)
678 index += map.cmap4->idDelta[i];
679 }
680 else
681 {
682 index = cc + map.cmap4->idDelta[i];
683 }
684 break;
685 }
686 }
687
688 return index;
689 }
690 static void ttfFreeCMAP4(MapPtr map)
691 {
692 free(map.cmap4->endCount);
693 free(map.cmap4->startCount);
694 free(map.cmap4->idDelta);
695 free(map.cmap4->idRangeOffset);
696 free(map.cmap4->glyphIndexArray);
697 free(map.cmap4);
698 }
699
700 static void ttfLoadCMAP6(FILE *fp, MapPtr *map)
701 {
702 USHORT len;
703
704 map->cmap6 = XCALLOC1 (CMAP6);
705 map->cmap6->length = ttfGetUSHORT(fp);
706 map->cmap6->version = ttfGetUSHORT(fp);
707 map->cmap6->firstCode = ttfGetUSHORT(fp);
708 map->cmap6->entryCount = len = ttfGetUSHORT(fp);
709 map->cmap6->glyphIndexArray = ttfMakeUSHORT (len, fp);
710 }
711 static void ttfPrintCMAP6(FILE *fp, MapPtr map)
712 {
713 USHORT i;
714
715 fprintf(fp, " Format 6 - Trimmed table mapping\n");
716 fprintf(fp,"\t\t Length: %6d\n", map.cmap6->length);
717 fprintf(fp,"\t\t Version: %6d\n", map.cmap6->version);
718 fprintf(fp,"\t\t First Code: 0x%04x\n", map.cmap6->firstCode);
719 fprintf(fp,"\t\t Entry Count: %d\n", map.cmap6->entryCount);
720
721 for (i=0;i<map.cmap6->entryCount;i++)
722 {
723 fprintf(fp,"\t\t glyphIdArray[%d] = %d\n",i,
724 map.cmap6->glyphIndexArray[i]);
725 }
726 }
727 static USHORT ttfLookUpCMAP6(MapPtr map, USHORT cc)
728 {
729 USHORT index;
730
731 index = cc - map.cmap6->firstCode;
732 if (index < map.cmap6->entryCount)
733 return map.cmap6->glyphIndexArray[index];
734 else
735 /* index out of range, return missing glyph */
736 return 0;
737 }
738 static void ttfFreeCMAP6(MapPtr map)
739 {
740 free(map.cmap6->glyphIndexArray);
741 free(map.cmap6);
742 }
743
744 static void ttfLoadCMAP8(FILE *fp, MapPtr *map)
745 {
746 int i;
747
748 map->cmap8 = XCALLOC1 (CMAP8);
749 ttfGetUSHORT(fp);
750 map->cmap8->length = ttfGetULONG(fp);
751 map->cmap8->version = ttfGetULONG(fp);
752 if (fread(map->cmap8->is32, sizeof(BYTE), 8192, fp) != 8192)
753 ttfError("Error when getting is32\n");
754 map->cmap8->nGroups = ttfGetULONG(fp);
755 map->cmap8->charGroup = XCALLOC (map->cmap8->nGroups, CharGroup);
756 for (i = 0; i < map->cmap8->nGroups; i++) {
757 map->cmap8->charGroup[i].startCharCode = ttfGetULONG(fp);
758 map->cmap8->charGroup[i].endCharCode = ttfGetULONG(fp);
759 map->cmap8->charGroup[i].startGlyphID = ttfGetULONG(fp);
760 }
761 }
762 static void ttfPrintCMAP8(FILE *fp, MapPtr map)
763 {
764 int i;
765
766 fprintf(fp, " Format 8 - Mixed 16-bit and 32-bit coverage\n");
767 fprintf(fp,"\t\t Length: %6d\n", map.cmap8->length);
768 fprintf(fp,"\t\t Version: %6d\n", map.cmap8->version);
769 fprintf(fp,"\t\t nGroups: %6d\n", map.cmap8->nGroups);
770 for (i = 0; i < 256; i++) {
771 int j;
772
773 fprintf(fp, "\t is32 %2x", i);
774 for (j = 0; j < 32; j++)
775 fprintf(fp, j % 4 ? "%2x" : " %2x", map.cmap8->is32[i << 5 | j]);
776 fprintf(fp, "\n");
777 }
778 for (i = 0; i < map.cmap8->nGroups; i++)
779 fprintf(fp, "\t\t Group %4d : startCharCode = %d, endCharCode = %d, startGlyphID = %d\n", i,
780 map.cmap8->charGroup[i].startCharCode,
781 map.cmap8->charGroup[i].endCharCode,
782 map.cmap8->charGroup[i].startGlyphID);
783 }
784 static USHORT ttfLookUpCMAP8(MapPtr map, ULONG cc)
785 {
786 int i;
787
788 for (i = 0; i < map.cmap8->nGroups; i++)
789 if (cc > map.cmap8->charGroup[i].endCharCode)
790 continue;
791 else if (cc < map.cmap8->charGroup[i].startCharCode)
792 break;
793 else
794 return map.cmap8->charGroup[i].startGlyphID +
795 (cc - map.cmap8->charGroup[i].startCharCode);
796
797 /* index out of range, return missing glyph */
798 return 0;
799 }
800 static void ttfFreeCMAP8(MapPtr map)
801 {
802 free(map.cmap8->charGroup);
803 free(map.cmap8);
804 }
805
806 static void ttfLoadCMAP10(FILE *fp, MapPtr *map)
807 {
808 ULONG len;
809
810 map->cmap10 = XCALLOC1 (CMAP10);
811 ttfGetUSHORT(fp);
812 map->cmap10->length = ttfGetULONG(fp);
813 map->cmap10->version = ttfGetULONG(fp);
814 map->cmap10->startCharCode = ttfGetULONG(fp);
815 map->cmap10->numChars = len = ttfGetULONG(fp);
816 map->cmap10->glyphs = ttfMakeUSHORT (len, fp);
817 }
818 static void ttfPrintCMAP10(FILE *fp, MapPtr map)
819 {
820 int i;
821
822 fprintf(fp, " Format 10 - Trimmed array\n");
823 fprintf(fp,"\t\t Length: %6d\n", map.cmap10->length);
824 fprintf(fp,"\t\t Version: %6d\n", map.cmap10->version);
825 fprintf(fp,"\t\t Start Char Code: 0x%04x\n", map.cmap10->startCharCode);
826 fprintf(fp,"\t\t Num Chars: %d\n", map.cmap10->numChars);
827
828 for (i = 0; i < map.cmap10->numChars; i++)
829 {
830 fprintf(fp, "\t\t glyphs[%d] = %d\n", i, map.cmap10->glyphs[i]);
831 }
832 }
833 static USHORT ttfLookUpCMAP10(MapPtr map, ULONG cc)
834 {
835 ULONG index;
836
837 index = cc - map.cmap10->startCharCode;
838 if (index < map.cmap10->numChars)
839 return map.cmap10->glyphs[index];
840 else
841 /* index out of range, return missing glyph */
842 return 0;
843 }
844 static void ttfFreeCMAP10(MapPtr map)
845 {
846 free(map.cmap10->glyphs);
847 free(map.cmap10);
848 }
849
850 static void ttfLoadCMAP12(FILE *fp, MapPtr *map)
851 {
852 int i;
853
854 map->cmap12 = XCALLOC1 (CMAP12);
855 ttfGetUSHORT(fp);
856 map->cmap12->length = ttfGetULONG(fp);
857 map->cmap12->version = ttfGetULONG(fp);
858 map->cmap12->nGroups = ttfGetULONG(fp);
859 map->cmap12->charGroup = XCALLOC (map->cmap12->nGroups, CharGroup);
860 for (i = 0; i < map->cmap12->nGroups; i++) {
861 map->cmap12->charGroup[i].startCharCode = ttfGetULONG(fp);
862 map->cmap12->charGroup[i].endCharCode = ttfGetULONG(fp);
863 map->cmap12->charGroup[i].startGlyphID = ttfGetULONG(fp);
864 }
865 }
866 static void ttfPrintCMAP12(FILE *fp, MapPtr map)
867 {
868 int i;
869
870 fprintf(fp, " Format 12 - Segmented coverage\n");
871 fprintf(fp,"\t\t Length: %6d\n", map.cmap12->length);
872 fprintf(fp,"\t\t Version: %6d\n", map.cmap12->version);
873 fprintf(fp,"\t\t nGroups: %6d\n", map.cmap12->nGroups);
874 for (i = 0; i < map.cmap12->nGroups; i++)
875 fprintf(fp, "\t\t Group %4d : startCharCode = %d, endCharCode = %d, startGlyphID = %d\n", i,
876 map.cmap12->charGroup[i].startCharCode,
877 map.cmap12->charGroup[i].endCharCode,
878 map.cmap12->charGroup[i].startGlyphID);
879 }
880 static USHORT ttfLookUpCMAP12(MapPtr map, ULONG cc)
881 {
882 int i;
883
884 for (i = 0; i < map.cmap12->nGroups; i++)
885 if (cc > map.cmap12->charGroup[i].endCharCode)
886 continue;
887 else if (cc < map.cmap12->charGroup[i].startCharCode)
888 break;
889 else
890 return map.cmap12->charGroup[i].startGlyphID +
891 (cc - map.cmap12->charGroup[i].startCharCode);
892
893 /* index out of range, return missing glyph */
894 return 0;
895 }
896 static void ttfFreeCMAP12(MapPtr map)
897 {
898 free(map.cmap12->charGroup);
899 free(map.cmap12);
900 }
901
902 static void ttfLoadCMAP13(FILE *fp, MapPtr *map)
903 {
904 int i;
905
906 map->cmap13 = XCALLOC1 (CMAP13);
907 ttfGetUSHORT(fp);
908 map->cmap13->length = ttfGetULONG(fp);
909 map->cmap13->version = ttfGetULONG(fp);
910 map->cmap13->nGroups = ttfGetULONG(fp);
911 map->cmap13->charGroup = XCALLOC (map->cmap13->nGroups, CharGroup);
912 for (i = 0; i < map->cmap13->nGroups; i++) {
913 map->cmap13->charGroup[i].startCharCode = ttfGetULONG(fp);
914 map->cmap13->charGroup[i].endCharCode = ttfGetULONG(fp);
915 map->cmap13->charGroup[i].startGlyphID = ttfGetULONG(fp);
916 }
917 }
918 static void ttfPrintCMAP13(FILE *fp, MapPtr map)
919 {
920 int i;
921
922 fprintf(fp, " Format 13 - Many-to-one range mappings\n");
923 fprintf(fp,"\t\t Length: %6d\n", map.cmap13->length);
924 fprintf(fp,"\t\t Version: %6d\n", map.cmap13->version);
925 fprintf(fp,"\t\t nGroups: %6d\n", map.cmap13->nGroups);
926 for (i = 0; i < map.cmap13->nGroups; i++)
927 fprintf(fp, "\t\t Group %4d : startCharCode = %d, endCharCode = %d, glyphID = %d\n", i,
928 map.cmap13->charGroup[i].startCharCode,
929 map.cmap13->charGroup[i].endCharCode,
930 map.cmap13->charGroup[i].startGlyphID);
931 }
932 static USHORT ttfLookUpCMAP13(MapPtr map, ULONG cc)
933 {
934 int i;
935
936 for (i = 0; i < map.cmap13->nGroups; i++)
937 if (cc > map.cmap13->charGroup[i].endCharCode)
938 continue;
939 else if (cc < map.cmap13->charGroup[i].startCharCode)
940 break;
941 else
942 return map.cmap12->charGroup[i].startGlyphID;
943
944 /* index out of range, return missing glyph */
945 return 0;
946 }
947 static void ttfFreeCMAP13(MapPtr map)
948 {
949 free(map.cmap13->charGroup);
950 free(map.cmap13);
951 }
952
953 static void ttfLoadCMAP14(FILE *fp, MapPtr *map)
954 {
955 map->cmap14 = XCALLOC1 (CMAP14);
956 map->cmap14->length = ttfGetULONG(fp);
957 }
958 static void ttfPrintCMAP14(FILE *fp, MapPtr map)
959 {
960 fprintf(fp, " Format 14 - Unicode variation sequences\n");
961 fprintf(fp,"\t\t Length: %6d\n", map.cmap14->length);
962 fprintf(fp,"\t\t *** NOT YET IMPLEMENTED ***\n");
963 }
964 static USHORT ttfLookUpCMAP14(MapPtr map, ULONG cc)
965 {
966 return 0;
967 }
968 static void ttfFreeCMAP14(MapPtr map)
969 {
970 free(map.cmap14);
971 }
972