This commit is contained in:
John Whitington 2016-11-10 15:03:34 +00:00
parent 1e6e63daba
commit 9224b2883e
1 changed files with 34 additions and 28 deletions

View File

@ -382,7 +382,8 @@ type args =
mutable creator : string option; mutable creator : string option;
mutable producer : string option; mutable producer : string option;
mutable embedfonts : bool; mutable embedfonts : bool;
mutable change_font_size_shift : string} mutable change_font_size_shift : string;
mutable change_font_size_color : float * float * float}
let args = let args =
{op = None; {op = None;
@ -466,7 +467,8 @@ let args =
producer = None; producer = None;
creator = None; creator = None;
embedfonts = true; embedfonts = true;
change_font_size_shift = "0 0"} change_font_size_shift = "0 0";
change_font_size_color = (0., 0., 0.)}
let reset_arguments () = let reset_arguments () =
args.op <- None; args.op <- None;
@ -1113,8 +1115,7 @@ let setfontsize f =
let setaddtext s = let setaddtext s =
setop (AddText s) () setop (AddText s) ()
let setcolor s = let parse_color s =
let r, g, b =
match String.lowercase s with match String.lowercase s with
| "white" -> 1., 1., 1. | "white" -> 1., 1., 1.
| "black" -> 0., 0., 0. | "black" -> 0., 0., 0.
@ -1130,8 +1131,9 @@ let setcolor s =
match Pdfgenlex.lex_string s with match Pdfgenlex.lex_string s with
| [a;b;c] -> getnum a, getnum b, getnum c | [a;b;c] -> getnum a, getnum b, getnum c
| _ -> error "Bad color" | _ -> error "Bad color"
in
args.color <- r, g, b let setcolor s =
args.color <- parse_color s
let setopacity o = let setopacity o =
args.opacity <- o args.opacity <- o
@ -1532,6 +1534,9 @@ let setchangefontsizeto i =
let setchangefontsizeshift s = let setchangefontsizeshift s =
args.change_font_size_shift <- s args.change_font_size_shift <- s
let setchangefontsizecolor s =
args.change_font_size_color <- parse_color s
(* Parse a control file, make an argv, and then make Arg parse it. *) (* Parse a control file, make an argv, and then make Arg parse it. *)
let rec make_control_argv_and_parse filename = let rec make_control_argv_and_parse filename =
control_args := !control_args @ parse_control_file filename control_args := !control_args @ parse_control_file filename
@ -2078,7 +2083,9 @@ and specs =
("-fix-prince", Arg.Unit (setop RemoveUnusedResources), ""); ("-fix-prince", Arg.Unit (setop RemoveUnusedResources), "");
("-extract-text", Arg.Unit (setop ExtractText), ""); ("-extract-text", Arg.Unit (setop ExtractText), "");
("-change-font-size-to", Arg.Float setchangefontsizeto, ""); ("-change-font-size-to", Arg.Float setchangefontsizeto, "");
("-change-font-size-shift", Arg.String setchangefontsizeshift, "")] ("-change-font-size-shift", Arg.String setchangefontsizeshift, "");
("-change-font-size-color", Arg.String setchangefontsizecolor, "")
]
and usage_msg = and usage_msg =
"Syntax: cpdf <op> <op-specific arguments> [-o <output file>] <input files>\n\n\ "Syntax: cpdf <op> <op-specific arguments> [-o <output file>] <input files>\n\n\
@ -3192,29 +3199,28 @@ let remove_clipping pdf range =
in in
Cpdf.process_pages remove_clipping_page pdf range Cpdf.process_pages remove_clipping_page pdf range
let change_font_size_ops dx dy source_size target_size pdf resources content = let change_font_size_ops (r, g, b) dx dy source_size target_size pdf resources content =
Printf.printf "dx = %f, dy = %f\n" dx dy;
let ops = Pdfops.parse_operators pdf resources content in let ops = Pdfops.parse_operators pdf resources content in
let tr = Pdftransform.mktranslate dx dy in let tr = Pdftransform.mktranslate dx dy in
let rec process a = function let rec process a = function
Pdfops.Op_Tf (fontname, size)::t when fabs (size -. source_size) < 0.01 -> Pdfops.Op_Tf (fontname, size)::t when fabs (size -. source_size) < 0.01 ->
process process
(Pdfops.Op_Tf (fontname, target_size)::Pdfops.Op_cm tr::a) (Pdfops.Op_rg (r, g, b)::Pdfops.Op_Tf (fontname, target_size)::Pdfops.Op_cm tr::a)
t t
| h::t -> process (h::a) t | h::t -> process (h::a) t
| [] -> rev a | [] -> rev a
in in
[Pdfops.stream_of_ops (process [] ops)] [Pdfops.stream_of_ops (process [] ops)]
let change_font_size pdf range dx dy source_size target_size = let change_font_size pdf range (r, g, b) dx dy source_size target_size =
let change_font_size_page _ page = let change_font_size_page _ page =
let content' = let content' =
change_font_size_ops change_font_size_ops
dx dy source_size target_size pdf (r, g, b) dx dy source_size target_size pdf
page.Pdfpage.resources page.Pdfpage.content page.Pdfpage.resources page.Pdfpage.content
in in
Cpdf.process_xobjects Cpdf.process_xobjects
pdf page (change_font_size_ops dx dy source_size target_size); pdf page (change_font_size_ops (r, g, b) dx dy source_size target_size);
{page with Pdfpage.content = content'} {page with Pdfpage.content = content'}
in in
Cpdf.process_pages change_font_size_page pdf range Cpdf.process_pages change_font_size_page pdf range
@ -3973,7 +3979,7 @@ 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 dx, dy = parse_coordinate pdf args.change_font_size_shift in let dx, dy = parse_coordinate pdf args.change_font_size_shift in
write_pdf false (change_font_size pdf range dx dy args.fontsize target_size) write_pdf false (change_font_size pdf range args.change_font_size_color dx dy args.fontsize target_size)
let parse_argv () = let parse_argv () =
if args.debug then if args.debug then