font.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. """pygame module for loading and rendering fonts (freetype alternative)"""
  2. __all__ = ['Font', 'init', 'quit', 'get_default_font', 'get_init', 'SysFont']
  3. from pygame._freetype import init, Font as _Font, get_default_resolution
  4. from pygame._freetype import quit, get_default_font, get_init as _get_init
  5. from pygame._freetype import __PYGAMEinit__
  6. from pygame.sysfont import match_font, get_fonts, SysFont as _SysFont
  7. from pygame import encode_file_path
  8. from pygame.compat import bytes_, unicode_, as_unicode, as_bytes
  9. from pygame import Surface as _Surface, Color as _Color, SRCALPHA as _SRCALPHA
  10. class Font(_Font):
  11. """Font(filename, size) -> Font
  12. Font(object, size) -> Font
  13. create a new Font object from a file (freetype alternative)
  14. This Font type differs from font.Font in that it can render glyphs
  15. for Unicode code points in the supplementary planes (> 0xFFFF).
  16. """
  17. __encode_file_path = staticmethod(encode_file_path)
  18. __get_default_resolution = staticmethod(get_default_resolution)
  19. __default_font = encode_file_path(get_default_font())
  20. __unull = as_unicode(r"\x00")
  21. __bnull = as_bytes("\x00")
  22. def __init__(self, file, size=-1):
  23. if size <= 1:
  24. size = 1
  25. if isinstance(file, unicode_):
  26. try:
  27. bfile = self.__encode_file_path(file, ValueError)
  28. except ValueError:
  29. bfile = ''
  30. else:
  31. bfile = file
  32. if isinstance(bfile, bytes_) and bfile == self.__default_font:
  33. file = None
  34. if file is None:
  35. resolution = int(self.__get_default_resolution() * 0.6875)
  36. if resolution == 0:
  37. kwds['resolution'] = 1
  38. else:
  39. resolution = 0
  40. super(Font, self).__init__(file, size=size, resolution=resolution)
  41. self.strength = 1.0 / 12.0
  42. self.kerning = False
  43. self.origin = True
  44. self.pad = True
  45. self.ucs4 = True
  46. self.underline_adjustment = 1.0
  47. def render(self, text, antialias, color, background=None):
  48. """render(text, antialias, color, background=None) -> Surface
  49. draw text on a new Surface"""
  50. if text is None:
  51. text = ""
  52. if (isinstance(text, unicode_) and # conditional and
  53. self.__unull in text):
  54. raise ValueError("A null character was found in the text")
  55. if (isinstance(text, bytes_) and # conditional and
  56. self.__bnull in text):
  57. raise ValueError("A null character was found in the text")
  58. save_antialiased = self.antialiased
  59. self.antialiased = bool(antialias)
  60. try:
  61. s, r = super(Font, self).render(text, color, background)
  62. return s
  63. finally:
  64. self.antialiased = save_antialiased
  65. def set_bold(self, value):
  66. """set_bold(bool) -> None
  67. enable fake rendering of bold text"""
  68. self.wide = bool(value)
  69. def get_bold(self):
  70. """get_bold() -> bool
  71. check if text will be rendered bold"""
  72. return self.wide
  73. def set_italic(self, value):
  74. """set_italic(bool) -> None
  75. enable fake rendering of italic text"""
  76. self.oblique = bool(value)
  77. def get_italic(self):
  78. """get_italic() -> bool
  79. check if the text will be rendered italic"""
  80. return self.oblique
  81. def set_underline(self, value):
  82. """set_underline(bool) -> None
  83. control if text is rendered with an underline"""
  84. self.underline = bool(value)
  85. def get_underline(self):
  86. """set_bold(bool) -> None
  87. enable fake rendering of bold text"""
  88. return self.underline
  89. def metrics(self, text):
  90. """metrics(text) -> list
  91. Gets the metrics for each character in the pased string."""
  92. return self.get_metrics(text)
  93. def get_ascent(self):
  94. """get_ascent() -> int
  95. get the ascent of the font"""
  96. return self.get_sized_ascender()
  97. def get_descent(self):
  98. """get_descent() -> int
  99. get the descent of the font"""
  100. return self.get_sized_descender()
  101. def get_height(self):
  102. """get_height() -> int
  103. get the height of the font"""
  104. return self.get_sized_ascender() - self.get_sized_descender() + 1
  105. def get_linesize(self):
  106. """get_linesize() -> int
  107. get the line space of the font text"""
  108. return self.get_sized_height();
  109. def size(self, text):
  110. """size(text) -> (width, height)
  111. determine the amount of space needed to render text"""
  112. return self.get_rect(text).size
  113. FontType = Font
  114. def get_init():
  115. """get_init() -> bool
  116. true if the font module is initialized"""
  117. return _get_init()
  118. def SysFont(name, size, bold=0, italic=0, constructor=None):
  119. """pygame.ftfont.SysFont(name, size, bold=False, italic=False, constructor=None) -> Font
  120. create a pygame Font from system font resources (freetype alternative)
  121. This will search the system fonts for the given font
  122. name. You can also enable bold or italic styles, and
  123. the appropriate system font will be selected if available.
  124. This will always return a valid Font object, and will
  125. fallback on the builtin pygame font if the given font
  126. is not found.
  127. Name can also be a comma separated list of names, in
  128. which case set of names will be searched in order. Pygame
  129. uses a small set of common font aliases, if the specific
  130. font you ask for is not available, a reasonable alternative
  131. may be used.
  132. if optional contructor is provided, it must be a function with
  133. signature constructor(fontpath, size, bold, italic) which returns
  134. a Font instance. If None, a pygame.ftfont.Font object is created.
  135. """
  136. if constructor is None:
  137. def constructor(fontpath, size, bold, italic):
  138. font = Font(fontpath, size)
  139. font.set_bold(bold)
  140. font.set_italic(italic)
  141. return font
  142. return _SysFont(name, size, bold, italic, constructor)
  143. del _Font, get_default_resolution, encode_file_path, as_unicode, as_bytes