1#!/usr/bin/env python 2""" pygame.examples.freetype_misc 3 4 5Miscellaneous (or misc) means: 6 "consisting of a mixture of various things that are not 7 usually connected with each other" 8 Adjective 9 10 11All those words you read on computers, magazines, books, and such over the years? 12Probably a lot of them were constructed with... 13 14The FreeType Project: a free, high-quality and portable Font engine. 15https://freetype.org 16 17Next time you're reading something. Think of them. 18 19 20Herein lies a *BOLD* demo consisting of a mixture of various things. 21 22 Not only is it a *BOLD* demo, it's an 23 italics demo, 24 a rotated demo, 25 it's a blend, 26 and is sized to go nicely with a cup of tea*. 27 28 * also goes well with coffee. 29 30Enjoy! 31""" 32import os 33import pygame as pg 34import pygame.freetype as freetype 35 36colors = { 37 "grey_light": pg.Color(200, 200, 200), 38 "grey_dark": pg.Color(100, 100, 100), 39 "green": pg.Color(50, 255, 63), 40 "red": pg.Color(220, 30, 30), 41 "blue": pg.Color(50, 75, 245), 42} 43 44 45def run(): 46 pg.init() 47 48 fontdir = os.path.dirname(os.path.abspath(__file__)) 49 font = freetype.Font(os.path.join(fontdir, "data", "sans.ttf")) 50 51 screen = pg.display.set_mode((800, 600)) 52 screen.fill(colors["grey_light"]) 53 54 font.underline_adjustment = 0.5 55 font.pad = True 56 font.render_to( 57 screen, 58 (32, 32), 59 "Hello World", 60 colors["red"], 61 colors["grey_dark"], 62 size=64, 63 style=freetype.STYLE_UNDERLINE | freetype.STYLE_OBLIQUE, 64 ) 65 font.pad = False 66 67 font.render_to( 68 screen, 69 (32, 128), 70 "abcdefghijklm", 71 colors["grey_dark"], 72 colors["green"], 73 size=64, 74 ) 75 76 font.vertical = True 77 font.render_to(screen, (32, 200), "Vertical?", colors["blue"], None, size=32) 78 font.vertical = False 79 80 font.render_to( 81 screen, (64, 190), "Let's spin!", colors["red"], None, size=48, rotation=55 82 ) 83 84 font.render_to( 85 screen, (160, 290), "All around!", colors["green"], None, size=48, rotation=-55 86 ) 87 88 font.render_to( 89 screen, (250, 220), "and BLEND", pg.Color(255, 0, 0, 128), None, size=64 90 ) 91 92 font.render_to( 93 screen, (265, 237), "or BLAND!", pg.Color(0, 0xCC, 28, 128), None, size=64 94 ) 95 96 # Some pinwheels 97 font.origin = True 98 for angle in range(0, 360, 45): 99 font.render_to( 100 screen, (150, 420), ")", pg.Color("black"), size=48, rotation=angle 101 ) 102 font.vertical = True 103 for angle in range(15, 375, 30): 104 font.render_to( 105 screen, (600, 400), "|^*", pg.Color("orange"), size=48, rotation=angle 106 ) 107 font.vertical = False 108 font.origin = False 109 110 utext = pg.compat.as_unicode(r"I \u2665 Unicode") 111 font.render_to(screen, (298, 320), utext, pg.Color(0, 0xCC, 0xDD), None, size=64) 112 113 utext = pg.compat.as_unicode(r"\u2665") 114 font.render_to( 115 screen, (480, 32), utext, colors["grey_light"], colors["red"], size=148 116 ) 117 118 font.render_to( 119 screen, 120 (380, 380), 121 "...yes, this is an SDL surface", 122 pg.Color(0, 0, 0), 123 None, 124 size=24, 125 style=freetype.STYLE_STRONG, 126 ) 127 128 font.origin = True 129 r = font.render_to( 130 screen, 131 (100, 530), 132 "stretch", 133 pg.Color("red"), 134 None, 135 size=(24, 24), 136 style=freetype.STYLE_NORMAL, 137 ) 138 font.render_to( 139 screen, 140 (100 + r.width, 530), 141 " VERTICAL", 142 pg.Color("red"), 143 None, 144 size=(24, 48), 145 style=freetype.STYLE_NORMAL, 146 ) 147 148 r = font.render_to( 149 screen, 150 (100, 580), 151 "stretch", 152 pg.Color("blue"), 153 None, 154 size=(24, 24), 155 style=freetype.STYLE_NORMAL, 156 ) 157 font.render_to( 158 screen, 159 (100 + r.width, 580), 160 " HORIZONTAL", 161 pg.Color("blue"), 162 None, 163 size=(48, 24), 164 style=freetype.STYLE_NORMAL, 165 ) 166 167 pg.display.flip() 168 169 while 1: 170 if pg.event.wait().type in (pg.QUIT, pg.KEYDOWN, pg.MOUSEBUTTONDOWN): 171 break 172 173 pg.quit() 174 175 176if __name__ == "__main__": 177 run() 178