commit dd89e74d8790bb19da89fba68cabfedfd3b73347 Author: Samuele Santi Date: Sun Dec 1 14:20:35 2013 +0100 First commit of the library diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e3d48c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*~ +*.pyc +/.ropeproject +.\#* +/output/* diff --git a/apyx/__init__.py b/apyx/__init__.py new file mode 100644 index 0000000..886af59 --- /dev/null +++ b/apyx/__init__.py @@ -0,0 +1,90 @@ +""" +Antipixels generator script +""" + +from __future__ import print_function + +import copy + +from PIL import Image, ImageFont, ImageDraw + + +def create_antipixel(cfg): + font = ImageFont.truetype(cfg['base']['font_path'], size=8) + width, height = cfg['base']['size'] + avail_space = width - 4 # padding: 2px on each side + image = Image.new('RGB', cfg['base']['size']) + draw = ImageDraw.Draw(image) + parts = copy.deepcopy(cfg['parts']) # so we can safely alter + + ##-------------------------------------------------- + ## Prepare sizing + + even_parts = [] + for part in parts: + part['text_width'] = draw.textsize(part['text'], font=font)[0] + part_sizing = part.get('sizing', 'even') + if part_sizing == 'even': + ## Keep track as we'll need later.. + even_parts.append(part) + elif part_sizing == 'fixed': + avail_space -= part['width'] + else: + raise ValueError("Invalid sizing: " + part_sizing) + avail_space -= len(parts) # spacing + all_text_space = sum(x['text_width'] for x in even_parts) + if all_text_space > avail_space: + raise ValueError("Not enough space to fit text!") + for part in even_parts: + # Give space proportional + part['sizing'] = 'fixed' + part['width'] = int(round(1.0 * part['text_width'] * + avail_space / all_text_space)) + + ##-------------------------------------------------- + ## Draw background + + draw.rectangle( + [(0, 0), (image.size[0] - 1, image.size[1] - 1)], + outline=cfg['base']['outline'], + fill=cfg['base']['background']) + + ##-------------------------------------------------- + ## Draw rectangles, with text + + # Vertical bounds + vmin, vmax = 2, image.size[1] - 3 + # Horizontal offset + offset = 2 + for part in parts: + draw.rectangle( + [(offset, vmin), (offset + part['width'], vmax)], + fill=part['background']) + + text_align = part.get('align', 'center') + if text_align == 'left': + text_offset = 1 + elif text_align == 'right': + text_offset = part['width'] - part['text_width'] + elif text_align == 'center': + text_offset = round((part['width'] - part['text_width']) / 2.0) + else: + raise ValueError("Invalid alignment: " + text_align) + + draw.text((offset + text_offset, vmin + 1), + part['text'], fill=part['color'], font=font) + + offset += part['width'] + offset += 1 # space between rectangles + + ##-------------------------------------------------- + ## Draw outline to make sure we clear overflows.. + + draw.rectangle( + [(0, 0), (image.size[0] - 1, image.size[1] - 1)], + outline=cfg['base']['outline']) + draw.rectangle( + [(1, 1), (image.size[0] - 2, image.size[1] - 2)], + outline=cfg['base']['background']) + + return image diff --git a/apyx/templates.py b/apyx/templates.py new file mode 100644 index 0000000..46438a8 --- /dev/null +++ b/apyx/templates.py @@ -0,0 +1,34 @@ +""" +Some templates for common antipixel patterns +""" + + +def twoparts_even(text1, fg1, bg1, text2, fg2, bg2, a1='center', a2='center'): + return { + 'parts': [ + dict(text=text1, background=bg1, color=fg1, sizing='even', + align=a1), + dict(text=text2, background=bg2, color=fg2, sizing='even', + align=a2), + ], + } + + +def twoparts_even_centered(text1, fg1, bg1, text2, fg2, bg2): + return twoparts_even(text1, fg1, bg1, text2, fg2, bg2, 'right', 'left') + + +def twoparts_even_altcolor(text1, text2, col1, col2): + return twoparts_even(text1, col2, col1, text2, col1, col2) + + +def twoparts_even_centered_altcolor(text1, text2, col1, col2): + return twoparts_even_centered(text1, col2, col1, text2, col1, col2) + + +def onepart(text, fg, bg): + return { + 'parts': [ + dict(text=text, background=bg, color=fg, sizing='even'), + ], + } diff --git a/fonts/slkscr.ttf b/fonts/slkscr.ttf new file mode 100644 index 0000000..e2dd974 Binary files /dev/null and b/fonts/slkscr.ttf differ diff --git a/fonts/slkscrb.ttf b/fonts/slkscrb.ttf new file mode 100644 index 0000000..ae4425d Binary files /dev/null and b/fonts/slkscrb.ttf differ diff --git a/fonts/slkscre.ttf b/fonts/slkscre.ttf new file mode 100644 index 0000000..1e2be59 Binary files /dev/null and b/fonts/slkscre.ttf differ diff --git a/fonts/slkscreb.ttf b/fonts/slkscreb.ttf new file mode 100644 index 0000000..cd68f57 Binary files /dev/null and b/fonts/slkscreb.ttf differ diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..453f301 --- /dev/null +++ b/setup.py @@ -0,0 +1,26 @@ +from setuptools import setup, find_packages + + +version = '0.1' +install_requires = [ + 'pillow', +] + +setup( + name='APyX', + version=version, + packages=find_packages(), + url='http://rshk.github.io/apyx', + license='Apache License 2.0', + author='Samuele Santi', + author_email='samuele@samuelesanti.com', + description='Simple antipixel generation utility', + long_description='Simple antipixel generation utility', + install_requires=install_requires, + classifiers=[ + "License :: OSI Approved :: Apache Software License", + "Development Status :: 3 - Alpha", + "Programming Language :: Python :: 2", + "Programming Language :: Python :: 2.7", + ], + package_data={'': ['README.md', 'LICENSE']})