summaryrefslogtreecommitdiff
path: root/Build/source/texk/lcdf-typetools/lcdf-typetools-2.98/mmpfb/t1rewrit.cc
blob: 86382ca239b0fdb8c3bde7b2633d4aa5aaaf0e37 (plain)
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
/* t1rewrit.cc -- routines for multiple- to single-master charstring conversion
 *
 * Copyright (c) 1997-2012 Eddie Kohler
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version. This program is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "t1rewrit.hh"
#include <efont/t1item.hh>
#include <efont/t1csgen.hh>
#include <lcdf/error.hh>
#include <lcdf/straccum.hh>
#include <stdio.h>
#include <math.h>

using namespace Efont;

static bool itc_complained = false;
static ErrorHandler *itc_errh;

void
itc_complain()
{
    //itc_errh->warning("strange %<callothersubr%>; is this an ITC font?");
    itc_complained = true;
}


/*****
 * HintReplacementDetector
 **/

class HintReplacementDetector : public CharstringInterp { public:

    HintReplacementDetector(Type1Font *, int);
    HintReplacementDetector(Type1Font *, const Vector<double> &, int);

    bool is_hint_replacement(int i) const { return _hint_replacements[i] != 0; }
    int call_count(int i) const		{ return _call_counts[i]; }

    bool type1_command(int);

    bool run(Type1Font *, Type1Charstring &);

  private:

    Vector<int> _hint_replacements;
    Vector<int> _call_counts;
    int _subr_level;
    int _count_calls_below;

};

HintReplacementDetector::HintReplacementDetector(Type1Font *f, int b)
    : CharstringInterp(),
      _hint_replacements(f->nsubrs(), 0), _call_counts(f->nsubrs(), 0),
      _count_calls_below(b)
{
}

HintReplacementDetector::HintReplacementDetector(Type1Font *f, const Vector<double> &wv, int b)
    : CharstringInterp(wv),
      _hint_replacements(f->nsubrs(), 0), _call_counts(f->nsubrs(), 0),
      _count_calls_below(b)
{
}

bool
HintReplacementDetector::type1_command(int cmd)
{
    switch (cmd) {

      case Cs::cCallothersubr: {
	  if (size() < 2)
	      goto unknown;
	  int command = (int)top(0);
	  int n = (int)top(1);
	  if (command == Cs::othcReplacehints && n == 1) {
	      pop(2);
	      _hint_replacements[(int)top()] = 1;
	      ps_clear();
	      ps_push(top());
	      pop();
	      break;
	  } else if (command >= Cs::othcMM1 && command <= Cs::othcMM6) {
	      pop(2);
	      return mm_command(command, n);
	  } else if (command >= Cs::othcITC_load && command <= Cs::othcITC_random) {
	      pop(2);
	      return itc_command(command, n);
	  } else
	      goto unknown;
      }

      case Cs::cCallsubr: {
	  if (size() < 1)
	      return error(errUnderflow, cmd);
	  int which = (int)pop();
	  if (!_count_calls_below || _subr_level < _count_calls_below)
	      _call_counts[which]++;

	  Charstring *subr_cs = get_subr(which);
	  if (!subr_cs)
	      return error(errSubr, which);

	  _subr_level++;
	  subr_cs->process(*this);
	  _subr_level--;

	  if (error() != errOK)
	      return false;
	  return !done();
      }

      case Cs::cEndchar:
      case Cs::cReturn:
	return CharstringInterp::type1_command(cmd);

      case Cs::cBlend:
      case Cs::cAbs:
      case Cs::cAdd:
      case Cs::cSub:
      case Cs::cDiv:
      case Cs::cNeg:
      case Cs::cRandom:
      case Cs::cMul:
      case Cs::cSqrt:
      case Cs::cDrop:
      case Cs::cExch:
      case Cs::cIndex:
      case Cs::cRoll:
      case Cs::cDup:
      case Cs::cAnd:
      case Cs::cOr:
      case Cs::cNot:
      case Cs::cEq:
      case Cs::cIfelse:
	return arith_command(cmd);

      case Cs::cPop:
	if (ps_size() >= 1)
	    push(ps_pop());
	break;

      default:
      unknown:
	clear();
	break;

    }
    return true;
}

bool
HintReplacementDetector::run(Type1Font *f, Type1Charstring &cs)
{
    _subr_level = 0;
    CharstringInterp::interpret(f, &cs);
    return error() == errOK;
}


