| 1 |
/* $XConsortium: fontfcn.c,v 1.8 92/03/27 18:15:45 eswu 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 |
#ifdef HAVE_CONFIG_H |
| 33 |
#include "c-auto.h" |
| 34 |
#endif |
| 35 |
|
| 36 |
#include <stdio.h> |
| 37 |
#include <string.h> |
| 38 |
#include "objects.h" |
| 39 |
#include "spaces.h" |
| 40 |
#include "paths.h" |
| 41 |
#include "regions.h" |
| 42 |
#include "util.h" |
| 43 |
#include "fontmisc.h" |
| 44 |
#include "ffilest.h" |
| 45 |
#include "fontfcn.h" |
| 46 |
|
| 47 |
#define FF_PARSE_ERROR 5 |
| 48 |
#define FF_PATH 1 |
| 49 |
|
| 50 |
/***================================================================***/ |
| 51 |
/* GLOBALS */ |
| 52 |
/***================================================================***/ |
| 53 |
char CurFontName[120]; |
| 54 |
char *CurFontEnv; |
| 55 |
char *vm_base = NULL; |
| 56 |
psfont *FontP = NULL; |
| 57 |
psfont TheCurrentFont; |
| 58 |
|
| 59 |
|
| 60 |
/***================================================================***/ |
| 61 |
/* SearchDict - look for name */ |
| 62 |
/* - compare for match on len and string */ |
| 63 |
/* return 0 - not found. */ |
| 64 |
/* return n - nth element in dictionary. */ |
| 65 |
/***================================================================***/ |
| 66 |
int SearchDictName(psdict *dictP, psobj *keyP) |
| 67 |
{ |
| 68 |
int i,n; |
| 69 |
|
| 70 |
|
| 71 |
n = dictP[0].key.len; |
| 72 |
for (i=1;i<=n;i++) { /* scan the intire dictionary */ |
| 73 |
if ( |
| 74 |
(dictP[i].key.len == keyP->len ) |
| 75 |
&& |
| 76 |
(strncmp(dictP[i].key.data.valueP, |
| 77 |
keyP->data.valueP, |
| 78 |
keyP->len) == 0 |
| 79 |
) |
| 80 |
) return(i); |
| 81 |
} |
| 82 |
return(0); |
| 83 |
} |
| 84 |
/***================================================================***/ |
| 85 |
static boolean initFont(void) |
| 86 |
{ |
| 87 |
|
| 88 |
if (!(vm_init())) return(FALSE); |
| 89 |
vm_base = vm_next_byte(); |
| 90 |
if (!(Init_BuiltInEncoding())) return(FALSE); |
| 91 |
strcpy(CurFontName, ""); /* iniitialize to none */ |
| 92 |
FontP = &TheCurrentFont; |
| 93 |
FontP->vm_start = vm_next_byte(); |
| 94 |
FontP->FontFileName.len = 0; |
| 95 |
FontP->FontFileName.data.valueP = CurFontName; |
| 96 |
return(TRUE); |
| 97 |
} |
| 98 |
/***================================================================***/ |
| 99 |
static void resetFont(char *env) |
| 100 |
{ |
| 101 |
|
| 102 |
vm_next = FontP->vm_start; |
| 103 |
vm_free = vm_size - ( vm_next - vm_base); |
| 104 |
FontP->Subrs.len = 0; |
| 105 |
FontP->Subrs.data.stringP = NULL; |
| 106 |
FontP->CharStringsP = NULL; |
| 107 |
FontP->Private = NULL; |
| 108 |
FontP->fontInfoP = NULL; |
| 109 |
FontP->BluesP = NULL; |
| 110 |
/* This will load the font into the FontP */ |
| 111 |
strcpy(CurFontName,env); |
| 112 |
FontP->FontFileName.len = strlen(CurFontName); |
| 113 |
FontP->FontFileName.data.valueP = CurFontName; |
| 114 |
|
| 115 |
} |
| 116 |
/***================================================================***/ |
| 117 |
/* Read font used to attempt to load the font and, upon failure, |
| 118 |
try a second time with twice as much memory. Unfortunately, if |
| 119 |
it's a really complex font, simply using 2*vm_size may be insufficient. |
| 120 |
I've modified it so that the program will try progressively larger |
| 121 |
amounts of memory until it really runs out or the font loads |
| 122 |
successfully. (ndw) |
| 123 |
*/ |
| 124 |
static int readFont(char *env) |
| 125 |
{ |
| 126 |
int rcode; |
| 127 |
|
| 128 |
/* restore the virtual memory and eliminate old font */ |
| 129 |
resetFont(env); |
| 130 |
/* This will load the font into the FontP */ |
| 131 |
rcode = scan_font(FontP); |
| 132 |
return(rcode); |
| 133 |
} |
| 134 |
/***================================================================***/ |
| 135 |
struct region *fontfcnB(struct XYspace *S, unsigned char *code, int *lenP, int *mode) |
| 136 |
{ |
| 137 |
psobj *charnameP; /* points to psobj that is name of character*/ |
| 138 |
int N; |
| 139 |
psdict *CharStringsDictP; /* dictionary with char strings */ |
| 140 |
psobj CodeName; /* used to store the translation of the name*/ |
| 141 |
psobj *SubrsArrayP; |
| 142 |
psobj *theStringP; |
| 143 |
|
| 144 |
struct region *charpath; /* the path for this character */ |
| 145 |
|
| 146 |
charnameP = &CodeName; |
| 147 |
charnameP->len = *lenP; |
| 148 |
charnameP->data.stringP = code; |
| 149 |
|
| 150 |
CharStringsDictP = FontP->CharStringsP; |
| 151 |
|
| 152 |
/* search the chars string for this charname as key */ |
| 153 |
N = SearchDictName(CharStringsDictP,charnameP); |
| 154 |
if (N<=0) { |
| 155 |
*mode = FF_PARSE_ERROR; |
| 156 |
return(NULL); |
| 157 |
} |
| 158 |
/* ok, the nth item is the psobj that is the string for this char */ |
| 159 |
theStringP = &(CharStringsDictP[N].value); |
| 160 |
|
| 161 |
/* get the dictionary pointers to the Subrs */ |
| 162 |
|
| 163 |
SubrsArrayP = &(FontP->Subrs); |
| 164 |
/* scale the Adobe fonts to 1 unit high */ |
| 165 |
S = (struct XYspace *)Permanent(Scale((struct xobject *)S, 1.0 , 1.0)); |
| 166 |
/* call the type 1 routine to rasterize the character */ |
| 167 |
charpath = (struct region *)Type1Char(FontP,S,theStringP,SubrsArrayP,NULL, |
| 168 |
FontP->BluesP, mode); |
| 169 |
Destroy(S); |
| 170 |
/* if Type1Char reported an error, then return */ |
| 171 |
if ( *mode == FF_PARSE_ERROR) return(NULL); |
| 172 |
/* fill with winding rule unless path was requested */ |
| 173 |
if (*mode != FF_PATH) { |
| 174 |
charpath = Interior((struct segment *)charpath,WINDINGRULE+CONTINUITY); |
| 175 |
} |
| 176 |
return(charpath); |
| 177 |
} |
| 178 |
/***================================================================***/ |
| 179 |
/* fontfcnA(env, mode) */ |
| 180 |
/* */ |
| 181 |
/* env is a pointer to a string that contains the fontname. */ |
| 182 |
/* */ |
| 183 |
/* 1) initialize the font - global indicates it has been done */ |
| 184 |
/* 2) load the font */ |
| 185 |
/***================================================================***/ |
| 186 |
Bool fontfcnA(char *env, int *mode) |
| 187 |
{ |
| 188 |
int rc; |
| 189 |
|
| 190 |
/* Has the FontP initialized? If not, then */ |
| 191 |
/* Initialize */ |
| 192 |
if (FontP == NULL) { |
| 193 |
InitImager(); |
| 194 |
if (!(initFont())) { |
| 195 |
/* we are really out of memory */ |
| 196 |
*mode = SCAN_OUT_OF_MEMORY; |
| 197 |
return(FALSE); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
/* if the env is null, then use font already loaded */ |
| 202 |
|
| 203 |
/* if the not same font name */ |
| 204 |
if ( (env) && (strcmp(env,CurFontName) != 0 ) ) { |
| 205 |
/* restore the virtual memory and eliminate old font, read new one */ |
| 206 |
rc = readFont(env); |
| 207 |
if (rc != 0 ) { |
| 208 |
strcpy(CurFontName, ""); /* no font loaded */ |
| 209 |
*mode = rc; |
| 210 |
return(FALSE); |
| 211 |
} |
| 212 |
} |
| 213 |
return(TRUE); |
| 214 |
|
| 215 |
} |
| 216 |
/***================================================================***/ |
| 217 |
/* QueryFontLib(env, infoName,infoValue,rcodeP) */ |
| 218 |
/* */ |
| 219 |
/* env is a pointer to a string that contains the fontname. */ |
| 220 |
/* */ |
| 221 |
/* 1) initialize the font - global indicates it has been done */ |
| 222 |
/* 2) load the font */ |
| 223 |
/* 3) use the font to call getInfo for that value. */ |
| 224 |
/***================================================================***/ |
| 225 |
|
| 226 |
void QueryFontLib( |
| 227 |
char *env, const char *infoName, |
| 228 |
void *infoValue, /* parameter returned here */ |
| 229 |
int *rcodeP) |
| 230 |
{ |
| 231 |
int rc,N,i; |
| 232 |
psdict *dictP; |
| 233 |
psobj nameObj; |
| 234 |
psobj *valueP; |
| 235 |
|
| 236 |
/* Has the FontP initialized? If not, then */ |
| 237 |
/* Initialize */ |
| 238 |
if (FontP == NULL) { |
| 239 |
InitImager(); |
| 240 |
if (!(initFont())) { |
| 241 |
*rcodeP = 1; |
| 242 |
return; |
| 243 |
} |
| 244 |
} |
| 245 |
/* if the env is null, then use font already loaded */ |
| 246 |
/* if the not same font name, reset and load next font */ |
| 247 |
if ( (env) && (strcmp(env,CurFontName) != 0 ) ) { |
| 248 |
/* restore the virtual memory and eliminate old font */ |
| 249 |
rc = readFont(env); |
| 250 |
if (rc != 0 ) { |
| 251 |
strcpy(CurFontName, ""); /* no font loaded */ |
| 252 |
*rcodeP = 1; |
| 253 |
return; |
| 254 |
} |
| 255 |
} |
| 256 |
dictP = FontP->fontInfoP; |
| 257 |
objFormatName(&nameObj,strlen(infoName),infoName); |
| 258 |
N = SearchDictName(dictP,&nameObj); |
| 259 |
/* if found */ |
| 260 |
if ( N > 0 ) { |
| 261 |
*rcodeP = 0; |
| 262 |
switch (dictP[N].value.type) { |
| 263 |
case OBJ_ARRAY: |
| 264 |
valueP = dictP[N].value.data.arrayP; |
| 265 |
if (strcmp(infoName,"FontMatrix") == 0) { |
| 266 |
/* 6 elments, return them as floats */ |
| 267 |
for (i=0;i<6;i++) { |
| 268 |
if (valueP->type == OBJ_INTEGER ) |
| 269 |
((float *)infoValue)[i] = valueP->data.integer; |
| 270 |
else |
| 271 |
((float *)infoValue)[i] = valueP->data.real; |
| 272 |
valueP++; |
| 273 |
} |
| 274 |
} |
| 275 |
if (strcmp(infoName,"FontBBox") == 0) { |
| 276 |
/* 4 elments for Bounding Box. all integers */ |
| 277 |
for (i=0;i<4;i++) { |
| 278 |
((int *)infoValue)[i] = valueP->data.integer; |
| 279 |
valueP++; |
| 280 |
} |
| 281 |
break; |
| 282 |
case OBJ_INTEGER: |
| 283 |
case OBJ_BOOLEAN: |
| 284 |
*((int *)infoValue) = dictP[N].value.data.integer; |
| 285 |
break; |
| 286 |
case OBJ_REAL: |
| 287 |
*((float *)infoValue) = dictP[N].value.data.real; |
| 288 |
break; |
| 289 |
case OBJ_NAME: |
| 290 |
case OBJ_STRING: |
| 291 |
*((char **)infoValue) = dictP[N].value.data.valueP; |
| 292 |
break; |
| 293 |
default: |
| 294 |
*rcodeP = 1; |
| 295 |
break; |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
else *rcodeP = 1; |
| 300 |
} |