w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

fvfonts.c
Go to the documentation of this file.
1 /* Copyright (C) 2000-2012 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 "fontforgevw.h"
28 #include "namehash.h"
29 
30 static int _SFFindExistingSlot(SplineFont *sf, int unienc, const char *name );
31 
33  struct glyphnamebucket *test, *next;
34  int i;
35 
36  if ( hash==NULL )
37 return;
38  for ( i=0; i<GN_HSIZE; ++i ) {
39  for ( test = hash->table[i]; test!=NULL; test = next ) {
40  next = test->next;
41  chunkfree(test,sizeof(struct glyphnamebucket));
42  }
43  }
44 }
45 
46 static void _GlyphHashFree(SplineFont *sf) {
47 
48  if ( sf->glyphnames==NULL )
49 return;
50  __GlyphHashFree(sf->glyphnames);
51  free(sf->glyphnames);
52  sf->glyphnames = NULL;
53 }
54 
57  if ( sf->cidmaster )
58  _GlyphHashFree(sf->cidmaster);
59 }
60 
61 static void GlyphHashCreate(SplineFont *sf) {
62  int i, k, hash;
63  SplineFont *_sf;
64  struct glyphnamehash *gnh;
65  struct glyphnamebucket *new;
66 
67  if ( sf->glyphnames!=NULL )
68 return;
69  sf->glyphnames = gnh = calloc(1,sizeof(*gnh));
70  k = 0;
71  do {
72  _sf = k<sf->subfontcnt ? sf->subfonts[k] : sf;
73  /* I walk backwards because there are some ttf files where multiple */
74  /* glyphs get the same name. In the cases I've seen only one of these */
75  /* has an encoding. That's the one we want. It will be earlier in the */
76  /* font than the others. If we build the list backwards then it will */
77  /* be the top name in the bucket, and will be the one we return */
78  for ( i=_sf->glyphcnt-1; i>=0; --i ) if ( _sf->glyphs[i]!=NULL ) {
79  new = chunkalloc(sizeof(struct glyphnamebucket));
80  new->sc = _sf->glyphs[i];
81  hash = hashname(new->sc->name);
82  new->next = gnh->table[hash];
83  gnh->table[hash] = new;
84  }
85  ++k;
86  } while ( k<sf->subfontcnt );
87 }
88 
90  struct glyphnamebucket *test;
91 
92  if ( sf->glyphnames==NULL )
94 
95  for ( test=sf->glyphnames->table[hashname(name)]; test!=NULL; test = test->next )
96  if ( strcmp(test->sc->name,name)==0 )
97 return( test->sc );
98 
99 return( NULL );
100 }
101 
102 static int SCUniMatch(SplineChar *sc,int unienc) {
103  struct altuni *alt;
104 
105  if ( sc->unicodeenc==unienc )
106 return( true );
107  for ( alt=sc->altuni; alt!=NULL; alt=alt->next )
108  if ( alt->unienc==unienc )
109 return( true );
110 
111 return( false );
112 }
113 
114 /* Find the position in the glyph list where this code point/name is found. */
115 /* Returns -1 else on error */
116 int SFFindGID(SplineFont *sf, int unienc, const char *name ) {
117  int gid;
118  SplineChar *sc;
119 
120  if ( unienc!=-1 ) {
121  for ( gid=0; gid<sf->glyphcnt; ++gid ) if ( sf->glyphs[gid]!=NULL ) {
122  if ( SCUniMatch(sf->glyphs[gid],unienc) )
123 return( gid );
124  }
125  }
126  if ( name!=NULL ) {
127  sc = SFHashName(sf,name);
128  if ( sc!=NULL )
129 return( sc->orig_pos );
130  }
131 
132 return ( -1 );
133 }
134 
135 int SFCIDFindCID(SplineFont *sf, int unienc, const char *name ) {
136  // For a given SplineFont *sf, find the index of the SplineChar with code unienc or name *name.
137  int j,ret;
138 
139  // If there is a cidmap or if there are multiple subfonts, do complicated things.
140  if ( sf->cidmaster!=NULL || sf->subfontcnt!=0 ) {
141  if ( sf->cidmaster!=NULL )
142  sf=sf->cidmaster;
143  }
144 
145  // If things are simple, perform a flat map.
146  if ( sf->subfonts==NULL && sf->cidmaster==NULL )
147  return( SFFindGID(sf,unienc,name));
148 
149  // If the cid lookup from before failed, look through subfonts.
150  if ( sf->cidmaster!=NULL )
151  sf=sf->cidmaster;
152  for ( j=0; j<sf->subfontcnt; ++j )
153  if (( ret = SFFindGID(sf,unienc,name))!=-1 )
154  return( ret );
155 
156  return( -1 );
157 }
158 
159 int SFHasCID(SplineFont *sf,int cid) {
160  int i;
161  /* What subfont (if any) contains this cid? */
162  if ( sf->cidmaster!=NULL )
163  sf=sf->cidmaster;
164  for ( i=0; i<sf->subfontcnt; ++i )
165  if ( cid<sf->subfonts[i]->glyphcnt &&
166  SCWorthOutputting(sf->subfonts[i]->glyphs[cid]) )
167 return( i );
168  for ( i=0; i<sf->subfontcnt; ++i )
169  if ( cid<sf->subfonts[i]->glyphcnt && sf->subfonts[i]->glyphs[cid]!=NULL )
170 return( i );
171 
172 return( -1 );
173 }
174 
175 SplineChar *SFGetChar(SplineFont *sf, int unienc, const char *name ) {
176  // This function presumably finds a glyph matching the code or name supplied. Undefined code is unienc = -1. Undefined name is name = NULL.
177  int ind = -1;
178  int j;
179  char *pt, *start; int ch;
180 
181  if ( name==NULL )
183  else {
184  for ( start=(char *) name; *start==' '; ++start );
185  for ( pt=start; *pt!='\0' && *pt!='('; ++pt );
186  ch = *pt;
187  // We truncate any glyph name before parentheses.
188  if ( ch=='\0' )
190  else {
191  char *tmp;
192  if ( (tmp = copy(name)) ) {
193  tmp[pt-name] = '\0';
194  ind = SFCIDFindCID(sf,unienc,tmp+(start-name));
195  tmp[pt-name] = ch;
196  free(tmp);
197  }
198  }
199  }
200  if ( ind==-1 )
201  return( NULL );
202 
203  // If the typeface is simple, return the result from the flat glyph collection.
204  if ( sf->subfonts==NULL && sf->cidmaster==NULL )
205  return( sf->glyphs[ind]);
206 
207  if ( sf->cidmaster!=NULL )
208  sf=sf->cidmaster;
209 
210  // Find a subfont that contains the glyph in question.
211  j = SFHasCID(sf,ind);
212  if ( j==-1 )
213  return( NULL );
214 
215  return( sf->subfonts[j]->glyphs[ind] );
216 }
217 
218 static int _SFFindExistingSlot(SplineFont *sf, int unienc, const char *name ) {
219  int gid = -1;
220  struct altuni *altuni;
221 
222  if ( unienc!=-1 ) {
223  for ( gid=sf->glyphcnt-1; gid>=0; --gid ) if ( sf->glyphs[gid]!=NULL ) {
224  if ( sf->glyphs[gid]->unicodeenc==unienc )
225  break;
226  for ( altuni=sf->glyphs[gid]->altuni ; altuni!=NULL &&
227  (altuni->unienc!=unienc || altuni->vs!=-1 || altuni->fid!=0);
228  altuni=altuni->next );
229  if ( altuni!=NULL )
230  break;
231  }
232  }
233  if ( gid==-1 && name!=NULL ) {
235  if ( sc==NULL )
236 return( -1 );
237  gid = sc->orig_pos;
238  if ( gid<0 || gid>=sf->glyphcnt ) {
239  IError("Invalid glyph location when searching for %s", name );
240 return( -1 );
241  }
242  }
243 return( gid );
244 }
245 
246 int SFFindExistingSlot(SplineFont *sf, int unienc, const char *name ) {
247  int gid = _SFFindExistingSlot(sf,unienc,name);
248 
249  if ( gid==-1 || !SCWorthOutputting(sf->glyphs[gid]) )
250 return( -1 );
251 
252 return( gid );
253 }
#define sc
Definition: aptex-macros.h:57
#define name
#define next(a)
Definition: aptex-macros.h:924
#define hash
Definition: aptex.h:388
#define free(a)
Definition: decNumber.cpp:310
int strcmp()
Definition: coll.cpp:143
static void copy(GlyphCachePtr *root)
Definition: gcache.c:378
#define NULL
Definition: ftobjs.h:61
small capitals from c petite p scientific i
Definition: afcover.h:80
voidp calloc()
void * new(uint32_t size)
Definition: mem.c:34
static int ret
Definition: convert.c:72
#define test
Definition: tie.c:129
char ind[999]
Definition: mkind.c:58
int k
Definition: otp-parser.c:70
void __GlyphHashFree(struct glyphnamehash *hash)
Definition: fvfonts.c:32
SplineChar * SFGetChar(SplineFont *sf, int unienc, const char *name)
Definition: fvfonts.c:175
static int SCUniMatch(SplineChar *sc, int unienc)
Definition: fvfonts.c:102
static void _GlyphHashFree(SplineFont *sf)
Definition: fvfonts.c:46
int SFCIDFindCID(SplineFont *sf, int unienc, const char *name)
Definition: fvfonts.c:135
int SFFindGID(SplineFont *sf, int unienc, const char *name)
Definition: fvfonts.c:116
static void GlyphHashCreate(SplineFont *sf)
Definition: fvfonts.c:61
SplineChar * SFHashName(SplineFont *sf, const char *name)
Definition: fvfonts.c:89
int SFFindExistingSlot(SplineFont *sf, int unienc, const char *name)
Definition: fvfonts.c:246
int SFHasCID(SplineFont *sf, int cid)
Definition: fvfonts.c:159
static int _SFFindExistingSlot(SplineFont *sf, int unienc, const char *name)
Definition: fvfonts.c:218
void GlyphHashFree(SplineFont *sf)
Definition: fvfonts.c:55
#define chunkalloc(size)
Definition: splinefont.h:1947
#define chunkfree(item, size)
Definition: splinefont.h:1948
#define IError
Definition: uiinterface.h:124
test
Definition: parser.c:257
static int hashname(const char *pt)
Definition: fvfonts.c:298
#define GN_HSIZE
Definition: fvfonts.c:283
#define SCWorthOutputting(a)
Definition: splinefont.h:2357
int32 unienc
Definition: splinefont.h:1316
struct altuni * next
Definition: splinefont.h:1316
uint32 fid
Definition: splinefont.h:1316
int32 vs
Definition: splinefont.h:1316
struct glyphnamebucket * table[257]
Definition: namehash.h:39
Definition: tfmaux.c:31
SplineChar ** glyphs
Definition: splinefont.h:1690
ch
Definition: t4ht.c:1443
int j
Definition: t4ht.c:1589
@ start
Definition: preamble.c:52
PATTERN * pt
Definition: vlna.c:74