/*****
 * Type1OneMMRemover
 **/

class Type1OneMMRemover: public CharstringInterp { public:

    Type1OneMMRemover(Type1MMRemover *);

    bool type1_command(int);

    inline bool run_fresh_subr(const Type1Charstring &, bool);
    inline bool run_fresh_glyph(const Type1Charstring &);
    inline bool rerun_subr(const Type1Charstring &);

    Type1Charstring *output_prefix();
    void output_main(Type1Charstring &);

  private:

    Type1MMRemover *_remover;
    Type1CharstringGen _prefix_gen;
    Type1CharstringGen _main_gen;

    int _subr_level;
    bool _in_subr;
    bool _in_prefix;
    bool _must_expand;

    inline void run_subr(Type1Charstring *);
    bool itc_command(int command, int on_stack);

    bool run(const Type1Charstring &, bool, bool, bool);

};

/* For version 1.1
 *
 * Problem: Sometimes a charstring will call one subroutine, which will call
 * another, etc., which finally does a multiple master CallOtherSubr! The
 * required arguments might build up only gradually over all the subrs. This
 * makes it hard to remove the eventual CallOtherSubr!
 *
 * Partial solution: Divide each subroutine into two parts: the initial
 * "prefix" contains the closure of any initial multiple-master commands
 * (including those from sub-subroutines), the following "main" part has all
 * the other commands. In a situation like this:
 *
 * subr-1 = 8 15 callothersubr pop pop pop return
 * subr-2 = 4 5 6  1 callsubr return
 * subr-3 = 1 2 3  2 callsubr return
 *
 * we'll divide it up like this: (prefix || main)
 *
 * subr-1 =             8 15 callothersubr pop pop pop || (nothing)
 * subr-2 =       4 5 6 8 15 callothersubr pop pop pop || (nothing)
 * subr-3 = 1 2 3 4 5 6 8 15 callothersubr pop pop pop || (nothing)
 *
 * Now, when we call a subroutine, we EXECUTE its prefix part. Then, if its
 * main part is nonempty, we output the original call to the subroutine to
 * take care of the main part. */


Type1OneMMRemover::Type1OneMMRemover(Type1MMRemover *remover)
    : CharstringInterp(remover->weight_vector()),
      _remover(remover), _prefix_gen(remover->precision()),
      _main_gen(remover->precision())
{
}

inline void
Type1OneMMRemover::run_subr(Type1Charstring *cs)
{
    _subr_level++;
    cs->process(*this);
    _subr_level--;
}

bool
Type1OneMMRemover::itc_command(int command, int on_stack)
{
    const Vector<double> &weight = weight_vector();
    assert(weight.size());
    Vector<double> *scratch = scratch_vector();
    Type1CharstringGen *gen =
	(_in_prefix ? &_prefix_gen : (_in_subr ? &_main_gen : 0));

    int base = size() - on_stack - 2;
    switch (command) {

      case Cs::othcITC_load: {
	  if (on_stack != 1)
	      return false;
	  int offset = (int)at(base);
	  for (int i = 0; i < weight.size(); i++)
	      vec(scratch, offset+i) = weight.at_u(i);
	  // save load command, so we expand its effects into the scratch
	  // vector
	  if (gen) {
	      gen->gen_number(offset);
	      gen->gen_number(1);
	      gen->gen_number(Cs::othcITC_load);
	      gen->gen_command(Cs::cCallothersubr);
	  }
	  break;
      }

      case Cs::othcITC_put: {
	  if (on_stack != 2)
	      return false;
	  int offset = (int)at(base+1);
	  vec(scratch, offset) = at(base);
	  // save put command, so we expand its effects into the scratch
	  // vector
	  if (gen) {
	      gen->gen_number(at(base));
	      gen->gen_number(offset);
	      gen->gen_number(2);
	      gen->gen_number(Cs::othcITC_put);
	      gen->gen_command(Cs::cCallothersubr);
	  }
	  break;
      }

      case Cs::othcITC_get: {
	  if (on_stack != 1)
	      return false;
	  int offset = (int)at(base);
	  double d = vec(scratch, offset);
	  if (!KNOWN(d)) {
	      _must_expand = true;
	      return false;
	  }
	  ps_push(d);
	  break;
      }

      case Cs::othcITC_add: {
	  if (on_stack != 2)
	      return false;
	  ps_push(at(base) + at(base+1));
	  break;
      }

      case Cs::othcITC_sub: {
	  if (on_stack != 2)
	      return false;
	  ps_push(at(base) - at(base+1));
	  break;
      }

      case Cs::othcITC_mul: {
	  if (on_stack != 2)
	      return false;
	  ps_push(at(base) * at(base+1));
	  break;
      }

      case Cs::othcITC_div: {
	  if (on_stack != 2)
	      return false;
	  ps_push(at(base) / at(base+1));
	  break;
      }

      case Cs::othcITC_ifelse: {
	  if (on_stack != 4)
	      return false;
	  if (at(base+2) <= at(base+3))
	      ps_push(at(base));
	  else
	      ps_push(at(base+1));
	  break;
      }

      default:
	return false;

    }

    pop(on_stack + 2);
    return true;
}

