-
Notifications
You must be signed in to change notification settings - Fork 1
/
fdf2xfdf.ps
749 lines (677 loc) · 30.4 KB
/
fdf2xfdf.ps
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
% MIT License
%
% Copyright (c) 2017 Notengrafik Berlin
%
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the "Software"), to deal
% in the Software without restriction, including without limitation the rights
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
% copies of the Software, and to permit persons to whom the Software is
% furnished to do so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in all
% copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
% SOFTWARE.
% TODO: Make everything more robust. Expect broken FDF!
% Temporary dictionary for making entries immediately evaluable using "//"
<<
/objectsByNo <<>>
/stdout (%stdout) (w) file
/stderr (%stderr) (w) file
/stringbuffer 128 string def
/blackColor (#000000)
/fdf2XfdfAnnotNames <<
% TODO: This list is not at all exhaustive
/Line (line)
/Popup (popup)
/FreeText (freetext)
/Square (square)
/Text (text)
/Highlight (highlight)
/Circle (circle)
/Ink (ink)
/Polygon (polygon)
/PolyLine (polyline)
>>
/pdf2XMLCharMap 256 array
dup 32 1 255 { % array array charcode
1 string dup 0 % array array charcode string string 0
3 index % array array charcode string string 0 charcode
put % array array charcode string
put dup % array array
} for
<<
% PDFDocEncoding is based on ISO 8859-1, with some additions.
% We list the characters that aren't identical in PDF and ISO 8859-1
% explicitly. We use ISO 8859-1 for our output.
9 (	) % CHARACTER TABULATION
10 (
) % LINE FEED
13 (
) % CARRIAGE RETURN
24 (˘) % BREVE
25 (ˇ) % CARON
26 (ˆ) % MODIFIER ACCENT
27 (˙) % DOT ABOVE
28 (˝) % DOUBLE ACUTE ACCENT
29 (˛) % OGONEK
30 (˚) % RING ABOVE
31 (˜) % SMALL TILDE
34 (") % QUOTATION MARK
38 (&) % AMPERSAND
60 (<) % LESS THAN SIGN
127 null
128 (•) % BULLET
129 (†) % DAGGER
130 (‡) % DOUBLE DAGGER
131 (…) % HORIZONTAL ELLIPSIS
132 (—) % EM DASH
133 (–) % EN DASH
134 (ƒ) %
135 (⁄) % FRACTION SLASH (solidus)
136 (‹) % SINGLE LEFT-POINTING QUOTATION MARK
137 (›) % SINGLE RIGHT-POINTING QUOTATION MARK
138 (−) %
139 (‰) % PER MILLE SIGN
140 („) % DOUBLE LOW-9 (quotedblbase)
141 (“) % LEFT DOUBLE QUOTATION MARK (double quote left)
142 (”) % RIGHT DOUBLE (quotedblright)
143 (‘) % LEFT SINGLE (quoteleft) QUOTATION MARK
144 (’) % RIGHT SINGLE (quoteright) QUOTATION MARK
145 (‚) % SINGLE LOW-9 (quotesinglbase) QUOTATION MARK
146 (™) % TRADE MARK SIGN
147 (fi) % LATIN SMALL LIGATURE FI
148 (fl) % LATIN SMALL LIGATURE FL
149 (Ł) % LATIN CAPITAL LETTER L WITH STROKE
150 (Œ) % LATIN CAPITAL LIGATURE OE
151 (Š) % LATIN CAPITAL LETTER S WITH CARON
152 (Ÿ) % LATIN CAPITAL DIAERESIS
153 (Ž) % LATIN CAPITAL LETTER Z WITH CARON
154 (ı) % LATIN SMALL LETTER DOTLESS I l
155 (ł) % LATIN SMALL LETTER L WITH STROKE
156 (œ) % LATIN SMALL LIGATURE OE
157 (š) % LATIN SMALL LETTER S WITH CARON
158 (ž) % LATIN SMALL LETTER Z WITH CARON
159 null
160 (€) % EURO SIGN
173 null
>> { % array array charcode xmlString
put dup % array array
} forall
pop
/string2hexstring { % string
dup length 2 mul string % string fullHexstring
dup 3 -1 roll % fullHexstring fullHexstring string
{ % forall % fullHexstring remainingHexstring char
% Iterate over "half-bytes" - most significant half first
2 copy % fullHexstring remainingHexstring char remainingHexstring char
16 -15 1 { % for % fullHexstring remainingHexstring char remainingHexstring char divisor
idiv 16 mod % fullHexstring remainingHexstring char remainingHexstring halfChar
16 //stringbuffer cvrs % fullHexstring remainingHexstring char remainingHexstring halfCharString
0 exch % fullHexstring remainingHexstring char remainingHexstring 0 halfCharString
putinterval % fullHexstring remainingHexstring char
exch dup length 1 sub % fullHexstring char remainingHexstring newRemainingLength
1 exch % fullHexstring char remainingHexstring 1 newRemainingLength
getinterval exch % fullHexstring remainingHexstring' char
2 copy % fullHexstring remainingHexstring' char remainingHexstring' char
} for % fullHexstring remainingHexstring' char remainingHexstring' char
pop pop pop % fullHexstring remainingHexstring'
} forall % fullHexstring remainingHexstring'
pop % fullHexstring
} bind
>> begin
/writeEscapedChar { % charCode
//pdf2XMLCharMap exch % charToStringMap charCode
get % string
//stdout exch % stdout string
writestring % -/-
} bind def
/attributeTypeHandlers <<
/stringtype {
//writeEscapedChar forall
} bind
/arraytype { % array
% We turn arrays into comma separated attribute strings
() exch % separator array
{ % separator value
exch % value separator
//stdout exch writestring % value
dup type /stringtype eq { % ifelse % value
//writeEscapedChar forall % -/-
}{ % else % number
% make sure we only convert numbers - they're safe in XML
10 //stringbuffer cvrs % numberString
//stdout exch writestring % -/-
} ifelse % -/-
(,) % separator
} forall % separator
pop % -/-
} bind
/integertype {
10 //stringbuffer cvrs
//stdout exch writestring
} bind
/realtype {
10 //stringbuffer cvrs
//stdout exch writestring
} bind
/nametype {
//stringbuffer cvs
//writeEscapedChar forall
} bind
/booleantype {
{(yes)}{(no)}ifelse
//writeEscapedChar forall
}
>> def
/borderEffectToAttributeConverters <<
/S { % styleName
% Both border styles and border effects apparently translate to a style
% attribute, but we can't have two style attributes. So we comment out the
% style for border effects for now.
/C eq { % ifelse
%(style)(cloudy)
}{ % else
%(style)(solid)
} ifelse
} bind
/I { % intensityValue
(intensity) exch
} bind
>> def
/borderStyleNames <<
/S (solid)
/D (dash)
/B (bevelled)
/I (inset)
/U (underline)
>> def
/borderStyleToAttributeConverters <<
/W { % borderWidth
(width) exch
} bind
/S { % styleName
(style) exch % (style) styleName
//borderStyleNames exch % (style) borderStyleNames styleName
get % (style) styleString
} bind
/D { % dashArray
(dashes) exch
} bind
>> def
/propToAttributeConverters <<
/BE { % borderEffectDict
{ % forall % key value
exch % value key
//borderEffectToAttributeConverters % value key borderEffectToAttributeConverters
exch get % value borderEffectConverter
exec % attName attValue
} forall
} bind
/BS { % borderStyleDictOrReference
dup type /integertype eq { % if % borderStyleReference
getObjByNo % borderStyleDict
} if % borderStyleDict
{ % forall % key value
exch % value key
//borderStyleToAttributeConverters % value key borderStyleToAttributeConverters
exch % value borderStyleToAttributeConverters key
2 copy known { % ifelse % value borderStyleToAttributeConverters key
get exec % attName attValue
}{ % else % value borderStyleToAttributeConverters key
//stderr (Unsupported border style property: ) writestring % value borderStyleToAttributeConverters key
//stringbuffer cvs % value borderStyleToAttributeConverters keyAsString
//stderr exch writestring % value borderStyleToAttributeConverters
//stderr (\n) writestring % value borderStyleToAttributeConverters
pop pop % -/-
} ifelse
} forall
} bind
/C { % rgbArray
rgbArrayToHex % hexRgb
(color) exch % (color) hexRgb
} bind
/CA { % opacityValue
(opacity) exch
} bind
/Cap { % caption flag
(caption) exch
} bind
/CL { % calloutLineCoordinateArray
(callout) exch
} bind
/CreationDate { % datestring
(creationdate) exch
} bind
/F { % bitmask
[ exch % [ bitmask
% This is a bitmask value
{
(invisible) % 1
(hidden) % 2
(print) % 3
(nozoom) % 4
(norotate) % 5
(noview) % 6
(readonly) % 7
(locked) % 8
(togglenoview) % 9
% (LockedContents) % 10 apparently not supported by XFDF (and FDF?)
}{ % [ flag* bitmask flag
1 index 2 mod % [ flag* bitmask flag bit
0 eq { % ifelse % [ flag* bitmask flag
pop % [ flag* bitmask
}{ % else % [ flag* bitmask flag
exch % [ flag+ bitmask
} ifelse % [ flag* bitmask
% Shift the bitmask one bit further
2 idiv % [ flag* bitmask'
} forall % [ flag* bitmask
pop ] % flagArray
(flags) exch % (flags) flagArray
} bind
/IC { % colorArray
rgbArrayToHex
(interior-color) exch
} bind
/IT { % polylineName
% There seems to be a bug in Adobe's XFDF export:
% Per the specs, /IT should be translated to an "intent" attribute,
% but the exported attribute is named "IT" as well. We simple do both
% so that no matter whether an application adheres to the specs or
% expects flawed Adobe export can work with what we write.
//stringbuffer cvs % nameInStringBuffer
dup length string dup % nameInStringBuffer finalString finalString
3 -1 roll % finalString finalString nameInStringBuffer
0 exch % finalString finalString 0 nameInStringBuffer
putinterval % finalString
(IT) exch % (IT) finalString
(intent) 1 index % (IT) finalString (intent) finalString
} bind
/L { % coordArray
(start) 1 index % coordArray (start) coordArray
0 2 getinterval % coordArray (start) startCoords
(end) 4 -1 roll % (start) startCoords (end) coordArray
2 2 getinterval % (start) startCoords (end) endCoords
} bind
/LE { % lineEnd
(head) exch % (head) lineEnd
dup type /arraytype eq { % if % (head) lineEndArray
dup length 1 eq { % ifelse % (head) lineEndArray
{} forall % (head) headName
}{ % else % (head) lineEndArray
{} forall % (head) headName tailName
(tail) exch % (head) headName (tail) tailName
} ifelse % tailArray? (head) headName
} if % tailArray? (head) headName
} bind
/M { % datestring
(date) exch
} bind
/Name { % objString
(icon) exch
} bind
/NM { % uniqueName
(name) exch
} bind
/Open { % boolean
(open) exch
% Convert boolean to 'yes' or 'no'
{
(yes)
}{
(no)
} ifelse
} bind
/Page { % pageNumber
(page) exch
} bind
/Q { % justificationCode
(justification) exch % (justification) justificationCode
{(left)(centered)(right)} exch % (justification) justificationValues justificationCode
get % (justification) justificationValue
} bind
/QuadPoints { % coordArray
(coords) exch
}
/RD { % fringeRectArray
(fringe) exch
} bind
/Rect { % rectArray
(rect) exch % rect rectArray
} bind
/Rotate {
(rotation) exch
} bind
/State {
(state) exch
} bind
/StateModel {
(statemodel) exch
} bind
/Subj { % subjectString
(subject) exch
} bind
/T { % titleString
(title) exch
} bind
>> def
/propToChildConverters <<
/RC { % annotDict xhtmlString
(contents-richtext) mark writeStartTag % annotDict xhtmlString
% We need to strip the XML declaration, if present
(?>) search { % if % annotDict htmlBodyString (?>)? xmlDecl?
pop pop % annotDict htmlBodyString
} if % annotDict htmlBodyString
% In this case, we don't need to escape anything
% because it's already proper XHTML
//stdout exch writestring % annotDict
(contents-richtext) writeEndTag % annotDict
pop % -/-
} bind
/Contents { % annotDict contentString
% We only write a contents elemnet if we don't have a richtext (/RC) property.
% /Contents is the same as /RC, but strippped of all markup.
exch /RC known { % ifelse % contentString
pop % -/-
}{ % else % contentString
(contents) mark writeStartTag % contentString
//writeEscapedChar forall % -/-
(contents) writeEndTag % annotDict
} ifelse
} bind
/DA { % annotDict appearanceString
(defaultappearance) mark writeStartTag % annotDict appearanceString
//writeEscapedChar forall % annotDict
(defaultappearance) writeEndTag % annotDict
pop % -/-
} bind
/DS { % annotDict styleString
(defaultstyle) mark writeStartTag % annotDict styleString
//writeEscapedChar forall % annotDict
(defaultstyle) writeEndTag % annotDict
pop % -/-
} bind
/InkList { % annotDict inkListArray
(inklist) mark writeStartTag % annotDict inkListArray
{ % annotDict coordArray
(gesture) mark writeStartTag % annotDict coordArray
writeVertexArray % annotDict
(gesture) writeEndTag % annotDict
} forall % annotDict
(inklist) writeEndTag % annotDict
pop % -/-
} bind
/Popup { % annotDict objNumber
getObjByNo % annotDict popupObj
writeAnnot % annotDict
pop
} bind
/Vertices { % annotDict vertexArray
(vertices) mark writeStartTag % annotDict vertexArray
writeVertexArray % annotDict
(vertices) writeEndTag % annotDict
pop % -/-
} bind
>> def
/knownProperties <<
propToAttributeConverters {pop true} forall
propToChildConverters {pop true} forall
{
/Subtype /Type /Parent
/AP % TODO: Support the harder to handle /AP property?
} {true} forall
>> def
<<
% The commands "R" and "obj" take two parameters:
% * The object number
% * The generation number
% In XFDF (and maybe even FDF), the generation number doesn't seem to have
% meaning, so we discard it by popping.
/R /pop load
% Leaves on the stack: objectsByNo objNumber
/obj {pop //objectsByNo exch} bind
% Arguments: objectsByNo objNumber object
/endobj /put load
/stream { % streamDictionary
dup /Length get % streamDictionary streamLength
currentfile fileposition % streamDictionary streamLength filePosition
add % streamDictionary streamEndPosition
currentfile exch % streamDictionary currentFile streamEndPosition
setfileposition % streamDictionary
currentfile token pop % streamDictionary nextToken
/endstream eq not { % if
% TODO: something is wrong if we end up here as the stream
% is expected to end with an "endstream" token.
% How can we signal the error?
quit
} if
} bind
/trailer {
% We've reached the end of the file and can start outputting XFDF.
% We push a dictionary that intercepts the final closing dictionary marker
<<
(>>) cvn { % << /Root rootObjNumber
end % end this ">>" interception dictionary
>> % trailerObj
/Root get % rootObjNumber
getObjByNo % rootObj
writeXfdf % -/-
}
>> begin
}
/writeXfdf { % rootObj
% Adobe Reader doesn't accept this without XML declaration,
% plus we'd run into problems if the XML defaulted to UTF-8
//stdout (<?xml version="1.0" encoding="ISO-8859-1"?>) writestring % rootObj
(xfdf) mark
(xmlns) (http://ns.adobe.com/xfdf/)
(xmlns:xfdf) (http://ns.adobe.com/xfdf/)
% The following declarations are nasty workarounds for Foxit's habit to
% give the namespace "http://www.xfa.org/schema/xfa-data/1.0/" either the
% namespace "ns1", "ns2" or "xfa" in XML strings without declaring them.
(xmlns:ns1) (http://www.xfa.org/schema/xfa-data/1.0/)
(xmlns:ns2) (http://www.xfa.org/schema/xfa-data/1.0/)
(xmlns:xfa) (http://www.xfa.org/schema/xfa-data/1.0/)
writeStartTag
/FDF get % rootObjFdfDict
% Loop through all annotations
dup /Annots known
{ % if
(annots) mark writeStartTag % rootObjFdfDict
dup /Annots get % rootObjFdfDict annotNumberArray
{ % forall % rootObjFdfDict annotNo
getObjByNo % rootObjFdfDict annotDict
dup /Parent known { % rootObjFdfDict annotDict
pop % rootObjFdfDict
}{ % rootObjFdfDict annotDict
writeAnnot % rootObjFdfDict
} ifelse % rootObjFdfDict
} forall % rootObjFdfDict
(annots) writeEndTag % rootObjFdfDict
} if
dup /Fields known
{ % if
//stderr (WARNING: Forms support is marginal. Consult README.md\n) writestring
(fields) mark writeStartTag % rootObjFdfDict fieldsArray
dup /Fields get % rootObjFdfDict fieldsArray
{ % forall
writeField
} forall
(fields) writeEndTag
} if
dup writeDocHref % rootObjFdfDict
dup writeIds % rootObjFdfDict
pop % -/-
(xfdf) writeEndTag % -/-
quit % -/-
}
/writeDocHref { % rootObjFdfDict
% Document reference (stored in /UF and /F)
% TODO:
% * /DOS, /MAC or /UNIX are present, /F is optional.
% * If /UF is present, Adobe recommends using that
% Make this more robust!
(f) mark (href) % rootObjFdfDict (f) mark (href)
4 -1 roll % (f) mark (href) rootObjFdfDict
/F get % (f) [[ (href) filepath
writeStartTag % -/-
(f) writeEndTag % -/-
}
/writeIds {
dup /ID known { % ifelse % rootObjFdfDict
(ids) exch mark exch % (ids) mark rootObjFdfDict
/ID get //string2hexstring forall % (ids) mark originalId modifiedId
(original) 3 1 roll % (ids) mark (original) originalId modifiedId
(modified) exch % (ids) mark (original) originalId (modified) modifiedId
writeStartTag
(ids) writeEndTag
}{ % else
pop
} ifelse
}
/writeStartTag { % name mark ...
//stdout (\n<) writestring % name mark ...
counttomark 2 add -1 roll % mark ... name
//stdout exch writestring % mark ...
{ % loop % mark ...
dup mark eq {exit} if % mark ... attName attValue
//stdout ( ) writestring % mark ... attName attValue
exch % mark ... attValue attName
//stdout exch writestring % mark ... attValue
//stdout (=") writestring % mark ... attValue
dup type % mark ... attValue attType
//attributeTypeHandlers exch % mark ... attValue attributeProcessors attType
get exec % mark ...
//stdout (") writestring % mark ...
} loop % mark
//stdout (>) writestring % mark
pop % -/-
} bind
/writeEndTag { % name
//stdout (</) writestring % name
//stdout exch % stdout name
writestring % -/-
//stdout (>) writestring % -/-
} bind
/writeAnnot { % annotDict
dup /Subtype get % annotDict annotType
//fdf2XfdfAnnotNames exch % annotDict fdf2XfdfAnnotNames annotType
2 copy known { % ifelse % annotDict fdf2XfdfAnnotNames annotType
get % annotDict tagName
exch 2 copy % tagName annotDict tagName annotDict
% Create array of attributes:
mark exch % tagName annotDict tagName [ annotDict
{ % forall % tagName annotDict tagName [ attArray* key value
exch //propToAttributeConverters exch % tagName annotDict tagName [ ... value propToAttributeConverters key
2 copy known { % ifelse % tagName annotDict tagName [ ... value propToAttributeConverters key
get exec % tagName annotDict tagName [ ... attName attValue
}{ % else % tagName annotDict tagName [ ... value annotPropertyHandlers key
pop pop pop % tagName annotDict tagName [ ...
} ifelse % tagName annotDict tagName [ ...
} forall % tagName annotDict tagName [ ...
writeStartTag % tagName annotDict
% Write children
dup dup { % forall % tagName annotDict annotDict key value
exch //propToChildConverters exch % tagName annotDict annotDict value propToChildConverters key
2 copy known { % ifelse % tagName annotDict annotDict value propToChildConverters key
get exec % tagName annotDict
}{ % else % tagName annotDict annotDict value propToChildConverters key
pop pop pop pop % tagName annotDict
} ifelse % tagName annotDict
dup % tagName annotDict annotDict
} forall % tagName annotDict annotDict
pop % tagName annotDict
% We check for the presence of unsupported properties
{ % forall % tagName key value
pop dup //knownProperties exch % tagName key knownProperties key
known { % tagName key
pop % tagName
}{ % else % tagName key
//stderr (Unsupported annotation property: ) writestring % annotDict annotDict annotDict key
//stringbuffer cvs % tagName keyString
//stderr exch writestring % tagName
//stderr (\n) writestring % tagName
} ifelse % tagName
} forall % tagName
writeEndTag % -/-
}{ % else % annotDict fdf2XfdfAnnotNames annotType
//stderr (Unsupported annotation type: ) writestring % annotDict fdf2XfdfAnnotNames annotType
//stringbuffer cvs % annotDict fdf2XfdfAnnotNames annotTypeString
//stderr exch writestring % annotDict fdf2XfdfAnnotNames
//stderr (\n) writestring % annotDict fdf2XfdfAnnotNames
pop pop % -/-
} ifelse % -/-
} bind
/writeField { % fieldDict
dup /T known % fieldDict bool
{ %if % fieldDict
(field) mark % fieldDict (field) mark
(name) 3 index /T get % fieldDict (field) mark (name) name
dup (.) search % fieldDict (field) mark (name) name pre? (.)? post? bool
{ % ifelse % fieldDict (field) mark (name) name pre (.) post
//stderr (WARNING: Hierarchical fields are not supported: ) writestring
3 {//stderr exch writestring} repeat % fieldDict (field) mark (name) name
//stderr (\n) writestring % fieldDict (field) mark (name) name
}{ % else % fieldDict (field) mark (name) name name
pop % fieldDict (field) mark (name) name
} ifelse % fieldDict (field) mark (name) name
writeStartTag % fieldDict
dup /V known % fieldDict bool
{ % if % fieldDict
dup /V get type /stringtype eq % fieldDict bool
{ % ifelse % fieldDict
(value) mark writeStartTag % fieldDict
dup /V get % fieldDict value
//writeEscapedChar forall % fieldDict
(value) writeEndTag % fieldDict
}{ % else % fieldDict
//stderr (WARNING: Only strings are supported as form fields values.\n) writestring
} ifelse % fieldDict
} if % fieldDict
(field) writeEndTag % fieldDict
} if % fieldDict
pop % -/-
}
/getObjByNo { % objNumber
//objectsByNo exch get % obj
}
% TODO: In principle, the /C or /IC entries from FDF could store arrays with
% 0 (transparent), 1 (DeviceGray), 3 (DeviceRGB) or 4 (DeviceCMYK) components.
% We only properly handle 3 right now
/rgbArrayToHex { % rgbArray
% Sum up all components to a single number that can be converted to hex
0 exch { % compoundColorValue colorComponent
255 mul round cvi % compoundColorValue colorComponentByte
exch 256 mul % colorComponentByte compoundColorValue'
add % compoundColorValue''
} forall % compoundColorValue''
16 //stringbuffer cvrs % hexString
% Now we need to add a leading hash and possibly leading zeroes.
7 string % hexString hexStringWithHash
% Initialize color with leading hash and all zeroes
dup 0 //blackColor putinterval % hexString hexStringWithHash
dup 7 % hexString hexStringWithHash hexStringWithHash 7
3 index length % hexString hexStringWithHash hexStringWithHash 7 hexLength
sub % hexString hexStringWithHash hexStringWithHash leadingZerosAndHashesCount
4 -1 roll % hexStringWithHash hexStringWithHash leadingZerosAndHashesCount hexString
putinterval % hexStringWithHash
} bind
/writeVertexArray { % coordArray
(,)(;)() % coordArray sep sep sep
4 -1 roll % sep sep sep coordArray
{ % sep sep sep coord
exch //stdout exch % sep sep coord stdout sep
writestring % sep sep coord
10 //stringbuffer cvrs % sep sep coordString
//stdout exch writestring % sep sep
exch dup % sep sep sep
} forall % sep sep sep
pop pop pop
} bind
>> end begin % pop the temporary /objregistry dict, begin the new one