Added -center option for text, soon to be used for stamps too.

This commit is contained in:
John Whitington 2014-10-03 15:55:03 +01:00
parent b13f1f592f
commit 21ce677313
5 changed files with 65 additions and 55 deletions

102
cpdf.ml
View File

@ -1694,6 +1694,7 @@ type position =
| Right of float
| Diagonal
| ReverseDiagonal
| Centre
let string_of_position = function
| PosCentre (a, b) -> Printf.sprintf "PosCentre %f %f" a b
@ -1709,6 +1710,7 @@ let string_of_position = function
| Right a -> Printf.sprintf "Right %f" a
| Diagonal -> "Diagonal"
| ReverseDiagonal -> "Reverse Diagonal"
| Centre -> "Centre"
type orientation =
| Horizontal
@ -1719,53 +1721,53 @@ type justification = LeftJustify | CentreJustify | RightJustify
(* Given the mediabox, calculate an absolute position for the text. *)
let calculate_position ignore_d w (xmin, ymin, xmax, ymax) orientation pos =
(*i Printf.printf "calculate_position %b %f %f %f %f %f %b\n" ignore_d w xmin ymin xmax ymax shorterside; i*)
let rot = if orientation = VerticalDown then rad_of_deg 270. else 0. in
match pos with
| Diagonal ->
let angle = atan ((ymax -. ymin) /. (xmax -. xmin))
in let cx, cy = (xmax +. xmin) /. 2., (ymax +. ymin) /. 2. in
let dl = w /. 2. in
let dx = dl *. cos angle
in let dy = dl *. sin angle in
(*i Printf.printf "Diagonal: angle = %f, cx = %f, cy = %f, dx = %f, dy = %f\n" angle cx cy dx dy; i*)
cx -. dx, cy -. dy, angle
| ReverseDiagonal ->
(*flprint "REVERSE DIAGONAL IN CALCULATE POSITION\n";*)
let angle = atan ((ymax -. ymin) /. (xmax -. xmin))
in let cx, cy = (xmax +. xmin) /. 2., (ymax +. ymin) /. 2. in
let dl = w /. 2. in
let dx = dl *. cos angle
in let dy = dl *. sin angle in
(*Printf.printf "Diagonal: angle = %f\n" (deg_of_rad angle);*)
cx -. dx, (ymax +. ymin) -. (cy -. dy), angle -. ((2. *. pi) -. ((pi -. (2. *. angle)) *. 2.) /. 2.) +. pi
| PosLeft (x, y) -> xmin +. x, ymin +. y, rot
| PosCentre (x, y) -> xmin +. x -. (w /. 2.), ymin +. y, rot
| PosRight (x, y) -> xmin +. x -. w, ymin +. y, rot
| Top d ->
let d = if ignore_d then 0. else d in
(xmin +. xmax) /. 2. -. w /. 2., ymax -. d, rot
| TopLeft d ->
let d = if ignore_d then 0. else d in
xmin +. d, ymax -. d, rot
| TopRight d ->
let d = if ignore_d then 0. else d in
xmax -. d -. w, ymax -. d, rot
| Left d ->
let d = if ignore_d then 0. else d in
xmin +. d, (ymax +. ymin) /. 2., rot
| BottomLeft d ->
let d = if ignore_d then 0. else d in
xmin +. d, ymin +. d, rot
| Bottom d ->
let d = if ignore_d then 0. else d in
(xmin +. xmax) /. 2. -. w /. 2., ymin +. d, rot
| BottomRight d ->
let d = if ignore_d then 0. else d in
xmax -. d -. w, ymin +. d, rot
| Right d ->
let d = if ignore_d then 0. else d in
xmax -. d -. w, (ymax +. ymin) /. 2., rot
let rot = if orientation = VerticalDown then rad_of_deg 270. else 0. in
match pos with
| Centre ->
(xmin +. xmax) /. 2. -. w /. 2.,
(ymin +. ymax) /. 2.,
rot
| Diagonal ->
let angle = atan ((ymax -. ymin) /. (xmax -. xmin))
in let cx, cy = (xmax +. xmin) /. 2., (ymax +. ymin) /. 2. in
let dl = w /. 2. in
let dx = dl *. cos angle
in let dy = dl *. sin angle in
cx -. dx, cy -. dy, angle
| ReverseDiagonal ->
let angle = atan ((ymax -. ymin) /. (xmax -. xmin))
in let cx, cy = (xmax +. xmin) /. 2., (ymax +. ymin) /. 2. in
let dl = w /. 2. in
let dx = dl *. cos angle
in let dy = dl *. sin angle in
cx -. dx, (ymax +. ymin) -. (cy -. dy), angle -. ((2. *. pi) -. ((pi -. (2. *. angle)) *. 2.) /. 2.) +. pi
| PosLeft (x, y) -> xmin +. x, ymin +. y, rot
| PosCentre (x, y) -> xmin +. x -. (w /. 2.), ymin +. y, rot
| PosRight (x, y) -> xmin +. x -. w, ymin +. y, rot
| Top d ->
let d = if ignore_d then 0. else d in
(xmin +. xmax) /. 2. -. w /. 2., ymax -. d, rot
| TopLeft d ->
let d = if ignore_d then 0. else d in
xmin +. d, ymax -. d, rot
| TopRight d ->
let d = if ignore_d then 0. else d in
xmax -. d -. w, ymax -. d, rot
| Left d ->
let d = if ignore_d then 0. else d in
xmin +. d, (ymax +. ymin) /. 2., rot
| BottomLeft d ->
let d = if ignore_d then 0. else d in
xmin +. d, ymin +. d, rot
| Bottom d ->
let d = if ignore_d then 0. else d in
(xmin +. xmax) /. 2. -. w /. 2., ymin +. d, rot
| BottomRight d ->
let d = if ignore_d then 0. else d in
xmax -. d -. w, ymin +. d, rot
| Right d ->
let d = if ignore_d then 0. else d in
xmax -. d -. w, (ymax +. ymin) /. 2., rot
(* Process UTF8 text to /WinAnsiEncoding string. *)
let winansi_of_utf8 s =
@ -1861,7 +1863,7 @@ let find_justification_offsets longest_w w position = function
| LeftJustify ->
begin match position with
| TopLeft _ | Left _ | PosLeft _ | BottomLeft _ -> 0.
| Top _ | PosCentre _ | Bottom _ -> (longest_w -. w) /. 2.
| Top _ | PosCentre _ | Bottom _ | Centre -> (longest_w -. w) /. 2.
| TopRight _ | BottomRight _ | PosRight _ | Right _ -> longest_w -. w
| Diagonal -> 0.
| ReverseDiagonal -> 0.
@ -1869,7 +1871,7 @@ let find_justification_offsets longest_w w position = function
| RightJustify ->
begin match position with
| TopLeft _ | Left _ | PosLeft _ | BottomLeft _ -> ~-.(longest_w -. w)
| Top _ | PosCentre _ | Bottom _ -> ~-.((longest_w -. w) /. 2.)
| Top _ | PosCentre _ | Bottom _ | Centre -> ~-.((longest_w -. w) /. 2.)
| TopRight _ | BottomRight _ | PosRight _ | Right _ -> 0.
| Diagonal -> 0.
| ReverseDiagonal -> 0.
@ -1877,7 +1879,7 @@ let find_justification_offsets longest_w w position = function
| CentreJustify ->
begin match position with
| TopLeft _ | Left _ | PosLeft _ | BottomLeft _ -> ~-.((longest_w -. w) /. 2.)
| Top _ | PosCentre _ | Bottom _ -> 0.
| Top _ | PosCentre _ | Bottom _ | Centre -> 0.
| TopRight _ | BottomRight _ | PosRight _ | Right _ -> (longest_w -. w) /. 2.
| Diagonal -> 0.
| ReverseDiagonal -> 0.

