



Search — render 0.1 documentation

### Navigation

* [index](genindex.html "General Index")

* [modules](py-modindex.html "Python Module Index") |

* [render 0.1 documentation](index.html) »

Search

======

 Please activate JavaScript to enable the search

 functionality.

 

 From here you can search these documents. Enter your search

 words into the box below and click "search". Note that the search

 function will automatically search for all of the words. Pages

 containing fewer words won't appear in the result list.

 

### Related Topics

* [Documentation overview](index.html)

 © Copyright 2013, Santhosh Thottingal.

 





Index — render 0.1 documentation

### Navigation

* [index](# "General Index")

* [modules](py-modindex.html "Python Module Index") |

* [render 0.1 documentation](index.html) »

Index

=====

[**G**](#G)

 | [**R**](#R)

 | [**W**](#W)

G

-

|  |  |

| --- | --- |

| 

[get\_info() (render.core.Render method)](index.html#render.core.Render.get_info)

 | 

[get\_module\_name() (render.core.Render method)](index.html#render.core.Render.get_module_name)

 |

R

-

|  |  |

| --- | --- |

| 

[Render (class in render.core)](index.html#render.core.Render)

[render.core (module)](index.html#module-render.core)

 | 

[render\_text() (render.core.Render method)](index.html#render.core.Render.render_text)

 |

W

-

|  |

| --- |

| 

[wiki2pdf() (render.core.Render method)](index.html#render.core.Render.wiki2pdf)

 |

### Related Topics

* [Documentation overview](index.html)

### Quick search

 Enter search terms or a module, class or function name.

 

 © Copyright 2013, Santhosh Thottingal.

 





render - a complex script rendering utility based on pangocairo — render 0.1 documentation

### Navigation

* [index](genindex.html "General Index")

* [modules](py-modindex.html "Python Module Index") |

* [render 0.1 documentation](#) »

render - a complex script rendering utility based on pangocairo[¶](#render-a-complex-script-rendering-utility-based-on-pangocairo "Permalink to this headline")

===============================================================================================================================================================

This is an experimental python package for generating renderings of complex

scripts. This is based on pypdflib and pangocairo.

The module currently has two functions

* a wiki to pdf generater

* a complex script renderer

Installation[¶](#installation "Permalink to this headline")

-----------------------------------------------------------

You will need to have pango and cairo installed on your system along with pycairo.

You will probably have them if you have pygtk on your system.

pycairo does not install in virtualenvs,so use you distributions package manger to

install pycairo or use pip to install pycairo system wide.

API reference[¶](#module-render.core "Permalink to this headline")

------------------------------------------------------------------

*class* render.core.Render[[source]](_modules/render/core.html#Render)[¶](#render.core.Render "Permalink to this definition")

The render class. Instantiate to get access to the methods.

get\_info()[[source]](_modules/render/core.html#Render.get_info)[¶](#render.core.Render.get_info "Permalink to this definition")

returns info on the module

get\_module\_name()[[source]](_modules/render/core.html#Render.get_module_name)[¶](#render.core.Render.get_module_name "Permalink to this definition")

returns the module name

render\_text(*text*, *file\_type='png'*, *path=None*, *filename=None*, *width=0*, *height=0*, *color='Black'*, *font='Serif'*, *font\_size=12*)[[source]](_modules/render/core.html#Render.render_text)[¶](#render.core.Render.render_text "Permalink to this definition")

| Parameters: | * **text** (*str.*) – the text to be rendered.

* **file\_type** (*str.*) – required output format. accepts png, svg and pdf

* **filename** (*str.*) – filename for the output

* **path** (*str.*) – the file path for the output. defaults to the current directory

* **width** (*int.*) – width of the output

* **height** (*int.*) – height of the output

* **color** (*str.*) – the background color for the rendering.

* **font** (*str.*) – the font to be used

* **font\_size** (*int*) – font size to be used. defaults to 12

 |

| Returns: | the path to the generated rendering. |

generates a rendering of the supplied text.

wiki2pdf(*url*, *path=None*, *font='Serif'*)[[source]](_modules/render/core.html#Render.wiki2pdf)[¶](#render.core.Render.wiki2pdf "Permalink to this definition")

| Parameters: | * **url** (*str.*) – the url for the wiki page

* **font** (*str.*) – the font to be used for the pdf.

* **path** – output path.Defaults to current dir

 |

| Returns: | the path to the generated pdf. |

Indices and tables[¶](#indices-and-tables "Permalink to this headline")

=======================================================================

* [*Index*](genindex.html)

* [*Module Index*](py-modindex.html)

* [*Search Page*](search.html)

### [Table Of Contents](#)

* [render - a complex script rendering utility based on pangocairo](#)

	+ [Installation](#installation)

	+ [API reference](#module-render.core)

* [Indices and tables](#indices-and-tables)

### Related Topics

* [Documentation overview](#)

### This Page

* [Show Source](_sources/index.txt)

### Quick search

 Enter search terms or a module, class or function name.

 

 © Copyright 2013, Santhosh Thottingal.

 





Python Module Index — render 0.1 documentation

### Navigation

* [index](genindex.html "General Index")

* [modules](# "Python Module Index") |

* [render 0.1 documentation](index.html) »

Python Module Index

===================

[**r**](#cap-r)

|  |  |  |

| --- | --- | --- |

|  |  |  |

|  | **r** |  |

| - | render |  |

|  | [render.core](index.html#module-render.core) |  |

### Related Topics

* [Documentation overview](index.html)

### Quick search

 Enter search terms or a module, class or function name.

 

 © Copyright 2013, Santhosh Thottingal.

 





krTheme Sphinx Style — render 0.1 documentation

### Navigation

* [index](../genindex.html "General Index")

* [modules](../py-modindex.html "Python Module Index") |

* [render 0.1 documentation](../index.html) »

krTheme Sphinx Style[¶](#krtheme-sphinx-style "Permalink to this headline")

===========================================================================

This repository contains sphinx styles Kenneth Reitz uses in most of

his projects. It is a drivative of Mitsuhiko’s themes for Flask and Flask related

projects. To use this style in your Sphinx documentation, follow

this guide:

1. put this folder as \_themes into your docs folder. Alternatively

you can also use git submodules to check out the contents there.

2. add this to your conf.py:

```

sys.path.append(os.path.abspath('\_themes'))

html\_theme\_path = ['\_themes']

html\_theme = 'kr'

```

The following themes exist:

**kr**

the standard flask documentation theme for large projects

**kr\_small**

small one-page theme. Intended to be used by very small addon libraries.

### Related Topics

* [Documentation overview](../index.html)

### This Page

* [Show Source](../_sources/_themes/README.txt)

### Quick search

 Enter search terms or a module, class or function name.

 

 © Copyright 2013, Santhosh Thottingal.

 





Overview: module code — render 0.1 documentation

### Navigation

* [index](../genindex.html "General Index")

* [modules](../py-modindex.html "Python Module Index") |

* [render 0.1 documentation](../index.html) »

All modules for which code is available

=======================================

* [render.core](render/core.html)

### Related Topics

* [Documentation overview](../index.html)

### Quick search

 Enter search terms or a module, class or function name.

 

 © Copyright 2013, Santhosh Thottingal.

 





render.core — render 0.1 documentation

### Navigation

* [index](../../genindex.html "General Index")

* [modules](../../py-modindex.html "Python Module Index") |

* [render 0.1 documentation](../../index.html) »

* [Module code](../index.html) »

Source code for render.core

===========================

```

#!/usr/bin/python

# -\*- coding: utf-8 -\*-

# Copyright 2010 Santhosh Thottingal <santhosh.thottingal@gmail.com>

#

# This program is free software; you can redistribute it and/or modify

# it under the terms of the GNU Affero General Public License as published by

# the Free Software Foundation; either version 3 of the License, or

# (at your option) any later version.

#

# This program is distributed in the hope that it will be useful,

# but WITHOUT ANY WARRANTY; without even the implied warranty of

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

# GNU Affero General Public License for more details.

#

# You should have received a copy of the GNU General Public License

# along with this program; if not, write to the Free Software

# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

import os

import cairo

import hashlib

import pango

import pangocairo

import hyphenation

from wiki2pdf import Wikiparser

from styles import \*

[[docs]](../../index.html#render.core.Render)class Render:

 """

 The render class. Instantiate to get access to the methods.

 """

 def \_\_init\_\_(self):

 self.tmp\_folder = "/static/tmp"

[[docs]](../../index.html#render.core.Render.wiki2pdf) def wiki2pdf(self, url, path=None, font='Serif'):

 """

 :param url: the url for the wiki page

 :type url: str.

 :param font: the font to be used for the pdf.

 :type font: str.

 :param path: output path.Defaults to current dir

 :returns: the path to the generated pdf.

 """

 if path is None:

 path = os.path.abspath(os.path.curdir)

 m = hashlib.md5()

 m.update(url.encode("utf-8"))

 filename = m.hexdigest()[0:5]+".pdf"

 filename = os.path.join(path, filename)

 print(filename)

 parser = Wikiparser(url, filename, font)

 parser.parse()

 #else:

 # print ("File already exists.")

 return (os.path.join(self.tmp\_folder, filename))

[[docs]](../../index.html#render.core.Render.render_text) def render\_text(self, text, file\_type='png',path=None,

 filename=None, width=0,

 height=0, color="Black",

 font='Serif', font\_size=12):

 """

 :param text: the text to be rendered.

 :type text: str.

 :param file\_type: required output format. accepts png, svg and pdf

 :type file\_type: str.

 :param filename: filename for the output

 :type filename: str.

 :param path: the file path for the output. defaults to the current directory

 :type path: str.

 :param width: width of the output

 :type width: int.

 :param height: height of the output

 :type height: int.

 :param color: the background color for the rendering.

 :type color: str.

 :param font: the font to be used

 :type font: str.

 :param font\_size: font size to be used. defaults to 12

 :type font\_size: int

 :returns: the path to the generated rendering.

 generates a rendering of the supplied text.

 """

 surface = None

 print width

 width = int(width)

 height = int(height)

 font\_size = int(font\_size)

 text = text.decode("utf-8")

 m = hashlib.md5()

 m.update(text.encode("utf-8"))

 if filename is None:

 filename = m.hexdigest()[0:5]+"."+file\_type

 if path is None:

 path = os.getcwd()

 outputfile = os.path.join(path, filename)

 if file\_type == 'png':

 surface = cairo.ImageSurface(cairo.FORMAT\_ARGB32,

 int(width), int(height))

 if file\_type == 'svg':

 surface = cairo.SVGSurface(outputfile,

 int(width), int(height))

 if file\_type == 'pdf':

 surface = cairo.PDFSurface(outputfile, int(width), int(height))

 context = cairo.Context(surface)

 try:

 text = hyphenation.getInstance().hyphenate(text, u'\u00AD')

 except:

 print("error while hyphenating. Proceeding without Hyphenation")

 width = int(width)

 left\_margin = 10

 top\_margin = 20

 bottom\_margin = 50

 position\_x = left\_margin

 position\_y = top\_margin

 rgba = get\_color(color)

 context.set\_source\_rgba(float(rgba.red), float(rgba.green),

 float(rgba.blue), float(rgba.alpha))

 pc = pangocairo.CairoContext(context)

 paragraph\_layout = pc.create\_layout()

 paragraph\_font\_description = pango.FontDescription()

 paragraph\_font\_description.set\_family(font)

 paragraph\_font\_description.set\_size((int)(int(font\_size) \*

 pango.SCALE))

 paragraph\_layout.set\_font\_description(paragraph\_font\_description)

 if width > 0:

 paragraph\_layout.set\_width((int)((width-2\*left\_margin) \*

 pango.SCALE))

 paragraph\_layout.set\_justify(True)

 paragraph\_layout.set\_text(text+"\n")

 context.move\_to(position\_x, position\_y)

 pango\_layout\_iter = paragraph\_layout.get\_iter()

 line\_width = 0

 while not pango\_layout\_iter.at\_last\_line():

 first\_line = True

 context.move\_to(position\_x, position\_y)

 while not pango\_layout\_iter.at\_last\_line():

 ink\_rect, logical\_rect = pango\_layout\_iter.get\_line\_extents()

 line = pango\_layout\_iter.get\_line\_readonly()

 has\_next\_line = pango\_layout\_iter.next\_line()

 # Decrease paragraph spacing

 if ink\_rect[2] == 0: # It is para break

 dy = font\_size / 2

 position\_y += dy

 if not first\_line:

 self.context.rel\_move\_to(0, dy)

 else:

 xstart = 1.0 \* logical\_rect[0] / pango.SCALE

 context.rel\_move\_to(xstart, 0)

 if width > 0 and height > 0:

 pc.show\_layout\_line(line)

 line\_height = (int)(logical\_rect[3] / pango.SCALE)

 line\_width = (int)(logical\_rect[2] / pango.SCALE)

 context.rel\_move\_to(-xstart, line\_height)

 position\_y += line\_height

 first\_line = False

 if width == 0 or height == 0:

 if width == 0:

 width = line\_width

 if height == 0:

 height = position\_y

 return self.render\_text(text, file\_type, path,filename, width + 2.5\*left\_margin,

 height, color, font, font\_size)

 if file\_type == 'png':

 surface.write\_to\_png(str(outputfile))

 else:

 context.show\_page()

 return outputfile

[[docs]](../../index.html#render.core.Render.get_module_name) def get\_module\_name(self):

 """

 returns the module name

 """

 return "Script Renderer"

[[docs]](../../index.html#render.core.Render.get_info) def get\_info(self):

 """

 returns info on the module

 """

 return "Provides rendered images for Complex scripts"

def getInstance():

    return Render()

```

### Related Topics

* [Documentation overview](../../index.html)

	+ [Module code](../index.html)

### Quick search

 Enter search terms or a module, class or function name.

 

 © Copyright 2013, Santhosh Thottingal.

 

