| 1 |
Text Formatting |
| 2 |
--------------- |
| 3 |
|
| 4 |
The :ref:`screen`'s ``draw.text()`` method has a very rich set of methods for |
| 5 |
position and formatting of text. Some examples:: |
| 6 |
|
| 7 |
screen.draw.text("Text color", (50, 30), color="orange") |
| 8 |
screen.draw.text("Font name and size", (20, 100), fontname="Boogaloo", fontsize=60) |
| 9 |
screen.draw.text("Positioned text", topright=(840, 20)) |
| 10 |
screen.draw.text("Allow me to demonstrate wrapped text.", (90, 210), width=180, lineheight=1.5) |
| 11 |
screen.draw.text("Outlined text", (400, 70), owidth=1.5, ocolor=(255,255,0), color=(0,0,0)) |
| 12 |
screen.draw.text("Drop shadow", (640, 110), shadow=(2,2), scolor="#202020") |
| 13 |
screen.draw.text("Color gradient", (540, 170), color="red", gcolor="purple") |
| 14 |
screen.draw.text("Transparency", (700, 240), alpha=0.1) |
| 15 |
screen.draw.text("Vertical text", midleft=(40, 440), angle=90) |
| 16 |
screen.draw.text("All together now:\nCombining the above options", |
| 17 |
midbottom=(427,460), width=360, fontname="Boogaloo", fontsize=48, |
| 18 |
color="#AAFF00", gcolor="#66AA00", owidth=1.5, ocolor="black", alpha=0.8) |
| 19 |
|
| 20 |
In its simplest usage, ``screen.draw.text`` requires the string you want to |
| 21 |
draw, and the position. You can either do this by passing coordinates as the |
| 22 |
second argument (which is the top left of where the text will appear), or use |
| 23 |
the positioning keyword arguments (described later):: |
| 24 |
|
| 25 |
screen.draw.text("hello world", (20, 100)) |
| 26 |
|
| 27 |
``screen.draw.text`` takes many optional keyword arguments, described below. |
| 28 |
|
| 29 |
Font name and size |
| 30 |
'''''''''''''''''' |
| 31 |
|
| 32 |
Fonts are loaded from a directory named ``fonts``, in a similar way to the |
| 33 |
handling of images and sounds. Fonts must be in ``.ttf`` format. For example:: |
| 34 |
|
| 35 |
screen.draw.text("hello world", (100, 100), fontname="Viga", fontsize=32) |
| 36 |
|
| 37 |
Keyword arguments: |
| 38 |
|
| 39 |
- ``fontname``: filename of the font to draw. By default, use the system font. |
| 40 |
- ``fontsize``: size of the font to use, in pixels. Defaults to ``24``. |
| 41 |
- ``antialias``: whether to render with antialiasing. Defaults to ``True``. |
| 42 |
|
| 43 |
Color and background color |
| 44 |
'''''''''''''''''''''''''' |
| 45 |
|
| 46 |
:: |
| 47 |
|
| 48 |
screen.draw.text("hello world", (100, 100), color=(200, 200, 200), background="gray") |
| 49 |
|
| 50 |
Keyword arguments: |
| 51 |
|
| 52 |
- ``color``: foreground color to use. Defaults to ``white``. |
| 53 |
- ``background``: background color to use. Defaults to ``None``. |
| 54 |
|
| 55 |
``color`` (as well as ``background``, ``ocolor``, ``scolor``, and |
| 56 |
``gcolor``) can be an (r, g, b) sequence such as ``(255,127,0)``, a |
| 57 |
``pygame.Color`` object, a color name such as ``"orange"``, an HTML hex |
| 58 |
color string such as ``"#FF7F00"``, or a string representing a hex color |
| 59 |
number such as ``"0xFF7F00"``. |
| 60 |
|
| 61 |
``background`` can also be ``None``, in which case the background is |
| 62 |
transparent. Unlike ``pygame.font.Font.render``, it's generally not more |
| 63 |
efficient to set a background color when calling ``screen.draw.text``. So only |
| 64 |
specify a background color if you actually want one. |
| 65 |
|
| 66 |
Colors with alpha transparency are not supported (except for the special |
| 67 |
case of invisible text with outlines or drop shadows - see below). See |
| 68 |
the ``alpha`` keyword argument for transparency. |
| 69 |
|
| 70 |
Positioning |
| 71 |
''''''''''' |
| 72 |
|
| 73 |
:: |
| 74 |
|
| 75 |
screen.draw.text("hello world", centery=50, right=300) |
| 76 |
screen.draw.text("hello world", midtop=(400, 0)) |
| 77 |
|
| 78 |
Keyword arguments: |
| 79 |
|
| 80 |
:: |
| 81 |
|
| 82 |
top left bottom right |
| 83 |
topleft bottomleft topright bottomright |
| 84 |
midtop midleft midbottom midright |
| 85 |
center centerx centery |
| 86 |
|
| 87 |
Positioning keyword arguments behave like the corresponding properties |
| 88 |
of ``pygame.Rect``. Either specify two arguments, corresponding to the |
| 89 |
horizontal and vertical positions of the box, or a single argument that |
| 90 |
specifies both. |
| 91 |
|
| 92 |
If the position is overspecified (e.g. both ``left`` and ``right`` are |
| 93 |
given), then extra specifications will be (arbitrarily but |
| 94 |
deterministically) discarded. For constrained text, see the section on |
| 95 |
``screen.draw.textbox`` below. |
| 96 |
|
| 97 |
Word wrap |
| 98 |
''''''''' |
| 99 |
|
| 100 |
:: |
| 101 |
|
| 102 |
screen.draw.text("splitting\nlines", (100, 100)) |
| 103 |
screen.draw.text("splitting lines", (100, 100), width=60) |
| 104 |
|
| 105 |
Keyword arguments: |
| 106 |
|
| 107 |
- ``width``: maximum width of the text to draw, in pixels. Defaults to |
| 108 |
``None``. |
| 109 |
- ``widthem``: maximum width of the text to draw, in font-based em |
| 110 |
units. Defaults to ``None``. |
| 111 |
- ``lineheight``: vertical spacing between lines, in units of the |
| 112 |
font's default line height. Defaults to ``1.0``. |
| 113 |
|
| 114 |
``screen.draw.text`` will always wrap lines at newline (``\n``) characters. If |
| 115 |
``width`` or ``widthem`` is set, it will also try to wrap lines in order |
| 116 |
to keep each line shorter than the given width. The text is not |
| 117 |
guaranteed to be within the given width, because wrapping only occurs at |
| 118 |
space characters, so if a single word is too long to fit on a line, it |
| 119 |
will not be broken up. Outline and drop shadow are also not accounted |
| 120 |
for, so they may extend beyond the given width. |
| 121 |
|
| 122 |
You can prevent wrapping on a particular space with non-breaking space |
| 123 |
characters (``\u00A0``). |
| 124 |
|
| 125 |
Text alignment |
| 126 |
'''''''''''''' |
| 127 |
|
| 128 |
:: |
| 129 |
|
| 130 |
screen.draw.text("hello\nworld", bottomright=(500, 400), align="left") |
| 131 |
|
| 132 |
Keyword argument: |
| 133 |
|
| 134 |
- ``align``: horizontal positioning of lines with respect to each |
| 135 |
other. Defaults to ``None``. |
| 136 |
|
| 137 |
``align`` determines how lines are positioned horizontally with respect |
| 138 |
to each other, when more than one line is drawn. Valid values for |
| 139 |
``align`` are the strings ``"left"``, ``"center"``, or ``"right"``, a |
| 140 |
numerical value between ``0.0`` (for left alignment) and ``1.0`` (for |
| 141 |
right alignment), or ``None``. |
| 142 |
|
| 143 |
If ``align`` is ``None``, the alignment is determined based on other arguments, |
| 144 |
in a way that should be what you want most of the time. It depends on any |
| 145 |
positioning arguments (``topleft``, ``centerx``, etc.), ``anchor``, and finally |
| 146 |
defaults to ``"left"``. I suggest you generally trust the default alignment, |
| 147 |
and only specify ``align`` if something doesn't look right. |
| 148 |
|
| 149 |
Outline |
| 150 |
''''''' |
| 151 |
|
| 152 |
:: |
| 153 |
|
| 154 |
screen.draw.text("hello world", (100, 100), owidth=1, ocolor="blue") |
| 155 |
|
| 156 |
Keyword arguments: |
| 157 |
|
| 158 |
- ``owidth``: outline thickness, in outline units. Defaults to |
| 159 |
``None``. |
| 160 |
- ``ocolor``: outline color. Defaults to ``"black"``. |
| 161 |
|
| 162 |
The text will be outlined if ``owidth`` is specified. The outlining is a |
| 163 |
crude manual method, and will probably look bad at large sizes. The |
| 164 |
units of ``owidth`` are chosen so that ``1.0`` is a good typical value |
| 165 |
for outlines. Specifically, they're the font size divided by 24. |
| 166 |
|
| 167 |
As a special case, setting ``color`` to a transparent value (e.g. |
| 168 |
``(0,0,0,0)``) while using outilnes will cause the text to be invisible, |
| 169 |
giving a hollow outline. (This feature is not compatible with |
| 170 |
``gcolor``.) |
| 171 |
|
| 172 |
Valid values for ``ocolor`` are the same as for ``color``. |
| 173 |
|
| 174 |
Drop shadow |
| 175 |
''''''''''' |
| 176 |
|
| 177 |
:: |
| 178 |
|
| 179 |
screen.draw.text("hello world", (100, 100), shadow=(1.0,1.0), scolor="blue") |
| 180 |
|
| 181 |
Keyword arguments: |
| 182 |
|
| 183 |
- ``shadow``: (x,y) values representing the drop shadow offset, in |
| 184 |
shadow units. Defaults to ``None``. |
| 185 |
- ``scolor``: drop shadow color. Defaults to ``"black"``. |
| 186 |
|
| 187 |
The text will have a drop shadow if ``shadow`` is specified. It must be |
| 188 |
set to a 2-element sequence representing the x and y offsets of the drop |
| 189 |
shadow, which can be positive, negative, or 0. For example, |
| 190 |
``shadow=(1.0,1.0)`` corresponds to a shadow down and to the right of |
| 191 |
the text. ``shadow=(0,-1.2)`` corresponds to a shadow higher than the |
| 192 |
text. |
| 193 |
|
| 194 |
The units of ``shadow`` are chosen so that ``1.0`` is a good typical |
| 195 |
value for the offset. Specifically, they're the font size divided by 18. |
| 196 |
|
| 197 |
As a special case, setting ``color`` to a transparent value (e.g. |
| 198 |
``(0,0,0,0)``) while using drop shadow will cause the text to be |
| 199 |
invisible, giving a hollow shadow. (This feature is not compatible with |
| 200 |
``gcolor``.) |
| 201 |
|
| 202 |
Valid values for ``scolor`` are the same as for ``color``. |
| 203 |
|
| 204 |
Gradient color |
| 205 |
'''''''''''''' |
| 206 |
|
| 207 |
:: |
| 208 |
|
| 209 |
screen.draw.text("hello world", (100, 100), color="black", gcolor="green") |
| 210 |
|
| 211 |
Keyword argument: |
| 212 |
|
| 213 |
- ``gcolor``: Lower gradient stop color. Defaults to ``None``. |
| 214 |
|
| 215 |
Specify ``gcolor`` to color the text with a vertical color gradient. The |
| 216 |
text's color will be ``color`` at the top and ``gcolor`` at the bottom. |
| 217 |
Positioning of the gradient stops and orientation of the gradient are |
| 218 |
hard coded and cannot be specified. |
| 219 |
|
| 220 |
Requries ``pygame.surfarray`` module, which uses numpy or Numeric |
| 221 |
library. |
| 222 |
|
| 223 |
Alpha transparency |
| 224 |
'''''''''''''''''' |
| 225 |
|
| 226 |
:: |
| 227 |
|
| 228 |
screen.draw.text("hello world", (100, 100), alpha=0.5) |
| 229 |
|
| 230 |
Keyword argument: |
| 231 |
|
| 232 |
- ``alpha``: alpha transparency value, between 0 and 1. Defaults to |
| 233 |
``1.0``. |
| 234 |
|
| 235 |
In order to maximize reuse of cached transparent surfaces, the value of |
| 236 |
``alpha`` is rounded. |
| 237 |
|
| 238 |
Requires ``pygame.surfarray`` module, which uses numpy or Numeric |
| 239 |
library. |
| 240 |
|
| 241 |
Anchored positioning |
| 242 |
'''''''''''''''''''' |
| 243 |
|
| 244 |
:: |
| 245 |
|
| 246 |
screen.draw.text("hello world", (100, 100), anchor=(0.3,0.7)) |
| 247 |
|
| 248 |
Keyword argument: |
| 249 |
|
| 250 |
- ``anchor``: a length-2 sequence of horizontal and vertical anchor |
| 251 |
fractions. Defaults to ``(0.0, 0.0)``. |
| 252 |
|
| 253 |
``anchor`` specifies how the text is anchored to the given position, |
| 254 |
when no positioning keyword arguments are passed. The two values in |
| 255 |
``anchor`` can take arbitrary values between ``0.0`` and ``1.0``. An |
| 256 |
``anchor`` value of ``(0,0)``, the default, means that the given |
| 257 |
position is the top left of the text. A value of ``(1,1)`` means the |
| 258 |
given position is the bottom right of the text. |
| 259 |
|
| 260 |
Rotation |
| 261 |
'''''''' |
| 262 |
|
| 263 |
:: |
| 264 |
|
| 265 |
screen.draw.text("hello world", (100, 100), angle=10) |
| 266 |
|
| 267 |
Keyword argument: |
| 268 |
|
| 269 |
- ``angle``: counterclockwise rotation angle in degrees. Defaults to |
| 270 |
``0``. |
| 271 |
|
| 272 |
Positioning of rotated surfaces is tricky. When drawing rotated text, the |
| 273 |
anchor point, the position you actually specify, remains fixed, and the text |
| 274 |
rotates around it. For instance, if you specify the top left of the text to be |
| 275 |
at ``(100, 100)`` with an angle of ``90``, then the Surface will actually be |
| 276 |
drawn so that its bottom left is at ``(100, 100)``. |
| 277 |
|
| 278 |
If you find that confusing, try specifying the center. If you anchor the |
| 279 |
text at the center, then the center will remain fixed, no matter how you |
| 280 |
rotate it. |
| 281 |
|
| 282 |
In order to maximize reuse of cached rotated surfaces, the value of |
| 283 |
``angle`` is rounded to the nearest multiple of 3 degrees. |
| 284 |
|
| 285 |
|
| 286 |
Constrained text |
| 287 |
'''''''''''''''' |
| 288 |
|
| 289 |
:: |
| 290 |
|
| 291 |
screen.draw.textbox("hello world", (100, 100, 200, 50)) |
| 292 |
|
| 293 |
``screen.draw.textbox`` requires two arguments: the text to be drawn, and a |
| 294 |
``pygame.Rect`` or a ``Rect``-like object to stay within. The font size |
| 295 |
will be chosen to be as large as possible while staying within the box. |
| 296 |
Other than ``fontsize`` and positional arguments, you can pass all the |
| 297 |
same keyword arguments to ``screen.draw.textbox`` as to ``screen.draw.text``. |