main.ml 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. ttfc_font_weight = TFWRegular;
  63. ttfc_font_posture = TFPNormal;
  64. } in
  65. let f2 = TTFSwfWriter.to_swf ttf config in
  66. let ch = IO.output_channel (open_out_bin (dir ^ "/" ^ ttf.ttf_font_name ^ ".dat")) in
  67. let b = IO.output_bits ch in
  68. IO.write_i16 ch 1;
  69. TTFSwfWriter.write_font2 ch b f2;
  70. IO.close_out ch;
  71. if !debug_hxswfml then begin
  72. if not (Sys.file_exists "Main.hx") then failwith "Could not find Main.hx required for -hxswfml-debug";
  73. let main = Std.input_file "Main.hx" in
  74. let old = Sys.getcwd () in
  75. Sys.chdir dir;
  76. Std.output_file ~filename:"Main.hx" ~text:main;
  77. gen_hxswfml_debug ttf.ttf_font_name;
  78. Unix.unlink "Main.hx";
  79. Sys.chdir old;
  80. end
  81. in
  82. targets := f :: !targets;
  83. ),"<dir> : generate swf tag data to <dir>");
  84. ("-canvas", Arg.String (fun dir ->
  85. mk_dir_rec dir;
  86. let f ttf range_str =
  87. let glyphs = TTFCanvasWriter.to_canvas ttf range_str in
  88. let ch = IO.output_channel (open_out_bin (dir ^ "/" ^ ttf.ttf_font_name ^ ".js")) in
  89. TTFCanvasWriter.write_font ch ttf glyphs;
  90. IO.close_out ch;
  91. in
  92. targets := f :: !targets;
  93. ),"<dir> : generate canvas draw commands to <dir>");
  94. ("-json", Arg.String (fun dir ->
  95. mk_dir_rec dir;
  96. let f ttf range_str =
  97. let glyphs = TTFJsonWriter.to_json ttf range_str in
  98. let ch = IO.output_channel (open_out_bin (dir ^ "/" ^ ttf.ttf_font_name ^ ".js")) in
  99. TTFJsonWriter.write_font ch ttf glyphs;
  100. IO.close_out ch;
  101. in
  102. targets := f :: !targets;
  103. ),"<dir> : generate json-encoded glyph information to <dir>");
  104. ("-hxswfml-debug", Arg.Unit (fun () ->
  105. debug_hxswfml := true;
  106. ),": generate debug swf with hxswfml")
  107. ] in
  108. if Array.length Sys.argv = 1 then
  109. Arg.usage basic_args usage
  110. else begin
  111. Arg.parse basic_args args_callback usage;
  112. match !fonts,!targets with
  113. | [],_ ->
  114. prerr_endline "Missing font argument";
  115. Arg.usage basic_args usage
  116. | _,[] ->
  117. prerr_endline "No targets specified (-swf|-canvas|-json)";
  118. Arg.usage basic_args usage
  119. | fonts,targets ->
  120. List.iter (fun font ->
  121. let ch = try open_in_bin font with _ -> exit ("No such file: " ^ font) in
  122. let ttf = TTFParser.parse ch in
  123. List.iter (fun target ->
  124. target ttf !range_str
  125. ) targets;
  126. close_in ch;
  127. ) fonts;
  128. end
  129. ;;
  130. try
  131. process Sys.argv;
  132. with Abort ->
  133. ()