-
Notifications
You must be signed in to change notification settings - Fork 0
/
document.py
142 lines (118 loc) · 4.96 KB
/
document.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
##############################################################################
#
# Copyright (c) 2017 Shoobx, Inc.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""RML ``document`` element
"""
import odf
from odf.svg import FontFaceUri
from odf.svg import DefinitionSrc
from reportlab.lib import styles
import reportlab
import zope.interface
from odf.opendocument import OpenDocumentText
from odf.style import FontFace
from odf.svg import FontFaceSrc
from reportlab.pdfbase import pdfmetrics, ttfonts, cidfonts
from z3c.rml import directive, canvas
from z3c.rml import document as rml_document, interfaces as rml_interfaces
# Import modules, so their directives get registered.
from shoobx.rml2odt import list, stylesheet, table, template
RMLSTYLE_HANDLERS = {
styles.ParagraphStyle: stylesheet.registerParagraphStyle
}
class ColorDefinition(directive.RMLDirective):
signature = rml_document.IColorDefinition
class RegisterTTFont(directive.RMLDirective):
signature = rml_document.IRegisterTTFont
def process(self):
fontName, location = self.getAttributeValues(valuesOnly=True)
font = FontFace(name=fontName, fontfamily=fontName,
fontfamilygeneric='modern', fontpitch='variable')
source = FontFaceSrc()
urn = FontFaceUri(href=location, actuate='onRequest', type='simple')
source.appendChild(urn)
defSource = DefinitionSrc(href=location, actuate='onRequest')
font.appendChild(source)
# font.appendChild(defSource)
self.parent.parent.document.fontfacedecls.addElement(font)
class DocInit(directive.RMLDirective):
signature = rml_document.IDocInit
factories = {
# 'name': special.Name,
'color': ColorDefinition,
# 'registerType1Face': RegisterType1Face,
# 'registerFont': RegisterFont,
'registerTTFont': RegisterTTFont,
# 'registerCidFont': RegisterCidFont,
# 'registerFontFamily': RegisterFontFamily,
# 'addMapping': AddMapping,
# 'logConfig': LogConfig,
# 'cropMarks': CropMarks,
# 'startIndex': StartIndex,
}
viewerOptions = {option[0].lower()+option[1:]: option for option in
['HideToolbar', 'HideMenubar', 'HideWindowUI',
'FitWindow', 'CenterWindow', 'DisplayDocTitle',
'NonFullScreenPageMode', 'Direction', 'ViewArea',
'ViewClip', 'PrintArea', 'PrintClip', 'PrintScaling']}
def process(self):
kwargs = dict(self.getAttributeValues())
self.parent.cropMarks = kwargs.get('useCropMarks', False)
self.parent.pageMode = kwargs.get('pageMode')
self.parent.pageLayout = kwargs.get('pageLayout')
for name in self.viewerOptions:
setattr(self.parent, name, kwargs.get(name))
super(DocInit, self).process()
@zope.interface.implementer(rml_interfaces.IManager)
class Document(directive.RMLDirective):
signature = rml_document.IDocument
factories = {
'docinit': DocInit,
'story': template.Story,
'template': template.Template,
# 'pageGraphics': template.PageGraphics,
'stylesheet': stylesheet.Stylesheet,
# 'frame': template.Frame
}
def __init__(self, element):
super(Document, self).__init__(element, None)
self.names = {}
self.styles = {}
self.styleCounters = {}
self.colors = {}
self.attributesCache = {}
self.filename = '<unknown>'
self.attributesCache = {}
def getNextStyleName(self, prefix):
self.styleCounters.setdefault(prefix, 0)
self.styleCounters[prefix] += 1
return prefix + str(self.styleCounters[prefix])
def registerDefaultStyles(self):
for name, style in stylesheet.SampleStyleSheet.byName.items():
handler = RMLSTYLE_HANDLERS.get(style.__class__)
if handler is None:
continue
handler(self.document, name, style)
def process(self, outputFile=None, maxPasses=2):
"""Process document"""
# Initialize the ODT Document.
self.document = OpenDocumentText()
self.registerDefaultStyles()
# Process common sub-directives
self.processSubDirectives(select=('docinit',))
# Handle Flowable-based documents.
if self.element.find('template') is not None:
self.processSubDirectives(select=('stylesheet', 'template'))
self.processSubDirectives(select=('story',))
# Save the output.
self.document.save(outputFile)