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

16
cpdf.ml
View File

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

View File

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

View File

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

Binary file not shown.

View File

@ -1276,6 +1276,7 @@ The starting point can be set with the \texttt{-bates} option. For example:
\small\verb!-right 10! & Right of baseline 10 pts in from the center 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!-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{tabular}
\end{framed} \end{framed}