diff --git a/Makefile b/Makefile index 4b86553..901616d 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ DOC = cpdfunicodedata cpdferror cpdfdebug cpdfjson cpdfstrftime cpdfcoord \ cpdfembed cpdfaddtext cpdffont cpdftype cpdfpad cpdfocg \ cpdfsqueeze cpdfdraft cpdfspot cpdfpagelabels cpdfcreate cpdfannot \ cpdfxobject cpdfimpose cpdftweak cpdftexttopdf cpdftoc cpdfjpeg \ - cpdfpng cpdfimage cpdfdraw cpdfcomposition cpdfgraphics cpdfshape \ + cpdfpng cpdfimage cpdfdraw cpdfcomposition cpdfshape \ cpdfcolours cpdfcommand MODS = $(NONDOC) $(DOC) diff --git a/cpdfaddtext.ml b/cpdfaddtext.ml index b0da4d3..c511a2f 100644 --- a/cpdfaddtext.ml +++ b/cpdfaddtext.ml @@ -431,8 +431,11 @@ let let realfontname = ref fontname in let font = match cpdffont with - | Cpdfembed.PreMadeFontPack f -> Some (hd (fst f)) - | Cpdfembed.EmbedInfo {fontfile; fontname; encoding} -> + | Cpdfembed.PreMadeFontPack f -> + (*Printf.printf "Cpdfaddtext.addtexts: PreMadeFontPack\n";*) + Some (hd (fst f)) + | Cpdfembed.EmbedInfo {fontfile; fontname; encoding} -> + (*Printf.printf "Cpdfaddtext.addtexts: EmbedInfo\n";*) let embedded = Cpdfembed.embed_truetype pdf ~fontfile ~fontname ~codepoints:[] ~encoding in Some (hd (fst embedded)) | Cpdfembed.ExistingNamedFont -> None diff --git a/cpdfcommand.ml b/cpdfcommand.ml index 4068904..6f7ff80 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -1854,12 +1854,12 @@ let addcircle s = | [x; y; r] -> let _, _, segs = hd (snd (Cpdfshape.circle x y r)) in (match segs with - | Cpdfgraphics.Bezier ((a, b), _, _, _)::_ -> addop (Cpdfdraw.To (a, b)) + | Cpdfshape.Bezier ((a, b), _, _, _)::_ -> addop (Cpdfdraw.To (a, b)) | _ -> assert false); iter (function - | Cpdfgraphics.Bezier (_, (c, d), (e, f), (g, h)) -> addop (Cpdfdraw.Bezier (c, d, e, f, g, h)) - | Cpdfgraphics.Straight _ -> assert false) + | Cpdfshape.Bezier (_, (c, d), (e, f), (g, h)) -> addop (Cpdfdraw.Bezier (c, d, e, f, g, h)) + | Cpdfshape.Straight _ -> assert false) segs | _ -> error "-circle requires three numbers" | exception _ -> error "malformed -circle" diff --git a/cpdfshape.ml b/cpdfshape.ml index 51b99d5..6b2a83d 100644 --- a/cpdfshape.ml +++ b/cpdfshape.ml @@ -4,6 +4,25 @@ primitives (circles, regular polygons etc). *) open Pdfutil +type fpoint = float * float + +type winding_rule = EvenOdd | NonZero + +type segment = + | Straight of fpoint * fpoint + | Bezier of fpoint * fpoint * fpoint * fpoint + +(* Each segment list may be marked as a hole or not. *) +type hole = Hole | Not_hole + +(* A [subpath] is either closed or open. *) +type closure = Closed | Open + +(* A [subpath] is the pair of a hole and a list of segments. *) +type subpath = hole * closure * segment list + +(* A path is made from a number of subpaths. *) +type path = winding_rule * subpath list (* \section{Common geometric functions} *) (* The factor by which we multiply the radius to find the length of the bezier @@ -52,13 +71,13 @@ let quarter s (cx, cy) r = match map (Pdftransform.transform transform) standard_quarter_points with - | [p; q; r; s] -> Cpdfgraphics.Bezier(p, q, r, s) + | [p; q; r; s] -> Bezier(p, q, r, s) | _ -> raise (Pdf.PDFError ("Shapes.quarter: inconsistency")) (* The anticlockwise variant. *) let quarter_anticlockwise s c r = match quarter s c r with - | Cpdfgraphics.Bezier(p, q, r, s) -> Cpdfgraphics.Bezier(s, r, q, p) + | Bezier(p, q, r, s) -> Bezier(s, r, q, p) | _ -> raise (Pdf.PDFError "Shapes.quarter_anticlockwise: inconsistency") (* Some of the following functions generate what is supposed to be a connected @@ -71,8 +90,8 @@ let rec joinsegs segments = match segments with | [] -> [] | [x] -> [x] - | Cpdfgraphics.Bezier(_, _, _, d) as s::Cpdfgraphics.Bezier(_, b', c', d')::rest -> - s::joinsegs (Cpdfgraphics.Bezier(d, b', c', d')::rest) + | Bezier(_, _, _, d) as s::Bezier(_, b', c', d')::rest -> + s::joinsegs (Bezier(d, b', c', d')::rest) | _ -> raise (Pdf.PDFError "PDFShapes.joinsegs: Segment not supported") (* This version sets the start and end points to p1 and p2 respectively. Used @@ -80,11 +99,11 @@ for ensuring round joins join correctly to the rails they connect *) let joinsegs_ends p1 p2 segments = match joinsegs segments with | [] -> [] - | [Cpdfgraphics.Bezier(a, b, c, d)] -> [Cpdfgraphics.Bezier(p1, b, c, p2)] + | [Bezier(a, b, c, d)] -> [Bezier(p1, b, c, p2)] | segs -> match extremes_and_middle segs with - | Cpdfgraphics.Bezier(_, b, c, d), m, Cpdfgraphics.Bezier(a', b', c', _) -> - Cpdfgraphics.Bezier(p1, b, c, d)::m @ [Cpdfgraphics.Bezier(a', b', c', p2)] + | Bezier(_, b, c, d), m, Bezier(a', b', c', _) -> + Bezier(p1, b, c, d)::m @ [Bezier(a', b', c', p2)] | _ -> raise (Pdf.PDFError "PDFShapes.joinsegs_ends: Segment not supported") (* The shorter arc made from bezier curves from [p1] to [p2] with centre [c]. @@ -127,9 +146,9 @@ are of equal length, the one chosen is undefined. *) (* Approximate a circle using four bezier curves.*) let circle x y r = - Cpdfgraphics.NonZero, - [(Cpdfgraphics.Not_hole, - Cpdfgraphics.Closed, + NonZero, + [(Not_hole, + Closed, joinsegs [quarter 0. (x, y) r; quarter (pi /. 2.) (x, y) r; @@ -137,10 +156,10 @@ let circle x y r = quarter (3. *. pi /. 2.) (x, y) r ])] let rectangle x y w h = - (Cpdfgraphics.EvenOdd, - ([(Cpdfgraphics.Not_hole, - Cpdfgraphics.Closed, - [Cpdfgraphics.Straight ((x, y), (x +. w, y)); - Cpdfgraphics.Straight ((x +. w, y), (x +. w, y +. h)); - Cpdfgraphics.Straight ((x +. w, y +. h), (x, y +. h)); - Cpdfgraphics.Straight ((x, y +. h), (x, y))])])) + (EvenOdd, + ([(Not_hole, + Closed, + [Straight ((x, y), (x +. w, y)); + Straight ((x +. w, y), (x +. w, y +. h)); + Straight ((x +. w, y +. h), (x, y +. h)); + Straight ((x, y +. h), (x, y))])])) diff --git a/cpdfshape.mli b/cpdfshape.mli index ed6d6aa..d0b8d16 100644 --- a/cpdfshape.mli +++ b/cpdfshape.mli @@ -1,5 +1,24 @@ (** Basic Shapes *) +type fpoint = float * float + +type winding_rule = EvenOdd | NonZero + +type segment = + | Straight of fpoint * fpoint + | Bezier of fpoint * fpoint * fpoint * fpoint + +(* Each segment list may be marked as a hole or not. *) +type hole = Hole | Not_hole + +(* A [subpath] is either closed or open. *) +type closure = Closed | Open + +(* A [subpath] is the pair of a hole and a list of segments. *) +type subpath = hole * closure * segment list + +(* A path is made from a number of subpaths. *) +type path = winding_rule * subpath list (** The factor by which the radius of a circle is multiplied to find the length of the bezier control lines when approximating quarter arcs to make circles. *) val kappa : float @@ -10,8 +29,8 @@ val restrict_angle : float -> float -> float (** Calling [circle x y r] builds a path representing a circle at [(x, y)] with radius [r]. *) -val circle : float -> float -> float -> Cpdfgraphics.path +val circle : float -> float -> float -> path (** Calling [rectangle x y w h] builds a path representing a rectangle with top left [(x, y)], width [w] and height [h]. *) -val rectangle : float -> float -> float -> float -> Cpdfgraphics.path +val rectangle : float -> float -> float -> float -> path diff --git a/cpdftruetype.ml b/cpdftruetype.ml index 9a476ef..a37f5ae 100644 --- a/cpdftruetype.ml +++ b/cpdftruetype.ml @@ -2,9 +2,9 @@ open Pdfutil open Pdfio -let fontpack_experiment = false +let fontpack_experiment = true -let dbg = ref true +let dbg = ref false type t = {flags : int; @@ -436,8 +436,12 @@ let subset_font major minor tables indexToLocFormat subset encoding cmap loca mk end; obs +(* FIXME: remove *) +let _ = + Pdfe.logger := (fun s -> print_string s; flush stdout) + let parse ?(subset=[]) data encoding = - if !dbg then + (*if !dbg then*) begin Printf.printf "********SUBSET is "; iter (Printf.printf "%i ") subset; @@ -554,7 +558,7 @@ let parse ?(subset=[]) data encoding = end; let flags = calculate_flags italicangle in let firstchar_1, lastchar_1 = calculate_limits subset_1 in - let firstchar_2, lastchar_2 = (0, length subset_2 - 1) in + let firstchar_2, lastchar_2 = (0, max 0 (length subset_2 - 1)) in let numOfLongHorMetrics = match keep (function (t, _, _, _) -> string_of_tag t = "hhea") !tables with | (_, _, o, l)::_ -> let b = mk_b (i32toi o) in read_hhea_table b @@ -565,9 +569,9 @@ let parse ?(subset=[]) data encoding = | (_, _, o, _)::_ -> read_hmtx_table numOfLongHorMetrics (mk_b (i32toi o)) | [] -> raise (Pdf.PDFError "No hmtx table found in TrueType font") in - (*Printf.printf "firstchar_1, lastchar_1, firstchar_2, lastchar_2 = %i, %i, %i%, %i\n" firstchar_1 lastchar_1 firstchar_2 lastchar_2;*) + Printf.printf "firstchar_1, lastchar_1, firstchar_2, lastchar_2 = %i, %i, %i%, %i\n" firstchar_1 lastchar_1 firstchar_2 lastchar_2; let widths_1 = calculate_widths unitsPerEm encoding firstchar_1 lastchar_1 subset_1 !glyphcodes hmtxdata in - (*let widths_2 = calculate_widths unitsPerEm encoding firstchar_2 lastchar_2 subset_2 !glyphcodes hmtxdata in*) + let widths_2 = calculate_widths unitsPerEm encoding firstchar_2 lastchar_2 subset_2 !glyphcodes hmtxdata in let maxwidth = calculate_maxwidth unitsPerEm hmtxdata in let stemv = calculate_stemv () in let b = mk_b (i32toi locaoffset) in @@ -581,23 +585,23 @@ let parse ?(subset=[]) data encoding = subset_font major minor !tables indexToLocFormat subset_1 encoding !glyphcodes loca mk_b glyfoffset data in - (*let second_subset = + let second_subset = subset_font major minor !tables indexToLocFormat subset_2 encoding !glyphcodes loca mk_b glyfoffset data - in*) - (*let second_tounicode = + in + let second_tounicode = if subset = [] then None else let h = null_hash () in let s = (implode (tl (tl (explode (Pdftext.utf16be_of_codepoints [hd subset]))))) in Printf.printf "String for tounicode = %S\n" s; Hashtbl.add h 0 s; Some h - in*) + in [{flags; minx; miny; maxx; maxy; italicangle; ascent; descent; capheight; stemv; xheight; avgwidth; maxwidth; firstchar = firstchar_1; lastchar = lastchar_1; widths = widths_1; subset_fontfile = main_subset; subset = subset_1; tounicode = None}] - (*@ if fontpack_experiment then + @ if fontpack_experiment then [{flags; minx; miny; maxx; maxy; italicangle; ascent; descent; capheight; stemv; xheight; avgwidth; maxwidth; firstchar = firstchar_2; lastchar = lastchar_2; widths = widths_2; subset_fontfile = second_subset; subset = subset_2; - tounicode = second_tounicode}] else []*) + tounicode = second_tounicode}] else [] diff --git a/old/README.md b/old/README.md new file mode 100644 index 0000000..514ae4f --- /dev/null +++ b/old/README.md @@ -0,0 +1 @@ +An old, unfinished module from CPDF's very early life. To be resuccitated one day? diff --git a/cpdfgraphics.ml b/old/cpdfgraphics.ml similarity index 100% rename from cpdfgraphics.ml rename to old/cpdfgraphics.ml diff --git a/cpdfgraphics.mli b/old/cpdfgraphics.mli similarity index 100% rename from cpdfgraphics.mli rename to old/cpdfgraphics.mli