Commit 37ed70f628c2e5a934e8347c7bfaf730a2a51c81

Ewald Hew 2017-09-25T06:59:26

Add Type 1 operations to Adobe CFF interpreter. The following Type 1 specific ops have been added (copied from `t1decode'): closepath vstem3 hstem3 seac sbw callothersubr pop setcurrentpoint hsbw The following require a Type 1 mode, because of differences in specification: hstem vstem vmoveto callsubr div rmoveto hmoveto Numbers The subsequent commits will implement these changes and adapt accesses of data and objects to the new interpreter. NOTE: Will not compile in the meantime! * src/psaux/psintrp.c: Add opcodes to enum. (cf2_interpT2CharString): Copy relevant code over from `t1_decoder_parse_charstrings' (in `t1decode.c').

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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
diff --git a/ChangeLog b/ChangeLog
index 5615f62..df034e6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,43 @@
 2017-09-25  Ewald Hew  <ewaldhew@gmail.com>
 
+	[psaux] Add Type 1 operations to Adobe CFF interpreter.
+
+	The following Type 1 specific ops have been added (copied from
+	`t1decode'):
+
+	  closepath
+	  vstem3
+	  hstem3
+	  seac
+	  sbw
+	  callothersubr
+	  pop
+	  setcurrentpoint
+	  hsbw
+
+	The following require a Type 1 mode, because of differences in
+	specification:
+
+	  hstem
+	  vstem
+	  vmoveto
+	  callsubr
+	  div
+	  rmoveto
+	  hmoveto
+	  Numbers
+
+	The subsequent commits will implement these changes and adapt
+	accesses of data and objects to the new interpreter.
+
+	NOTE: Will not compile in the meantime!
+
+	* src/psaux/psintrp.c: Add opcodes to enum.
+	(cf2_interpT2CharString): Copy relevant code over from
+	`t1_decoder_parse_charstrings' (in `t1decode.c').
+
+2017-09-25  Ewald Hew  <ewaldhew@gmail.com>
+
 	[type1] Fixes for rendering.
 
 	The Type 1 advance width calculation passes null for glyph slot,
diff --git a/src/psaux/psintrp.c b/src/psaux/psintrp.c
index d342d6a..133c3bc 100644
--- a/src/psaux/psintrp.c
+++ b/src/psaux/psintrp.c
@@ -205,11 +205,11 @@
     cf2_cmdHLINETO,      /* 6 */
     cf2_cmdVLINETO,      /* 7 */
     cf2_cmdRRCURVETO,    /* 8 */
-    cf2_cmdRESERVED_9,   /* 9 */
+    cf2_cmdCLOSEPATH,    /* 9      T1 only */
     cf2_cmdCALLSUBR,     /* 10 */
     cf2_cmdRETURN,       /* 11 */
     cf2_cmdESC,          /* 12 */
-    cf2_cmdRESERVED_13,  /* 13 */
+    cf2_cmdHSBW,         /* 13     T1 only */
     cf2_cmdENDCHAR,      /* 14 */
     cf2_cmdVSINDEX,      /* 15 */
     cf2_cmdBLEND,        /* 16 */
