q/Q/BT/ET by data structure

This commit is contained in:
John Whitington 2023-05-09 14:30:30 +01:00
parent a2628254b8
commit 6bc3ada2b2
4 changed files with 27 additions and 28 deletions

View File

@ -1777,7 +1777,7 @@ let whingemalformed () =
exit 1
(* Drawing operations. *)
let drawops = ref [("main", [])]
let drawops = ref [("_MAIN", [])]
let startxobj n =
drawops := (n, [])::!drawops
@ -1797,17 +1797,27 @@ let endxobj () =
let a, b, c, d = args.xobj_bbox in
addop (Cpdfdraw.FormXObject (a, b, c, d, n, rev ops))
| [] ->
error "too many -endxobj"
let tdeep = ref 0
error "too many -endxobj or -et"
let addbt () =
addop Cpdfdraw.BT;
tdeep +=1
drawops := ("_TEXT", [])::!drawops
let addet () =
addop Cpdfdraw.ET;
tdeep -=1
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 Q section at -q"
let readfloats s = map float_of_string (String.split_on_char ' ' s)
@ -1922,11 +1932,6 @@ let setdash s =
with
_ -> error "Dash pattern elements must one or more numbers"
let push () =
addop Cpdfdraw.Push
let pop () =
addop Cpdfdraw.Pop
let setmatrix s =
match readfloats s with
@ -4460,10 +4465,9 @@ let go () =
let w = (float rawwidth *. args.fontsize) /. 1000. in
Printf.printf "%f\n" w
| Some Draw ->
if !tdeep <> 0 then error "Unmatched -bt / -et" else
let pdf = get_single_pdf args.op false in
let range = parse_pagespec_allow_empty pdf (get_pagespec ()) in
let ops = match !drawops with [("main", ops)] -> rev ops | _ -> error "not enough -endxobj" in
let ops = match !drawops with [("_MAIN", ops)] -> rev ops | _ -> error "not enough -endxobj or -et" in
write_pdf
false
(Cpdfdraw.draw ~filename:args.original_filename ~bates:args.bates ~batespad:args.batespad args.fast range pdf ops)

View File

@ -20,8 +20,7 @@ type drawops =
| SetMiterLimit of float
| SetDashPattern of float list * float
| Matrix of Pdftransform.transform_matrix
| Push
| Pop
| Qq of drawops list
| Fill
| FillEvenOdd
| Stroke
@ -37,8 +36,7 @@ type drawops =
| Opacity of float
| SOpacity of float
| Font of Pdftext.standard_font * float
| BT
| ET
| TextSection of drawops list
| Text of string
| SpecialText of string
| Newline
@ -146,8 +144,7 @@ let update_resources pdf old_resources =
(Pdf.Dictionary new_fonts)
let rec ops_of_drawop pdf endpage filename bates batespad num page = function
| Push -> [Pdfops.Op_q]
| Pop -> [Pdfops.Op_Q]
| Qq ops -> [Pdfops.Op_q] @ ops_of_drawops pdf endpage filename bates batespad num page ops @ [Pdfops.Op_Q]
| Matrix m -> [Pdfops.Op_cm m]
| Rect (x, y, w, h) -> [Pdfops.Op_re (x, y, w, h)]
| Bezier (a, b, c, d, e, f) -> [Pdfops.Op_c (a, b, c, d, e, f)]
@ -208,8 +205,7 @@ let rec ops_of_drawop pdf endpage filename bates batespad num page = function
(res ()).current_font <- font;
(res ()).page_names <- n::(res ()).page_names;
[Pdfops.Op_Tf (n, f)]
| BT -> [Pdfops.Op_BT]
| ET -> [Pdfops.Op_ET]
| TextSection ops -> [Pdfops.Op_BT] @ ops_of_drawops pdf endpage filename bates batespad num page ops @ [Pdfops.Op_ET]
| Text s -> [Pdfops.Op_Tj (charcodes_of_utf8 s)]
| SpecialText s ->
let s = process_specials pdf endpage filename bates batespad num page s in

View File

@ -18,8 +18,7 @@ type drawops =
| SetMiterLimit of float
| SetDashPattern of float list * float
| Matrix of Pdftransform.transform_matrix
| Push
| Pop
| Qq of drawops list
| Fill
| FillEvenOdd
| Stroke
@ -35,8 +34,7 @@ type drawops =
| Opacity of float
| SOpacity of float
| Font of Pdftext.standard_font * float
| BT
| ET
| TextSection of drawops list
| Text of string
| SpecialText of string
| Newline

View File

@ -140,7 +140,8 @@ let impose_pages fit x y columns rtl btt center margin output_mediabox fast fit_
[Pdfops.stream_of_ops [Pdfops.Op_q; Pdfops.Op_cm transform]] @ contents @ [Pdfops.stream_of_ops [Pdfops.Op_Q]]
else
(* If slow, use protect from Pdfpage. *)
let ops = Pdfpage.protect pdf resources' contents @ Pdfops.parse_operators pdf resources' contents in
let ours = Pdfops.parse_operators pdf resources' contents in
let ops = Pdfpage.protect ours @ ours in
[Pdfops.stream_of_ops ([Pdfops.Op_q] @ [Pdfops.Op_cm transform] @ ops @ [Pdfops.Op_Q])]
in
flatten