1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import unittest
from reportlab.lib.units import cm
from reportlab.lib import pagesizes
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import \
Frame, Paragraph, PageTemplate, BaseDocTemplate
from reportlab.platypus import Paragraph as NonHyphParagraph
__author__ = "Dinu Gherman"
USE_HYPHENATION = True
if USE_HYPHENATION:
try:
import wordaxe.rl.styles
from wordaxe.rl.paragraph import Paragraph
from wordaxe.DCWHyphenator import DCWHyphenator
wordaxe.hyphRegistry['DE'] = DCWHyphenator('DE', 5)
except SyntaxError:
print "could not import hyphenation - try to continue WITHOUT hyphenation!"
PAGESIZE = pagesizes.landscape(pagesizes.A4)
class TwoColumnDocTemplate(BaseDocTemplate):
"Define a simple, two column document."
def __init__(self, filename, **kw):
m = 2*cm
cw, ch = (PAGESIZE[0]-2*m)/2., (PAGESIZE[1]-2*m)
ch -= 14*cm
f1 = Frame(m, m+0.5*cm, cw-0.75*cm, ch-1.9*cm, id='F1',
leftPadding=0, topPadding=0, rightPadding=0, bottomPadding=0,
showBoundary=True
)
f2 = Frame(cw+2.7*cm, m+0.5*cm, cw-0.75*cm, ch-1*cm, id='F2',
leftPadding=0, topPadding=0, rightPadding=0, bottomPadding=0,
showBoundary=True
)
apply(BaseDocTemplate.__init__, (self, filename), kw)
template = PageTemplate('template', [f1, f2])
self.addPageTemplates(template)
class FrameSwitchTestCase(unittest.TestCase):
"Test hyphenation with switching frames and font-tags in paragraphs."
def test(self):
stylesheet = getSampleStyleSheet()
normal = stylesheet['BodyText']
normal.fontName = "Helvetica"
normal.fontSize = 12
normal.leading = 16
normal.language = 'DE'
normal.hyphenation = True
text = "schiffmeister auch nur ein Dampfschiffmeister. Bedauerlicherweise ist ein Donaudampfschiffmeister auch nur ein Dampfschiffmeister."
# strange behaviour when next line uncommented
text = " ".join(['<font color="red">%s</font>' % w for w in text.split()])
text += " "
story = []
story.append(Paragraph(text, style=normal))
doc = TwoColumnDocTemplate("test_frames3g.pdf", pagesize=PAGESIZE)
doc.build(story)
if __name__ == "__main__":
unittest.main()
|