@@ -233,13 +233,13 @@
   enum
   {
     cf2_escDOTSECTION,   /* 0 */
-    cf2_escRESERVED_1,   /* 1 */
-    cf2_escRESERVED_2,   /* 2 */
+    cf2_escVSTEM3,       /* 1      T1 only */
+    cf2_escHSTEM3,       /* 2      T1 only */
     cf2_escAND,          /* 3 */
     cf2_escOR,           /* 4 */
     cf2_escNOT,          /* 5 */
-    cf2_escRESERVED_6,   /* 6 */
-    cf2_escRESERVED_7,   /* 7 */
+    cf2_escSEAC,         /* 6      T1 only */
+    cf2_escSBW,          /* 7      T1 only */
     cf2_escRESERVED_8,   /* 8 */
     cf2_escABS,          /* 9 */
     cf2_escADD,          /* 10     like otherADD */
@@ -248,8 +248,8 @@
     cf2_escRESERVED_13,  /* 13 */
     cf2_escNEG,          /* 14 */
     cf2_escEQ,           /* 15 */
-    cf2_escRESERVED_16,  /* 16 */
-    cf2_escRESERVED_17,  /* 17 */
+    cf2_escCALLOTHERSUBR,/* 16     T1 only */
+    cf2_escPOP,          /* 17     T1 only */
     cf2_escDROP,         /* 18 */
     cf2_escRESERVED_19,  /* 19 */
     cf2_escPUT,          /* 20     like otherPUT    */
@@ -265,7 +265,7 @@
     cf2_escROLL,         /* 30 */
     cf2_escRESERVED_31,  /* 31 */
     cf2_escRESERVED_32,  /* 32 */
-    cf2_escRESERVED_33,  /* 33 */
+    cf2_escSETCURRENTPT, /* 33     T1 only */
     cf2_escHFLEX,        /* 34 */
     cf2_escFLEX,         /* 35 */
     cf2_escHFLEX1,       /* 36 */
@@ -484,6 +484,12 @@
     CF2_Fixed  scaleY        = font->innerTransform.d;
     CF2_Fixed  nominalWidthX = cf2_getNominalWidthX( decoder );
 
+    /* Stuff for Type 1 */
+    FT_Pos     orig_x, orig_y;
+    FT_Int     known_othersubr_result_cnt   = 0;
+    FT_Int     unknown_othersubr_result_cnt = 0;
+    FT_Bool    large_int;
+
     /* save this for hinting seac accents */
     CF2_Fixed  hintOriginY = curY;
 
@@ -606,6 +612,12 @@
     /* main interpreter loop */
     while ( 1 )
     {
+      if ( font->isT1 )
+      {
+        FT_ASSERT( known_othersubr_result_cnt == 0   ||
+                   unknown_othersubr_result_cnt == 0 );
+      }
+
       if ( cf2_buf_isEnd( charstring ) )
       {
         /* If we've reached the end of the charstring, simulate a */
@@ -627,6 +639,27 @@
           op1 = cf2_cmdRESERVED_0;
       }
 
+      if ( font->isT1 )
+      {
+        if ( unknown_othersubr_result_cnt > 0 &&
+             !( op1 == cf2_cmdCALLSUBR ||
+                op1 == cf2_cmdRETURN   ||
+                op1 == cf2_escPOP      ||
+                op1 >= 32 /* Numbers */ ) )
+        {
+          /* all operands have been transferred by previous pops */
+          unknown_othersubr_result_cnt = 0;
+        }
+
+        if ( large_int && !( op1 >= 32 || op1 == cf2_escDIV ) )
+        {
+          FT_ERROR(( "cf2_interpT2CharString (Type 1 mode):"
+                     " no `div' after large integer\n" ));
+
+          large_int = FALSE;
+        }
+      }
+
       /* check for errors once per loop */
       if ( *error )
         goto exit;
@@ -642,8 +675,6 @@
       {
       case cf2_cmdRESERVED_0:
       case cf2_cmdRESERVED_2:
-      case cf2_cmdRESERVED_9:
-      case cf2_cmdRESERVED_13:
       case cf2_cmdRESERVED_17:
         /* we may get here if we have a prior error */
         FT_TRACE4(( " unknown op (%d)\n", op1 ));
@@ -777,6 +808,13 @@
         if ( font->decoder->width_only )
           goto exit;
 
+        if ( font->isT1 && !decoder->flex_state )
+        {
+          if ( builder->parse_state == T1_Parse_Start )
+            goto Syntax_Error;
+          builder->parse_state = T1_Parse_Have_Moveto;
+        }
+
         curY = ADD_INT32( curY, cf2_stack_popFixed( opStack ) );
 
         cf2_glyphpath_moveTo( &glyphPath, curX, curY );
@@ -878,6 +916,24 @@
         }
         continue; /* no need to clear stack again */
 
+      case cf2_cmdCLOSEPATH:
+        if ( !font->isT1 )
+        {
+          FT_TRACE4(( " unknown op (%d)\n", op1 ));
+        }
+        else
+        {
+          FT_TRACE4(( " closepath" ));
+
+          /* if there is no path, `closepath' is a no-op */
+          if ( builder->parse_state == T1_Parse_Have_Path   ||
+               builder->parse_state == T1_Parse_Have_Moveto )
+            t1_builder_close_contour( builder );
+
+          builder->parse_state = T1_Parse_Have_Width;
+        }
+        break;
+
       case cf2_cmdCALLGSUBR:
       case cf2_cmdCALLSUBR:
         {
@@ -903,6 +959,17 @@
           /* set up the new CFF region and pointer */
           subrNum = cf2_stack_popInt( opStack );
 
+          if ( font->isT1 && decoder->subrs_hash )
+          {
+            size_t*  val = ft_hash_num_lookup( subrNum,
+                                               decoder->subrs_hash );
+
+            if ( val )
+              subrNum = *val;
+            else
+              subrNum = -1;
+          }
+
           switch ( op1 )
           {
           case cf2_cmdCALLGSUBR:
@@ -1060,20 +1127,13 @@
             }
             continue;
 
-          /* these opcodes are reserved in both CFF & CFF2 */
-          case cf2_escRESERVED_1:
-          case cf2_escRESERVED_2:
-          case cf2_escRESERVED_6:
-          case cf2_escRESERVED_7:
+          /* these opcodes are always reserved */
           case cf2_escRESERVED_8:
           case cf2_escRESERVED_13:
-          case cf2_escRESERVED_16:
-          case cf2_escRESERVED_17:
           case cf2_escRESERVED_19:
           case cf2_escRESERVED_25:
           case cf2_escRESERVED_31:
           case cf2_escRESERVED_32:
-          case cf2_escRESERVED_33:
             FT_TRACE4(( " unknown op (12, %d)\n", op2 ));
             break;
 
@@ -1083,7 +1143,7 @@
                 FT_TRACE4(( " unknown op (12, %d)\n", op2 ));
               else
               {
-                /* second switch for 2-byte operators handles just CFF */
+                /* second switch for 2-byte operators handles CFF and Type 1 */
                 switch ( op2 )
                 {
 
@@ -1093,6 +1153,22 @@
 
                   break;
 
+                case cf2_escVSTEM3:
+                  if ( !font->isT1 )
+                    FT_TRACE4(( " unknown op (12, %d)\n", op2 ));
+                  else
+                  {
+                  }
+                  break;
+
+                case cf2_escHSTEM3:
+                  if ( !font->isT1 )
+                    FT_TRACE4(( " unknown op (12, %d)\n", op2 ));
+                  else
+                  {
+                  }
+                  break;
+
                 case cf2_escAND:
                   {
                     CF2_F16Dot16  arg1;
@@ -1136,6 +1212,52 @@
                   }
                   continue; /* do not clear the stack */
 
+                case cf2_escSEAC:
+                  {
+                    if ( !font->isT1 )
+                      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] ) );
+                    }
+                  }
+                  break;
+
+                case cf2_escSBW:
+                  {
+                    if ( !font->isT1 )
+                      FT_TRACE4(( " unknown op (12, %d)\n", op2 ));
+                    else
+                    {
+                      FT_TRACE4(( " sbw" ));
+
+                      builder->parse_state = T1_Parse_Have_Width;
+
+                      builder->left_bearing.x = ADD_LONG( builder->left_bearing.x,
+                                                          top[0] );
+                      builder->left_bearing.y = ADD_LONG( builder->left_bearing.y,
+                                                          top[1] );
+
+                      builder->advance.x = top[2];
+                      builder->advance.y = top[3];
+
+                      x = ADD_LONG( builder->pos_x, top[0] );
+                      y = ADD_LONG( builder->pos_y, top[1] );
+
+                      /* the `metrics_only' indicates that we only want to compute */
+                      /* the glyph's metrics (lsb + advance width), not load the   */
+                      /* rest of it; so exit immediately                           */
+                      if ( builder->metrics_only )
+                        return FT_Err_Ok;
+                    }
+                  }
+                  break;
+
                 case cf2_escABS:
                   {
                     CF2_F16Dot16  arg;
@@ -1198,6 +1320,9 @@
 
                     cf2_stack_pushFixed( opStack,
                                          FT_DivFix( dividend, divisor ) );
+
+                    if ( font->isT1 )
+                      large_int = FALSE;
                   }
                   continue; /* do not clear the stack */
 
@@ -1232,6 +1357,40 @@
                   }
                   continue; /* do not clear the stack */
 
