font-generator.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Font Generator
  5. # Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. import argparse
  20. import cairo
  21. import codecs
  22. import re
  23. class FontGenerator(object):
  24. def __init__(self):
  25. self.font_size = 24
  26. self.letters = u"""!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"""
  27. self.font_family = "WenQuanYi Micro Hei"
  28. self.columns = 20
  29. self.glyph_width = 20
  30. self.glyph_height = 20
  31. self.draw_outline = False
  32. self.draw_shadow = False
  33. def generate_image(self):
  34. rows = (len(self.letters) + self.columns - 1) // self.columns
  35. width = self.glyph_width * self.columns
  36. height = self.glyph_height * rows
  37. surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, width, height)
  38. cr = cairo.Context (surface)
  39. # cr.translate(0.5, 0.5)
  40. self.draw_glyphs(cr, self.columns, rows)
  41. return surface
  42. def draw_glyphs(self, cr, cols, rows):
  43. if False:
  44. cr.set_source_rgb(255, 255, 255)
  45. cr.fill()
  46. elif self.checkerboard:
  47. for row in range(rows):
  48. for col in range(cols):
  49. idx = row * cols + col
  50. cr.rectangle(col * self.glyph_width,
  51. row * self.glyph_height,
  52. self.glyph_width,
  53. self.glyph_height)
  54. if (col + row % 2) % 2 == 0 :
  55. cr.set_source_rgb(0.5, 0.5, 0.5)
  56. else:
  57. cr.set_source_rgb(0.75, 0.75, 0.75)
  58. cr.fill()
  59. cr.select_font_face(self.font_family, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
  60. cr.set_font_size(self.font_size)
  61. fascent, fdescent, fheight, fyadvance = cr.font_extents()
  62. print self.glyph_height, fheight
  63. for row in range(rows):
  64. for col in range(cols):
  65. idx = row * cols + col
  66. if idx < len(self.letters):
  67. letter = self.letters[idx]
  68. xbearing, ybearing, width, height, yadvance = cr.text_extents(letter)
  69. #cr.move_to(cx + 0.5 - xbearing - width / 2,
  70. # 0.5 - fdescent + fheight / 2)
  71. gx = col * self.glyph_width + self.glyph_width/2.0 - xbearing - width/2.0
  72. gy = row * self.glyph_height + self.glyph_height/2.0 - fdescent + fheight/2
  73. if self.draw_outline or self.draw_shadow:
  74. cr.move_to(gx, gy)
  75. cr.set_line_width(2.0)
  76. cr.set_line_join(cairo.LINE_JOIN_ROUND)
  77. cr.set_source_rgb(0, 0, 0)
  78. cr.text_path(letter)
  79. cr.stroke_preserve()
  80. if self.draw_shadow:
  81. cr.set_source_rgb(0, 0, 0)
  82. else:
  83. cr.set_source_rgb(255, 255, 255)
  84. cr.fill()
  85. else:
  86. cr.set_source_rgb(255, 255, 255)
  87. cr.move_to(gx, gy)
  88. cr.show_text(letter)
  89. if __name__ == "__main__":
  90. parser = argparse.ArgumentParser(description='Font Generator')
  91. parser.add_argument('-o', '--output', metavar='FILE', type=str, required=True,
  92. help="output file")
  93. parser.add_argument('--font-size', metavar='INT', type=int,
  94. help="font size")
  95. parser.add_argument('--font', metavar='NAME', type=str, help="font")
  96. parser.add_argument('--columns', metavar='INT', type=int, help="font")
  97. parser.add_argument('--letters', metavar='NAME', type=str, help="letters")
  98. parser.add_argument('--letters-from-file', metavar='FILE', type=str, help="letters from file")
  99. parser.add_argument('--outline', action="store_true", help="draw outline")
  100. parser.add_argument('--shadow', action="store_true", help="draw shadow")
  101. parser.add_argument('--glyph-width', type=int, help="glyph width")
  102. parser.add_argument('--glyph-height', type=int, help="glyph height")
  103. parser.add_argument('--checkerboard', action="store_true", help="draw checkerboard background")
  104. args = parser.parse_args()
  105. generator = FontGenerator()
  106. if args.font_size is not None:
  107. generator.font_size = args.font_size
  108. if args.letters is not None:
  109. generator.letters = args.letters
  110. if args.letters_from_file is not None:
  111. with codecs.open(args.letters_from_file, encoding='utf-8') as fin:
  112. generator.letters = re.sub(r"\s", "", fin.read())
  113. if args.font is not None:
  114. generator.font_family = args.font
  115. if args.letters is not None:
  116. generator.letters = args.letters.decode('utf8')
  117. if args.columns is not None:
  118. generator.columns = args.columns
  119. if args.outline is not None:
  120. generator.draw_outline = args.outline
  121. if args.shadow is not None:
  122. generator.draw_shadow = args.shadow
  123. if args.checkerboard is not None:
  124. generator.checkerboard = args.checkerboard
  125. if args.glyph_width is not None:
  126. generator.glyph_width = args.glyph_width
  127. if args.glyph_height is not None:
  128. generator.glyph_height = args.glyph_height
  129. img = generator.generate_image()
  130. img.write_to_png(args.output)
  131. # EOF #