]> git.sesse.net Git - rdpsrv/blob - Xserver/lib/font/Type1/t1info.c
Import X server from vnc-3.3.7.
[rdpsrv] / Xserver / lib / font / Type1 / t1info.c
1 /* $TOG: t1info.c /main/20 1997/06/09 11:21:53 barstow $ */
2 /* Copyright International Business Machines,Corp. 1991
3  * All Rights Reserved
4  *
5  * License, subject to the license given below, to use,
6  * copy, modify, and distribute this software * and its
7  * documentation for any purpose and without fee is hereby
8  * granted, provided that the above copyright notice appear
9  * in all copies and that both that copyright notice and
10  * this permission notice appear in supporting documentation,
11  * and that the name of IBM not be used in advertising or
12  * publicity pertaining to distribution of the software
13  * without specific, written prior permission.
14  *
15  * IBM PROVIDES THIS SOFTWARE "AS IS", WITHOUT ANY WARRANTIES
16  * OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT
17  * LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT OF
19  * THIRD PARTY RIGHTS.  THE ENTIRE RISK AS TO THE QUALITY AND
20  * PERFORMANCE OF THE SOFTWARE, INCLUDING ANY DUTY TO SUPPORT
21  * OR MAINTAIN, BELONGS TO THE LICENSEE.  SHOULD ANY PORTION OF
22  * THE SOFTWARE PROVE DEFECTIVE, THE LICENSEE (NOT IBM) ASSUMES
23  * THE ENTIRE COST OF ALL SERVICING, REPAIR AND CORRECTION.  IN
24  * NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR
25  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
26  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
27  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
28  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
29  * SOFTWARE.
30  *
31  * Author: Carol H. Thompson  IBM Almaden Research Center
32  *   Modeled on spinfo.c by Dave Lemke, Network Computing Devices, Inc
33  *   which contains the following copyright and permission notices:
34  *
35  * Copyright 1990, 1991 Network Computing Devices;
36  * Portions Copyright 1987 by Digital Equipment Corporation
37  *
38  * Permission to use, copy, modify, distribute, and sell this software and its
39  * documentation for any purpose is hereby granted without fee, provided that
40  * the above copyright notice appear in all copies and that both that
41  * copyright notice and this permission notice appear in supporting
42  * documentation, and that the names of Network Computing Devices or Digital 
43  * not be used in advertising or publicity pertaining to distribution of the 
44  * software without specific, written prior permission. Network Computing 
45  * Devices and Digital make no representations about the suitability of this 
46  * software for any purpose.  It is provided "as is" without express or 
47  * implied warranty.
48  *
49  * NETWORK COMPUTING DEVICES AND DIGITAL DISCLAIM ALL WARRANTIES WITH
50  * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
51  * AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES OR DIGITAL BE
52  * LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
53  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
54  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
55  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
56  */
57
58 /*
59
60 Copyright (c) 1987  X Consortium
61
62 Permission is hereby granted, free of charge, to any person obtaining
63 a copy of this software and associated documentation files (the
64 "Software"), to deal in the Software without restriction, including
65 without limitation the rights to use, copy, modify, merge, publish,
66 distribute, sublicense, and/or sell copies of the Software, and to
67 permit persons to whom the Software is furnished to do so, subject to
68 the following conditions:
69
70 The above copyright notice and this permission notice shall be included
71 in all copies or substantial portions of the Software.
72
73 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
74 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
75 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
76 IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
77 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
78 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
79 OTHER DEALINGS IN THE SOFTWARE.
80
81 Except as contained in this notice, the name of the X Consortium shall
82 not be used in advertising or otherwise to promote the sale, use or
83 other dealings in this Software without prior written authorization
84 from the X Consortium.
85
86 */
87
88 #include <stdio.h> 
89 #include "fntfilst.h"
90 #include "FSproto.h"
91 #include "t1intf.h"
92 #include <math.h>
93  
94 #define DECIPOINTSPERINCH 722.7
95 #define DEFAULTRES 75
96 #define DEFAULTPOINTSIZE 120
97  
98 enum scaleType {
99     atom, truncate_atom, pixel_size, point_size, resolution_x,
100     resolution_y, average_width
101 };
102  
103 typedef struct _fontProp {
104     char       *name;
105     long        atom;
106     enum scaleType type;
107 }           fontProp;
108  
109 static fontProp fontNamePropTable[] = {  /* Example: */
110     "FOUNDRY", 0, atom,                  /* adobe */
111     "FAMILY_NAME", 0, atom,              /* times roman */
112     "WEIGHT_NAME", 0, atom,              /* bold */
113     "SLANT", 0, atom,                    /* i */
114     "SETWIDTH_NAME", 0, atom,            /* normal */
115     "ADD_STYLE_NAME", 0, atom,           /* */
116     "PIXEL_SIZE", 0, pixel_size,         /* 18 */
117     "POINT_SIZE", 0, point_size,         /* 180 */
118     "RESOLUTION_X", 0, resolution_x,     /* 72 */
119     "RESOLUTION_Y", 0, resolution_y,     /* 72 */
120     "SPACING", 0, atom,                  /* p */
121     "AVERAGE_WIDTH", 0, average_width,   /* 0 */
122     "CHARSET_REGISTRY", 0, atom,         /* ISO8859 */
123     "CHARSET_ENCODING", 0, truncate_atom, /* 1 */
124 };
125  
126 /* NOTICE: Following array is closely related to the sequence of defines
127    following it. */
128 static fontProp extraProps[] = {
129     "FONT", 0, 0,
130     "COPYRIGHT", 0, 0,
131     "RAW_PIXEL_SIZE", 0, 0,
132     "RAW_POINT_SIZE", 0, 0,
133     "RAW_ASCENT", 0, 0,
134     "RAW_DESCENT", 0, 0,
135     "RAW_AVERAGE_WIDTH", 0, 0,
136     "FACE_NAME", 0, 0,
137 };
138  
139 /* this is a bit kludgy */
140 #define FONTPROP        0
141 #define COPYRIGHTPROP   1
142 #define RAWPIXELPROP    2
143 #define RAWPOINTPROP    3
144 #define RAWASCENTPROP   4
145 #define RAWDESCENTPROP  5
146 #define RAWWIDTHPROP    6
147 #define FACE_NAMEPROP   7
148
149 #define NNAMEPROPS (sizeof(fontNamePropTable) / sizeof(fontProp))
150 #define NEXTRAPROPS (sizeof(extraProps) / sizeof(fontProp))
151  
152 #define NPROPS  (NNAMEPROPS + NEXTRAPROPS)
153  
154 /*ARGSUSED*/
155 static void
156 FillHeader(pInfo, Vals)
157     FontInfoPtr         pInfo;
158     FontScalablePtr     Vals;
159 {
160     /* OpenScalable in T1FUNCS sets the following:
161     pInfo->firstCol,
162     pInfo->firstRow,
163     pInfo->lastCol, and
164     pInfo->lastRow. */
165     /* the following are ununsed
166     pInfo->pad. */
167  
168     /* Items we should handle better someday +++ */
169     pInfo->defaultCh = 0;
170     pInfo->drawDirection = LeftToRight;
171     if (Vals->point_matrix[0] == Vals->point_matrix[3])
172         pInfo->anamorphic = 0;
173     else
174         pInfo->anamorphic = 1;
175     pInfo->inkMetrics = 0;  /* no ink metrics here */
176     pInfo->cachable = 1;    /* no licensing (yet) */
177 }
178  
179 static void
180 adjust_min_max(minc, maxc, tmp)
181     xCharInfo  *minc,
182                *maxc,
183                *tmp;
184 {
185 #define MINMAX(field,ci) \
186         if (minc->field > (ci)->field) \
187              minc->field = (ci)->field; \
188         if (maxc->field < (ci)->field) \
189             maxc->field = (ci)->field;
190  
191     MINMAX(ascent, tmp);
192     MINMAX(descent, tmp);
193     MINMAX(leftSideBearing, tmp);
194     MINMAX(rightSideBearing, tmp);
195     MINMAX(characterWidth, tmp);
196
197     /* Do MINMAX for attributes field.  Since that field is CARD16,
198        we'll cast to a signed integer */
199     if ((INT16)minc->attributes > (INT16)tmp->attributes)
200          minc->attributes = tmp->attributes;
201     if ((INT16)maxc->attributes < (INT16)tmp->attributes)
202         maxc->attributes = tmp->attributes;
203  
204 #undef  MINMAX
205 }
206  
207 static void
208 ComputeBounds(pInfo, pChars, Vals)
209     FontInfoPtr         pInfo;
210     CharInfoPtr         pChars;
211     FontScalablePtr     Vals;
212 {
213     int i;
214     xCharInfo minchar, maxchar;
215     int numchars = 0;
216     int totchars;
217     int overlap;
218     int maxlap;
219  
220     minchar.ascent = minchar.descent =
221         minchar.leftSideBearing = minchar.rightSideBearing =
222         minchar.characterWidth = minchar.attributes = 32767;
223     maxchar.ascent = maxchar.descent =
224         maxchar.leftSideBearing = maxchar.rightSideBearing =
225         maxchar.characterWidth = maxchar.attributes = -32767;
226  
227     maxlap = -32767;
228     totchars = pInfo->lastCol - pInfo->firstCol + 1;
229     pChars += pInfo->firstCol - FIRSTCOL;
230     pInfo->allExist = 1;
231     for (i = 0; i < totchars; i++,pChars++) {
232         xCharInfo *pmetrics = &pChars->metrics;
233  
234         if (pmetrics->attributes ||
235             pmetrics->ascent != -pmetrics->descent ||
236             pmetrics->leftSideBearing != pmetrics->rightSideBearing) {
237             numchars++;
238             adjust_min_max(&minchar, &maxchar, pmetrics);
239             overlap = pmetrics->rightSideBearing - pmetrics->characterWidth;
240             if (overlap > maxlap) maxlap = overlap;
241         }
242         else pInfo->allExist = 0;
243     }
244
245     /* If we're monospaced, round the average width field to the
246        nearest pixel */
247     if (minchar.characterWidth == maxchar.characterWidth)
248         Vals->width = minchar.characterWidth * 10;
249  
250     pInfo->maxbounds = maxchar;
251     pInfo->minbounds = minchar;
252     pInfo->ink_maxbounds = maxchar;
253     pInfo->ink_minbounds = minchar;
254     pInfo->maxOverlap = maxlap + -(minchar.leftSideBearing);
255  
256     /* Set the pInfo flags */
257     /* Properties set by FontComputeInfoAccelerators:
258         pInfo->noOverlap;
259         pInfo->terminalFont;
260         pInfo->constantMetrics;
261         pInfo->constantWidth;
262         pInfo->inkInside;
263  
264     */
265     FontComputeInfoAccelerators (pInfo);
266 }
267  
268 static void
269 ComputeProps(pInfo, Vals, Filename, sAscent, sDescent)
270     FontInfoPtr         pInfo;
271     FontScalablePtr     Vals;
272     char                *Filename;
273     long                *sAscent;
274     long                *sDescent;
275 {
276     int infoint;
277     int infoBBox[4];
278     int rc;
279  
280     QueryFontLib(Filename, "isFixedPitch", &infoint, &rc);
281     if (!rc) {
282         pInfo->constantWidth = infoint;
283     }
284     QueryFontLib((char *)0, "FontBBox", infoBBox, &rc);
285     if (!rc) {
286         pInfo->fontAscent =
287             (int)((double)infoBBox[3] * Vals->pixel_matrix[3] +
288                   (infoBBox[3] > 0 ? 500 : -500)) / 1000;
289         pInfo->fontDescent =
290             -(int)((double)infoBBox[1] * Vals->pixel_matrix[3] +
291                    (infoBBox[1] > 0 ? 500 : -500)) / 1000;
292         *sAscent = infoBBox[3];
293         *sDescent = -infoBBox[1];
294     }
295 }
296  
297 static void
298 ComputeStdProps(pInfo, Vals, Filename, Fontname, sAscent, sDescent, sWidth)
299     FontInfoPtr         pInfo;
300     FontScalablePtr     Vals;
301     char                *Filename;
302     char                *Fontname;
303     long                sAscent;
304     long                sDescent;
305     long                sWidth;
306 {
307     FontPropPtr pp;
308     int         i,
309                 nprops;
310     fontProp   *fpt;
311     char       *is_str;
312     char       *ptr1,
313                *ptr2;
314     char       *ptr3;
315     char *infostrP;
316     long rc;
317     char      scaledName[MAXFONTNAMELEN];
318  
319     strcpy (scaledName, Fontname);
320     /* Fill in our copy of the fontname from the Vals structure */
321     FontParseXLFDName (scaledName, Vals, FONT_XLFD_REPLACE_VALUE);
322  
323     /* This form of the properties is used by the X-client; the X-server
324        doesn't care what they are. */
325     nprops = pInfo->nprops = NPROPS;
326     pInfo->isStringProp = (char *) xalloc(sizeof(char) * nprops);
327     pInfo->props = (FontPropPtr) xalloc(sizeof(FontPropRec) * nprops);
328     if (!pInfo->isStringProp || !pInfo->props) {
329         xfree(pInfo->isStringProp);
330         pInfo->isStringProp = (char *) 0;
331         xfree(pInfo->props);
332         pInfo->props = (FontPropPtr) 0;
333         return;
334     }
335     bzero(pInfo->isStringProp, (sizeof(char) * nprops));
336  
337     ptr2 = scaledName;
338     for (i = NNAMEPROPS, pp = pInfo->props, fpt = fontNamePropTable, is_str = pInfo->isStringProp;
339             i;
340             i--, pp++, fpt++, is_str++) {
341
342         if (*ptr2)
343         {
344             ptr1 = ptr2 + 1;
345             if (!(ptr2 = strchr(ptr1, '-'))) ptr2 = strchr(ptr1, '\0');
346         }
347
348         pp->name = fpt->atom;
349         switch (fpt->type) {
350          case atom:  /* Just copy info from scaledName */
351             *is_str = TRUE;
352             pp->value = MakeAtom(ptr1, ptr2 - ptr1, TRUE);
353             break;
354         case truncate_atom:
355             *is_str = TRUE;
356             for (ptr3 = ptr1; *ptr3; ptr3++)
357                 if (*ptr3 == '[')
358                     break;
359             pp->value = MakeAtom(ptr1, ptr3 - ptr1, TRUE);
360             break;
361          case pixel_size:
362             pp->value = (int)(fabs(Vals->pixel_matrix[3]) + .5);
363             break;
364          case point_size:
365             pp->value = (int)(fabs(Vals->point_matrix[3]) * 10.0 + .5);
366             break;
367          case resolution_x:
368             pp->value = Vals->x;
369             break;
370          case resolution_y:
371             pp->value = Vals->y;
372             break;
373          case average_width:
374             pp->value = Vals->width;
375             break;
376         }
377     }
378  
379     for (i = 0, fpt = extraProps;
380           i < NEXTRAPROPS;
381           i++, is_str++, pp++, fpt++) {
382         pp->name = fpt->atom;
383         switch (i) {
384          case FONTPROP:
385             *is_str = TRUE;
386             pp->value = MakeAtom(scaledName, strlen(scaledName), TRUE);
387             break;
388          case COPYRIGHTPROP:
389             *is_str = TRUE;
390             QueryFontLib(Filename, "Notice", &infostrP, &rc);
391             if (rc || !infostrP) {
392                 infostrP = "Copyright Notice not available";
393             }
394             pp->value = MakeAtom(infostrP, strlen(infostrP), TRUE);
395             break;
396          case FACE_NAMEPROP:
397             *is_str = TRUE;
398             QueryFontLib(Filename, "FontName", &infostrP, &rc);
399             if (rc || !infostrP) {
400                 infostrP = "(unknown)"; 
401             }
402             pp->value = MakeAtom(infostrP, strlen(infostrP), TRUE);
403             break;
404          case RAWPIXELPROP:
405             *is_str = FALSE;
406             pp->value = 1000;
407             break;
408          case RAWPOINTPROP:
409             *is_str = FALSE;
410             pp->value = (long)(72270.0 / (double)Vals->y + .5);
411             break;
412          case RAWASCENTPROP:
413             *is_str = FALSE;
414             pp->value = sAscent;
415             break;
416          case RAWDESCENTPROP:
417             *is_str = FALSE;
418             pp->value = sDescent;
419             break;
420          case RAWWIDTHPROP:
421             *is_str = FALSE;
422             pp->value = sWidth;
423             break;
424         }
425     }
426 }
427  
428 /*ARGSUSED*/
429 int
430 Type1GetInfoScalable(fpe, pInfo, entry, fontName, fileName, Vals)
431     FontPathElementPtr  fpe;
432     FontInfoPtr         pInfo;
433     FontEntryPtr        entry;
434     FontNamePtr         fontName;
435     char                *fileName;
436     FontScalablePtr     Vals;
437 {
438     FontPtr pfont;
439     int flags = 0;
440     long format = 0;  /* It doesn't matter what format for just info */
441     long fmask = 0;
442     int ret;
443  
444     ret = Type1OpenScalable(fpe, &pfont, flags, entry, fileName, Vals, format, fmask);
445     if (ret != Successful)
446         return ret;
447     *pInfo = pfont->info;
448
449     /* XXX - Set pointers in pfont->info to NULL so they are not freed. */
450     pfont->info.props = NULL;
451     pfont->info.isStringProp = NULL;
452
453     Type1CloseFont(pfont);
454     return Successful;
455 }
456  
457 void
458 T1FillFontInfo(pFont, Vals, Filename, Fontname, sWidth)
459     FontPtr             pFont;
460     FontScalablePtr     Vals;
461     char                *Filename;
462     char                *Fontname;
463     long                sWidth;
464 {
465     FontInfoPtr         pInfo = &pFont->info;
466     struct type1font *p = (struct type1font *)pFont->fontPrivate;
467     long sAscent, sDescent;     /* Scalable 1000-pixel values */
468  
469     FillHeader(pInfo, Vals);
470  
471     ComputeBounds(pInfo, p->glyphs, Vals);
472  
473     ComputeProps(pInfo, Vals, Filename, &sAscent, &sDescent);
474     ComputeStdProps(pInfo, Vals, Filename, Fontname, sAscent, sDescent, sWidth);
475 }
476  
477 /* Called once, at renderer registration time */
478 void
479 T1InitStdProps()
480 {
481     int         i;
482     fontProp   *t;
483  
484     i = sizeof(fontNamePropTable) / sizeof(fontProp);
485     for (t = fontNamePropTable; i; i--, t++)
486         t->atom = MakeAtom(t->name, (unsigned) strlen(t->name), TRUE);
487     i = sizeof(extraProps) / sizeof(fontProp);
488     for (t = extraProps; i; i--, t++)
489         t->atom = MakeAtom(t->name, (unsigned) strlen(t->name), TRUE);
490 }