-midline and -topline for stamps

This commit is contained in:
John Whitington 2015-02-17 15:48:00 +00:00
parent 95b7258e30
commit 0f375d2573
4 changed files with 40 additions and 25 deletions

View File

@ -1,10 +1,11 @@
Version 2.2 Version 2.2 (To come)
o -keep-l keeps existing linearization status o -keep-l keeps existing linearization status
o -remove-dict-entry to remove the contents of a dictionary entry o -remove-dict-entry to remove the contents of a dictionary entry
o -topline in addition to -midline o -topline in addition to -midline
o -producer and -creator may be used to alter producer and creator o -producer and -creator may be used to alter producer and creator
in conjunction with any other operation in conjunction with any other operation
o -topline and -midline now apply to stamps
Version 2.1 (November 2014) Version 2.1 (November 2014)

50
cpdf.ml
View File

@ -2024,23 +2024,29 @@ let hflip_pdf ?(fast=false) pdf range =
in in
process_pages (flip_page ~fast transform_op pdf) pdf range process_pages (flip_page ~fast transform_op pdf) pdf range
let stamp_shift_of_position sw sh w h p = let stamp_shift_of_position topline midline sw sh w h p =
let half x = x /. 2. in let half x = x /. 2.
and dy =
if midline then sh /. 2.
else if topline then sh
else 0.
in
match p with match p with
| PosCentre (ox, oy) -> ox -. half sw, oy | PosCentre (ox, oy) -> ox -. half sw, oy -. dy
| PosLeft (ox, oy) -> ox, oy | PosLeft (ox, oy) -> ox, oy -. dy
| PosRight (ox, oy) -> ox -. sw, oy | PosRight (ox, oy) -> ox -. sw, oy -. dy
| Top o -> half w -. half sw, h -. o -. sh | Top o -> half w -. half sw, h -. o -. sh -. dy
| TopLeft o -> o, h -. sh -. o | TopLeft o -> o, h -. sh -. o -. dy
| TopRight o -> w -. sw -. o, h -. sh -. o | TopRight o -> w -. sw -. o, h -. sh -. o -. dy
| Left o -> o, half h -. half sh | Left o -> o, half h -. half sh -. dy
| BottomLeft o -> o, o | BottomLeft o -> o, o -. dy
| Bottom o -> half w -. half sw, o | Bottom o -> half w -. half sw, o -. dy
| BottomRight o -> w -. sw, o | BottomRight o -> w -. sw, o -. dy
| Right o -> w -. sw -. o, half h -. half sh | Right o -> w -. sw -. o, half h -. half sh -. dy
| Diagonal | ReverseDiagonal | Centre -> half w -. half sw, half h -. half sh | Diagonal | ReverseDiagonal | Centre ->
half w -. half sw, half h -. half sh -. dy
let do_stamp fast position scale_to_fit isover pdf o u opdf = let do_stamp fast position topline midline scale_to_fit isover pdf o u opdf =
(* Scale page stamp o to fit page u *) (* Scale page stamp o to fit page u *)
let sxmin, symin, sxmax, symax = let sxmin, symin, sxmax, symax =
Pdf.parse_rectangle Pdf.parse_rectangle
@ -2075,7 +2081,7 @@ let do_stamp fast position scale_to_fit isover pdf o u opdf =
and sh = symax -. symin and sh = symax -. symin
and w = txmax -. txmin and w = txmax -. txmin
and h = tymax -. tymin in and h = tymax -. tymin in
let dx, dy = stamp_shift_of_position sw sh w h position in let dx, dy = stamp_shift_of_position topline midline sw sh w h position in
let translate_op = let translate_op =
Pdfops.Op_cm Pdfops.Op_cm
(Pdftransform.matrix_of_transform [Pdftransform.Translate (dx, dy)]) (Pdftransform.matrix_of_transform [Pdftransform.Translate (dx, dy)])
@ -2115,7 +2121,7 @@ let change_bookmark t m =
{m with Pdfmarks.target = {m with Pdfmarks.target =
try change_destination t m.Pdfmarks.target with Not_found -> m.Pdfmarks.target} try change_destination t m.Pdfmarks.target with Not_found -> m.Pdfmarks.target}
let stamp position fast scale_to_fit isover range over pdf = let stamp position topline midline fast scale_to_fit isover range over pdf =
let marks = Pdfmarks.read_bookmarks pdf in let marks = Pdfmarks.read_bookmarks pdf in
let marks_refnumbers = Pdf.page_reference_numbers pdf in let marks_refnumbers = Pdf.page_reference_numbers pdf in
let pdf = Pdfmarks.remove_bookmarks pdf in let pdf = Pdfmarks.remove_bookmarks pdf in
@ -2145,7 +2151,7 @@ let stamp position fast scale_to_fit isover range over pdf =
let new_pages = let new_pages =
map2 map2
(fun pageseq under_page -> (fun pageseq under_page ->
do_stamp fast position scale_to_fit isover renamed_pdf do_stamp fast position topline midline scale_to_fit isover renamed_pdf
(if mem pageseq range then over_page else (if mem pageseq range then over_page else
Pdfpage.blankpage Pdfpaper.a4) Pdfpage.blankpage Pdfpaper.a4)
under_page over) under_page over)
@ -2197,7 +2203,11 @@ let combine_pages (fast : bool) under over scaletofit swap equalize =
cleave (Pdfpage.pages_of_pagetree renamed_pdf) under_length cleave (Pdfpage.pages_of_pagetree renamed_pdf) under_length
in in
let new_pages = let new_pages =
map2 (fun o u -> do_stamp fast (BottomLeft 0.) scaletofit (not swap) renamed_pdf o u over) over_pages under_pages map2
(fun o u ->
do_stamp fast (BottomLeft 0.) false false scaletofit (not swap) renamed_pdf o u over)
over_pages
under_pages
in in
Pdfmarks.add_bookmarks (marks_under @ marks_over) (Pdfpage.change_pages true renamed_pdf new_pages) Pdfmarks.add_bookmarks (marks_under @ marks_over) (Pdfpage.change_pages true renamed_pdf new_pages)
@ -2249,7 +2259,7 @@ let nobble_page pdf _ page =
] ]
} }
in in
do_stamp false (BottomLeft 0.) false true pdf page' page (Pdf.empty ()) do_stamp false (BottomLeft 0.) false false false true pdf page' page (Pdf.empty ())
(* \section{Set media box} *) (* \section{Set media box} *)
let set_mediabox x y w h pdf range = let set_mediabox x y w h pdf range =

