Commit d813b5da5994807fe2357eddae95bd6642e3e7bb

Ewald Hew 2017-09-25T08:04:09

Extend Adobe interpreter (seac). This concludes the changes needed to add Type 1 support. * src/psaux/psintrp.c: Update includes. (cf2_interpT2CharString) <cf2_escSEAC>: Implement this similarly to implied seac for CFF. * src/psaux/t1decode.c (t1_lookup_glyph_by_stdcharcode_ps): New function to look up the glyph index. * src/psaux/psft.c (cf2_getT1SeacComponent, cf2_freeT1SeacComponent): New functions to get the charstrings for seac components. * src/psaux/t1decode.h, src/psaux/psft.h: Update declarations.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
diff --git a/ChangeLog b/ChangeLog
index a44ac1a..24ad011 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,24 @@
 2017-09-25  Ewald Hew  <ewaldhew@gmail.com>
 
+	[psaux] Extend Adobe interpreter (seac).
+
+	This concludes the changes needed to add Type 1 support.
+
+	* src/psaux/psintrp.c: Update includes.
+	(cf2_interpT2CharString) <cf2_escSEAC>: Implement this similarly to
+	implied seac for CFF.
+
+	* src/psaux/t1decode.c (t1_lookup_glyph_by_stdcharcode_ps): New
+	function to look up the glyph index.
+
+	* src/psaux/psft.c (cf2_getT1SeacComponent,
+	cf2_freeT1SeacComponent): New functions to get the charstrings for
+	seac components.
+
+	* src/psaux/t1decode.h, src/psaux/psft.h: Update declarations.
+
+2017-09-25  Ewald Hew  <ewaldhew@gmail.com>
+
 	[psaux] Extend Adobe interpreter (flex in callothersubr).
 
 	* src/psaux/psintrp.c (cf2_interpT2CharString)
diff --git a/src/psaux/psft.c b/src/psaux/psft.c
index 81d3fd8..b12d9d1 100644
--- a/src/psaux/psft.c
+++ b/src/psaux/psft.c
@@ -705,6 +705,74 @@
   }
 
 