+                case cf2_escCALLOTHERSUBR:
+                  if ( !font->isT1 )
+                    FT_TRACE4(( " unknown op (12, %d)\n", op2 ));
+                  else
+                  {
+                  }
+                  break;
+
+                case cf2_escPOP:
+                  if ( !font->isT1 )
+                    FT_TRACE4(( " unknown op (12, %d)\n", op2 ));
+                  else
+                  {
+                    FT_TRACE4(( " pop" ));
+
+                    if ( known_othersubr_result_cnt > 0 )
+                    {
+                      known_othersubr_result_cnt--;
+                      /* ignore, we pushed the operands ourselves */
+                      break;
+                    }
+
+                    if ( unknown_othersubr_result_cnt == 0 )
+                    {
+                      FT_ERROR(( "cf2_interpT2CharString (Type 1 mode):"
+                                 " no more operands for othersubr\n" ));
+                      goto Syntax_Error;
+                    }
+
+                    unknown_othersubr_result_cnt--;
+                    top++;   /* `push' the operand to callothersubr onto the stack */
+                  }
+                  break;
+
                 case cf2_escDROP:
                   FT_TRACE4(( " drop\n" ));
 
@@ -1433,6 +1592,43 @@
                   }
                   continue; /* do not clear the stack */
 
+                case cf2_escSETCURRENTPT:
+                  if ( !font->isT1 )
+                    FT_TRACE4(( " unknown op (12, %d)\n", op2 ));
+                  else
+                  {
+                    FT_TRACE4(( " setcurrentpoint" ));
+
+                    /* From the T1 specification, section 6.4:                */
+                    /*                                                        */
+                    /*   The setcurrentpoint command is used only in          */
+                    /*   conjunction with results from OtherSubrs procedures. */
+
+                    /* known_othersubr_result_cnt != 0 is already handled     */
+                    /* above.                                                 */
+
+                    /* Note, however, that both Ghostscript and Adobe         */
+                    /* Distiller handle this situation by silently ignoring   */
+                    /* the inappropriate `setcurrentpoint' instruction.  So   */
+                    /* we do the same.                                        */
+#if 0
+
+                    if ( decoder->flex_state != 1 )
+                    {
+                      FT_ERROR(( "t1_decoder_parse_charstrings:"
+                                 " unexpected `setcurrentpoint'\n" ));
+                      goto Syntax_Error;
+                    }
+                    else
+                      ...
+#endif
+
+                    x = top[0];
+                    y = top[1];
+                    decoder->flex_state = 0;
+                  }
+                  break;
+
                 } /* end of 2nd switch checking op2 */
               }
             }
