def __init__(self, title):
pygame.display.set_caption(title)
self.screen.fill(Color('white'))
pygame.display.flip()
pygame.freetype.init()
self.font = pygame.freetype.Font(None, 20)
self.font.origin = True
self.ascender = int(self.font.get_sized_ascender() * 1.5)
self.descender = int(self.font.get_sized_descender() * 1.5)
self.line_height = self.ascender - self.descender
self.write_lines("'q', ESCAPE or close this window to quit\n"
"SPACE to play/pause\n"
"'r' to rewind\n"
"'f' to faid out over 5 seconds\n", 0)
python类freetype()的实例源码
def draw(screen, etc):
global string, word, color, scrollX, gs, text, size
size = int(etc.knob1*200)+1
color = etc.color_picker()
font = pygame.freetype.Font(etc.mode_root + "/font.ttf", size)
(text, textpos1) = font.render(string, color)
if etc.audio_trig or etc.midi_note_new :
string = unicode(subjList[random.randint(0,3)]) + unicode(verbList[random.randint(0,5)]) +unicode(artList[random.randint(0,3)]) + unicode(adjList[random.randint(0,4)]) +unicode(nounList[random.randint(0,6)])
gs = 1
scrollX = 1280 + text.get_width()
if gs == 1 and size > 0 :
font = pygame.freetype.Font(etc.mode_root + "/font.ttf", size)
#audio movement
textpos1 =(scrollX - (text.get_width()+1), (int(600 * etc.knob2)) )
screen.blit(text, textpos1)
scrollX = scrollX - (etc.knob3*40) + 1
if scrollX < 1 : scrollX = 1
def test_freetype_Font_path(self):
self.assertEqual(self._TEST_FONTS['sans'].path, self._sans_path)
self.assertRaises(AttributeError, getattr, nullfont(), 'path')
# This Font cache test is conditional on freetype being built by a debug
# version of Python or with the C macro PGFT_DEBUG_CACHE defined.
def test_issue_243(self):
"""Issue Y: trailing space ignored in boundary calculation"""
# Issue #243: For a string with trailing spaces, freetype ignores the
# last space in boundary calculations
#
font = self._TEST_FONTS['fixed']
r1 = font.get_rect(" ", size=64)
self.assertTrue(r1.width > 1)
r2 = font.get_rect(" ", size=64)
self.assertEqual(r2.width, 2 * r1.width)
def loadfont(size):
global font
font = pygame.freetype.Font('font/De Valencia (beta).otf', size)
# Utility functions
def font_data(data_dir):
return resource_handler(data_dir,'.ttf', pygame.freetype.Font)
def __init__(self, path="res/RobotoCondensed-Regular.ttf", minSize=10, maxSize=30):
self.path = path
curr_size = minSize
self.sizes = {}
self.ft_support = True
self.ft_sizes = {}
while curr_size <= maxSize:
if self.ft_support:
try:
self.ft_sizes[curr_size] = pygame.freetype.Font(path, curr_size)
except:
self.ft_support = False
self.sizes[curr_size] = pygame.font.Font(path, curr_size)
curr_size += 1
def get(self, size=14, ft=False):
if ft and self.ft_support:
if size not in self.ft_sizes:
self.ft_sizes[size] = pygame.freetype.Font(self.path, size)
return self.ft_sizes[size]
else:
if size not in self.sizes:
self.sizes[size] = pygame.font.Font(self.path, size)
return self.sizes[size]
def __init__(self, position, text, color=DEFAULT, size=DEFAULT, **data):
#Defaults are "item" and 14.
color, size = GUI.Component.default(color, state.getColorPalette().getColor("item"), size, 14)
self.text = text
self._originalText = text
self.size = size
self.color = color
self.font = data.get("font", state.getFont())
self.use_freetype = data.get("freetype", False)
self.responsive_width = data.get("responsive_width", True)
data["surface"] = self.getRenderedText()
super(GUI.Text, self).__init__(position, **data)
def __init__(self, position, text, color=DEFAULT, size=DEFAULT, justification=DEFAULT, **data):
#Defaults are "item", and 0 (left).
color, size, justification = GUI.Component.default(color, state.getColorPalette().getColor("item"), size, 14,
justification, 0)
self.justification = justification
self.color = color
self.size = size
self.text = text if type(text) == str or type(text) == unicode else str(text)
self.textSurface = None
self.font = data.get("font", state.getFont())
self.use_freetype = data.get("freetype", False)
super(GUI.MultiLineText, self).__init__(position, **data)
self.refresh()
if self.width > state.getGUI().width:
self.width = state.getGUI().width
def __init__(self, position, text, bgColor=DEFAULT, textColor=DEFAULT, textSize=DEFAULT, **data):
#Defaults are "darker:background", "item", and 14.
bgColor, textColor, textSize = GUI.Component.default(bgColor, state.getColorPalette().getColor("darker:background"),
textColor, state.getColorPalette().getColor("item"),
textSize, 14)
self.textComponent = GUI.Text((0, 0), text, textColor, textSize, font=data.get("font", state.getFont()), freetype=data.get("freetype", False))
self.paddingAmount = data.get("padding", 5)
if "width" not in data: data["width"] = self.textComponent.computedWidth + (2 * self.paddingAmount)
if "height" not in data: data["height"] = self.textComponent.computedHeight + (2 * self.paddingAmount)
super(GUI.Button, self).__init__(position, **data)
self.SKIP_CHILD_CHECK = True
self.textComponent.setPosition(GUI.getCenteredCoordinates(self.textComponent, self))
self.backgroundColor = bgColor
self.addChild(self.textComponent)
def setup(screen, etc):
pygame.freetype.init()
pass
def draw(screen, etc) :
global xpos, ypos, xdirection, ydirection, rIght, bOttom, lEft, tOp, unistr, word, coloryo, avg
color = etc.color_picker()
auDio = etc.audio_peak / 128
if auDio > 255 : auDio = 255
coloryo = (auDio,color[1],color[2])
size = int(100*etc.knob3)+1
font = pygame.freetype.Font(etc.mode_root + "/font.ttf", size)
unistr = unicode(stringList[word])
(text, textpos) = font.render(unistr, (coloryo))
xspeed = int(etc.knob1 * 50) + 1
yspeed = int(etc.knob2 * 50) + 1
xpos = xpos + (xspeed * xdirection)
ypos = ypos + (yspeed * ydirection)
rIght = xpos + text.get_width()
bOttom = ypos + text.get_height()
lEft = xpos
tOp = ypos
screen.blit(text, (xpos,ypos))
if rIght >= 1280 or lEft <= 0 :
xdirection *= -1
word = (word+1)%7
if bOttom >= 720 or tOp <= 0 :
ydirection *= -1
word = (word+1)%7
def setup(screen, etc):
pygame.freetype.init()
pass
def setup(screen, etc):
pygame.freetype.init()
pass
def setup(screen, etc):
global words
pygame.freetype.init()
spanish_words = imp.load_source('spanish',etc.mode_root + '/spanish.py')
words = spanish_words.words
pass
def draw(screen, etc):
global words, word1, word2, count, word_index
color = etc.color_picker()
size = etc.knob1 * 300 + 10
x = etc.knob2 * 400
y = etc.knob3 * 400
font = pygame.freetype.Font(etc.mode_root + "/font.ttf", size)
if etc.audio_trig or etc.midi_note_new :
count += 1
count %= 3
if count == 1 :
word_index = random.randint(0,len(words))
word1 = unicode(words[word_index][1])
if count == 2 :
word2 = unicode(words[word_index][0])
if (count == 0) :
pass
if (count == 1) :
(text, textpos1) = font.render(word1, color)
textpos1 =(x, y)
screen.blit(text, textpos1)
if (count == 2) :
(text, textpos1) = font.render(word1, color)
textpos1 =(x, y)
screen.blit(text, textpos1)
(text, textpos1) = font.render(word2, color)
textpos1 =(x, y + size)
screen.blit(text, textpos1)
def test_issue_242(self):
"""Issue #242: get_rect() uses 0 as default style"""
# Issue #242: freetype.Font.get_rect() ignores style defaults when
# the style argument is not given
#
# The text boundary rectangle returned by freetype.Font.get_rect()
# should match the boundary of the same text rendered directly to a
# surface. This permits accurate text positioning. To work properly,
# get_rect() should calculate the text boundary to reflect text style,
# such as underline. Instead, it ignores the style settings for the
# Font object when the style argument is omitted.
#
# When the style argument is not given, freetype.get_rect() uses
# unstyled text when calculating the boundary rectangle. This is
# because _ftfont_getrect(), in _freetype.c, set the default
# style to 0 rather than FT_STYLE_DEFAULT.
#
font = self._TEST_FONTS['sans']
# Try wide style on a wide character.
prev_style = font.wide
font.wide = True
try:
rect = font.get_rect('M', size=64)
surf, rrect = font.render(None, size=64)
self.assertEqual(rect, rrect)
finally:
font.wide = prev_style
# Try strong style on several wide characters.
prev_style = font.strong
font.strong = True
try:
rect = font.get_rect('Mm_', size=64)
surf, rrect = font.render(None, size=64)
self.assertEqual(rect, rrect)
finally:
font.strong = prev_style
# Try oblique style on a tall, narrow character.
prev_style = font.oblique
font.oblique = True
try:
rect = font.get_rect('|', size=64)
surf, rrect = font.render(None, size=64)
self.assertEqual(rect, rrect)
finally:
font.oblique = prev_style
# Try underline style on a glyphless character.
prev_style = font.underline
font.underline = True
try:
rect = font.get_rect(' ', size=64)
surf, rrect = font.render(None, size=64)
self.assertEqual(rect, rrect)
finally:
font.underline = prev_style