1 /* Copyright (C) 2000-2008 by George Williams */
2 /*
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are met:
5 
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer.
8 
9  * Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12 
13  * The name of the author may not be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15 
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 #include "pfaedit.h"
28 #include <stdio.h>
29 #include <math.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ustring.h>
33 #include <utype.h>
34 #include <unistd.h>
35 #include <locale.h>
36 #ifndef _WIN32
37 # include <pwd.h>
38 #endif
39 #include <stdarg.h>
40 #include <time.h>
41 #include "psfont.h"
42 #include "splinefont.h"
43 
44 #ifdef __CygWin
45  #include <sys/types.h>
46  #include <sys/stat.h>
47  #include <unistd.h>
48 #endif
49 
50 extern int autohint_before_generate;
51 char *xuid=NULL;
52 
53 typedef void (*DumpChar)(int ch,void *data);
54 struct fileencryptdata {
55     DumpChar olddump;
56     void *olddata;
57     unsigned short r;
58     int hexline;
59 };
60 #define c1	52845
61 #define c2	22719
62 
PSDictCopy(struct psdict * dict)63 struct psdict *PSDictCopy(struct psdict *dict) {
64     struct psdict *ret;
65     int i;
66 
67     if ( dict==NULL )
68 return( NULL );
69 
70     ret = gcalloc(1,sizeof(struct psdict));
71     ret->cnt = dict->cnt; ret->next = dict->next;
72     ret->keys = gcalloc(ret->cnt,sizeof(char *));
73     ret->values = gcalloc(ret->cnt,sizeof(char *));
74     for ( i=0; i<dict->next; ++i ) {
75 	ret->keys[i] = copy(dict->keys[i]);
76 	ret->values[i] = copy(dict->values[i]);
77     }
78 
79 return( ret );
80 }
81 
PSDictFindEntry(struct psdict * dict,char * key)82 int PSDictFindEntry(struct psdict *dict, char *key) {
83     int i;
84 
85     if ( dict==NULL )
86 return( -1 );
87 
88     for ( i=0; i<dict->next; ++i )
89 	if ( strcmp(dict->keys[i],key)==0 )
90 return( i );
91 
92 return( -1 );
93 }
94 
PSDictHasEntry(struct psdict * dict,char * key)95 char *PSDictHasEntry(struct psdict *dict, char *key) {
96     int i;
97 
98     if ( dict==NULL )
99 return( NULL );
100 
101     for ( i=0; i<dict->next; ++i )
102 	if ( strcmp(dict->keys[i],key)==0 )
103 return( dict->values[i] );
104 
105 return( NULL );
106 }
107 
PSDictRemoveEntry(struct psdict * dict,char * key)108 int PSDictRemoveEntry(struct psdict *dict, char *key) {
109     int i;
110 
111     if ( dict==NULL )
112 return( false );
113 
114     for ( i=0; i<dict->next; ++i )
115 	if ( strcmp(dict->keys[i],key)==0 )
116     break;
117     if ( i==dict->next )
118 return( false );
119     free( dict->keys[i]);
120     free( dict->values[i] );
121     --dict->next;
122     while ( i<dict->next ) {
123 	dict->keys[i] = dict->keys[i+1];
124 	dict->values[i] = dict->values[i+1];
125 	++i;
126     }
127 
128 return( true );
129 }
130 
PSDictChangeEntry(struct psdict * dict,char * key,char * newval)131 int PSDictChangeEntry(struct psdict *dict, char *key, char *newval) {
132     int i;
133 
134     if ( dict==NULL )
135 return( -1 );
136 
137     for ( i=0; i<dict->next; ++i )
138 	if ( strcmp(dict->keys[i],key)==0 )
139     break;
140     if ( i==dict->next ) {
141 	if ( dict->next>=dict->cnt ) {
142 	    dict->cnt += 10;
143 	    dict->keys = grealloc(dict->keys,dict->cnt*sizeof(char *));
144 	    dict->values = grealloc(dict->values,dict->cnt*sizeof(char *));
145 	}
146 	dict->keys[dict->next] = copy(key);
147 	dict->values[dict->next] = NULL;
148 	++dict->next;
149     }
150     free(dict->values[i]);
151     dict->values[i] = copy(newval);
152 return( i );
153 }
154 
155 
FindMaxDiffOfBlues(char * pt,double max_diff)156 static double FindMaxDiffOfBlues(char *pt, double max_diff) {
157     char *end;
158     double p1, p2;
159 
160     while ( *pt==' ' || *pt=='[' ) ++pt;
161     forever {
162 	p1 = strtod(pt,&end);
163 	if ( end==pt )
164     break;
165 	pt = end;
166 	p2 = strtod(pt,&end);
167 	if ( end==pt )
168     break;
169 	if ( p2-p1 >max_diff ) max_diff = p2-p1;
170 	pt = end;
171     }
172 return( max_diff );
173 }
174 
BlueScaleFigureForced(struct psdict * private,real bluevalues[],real otherblues[])175 double BlueScaleFigureForced(struct psdict *private,real bluevalues[], real otherblues[]) {
176     double max_diff=0;
177     char *pt;
178     int i;
179 
180     pt = PSDictHasEntry(private,"BlueValues");
181     if ( pt!=NULL ) {
182 	max_diff = FindMaxDiffOfBlues(pt,max_diff);
183     } else if ( bluevalues!=NULL ) {
184 	for ( i=0; i<14 && (bluevalues[i]!=0 || bluevalues[i+1])!=0; i+=2 ) {
185 	    if ( bluevalues[i+1] - bluevalues[i]>=max_diff )
186 		max_diff = bluevalues[i+1] - bluevalues[i];
187 	}
188     }
189     pt = PSDictHasEntry(private,"FamilyBlues");
190     if ( pt!=NULL )
191 	max_diff = FindMaxDiffOfBlues(pt,max_diff);
192 
193     pt = PSDictHasEntry(private,"OtherBlues");
194     if ( pt!=NULL )
195 	max_diff = FindMaxDiffOfBlues(pt,max_diff);
196     else if ( otherblues!=NULL ) {
197 	for ( i=0; i<10 && (otherblues[i]!=0 || otherblues[i+1]!=0); i+=2 ) {
198 	    if ( otherblues[i+1] - otherblues[i]>=max_diff )
199 		max_diff = otherblues[i+1] - otherblues[i];
200 	}
201     }
202     pt = PSDictHasEntry(private,"FamilyOtherBlues");
203     if ( pt!=NULL )
204 	max_diff = FindMaxDiffOfBlues(pt,max_diff);
205     if ( max_diff<=0 )
206 return( -1 );
207     if ( 1/max_diff > .039625 )
208 return( -1 );
209 
210 return( .99/max_diff );
211 }
212 
BlueScaleFigure(struct psdict * private,real bluevalues[],real otherblues[])213 double BlueScaleFigure(struct psdict *private,real bluevalues[], real otherblues[]) {
214     if ( PSDictHasEntry(private,"BlueScale")!=NULL )
215 return( -1 );
216     return BlueScaleFigureForced(private, bluevalues, otherblues);
217 }
218 
219