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 | 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,53 +1721,53 @@ 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 ->
| Diagonal -> (xmin +. xmax) /. 2. -. w /. 2.,
let angle = atan ((ymax -. ymin) /. (xmax -. xmin)) (ymin +. ymax) /. 2.,
in let cx, cy = (xmax +. xmin) /. 2., (ymax +. ymin) /. 2. in rot
let dl = w /. 2. in | Diagonal ->
let dx = dl *. cos angle let angle = atan ((ymax -. ymin) /. (xmax -. xmin))
in let dy = dl *. sin angle in in let cx, cy = (xmax +. xmin) /. 2., (ymax +. ymin) /. 2. in
(*i Printf.printf "Diagonal: angle = %f, cx = %f, cy = %f, dx = %f, dy = %f\n" angle cx cy dx dy; i*) let dl = w /. 2. in
cx -. dx, cy -. dy, angle let dx = dl *. cos angle
| ReverseDiagonal -> in let dy = dl *. sin angle in
(*flprint "REVERSE DIAGONAL IN CALCULATE POSITION\n";*) cx -. dx, cy -. dy, angle
let angle = atan ((ymax -. ymin) /. (xmax -. xmin)) | ReverseDiagonal ->
in let cx, cy = (xmax +. xmin) /. 2., (ymax +. ymin) /. 2. in let angle = atan ((ymax -. ymin) /. (xmax -. xmin))
let dl = w /. 2. in in let cx, cy = (xmax +. xmin) /. 2., (ymax +. ymin) /. 2. in
let dx = dl *. cos angle let dl = w /. 2. in
in let dy = dl *. sin angle in let dx = dl *. cos angle
(*Printf.printf "Diagonal: angle = %f\n" (deg_of_rad angle);*) in let dy = dl *. sin angle in
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
| PosRight (x, y) -> xmin +. x -. w, ymin +. y, rot | PosRight (x, y) -> xmin +. x -. w, ymin +. y, rot
| Top d -> | Top d ->
let d = if ignore_d then 0. else d in let d = if ignore_d then 0. else d in
(xmin +. xmax) /. 2. -. w /. 2., ymax -. d, rot (xmin +. xmax) /. 2. -. w /. 2., ymax -. d, rot
| TopLeft d -> | TopLeft d ->
let d = if ignore_d then 0. else d in let d = if ignore_d then 0. else d in
xmin +. d, ymax -. d, rot xmin +. d, ymax -. d, rot
| TopRight d -> | TopRight d ->
let d = if ignore_d then 0. else d in let d = if ignore_d then 0. else d in
xmax -. d -. w, ymax -. d, rot xmax -. d -. w, ymax -. d, rot
| Left d -> | Left d ->
let d = if ignore_d then 0. else d in let d = if ignore_d then 0. else d in
xmin +. d, (ymax +. ymin) /. 2., rot xmin +. d, (ymax +. ymin) /. 2., rot
| BottomLeft d -> | BottomLeft d ->
let d = if ignore_d then 0. else d in let d = if ignore_d then 0. else d in
xmin +. d, ymin +. d, rot xmin +. d, ymin +. d, rot
| Bottom d -> | Bottom d ->
let d = if ignore_d then 0. else d in let d = if ignore_d then 0. else d in
(xmin +. xmax) /. 2. -. w /. 2., ymin +. d, rot (xmin +. xmax) /. 2. -. w /. 2., ymin +. d, rot
| BottomRight d -> | BottomRight d ->
let d = if ignore_d then 0. else d in let d = if ignore_d then 0. else d in
xmax -. d -. w, ymin +. d, rot xmax -. d -. w, ymin +. d, rot
| Right d -> | Right d ->
let d = if ignore_d then 0. else d in let d = if ignore_d then 0. else d in
xmax -. d -. w, (ymax +. ymin) /. 2., rot xmax -. d -. w, (ymax +. ymin) /. 2., rot
(* Process UTF8 text to /WinAnsiEncoding string. *) (* Process UTF8 text to /WinAnsiEncoding string. *)
let winansi_of_utf8 s = let winansi_of_utf8 s =
@ -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

@ -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!-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!-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}