-
Notifications
You must be signed in to change notification settings - Fork 28
/
pdfannot.ml
312 lines (298 loc) · 10.7 KB
/
pdfannot.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
(* Read and Write Annotations *)
open Pdfutil
(* Annotation Border Styles *)
type style =
| NoStyle
| Solid
| Dashed
| Beveled
| Inset
| UnderlineStyle
type border =
{width : float;
vradius : float;
hradius : float;
style : style;
dasharray : int array}
type subtype =
| Text
| Link
| FreeText
| Line
| Square
| Circle
| Polygon
| PolyLine
| Highlight
| Underline
| Squiggly
| StrikeOut
| Stamp
| Caret
| Ink
| Popup of t
| FileAttachment
| Sound
| Movie
| Widget
| Screen
| PrinterMark
| TrapNet
| Watermark
| ThreeDee
| Unknown of string
(* Main type. 'rest' contains the raw annotation dictionary with the exception
of the entries corresponding to the other items in the record. *)
and t =
{subtype : subtype;
annot_contents : string option;
subject : string option;
rectangle : float * float * float * float;
border : border;
colour : (int * int * int) option;
annotrest : Pdf.pdfobject}
(* Read a single annotation *)
let rec read_annotation pdf annot =
let subtype =
match Pdf.lookup_direct pdf "/Subtype" annot with
| Some (Pdf.Name "/Text") -> Text
| Some (Pdf.Name "/FreeText") -> FreeText
| Some (Pdf.Name "/Popup") ->
(* Look up /Parent. If exists, include it *)
begin match Pdf.direct pdf annot with
| Pdf.Dictionary d ->
begin match lookup "/Parent" d with
| Some (Pdf.Indirect i) ->
Popup (read_annotation pdf (Pdf.Indirect i))
| _ -> Unknown ""
end
| _ -> raise (Pdf.PDFError "read_annotation failed")
end
| Some (Pdf.Name "/Stamp") -> Stamp
| Some (Pdf.Name "/Link") -> Link
| Some (Pdf.Name "/Line") -> Line
| Some (Pdf.Name "/Square") -> Square
| Some (Pdf.Name "/Circle") -> Circle
| Some (Pdf.Name "/Polygon") -> Polygon
| Some (Pdf.Name "/PolyLine") -> PolyLine
| Some (Pdf.Name "/Highlight") -> Highlight
| Some (Pdf.Name "/Underline") -> Underline
| Some (Pdf.Name "/Squiggly") -> Squiggly
| Some (Pdf.Name "/StrikeOut") -> StrikeOut
| Some (Pdf.Name "/Caret") -> Caret
| Some (Pdf.Name "/Ink") -> Ink
| Some (Pdf.Name "/FileAttachment") -> FileAttachment
| Some (Pdf.Name "/Sound") -> Sound
| Some (Pdf.Name "/Movie") -> Movie
| Some (Pdf.Name "/Widget") -> Widget
| Some (Pdf.Name "/Screen") -> Screen
| Some (Pdf.Name "/PrinterMark") -> PrinterMark
| Some (Pdf.Name "/TrapNet") -> TrapNet
| Some (Pdf.Name "/Watermark") -> Watermark
| Some (Pdf.Name "/3D") -> ThreeDee
| Some (Pdf.Name n) -> Unknown n
| _ -> Unknown ""
in let contents =
match Pdf.lookup_direct pdf "/Contents" annot with
| Some (Pdf.String s) -> Some s
| _ -> None
in let subject =
match Pdf.lookup_direct pdf "/Subj" annot with
| Some (Pdf.String s) -> Some s
| _ -> None
in let rectangle =
Pdf.parse_rectangle pdf (Pdf.lookup_fail "No /rect in annot" pdf "/Rect" annot)
in let border =
match Pdf.lookup_direct pdf "/BS" annot with
| Some bsdict ->
let width =
match Pdf.lookup_direct pdf "/W" bsdict with
| Some x -> Pdf.getnum pdf x
| _ -> 1.
in let style =
match Pdf.lookup_direct pdf "/S" bsdict with
| Some (Pdf.Name "/S") -> Solid
| Some (Pdf.Name "/D") -> Dashed
| Some (Pdf.Name "/B") -> Beveled
| Some (Pdf.Name "/I") -> Inset
| Some (Pdf.Name "/U") -> UnderlineStyle
| _ -> NoStyle
in let dasharray =
match Pdf.lookup_direct pdf "/D" bsdict with
| Some (Pdf.Array dash) ->
Array.of_list
(map int_of_float (map (Pdf.getnum pdf) (map (Pdf.direct pdf) dash)))
| _ -> [||]
in
{width = width;
vradius = 0.;
hradius = 0.;
style = style;
dasharray = dasharray}
| None ->
match Pdf.lookup_direct pdf "/Border" annot with
| Some (Pdf.Array [h; v; w]) ->
{width = Pdf.getnum pdf (Pdf.direct pdf w);
vradius = Pdf.getnum pdf (Pdf.direct pdf v);
hradius = Pdf.getnum pdf (Pdf.direct pdf h);
style = NoStyle;
dasharray = [||]}
| Some (Pdf.Array [h; v; w; Pdf.Array dash]) ->
{width = Pdf.getnum pdf (Pdf.direct pdf w);
vradius = Pdf.getnum pdf (Pdf.direct pdf v);
hradius = Pdf.getnum pdf (Pdf.direct pdf h);
style = NoStyle;
dasharray =
Array.of_list
(map
int_of_float
(map (Pdf.getnum pdf) (map (Pdf.direct pdf) dash)))}
| _ ->
{width = 1.;
vradius = 0.;
hradius = 0.;
style = NoStyle;
dasharray = [||]}
in let colour =
match Pdf.lookup_direct pdf "/C" annot with
| Some (Pdf.Array [r; g; b]) ->
Some (int_of_float (Pdf.getnum pdf (Pdf.direct pdf r)),
int_of_float (Pdf.getnum pdf (Pdf.direct pdf g)),
int_of_float (Pdf.getnum pdf (Pdf.direct pdf b)))
| _ -> None
in let annotrest =
match Pdf.direct pdf annot with
| Pdf.Dictionary entries ->
Pdf.Dictionary
(lose
(fun (k, _) -> mem k ["/Subtype"; "/Contents"; "/Rect"; "/Border"; "/Subj"; "/BS"; "/C"])
entries)
| _ -> raise (Pdf.PDFError "Bad annotation dictionary")
in
{subtype = subtype;
annot_contents = contents;
subject = subject;
rectangle = rectangle;
border = border;
colour = colour;
annotrest = annotrest}
let get_popup_parent pdf annotation =
match Pdf.direct pdf annotation with
| Pdf.Dictionary d ->
begin match lookup "/Parent" d with
| Some (Pdf.Indirect i) -> Some i
| _ -> None
end
| _ -> raise (Pdf.PDFError "Pdfannot.get_popup_parent: not a dictionary")
(* Read the annotations from a page. *)
let annotations_of_page pdf page =
match Pdf.lookup_direct pdf "/Annots" page.Pdfpage.rest with
| Some (Pdf.Array annotations) ->
(* We don't read annotations which are parents of Popup annotations - they
will be caught anyway. This seems to be the right thing to do, but will
need more advice. *)
let popup_parents =
option_map (get_popup_parent pdf) annotations
in
map
(read_annotation pdf)
(lose
(function Pdf.Indirect i -> mem i popup_parents | _ -> false)
annotations)
| _ -> []
(* Add an annotation to a page *)
let string_of_subtype = function
| Text -> "/Text" | Link -> "/Link" | FreeText -> "/FreeText" | Line -> "/Line"
| Square -> "/Square" | Circle -> "/Circle" | Polygon -> "/Polygon"
| PolyLine -> "/PolyLine" | Highlight -> "/Highlight" | Underline -> "/Underline"
| Squiggly -> "/Squiggly" | StrikeOut -> "/StrikeOut" | Stamp -> "/Stamp"
| Caret -> "/Caret" | Ink -> "/Ink" | FileAttachment -> "/FileAttachment" | Sound -> "/Sound"
| Movie -> "/Movie" | Widget -> "/Widget" | Screen -> "/Screen"
| PrinterMark -> "/PrinterMark" | TrapNet -> "/TrapNet" | Watermark -> "/Watermark"
| Unknown _ -> "/Unknown" | Popup _ -> "/Popup" | ThreeDee -> "/3D"
let obj_of_annot t =
let d =
["/Subtype", Pdf.Name (string_of_subtype t.subtype);
"/Contents", (match t.annot_contents with None -> Pdf.Null | Some s -> Pdf.String s);
"/Rect", (let a, b, c, d = t.rectangle in Pdf.Array [Pdf.Real a; Pdf.Real b; Pdf.Real c; Pdf.Real d]);
"/Border", match t.border.dasharray with
| [||] -> Pdf.Array [Pdf.Real t.border.hradius; Pdf.Real t.border.vradius; Pdf.Real t.border.width]
| _ -> raise (Pdf.PDFError "non-empty dash array unsupported")]
in
let d = match t.annotrest with
| Pdf.Null -> d
| Pdf.Dictionary d' -> d @ d'
| _ -> raise (Pdf.PDFError "Bad annotation dictionary") in
let colorize d = match t.colour with
| None -> d
| Some (r,g,b) -> (("/C", Pdf.Array [Pdf.Integer r; Pdf.Integer g; Pdf.Integer b]))::d
in
let subject d = match t.subject with
| None -> d
| Some s -> (("/Subj", Pdf.String s)::d)
in
Pdf.Dictionary (subject (colorize d))
let make_border ?(vradius=0.0) ?(hradius=0.0) ?(style=NoStyle) ?(dasharray = [||]) width =
{vradius; hradius; style; dasharray; width}
let make ?content ?(border=make_border 0.0) ?(rectangle=(0., 0., 0., 0.)) ?colour ?subject subtype =
{annot_contents = content;
border;
rectangle;
colour;
subject;
subtype;
annotrest = Pdf.Null}
let add_annotation pdf page anno =
let obj = obj_of_annot anno in
match Pdf.lookup_direct pdf "/Annots" page.Pdfpage.rest with
| Some (Pdf.Array annotations) ->
{page with
Pdfpage.rest = Pdf.add_dict_entry page.Pdfpage.rest "/Annots" (Pdf.Array (obj::annotations))}
| Some _ ->
raise (Pdf.PDFError "Bad annotation dictionary")
| None ->
{page with
Pdfpage.rest = Pdf.add_dict_entry page.Pdfpage.rest "/Annots" (Pdf.Array [obj])}
(* Apply transformations to any annotations in /Annots (i.e their /Rect and
/QuadPoints entries). Also as a best-effort service, altering other
coordinates, like the endpoints /L in a line annotation. *)
let transform_annotations pdf transform rest =
match Pdf.lookup_direct pdf "/Annots" rest with
| Some (Pdf.Array annots) ->
(* Always indirect references, so alter in place *)
iter
(function
| Pdf.Indirect i ->
let annot = Pdf.lookup_obj pdf i in
let rect' =
match Pdf.lookup_direct pdf "/Rect" annot with
| Some rect -> Some (Pdf.transform_rect pdf transform rect)
| None -> None (* malformed, but seen in the wild *)
in
let quadpoints' =
match Pdf.lookup_direct pdf "/QuadPoints" annot with
| Some qp -> Some (Pdf.transform_quadpoints pdf transform qp)
| None -> None
in
let line' =
match Pdf.lookup_direct pdf "/L" annot with
| Some rect -> Some (Pdf.transform_rect pdf transform rect)
| _ -> None
in
let annot =
match rect' with None -> annot | Some rect' -> Pdf.add_dict_entry annot "/Rect" rect'
in
let annot =
match quadpoints' with
| Some qp -> Pdf.add_dict_entry annot "/QuadPoints" qp
| None -> annot
in
let annot =
match line' with
| Some l -> Pdf.add_dict_entry annot "/L" l
| None -> annot
in
Pdf.addobj_given_num pdf (i, annot)
| _ -> Pdfe.log "transform_annotations: not indirect\n")
annots
| _ -> ()