Debian Codegraph @

examples/text/advanced_font.py

raw symbols SCIP
1 """Example of advanced font rendering features.
2 mostliky for windows with DirectWrite, other than that, might not work as expected."""
3
4 import warnings
5 import pyglet
6 import os
7
8 if os.name == "nt":
9 # On Windows, it's possible to change the font anti-aliasing mode.
10 # Uncomment the below lines to set the options:
11 #
12 from pyglet.font.dwrite import DirectWriteGlyphRenderer
13
14 D2D1_TEXT_ANTIALIAS_MODE_DEFAULT = 0
15 D2D1_TEXT_ANTIALIAS_MODE_CLEARTYPE = 1 # not working?
16 D2D1_TEXT_ANTIALIAS_MODE_GRAYSCALE = 2 # same as default
17 D2D1_TEXT_ANTIALIAS_MODE_ALIASED = 3 # looks worse than grayscale
18 DirectWriteGlyphRenderer.antialias_mode = D2D1_TEXT_ANTIALIAS_MODE_DEFAULT
19 else:
20 warnings.warn("This example works best on Windows with DirectWrite, other platforms might not work as expected.")
21 exit(0)
22
23 window = pyglet.window.Window()
24 batch = pyglet.graphics.Batch()
25
26 float_font_size = pyglet.text.Label("Hello World on size 15.1", font_name="Arial", font_size=15.1, x=30, y=500, batch=batch)
27 float_font_size_1 = pyglet.text.Label("Hello World on size 15", font_name="Arial", font_size=15, x=270, y=500, batch=batch)
28
29 arial_bold = pyglet.text.Label("Hello World 👽", font_name="Arial", weight="normal", font_size=25, x=50, y=400, batch=batch)
30 arial_black = pyglet.text.Label("Hello World 👾", font_name="Arial", weight="black", font_size=25, x=50, y=350, batch=batch)
31 arial_narrow = pyglet.text.Label("Hello World 🤖", font_name="Arial", weight="normal", stretch="condensed", font_size=25, x=50, y=300, batch=batch)
32 arial = pyglet.text.Label("Hello World 👀", font_name="Arial", font_size=25, x=50, y=250, batch=batch)
33
34 segoe_ui_black = pyglet.text.Label("Hello World ☂️", font_name="Segoe UI", weight="black", font_size=25, x=50, y=200, batch=batch)
35 segoe_ui_semilbold = pyglet.text.Label("Hello World ⚽️", font_name="Segoe UI", weight="semibold", font_size=25, x=50, y=150, batch=batch)
36 segoe_ui_semilight = pyglet.text.Label("Hello World 🎱", font_name="Segoe UI", weight="semilight", font_size=25, x=50, y=100, batch=batch)
37 segoe_ui_light = pyglet.text.Label("Hello World 🥳👍", font_name="Segoe UI", weight="light", font_size=25, x=50, y=50, batch=batch)
38 segoe_ui = pyglet.text.Label("Hello World 😀✌", font_name="Segoe UI", font_size=25, x=50, y=10, batch=batch)
39
40 if os.name == "nt":
41 if not pyglet.options.win32_gdi_font:
42 # On Windows DirectWrite can render directly to an image for special cases!
43 # Note: Labels are recommended unless you know what you are doing, or if you use these in a limited fashion.
44 font = pyglet.font.load("Segoe UI")
45 image = font.render_to_image("I am rendered as a texture! 🌎", 100, 300)
46 sprite = pyglet.sprite.Sprite(image, x=400, y=400, batch=batch)
47 sprite.rotation = 45
48 else:
49 warnings.warn("Text into image rendering is only with DirectWrite enabled, GDI can't do it")
50 else:
51 warnings.warn("Text into image rendering is only supported on Windows with DirectWrite enabled.")
52
53
54 @window.event
55 def on_draw():
56 window.clear()
57 batch.draw()
58
59
60 pyglet.app.run()