bool
Type1OneMMRemover::type1_command(int cmd)
{
    switch (cmd) {

      case Cs::cCallothersubr: {
	  // Expand known othersubr calls. If we cannot expand the othersubr
	  // call completely, then write it to the expander.
	  if (size() < 2)
	      goto partial_othersubr;
	  int command = (int)top(0);
	  int n = (int)top(1);
	  if (command >= Cs::othcITC_load && command <= Cs::othcITC_random) {
	      if (!itc_complained)
		  itc_complain();
	      if (size() < 2 + n || !itc_command(command, n))
		  goto partial_othersubr;
	  } else if (command >= Cs::othcMM1 && command <= Cs::othcMM6) {
	      if (size() < 2 + n)
		  goto partial_othersubr;
	      pop(2);
	      mm_command(command, n);
	  } else
	      goto normal;
	  break;
      }

      partial_othersubr: {
	  if (!_in_prefix) {
	      _must_expand = true;
	      goto normal;
	  }
	  _prefix_gen.gen_stack(*this, 0);
	  _prefix_gen.gen_command(Cs::cCallothersubr);
	  break;
      }

      case Cs::cCallsubr: {
	  // expand subroutines in line if necessary
	  if (size() < 1)
	      goto normal;
	  int subrno = (int)pop();
	  if (_subr_level < 1) { // otherwise, have already included prefix
	      if (Type1Charstring *cs = _remover->subr_prefix(subrno))
		  run_subr(cs);
	  }
	  if (Type1Charstring *cs = _remover->subr_expander(subrno))
	      run_subr(cs);
	  else {
	      push(subrno);
	      goto normal;
	  }
	  break;
      }

      case Cs::cPop:
	if (ps_size() >= 1)
	    push(ps_pop());
	else if (_in_prefix && ps_size() == 0) {
	    _prefix_gen.gen_stack(*this, 0);
	    _prefix_gen.gen_command(Cs::cPop);
	} else
	    goto normal;
	break;

      case Cs::cDiv:
	if (size() < 2)
	    goto normal;
	top(1) /= top(0);
	pop();
	break;

      case Cs::cReturn:
	return false;

      normal:
      default:
	_main_gen.gen_stack(*this, cmd);
	_main_gen.gen_command(cmd);
	_in_prefix = 0;
	return (cmd != Cs::cEndchar);

    }
    return true;
}


bool
Type1OneMMRemover::run(const Type1Charstring &cs,
		       bool in_subr, bool do_prefix, bool fresh)
{
    _prefix_gen.clear();
    _main_gen.clear();
    _in_subr = in_subr;
    _in_prefix = do_prefix;
    _subr_level = (fresh ? 0 : 1);
    _must_expand = false;
    Vector<double> *scratch = scratch_vector();
    scratch->assign(scratch->size(), UNKDOUBLE);

    CharstringInterp::interpret(_remover->program(), &cs);

    if (in_subr) {
	_main_gen.gen_stack(*this, Cs::cReturn);
	_main_gen.gen_command(Cs::cReturn);
    }
    if (_must_expand)
	return true;
    if (fresh && in_subr) {
	if (_main_gen.length() == 0
	    || (_main_gen.length() == 1 && _main_gen.data()[0] == Cs::cReturn))
	    return true;
    }
    return false;
}

inline bool
Type1OneMMRemover::run_fresh_subr(const Type1Charstring &cs, bool do_prefix)
{
    return run(cs, true, do_prefix, true);
}

inline bool
Type1OneMMRemover::run_fresh_glyph(const Type1Charstring &cs)
{
    return run(cs, false, false, true);
}