View File

@ -197,10 +197,10 @@ swapped. If [fast] is true, the PDFs are assumed to be well-formed and no
fixes are done. *) fixes are done. *)
val combine_pages : bool -> Pdf.t -> Pdf.t -> bool -> bool -> bool -> Pdf.t val combine_pages : bool -> Pdf.t -> Pdf.t -> bool -> bool -> bool -> Pdf.t
(** [stamp scale_to_fit position fast isover range over pdf] stamps the first page of (** [stamp topline midline scale_to_fit position fast isover range over pdf] stamps the first page of
[over] over each page of the PDF. The arguments have the same meaning as in [over] over each page of the PDF. The arguments have the same meaning as in
[combine_pages]. *) [combine_pages]. *)
val stamp : position -> bool -> bool -> bool -> int list -> Pdf.t -> Pdf.t -> Pdf.t val stamp : position -> bool -> bool -> bool -> bool -> bool -> int list -> Pdf.t -> Pdf.t -> Pdf.t
(** {2 Splitting PDFs} *) (** {2 Splitting PDFs} *)

View File

@ -3531,7 +3531,9 @@ let go () =
let pdf = get_single_pdf args.op false in let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf (get_pagespec ()) in let range = parse_pagespec pdf (get_pagespec ()) in
let pdf = let pdf =
Cpdf.stamp args.position args.fast args.scale_stamp_to_fit true range overpdf pdf Cpdf.stamp
args.position args.topline args.midline args.fast
args.scale_stamp_to_fit true range overpdf pdf
in in
write_pdf false pdf write_pdf false pdf
| Some (StampUnder under) -> | Some (StampUnder under) ->
@ -3543,7 +3545,9 @@ let go () =
let pdf = get_single_pdf args.op false in let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf (get_pagespec ()) in let range = parse_pagespec pdf (get_pagespec ()) in
let pdf = let pdf =
Cpdf.stamp args.position args.fast args.scale_stamp_to_fit false range underpdf pdf Cpdf.stamp
args.position args.topline args.midline args.fast
args.scale_stamp_to_fit false range underpdf pdf
in in
write_pdf false pdf write_pdf false pdf
| Some (CombinePages over) -> | Some (CombinePages over) ->