|
| 1 | +# Test font rendering using the FontTTF class |
| 2 | + |
| 3 | +# This example allows you to try typing out some text in a given font, and |
| 4 | +# supports the following keys: |
| 5 | +# - Left/Right Arrow Keys: Cycles text alignment (left/right/center) |
| 6 | +# - Up/Down Arrow Keys: Increases/decreases line height |
| 7 | +# - Tab: Cycles font rendering style |
| 8 | + |
| 9 | +import os |
| 10 | +import sys |
| 11 | +import sdl2.ext |
| 12 | +from sdl2.sdlttf import TTF_FontLineSkip |
| 13 | +from sdl2.ext import FontTTF |
| 14 | + |
| 15 | +filepath = os.path.abspath(os.path.dirname(__file__)) |
| 16 | +RESOURCES = sdl2.ext.Resources(filepath, "resources") |
| 17 | +BLACK_RGBA = (0, 0, 0, 255) |
| 18 | +WHITE_RGBA = (255, 255, 255, 255) |
| 19 | + |
| 20 | + |
| 21 | +def update_text(renderer, surface): |
| 22 | + # Create a texture for the surface and render it to the screen |
| 23 | + tx = sdl2.ext.Texture(renderer, surface) |
| 24 | + renderer.clear(BLACK_RGBA) |
| 25 | + renderer.copy(tx, dstrect=(10, 10)) |
| 26 | + renderer.present() |
| 27 | + |
| 28 | + |
| 29 | +def run(): |
| 30 | + # Initialize SDL2 and create a Window and Renderer |
| 31 | + sdl2.ext.init() |
| 32 | + window = sdl2.ext.Window("Font Rendering", size=(800, 500)) |
| 33 | + renderflags = sdl2.SDL_RENDERER_SOFTWARE |
| 34 | + if "-hardware" in sys.argv: |
| 35 | + renderflags = ( |
| 36 | + sdl2.SDL_RENDERER_ACCELERATED | sdl2.SDL_RENDERER_PRESENTVSYNC |
| 37 | + ) |
| 38 | + renderer = sdl2.ext.Renderer(window, flags=renderflags) |
| 39 | + window.show() |
| 40 | + |
| 41 | + # Create and initialize a font to render text with |
| 42 | + fontpath = RESOURCES.get_path("tuffy.ttf") |
| 43 | + font = FontTTF(fontpath, "20px", WHITE_RGBA) |
| 44 | + |
| 45 | + # Add some additional font styles |
| 46 | + styles = ['default', 'small', 'red', 'large', 'bg_fill'] |
| 47 | + font.add_style('small', '10px', WHITE_RGBA) |
| 48 | + font.add_style('red', '20px', (255, 0, 0)) |
| 49 | + font.add_style('large', '35px', WHITE_RGBA) |
| 50 | + font.add_style('bg_fill', '20px', BLACK_RGBA, WHITE_RGBA) |
| 51 | + |
| 52 | + # Initialize font rendering options |
| 53 | + line_height = TTF_FontLineSkip(font.get_ttf_font()) |
| 54 | + alignments = ["left", "center", "right"] |
| 55 | + align_idx = 0 |
| 56 | + style_idx = 0 |
| 57 | + |
| 58 | + # Set a default string with which to render text |
| 59 | + txt = u"Hi There!\nYou can edit this text using the keyboard and delete keys." |
| 60 | + |
| 61 | + # Render the text and present it on the screen |
| 62 | + txt_rendered = font.render_text(txt, width=780) |
| 63 | + update_text(renderer, txt_rendered) |
| 64 | + |
| 65 | + # Tell SDL2 to start reading Text Editing events. This allows for proper |
| 66 | + # handling of unicode characters and modifier keys. |
| 67 | + sdl2.SDL_StartTextInput() |
| 68 | + |
| 69 | + # Create a simple event loop and wait for keydown, text editing, and quit events. |
| 70 | + running = True |
| 71 | + while running: |
| 72 | + events = sdl2.ext.get_events() |
| 73 | + for event in events: |
| 74 | + update_txt = False |
| 75 | + if event.type == sdl2.SDL_QUIT: |
| 76 | + running = False |
| 77 | + break |
| 78 | + |
| 79 | + # Handle non-standard keyboard events |
| 80 | + elif event.type == sdl2.SDL_KEYDOWN: |
| 81 | + update_txt = True |
| 82 | + sdl_keysym = event.key.keysym.sym |
| 83 | + # If backspace pressed, remove last character (if any) from txt |
| 84 | + if sdl_keysym == sdl2.SDLK_BACKSPACE: |
| 85 | + txt = txt[:-1] |
| 86 | + # If enter/return pressed, insert a newline |
| 87 | + elif sdl_keysym == sdl2.SDLK_RETURN: |
| 88 | + txt = txt + u"\n" |
| 89 | + # If left or right arrow pressed, change text alignment mode |
| 90 | + elif sdl_keysym == sdl2.SDLK_LEFT: |
| 91 | + align_idx = (align_idx - 1) % 3 |
| 92 | + elif sdl_keysym == sdl2.SDLK_RIGHT: |
| 93 | + align_idx = (align_idx + 1) % 3 |
| 94 | + elif sdl_keysym == sdl2.SDLK_UP: |
| 95 | + line_height += 1 |
| 96 | + elif sdl_keysym == sdl2.SDLK_DOWN: |
| 97 | + if line_height > 1: |
| 98 | + line_height -= 1 |
| 99 | + # If tab pressed, cycle through the different font styles |
| 100 | + elif sdl_keysym == sdl2.SDLK_TAB: |
| 101 | + style_idx = (style_idx + 1) % len(styles) |
| 102 | + |
| 103 | + # Handle text input events |
| 104 | + elif event.type == sdl2.SDL_TEXTINPUT: |
| 105 | + update_txt = True |
| 106 | + txt += event.text.text.decode("utf-8") |
| 107 | + |
| 108 | + # If txt has changed since the start of the loop, update the renderer |
| 109 | + if update_txt: |
| 110 | + align = alignments[align_idx] |
| 111 | + style = styles[style_idx] |
| 112 | + txt_rendered = font.render_text( |
| 113 | + txt, style, width=780, line_h=line_height, align=align |
| 114 | + ) |
| 115 | + update_text(renderer, txt_rendered) |
| 116 | + |
| 117 | + # Now that we're done, close everything down and quit SDL2 |
| 118 | + font.close() |
| 119 | + renderer.destroy() |
| 120 | + window.close() |
| 121 | + sdl2.ext.quit() |
| 122 | + return 0 |
| 123 | + |
| 124 | +if __name__ == "__main__": |
| 125 | + sys.exit(run()) |
0 commit comments