@@ -1441,6 +1637,36 @@
 
         break;
 
+      case cf2_cmdHSBW:
+        if ( !font->isT1 )
+        {
+          FT_TRACE4(( " unknown op (%d)\n", op1 ));
+        }
+        else
+        {
+          FT_TRACE4(( " hsbw" ));
+
+          builder->parse_state = T1_Parse_Have_Width;
+
+          builder->left_bearing.x = ADD_LONG( builder->left_bearing.x,
+                                              top[0] );
+
+          builder->advance.x = top[1];
+          builder->advance.y = 0;
+
+          orig_x = x = ADD_LONG( builder->pos_x, top[0] );
+          orig_y = y = builder->pos_y;
+
+          FT_UNUSED( orig_y );
+
+          /* the `metrics_only' indicates that we only want to compute */
+          /* the glyph's metrics (lsb + advance width), not load the   */
+          /* rest of it; so exit immediately                           */
+          if ( builder->metrics_only )
+            return FT_Err_Ok;
+        }
+        break;
+
       case cf2_cmdENDCHAR:
         FT_TRACE4(( " endchar\n" ));
 
@@ -1461,8 +1687,8 @@
         /* close path if still open */
         cf2_glyphpath_closeOpenPath( &glyphPath );
 
-        /* disable seac for CFF2 (charstring ending with args on stack) */
-        if ( !font->isCFF2 && cf2_stack_count( opStack ) > 1 )
+        /* disable seac for CFF2 and Type1 (charstring ending with args on stack) */
+        if ( !font->isCFF2 && !font->isT1 && cf2_stack_count( opStack ) > 1 )
         {
           /* must be either 4 or 5 --                       */
           /* this is a (deprecated) implied `seac' operator */
@@ -1606,6 +1832,13 @@
         if ( font->decoder->width_only )
           goto exit;
 
+        if ( font->isT1 && !decoder->flex_state )
+        {
+          if ( builder->parse_state == T1_Parse_Start )
+            goto Syntax_Error;
+          builder->parse_state = T1_Parse_Have_Moveto;
+        }
+
         curY = ADD_INT32( curY, cf2_stack_popFixed( opStack ) );
         curX = ADD_INT32( curX, cf2_stack_popFixed( opStack ) );
 
@@ -1626,6 +1859,13 @@
         if ( font->decoder->width_only )
           goto exit;
 
+        if ( font->isT1 && !decoder->flex_state )
+        {
+          if ( builder->parse_state == T1_Parse_Start )
+            goto Syntax_Error;
+          builder->parse_state = T1_Parse_Have_Moveto;
+        }
+        
         curX = ADD_INT32( curX, cf2_stack_popFixed( opStack ) );
 
         cf2_glyphpath_moveTo( &glyphPath, curX, curY );