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 "ttf.h"
9 #include "ttfutil.h"
10 
11 #define __TTF_OS2
12 #include "os2_P.h"
13 
14 /* 	$Id: os2.c,v 1.2 1998/07/06 06:07:01 werner Exp $	 */
15 
16 static void ttfLoadOS2(FILE *fp, OS_2Ptr os2, ULONG offset);
17 
ttfInitOS2(TTFontPtr font)18 void ttfInitOS2(TTFontPtr font)
19 {
20     ULONG tag = FT_MAKE_TAG ('O', 'S', '/', '2');
21     TableDirPtr ptd;
22 
23     if ((ptd = ttfLookUpTableDir(tag, font)) != NULL)
24 	{
25 	    font->os2 = XCALLOC1 (OS_2);
26 	    ttfLoadOS2(font->fp, font->os2, ptd->offset);
27 	}
28 }
29 
ttfLoadOS2(FILE * fp,OS_2Ptr os2,ULONG offset)30 static void ttfLoadOS2(FILE *fp, OS_2Ptr os2, ULONG offset)
31 {
32     xfseek(fp, offset, SEEK_SET, "ttfLoadOS2");
33 
34     os2->version = ttfGetUSHORT(fp);
35     os2->xAvgCharWidth = ttfGetSHORT(fp);
36     os2->usWeightClass = ttfGetUSHORT(fp);
37     os2->usWidthClass = ttfGetUSHORT(fp);
38     os2->fsType = ttfGetUSHORT(fp);
39     os2->ySubscriptXSize = ttfGetSHORT(fp);
40     os2->ySubscriptYSize = ttfGetSHORT(fp);
41     os2->ySubscriptXOffset = ttfGetSHORT(fp);
42     os2->ySubscriptYOffset = ttfGetSHORT(fp);
43     os2->ySuperscriptXSize = ttfGetSHORT(fp);
44     os2->ySuperscriptYSize = ttfGetSHORT(fp);
45     os2->ySuperscriptXOffset = ttfGetSHORT(fp);
46     os2->ySuperscriptYOffset = ttfGetSHORT(fp);
47     os2->yStrikeoutSize = ttfGetSHORT(fp);
48     os2->yStrikeoutPosition = ttfGetSHORT(fp);
49     os2->sFamilyClass = ttfGetSHORT(fp);
50 
51     if (fread(os2->panose, sizeof(CHAR), 10, fp) != 10)
52 	ttfError("Error reading PANOSE\n");
53 
54     os2->ulUnicodeRange1 = ttfGetULONG(fp);
55     os2->ulUnicodeRange2 = ttfGetULONG(fp);
56     os2->ulUnicodeRange3 = ttfGetULONG(fp);
57     os2->ulUnicodeRange4 = ttfGetULONG(fp);
58 
59     if (fread(os2->achVendID, sizeof(CHAR), 4, fp) != 4)
60 	ttfError("Error reading achVendID\n");
61     os2->achVendID[4] = 0x0;
62 
63     os2->fsSelection = ttfGetUSHORT(fp);
64     os2->usFirstCharIndex = ttfGetUSHORT(fp);
65     os2->usLastCharIndex = ttfGetUSHORT(fp);
66     os2->sTypoAscender = ttfGetSHORT(fp);
67     os2->sTypoDescender = ttfGetSHORT(fp);
68     os2->sTypoLineGap = ttfGetSHORT(fp);
69     os2->usWinAscent = ttfGetUSHORT(fp);
70     os2->usWinDescent = ttfGetUSHORT(fp);
71 
72     if (os2->version < 0x0001)
73         return;
74 
75     os2->ulCodePageRange1 = ttfGetULONG(fp);
76     os2->ulCodePageRange2 = ttfGetULONG(fp);
77 
78     if (os2->version < 0x0002)
79         return;
80 
81     os2->sxHeight = ttfGetSHORT(fp);
82     os2->sCapHeight = ttfGetSHORT(fp);
83     os2->usDefaultChar = ttfGetUSHORT(fp);
84     os2->usBreakChar = ttfGetUSHORT(fp);
85     os2->usMaxContext = ttfGetUSHORT(fp);
86 }
87 
88 /* Index checking for string array elements.  */
89 #define StrFromInd(str, ind) StringFromIndex(str, sizeof(str) / sizeof(str[0]), (int)ind)
StringFromIndex(const char ** str,size_t sz,int ind)90 static inline const char *StringFromIndex(const char **str, size_t sz, int ind)
91 {
92   return ind >= 0 && ind < sz ? str[ind] : "invalid";
93 }
94 
ttfPrintOS2(FILE * fp,OS_2Ptr os2)95 void ttfPrintOS2(FILE *fp,OS_2Ptr os2)
96 {
97     char buf[80];
98 
99     fprintf(fp,"'OS/2' Table - OS/2 and Windows Metrics\n");
100     fprintf(fp,"---------------------------------------\n");
101 
102     fprintf(fp,"\t 'OS/2' version:\t %d\n",os2->version);
103     fprintf(fp,"\t xAvgCharWidth:\t\t %d\n",os2->xAvgCharWidth);
104     fprintf(fp,"\t usWeightClass:\t\t %d \t '%s'\n",os2->usWeightClass,
105 	    StrFromInd(WeightClassName,os2->usWeightClass/100 - 1));
106     fprintf(fp,"\t usWidthClass:\t\t %d \t '%s'\n",os2->usWidthClass,
107 	    StrFromInd(WidthClassName,os2->usWidthClass - 1));
108     fprintf(fp,"\t fsType:\t\t %d\n",os2->fsType);
109     fprintf(fp,"\t ySubscriptXSize:\t %d\n",os2->ySubscriptXSize);
110     fprintf(fp,"\t ySubscriptYSize:\t %d\n",os2->ySubscriptYSize);
111     fprintf(fp,"\t ySubscriptXOffset:\t %d\n",os2->ySubscriptXOffset);
112     fprintf(fp,"\t ySubscriptYOffset:\t %d\n",os2->ySubscriptYOffset);
113     fprintf(fp,"\t ySuperscriptXSize:\t %d\n",os2-> ySuperscriptXSize);
114     fprintf(fp,"\t ySuperscriptYSize:\t %d\n",os2->ySuperscriptYSize);
115     fprintf(fp,"\t ySuperscriptXOffset:\t %d\n",os2->ySuperscriptXOffset);
116     fprintf(fp,"\t ySuperscriptYOffset:\t %d\n",os2->ySuperscriptYOffset);
117     fprintf(fp,"\t yStrikeoutSize:\t %d\n",os2->yStrikeoutSize);
118     fprintf(fp,"\t yStrikeoutPosition\t %d\n",os2->yStrikeoutPosition);
119     fprintf(fp,"\t sFamilyClass:\t %d \t subclass = %d\n",
120 	    (os2->sFamilyClass) >> 8,(os2->sFamilyClass) & 0x00ff);
121     fprintf(fp,"\t PANOSE:\n");
122     fprintf(fp,"\t\t Family Kind:\t %d \t '%s'\n",os2->panose[0],
123 	    StrFromInd(PanoseFamily,os2->panose[0]));
124     fprintf(fp,"\t\t Serif Style:\t %d \t '%s'\n",os2->panose[1],
125 	    StrFromInd(PanoseSerif,os2->panose[1]));
126     fprintf(fp,"\t\t Weight:\t %d \t '%s'\n",os2->panose[2],
127 	    StrFromInd(PanoseWeight,os2->panose[2]));
128     fprintf(fp,"\t\t Proportion:\t %d \t '%s'\n",os2->panose[3],
129 	    StrFromInd(PanoseProportion,os2->panose[3]));
130     fprintf(fp,"\t\t Contrast:\t %d \t '%s'\n",os2->panose[4],
131 	    StrFromInd(PanoseContrast,os2->panose[4]));
132     fprintf(fp,"\t\t Stroke:\t %d \t '%s'\n",os2->panose[5],
133 	    StrFromInd(PanoseStroke,os2->panose[5]));
134     fprintf(fp,"\t\t Arm Style:\t %d \t '%s'\n",os2->panose[6],
135 	    StrFromInd(PanoseArm,os2->panose[6]));
136     fprintf(fp,"\t\t Lettreform:\t %d \t '%s'\n",os2->panose[7],
137 	    StrFromInd(PanoseLetterform,os2->panose[7]));
138     fprintf(fp,"\t\t Midline:\t %d \t '%s'\n",os2->panose[8],
139 	    StrFromInd(PanoseMidline,os2->panose[8]));
140     fprintf(fp,"\t\t X-height:\t %d \t '%s'\n",os2->panose[9],
141 	    StrFromInd(PanoseXHeight,os2->panose[9]));
142     fprintf(fp,"\t Unicode Range 1( Bits 0 - 31 ): \t 0x%08x\n",
143 	    os2->ulUnicodeRange1);
144     fprintf(fp,"\t Unicode Range 2( Bits 32 - 63 ): \t 0x%08x\n",
145 	    os2->ulUnicodeRange2);
146     fprintf(fp,"\t Unicode Range 3( Bits 64 - 95 ): \t 0x%08x\n",
147 	    os2->ulUnicodeRange3);
148     fprintf(fp,"\t Unicode Range 4( Bits 96 - 128 ): \t 0x%08x\n",
149 	    os2->ulUnicodeRange4);
150 
151     fprintf(fp,"\t achVendID:\t\t '%s'\n",os2->achVendID);
152 
153     buf[0] = 0x0;
154     if (os2->fsSelection & FS_FLAGS_REGULAR)
155 	{
156 	    strcat(buf,"Regular ");
157 	}
158     else
159 	{
160 	    if (os2->fsSelection & FS_FLAGS_BOLD)
161 		strcat(buf,"Bold ");
162 	    if (os2->fsSelection & FS_FLAGS_ITALIC)
163 		strcat(buf,"Italic ");
164 	}
165     fprintf(fp,"\t fsSelection:\t\t 0x%04x \t '%s'\n",os2->fsSelection,buf);
166     fprintf(fp,"\t usFirstCharIndex:\t 0x%04x\n ",os2->usFirstCharIndex);
167     fprintf(fp,"\t usLastCharIndex:\t 0x%04x\n",os2->usLastCharIndex);
168     fprintf(fp,"\t sTypoAscender:\t\t %d\n",os2->sTypoAscender);
169     fprintf(fp,"\t sTypoDescender:\t %d\n",os2->sTypoDescender);
170     fprintf(fp,"\t sTypoLineGap:\t\t %d\n",os2->sTypoLineGap);
171     fprintf(fp,"\t usWinAscent:\t\t %d\n",os2->usWinAscent);
172     fprintf(fp,"\t usWinDescent:\t\t %d\n",os2->usWinDescent);
173 
174     if (os2->version < 0x0001)
175         return;
176 
177     fprintf(fp,"\t CodePage Range 1( Bits 0 - 31 ):\t 0x%08x\n",
178 	    os2->ulCodePageRange1);
179     fprintf(fp,"\t CodePage Range 2( Bits 32- 63 ):\t 0x%08x\n",
180 	    os2->ulCodePageRange2);
181 
182     if (os2->version < 0x0002)
183         return;
184 
185     fprintf(fp,"\t sxHeight:\t\t %d\n",os2->sxHeight);
186     fprintf(fp,"\t sCapHeight:\t\t %d\n",os2->sCapHeight);
187     fprintf(fp,"\t usDefaultChar:\t\t 0x%04x\n",os2->usDefaultChar);
188     fprintf(fp,"\t usBreakChar:\t\t 0x%04x\n",os2->usBreakChar);
189     fprintf(fp,"\t usMaxContext:\t\t %d\n",os2->usMaxContext);
190 }
191 
ttfFreeOS2(OS_2Ptr os2)192 void ttfFreeOS2(OS_2Ptr os2)
193 {
194     free(os2);
195 }
196