5 from colorsys import rgb_to_hls, hls_to_rgb
6 from xonstat.util import strip_colors, qfont_decode, _all_colors
8 # similar to html_colors() from util.py
9 _contrast_threshold = 0.5
11 _dec_colors = [ (0.5,0.5,0.5),
24 def writepng(filename, buf, width, height):
25 width_byte_4 = width * 4
26 # fix color ordering (BGR -> RGB)
27 for byte in xrange(width*height):
29 buf[pos:pos+4] = buf[pos+2] + buf[pos+1] + buf[pos+0] + buf[pos+3]
31 #raw_data = b"".join(b'\x00' + buf[span:span + width_byte_4] for span in xrange((height - 1) * width * 4, -1, - width_byte_4))
32 raw_data = b"".join(b'\x00' + buf[span:span + width_byte_4] for span in range(0, (height-1) * width * 4 + 1, width_byte_4))
33 def png_pack(png_tag, data):
34 chunk_head = png_tag + data
35 return struct.pack("!I", len(data)) + chunk_head + struct.pack("!I", 0xFFFFFFFF & zlib.crc32(chunk_head))
38 png_pack(b'IHDR', struct.pack("!2I5B", width, height, 8, 6, 0, 0, 0)),
39 png_pack(b'IDAT', zlib.compress(raw_data, 9)),
40 png_pack(b'IEND', b'')])
41 f = open(filename, "wb")
50 # skin parameters, can be overriden by init
56 def __init__(self, name, **params):
60 'bg': None, # None - plain; otherwise use given texture
61 'bgcolor': None, # transparent bg when bgcolor==None
62 'overlay': None, # add overlay graphic on top of bg
69 'gametype_fontsize':12,
70 'gametype_pos': (60,31),
71 'gametype_width': 110,
72 'gametype_color': (0.9, 0.9, 0.9),
73 'gametype_text': "[ %s ]",
75 'nostats_fontsize': 12,
76 'nostats_pos': (60,59),
77 'nostats_color': (0.8, 0.2, 0.2),
79 'nostats_text': "no stats yet!",
82 'elo_color': (1.0, 1.0, 0.5),
83 'elo_text': "Elo %.0f",
86 'rank_color': (0.8, 0.8, 1.0),
87 'rank_text': "Rank %d of %d",
88 'wintext_fontsize': 10,
89 'wintext_pos': (508,3),
90 'wintext_color': (0.8, 0.8, 0.8),
91 'wintext_text': "Win Percentage",
94 'winp_colortop': (0.2, 1.0, 1.0),
95 'winp_colorbot': (1.0, 1.0, 0.2),
98 'wins_color': (0.6, 0.8, 0.8),
100 'loss_pos': (508,43),
101 'loss_color': (0.8, 0.8, 0.6),
102 'kdtext_fontsize': 10,
103 'kdtext_pos': (390,3),
105 'kdtext_color': (0.8, 0.8, 0.8),
106 'kdtext_bg': (0.8, 0.8, 0.8, 0.1),
107 'kdtext_text': "Kill Ratio",
110 'kdr_colortop': (0.2, 1.0, 0.2),
111 'kdr_colorbot': (1.0, 0.2, 0.2),
113 'kills_pos': (392,46),
114 'kills_color': (0.6, 0.8, 0.6),
115 'deaths_fontsize': 8,
116 'deaths_pos': (392,56),
117 'deaths_color': (0.8, 0.6, 0.6),
118 'ptime_fontsize': 10,
119 'ptime_pos': (451,60),
120 'ptime_color': (0.1, 0.1, 0.1),
121 'ptime_text': "Playing Time: %s",
124 for k,v in params.items():
125 if self.params.has_key(k):
131 def __getattr__(self, key):
132 if self.params.has_key(key):
133 return self.params[key]
136 def render_image(self, data, output_filename):
137 """Render an image for the given player id."""
144 #games = data.total_stats['games']
145 wins, losses = data.total_stats['wins'], data.total_stats['losses']
146 games = wins + losses
147 kills, deaths = data.total_stats['kills'], data.total_stats['deaths']
148 alivetime = data.total_stats['alivetime']
158 surf = C.ImageSurface(C.FORMAT_ARGB32, self.width, self.height)
159 ctx = C.Context(surf)
160 ctx.set_antialias(C.ANTIALIAS_GRAY)
164 if self.bgcolor != None:
165 # plain fillcolor, full transparency possible with (1,1,1,0)
167 ctx.set_operator(C.OPERATOR_SOURCE)
168 ctx.rectangle(0, 0, self.width, self.height)
169 ctx.set_source_rgba(self.bgcolor[0], self.bgcolor[1], self.bgcolor[2], self.bgcolor[3])
175 bg = C.ImageSurface.create_from_png("img/%s.png" % self.bg)
179 bg_w, bg_h = bg.get_width(), bg.get_height()
181 while bg_xoff < self.width:
183 while bg_yoff < self.height:
184 ctx.set_source_surface(bg, bg_xoff, bg_yoff)
185 #ctx.mask_surface(bg)
190 #print "Error: Can't load background texture: %s" % self.bg
193 # draw overlay graphic
194 if self.overlay != None:
196 overlay = C.ImageSurface.create_from_png("img/%s.png" % self.overlay)
197 ctx.set_source_surface(overlay, 0, 0)
198 #ctx.mask_surface(overlay)
201 #print "Error: Can't load overlay texture: %s" % self.overlay
205 ## draw player's nickname with fancy colors
207 # deocde nick, strip all weird-looking characters
208 qstr = qfont_decode(player.nick).replace('^^', '^').replace(u'\x00', '')
211 # replace weird characters that make problems - TODO
214 qstr = ''.join(chars)
215 stripped_nick = strip_colors(qstr.replace(' ', '_'))
217 # fontsize is reduced if width gets too large
218 ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
220 while shrinknick < 10:
221 ctx.set_font_size(self.nick_fontsize - shrinknick)
222 xoff, yoff, tw, th = ctx.text_extents(stripped_nick)[:4]
223 if tw > self.nick_maxwidth:
228 # determine width of single whitespace for later use
229 xoff, yoff, tw, th = ctx.text_extents("_")[:4]
232 # split nick into colored segments
234 _all_colors = re.compile(r'(\^\d|\^x[\dA-Fa-f]{3})')
235 parts = _all_colors.split(qstr)
236 while len(parts) > 0:
239 if _all_colors.match(txt):
240 tag = txt[1:] # strip leading '^'
247 if not txt or len(txt) == 0:
248 # only colorcode and no real text, skip this
252 if tag.startswith('x'):
253 r = int(tag[1] * 2, 16) / 255.0
254 g = int(tag[2] * 2, 16) / 255.0
255 b = int(tag[3] * 2, 16) / 255.0
256 hue, light, satur = rgb_to_hls(r, g, b)
257 if light < _contrast_threshold:
258 light = _contrast_threshold
259 r, g, b = hls_to_rgb(hue, light, satur)
261 r,g,b = _dec_colors[int(tag[0])]
263 r,g,b = _dec_colors[7]
265 ctx.set_source_rgb(r, g, b)
266 ctx.move_to(self.nick_pos[0] + xoffset, self.nick_pos[1])
269 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
270 tw += (len(txt)-len(txt.strip())) * space_w # account for lost whitespaces
273 ## print elos and ranks
275 # show up to three gametypes the player has participated in
277 for gt in data.total_stats['gametypes'][:self.num_gametypes]:
278 if self.gametype_pos:
279 ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_BOLD)
280 ctx.set_font_size(self.gametype_fontsize)
281 ctx.set_source_rgb(self.gametype_color[0],self.gametype_color[1],self.gametype_color[2])
282 txt = self.gametype_text % gt.upper()
283 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
284 ctx.move_to(self.gametype_pos[0]+xoffset-xoff-tw/2, self.gametype_pos[1]-yoff)
287 if not elos.has_key(gt) or not ranks.has_key(gt):
289 ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_BOLD)
290 ctx.set_font_size(self.nostats_fontsize)
291 ctx.set_source_rgb(self.nostats_color[0],self.nostats_color[1],self.nostats_color[2])
292 txt = self.nostats_text
293 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
294 ctx.move_to(self.nostats_pos[0]+xoffset-xoff-tw/2, self.nostats_pos[1]-yoff)
296 ctx.rotate(math.radians(self.nostats_angle))
301 ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
302 ctx.set_font_size(self.elo_fontsize)
303 ctx.set_source_rgb(self.elo_color[0], self.elo_color[1], self.elo_color[2])
304 txt = self.elo_text % round(elos[gt], 0)
305 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
306 ctx.move_to(self.elo_pos[0]+xoffset-xoff-tw/2, self.elo_pos[1]-yoff)
309 ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
310 ctx.set_font_size(self.rank_fontsize)
311 ctx.set_source_rgb(self.rank_color[0], self.rank_color[1], self.rank_color[2])
312 txt = self.rank_text % ranks[gt]
313 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
314 ctx.move_to(self.rank_pos[0]+xoffset-xoff-tw/2, self.rank_pos[1]-yoff)
317 xoffset += self.gametype_width
320 # print win percentage
323 ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
324 ctx.set_font_size(self.wintext_fontsize)
325 ctx.set_source_rgb(self.wintext_color[0], self.wintext_color[1], self.wintext_color[2])
326 txt = self.wintext_text
327 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
328 ctx.move_to(self.wintext_pos[0]-xoff-tw/2, self.wintext_pos[1]-yoff)
333 ratio = float(wins)/games
334 txt = "%.2f%%" % round(ratio * 100, 2)
339 ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_BOLD)
340 ctx.set_font_size(self.winp_fontsize)
341 r = ratio*self.winp_colortop[0] + (1-ratio)*self.winp_colorbot[0]
342 g = ratio*self.winp_colortop[1] + (1-ratio)*self.winp_colorbot[1]
343 b = ratio*self.winp_colortop[2] + (1-ratio)*self.winp_colorbot[2]
344 ctx.set_source_rgb(r, g, b)
345 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
346 ctx.move_to(self.winp_pos[0]-xoff-tw/2, self.winp_pos[1]-yoff)
350 ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
351 ctx.set_font_size(self.wins_fontsize)
352 ctx.set_source_rgb(self.wins_color[0], self.wins_color[1], self.wins_color[2])
353 txt = "%d win" % wins
356 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
357 ctx.move_to(self.wins_pos[0]-xoff-tw/2, self.wins_pos[1]-yoff)
361 ctx.set_font_size(self.loss_fontsize)
362 ctx.set_source_rgb(self.loss_color[0], self.loss_color[1], self.loss_color[2])
363 txt = "%d loss" % losses
366 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
367 ctx.move_to(self.loss_pos[0]-xoff-tw/2, self.loss_pos[1]-yoff)
371 # print kill/death ratio
374 ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
375 ctx.set_font_size(self.kdtext_fontsize)
376 ctx.set_source_rgb(self.kdtext_color[0], self.kdtext_color[1], self.kdtext_color[2])
377 txt = self.kdtext_text
378 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
379 ctx.move_to(self.kdtext_pos[0]-xoff-tw/2, self.kdtext_pos[1]-yoff)
384 ratio = float(kills)/deaths
385 txt = "%.3f" % round(ratio, 3)
390 ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_BOLD)
391 ctx.set_font_size(self.kdr_fontsize)
395 r = nr*self.kdr_colortop[0] + (1-nr)*self.kdr_colorbot[0]
396 g = nr*self.kdr_colortop[1] + (1-nr)*self.kdr_colorbot[1]
397 b = nr*self.kdr_colortop[2] + (1-nr)*self.kdr_colorbot[2]
398 ctx.set_source_rgb(r, g, b)
399 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
400 ctx.move_to(self.kdr_pos[0]-xoff-tw/2, self.kdr_pos[1]-yoff)
404 ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
405 ctx.set_font_size(self.kills_fontsize)
406 ctx.set_source_rgb(self.kills_color[0], self.kills_color[1], self.kills_color[2])
407 txt = "%d kill" % kills
410 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
411 ctx.move_to(self.kills_pos[0]-xoff-tw/2, self.kills_pos[1]+yoff)
415 ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
416 ctx.set_font_size(self.deaths_fontsize)
417 ctx.set_source_rgb(self.deaths_color[0], self.deaths_color[1], self.deaths_color[2])
418 if deaths is not None:
419 txt = "%d death" % deaths
424 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
425 ctx.move_to(self.deaths_pos[0]-xoff-tw/2, self.deaths_pos[1]+yoff)
432 ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
433 ctx.set_font_size(self.ptime_fontsize)
434 ctx.set_source_rgb(self.ptime_color[0], self.ptime_color[1], self.ptime_color[2])
435 txt = self.ptime_text % str(alivetime)
436 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
437 ctx.move_to(self.ptime_pos[0]-xoff-tw/2, self.ptime_pos[1]-yoff)
442 #surf.write_to_png(output_filename)
444 imgdata = surf.get_data()
445 writepng(output_filename, imgdata, self.width, self.height)