+  FT_LOCAL_DEF( FT_Error )
+  cf2_getT1SeacComponent( PS_Decoder*  decoder,
+                          FT_UInt      glyph_index,
+                          CF2_Buffer   buf )
+  {
+    FT_Data   glyph_data;
+    FT_Error  error = FT_Err_Ok;
+    T1_Face   face  = (T1_Face)decoder->builder.face;
+    T1_Font   type1 = &face->type1;
+
+#ifdef FT_CONFIG_OPTION_INCREMENTAL
+    FT_Incremental_InterfaceRec *inc =
+      face->root.internal->incremental_interface;
+    /* For incremental fonts get the character data using the */
+    /* callback function.                                     */
+    if ( inc )
+      error = inc->funcs->get_glyph_data( inc->object,
+                                          glyph_index, &glyph_data );
+    else
+#endif
+      /* For ordinary fonts get the character data stored in the face record. */
+    {
+      glyph_data.pointer = type1->charstrings[glyph_index];
+      glyph_data.length  = (FT_Int)type1->charstrings_len[glyph_index];
+    }
+
+    if ( !error )
+    {
+      FT_Byte*  charstring_base = (FT_Byte*)glyph_data.pointer;
+      FT_ULong  charstring_len  = (FT_ULong)glyph_data.length;
+
+
+      FT_ASSERT( charstring_base + charstring_len >= charstring_base );
+
+      FT_ZERO( buf );
+      buf->start =
+      buf->ptr   = charstring_base;
+      buf->end   = charstring_base + charstring_len;
+    }
+
+    return error;
+  }
+
+
+  FT_LOCAL_DEF( void )
+  cf2_freeT1SeacComponent( PS_Decoder*  decoder,
+                           CF2_Buffer   buf )
+  {
+    T1_Face  face;
+    FT_Data  data;
+
+
+    FT_ASSERT( decoder );
+
+#ifdef FT_CONFIG_OPTION_INCREMENTAL
+    face = (T1_Face)decoder->builder.face;
+
+    data.pointer = buf->start;
+    data.length  = (FT_Int)( buf->end - buf->start );
+
+    if ( face->root.internal->incremental_interface )
+      face->root.internal->incremental_interface->funcs->free_glyph_data(
+        face->root.internal->incremental_interface->object,
+        &data );
+#endif /* FT_CONFIG_OPTION_INCREMENTAL */
+  }
+
+
   FT_LOCAL_DEF( CF2_Int )
   cf2_initLocalRegionBuffer( PS_Decoder*  decoder,
                              CF2_Int      subrNum,
diff --git a/src/psaux/psft.h b/src/psaux/psft.h
index 1628970..95e7954 100644
--- a/src/psaux/psft.h
+++ b/src/psaux/psft.h
@@ -131,6 +131,14 @@ FT_BEGIN_HEADER
   cf2_getNominalWidthX( PS_Decoder*  decoder );
 
 
+  FT_LOCAL( FT_Error )
+  cf2_getT1SeacComponent( PS_Decoder*  decoder,
+                          FT_UInt      glyph_index,
+                          CF2_Buffer   buf );
+  FT_LOCAL( void )
+  cf2_freeT1SeacComponent( PS_Decoder*  decoder,
+                           CF2_Buffer   buf );
+
   /*
    * FreeType client outline
    *
diff --git a/src/psaux/psintrp.c b/src/psaux/psintrp.c
index 5705af0..e4018c9 100644
--- a/src/psaux/psintrp.c
+++ b/src/psaux/psintrp.c
@@ -47,8 +47,9 @@
 #include "psintrp.h"
 
 #include "pserror.h"
-#include "psobjs.h"  /* for cff_random */
 
+#include "psobjs.h"   /* for cff_random */
+#include "t1decode.h" /* for t1  seac */
 
   /*************************************************************************/
   /*                                                                       */
@@ -1289,12 +1290,179 @@
                       FT_TRACE4(( " unknown op (12, %d)\n", op2 ));
                     else
                     {
-                      return t1operator_seac( decoder,
-                                              top[0],
-                                              top[1],
-                                              top[2],
-                                              Fix2Int( top[3] ),
-                                              Fix2Int( top[4] ) );
+                      FT_Error     error2;
+                      CF2_Int      bchar_index, achar_index;
+                      FT_Vector    left_bearing, advance;
+#ifdef FT_CONFIG_OPTION_INCREMENTAL
+                      T1_Face      face  = (T1_Face)decoder->builder.face;
+#endif
+                      CF2_BufferRec  component;
+                      CF2_Fixed      dummyWidth;
+
+                      CF2_Int  achar = cf2_stack_popInt( opStack );
+                      CF2_Int  bchar = cf2_stack_popInt( opStack );
+
+                      FT_Pos   ady   = cf2_stack_popFixed ( opStack );
+                      FT_Pos   adx   = cf2_stack_popFixed ( opStack );
+                      FT_Pos   asb   = cf2_stack_popFixed ( opStack );
+
+
+                      FT_TRACE4(( " seac\n" ));
+
+                      if ( doingSeac )
+                      {
+                        FT_ERROR(( " nested seac\n" ));
+                        lastError = FT_THROW( Invalid_Glyph_Format );
+                        goto exit;      /* nested seac */
+                      }
+
+                      if ( decoder->builder.metrics_only )
+                      {
+                        FT_ERROR(( " unexpected seac\n" ));
+                        lastError = FT_THROW( Invalid_Glyph_Format );
+                        goto exit;      /* unexpected seac */
+                      }
+
+                      /* `glyph_names' is set to 0 for CID fonts which do not */
+                      /* include an encoding.  How can we deal with these?    */
+#ifdef FT_CONFIG_OPTION_INCREMENTAL
+                      if ( decoder->glyph_names == 0                   &&
+                           !face->root.internal->incremental_interface )
+#else
+                        if ( decoder->glyph_names == 0 )
+#endif /* FT_CONFIG_OPTION_INCREMENTAL */
+                        {
+                          FT_ERROR(( "cf2_interpT2CharString: (Type 1 seac)"
+                                     " glyph names table not available in this font\n" ));
+                          lastError = FT_THROW( Invalid_Glyph_Format );
+                          goto exit;
+                        }
+
+
+                      /* seac weirdness */
+                      adx += decoder->builder.left_bearing->x;
+
+#ifdef FT_CONFIG_OPTION_INCREMENTAL
+                      if ( face->root.internal->incremental_interface )
+                      {
+                        /* the caller must handle the font encoding also */
+                        bchar_index = bchar;
+                        achar_index = achar;
+                      }
+                      else
+#endif
+                      {
+                        bchar_index = t1_lookup_glyph_by_stdcharcode_ps( decoder, bchar );
+                        achar_index = t1_lookup_glyph_by_stdcharcode_ps( decoder, achar );
+                      }
+
+                      if ( bchar_index < 0 || achar_index < 0 )
+                      {
+                        FT_ERROR(( "cf2_interpT2CharString: (Type 1 seac)"
+                                   " invalid seac character code arguments\n" ));
+                        lastError = FT_THROW( Invalid_Glyph_Format );
+                        goto exit;
+                      }
+
+                      /* if we are trying to load a composite glyph, do not load the */
+                      /* accent character and return the array of subglyphs.         */
+                      if ( decoder->builder.no_recurse )
+                      {
+                        FT_GlyphSlot    glyph  = (FT_GlyphSlot)decoder->builder.glyph;
+                        FT_GlyphLoader  loader = glyph->internal->loader;
+                        FT_SubGlyph     subg;
+
+
+                        /* reallocate subglyph array if necessary */
+                        error2 = FT_GlyphLoader_CheckSubGlyphs( loader, 2 );
+                        if ( error2 )
+                        {
+                          lastError = error2;      /* pass FreeType error through */
+                          goto exit;
+                        }
+
+                        subg = loader->current.subglyphs;
+
+                        /* subglyph 0 = base character */
+                        subg->index = bchar_index;
+                        subg->flags = FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES |
+                                      FT_SUBGLYPH_FLAG_USE_MY_METRICS;
+                        subg->arg1  = 0;
+                        subg->arg2  = 0;
+                        subg++;
+
+                        /* subglyph 1 = accent character */
+                        subg->index = achar_index;
+                        subg->flags = FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES;
+                        subg->arg1  = (FT_Int)FIXED_TO_INT( adx - asb );
+                        subg->arg2  = (FT_Int)FIXED_TO_INT( ady );
+
+                        /* set up remaining glyph fields */
+                        glyph->num_subglyphs = 2;
+                        glyph->subglyphs     = loader->base.subglyphs;
+                        glyph->format        = FT_GLYPH_FORMAT_COMPOSITE;
+
+                        loader->current.num_subglyphs = 2;
+
+                        goto exit;
+                      }
+
+                      /* First load `bchar' in builder */
+                      /* now load the unscaled outline */
+
+                      FT_GlyphLoader_Prepare( decoder->builder.loader );  /* prepare loader */
+
+                      error2 = cf2_getT1SeacComponent( decoder, (FT_UInt)bchar_index, &component );
+                      if ( error2 )
+                      {
+                        lastError = error2;      /* pass FreeType error through */
+                        goto exit;
+                      }
+                      cf2_interpT2CharString( font,
+                                              &component,
+                                              callbacks,
+                                              translation,
+                                              TRUE,
+                                              0,
+                                              0,
+                                              &dummyWidth );
+                      cf2_freeT1SeacComponent( decoder, &component );
+
+                      /* save the left bearing and width of the base character */
+                      /* as they will be erased by the next load.              */
+
+                      left_bearing = *decoder->builder.left_bearing;
+                      advance      = *decoder->builder.advance;
+
+                      decoder->builder.left_bearing->x = 0;
+                      decoder->builder.left_bearing->y = 0;
+
+                      /* Now load `achar' on top of */
+                      /* the base outline           */
+
+                      error2 = cf2_getT1SeacComponent( decoder, (FT_UInt)achar_index, &component );
+                      if ( error2 )
+                      {
+                        lastError = error2;      /* pass FreeType error through */
+                        goto exit;
+                      }
+                      cf2_interpT2CharString( font,
+                                              &component,
+                                              callbacks,
+                                              translation,
+                                              TRUE,
+                                              adx - asb,
+                                              ady,
+                                              &dummyWidth );
+                      cf2_freeT1SeacComponent( decoder, &component );
+
+                      /* restore the left side bearing and   */
+                      /* advance width of the base character */
+
+                      *decoder->builder.left_bearing = left_bearing;
+                      *decoder->builder.advance      = advance;
+
+                      goto exit;
                     }
                   }
                   break;
