Commit e8dc0977 authored by Michal Čihař's avatar Michal Čihař

Switch charts rendering to Pillow

Pillow (or PIL) is much lighter dependency than Cairo/Pango. Also it
turns out to be easier to use.
parent 4d667910
...@@ -345,6 +345,13 @@ SITE_TITLE ...@@ -345,6 +345,13 @@ SITE_TITLE
Site title to be used in website and emails as well. Site title to be used in website and emails as well.
.. setting:: TTF_FONT
TTF_FONT
--------
TrueType font to be used for widgets and charts.
.. setting:: WHOOSH_INDEX .. setting:: WHOOSH_INDEX
WHOOSH_INDEX WHOOSH_INDEX
......
...@@ -22,6 +22,8 @@ Whoosh ...@@ -22,6 +22,8 @@ Whoosh
http://bitbucket.org/mchaput/whoosh/ http://bitbucket.org/mchaput/whoosh/
PyCairo PyCairo
http://cairographics.org/pycairo/ http://cairographics.org/pycairo/
PIL or Pillow library
http://python-imaging.github.io/
PyGtk PyGtk
http://www.pygtk.org/ http://www.pygtk.org/
south south
......
...@@ -5,3 +5,4 @@ django-registration==0.8 ...@@ -5,3 +5,4 @@ django-registration==0.8
Whoosh>=2.3 Whoosh>=2.3
Translate-Toolkit>=1.10.0 Translate-Toolkit>=1.10.0
lxml>=3.1.0 lxml>=3.1.0
Pillow
...@@ -19,6 +19,8 @@ ...@@ -19,6 +19,8 @@
# #
from distutils.version import LooseVersion from distutils.version import LooseVersion
import importlib
def get_version_module(module, name, url, optional=False): def get_version_module(module, name, url, optional=False):
...@@ -27,7 +29,7 @@ def get_version_module(module, name, url, optional=False): ...@@ -27,7 +29,7 @@ def get_version_module(module, name, url, optional=False):
exception with name and URL. exception with name and URL.
''' '''
try: try:
mod = __import__(module) mod = importlib.import_module(module)
except ImportError: except ImportError:
if optional: if optional:
return None return None
...@@ -173,6 +175,16 @@ def get_versions(): ...@@ -173,6 +175,16 @@ def get_versions():
'0.7', '0.7',
)) ))
name = 'Pillow (PIL)'
url = 'http://python-imaging.github.io/'
mod = get_version_module('PIL.Image', name, url)
result.append((
name,
url,
mod.VERSION,
'1.1.6',
))
return result return result
......
...@@ -24,16 +24,14 @@ Charting library for Weblate. ...@@ -24,16 +24,14 @@ Charting library for Weblate.
from trans.models import Change from trans.models import Change
from lang.models import Language from lang.models import Language
from trans.views.helper import get_project_translation from trans.views.helper import get_project_translation
from weblate import appsettings
from django.shortcuts import render_to_response, get_object_or_404 from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext from django.template import RequestContext
from django.http import HttpResponse from django.http import HttpResponse
from django.contrib.auth.models import User from django.contrib.auth.models import User
from cStringIO import StringIO from cStringIO import StringIO
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
import cairo from PIL import Image, ImageDraw, ImageFont
import pango
import pangocairo
import math
def render_activity(activity): def render_activity(activity):
...@@ -45,48 +43,24 @@ def render_activity(activity): ...@@ -45,48 +43,24 @@ def render_activity(activity):
step = 780.0 / len(activity) step = 780.0 / len(activity)
width = step / 2 width = step / 2
# Prepare cairo surface and context # Prepare image
surface = cairo.ImageSurface(cairo.FORMAT_RGB24, 800, 100) image = Image.new('RGB', (800, 100), 'white')
ctx = cairo.Context(surface) draw = ImageDraw.Draw(image)
# Render background
ctx.set_source_rgb(1, 1, 1)
ctx.rectangle(0, 0, 800, 100)
ctx.fill()
# Render axises # Render axises
ctx.new_path() draw.line(((15, 5), (15, 85), (795, 85)), fill='black')
ctx.set_line_width(1)
ctx.set_source_rgb(0, 0, 0)
ctx.move_to(15, 5)
ctx.line_to(15, 85)
ctx.line_to(795, 85)
ctx.stroke()
# Context for text rendering
pangocairo_context = pangocairo.CairoContext(ctx)
pangocairo_context.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
font = pango.FontDescription('Sans 8')
# Rotate context for vertical text
ctx.rotate(-math.pi / 2)
# Create Y axis label
layout = pangocairo_context.create_layout()
layout.set_width(80)
layout.set_alignment(pango.ALIGN_RIGHT)
layout.set_font_description(font)
layout.set_text(str(maximum))
# Render Y axis label # Load font
ctx.move_to(-5, 0) font = ImageFont.truetype(appsettings.TTF_FONT, 11)
ctx.set_source_rgb(0, 0, 0)
pangocairo_context.update_layout(layout)
pangocairo_context.show_layout(layout)
# Rotate context back # Create Y axis label
ctx.rotate(math.pi / 2) y_label = str(maximum)
text = Image.new('L', font.getsize(y_label), 'white')
draw_txt = ImageDraw.Draw(text)
draw_txt.text((0, 0), y_label, font=font, fill='black')
text = text.transpose(Image.ROTATE_90)
image.paste(text, (2, 5))
# Counter for rendering ticks # Counter for rendering ticks
last = -40 last = -40
...@@ -96,35 +70,31 @@ def render_activity(activity): ...@@ -96,35 +70,31 @@ def render_activity(activity):
current = offset * step current = offset * step
# Render bar # Render bar
ctx.new_path() draw.rectangle(
ctx.set_source_rgb(0, 67.0 / 255, 118.0 / 255) (
ctx.rectangle( 20 + current,
20 + current, 84,
84, 20 + current + width,
width, 84 - value[1] * 78.0 / maximum
- 1.0 - value[1] * 78.0 / maximum ),
fill=(0, 67, 118)
) )
ctx.fill()
# Skip axis labels if they are too frequent # Skip axis labels if they are too frequent
if current < last + 40: if current < last + 40:
continue continue
last = current last = current
# Create text # X-Axis ticks
layout = pangocairo_context.create_layout() draw.text(
layout.set_font_description(font) (15 + current, 86),
layout.set_text(value[0].strftime('%m/%d')) value[0].strftime('%m/%d'),
font=font, fill='black'
# Render text )
ctx.move_to(15 + current, 86)
ctx.set_source_rgb(0, 0, 0)
pangocairo_context.update_layout(layout)
pangocairo_context.show_layout(layout)
# Render surface to PNG # Render surface to PNG
out = StringIO() out = StringIO()
surface.write_to_png(out) image.save(out, 'PNG')
# Return response # Return response
return HttpResponse(content_type='image/png', content=out.getvalue()) return HttpResponse(content_type='image/png', content=out.getvalue())
......
...@@ -139,3 +139,6 @@ PRE_COMMIT_SCRIPTS = get('PRE_COMMIT_SCRIPTS', ()) ...@@ -139,3 +139,6 @@ PRE_COMMIT_SCRIPTS = get('PRE_COMMIT_SCRIPTS', ())
SCRIPT_CHOICES = [ SCRIPT_CHOICES = [
(script, get_script_name(script)) for script in PRE_COMMIT_SCRIPTS (script, get_script_name(script)) for script in PRE_COMMIT_SCRIPTS
] + [('', '')] ] + [('', '')]
# Font for charts and widgets
TTF_FONT = get('TTF_FONT', os.path.join(WEB_ROOT, 'ttf', 'DejaVuSans.ttf'))
Fonts are (c) Bitstream (see below). DejaVu changes are in public domain.
Glyphs imported from Arev fonts are (c) Tavmjong Bah (see below)
Bitstream Vera Fonts Copyright
------------------------------
Copyright (c) 2003 by Bitstream, Inc. All Rights Reserved. Bitstream Vera is
a trademark of Bitstream, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of the fonts accompanying this license ("Fonts") and associated
documentation files (the "Font Software"), to reproduce and distribute the
Font Software, including without limitation the rights to use, copy, merge,
publish, distribute, and/or sell copies of the Font Software, and to permit
persons to whom the Font Software is furnished to do so, subject to the
following conditions:
The above copyright and trademark notices and this permission notice shall
be included in all copies of one or more of the Font Software typefaces.
The Font Software may be modified, altered, or added to, and in particular
the designs of glyphs or characters in the Fonts may be modified and
additional glyphs or characters may be added to the Fonts, only if the fonts
are renamed to names not containing either the words "Bitstream" or the word
"Vera".
This License becomes null and void to the extent applicable to Fonts or Font
Software that has been modified and is distributed under the "Bitstream
Vera" names.
The Font Software may be sold as part of a larger software package but no
copy of one or more of the Font Software typefaces may be sold by itself.
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT,
TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL BITSTREAM OR THE GNOME
FOUNDATION BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING
ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE
FONT SOFTWARE.
Except as contained in this notice, the names of Gnome, the Gnome
Foundation, and Bitstream Inc., shall not be used in advertising or
otherwise to promote the sale, use or other dealings in this Font Software
without prior written authorization from the Gnome Foundation or Bitstream
Inc., respectively. For further information, contact: fonts at gnome dot
org.
Arev Fonts Copyright
------------------------------
Copyright (c) 2006 by Tavmjong Bah. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining
a copy of the fonts accompanying this license ("Fonts") and
associated documentation files (the "Font Software"), to reproduce
and distribute the modifications to the Bitstream Vera Font Software,
including without limitation the rights to use, copy, merge, publish,
distribute, and/or sell copies of the Font Software, and to permit
persons to whom the Font Software is furnished to do so, subject to
the following conditions:
The above copyright and trademark notices and this permission notice
shall be included in all copies of one or more of the Font Software
typefaces.
The Font Software may be modified, altered, or added to, and in
particular the designs of glyphs or characters in the Fonts may be
modified and additional glyphs or characters may be added to the
Fonts, only if the fonts are renamed to names not containing either
the words "Tavmjong Bah" or the word "Arev".
This License becomes null and void to the extent applicable to Fonts
or Font Software that has been modified and is distributed under the
"Tavmjong Bah Arev" names.
The Font Software may be sold as part of a larger software package but
no copy of one or more of the Font Software typefaces may be sold by
itself.
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL
TAVMJONG BAH BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.
Except as contained in this notice, the name of Tavmjong Bah shall not
be used in advertising or otherwise to promote the sale, use or other
dealings in this Font Software without prior written authorization
from Tavmjong Bah. For further information, contact: tavmjong @ free
. fr.
$Id: LICENSE 2133 2007-11-28 02:46:28Z lechimp $
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment