-
Notifications
You must be signed in to change notification settings - Fork 25
/
cpdfdrawcontrol.ml
323 lines (261 loc) · 9.25 KB
/
cpdfdrawcontrol.ml
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
(* Drawing operations. *)
open Pdfutil
open Cpdferror
let embed_font = ref (fun () -> Printf.printf "BAD *\n%!"; Cpdfembed.ExistingNamedFont)
let setdrawing = ref (fun () -> Printf.printf "BAD **\n%!")
let getfontname = ref (fun () -> Printf.printf "BAD ***\n%!"; "")
let getfontsize = ref (fun () -> Printf.printf "BAD ****\n%!"; 0.)
let setfontname = ref (fun _ -> Printf.printf "BAD *****\n%!")
let setfontsize = ref (fun _ -> Printf.printf "BAD ******\n%!")
let loadttf = ref (fun _ -> Printf.printf "BAD *******\n%!")
let setembedstd14 = ref (fun _ _ -> Printf.printf "BAD ********\n%!")
let ttfs = null_hash ()
let loadttfseparate name filename = !loadttf (name ^ "=" ^ filename)
let fontpack_initialised = ref false
let drawops = ref [("_MAIN", [])]
let startxobj n =
drawops := (n, [])::!drawops
let xobj_bbox = ref (0., 0., 1000., 1000.)
let xobjbbox s =
xobj_bbox := Cpdfcoord.parse_rectangle (Pdf.empty ()) s
let addop o =
match !drawops with
| (n, ops)::t -> drawops := (n, (o::ops))::t
| [] -> error "no drawops"
let endxobj () =
match !drawops with
| (n, ops)::t ->
drawops := t;
let a, b, c, d = !xobj_bbox in
addop (Cpdfdraw.FormXObject (a, b, c, d, n, rev ops))
| [] ->
error "too many -end-xobj or -et"
let addbt () =
drawops := ("_TEXT", [])::!drawops
let addet () =
match !drawops with
| ("_TEXT", ops)::t ->
drawops := t;
addop (Cpdfdraw.TextSection (rev ops))
| _ -> error "not in a text section at -et"
let push () =
drawops := ("_PUSH", [])::!drawops
let pop () =
match !drawops with
| ("_PUSH", ops)::t ->
drawops := t;
addop (Cpdfdraw.Qq (rev ops))
| _ -> error "not in a pushed section at -pop"
let readfloats s = map float_of_string (String.split_on_char ' ' s)
let parse_colour s =
match lookup (String.lowercase_ascii s) Cpdfcolours.colours with
| Some c ->
let r = float_of_int ((c land 0xFF0000) lsr 16) /. 255. in
let g = float_of_int ((c land 0x00FF00) lsr 8) /. 255. in
let b = float_of_int (c land 0x0000FF) /. 255. in
Cpdfaddtext.RGB (r, g, b)
| None ->
let getnum = function
| Pdfgenlex.LexInt i -> float i
| Pdfgenlex.LexReal f -> f
| _ -> error "Bad color"
in
match Pdfgenlex.lex_string s with
| [g] -> Cpdfaddtext.Grey (getnum g)
| [r; g; b] -> Cpdfaddtext.RGB (getnum r, getnum g, getnum b)
| [c; y; m; k] -> Cpdfaddtext.CYMK (getnum c, getnum y, getnum m, getnum k)
| _ -> error "Bad color"
let col_of_string s =
match parse_colour s with
| Cpdfaddtext.RGB (r, g, b) -> Cpdfdraw.RGB (r, g, b)
| Cpdfaddtext.Grey g -> Cpdfdraw.Grey g
| Cpdfaddtext.CYMK (c, y, m, k) -> Cpdfdraw.CYMK (c, y, m, k)
| exception _ -> Cpdfdraw.NoCol
let setstroke s =
addop (Cpdfdraw.SetStroke (col_of_string s))
let setfill s =
addop (Cpdfdraw.SetFill (col_of_string s))
let addrect s =
let x, y, w, h = Cpdfcoord.parse_rectangle (Pdf.empty ()) s in
addop (Cpdfdraw.Rect (x, y, w, h))
let addto s =
let x, y = Cpdfcoord.parse_coordinate (Pdf.empty ()) s in
addop (Cpdfdraw.To (x, y))
let addline s =
let x, y = Cpdfcoord.parse_coordinate (Pdf.empty ()) s in
addop (Cpdfdraw.Line (x, y))
let addbezier s =
match readfloats s with
| [a; b; c; d; e; f] -> addop (Cpdfdraw.Bezier (a, b, c, d, e, f))
| _ -> error "-bez requires six numbers"
| exception _ -> error "malformed -bez"
let addbezier23 s =
match readfloats s with
| [a; b; c; d] -> addop (Cpdfdraw.Bezier23 (a, b, c, d))
| _ -> error "-bez23 requires four numbers"
| exception _ -> error "malformed -bez23"
let addbezier13 s =
match readfloats s with
| [a; b; c; d] -> addop (Cpdfdraw.Bezier13 (a, b, c, d))
| _ -> error "-bez13 requires four numbers"
| exception _ -> error "malformed -bez13"
let addcircle s =
match readfloats s with
| [x; y; r] ->
let _, _, segs = hd (snd (Cpdfshape.circle x y r)) in
(match segs with
| Cpdfshape.Bezier ((a, b), _, _, _)::_ -> addop (Cpdfdraw.To (a, b))
| _ -> assert false);
iter
(function
| Cpdfshape.Bezier (_, (c, d), (e, f), (g, h)) -> addop (Cpdfdraw.Bezier (c, d, e, f, g, h))
| Cpdfshape.Straight _ -> assert false)
segs
| _ -> error "-circle requires three numbers"
| exception _ -> error "malformed -circle"
let stroke () =
addop Cpdfdraw.Stroke
let fill () =
addop Cpdfdraw.Fill
let fillevenodd () =
addop Cpdfdraw.FillEvenOdd
let strokefill () =
addop Cpdfdraw.FillStroke
let strokefillevenodd () =
addop Cpdfdraw.FillStrokeEvenOdd
let clip () =
addop Cpdfdraw.Clip
let clipevenodd () =
addop Cpdfdraw.ClipEvenOdd
let closepath () =
addop Cpdfdraw.ClosePath
let setthickness s =
try addop (Cpdfdraw.SetLineThickness (float_of_string s)) with
_ -> error "Thickness must be a number"
let setcap s =
let num =
match s with
| "butt" -> 0
| "round" -> 1
| "square" -> 2
| _ -> error "Unknown cap type"
in
addop (Cpdfdraw.SetLineCap num)
let setjoin s =
let num =
match s with
| "miter" -> 0
| "round" -> 1
| "bevel" -> 2
| _ -> error "Unknown join type"
in
addop (Cpdfdraw.SetLineJoin num)
let setmiter s =
try addop (Cpdfdraw.SetMiterLimit (float_of_string s)) with
_ -> error "Miter limit must be a number"
let setdash s =
try
let x, y =
let nums = readfloats s in all_but_last nums, last nums
in
addop (Cpdfdraw.SetDashPattern (x, y))
with
_ -> error "Dash pattern elements must one or more numbers"
let setmatrix s =
match readfloats s with
| [a; b; c; d; e; f] ->
addop (Cpdfdraw.Matrix {Pdftransform.a = a; Pdftransform.b = b; Pdftransform.c = c;
Pdftransform.d = d; Pdftransform.e = e; Pdftransform.f = f})
| _ -> error "Matrix must have six numbers"
| exception _ -> error "Matrix elements must be numbers"
let setmtranslate s =
match readfloats s with
| [a; b] -> addop (Cpdfdraw.Matrix (Pdftransform.matrix_of_transform [Pdftransform.Translate (a, b)]))
| _ | exception _ -> error "-mtrans takes two numbers"
let setmrotate s =
match readfloats s with
| [a; b; c] -> addop (Cpdfdraw.Matrix (Pdftransform.matrix_of_transform [Pdftransform.Rotate ((a, b), c)]))
| _ | exception _ -> error "-mrot takes three numbers"
let setmscale s =
match readfloats s with
| [a; b; c; d] -> addop (Cpdfdraw.Matrix (Pdftransform.matrix_of_transform [Pdftransform.Scale ((a, b), c, d)]))
| _ | exception _ -> error "-mscale takes four numbers"
let setmshearx s =
match readfloats s with
| [a; b; c] -> addop (Cpdfdraw.Matrix (Pdftransform.matrix_of_transform [Pdftransform.ShearX ((a, b), c)]))
| _ | exception _ -> error "-mshearx takes three numbers"
let setmsheary s =
match readfloats s with
| [a; b; c] -> addop (Cpdfdraw.Matrix (Pdftransform.matrix_of_transform [Pdftransform.ShearY ((a, b), c)]))
| _ | exception _ -> error "-msheary takes three numbers"
let usexobj s =
addop (Cpdfdraw.Use s)
let addjpeg ?data n =
match data with
| Some d ->
addop
(Cpdfdraw.ImageXObject
(n, fst (Cpdfimage.obj_of_jpeg_data (Pdfio.bytes_of_raw d))))
| None ->
let name, filename =
match String.split_on_char '=' n with
| [name; filename] -> name, filename
| _ -> error "addjpeg: bad file specification"
in
try
let data = Pdfio.bytes_of_string (contents_of_file filename) in
addop (Cpdfdraw.ImageXObject (name, fst (Cpdfimage.obj_of_jpeg_data data)))
with
_ -> error "addjpeg: could not load JPEG"
let addpng ?data n =
match data with
| Some d ->
addop
(Cpdfdraw.ImageXObject
(n, fst (Cpdfimage.obj_of_png_data (Pdfio.bytes_of_raw d))))
| None ->
let name, filename =
match String.split_on_char '=' n with
| [name; filename] -> name, filename
| _ -> error "addpng: bad file specification"
in
let data = Pdfio.bytes_of_string (contents_of_file filename) in
addop (Cpdfdraw.ImageXObject (name, fst (Cpdfimage.obj_of_png_data data)))
let addimage s =
addop (Cpdfdraw.Image s)
let addnewpage s =
addop Cpdfdraw.NewPage
let addopacity f =
addop (Cpdfdraw.Opacity f)
let addsopacity f =
addop (Cpdfdraw.SOpacity f)
let addleading f =
addop (Cpdfdraw.Leading f)
let addcharspace f =
addop (Cpdfdraw.CharSpace f)
let addwordspace f =
addop (Cpdfdraw.WordSpace f)
let addtextscale f =
addop (Cpdfdraw.TextScale f)
let addrendermode i =
addop (Cpdfdraw.RenderMode i)
let addrise f =
addop (Cpdfdraw.Rise f)
let addnewline () =
addop Cpdfdraw.Newline
let add_default_fontpack fontname =
if not !fontpack_initialised then
begin
addop (Cpdfdraw.FontPack (fontname, !embed_font (), null_hash ()));
set fontpack_initialised
end
let addtext s =
begin match !drawops with _::_::_ -> () | _ -> error "-text must be in a -bt / -et section" end;
add_default_fontpack (!getfontname ());
addop (Cpdfdraw.Font (!getfontname (), !getfontsize ()));
addop (Cpdfdraw.Text s)
let addspecialtext s =
begin match !drawops with _::_::_ -> () | _ -> error "-stext must be in a -bt / -et section" end;
add_default_fontpack (!getfontname ());
addop (Cpdfdraw.Font (!getfontname (), !getfontsize ()));
addop (Cpdfdraw.SpecialText s)