inline bool
Type1OneMMRemover::rerun_subr(const Type1Charstring &cs)
{
    return run(cs, true, false, false);
}

Type1Charstring *
Type1OneMMRemover::output_prefix()
{
    if (_prefix_gen.length() > 0) {
	_prefix_gen.gen_command(Cs::cReturn);
	return _prefix_gen.output();
    } else
	return 0;
}

void
Type1OneMMRemover::output_main(Type1Charstring &cs)
{
    _main_gen.output(cs);
}


/*****
 * Type1BadCallRemover
 **/

class Type1BadCallRemover: public CharstringInterp { public:

    Type1BadCallRemover(Type1MMRemover *);

    bool type1_command(int);

    bool run(Type1Charstring &, bool is_subr);

  private:

    Type1CharstringGen _gen;
    Type1MMRemover *_remover;
    bool _is_subr;

};

Type1BadCallRemover::Type1BadCallRemover(Type1MMRemover *remover)
    : CharstringInterp(remover->weight_vector()),
      _gen(remover->precision()), _remover(remover)
{
}

bool
Type1BadCallRemover::type1_command(int cmd)
{
    switch (cmd) {

      case Cs::cCallsubr: {
	  if (size() < 1)
	      goto normal;
	  int subrno = (int)top();
	  if (!get_subr(subrno)) {
	      pop();
	      return false;
	  } else
	      goto normal;
      }

      normal:
      default:
	_gen.gen_stack(*this, 0);
	_gen.gen_command(cmd);
	return (cmd != Cs::cEndchar || _is_subr) && cmd != Cs::cReturn;

    }
}

bool
Type1BadCallRemover::run(Type1Charstring &cs, bool is_subr)
{
    _is_subr = is_subr;
    _gen.clear();
    CharstringInterp::interpret(_remover->program(), &cs);
    _gen.output(cs);
    return error() == errOK;
}


/*****
 * Type1MMRemover
 **/

Type1MMRemover::Type1MMRemover(Type1Font *font, const Vector<double> &wv,
			       int precision, ErrorHandler *errh)
    : _font(font), _weight_vector(wv), _precision(precision),
      _nsubrs(font->nsubrs()),
      _subr_done(_nsubrs, 0),
      _subr_prefix(_nsubrs, (Type1Charstring *)0),
      _must_expand_subr(_nsubrs, 0),
      _hint_replacement_subr(_nsubrs, 0),
      _expand_all_subrs(false), _errh(errh)
{
    itc_errh = _errh;

    // find subroutines needed for hint replacement
    HintReplacementDetector hr(font, wv, 0);
    for (int i = 0; i < _font->nglyphs(); i++)
	if (Type1Subr *g = _font->glyph_x(i))
	    hr.run(font, g->t1cs());
    for (int i = 0; i < _nsubrs; i++)
	if (hr.is_hint_replacement(i))
	    _hint_replacement_subr[i] = 1;

    // don't get rid of first 4 subrs
    for (int i = 0; i < _nsubrs && i < 4; i++)
	_subr_done[i] = 1;
}

Type1MMRemover::~Type1MMRemover()
{
    for (int i = 0; i < _nsubrs; i++)
	if (_subr_prefix[i])
	    delete _subr_prefix[i];
}


Type1Charstring *
Type1MMRemover::subr_prefix(int subrno)
{
    if (subrno < 0 || subrno >= _nsubrs)
	return 0;

    if (!_subr_done[subrno]) {
	_subr_done[subrno] = 1;

	Type1Charstring *subr = _font->subr(subrno);
	if (!subr)
	    return 0;

	Type1OneMMRemover one(this);
	if (one.run_fresh_subr(*subr, !_hint_replacement_subr[subrno]))
	    _must_expand_subr[subrno] = true;
	_subr_prefix[subrno] = one.output_prefix();
	one.output_main(*subr);
    }

    return _subr_prefix[subrno];
}

Type1Charstring *
Type1MMRemover::subr_expander(int subrno)
{
    if (subrno < 0 || subrno >= _nsubrs)
	return 0;
    if (!_subr_done[subrno])
	(void)subr_prefix(subrno);
    if (!_expand_all_subrs && !_must_expand_subr[subrno])
	return 0;
    return _font->subr(subrno);
}

