<s> from __future__ import absolute_import , division , print_function , unicode_literals <EOL> import ctypes <EOL> import numpy as np <EOL> import itertools <EOL> import os . path <EOL> import sys <EOL> if sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:3> : <EOL> unichr = chr <EOL> try : <EOL> from PIL import Image , ImageDraw , ImageFont <EOL> except : <EOL> print ( '<STR_LIT>' ) <EOL> from pi3d . constants import * <EOL> from pi3d . Texture import * <EOL> from pi3d . shape . Sprite import Sprite <EOL> class FixedString ( Texture ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , font , string , camera = None , color = ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) , <EOL> font_size = <NUM_LIT> , margin = <NUM_LIT> , justify = '<STR_LIT:C>' , <EOL> background_color = None , shader = None , f_type = '<STR_LIT>' , mipmap = True ) : <EOL> """<STR_LIT>""" <EOL> super ( FixedString , self ) . __init__ ( font , mipmap = mipmap ) <EOL> self . font = font <EOL> try : <EOL> imgfont = ImageFont . truetype ( font , font_size ) <EOL> except IOError : <EOL> abspath = os . path . abspath ( font ) <EOL> msg = "<STR_LIT>" % font <EOL> if font != abspath : <EOL> msg = "<STR_LIT>" % ( msg , abspath ) <EOL> raise Exception ( msg ) <EOL> justify = justify . upper ( ) <EOL> f_type = f_type . upper ( ) <EOL> ascent , descent = imgfont . getmetrics ( ) <EOL> height = ascent + descent <EOL> lines = string . split ( '<STR_LIT:\n>' ) <EOL> nlines = len ( lines ) <EOL> maxwid = <NUM_LIT:0> <EOL> for l in lines : <EOL> line_wid = imgfont . getsize ( l ) [ <NUM_LIT:0> ] <EOL> if line_wid > maxwid : <EOL> maxwid = line_wid <EOL> maxwid += <NUM_LIT> * margin <EOL> texture_wid = WIDTHS [ <NUM_LIT:0> ] <EOL> for l in WIDTHS : <EOL> if l > maxwid : <EOL> texture_wid = l <EOL> break <EOL> texture_wid = l <EOL> texture_hgt = int ( nlines * height + <NUM_LIT:2> * margin ) <EOL> self . im = Image . new ( "<STR_LIT>" , ( texture_wid , texture_hgt ) , background_color ) <EOL> self . ix , self . iy = texture_wid , texture_hgt <EOL> draw = ImageDraw . Draw ( self . im ) <EOL> for i , line in enumerate ( lines ) : <EOL> line_len = imgfont . getsize ( line ) [ <NUM_LIT:0> ] <EOL> if justify == "<STR_LIT:C>" : <EOL> xoff = ( maxwid - line_len ) / <NUM_LIT> <EOL> elif justify == "<STR_LIT:L>" : <EOL> xoff = margin <EOL> else : <EOL> xoff = maxwid - line_len <EOL> draw . text ( ( xoff , margin + i * height ) , line , font = imgfont , fill = color ) <EOL> if f_type == '<STR_LIT>' : <EOL> pass <EOL> elif f_type == '<STR_LIT>' : <EOL> self . im = self . make_bump_map ( ) <EOL> else : <EOL> from PIL import ImageFilter <EOL> if f_type == '<STR_LIT>' : <EOL> self . im = self . im . filter ( ImageFilter . EMBOSS ) <EOL> elif f_type == '<STR_LIT>' : <EOL> self . im = self . im . filter ( ImageFilter . CONTOUR ) <EOL> elif f_type == '<STR_LIT>' : <EOL> self . im = self . im . filter ( ImageFilter . BLUR ) <EOL> elif f_type == '<STR_LIT>' : <EOL> self . im = self . im . filter ( ImageFilter . SMOOTH_MORE ) <EOL> self . im = self . im . convert ( '<STR_LIT>' ) <EOL> self . image = np . array ( self . im ) <EOL> self . _tex = ctypes . c_int ( ) <EOL> bmedge = nlines * height + <NUM_LIT> * margin <EOL> self . sprite = Sprite ( camera = camera , w = maxwid , h = bmedge ) <EOL> buf = self . sprite . buf [ <NUM_LIT:0> ] <EOL> buf . textures = [ self ] <EOL> if shader != None : <EOL> self . sprite . shader = shader <EOL> buf . shader = shader <EOL> buf . unib [ <NUM_LIT:6> ] = float ( maxwid / texture_wid ) <EOL> buf . unib [ <NUM_LIT:7> ] = float ( bmedge / texture_hgt ) <EOL> def set_shader ( self , shader ) : <EOL> '''<STR_LIT>''' <EOL> self . sprite . set_shader ( shader ) <EOL> def draw ( self , shader = None , txtrs = None , ntl = None , shny = None , camera = None , mlist = [ ] ) : <EOL> '''<STR_LIT>''' <EOL> self . sprite . draw ( shader , txtrs , ntl , shny , camera , mlist ) <EOL> def make_bump_map ( self ) : <EOL> """<STR_LIT>""" <EOL> import numpy as np <EOL> a = np . array ( self . im , dtype = np . uint8 ) <EOL> a = np . average ( a , axis = <NUM_LIT:2> , weights = [ <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:0.0> ] ) . astype ( int ) <EOL> b = [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] <EOL> c = np . zeros ( a . shape , dtype = np . uint8 ) <EOL> steps = [ i - <NUM_LIT:2> for i in range ( <NUM_LIT:5> ) ] <EOL> for i , istep in enumerate ( steps ) : <EOL> for j , jstep in enumerate ( steps ) : <EOL> c += ( np . roll ( np . roll ( a , istep , <NUM_LIT:0> ) , jstep , <NUM_LIT:1> ) * b [ i ] [ j ] ) . astype ( np . uint8 ) <EOL> cx = np . roll ( c , <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> cy = np . roll ( c , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> d = np . zeros ( ( a . shape [ <NUM_LIT:0> ] , a . shape [ <NUM_LIT:1> ] , <NUM_LIT:4> ) , dtype = np . uint8 ) <EOL> d [ : , : , <NUM_LIT:0> ] = ( ( c - cy ) + <NUM_LIT> ) . astype ( int ) <EOL> d [ : , : , <NUM_LIT:1> ] = ( ( c - cx ) + <NUM_LIT> ) . astype ( int ) <EOL> d [ : , : , <NUM_LIT:2> ] = ( np . clip ( ( <NUM_LIT> - ( <NUM_LIT> - d [ : , : , <NUM_LIT:0> ] ) ** <NUM_LIT:2> - ( <NUM_LIT> - d [ : , : , <NUM_LIT:1> ] ) ** <NUM_LIT:2> ) ** <NUM_LIT:0.5> , <NUM_LIT:0> , <NUM_LIT:255> ) ) . astype ( int ) <EOL> d [ : , : , <NUM_LIT:3> ] = <NUM_LIT:255> <EOL> return Image . fromarray ( d ) <EOL> def _load_disk ( self ) : <EOL> """<STR_LIT>""" </s>
