main.ml 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. open TTFData
  2. exception Abort
  3. let gen_hxswfml_debug fontname =
  4. let xml = "<?xml version=\"1.0\" ?>
  5. <swf>
  6. <FileAttributes/>
  7. <Custom tagId=\"75\" file=\"" ^ fontname ^ ".dat\" comment=\"DefineFont3\"/>
  8. <SymbolClass id=\"1\" class=\"TestFont\" base=\"flash.text.Font\"/>
  9. <DefineABC file=\"Main.swf\" isBoot=\"true\"/>
  10. <ShowFrame/>
  11. </swf>"
  12. in
  13. Std.output_file (fontname ^ ".fxml") xml;
  14. if Sys.command "haxe -main Main -swf main.swf" <> 0 then failwith "Error while executing haxe";
  15. if Sys.command ("hxswfml xml2swf \"" ^ fontname ^ ".fxml\" \"" ^ fontname ^ ".swf\" -no-strict") <> 0 then failwith "Error while executing hxswfml";
  16. Unix.unlink (fontname ^ ".fxml");
  17. Unix.unlink "main.swf"
  18. let normalize_path p =
  19. let l = String.length p in
  20. if l = 0 then
  21. "./"
  22. else begin
  23. let p = String.concat "/" (ExtString.String.nsplit p "\\") in
  24. match p.[l-1] with
  25. | '/' -> p
  26. | _ -> p ^ "/"
  27. end
  28. let mk_dir_rec dir =
  29. let dir = normalize_path dir in
  30. let parts = ExtString.String.nsplit dir "/" in
  31. let rec create acc = function
  32. | [] -> ()
  33. | "" :: [] -> ()
  34. | d :: l ->
  35. let dir = String.concat "/" (List.rev (d :: acc)) in
  36. if not (Sys.file_exists dir) then Unix.mkdir dir 0o755;
  37. create (d :: acc) l
  38. in
  39. create [] parts
  40. let exit msg =
  41. prerr_endline msg;
  42. raise Abort
  43. let process args =
  44. let fonts = ref [] in
  45. let range_str = ref "" in
  46. let targets = ref [] in
  47. let debug_hxswfml = ref false in
  48. let args_callback s = fonts := s :: !fonts in
  49. let usage = Printf.sprintf
  50. "Ttf <font paths> (-swf|-canvas)"
  51. in
  52. let basic_args = [
  53. ("-range",Arg.String (fun str ->
  54. range_str := str;
  55. ),"<str> : specifies the character range");
  56. ("-swf",Arg.String (fun dir ->
  57. mk_dir_rec dir;
  58. let f ttf range_str =
  59. let config = {
  60. ttfc_range_str = range_str;
  61. ttfc_font_name = None;
  62. } in
  63. let f2 = TTFSwfWriter.to_swf ttf config in
  64. let ch = IO.output_channel (open_out_bin (dir ^ "/" ^ ttf.ttf_font_name ^ ".dat")) in
  65. let b = IO.output_bits ch in
  66. IO.write_i16 ch 1;
  67. TTFSwfWriter.write_font2 ch b f2;
  68. IO.close_out ch;
  69. if !debug_hxswfml then begin
  70. if not (Sys.file_exists "Main.hx") then failwith "Could not find Main.hx required for -hxswfml-debug";
  71. let main = Std.input_file "Main.hx" in
  72. let old = Sys.getcwd () in
  73. Sys.chdir dir;
  74. Std.output_file ~filename:"Main.hx" ~text:main;
  75. gen_hxswfml_debug ttf.ttf_font_name;
  76. Unix.unlink "Main.hx";
  77. Sys.chdir old;
  78. end
  79. in
  80. targets := f :: !targets;
  81. ),"<dir> : generate swf tag data to <dir>");
  82. ("-canvas", Arg.String (fun dir ->
  83. mk_dir_rec dir;
  84. let f ttf range_str =
  85. let glyphs = TTFCanvasWriter.to_canvas ttf range_str in
  86. let ch = IO.output_channel (open_out_bin (dir ^ "/" ^ ttf.ttf_font_name ^ ".js")) in
  87. TTFCanvasWriter.write_font ch ttf glyphs;
  88. IO.close_out ch;
  89. in
  90. targets := f :: !targets;
  91. ),"<dir> : generate canvas draw commands to <dir>");
  92. ("-json", Arg.String (fun dir ->
  93. mk_dir_rec dir;
  94. let f ttf range_str =
  95. let glyphs = TTFJsonWriter.to_json ttf range_str in
  96. let ch = IO.output_channel (open_out_bin (dir ^ "/" ^ ttf.ttf_font_name ^ ".js")) in
  97. TTFJsonWriter.write_font ch ttf glyphs;
  98. IO.close_out ch;
  99. in
  100. targets := f :: !targets;
  101. ),"<dir> : generate json-encoded glyph information to <dir>");
  102. ("-hxswfml-debug", Arg.Unit (fun () ->
  103. debug_hxswfml := true;
  104. ),": generate debug swf with hxswfml")
  105. ] in
  106. if Array.length Sys.argv = 1 then
  107. Arg.usage basic_args usage
  108. else begin
  109. Arg.parse basic_args args_callback usage;
  110. match !fonts,!targets with
  111. | [],_ ->
  112. prerr_endline "Missing font argument";
  113. Arg.usage basic_args usage
  114. | _,[] ->
  115. prerr_endline "No targets specified (-swf|-canvas|-json)";
  116. Arg.usage basic_args usage
  117. | fonts,targets ->
  118. List.iter (fun font ->
  119. let ch = try open_in_bin font with _ -> exit ("No such file: " ^ font) in
  120. let ttf = TTFParser.parse ch in
  121. List.iter (fun target ->
  122. target ttf !range_str
  123. ) targets;
  124. close_in ch;
  125. ) fonts;
  126. end
  127. ;;
  128. try
  129. process Sys.argv;
  130. with Abort ->
  131. ()