| 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 |
|
| 10 |
|
| 11 |
|
| 12 |
from pyglet.font.dwrite import DirectWriteGlyphRenderer |
| 13 |
|
| 14 |
D2D1_TEXT_ANTIALIAS_MODE_DEFAULT = 0 |
| 15 |
D2D1_TEXT_ANTIALIAS_MODE_CLEARTYPE = 1 |
| 16 |
D2D1_TEXT_ANTIALIAS_MODE_GRAYSCALE = 2 |
| 17 |
D2D1_TEXT_ANTIALIAS_MODE_ALIASED = 3 |
| 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 |
|
| 43 |
|
| 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() |