diff --git a/src/psaux/t1decode.c b/src/psaux/t1decode.c
index 81f5797..37790c5 100644
--- a/src/psaux/t1decode.c
+++ b/src/psaux/t1decode.c
@@ -112,6 +112,55 @@
   /*************************************************************************/
   /*                                                                       */
   /* <Function>                                                            */
+  /*    t1_lookup_glyph_by_stdcharcode_ps                                  */
+  /*                                                                       */
+  /* <Description>                                                         */
+  /*    Looks up a given glyph by its StandardEncoding charcode.  Used to  */
+  /*    implement the SEAC Type 1 operator in the Adobe engine             */
+  /*                                                                       */
+  /* <Input>                                                               */
+  /*    face     :: The current face object.                               */
+  /*                                                                       */
+  /*    charcode :: The character code to look for.                        */
+  /*                                                                       */
+  /* <Return>                                                              */
+  /*    A glyph index in the font face.  Returns -1 if the corresponding   */
+  /*    glyph wasn't found.                                                */
+  /*                                                                       */
+  FT_LOCAL_DEF( FT_Int )
+  t1_lookup_glyph_by_stdcharcode_ps( PS_Decoder*  decoder,
+                                     FT_Int       charcode )
+  {
+    FT_UInt             n;
+    const FT_String*    glyph_name;
+    FT_Service_PsCMaps  psnames = decoder->psnames;
+
+
+    /* check range of standard char code */
+    if ( charcode < 0 || charcode > 255 )
+      return -1;
+
+    glyph_name = psnames->adobe_std_strings(
+                   psnames->adobe_std_encoding[charcode]);
+
+    for ( n = 0; n < decoder->num_glyphs; n++ )
+    {
+      FT_String*  name = (FT_String*)decoder->glyph_names[n];
+
+
+      if ( name                               &&
+           name[0] == glyph_name[0]           &&
+           ft_strcmp( name, glyph_name ) == 0 )
+        return (FT_Int)n;
+    }
+
+    return -1;
+  }
+
+
+  /*************************************************************************/
+  /*                                                                       */
+  /* <Function>                                                            */
   /*    t1_lookup_glyph_by_stdcharcode                                     */
   /*                                                                       */
   /* <Description>                                                         */
diff --git a/src/psaux/t1decode.h b/src/psaux/t1decode.h
index 12c27de..1ca7e01 100644
--- a/src/psaux/t1decode.h
+++ b/src/psaux/t1decode.h
@@ -31,6 +31,9 @@ FT_BEGIN_HEADER
   FT_CALLBACK_TABLE
   const T1_Decoder_FuncsRec  t1_decoder_funcs;
 
+  FT_LOCAL( FT_Int )
+  t1_lookup_glyph_by_stdcharcode_ps( PS_Decoder*  decoder,
+                                     FT_Int       charcode );
 
   FT_LOCAL( FT_Error )
   t1_decoder_parse_glyph( T1_Decoder  decoder,