Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""":mod:`wand.font` --- Fonts 

2~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

3 

4.. versionadded:: 0.3.0 

5 

6:class:`Font` is an object which takes the :attr:`~Font.path` of font file, 

7:attr:`~Font.size`, :attr:`~Font.color`, and whether to use 

8:attr:`~Font.antialias`\\ ing. If you want to use font by its name rather 

9than the file path, use TTFQuery_ package. The font path resolution by its 

10name is a very complicated problem to achieve. 

11 

12.. seealso:: 

13 

14 TTFQuery_ --- Find and Extract Information from TTF Files 

15 TTFQuery builds on the `FontTools-TTX`_ package to allow the Python 

16 programmer to accomplish a number of tasks: 

17 

18 - query the system to find installed fonts 

19 

20 - retrieve metadata about any TTF font file 

21 

22 - this includes the glyph outlines (shape) of individual code-points, 

23 which allows for rendering the glyphs in 3D (such as is done in 

24 OpenGLContext) 

25 

26 - lookup/find fonts by: 

27 

28 - abstract family type 

29 - proper font name 

30 

31 - build simple metadata registries for run-time font matching 

32 

33.. _TTFQuery: http://ttfquery.sourceforge.net/ 

34.. _FontTools-TTX: http://sourceforge.net/projects/fonttools/ 

35 

36""" 

37from . import assertions 

38from .color import Color 

39from .compat import string_type, text 

40 

41__all__ = ('Font',) 

42 

43 

44class Font(tuple): 

45 """Font struct which is a subtype of :class:`tuple`. 

46 

47 :param path: the path of the font file 

48 :type path: :class:`str`, :class:`basestring` 

49 :param size: the size of typeface. 0 by default which means *autosized* 

50 :type size: :class:`numbers.Real` 

51 :param color: the color of typeface. black by default 

52 :type color: :class:`~wand.color.Color` 

53 :param antialias: whether to use antialiasing. :const:`True` by default 

54 :type antialias: :class:`bool` 

55 :param stroke_color: optional color to outline typeface. 

56 :type stroke_color: :class:`~wand.color.Color` 

57 :param stroke_width: optional thickness of typeface outline. 

58 :type stroke_width: :class:`numbers.Real` 

59 

60 .. versionchanged:: 0.3.9 

61 The ``size`` parameter becomes optional. Its default value is 

62 0, which means *autosized*. 

63 

64 .. versionchanged:: 0.5.0 

65 Added ``stroke_color`` & ``stoke_width`` paramaters. 

66 """ 

67 

68 def __new__(cls, path, size=0, color=None, antialias=True, 

69 stroke_color=None, stroke_width=None): 

70 assertions.assert_string(path=path) 

71 assertions.assert_real(size=size) 

72 if color is None: 

73 color = Color('black') 

74 elif isinstance(color, string_type): 

75 color = Color(color) 

76 assertions.assert_color(color=color) 

77 if stroke_color: 

78 if isinstance(stroke_color, string_type): 

79 stroke_color = Color(stroke_color) 

80 assertions.assert_color(stroke_color=stroke_color) 

81 if stroke_width is not None: 

82 assertions.assert_real(stroke_width=stroke_width) 

83 path = text(path) 

84 return tuple.__new__(cls, (path, size, color, bool(antialias), 

85 stroke_color, stroke_width)) 

86 

87 @property 

88 def path(self): 

89 """(:class:`basestring`) The path of font file.""" 

90 return self[0] 

91 

92 @property 

93 def size(self): 

94 """(:class:`numbers.Real`) The font size in pixels.""" 

95 return self[1] 

96 

97 @property 

98 def color(self): 

99 """(:class:`wand.color.Color`) The font color.""" 

100 return self[2] 

101 

102 @property 

103 def antialias(self): 

104 """(:class:`bool`) Whether to apply antialiasing (``True``) 

105 or not (``False``). 

106 

107 """ 

108 return self[3] 

109 

110 @property 

111 def stroke_color(self): 

112 """(:class:`wand.color.Color`) The stroke color.""" 

113 return self[4] 

114 

115 @property 

116 def stroke_width(self): 

117 """(:class:`numbers.Real`) The width of the stroke line.""" 

118 return self[5] 

119 

120 def __repr__(self): 

121 return '{0.__module__}.{0.__name__}({1})'.format( 

122 type(self), 

123 tuple.__repr__(self) 

124 )