Debian Codegraph @

texk/ttfdump/libttf/name.c

raw
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <ctype.h>
9 #include "ttf.h"
10 #include "ttfutil.h"
11
12 /* $Id: name.c,v 1.1.1.1 1998/06/05 07:47:52 robert Exp $ */
13
14 static void ttfLoadNAME(FILE *fp,NAMEPtr name,ULONG offset);
15 static void ttfLoadNameRecord(FILE *fp,NameRecordPtr rec,ULONG offset);
16 static void ttfPrintNameRecord(FILE *fp,NameRecordPtr rec);
17 static void ttfLoadNameRecordData(FILE *fp,NameRecordPtr rec,ULONG offset);
18 static void ttfPrintNameRecordData(FILE *fp,NameRecordPtr rec);
19 static void ttfFreeNameRecordData(NameRecordPtr rec);
20 static void HexDump(char *p,char *hex,char *asc,int len);
21
22 void ttfInitNAME(TTFontPtr font)
23 {
24 ULONG tag = FT_MAKE_TAG ('n', 'a', 'm', 'e');
25 TableDirPtr ptd;
26
27 if ((ptd = ttfLookUpTableDir(tag,font)) != NULL)
28 {
29 font->name = XCALLOC1 (NAME);
30 ttfLoadNAME(font->fp,font->name,ptd->offset);
31 }
32 }
33
34 static void ttfLoadNAME(FILE *fp,NAMEPtr name,ULONG offset)
35 {
36 USHORT i,n;
37 ULONG pos;
38
39 xfseek(fp, offset, SEEK_SET, "ttfLoadNAME");
40
41 name->format = ttfGetUSHORT(fp);
42 name->numberOfRecords = n = ttfGetUSHORT(fp);
43 name->offset = ttfGetUSHORT(fp);
44
45 name->NameRecords = XCALLOC (n, NameRecord);
46
47 pos = offset + sizeof(USHORT)*3;
48
49 for(i=0;i<n;i++,pos+=12 /*sizeof(NameRecord)*/)
50 {
51 ttfLoadNameRecord(fp,name->NameRecords+i,pos);
52 ttfLoadNameRecordData(fp,name->NameRecords+i,offset+name->offset);
53 }
54 }
55 void ttfPrintNAME(FILE *fp,NAMEPtr name)
56 {
57 USHORT i;
58
59 fprintf(fp,"'name' Table - Naming Table\n");
60 fprintf(fp,"---------------------------\n");
61 fprintf(fp,"\t Format:\t\t %d\n",name->format);
62 fprintf(fp,"\t Number of Record:\t %d\n",name->numberOfRecords);
63 fprintf(fp,"\t Storage offset:\t %d\n",name->offset);
64
65 for (i=0;i<name->numberOfRecords;i++)
66 {
67 fprintf(fp,"Name table %3d.\t",i);
68 ttfPrintNameRecord(fp,name->NameRecords+i);
69 }
70
71 }
72 void ttfFreeNAME(NAMEPtr name)
73 {
74 USHORT i;
75
76 if (!name)
77 return;
78
79 for (i=0;i<name->numberOfRecords;i++)
80 ttfFreeNameRecordData(name->NameRecords+i);
81 free(name->NameRecords);
82 free(name);
83 }
84 /* offset: address of the beginning of the name record */
85 static void ttfLoadNameRecord(FILE *fp,NameRecordPtr rec,ULONG offset)
86 {
87 xfseek(fp, offset, SEEK_SET, "ttfLoadNameRecord");
88
89 rec->PlatformID = ttfGetUSHORT(fp);
90 rec->EncodingID = ttfGetUSHORT(fp);
91 rec->LanguageID = ttfGetUSHORT(fp);
92 rec->NameID = ttfGetUSHORT(fp);
93 rec->length = ttfGetUSHORT(fp);
94 rec->offset = ttfGetUSHORT(fp);
95 }
96 static void ttfPrintNameRecord(FILE *fp,NameRecordPtr rec)
97 {
98 fprintf(fp," PlatformID:\t %d\n",rec->PlatformID);
99 fprintf(fp,"\t\t EncodingID:\t %d\n",rec->EncodingID);
100 fprintf(fp,"\t\t LanguageID:\t %d\n",rec->LanguageID);
101 fprintf(fp,"\t\t NameID:\t %d\n",rec->NameID);
102 fprintf(fp,"\t\t Length:\t %d\n",rec->length);
103 fprintf(fp,"\t\t Offset:\t %d\n",rec->offset);
104 ttfPrintNameRecordData(fp,rec);
105 }
106 /* ULONG offset: address of the beginning of the actual data (base actually)
107 * base: start of data storage, specified by NAME.offset
108 * offset: specified by NameRecord.offset */
109 static void ttfLoadNameRecordData(FILE *fp,NameRecordPtr rec,ULONG offset)
110 {
111 ULONG pos;
112
113 pos = offset + rec->offset;
114 xfseek(fp, pos, SEEK_SET, "ttfLoadNameRecordData");
115
116 rec->data = XTALLOC (rec->length, char);
117 if (fread(rec->data,sizeof(char),rec->length,fp) != rec->length)
118 ttfError("Error when getting Name Record Data\n");
119 }
120
121 static void ttfPrintNameRecordData(FILE *fp,NameRecordPtr rec)
122 {
123 USHORT i,j;
124 char hexbuf[100],ascbuf[100],*p;
125
126 p = rec->data;
127 for (i=0;i<rec->length/10;i++,p+=10)
128 {
129 HexDump(p,hexbuf,ascbuf,10);
130 fprintf(fp,"\t\t %s > %s\n",hexbuf,ascbuf);
131 }
132 HexDump(p,hexbuf,ascbuf,rec->length % 10);
133 /* i know that this is ugly, but this makes output beautiful */
134 i = strlen(hexbuf);
135 for (j=i;j<30;j++)
136 {
137 hexbuf[j] = ' ';
138 }
139 fprintf(fp,"\t\t %s > %s\n",hexbuf,ascbuf);
140
141 }
142 static void HexDump(char *p,char *hex,char *asc,int len)
143 {
144 int i;
145 unsigned char c;
146 char buf[100];
147
148 *hex = '\0';
149 *asc = '\0';
150 for (i=0;i<len;i++)
151 {
152 c = *(p+i);
153 sprintf(buf,"%02x ",c);
154 strcat(hex,buf);
155 if (isprint(c))
156 *(asc+i) = c;
157 else
158 *(asc+i) = '.';
159 }
160 *(asc+len) = *(hex+len*3) = '\0';
161 }
162 static void ttfFreeNameRecordData(NameRecordPtr rec)
163 {
164 free(rec->data);
165 }