text
stringlengths
12
786k
let remove_string_compare ( k ' : string ) l = let rec remove_inner r ( k ' : string ) = function | [ ] -> r | ( k , _ ) :: t when k = k ' -> List . rev_append r t | h :: t -> remove_inner ( h :: r ) k ' t in remove_inner [ ] k ' l
let rec remove_dict_entry dict key = match dict with | Dictionary d -> Dictionary ( remove_string_compare key d ) | Stream ( { contents = ( dict ' , stream ) } as s ) -> s := ( remove_dict_entry dict ' key , stream ) ; Stream s | _ -> raise ( PDFError " remove_dict_entry : n...
let rec replace_dict_entry dict key value = match dict with | Null -> Dictionary ( replace key value [ ] ) | Dictionary d -> Dictionary ( replace key value d ) | Stream ( { contents = ( dict ' , stream ) } as s ) -> s := ( replace_dict_entry dict ' key value , stream ) ; S...
let rec add_dict_entry dict key value = match dict with | Null -> Dictionary ( add key value [ ] ) | Dictionary d -> Dictionary ( add key value d ) | Stream ( { contents = ( dict ' , stream ) } as s ) -> s := ( add_dict_entry dict ' key value , stream ) ; Stream s | _ -...
let getstream = function | Stream ( { contents = ( d , ToGet { input = i ; position = o ; length = l ; crypt = crypt } ) } as stream ) -> if l = 0 then stream := ( d , Got ( mkbytes 0 ) ) else begin try let data = process_deferred_cryption crypt ( Pdfio . bytes_of_inpu...
let recurse_array ( f : pdfobject -> pdfobject ) elts = Array ( map f elts )
let rec recurse_dict_inner f prev = function | [ ] -> prev | ( n , o ) :: t -> recurse_dict_inner f ( ( n , f o ) :: prev ) t
let recurse_dict ( f : pdfobject -> pdfobject ) elts = Dictionary ( recurse_dict_inner f [ ] elts )
let getnum = function | Real a -> a | Integer a -> float a | _ -> raise ( PDFError " Pdf . getnum : not a number " )
let parse_rectangle = function | Array [ a ; b ; c ; d ] -> begin try let x , y , x ' , y ' = getnum a , getnum b , getnum c , getnum d in fmin x x ' , fmin y y ' , fmax x x ' , fmax y y ' with PDFError _ -> raise ( PDFError " Pdf . parse_rectangle : bad rectangle " ...
let change_obj doc i obj = fst ( pdfobjmap_find i doc . objects . pdfobjects ) := Parsed obj
let parse_lazy pdf n = match pdf . objects . parse with | None -> assert false | Some f -> let obj = f n in change_obj pdf n obj ; obj
let removeobj doc o = doc . objects <- { doc . objects with pdfobjects = pdfobjmap_remove o doc . objects . pdfobjects }
let rec lookup_obj doc i = try match fst ( pdfobjmap_find i doc . objects . pdfobjects ) with | { contents = Parsed obj | ParsedAlreadyDecrypted obj } -> obj | { contents = ToParse } -> parse_lazy doc i | { contents = ToParseFromObjectStream ( themap , streamobjnum , _ , objstreampa...
let resolve_all_delayed_object_streams pdf = iter ( function ( n , _ ) -> ignore ( lookup_obj pdf n ) ) ( pdfobjmap_bindings pdf . objects . pdfobjects )
let catalog_of_pdf pdf = try lookup_obj pdf pdf . root with Not_found -> raise ( PDFError " No catalog " )
let rec direct pdf = function | Indirect i -> begin try match fst ( pdfobjmap_find i pdf . objects . pdfobjects ) with | { contents = Parsed pdfobject | ParsedAlreadyDecrypted pdfobject } -> direct pdf pdfobject | { contents = ToParse } -> parse_lazy pdf i | { contents = ToParseFromObjectS...
let iter_stream f pdf = let rec iter_stream_inner f i = function | ( { contents = ParsedAlreadyDecrypted ( Stream _ as stream ) } | { contents = Parsed ( Stream _ as stream ) } ) , _ -> f stream | { contents = ToParse } as r , g -> r := Parsed ( parse_lazy pdf i ) ; iter_...
let rec lookup_string_compare ( k ' : string ) = function | [ ] -> None | ( k , v ) :: t -> if k = k ' then Some v else lookup_string_compare k ' t
let lookup_direct pdf key dict = match direct pdf dict with | Dictionary d | Stream { contents = ( Dictionary d , _ ) } -> begin match lookup_string_compare key d with | None | Some Null -> None | Some o -> Some ( direct pdf o ) end | _ -> None
let indirect_number pdf key dict = match direct pdf dict with | Dictionary d | Stream { contents = ( Dictionary d , _ ) } -> begin match lookup_string_compare key d with | Some ( Indirect i ) -> Some i | _ -> None end | _ -> None
let lookup_direct_orelse pdf k k ' d = match lookup_direct pdf k d with | None -> lookup_direct pdf k ' d | result -> result
let lookup_exception ( exp : exn ) pdf key dict = let dict ' = match direct pdf dict with | Dictionary d | Stream { contents = Dictionary d , _ } -> d | _ -> raise ( PDFError ( " lookup_exception : not a dictionary , key = " ^ key ) ) in match lookup key dict ' with | None | ...
let lookup_fail text = lookup_exception ( PDFError text )
let parse_matrix pdf name dict = match lookup_direct pdf name dict with | None -> Pdftransform . i_matrix | Some ( Array [ a ; b ; c ; d ; e ; f ] ) -> let a = getnum a in let b = getnum b in let c = getnum c in let d = getnum d in let e = getnum e in let f = getnum f in { Pdftransform ...
let make_matrix tr = Array [ Real tr . Pdftransform . a ; Real tr . Pdftransform . b ; Real tr . Pdftransform . c ; Real tr . Pdftransform . d ; Real tr . Pdftransform . e ; Real tr . Pdftransform . f ]
let objiter f doc = resolve_all_delayed_object_streams doc ; let f ' k v = match v with | { contents = Parsed obj } , _ -> f k obj | { contents = ParsedAlreadyDecrypted obj } , _ -> f k obj | { contents = ToParse } , _ -> f k ( parse_lazy doc k ) | { contents = ToParseFromObj...
let objselfmap f doc = resolve_all_delayed_object_streams doc ; let rec f ' k v = match v with | { contents = Parsed obj } as r , _ -> r := Parsed ( f obj ) | { contents = ParsedAlreadyDecrypted obj } as r , _ -> r := ParsedAlreadyDecrypted ( f obj ) | { contents = ToParse } , ...
let objiter_inorder f doc = resolve_all_delayed_object_streams doc ; let f ' k v = match v with | { contents = Parsed obj } , _ -> f k obj | { contents = ParsedAlreadyDecrypted obj } , _ -> f k obj | { contents = ToParse } , _ -> f k ( parse_lazy doc k ) | { contents = ToPars...
let objiter_gen f doc = resolve_all_delayed_object_streams doc ; let f ' k v = match v with | { contents = Parsed obj } , g -> f k g obj | { contents = ParsedAlreadyDecrypted obj } , g -> f k g obj | { contents = ToParse } , g -> f k g ( parse_lazy doc k ) | { contents = ToParse...
let objnumbers pdf = let keys = ref [ ] in objiter ( fun k _ -> keys =| k ) pdf ; rev ! keys
let objcard pdf = let card = ref 0 in objiter ( fun _ _ -> incr card ) pdf ; ! card
let list_of_objs doc = let objs = ref [ ] in objiter ( fun k v -> objs =| ( k , Parsed v ) ) doc ; ! objs
let addobj_given_num doc ( num , obj ) = doc . objects . maxobjnum <- max doc . objects . maxobjnum num ; doc . objects . pdfobjects <- pdfobjmap_add num ( ref ( Parsed obj ) , 0 ) doc . objects . pdfobjects
let addobj doc obj = let num = doc . objects . maxobjnum + 1 in addobj_given_num doc ( num , obj ) ; num
let objects_of_list parse l = let maxobj = ref 0 in let map = ref ( pdfobjmap_empty ( ) ) in iter ( fun ( k , v ) -> maxobj := max ! maxobj k ; map := pdfobjmap_add k v ! map ) l ; { parse = parse ; pdfobjects = ! map ; maxobjnum = ! maxobj ; object_stream_ids = null_hash ( ...
let rec page_reference_numbers_inner pdf pages_node node_number = match lookup_direct pdf " / Type " pages_node with Some ( Name " / Page " ) -> [ node_number ] | _ -> match lookup_direct pdf " / Kids " pages_node with Some ( Array elts ) -> flatten ( option_map ( function | Indire...
let page_reference_numbers pdf = let root = lookup_obj pdf pdf . root in let pages_node = match lookup_direct pdf " / Pages " root with | Some p -> p | None -> raise ( PDFError " No / Pages found in / Root " ) in page_reference_numbers_inner pdf pages_node ( - 1 )
let rec renumber_object_parsed ( pdf : t ) changes obj = match obj with | Indirect i -> let i ' = match tryfind changes i with | Some x -> x | None -> i in Indirect i ' | Array a -> recurse_array ( renumber_object_parsed pdf changes ) a | Dictionary d -> recurse_dict ( renumber_object_parsed ...
let renumber_object pdf changes objnum = function | ToParse -> renumber_object_parsed pdf changes ( parse_lazy pdf objnum ) | ToParseFromObjectStream ( themap , s , _ , func ) -> renumber_object_parsed pdf changes ( parse_delayed_object_stream themap objnum s pdf func ) | Parsed obj | ParsedAl...
let changes pdf = let card = objcard pdf in let order = ilist_fail_null 1 card and change_table = Hashtbl . create card in List . iter2 ( Hashtbl . add change_table ) ( objnumbers pdf ) order ; change_table
let renumber change_table pdf = let root ' = match tryfind change_table pdf . root with Some x -> x | None -> pdf . root and trailerdict ' = renumber_object pdf change_table 0 ( Parsed pdf . trailerdict ) and objects ' = let nums , objs = split ( list_of_objs pdf ) in let objs ' = map2...
let renumber_pdfs pdfs = let keylists = map objnumbers pdfs and bse = ref 1 and tables = ref [ ] in iter ( fun k -> let length = length k in let table = Hashtbl . create length in List . iter2 ( Hashtbl . add table ) k ( ilist ! bse ( ! bse + length - 1 ) ) ; tables =| table ; ...
let rec allfalse = function | [ ] -> true | h :: t -> not h && allfalse t
let rec containing l a = match a , l with | _ , [ ] -> false | ( name , Name n ) , ( ( h , Name hh ) :: _ ) when name = h && n = hh -> true | _ , ( _ :: t ) -> containing t a
let tocontinue no_follow_entries no_follow_contains d = ( isnull no_follow_entries && isnull no_follow_contains ) || allfalse ( map ( containing d ) no_follow_contains )
let refset_empty ( ) = Hashtbl . create 500
let refset_add n rs = Hashtbl . replace rs n ( ) ; rs
let refset_mem n rs = Hashtbl . mem rs n
let refset_elts rs = let r = ref [ ] in Hashtbl . iter ( fun k _ -> r := k ::! r ) rs ; ! r
let rec referenced_pdfobj no_follow_entries no_follow_contains pdf found i | Indirect j -> if not ( refset_mem j ! found ) then begin let obj = try lookup_obj pdf j with Not_found -> Null in match obj with | Dictionary d -> if tocontinue no_follow_entries no_follow_contains d then begin found := refset_add ...
let nullify_deleted_page_references pdf = let rec nullify numbers = function | Indirect i when tryfind numbers i <> None -> Null | Array elts -> recurse_array ( nullify numbers ) elts | Dictionary elts -> recurse_dict ( let p2 = nullify numbers in p2 ) elts | Stream { contents = ( p , s ) } ...
let remove_unreferenced pdf = nullify_deleted_page_references pdf ; let found = ref ( refset_empty ( ) ) in referenced [ ] [ ] pdf found pdf . root ( Parsed ( lookup_obj pdf pdf . root ) ) ; referenced [ ] [ ] pdf found 0 ( Parsed pdf . trailerdict ) ; found := refset...
let objects_referenced no_follow_entries no_follow_contains pdf pdfobject = let set = ref ( refset_empty ( ) ) in referenced no_follow_entries no_follow_contains pdf set 0 ( Parsed pdfobject ) ; refset_elts ! set
let bigarray_of_stream s = getstream s ; match s with | Stream { contents = _ , Got bytes } -> bytes | _ -> raise ( PDFError " couldn ' t extract raw stream " )
let unique_key prefix obj = let elts = match obj with | Dictionary es | Stream { contents = Dictionary es , _ } -> es | _ -> raise ( PDFError " unique_key : Not a dictionary or stream " ) in let names = fst ( split elts ) in let name_of_num n = Printf . sprintf " /% s % i " prefix...
let generate_id _ path gettime = let d = match Sys . getenv_opt " CAMLPDF_REPRODUCIBLE_IDS " with | Some " true " -> Digest . string " camlpdf " | _ -> Digest . string ( path ^ string_of_float ( gettime ( ) ) ) in Array [ String d ; String d ]
let find_indirect key dict = match dict with | Dictionary d -> begin match lookup key d with | Some ( Indirect i ) -> Some i | _ -> None end | _ -> raise ( PDFError " find_indirect : not a dictionary " )
let rec nametree_lookup_kids pdf k = function | Array ( h :: t ) -> begin match nametree_lookup pdf k h with | None -> nametree_lookup_kids pdf k ( Array t ) | Some result -> Some result end | Array [ ] -> None | _ -> raise ( PDFError " nametree_lookup_kids : malformed name tree " ) |...
let rec contents_of_nametree pdf tree = match lookup_direct_orelse pdf " / Names " " / Nums " tree with | Some ( Array names ) -> let rec pairs_of_list prev = function | [ ] -> rev prev | [ _ ] -> raise ( PDFError " contents_of_nametree : bad / Names " ) | k :: v :: r -> pai...
let deep_copy_pdfobjects frompdf from = resolve_all_delayed_object_streams frompdf ; let deep_copy_objdata objdata = let deep_copy_pdfobject = function | Stream _ as s -> begin getstream s ; match s with | Stream { contents = ( dict , Got stream ) } -> Stream ( ref ( dict , Got ( copybyte...
let deep_copy from = { major = from . major ; minor = from . minor ; root = from . root ; objects = { maxobjnum = from . objects . maxobjnum ; parse = from . objects . parse ; pdfobjects = deep_copy_pdfobjects from from . objects . pdfobjects ; object_stream_ids = Hashtbl . ...
let change_id pdf f = match pdf . trailerdict with | Dictionary d -> pdf . trailerdict <- Dictionary ( add " / ID " ( generate_id pdf f ( fun ( ) -> Random . float 1 . ) ) d ) | _ -> raise ( PDFError " Bad trailer dictionary " )
let read_char_metrics_line l = match String . split_on_char ' ' l with | " C " :: charnum " ; " " :::: WX " :: width " ; " " :::: N " :: name :: _ -> ( name , ( int_of_string charnum , int_of_string width ) ) | _ -> failwith " badline in read_char_metrics_line " ...
let lookup_charnum table name = match Hashtbl . find table name with ( c ' , _ ) -> c '
let read_kern_line l = match String . split_on_char ' ' l with | " KPX " :: n :: n ' :: i :: _ -> n , n ' , int_of_string ( implode ( option_map ( function ' \ r ' | ' \ n ' -> None | c -> Some c ) ( explode i ) ) ) | _ -> failwith " badline in read_kern_line...
let string_starts_with sub s = let sublength = String . length sub in if String . length s < sublength then false else let rec loop n = if n < 0 then true else s . [ n ] = sub . [ n ] && loop ( n - 1 ) in loop ( sublength - 1 )
let get_tables lines = let char_metrics_lines = isolate ( string_starts_with " StartCharMetrics " ) ( string_starts_with " EndCharMetrics " ) lines and kern_lines = isolate ( string_starts_with " StartKernPairs " ) ( string_starts_with " EndKernPairs " ) lines and header_lines = map ...
let read i = try let lines = Pdfio . read_lines i in get_tables lines with e -> failwith ( Printexc . to_string e )
type style = | NoStyle | Solid | Dashed | Beveled | Inset | UnderlineStyle
type border = { width : float ; vradius : float ; hradius : float ; style : style ; dasharray : int array }
type subtype = | Text | Link | FreeText | Line | Square | Circle | Polygon | PolyLine | Highlight | Underline | Squiggly | StrikeOut | Stamp | Caret | Ink | Popup of t | FileAttachment | Sound | Movie | Widget | Screen | PrinterMark | TrapNet | Watermark | ThreeDee | Unknown { subtype : su...
let rec read_annotation pdf annot = let subtype = match Pdf . lookup_direct pdf " / Subtype " annot with | Some ( Pdf . Name " / Text " ) -> Text | Some ( Pdf . Name " / FreeText " ) -> FreeText | Some ( Pdf . Name " / Popup " ) -> begin match Pdf . direct pdf annot wit...
let get_popup_parent pdf annotation = match Pdf . direct pdf annotation with | Pdf . Dictionary d -> begin match lookup " / Parent " d with | Some ( Pdf . Indirect i ) -> Some i | _ -> None end | _ -> raise ( Pdf . PDFError " Pdfannot . get_popup_parent : not a dictionary " )
let annotations_of_page pdf page = match Pdf . lookup_direct pdf " / Annots " page . Pdfpage . rest with | Some ( Pdf . Array annotations ) -> let popup_parents = option_map ( get_popup_parent pdf ) annotations in map ( read_annotation pdf ) ( lose ( function Pdf . Indirect i -> mem ...
let string_of_subtype = function | Text -> " / Text " | Link -> " / Link " | FreeText -> " / FreeText " | Line -> " / Line " | Square -> " / Square " | Circle -> " / Circle " | Polygon -> " / Polygon " | PolyLine -> " / PolyLine " | Highlight -> " / Highligh...
let obj_of_annot t = let d = [ " / Subtype " , Pdf . Name ( string_of_subtype t . subtype ) ; " / Contents " , ( match t . annot_contents with None -> Pdf . Null | Some s -> Pdf . String s ) ; " / Rect " , ( let a , b , c , d = t . rectangle in Pdf . Array ...
let make_border ( ? vradius = 0 . 0 ) ( ? hradius = 0 . 0 ) ( ? style = NoStyle ) ( ? dasharray = [ ] ) || width = { vradius ; hradius ; style ; dasharray ; width }
let make ? content ( ? border = make_border 0 . 0 ) ( ? rectangle ( = 0 . , 0 . , 0 . , 0 . ) ) ? colour ? subject subtype = { annot_contents = content ; border ; rectangle ; colour ; subject ; subtype ; annotrest = Pdf . Null }
let add_annotation pdf page anno = let obj = obj_of_annot anno in match Pdf . lookup_direct pdf " / Annots " page . Pdfpage . rest with | Some ( Pdf . Array annotations ) -> { page with Pdfpage . rest = Pdf . add_dict_entry page . Pdfpage . rest " / Annots " ( Pdf . Array ( ob...
type dictop = | IntOp of int32 | RealOp of float | Operator of int | Operator2 of int
let read_card8 b = i32toi ( getval_32 b 8 )
let read_card16 b = i32toi ( getval_32 b 16 )
let discard_bytes b n = for x = 1 to n do ignore ( getval_32 b 8 ) done
let read_string b l = let chars = ref [ ] in for x = 1 to l do chars =| char_of_int ( i32toi ( getval_32 b 8 ) ) done ; implode ( rev ! chars )
let read_index b = let count = read_card16 b in if count = 0 then [ ] else let offsize = read_card8 b * 8 and offsets = ref [ ] in for x = 1 to count + 1 do offsets =| i32toi ( getval_32 b offsize ) done ; let offsets = rev ! offsets in map ( fun ( x , y ) -> y - x ) ( pairs o...
let rec read_float_dict_item prev n_read b = match i32toi ( getval_32 b 4 ) with | 0xF -> align b ; ( ( n_read + 2 ) / 2 ) , Scanf . sscanf ( implode ( rev prev ) ) " % f " ident | 0x0 -> read_float_dict_item ( ' 0 ' :: prev ) ( n_read + 1 ) b | 0x1 -> read_...
let read_dict_item b = let b1 = read_card8 b in if b1 >= 0 && b1 <= 21 then if b1 = 12 then let b2 = read_card8 b in 2 , Operator2 b2 else 1 , Operator b1 else if b1 = 28 then let ob1 = read_card8 b in let ob2 = read_card8 b in 3 , IntOp ( i32ofi ( ( ob1 lsl 8 ) lor ob2 ) ) else if b...
let rec read_dict prev b l = if l <= 0 then rev prev else let bytes_read , op = read_dict_item b in read_dict ( op :: prev ) b ( l - bytes_read )
let standard_encoding = [ ( 0 , 0 ) ; ( 1 , 0 ) ; ( 2 , 0 ) ; ( 3 , 0 ) ; ( 4 , 0 ) ; ( 5 , 0 ) ; ( 6 , 0 ) ; ( 7 , 0 ) ; ( 8 , 0 ) ; ( 9 , 0 ) ; ( 10 , 0 ) ; ( 11 , 0 ) ; ( 12 , 0 ) ; ( ...
let expert_encoding = [ ( 0 , 0 ) ; ( 1 , 0 ) ; ( 2 , 0 ) ; ( 3 , 0 ) ; ( 4 , 0 ) ; ( 5 , 0 ) ; ( 6 , 0 ) ; ( 7 , 0 ) ; ( 8 , 0 ) ; ( 9 , 0 ) ; ( 10 , 0 ) ; ( 11 , 0 ) ; ( 12 , 0 ) ; ( ...
let sids = [ ( 0 , " . notdef " ) ; ( 1 , " space " ) ; ( 2 , " exclam " ) ; ( 3 , " quotedbl " ) ; ( 4 , " numbersign " ) ; ( 5 , " dollar " ) ; ( 6 , " percent " ) ; ( 7 , " ampersand " ) ; ( 8 , " quot...
type topdict_entry = | CharStringType of int | CharSetOffset of int | EncodingOffset of int | CharStringOffset of int | PrivateDict of int * int | FontMatrix of Pdftransform . transform_matrix
let parse_topdict_entry = function | Operator2 6 , [ IntOp x ] -> Some ( CharStringType ( i32toi x ) ) | Operator 15 , [ IntOp x ] -> Some ( CharSetOffset ( i32toi x ) ) | Operator 16 , [ IntOp x ] -> Some ( EncodingOffset ( i32toi x ) ) | Operator 17 , [ IntOp ...
let rec read_dict_sections prev ops = match let operands , more = cleavewhile ( function Operator _ | Operator2 _ -> false | _ -> true ) ops in match more with | Operator x :: rest -> Some ( Operator x , operands , rest ) | Operator2 x :: rest -> Some ( Operator2 x , operands , rest )...
let parse_topdict ops = option_map parse_topdict_entry ( read_dict_sections [ ] ops )
let string_of_topdict_entry = function | CharStringType i -> " CharStringType is " ^ string_of_int i ^ " \ n " | CharSetOffset i -> " CharSetOffset is " ^ string_of_int i ^ " \ n " | EncodingOffset i -> " EncodingOffset is " ^ string_of_int i ^ " \ n " | CharStringOffset i -> "...
type privatedict_entry = | Subrs of int
let parse_privatedict_entry = function | Operator 19 , [ IntOp offset ] -> Some ( Subrs ( i32toi offset ) ) | _ -> None
let parse_privatedict ops = option_map parse_privatedict_entry ( read_dict_sections [ ] ops )