Guest User

Untitled

a guest
Aug 21st, 2024
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 11.36 KB | None | 0 0
  1. module Macro = struct
  2.   type t =
  3.     { name        : string
  4.     ; params      : int
  5.     ; defn        : ContrlSeq.t
  6.     }
  7.  
  8.   let macro_tbl: (string, t) Hashtbl.t = Hashtbl.create 21
  9.   let add_macro n d = Hashtbl.add macro_tbl n d
  10.   let get_macro n = Hashtbl.find_opt macro_tbl n
  11.   let remove_macro n = Hashtbl.remove macro_tbl n
  12. end
  13.  
  14. module Dimen = struct
  15.   type t =
  16.     | DIMEN_Absolute of absolute
  17.     | DIMEN_Relative of relative
  18.     | DIMEN_Special of special
  19.   and absolute =
  20.     | ABSDIM_Point of float
  21.     | ABSDIM_Pica of float
  22.     | ABSDIM_Inch of float
  23.     | ABSDIM_BigPoint of float
  24.     | ABSDIM_Centimeter of float
  25.     | ABSDIM_Millimiter of float
  26.     | ABSDIM_DidoPoint of float
  27.     | ABSDIM_Cicero of float
  28.     | ABSDIM_ScaledPoint of float
  29.   and relative =
  30.     | RELDIM_Em of float
  31.     | RELDIM_Ex of float
  32.     | RELDIM_Mu of float
  33.   and special =
  34.     | SPECDIM_Fil of float
  35.     | SPECDIM_Fill of float
  36.     | SPECDIM_Filll of float
  37.  
  38.   let dimens_tbl: (string, t) Hashtbl.t = Hashtbl.create 21
  39.   let add_dimen n d = Hashtbl.add dimens_tbl n d
  40.   let get_dimen n = Hashtbl.find_opt dimens_tbl n
  41.   let remove_dimen n = Hashtbl.remove dimens_tbl n
  42.  
  43.   let point = 1.0
  44.   let pica = 12.0
  45.   let inch = 72.27
  46.   let bigpoint = 1.003
  47.   let centimeter = 180.67
  48.   let millimeter = 18.6
  49.   let didotpoint = 1238
  50.   let cicreo = 12.8
  51.   let scaledpoint = 0.000015
  52.  
  53.   let em = 2.0
  54.   let ex = 0.5
  55.   let mu = 0.0555
  56. end
  57.  
  58. module Character = struct
  59.   type t =
  60.     { value         : char
  61.     ; category      : category
  62.     }
  63.   and category =
  64.     | CAT_Escape
  65.     | CAT_GroupStart
  66.     | CAT_GroupEnd
  67.     | CAT_MathShift
  68.     | CAT_AlignmentTab
  69.     | CAT_EndOfLine
  70.     | CAT_ParameterChar
  71.     | CAT_SuperscriptMark
  72.     | CAT_SubscriptMark
  73.     | CAT_Ignored
  74.     | CAT_BlankSpace
  75.     | CAT_Letter
  76.     | CAT_OtherChars
  77.     | CAT_ActiveChar
  78.     | CAT_Comment
  79.     | CAT_InvalidChar
  80.  
  81.   let chars_tbl: (string, t) Hashtbl.t = Hashtbl.create 21
  82.   let add_chars n d = Hashtbl.add charss_tbl n d
  83.   let get_chars n = Hashtbl.find_opt charss_tbl n
  84.   let remove_chars n = Hashtbl.remove charss_tbl n
  85.  
  86.   let category_numbers = [| CAT_Escape
  87.                           ; CAT_GroupStart
  88.                           ; CAT_GroupEnd
  89.                           ; CAT_MathShift
  90.                           ; CAT_EndOfLine
  91.                           ; CAT_ParameterChar
  92.                           ; CAT_SuperscriptMark
  93.                           ; CAT_SubscriptMark
  94.                           ; CAT_Ignored
  95.                           ; CAT_BlankSpace
  96.                           ; CAT_Letter
  97.                           ; CAT_OtherChars
  98.                           ; CAT_ActiveChar
  99.                           ; CAT_Comment
  100.                           ; CAT_InvalidChar
  101.                          |]
  102.   let get_category_num cat =
  103.     match Shrdlutil.find_element_index_opt cat category_numbers with
  104.     | Some n -> n
  105.     | None -> failwith No_such_category
  106.  
  107.   exception No_such_category
  108.  
  109.   let eqv_tab = [| ref [ $PFX$ ]
  110.                  ; ref [ $GPS$ ]
  111.                  ; ref [ $GPE$ ]
  112.                  ; ref [ $SHF$ ]
  113.                  ; ref [ $NLN$ ]
  114.                  ; ref [ $PAR$ ]
  115.                  ; ref [ $SUP$ ]
  116.                  ; ref [ $SUB$ ]
  117.                  ; ref [ $IGN$ ]
  118.                  ; ref [ $WHS$ ]
  119.                  ; ref [ $LET$ ]
  120.                  ; ref [ $OTH$ ]
  121.                  ; ref [ $ACT$ ]
  122.                  ; ref [ $COM$ ]
  123.                  ; ref [ $INV$ ]
  124.                 |]
  125.   let insert_eqv cat ch =
  126.     let cat_num = get_category_num cat in
  127.     eqv_tab.[cat_num] := !eqv_tab.[cat_num] @ [ch]
  128. end
  129.  
  130. module Glue = struct
  131.   type t =
  132.     { width         : Dimen.t
  133.     ; stretch       : Dimen.t
  134.     ; shink         : Dimen.t
  135.     }
  136.  
  137.   let glues_tbl: (string, t) Hashtbl.t = Hashtbl.create 21
  138.   let add_glue n d = Hashtbl.add glues_tbl n d
  139.   let get_glue n = Hashtbl.find_opt glues_tbl n
  140.   let remove_glue n = Hashtbl.remove glues_tbl n
  141. end
  142.  
  143. module Insert = struct
  144.   type t =
  145.     { box_ref       : Box.ref
  146.     ; space         : Dimen.ref
  147.     ; glue          : Glue.ref
  148.     ; height        : Count.ref
  149.     }
  150.  
  151.   let inserts_tbl: (string, t) Hashtbl.t = Hashtbl.create 21
  152.   let add_insert n d = Hashtbl.add inserts_tbl n d
  153.   let get_insert n = Hashtbl.find_opt inserts_tbl n
  154.   let remove_insert n = Hashtbl.remove inserts_tbl n
  155. end
  156.  
  157. module Mark = struct
  158.   type t =
  159.     | MARK_First of string
  160.     | MARK_Bottom of string
  161.     | MARK_Top of string
  162.  
  163.   let marks_tbl: (string, t) Hashtbl.t = Hashtbl.create 21
  164.   let add_mark n d = Hashtbl.add marks_tbl n d
  165.   let get_mark n = Hashtbl.find_opt marks_tbl n
  166.   let remove_mark n = Hashtbl.remove marks_tbl n
  167. end
  168.  
  169. module Adjust = struct
  170.   type t =
  171.     { material      : string
  172.     ; destination   : Box.ref
  173.     }
  174.  
  175.   let adjusts_tbl: (string, t) Hashtbl.t = Hashtbl.create 21
  176.   let add_adjust n d = Hashtbl.add adjusts_tbl n d
  177.   let get_adjust n = Hashtbl.find_opt adjusts_tbl n
  178.   let remove_adjust n = Hashtbl.remove adjusts_tbl n
  179. end
  180.  
  181. module Ligature = struct
  182.   type t =
  183.     { value         : Character.t list
  184.     ; typeface      : Typeface.t
  185.     }
  186.  
  187.   let ligature_tbl: (string, t) Hashtbl.t = Hashtbl.create 21
  188.   let add_ligature n d = Hashtbl.add ligatures_tbl n d
  189.   let get_ligature n = Hashtbl.find_opt ligatures_tbl n
  190.   let remove_ligature n = Hashtbl.remove ligatures_tbl n        
  191. end
  192.  
  193. module Kern = struct
  194.   type t =
  195.     { explicit      : bool
  196.     ; accent        : bool
  197.     ; value         : float
  198.     ; parent        : Box.ref
  199.     }
  200.  
  201.   let kern_tbl: (string, t) Hashtbl.t = Hashtbl.create 21
  202.   let add_kern n d = Hashtbl.add kerns_tbl n d
  203.   let get_kern n = Hashtbl.find_opt kerns_tbl n
  204.   let remove_kern n = Hashtbl.remove kerns_tbl n
  205. end
  206.  
  207. module Penalty = struct
  208.   type t =
  209.     | PENALTY_Force of float
  210.     | PENALTY_Decide of float
  211.     | PENALTY_Ignore of float
  212.  
  213.   let penalty_tbl: (string, t) Hashtbl.t = Hashtbl.create 21
  214.   let add_penalty n d = Hashtbl.add penaltys_tbl n d
  215.   let get_penalty n = Hashtbl.find_opt penaltys_tbl n
  216.   let remove_penalty n = Hashtbl.remove penaltys_tbl n
  217. end
  218.  
  219. module Discretionary = struct
  220.   type t =
  221.     { prebreak      : string
  222.     ; postbreak     : string
  223.     ; nobreak       : string
  224.     }
  225.  
  226.   let disc_tbl: (string, t) Hashtbl.t = Hashtbl.create 21
  227.   let add_disc n d = Hashtbl.add discs_tbl n d
  228.   let get_disc n = Hashtbl.find_opt discs_tbl n
  229.   let remove_disc n = Hashtbl.remove discs_tbl n
  230. end
  231.  
  232. module Color = struct
  233.   type t =
  234.     { fill          : colorspace
  235.     ; stroke        : colorspace
  236.     }
  237.   and colorspace =
  238.     | COLOR_RGB of (float * float * float)
  239.     | COLOR_Hex of string
  240.     | COLOR_RGBA of (float * float * float * float)
  241.     | COLOR_CMYK of (float * float * float * float * float)
  242.  
  243.   let color_tbl: (string, t) Hashtbl.t = Hashtbl.create 21
  244.   let add_color n d = Hashtbl.add colors_tbl n d
  245.   let get_color n = Hashtbl.find_opt colors_tbl n
  246.   let remove_color n = Hashtbl.remove colors_tbl n
  247. end
  248.  
  249. module Curve = struct
  250.   type t =
  251.     { points        : point list
  252.     ; kind          : curve_kind
  253.     }
  254.   and curve_kind =
  255.     | CURVE_Bezier
  256.     | CURVE_Spline
  257.     | CURVE_Line
  258.     | CURVE_Arc
  259.   and point = (Dimen.t * Dimen.t)
  260.  
  261.   let curve_tbl: (string, t) Hashtbl.t = Hashtbl.create 21
  262.   let add_curve n d = Hashtbl.add curves_tbl n d
  263.   let get_curve n = Hashtbl.find_opt curves_tbl n
  264.   let remove_curve n = Hashtbl.remove curves_tbl n
  265. end
  266.  
  267. module Math = struct
  268.   type t =
  269.     { elements      : math_elt list
  270.     ; is_display    : bool
  271.     }
  272.   and math_elt =
  273.     { subelements   : math_subelt list
  274.     ; supers        : math_subelt option
  275.     ; subs          : math_subelt option
  276.     }
  277.   and math_subelt =
  278.     | MATH_Ord of string
  279.     | MATH_Op of string
  280.     | MATH_Bin of string
  281.     | MATH_Rel of string
  282.     | MATH_Open of string
  283.     | MATH_Close of string
  284.     | MATH_Punct of string
  285.     | MATH_Inner of string
  286.  
  287.   let math_tbl: (string, t) Hashtbl.t = Hashtbl.create 21
  288.   let add_math n d = Hashtbl.add maths_tbl n d
  289.   let get_math n = Hashtbl.find_opt maths_tbl n
  290.   let remove_math n = Hashtbl.remove maths_tbl n
  291.  
  292.   let math_subelt_list: math_subelt Array.t = [||]
  293. end
  294.  
  295. module Box = struct
  296.   type t =
  297.     { contents      : contents list
  298.     ; vertical      : bool
  299.     ; glue          : Glue.t
  300.     ; demerits      : Demerits.t
  301.     }
  302.   and contents =
  303.     { height        : Dimen.t
  304.     ; width         : Dimen.t
  305.     ; depth         : Dimen.t
  306.     ; vshift        : Dimen.t
  307.     ; hshift        : Dimen.t
  308.     ; box           : box_node list
  309.     }
  310.   and box_node =
  311.     | NODE_Character of Character.t
  312.     | NODE_Math of Math.t
  313.     | NODE_Penalty of Penalty.t
  314.     | NODE_Mark of Mark.t
  315.     | NODE_Discretionary of Discretionary.t
  316.     | NODE_Color of Color.t
  317.     | NODE_Adjust of Adjust.t
  318.     | NODE_Ligature of Ligature.t
  319.     | NODE_Kern of Kern.t
  320.     | NODE_Insert of Insert.t
  321.  
  322.   let box_tbl: (string, t) Hashtbl.t = Hashtbl.create 21
  323.   let add_box n d = Hashtbl.add boxs_tbl n d
  324.   let get_box n = Hashtbl.find_opt boxs_tbl n
  325.   let remove_box n = Hashtbl.remove boxs_tbl n
  326. end
  327.  
  328. module Demerits = struct
  329.   type t =
  330.     { badness       : Badness.score
  331.     ; penalty       : Penalty.score
  332.     ; hyphenation   : Hyphenetion.score
  333.     ; discretionary : Discretionary.score
  334.     ; adjust        : Adjust.score
  335.     }
  336. end
  337.  
  338. module Hyphenation = struct
  339.   type t =
  340.     | HYPH_Pattern
  341.     | HYPH_Intredit
  342.  
  343.   let hyph_mode = $HYHP_DEFAULT$
  344.  
  345.                   let intredit_tbl = ref [  $HYPH_INTREDIT$   ]
  346.   let word_is_intredit word =
  347.     List.mem word !intredit_tbl
  348.   let mark_word_intredit word =
  349.     intredit_tbl := !intredt_tbl :: word
  350.   let mark_word_not_intredit word =
  351.     if (word_is_intredit word) = false
  352.     then Runtime.error "Word " ^ word ^ " is not in exception list"
  353.     else intredit_tbl := List.filter (fun word' -> word != word') !intredit_tbl
  354. end
  355.  
  356. module Trie = struct
  357.   type t =
  358.          | Leaf of 'a
  359.          | Node of (char * 'a trie) list
  360.  
  361.   let rec insert trie key value =
  362.     match key with
  363.     | [] -> Leaf value
  364.     | h :: t ->
  365.         match trie with
  366.         | Leaf _ -> failwith "Cannot insert into leaf node"
  367.         | Node children ->
  368.            let insert_child (ch, subtree) =
  369.             if ch = h then
  370.                 (ch, insert t value subtree)
  371.             else
  372.                 (ch, subtree)
  373.            in
  374.            let new_children =
  375.              match List.assoc_op children with
  376.              | Some subtree -> List.map insert_child children
  377.              | None -> (c, insert t value (Node [])) :: children
  378.            in
  379.            Node new_children
  380.  
  381.   let rec search trie key =
  382.     match key with
  383.     | [] ->
  384.        match trie with
  385.        | Leaf v -> Some v
  386.        | Node _ -> None
  387.     | h :: t ->
  388.       match trie with
  389.       | Leaf _ -> None
  390.       | Node children ->
  391.          let rec find_child = function
  392.           | [] -> None
  393.           | (ch, subtree) :: t ->
  394.                 if ch = c then search t subtree
  395.                 else find_child t
  396.          in
  397.          find_child children
  398.  
  399.  
  400. end
  401.  
  402. module Typeface = struct
  403.   type t = None (* TODO *)
  404. end
  405.  
  406. module Shrdlutil = struct
  407.   let find_element_index_opt element arr =
  408.     let indexed_array = Array.mapi (fun i -> (i, x)) arr in
  409.     try
  410.       let (_, index) = Array.find (fun (_, x) -> x = element) indexed_array;
  411.         Some index
  412.     with Not_found -> None
  413. end
  414.  
  415.  
Advertisement
Add Comment
Please, Sign In to add comment