process_font.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals, print_function
  3. try:
  4. import Image
  5. except ImportError:
  6. from PIL import Image
  7. import os
  8. from xml.dom import minidom
  9. import tempfile
  10. import hashlib
  11. from . import process
  12. class bmfc_font_Processor(process.Process):
  13. node_id = "bmfc_font"
  14. def __init__(self):
  15. pass
  16. def process(self, context, walker):
  17. el = walker.root
  18. file_name = el.getAttribute("file")
  19. id = el.getAttribute("id")
  20. if not id:
  21. id = os.path.split(os.path.splitext(file_name)[0])[1]
  22. path_font = context.src_data + walker.getPath("file")
  23. chars = walker.getPath("chars")
  24. scale = context.scale * walker.scale_quality * walker.scale_factor
  25. build_bmfont(context.args.hash,
  26. context.get_inner_dest(id + ".fnt"),
  27. path_font,
  28. context.get_inner_dest(""),
  29. scale,
  30. context.src_data + chars)
  31. meta = walker.root_meta
  32. ns = get_bmfc_fontSize(path_font)
  33. font_size = int(ns * walker.scale_factor)
  34. meta.setAttribute("size", str(font_size))
  35. meta.setAttribute("sf", str(scale))
  36. class font_Processor(process.Process):
  37. create_folder = False
  38. node_id = "font"
  39. def __init__(self):
  40. pass
  41. def process(self, context, walker):
  42. el = walker.root
  43. # note: variable unused
  44. file_name = el.getAttribute("file")
  45. meta = walker.root_meta
  46. path = context.src_data + walker.getPath("file")
  47. try:
  48. font_doc = context._open_xml(path)
  49. font_info = font_doc.getElementsByTagName("info")[0]
  50. size = int(font_info.getAttribute("size"))
  51. except:
  52. data = open(path, "r").read()
  53. pos = data.find("size=") + 5
  54. end = data.find(" ", pos)
  55. st = data[pos:end]
  56. size = int(st)
  57. size = int(abs(size) * walker.scale_factor)
  58. meta.setAttribute("size", str(size))
  59. meta.setAttribute("sf", str(1))
  60. def as_int(attr):
  61. if not attr:
  62. return 0
  63. return int(attr)
  64. def get_bmfc_fontSize(bm_config):
  65. font_file = open(bm_config, "r")
  66. lines = font_file.readlines()
  67. for line in lines:
  68. spl = line.split("=")
  69. if spl[0] == "fontSize":
  70. return abs(int(spl[1]))
  71. def build_bmfont(need_md5, fnt, bm_config, ext_folder, scale, font_chars):
  72. # open config file and apply scale to size
  73. if not os.path.exists(font_chars):
  74. print("error! you didn't set to bmfont 'chars'")
  75. bm_config_temp = tempfile.NamedTemporaryFile(
  76. prefix="oxygine", delete=False)
  77. bm_config_temp.close()
  78. bm_config_temp = bm_config_temp.name
  79. # font_size = get_bmfc_fontSize(bm_config)
  80. def rewrite_config(bm_original, bm_new, pngHeight):
  81. font_file = open(bm_original, "r")
  82. scaled_font_file = open(bm_new, "w")
  83. lines = font_file.readlines()
  84. for line in lines:
  85. # line = ""
  86. spl = line.split("=")
  87. if spl[0] == "fontSize":
  88. new_size = int(abs(int(spl[1])) * scale + 0.5)
  89. new_size = -new_size
  90. line = "fontSize=%(size)d\n" % {"size": new_size}
  91. if spl[0] == "outHeight":
  92. line = "outHeight=%(pngHeight)d\n" % {"pngHeight": pngHeight}
  93. scaled_font_file.write(line)
  94. font_file.close()
  95. scaled_font_file.close()
  96. rewrite_config(bm_config, bm_config_temp, 1024)
  97. lang = font_chars
  98. bmfont = os.path.split(__file__)[0] + \
  99. "/../../3rdPartyTools/BMFont/bmfont.com"
  100. cmd = "%(bmfont)s -t %(lang)s -c %(bmfc)s -o %(fnt)s" % {
  101. "bmfont": bmfont, "bmfc": bm_config_temp, "fnt": fnt, "lang": lang}
  102. cmd = cmd.replace("/", "\\")
  103. os.system(cmd)
  104. png_file = os.path.splitext(fnt)[0] + "_0.png"
  105. font_image = Image.open(png_file)
  106. font_image.load()
  107. _, _, _, a = font_image.split()
  108. bbox = a.getbbox()
  109. h = bbox[3] + 2
  110. if h > 512:
  111. h = 1024
  112. elif h > 256:
  113. h = 512
  114. elif h > 128:
  115. h = 256
  116. elif h > 64:
  117. h = 128
  118. elif h < 64:
  119. h = 64
  120. del font_image
  121. rewrite_config(bm_config, bm_config_temp, h)
  122. os.system(cmd)
  123. if need_md5:
  124. # if 0:
  125. md = hashlib.md5()
  126. md.update(open(bm_config_temp).read())
  127. md.update(open(lang).read())
  128. with open(os.path.split(fnt)[0] + "/md5.oxygine", "a") as m:
  129. m.write("%s\n%s\n" % (os.path.split(png_file)[1], md.hexdigest()))
  130. m.write("%s\n%s\n" % (os.path.split(fnt)[1], md.hexdigest()))
  131. file = open(fnt, "r")
  132. doc = minidom.parse(file)
  133. kern = doc.documentElement.getElementsByTagName("kernings")
  134. if kern:
  135. el = kern[0]
  136. el.parentNode.removeChild(el)
  137. pages = doc.documentElement.getElementsByTagName("pages")[0]
  138. for page in pages.getElementsByTagName("page"):
  139. fn = os.path.split(page.getAttribute("file"))[1]
  140. page.setAttribute("file", fn)
  141. file.close()
  142. file = open(fnt, "w")
  143. doc.writexml(file)
  144. file.close()
  145. os.remove(bm_config_temp)