extern "C" {
static int CDECL
sort_permstring_compare(const void *v1, const void *v2)
{
    const PermString *s1 = (const PermString *)v1;
    const PermString *s2 = (const PermString *)v2;
    return strcmp(s1->c_str(), s2->c_str());
}
}

void
Type1MMRemover::run()
{
    Type1OneMMRemover one(this);

    // check subroutines
    for (int subrno = 0; subrno < _nsubrs; subrno++)
	(void)subr_prefix(subrno);

    // expand glyphs
    Vector<PermString> bad_glyphs;
    for (int i = 0; i < _font->nglyphs(); i++) {
	Type1Subr *g = _font->glyph_x(i);
	if (g) {
	    if (one.run_fresh_glyph(g->t1cs())) {
		// Every glyph should be fully expandable without encountering
		// a MM command. If we fail the first time, try again,
		// expanding ALL subroutines. This catches, for example, SUBR
		// 1 { 1 0 return }; GLYPH g { 1 callsubr 2 blend }; This will
		// fail the first time, because `1 callsubr' will be left as a
		// subroutine call, so `1 0' (required arguments to `blend')
		// won't be visible.
		_expand_all_subrs = true;
		if (one.run_fresh_glyph(g->t1cs()))
		    bad_glyphs.push_back(g->name());
		_expand_all_subrs = false;
	    }
	    one.output_main(g->t1cs());
	}
    }

    // remove uncalled subroutines, expand hint replacement subroutines
    HintReplacementDetector hr(_font, _weight_vector, 0);
    for (int i = 0; i < _font->nglyphs(); i++)
	if (Type1Subr *g = _font->glyph_x(i))
	    hr.run(_font, g->t1cs());
    // don't remove first four subroutines!
    for (int subrno = 4; subrno < _nsubrs; subrno++)
	if (hr.call_count(subrno) || _hint_replacement_subr[subrno]) {
	    Type1Charstring *cs = _font->subr(subrno);
	    if (one.rerun_subr(*cs)) {
		_expand_all_subrs = true;
		if (one.rerun_subr(*cs))
		    bad_glyphs.push_back(permprintf("subr %d", subrno));
		_expand_all_subrs = false;
	    }
	    one.output_main(*cs);
	} else
	    _font->remove_subr(subrno);

    // remove calls to removed subroutines
    Type1BadCallRemover bcr(this);
    for (int i = 0; i < _font->nglyphs(); i++)
	if (Type1Subr *g = _font->glyph_x(i))
	    bcr.run(g->t1cs(), false);
    for (int subrno = 4; subrno < _nsubrs; subrno++)
	if (Type1Charstring *cs = _font->subr(subrno))
	    bcr.run(*cs, true);

    // report warnings
    if (bad_glyphs.size()) {
	qsort(&bad_glyphs[0], bad_glyphs.size(), sizeof(PermString), sort_permstring_compare);
	_errh->error("could not fully interpolate the following glyphs:");
	StringAccum sa;
	for (int i = 0; i < bad_glyphs.size(); i++) {
	    PermString n = bad_glyphs[i];
	    bool comma = (i < bad_glyphs.size() - 1);
	    if (sa.length() && sa.length() + 1 + n.length() + comma > 70) {
		_errh->message("  %s", sa.c_str());
		sa.clear();
	    }
	    sa << (sa.length() ? " " : "") << n << (comma ? "," : "");
	}
	_errh->message("  %s", sa.c_str());
    }
}


/*****
 * SubrExpander
 **/

class SubrExpander : public CharstringInterp { public:

    SubrExpander();

    void set_renumbering(const Vector<int> *v) { _renumbering = v; }

    bool type1_command(int);

    bool run(Type1Font *, Type1Charstring &);

  private:

    Type1CharstringGen _gen;
    const Vector<int> *_renumbering;
    int _subr_level;

};

SubrExpander::SubrExpander()
    : CharstringInterp(), _gen(0), _renumbering(0)
{
}

bool
SubrExpander::type1_command(int cmd)
{
    switch (cmd) {

      case Cs::cCallsubr: {
	  if (size() < 1)
	      goto unknown;
	  int subrno = (int)top(0);
	  int renumber_subrno = (subrno >= 0 && subrno < _renumbering->size() ? (*_renumbering)[subrno] : subrno);
	  if (renumber_subrno >= 0) {
	      top(0) = renumber_subrno;
	      goto unknown;
	  }
	  pop();
	  if (Charstring *subr_cs = get_subr(subrno)) {
	      _subr_level++;
	      subr_cs->process(*this);
	      _subr_level--;
	  }
	  return !done();
      }

      case Cs::cEndchar:
	set_done();
	goto end_cs;

      case Cs::cReturn:
	if (_subr_level)
	    return false;
	goto end_cs;

      end_cs:
	_gen.gen_stack(*this, cmd);
	_gen.gen_command(cmd);
	return false;

      default:
      unknown:
	_gen.gen_stack(*this, cmd);
	_gen.gen_command(cmd);
	break;

    }
    return true;
}