View File

@ -232,6 +232,7 @@ type position =
| Right of float
| Diagonal
| ReverseDiagonal
| Centre
(** Produce a debug string of a [position] *)
val string_of_position : position -> string

View File

@ -932,9 +932,12 @@ let setreversediagonal n =
args.position <- Cpdf.ReverseDiagonal;
args.justification <- Cpdf.CentreJustify
(* FIXME: We will add a center option to text positioning, which can be used for this too *)
let setcenter n =
args.position <- Cpdf.Diagonal;
args.position <- Cpdf.Centre;
args.justification <- Cpdf.CentreJustify
let setscalecenter n =
args.position <- Cpdf.ReverseDiagonal;
args.justification <- Cpdf.CentreJustify
let setbates n =
@ -1331,8 +1334,8 @@ and specs =
("-scale-contents",
Arg.Float setscalecontents,
" Scale Contents by the given factor");
("-center",
Arg.Float setcenter,
("-scale-center",
Arg.Float setscalecenter,
" Scale contents around center");
("-scale-to-fit-scale",
Arg.Float setscaletofitscale,
@ -1510,6 +1513,9 @@ and specs =
("-reverse-diagonal",
Arg.Unit setreversediagonal,
" Place text diagonally across page from top left");
("-center",
Arg.Unit setcenter,
" Place text in the center of the page");
("-justify-left",
Arg.Unit setjustifyleft,
" Justify multiline text left");

Binary file not shown.

View File

@ -1275,7 +1275,8 @@ The starting point can be set with the \texttt{-bates} option. For example:
\small\verb!-bottomright 10! & Right of baseline 10 pts up and in from bottom right \\
\small\verb!-right 10! & Right of baseline 10 pts in from the center right \\
\small\verb!-diagonal! & Diagonal, bottom left to top right, centered on page\\
\small\verb!-reverse-diagonal! & Diagonal, bottom right to top left, centered on page\\
\small\verb!-reverse-diagonal! & Diagonal, bottom right to top left, centered on page\\
\small\verb!-center! & Centered on page\\
\end{tabular}
\end{framed}