]> git.sesse.net Git - rdpsrv/blob - Xserver/lib/font/Type1/fontfcn.c
Import X server from vnc-3.3.7.
[rdpsrv] / Xserver / lib / font / Type1 / fontfcn.c
1 /* $XConsortium: fontfcn.c,v 1.10 94/02/06 16:07:23 gildea Exp $ */
2 /* Copyright International Business Machines,Corp. 1991
3  * All Rights Reserved
4  *
5  * License to use, copy, modify, and distribute this software
6  * and its documentation for any purpose and without fee is
7  * hereby granted, provided that the above copyright notice
8  * appear in all copies and that both that copyright notice and
9  * this permission notice appear in supporting documentation,
10  * and that the name of IBM not be used in advertising or
11  * publicity pertaining to distribution of the software without
12  * specific, written prior permission.
13  *
14  * IBM PROVIDES THIS SOFTWARE "AS IS", WITHOUT ANY WARRANTIES
15  * OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT
16  * LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT OF
18  * THIRD PARTY RIGHTS.  THE ENTIRE RISK AS TO THE QUALITY AND
19  * PERFORMANCE OF THE SOFTWARE, INCLUDING ANY DUTY TO SUPPORT
20  * OR MAINTAIN, BELONGS TO THE LICENSEE.  SHOULD ANY PORTION OF
21  * THE SOFTWARE PROVE DEFECTIVE, THE LICENSEE (NOT IBM) ASSUMES
22  * THE ENTIRE COST OF ALL SERVICING, REPAIR AND CORRECTION.  IN
23  * NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR
24  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
25  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
26  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
27  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
28  * SOFTWARE.
29  */
30 /* Author: Katherine A. Hitchcock    IBM Almaden Research Laboratory */
31  
32 #include <stdio.h>
33 #include <string.h>
34 #include "t1imager.h"
35 #include "util.h"
36 #include "fontfcn.h"
37 #include "fontmisc.h"
38  
39 extern xobject Type1Char();
40 /***================================================================***/
41 /*   GLOBALS                                                          */
42 /***================================================================***/
43 char CurFontName[120];
44 char *CurFontEnv;
45 char *vm_base = NULL;
46 psfont *FontP = NULL;
47 psfont TheCurrentFont;
48  
49  
50 /***================================================================***/
51 /*   SearchDict - look for  name                                      */
52 /*              - compare for match on len and string                 */
53 /*                return 0 - not found.                               */
54 /*                return n - nth element in dictionary.               */
55 /***================================================================***/
56 int SearchDictName(dictP,keyP)
57  psdict *dictP;
58  psobj  *keyP;
59 {
60   int i,n;
61  
62  
63   n =  dictP[0].key.len;
64   for (i=1;i<=n;i++) {          /* scan the intire dictionary */
65     if (
66         (dictP[i].key.len  == keyP->len )
67         &&
68         (strncmp(dictP[i].key.data.valueP,
69                  keyP->data.valueP,
70                  keyP->len) == 0
71         )
72        ) return(i);
73   }
74   return(0);
75 }
76 /***================================================================***/
77 boolean initFont(cnt)
78 int cnt;
79 {
80
81   if (!(vm_init(cnt))) return(FALSE);
82   vm_base = vm_next_byte();
83   if (!(Init_BuiltInEncoding())) return(FALSE);
84   strcpy(CurFontName, "");    /* iniitialize to none */
85   FontP = &TheCurrentFont;
86   FontP->vm_start = vm_next_byte();
87   FontP->FontFileName.len = 0;
88   FontP->FontFileName.data.valueP = CurFontName;
89   return(TRUE);
90 }
91 /***================================================================***/
92 static void resetFont(env)
93     char *env;
94 {
95  
96   vm_next =  FontP->vm_start;
97   vm_free = vm_size - ( vm_next - vm_base);
98   FontP->Subrs.len = 0;
99   FontP->Subrs.data.stringP = NULL;
100   FontP->CharStringsP = NULL;
101   FontP->Private = NULL;
102   FontP->fontInfoP = NULL;
103   FontP->BluesP = NULL;
104   /* This will load the font into the FontP */
105   strcpy(CurFontName,env);
106   FontP->FontFileName.len = strlen(CurFontName);
107   FontP->FontFileName.data.valueP = CurFontName;
108  
109 }
110 /***================================================================***/
111 int readFont(env)
112 char *env;
113 {
114   int rcode;
115  
116   /* restore the virtual memory and eliminate old font */
117   resetFont(env);
118   /* This will load the font into the FontP */
119   rcode = scan_font(FontP);
120   if (rcode == SCAN_OUT_OF_MEMORY) {
121     /* free the memory and start again */
122     xfree(vm_base);
123     if (!(initFont(vm_size * 2))) {
124       /* we are really out of memory */
125       return(SCAN_OUT_OF_MEMORY);
126       }
127     resetFont(env);
128     rcode = scan_font(FontP);
129     /* only double the memory once, then report error */
130   }
131   return(rcode);
132 }
133 /***================================================================***/
134 xobject fontfcnB(S,code,lenP,mode)
135 XYspace S;
136 unsigned char *code;
137 int  *lenP;
138 int  *mode;
139 {
140   path updateWidth();
141  
142   psobj *charnameP; /* points to psobj that is name of character*/
143   int   N;
144   unsigned char *s;          /* used to search the name for '|' */
145   psdict *CharStringsDictP; /* dictionary with char strings     */
146   psobj   CodeName;   /* used to store the translation of the name*/
147   psobj  *SubrsArrayP;
148   psobj  *theStringP;
149  
150   path  charpath;   /* the path for this character              */
151  
152   charnameP = &CodeName;
153   charnameP->len = *lenP;
154   charnameP->data.stringP = code;
155
156   CharStringsDictP =  FontP->CharStringsP;
157  
158   /* search the chars string for this charname as key */
159   N = SearchDictName(CharStringsDictP,charnameP);
160   if (N<=0) {
161     *mode = FF_PARSE_ERROR;
162     return(NULL);
163   }
164   /* ok, the nth item is the psobj that is the string for this char */
165   theStringP = &(CharStringsDictP[N].value);
166  
167   /* get the dictionary pointers to the Subrs  */
168  
169   SubrsArrayP = &(FontP->Subrs);
170   /* scale the Adobe fonts to 1 unit high */
171   /* call the type 1 routine to rasterize the character     */
172   charpath = Type1Char(FontP,S,theStringP,SubrsArrayP,NULL,
173                FontP->BluesP , mode);
174   /* if Type1Char reported an error, then return */
175   if ( *mode == FF_PARSE_ERROR)  return(NULL);
176   /* fill with winding rule unless path was requested */
177   if (*mode != FF_PATH) {
178     charpath =  Interior(charpath,WINDINGRULE+CONTINUITY);
179   }
180   return(charpath);
181 }
182 /***================================================================***/
183 /*   fontfcnA(env, mode)                                              */
184 /*                                                                    */
185 /*          env is a pointer to a string that contains the fontname.  */
186 /*                                                                    */
187 /*     1) initialize the font     - global indicates it has been done */
188 /*     2) load the font                                               */
189 /***================================================================***/
190 Bool fontfcnA(env,mode)
191 char *env;
192 int  *mode;
193 {
194   int rc;
195  
196   /* Has the FontP initialized?  If not, then   */
197   /* Initialize  */
198   if (FontP == NULL) {
199     InitImager();
200     if (!(initFont(VM_SIZE))) {
201       /* we are really out of memory */
202       *mode = SCAN_OUT_OF_MEMORY;
203       return(FALSE);
204     }
205   }
206  
207   /* if the env is null, then use font already loaded */
208  
209   /* if the not same font name */
210   if ( (env) && (strcmp(env,CurFontName) != 0 ) ) {
211     /* restore the virtual memory and eliminate old font, read new one */
212     rc = readFont(env);
213     if (rc != 0 ) {
214       strcpy(CurFontName, "");    /* no font loaded */
215       *mode = rc;
216       return(FALSE);
217     }
218   }
219   return(TRUE);
220  
221 }
222 /***================================================================***/
223 /*   QueryFontLib(env, infoName,infoValue,rcodeP)                     */
224 /*                                                                    */
225 /*          env is a pointer to a string that contains the fontname.  */
226 /*                                                                    */
227 /*     1) initialize the font     - global indicates it has been done */
228 /*     2) load the font                                               */
229 /*     3) use the font to call getInfo for that value.                */
230 /***================================================================***/
231
232 void QueryFontLib(env,infoName,infoValue,rcodeP)
233 char *env;
234 char *infoName;
235 pointer infoValue;    /* parameter returned here    */
236 int  *rcodeP;
237 {
238   int rc,N,i;
239   psdict *dictP;
240   psobj  nameObj;
241   psobj  *valueP;
242  
243   /* Has the FontP initialized?  If not, then   */
244   /* Initialize  */
245   if (FontP == NULL) {
246     InitImager();
247     if (!(initFont(VM_SIZE))) {
248       *rcodeP = 1;
249       return;
250     }
251   }
252   /* if the env is null, then use font already loaded */
253   /* if the not same font name, reset and load next font */
254   if ( (env) && (strcmp(env,CurFontName) != 0 ) ) {
255     /* restore the virtual memory and eliminate old font */
256     rc = readFont(env);
257     if (rc != 0 ) {
258       strcpy(CurFontName, "");    /* no font loaded */
259       *rcodeP = 1;
260       return;
261     }
262   }
263   dictP = FontP->fontInfoP;
264   objFormatName(&nameObj,strlen(infoName),infoName);
265   N = SearchDictName(dictP,&nameObj);
266   /* if found */
267   if ( N > 0 ) {
268     *rcodeP = 0;
269     switch (dictP[N].value.type) {
270        case OBJ_ARRAY:
271          valueP = dictP[N].value.data.arrayP;
272          /* Just double check valueP. H.J. */
273          if (valueP == NULL) break;
274          if (strcmp(infoName,"FontMatrix") == 0) {
275            /* 6 elments, return them as floats      */
276            for (i=0;i<6;i++) {
277              if (valueP->type == OBJ_INTEGER )
278                ((float *)infoValue)[i] = valueP->data.integer;
279              else
280                ((float *)infoValue)[i] = valueP->data.real;
281             valueP++;
282            }
283          }
284          if (strcmp(infoName,"FontBBox") == 0) {
285            /* 4 elments for Bounding Box.  all integers   */
286            for (i=0;i<4;i++) {
287              ((int *)infoValue)[i] = valueP->data.integer;
288              valueP++;
289            }
290          break;
291        case OBJ_INTEGER:
292        case OBJ_BOOLEAN:
293          *((int *)infoValue) = dictP[N].value.data.integer;
294          break;
295        case OBJ_REAL:
296          *((float *)infoValue) = dictP[N].value.data.real;
297          break;
298        case OBJ_NAME:
299        case OBJ_STRING:
300          *((char **)infoValue) =  dictP[N].value.data.valueP;
301          break;
302        default:
303          *rcodeP = 1;
304          break;
305      }
306    }
307   }
308   else *rcodeP = 1;
309 }