-
Notifications
You must be signed in to change notification settings - Fork 2
/
cmapi.in
195 lines (178 loc) · 4.2 KB
/
cmapi.in
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
#! /usr/bin/perl
use strict;
my $copyright = <<EOT;
// Copyright (c) 2009 Helmar Wodtke. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// The MIT License is an OSI approved license and can
// be found at
// http://www.opensource.org/licenses/mit-license.php
EOT
while (<DATA>) {
last if /^%%\s*$/;
s/%copyright%/$copyright/;
print $_;
}
sub func {
my $n = shift;
return "" if $n == 0;
return " a := t.St.Pop(); _ = a;\n"
if $n == 1;
return " a := t.St.Drop($n); _ = a;\n"
}
my %funcs;
my $func;
while (<DATA>) {
last if /^%%\s*$/;
if (/^(\S+)\s+(\S+)\s+(\S+)\s*$/) {
my ($st, $fn, $fl) = ($1, $2, $3);
if ($fl eq "+") {
$funcs{"begin$fn"} = func 0;
$funcs{"begin$fn"} .= " t.Args = t.St.Drop($st);\n";
$funcs{"begin$fn"} .= " t.Marker = t.St.Depth();\n";
$fn = "end$fn";
$st = 0;
}
$func = $fn;
$funcs{$func} = func $st;
if ($fl eq "+") {
$funcs{$func} .= " a := t.St.Drop(t.St.Depth() - t.Marker); _ = a;\n";
}
next;
}
if (/^\s+[+](\s+.*)$/) {
my $n = $1;
$funcs{$func} =~ s/ _ = a;\n$/\n/s;
$funcs{$func} .= "$n\n";
next;
}
}
my @funcs;
for (sort keys %funcs) {
push @funcs, " \"$_\" : func (t *CharMapperI) {\n$funcs{$_} }";
}
print "var Ops = map[string]func (t *CharMapperI) {\n";
print join(",\n", @funcs);
print ",\n}\n\n";
while (<DATA>) {
last if /^%%\s*$/;
print $_;
}
__DATA__
%copyright%
package cmapi
// CMap "interpreter" - this PS btw.
// WARNING: This file is automatically generated!
// It makes no sense to change anything here.
import (
"ps";
"util";
"cmapt";
"fancy";
"xchar";
)
type CharMapperT struct {
Ranges, Uni *cmapt.CMapT;
}
func New() *CharMapperT {
r := new(CharMapperT);
r.Ranges = cmapt.New();
r.Uni = cmapt.New();
return r;
}
type CharMapperI struct {
Target *CharMapperT;
St stacks.Stack;
Dic map[string][]byte;
Marker int;
Args [][]byte;
}
func NewInterpreter(t *CharMapperT) *CharMapperI {
r := new(CharMapperI);
r.Target = t;
r.St = stacks.NewStack(1024);
r.Dic = make(map[string][]byte);
return r;
}
%%
1 dict -
+ t.St.Push([]byte{'?'});
+ _ = a;
1 begin -
0 end -
1 dup -
+ t.St.Push(a);
+ t.St.Push(a);
1 pop -
2 exch -
+ t.St.Push(a[1]);
+ t.St.Push(a[0]);
2 def -
+ t.Dic[string(a[0])] = a[1];
0 currentdict -
+ t.St.Push([]byte{'?'});
3 defineresource -
+ t.St.Push([]byte{'?'});
+ _ = a;
2 findresource -
+ t.St.Push([]byte{'?'});
+ _ = a;
1 usecmap -
1 usefont -
1 usematrix +
1 codespacerange +
+ for k := 0; k < len(a); k += 2 {
+ to, l := ps.StrIntL(ps.String(a[k + 1]));
+ t.Target.Ranges.AddDef(int(a[k][0]), int(a[k+1][0]) + 1, l);
+ t.Target.Ranges.AddDef(ps.StrInt(ps.String(a[k])), to + 1, l); // just not used.
+ }
0 begincmap -
0 endcmap -
1 bfchar +
+ for k := 0; k < len(a); k += 2 {
+ t.Target.Uni.Add(ps.StrInt(ps.String(a[k])), ps.StrInt(ps.String(a[k+1])));
+ }
1 bfrange +
+ for k := 0; k < len(a); k += 3 {
+ // leaving the array expression as it is: invalidate - we do not have char names to unicode now
+ t.Target.Uni.AddRange(ps.StrInt(ps.String(a[k])),
+ ps.StrInt(ps.String(a[k+1])), ps.StrInt(ps.String(a[k+2])));
+ }
1 cidchar +
1 cidrange +
1 notdefchar +
1 notdefrange +
2 rearrangedfont +
%%
func Read(rdr fancy.Reader) (r *CharMapperT) {
r = New();
if rdr == nil { // make identity setup
r.Uni.AddRange(0, 256, 0);
r.Ranges.AddDef(0, 256, 1);
return;
}
cm := NewInterpreter(r);
for {
t, _ := ps.Token(rdr);
if len(t) == 0 {
break
}
if f, ok := Ops[string(t)]; ok {
f(cm)
} else {
cm.St.Push(t)
}
}
return;
}
func Decode(s []byte, to *CharMapperT) (r []byte) {
r = make([]byte, len(s)*6);
p := 0;
for k := 0; k < len(s); {
l := to.Ranges.Code(int(s[k]));
a := ps.StrInt(s[k : k+l]);
k += l;
p += xchar.EncodeRune(to.Uni.Code(a), r[p:len(r)]);
}
return r[0:p];
}