bool
SubrExpander::run(Type1Font *font, Type1Charstring &cs)
{
    _gen.clear();
    _subr_level = 0;
    CharstringInterp::interpret(font, &cs);
    _gen.output(cs);
    return error() == errOK;
}


/*****
 * Type1SubrRemover
 **/

Type1SubrRemover::Type1SubrRemover(Type1Font *font, ErrorHandler *errh)
    : _font(font), _nsubrs(font->nsubrs()),
      _renumbering(_nsubrs, REMOVABLE), _cost(_nsubrs, 0),
      _save_count(0), _nonexist_count(0), _errh(errh)
{
    // find subroutines needed for hint replacement
    HintReplacementDetector hr(font, 2);
    for (int i = 0; i < _font->nglyphs(); i++) {
	Type1Subr *g = _font->glyph_x(i);
	if (g)
	    hr.run(_font, g->t1cs());
    }

    // save necessary subroutines
    for (int i = 0; i < 4; i++) {
	_renumbering[i] = i;
	_save_count++;
    }
    // save hint-replacement subroutines
    for (int i = 0; i < _nsubrs; i++) {
	Type1Subr *cs = _font->subr_x(i);
	if (!cs) {
	    _renumbering[i] = DEAD;
	    _nonexist_count++;
	} else if (hr.is_hint_replacement(i)) {
	    _renumbering[i] = i;
	    _save_count++;
	} else
	    _cost[i] = hr.call_count(i) * (cs->t1cs().length() - (i <= 107 ? 2 : 3));
    }
}

Type1SubrRemover::~Type1SubrRemover()
{
}

static Vector<int> *sort_keys;

extern "C" {
static int CDECL
sort_permute_compare(const void *v1, const void *v2)
{
    const int *i1 = (const int *)v1;
    const int *i2 = (const int *)v2;
    return (*sort_keys)[*i1] - (*sort_keys)[*i2];
}
}

bool
Type1SubrRemover::run(int lower_to)
{
    if (lower_to < 0)
	lower_to = _nsubrs;
    if (lower_to < _save_count) {
	_errh->warning("reducing %s to minimum number of subroutines (%d)",
		       _font->font_name().c_str(), _save_count - _nonexist_count);
	lower_to = _save_count;
    }
    int to_remove = _nsubrs - _nonexist_count - lower_to;
    if (to_remove < 0)
	to_remove = 0;

    // multiply by lost bytes per call
    Vector<int> permute;
    for (int i = 0; i < _nsubrs; i++)
	permute.push_back(i);

    // sort them by least frequent use -> most frequent use
    sort_keys = &_cost;
    qsort(&permute[0], _nsubrs, sizeof(int), sort_permute_compare);

    // mark first portion of `permute' to be removed
    int removed = 0;
    for (int i = 0; i < _nsubrs; i++) {
	int p = permute[i];
	if (_renumbering[p] == REMOVABLE && removed < to_remove) {
	    _renumbering[p] = DEAD;
	    removed++;
	}
    }

    // renumber the rest
    int renumber_pos = 0;
    for (int i = 0; i < _nsubrs; i++)
	if (_renumbering[i] == REMOVABLE) { // save it
	    while (_renumbering[renumber_pos] >= 0)
		renumber_pos++;
	    _renumbering[i] = renumber_pos++;
	}
    SubrExpander rem0;
    rem0.set_renumbering(&_renumbering);

    // go through and change them all
    for (int i = 0; i < _nsubrs; i++) {
	Type1Subr *s = _font->subr_x(i);
	if (s && _renumbering[i] >= 0)
	    rem0.run(_font, s->t1cs());
    }
    for (int i = 0; i < _font->nglyphs(); i++)
	if (Type1Subr *g = _font->glyph_x(i))
	    rem0.run(_font, g->t1cs());

    // actually remove subroutines
    _font->renumber_subrs(_renumbering);

    return true;
}