cpdf-source/cpdfposition.ml

88 lines
3.2 KiB
OCaml
Raw Normal View History

2021-08-12 21:38:55 +02:00
open Pdfutil
type position =
| PosCentre of float * float
| PosLeft of float * float
| PosRight of float * float
| Top of float
2023-04-07 16:31:21 +02:00
| TopLeft of float * float
| TopRight of float * float
2021-08-12 21:38:55 +02:00
| Left of float
2023-04-07 16:31:21 +02:00
| BottomLeft of float * float
2021-08-12 21:38:55 +02:00
| Bottom of float
2023-04-07 16:31:21 +02:00
| BottomRight of float * float
2021-08-12 21:38:55 +02:00
| Right of float
| Diagonal
| ReverseDiagonal
| Centre
let string_of_position = function
| PosCentre (a, b) -> Printf.sprintf "PosCentre %f %f" a b
| PosLeft (a, b) -> Printf.sprintf "PosLeft %f %f" a b
| PosRight (a, b) -> Printf.sprintf "PosRight %f %f" a b
| Top a -> Printf.sprintf "Top %f" a
2023-04-07 16:31:21 +02:00
| TopLeft (a, b) -> Printf.sprintf "TopLeft %f %f" a b
| TopRight (a, b) -> Printf.sprintf "TopRight %f %f" a b
2021-08-12 21:38:55 +02:00
| Left a -> Printf.sprintf "Left %f" a
2023-04-07 16:31:21 +02:00
| BottomLeft (a, b) -> Printf.sprintf "BottomLeft %f %f" a b
2021-08-12 21:38:55 +02:00
| Bottom a -> Printf.sprintf "Bottom %f" a
2023-04-07 16:31:21 +02:00
| BottomRight (a, b) -> Printf.sprintf "BottomRight %f %f" a b
2021-08-12 21:38:55 +02:00
| Right a -> Printf.sprintf "Right %f" a
| Diagonal -> "Diagonal"
| ReverseDiagonal -> "Reverse Diagonal"
| Centre -> "Centre"
(* Given the mediabox, calculate an absolute position for the text. *)
2022-09-21 18:40:28 +02:00
let calculate_position ignore_d w (xmin, ymin, xmax, ymax) pos =
let rot = 0. in
2021-08-12 21:38:55 +02:00
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
2023-04-07 16:31:21 +02:00
| TopLeft (a, b) ->
let a = if ignore_d then 0. else a in
let b = if ignore_d then 0. else b in
xmin +. a, ymax -. b, rot
| TopRight (a, b) ->
let a = if ignore_d then 0. else a in
let b = if ignore_d then 0. else b in
xmax -. a -. w, ymax -. b, rot
2021-08-12 21:38:55 +02:00
| Left d ->
let d = if ignore_d then 0. else d in
xmin +. d, (ymax +. ymin) /. 2., rot
2023-04-07 16:31:21 +02:00
| BottomLeft (a, b) ->
let a = if ignore_d then 0. else a in
let b = if ignore_d then 0. else b in
xmin +. a, ymin +. b, rot
2021-08-12 21:38:55 +02:00
| Bottom d ->
let d = if ignore_d then 0. else d in
(xmin +. xmax) /. 2. -. w /. 2., ymin +. d, rot
2023-04-07 16:31:21 +02:00
| BottomRight (a, b) ->
let a = if ignore_d then 0. else a in
let b = if ignore_d then 0. else b in
xmax -. a -. w, ymin +. b, rot
2021-08-12 21:38:55 +02:00
| Right d ->
let d = if ignore_d then 0. else d in
xmax -. d -. w, (ymax +. ymin) /. 2., rot