/usr/share/pyshared/z3c/rml/document.py is in python-z3c.rml 2.0.0-0ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 | ##############################################################################
#
# Copyright (c) 2007 Zope Foundation and Contributors.
# 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
$Id: document.py 128841 2012-12-21 05:02:18Z srichter $
"""
__docformat__ = "reStructuredText"
import cStringIO
import logging
import sys
import zope.interface
import reportlab.pdfgen.canvas
from reportlab.pdfbase import pdfmetrics, ttfonts, cidfonts
from reportlab.lib import colors, fonts
from reportlab.platypus import tableofcontents
from z3c.rml import attr, canvas, directive, doclogic, interfaces, list
from z3c.rml import occurence, pdfinclude, special, storyplace, stylesheet
from z3c.rml import template
LOGGER_NAME = 'z3c.rml.render'
class IRegisterType1Face(interfaces.IRMLDirectiveSignature):
"""Register a new Type 1 font face."""
afmFile = attr.String(
title=u'AFM File',
description=u'Path to AFM file used to register the Type 1 face.',
required=True)
pfbFile = attr.String(
title=u'PFB File',
description=u'Path to PFB file used to register the Type 1 face.',
required=True)
class RegisterType1Face(directive.RMLDirective):
signature = IRegisterType1Face
def process(self):
args = self.getAttributeValues(valuesOnly=True)
face = pdfmetrics.EmbeddedType1Face(*args)
pdfmetrics.registerTypeFace(face)
class IRegisterFont(interfaces.IRMLDirectiveSignature):
"""Register a new font based on a face and encoding."""
name = attr.String(
title=u'Name',
description=(u'The name under which the font can be used in style '
u'declarations or other parameters that lookup a font.'),
required=True)
faceName = attr.String(
title=u'Face Name',
description=(u'The name of the face the font uses. The face has to '
u'be previously registered.'),
required=True)
encName = attr.String(
title=u'Encoding Name',
description=(u'The name of the encdoing to be used.'),
required=True)
class RegisterFont(directive.RMLDirective):
signature = IRegisterFont
def process(self):
args = self.getAttributeValues(valuesOnly=True)
font = pdfmetrics.Font(*args)
pdfmetrics.registerFont(font)
class IAddMapping(interfaces.IRMLDirectiveSignature):
"""Map various styles(bold, italic) of a font name to the actual ps fonts
used."""
faceName = attr.String(
title=u'Name',
description=(u'The name of the font to be mapped'),
required=True)
bold = attr.Integer(
title=u'Bold',
description=(u'Bold'),
required=True)
italic = attr.Integer(
title=u'Italic',
description=(u'Italic'),
required=True)
psName = attr.String(
title=u'psName',
description=(u'Actual font name mapped'),
required=True)
class AddMapping(directive.RMLDirective):
signature = IAddMapping
def process(self):
args = self.getAttributeValues(valuesOnly=True)
fonts.addMapping(*args)
class IRegisterTTFont(interfaces.IRMLDirectiveSignature):
"""Register a new TrueType font given the TT file and face name."""
faceName = attr.String(
title=u'Face Name',
description=(u'The name of the face the font uses. The face has to '
u'be previously registered.'),
required=True)
fileName = attr.String(
title=u'File Name',
description=u'File path of the of the TrueType font.',
required=True)
class RegisterTTFont(directive.RMLDirective):
signature = IRegisterTTFont
def process(self):
args = self.getAttributeValues(valuesOnly=True)
font = ttfonts.TTFont(*args)
pdfmetrics.registerFont(font)
class IRegisterCidFont(interfaces.IRMLDirectiveSignature):
"""Register a new CID font given the face name."""
faceName = attr.String(
title=u'Face Name',
description=(u'The name of the face the font uses. The face has to '
u'be previously registered.'),
required=True)
encName = attr.String(
title=u'Encoding Name',
description=(u'The name of the encoding to use for the font.'),
required=False)
class RegisterCidFont(directive.RMLDirective):
signature = IRegisterCidFont
attrMapping = {'faceName': 'face', 'encName': 'encoding'}
def process(self):
args = dict(self.getAttributeValues(attrMapping=self.attrMapping))
if 'encoding' in args:
font = cidfonts.CIDFont(**args)
else:
font = cidfonts.UnicodeCIDFont(**args)
pdfmetrics.registerFont(font)
class IRegisterFontFamily(interfaces.IRMLDirectiveSignature):
"""Register a new font family."""
name = attr.String(
title=u'Name',
description=(u'The name of the font family.'),
required=True)
normal = attr.String(
title=u'Normal Font Name',
description=(u'The name of the normal font variant.'),
required=False)
bold = attr.String(
title=u'Bold Font Name',
description=(u'The name of the bold font variant.'),
required=False)
italic = attr.String(
title=u'Italic Font Name',
description=(u'The name of the italic font variant.'),
required=False)
boldItalic = attr.String(
title=u'Bold/Italic Font Name',
description=(u'The name of the bold/italic font variant.'),
required=True)
class RegisterFontFamily(directive.RMLDirective):
signature = IRegisterFontFamily
def process(self):
args = self.getAttributeValues(valuesOnly=True)
pdfmetrics.registerFontFamily(*args)
class IColorDefinition(interfaces.IRMLDirectiveSignature):
"""Define a new color and give it a name to be known under."""
id = attr.String(
title=u'Id',
description=(u'The id/name the color will be available under.'),
required=True)
RGB = attr.Color(
title=u'RGB Color',
description=(u'The color value that is represented.'),
required=False)
CMYK = attr.Color(
title=u'CMYK Color',
description=(u'The color value that is represented.'),
required=False)
value = attr.Color(
title=u'Color',
description=(u'The color value that is represented.'),
required=False)
spotName = attr.String(
title=u'Spot Name',
description=(u'The Spot Name of the CMYK color.'),
required=False)
density = attr.Float(
title=u'Density',
description=(u'The color density of the CMYK color.'),
min=0.0,
max=1.0,
required=False)
knockout = attr.String(
title=u'Knockout',
description=(u'The knockout of the CMYK color.'),
required=False)
alpha = attr.Float(
title=u'Alpha',
description=(u'The alpha channel of the color.'),
min=0.0,
max=1.0,
required=False)
class ColorDefinition(directive.RMLDirective):
signature = IColorDefinition
def process(self):
kwargs = dict(self.getAttributeValues())
id = kwargs.pop('id')
for attrName in ('RGB', 'CMYK', 'value'):
color = kwargs.pop(attrName, None)
if color is not None:
# CMYK has additional attributes.
for name, value in kwargs.items():
setattr(color, name, value)
manager = attr.getManager(self)
manager.colors[id] = color
return
raise ValueError('At least one color definition must be specified.')
# Initialize also supports the <color> tag.
stylesheet.Initialize.factories['color'] = ColorDefinition
stylesheet.IInitialize.setTaggedValue(
'directives',
stylesheet.IInitialize.getTaggedValue('directives') +
(occurence.ZeroOrMore('color', IColorDefinition),)
)
class IStartIndex(interfaces.IRMLDirectiveSignature):
"""Start a new index."""
name = attr.String(
title=u'Name',
description=u'The name of the index.',
default='index',
required=True)
offset = attr.Integer(
title=u'Offset',
description=u'The counting offset.',
min=0,
required=False)
format = attr.Choice(
title=u'Format',
description=(u'The format the index is going to use.'),
choices=interfaces.LIST_FORMATS,
required=False)
class StartIndex(directive.RMLDirective):
signature = IStartIndex
def process(self):
kwargs = dict(self.getAttributeValues())
name = kwargs['name']
manager = attr.getManager(self)
manager.indexes[name] = tableofcontents.SimpleIndex(**kwargs)
class ICropMarks(interfaces.IRMLDirectiveSignature):
"""Crop Marks specification"""
name = attr.String(
title=u'Name',
description=u'The name of the index.',
default='index',
required=True)
borderWidth = attr.Measurement(
title=u'Border Width',
description=u'The width of the crop mark border.',
required=False)
markColor = attr.Color(
title=u'Mark Color',
description=u'The color of the crop marks.',
required=False)
markWidth = attr.Measurement(
title=u'Mark Width',
description=u'The line width of the actual crop marks.',
required=False)
markLength = attr.Measurement(
title=u'Mark Length',
description=u'The length of the actual crop marks.',
required=False)
markLast = attr.Boolean(
title=u'Mark Last',
description=u'If set, marks are drawn after the content is rendered.',
required=False)
bleedWidth = attr.Measurement(
title=u'Bleed Width',
description=(u'The width of the page bleed.'),
required=False)
class CropMarksProperties(object):
borderWidth = 36
markWidth = 0.5
markColor = colors.toColor('green')
markLength = 18
markLast = True
bleedWidth = 0
class CropMarks(directive.RMLDirective):
signature = ICropMarks
def process(self):
cmp = CropMarksProperties()
for name, value in self.getAttributeValues():
setattr(cmp, name, value)
self.parent.parent.cropMarks = cmp
class ILogConfig(interfaces.IRMLDirectiveSignature):
"""Configure the render logger."""
level = attr.Choice(
title=u'Level',
description=u'The default log level.',
choices=interfaces.LOG_LEVELS,
doLower=False,
required=False)
format = attr.String(
title=u'Format',
description=u'The format of the log messages.',
required=False)
filename = attr.File(
title=u'File Name',
description=u'The path to the file that is being logged.',
doNotOpen=True,
required=True)
filemode = attr.Choice(
title=u'File Mode',
description=u'The mode to open the file in.',
choices={'WRITE': 'w', 'APPEND': 'a'},
default='a',
required=False)
datefmt = attr.String(
title=u'Date Format',
description=u'The format of the log message date.',
required=False)
class LogConfig(directive.RMLDirective):
signature = ILogConfig
def process(self):
args = dict(self.getAttributeValues())
logger = logging.Logger(LOGGER_NAME)
handler = logging.FileHandler(args['filename'], args['filemode'])
formatter = logging.Formatter(
args.get('format'), args.get('datefmt'))
handler.setFormatter(formatter)
logger.addHandler(handler)
if 'level' in args:
logger.setLevel(args['level'])
self.parent.parent.logger = logger
class IDocInit(interfaces.IRMLDirectiveSignature):
occurence.containing(
occurence.ZeroOrMore('color', IColorDefinition),
occurence.ZeroOrMore('name', special.IName),
occurence.ZeroOrMore('registerType1Face', IRegisterType1Face),
occurence.ZeroOrMore('registerFont', IRegisterFont),
occurence.ZeroOrMore('registerCidFont', IRegisterCidFont),
occurence.ZeroOrMore('registerTTFont', IRegisterTTFont),
occurence.ZeroOrMore('registerFontFamily', IRegisterFontFamily),
occurence.ZeroOrMore('addMapping', IAddMapping),
occurence.ZeroOrMore('logConfig', ILogConfig),
occurence.ZeroOrMore('cropMarks', ICropMarks),
occurence.ZeroOrMore('startIndex', IStartIndex),
)
pageMode = attr.Choice(
title=u'Page Mode',
description=(u'The page mode in which the document is opened in '
u'the viewer.'),
choices=('UseNone', 'UseOutlines', 'UseThumbs', 'FullScreen'),
required=False)
pageLayout = attr.Choice(
title=u'Page Layout',
description=(u'The layout in which the pages are displayed in '
u'the viewer.'),
choices=('SinglePage', 'OneColumn', 'TwoColumnLeft', 'TwoColumnRight'),
required=False)
useCropMarks = attr.Boolean(
title=u'Use Crop Marks',
description=u'A flag when set shows crop marks on the page.',
required=False)
class DocInit(directive.RMLDirective):
signature = IDocInit
factories = {
'name': special.Name,
'color': ColorDefinition,
'registerType1Face': RegisterType1Face,
'registerFont': RegisterFont,
'registerTTFont': RegisterTTFont,
'registerCidFont': RegisterCidFont,
'addMapping': AddMapping,
'logConfig': LogConfig,
'cropMarks': CropMarks,
'startIndex': StartIndex,
}
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')
super(DocInit, self).process()
class IDocument(interfaces.IRMLDirectiveSignature):
occurence.containing(
occurence.ZeroOrOne('docinit', IDocInit),
occurence.ZeroOrOne('stylesheet', stylesheet.IStylesheet),
occurence.ZeroOrOne('template', template.ITemplate),
occurence.ZeroOrOne('story', template.IStory),
occurence.ZeroOrOne('pageInfo', canvas.IPageInfo),
occurence.ZeroOrMore('pageDrawing', canvas.IPageDrawing),
)
filename = attr.String(
title=u'File Name',
description=(u'The default name of the output file, if no output '
u'file was provided.'),
required=True)
debug = attr.Boolean(
title=u'Debug',
description=u'A flag to activate the debug output.',
required=False)
compression = attr.BooleanWithDefault(
title=u'Compression',
description=(u'A flag determining whether page compression should '
u'be used.'),
required=False)
invariant = attr.BooleanWithDefault(
title=u'Invariant',
description=(u'A flag that determines whether the produced PDF '
u'should be invariant with respect to the date and '
u'the exact contents.'),
required=False)
class Document(directive.RMLDirective):
signature = IDocument
zope.interface.implements(interfaces.IManager,
interfaces.IPostProcessorManager,
interfaces.ICanvasManager)
factories = {
'docinit': DocInit,
'stylesheet': stylesheet.Stylesheet,
'template': template.Template,
'story': template.Story,
'pageInfo': canvas.PageInfo,
'pageDrawing': canvas.PageDrawing,
}
def __init__(self, element):
super(Document, self).__init__(element, None)
self.names = {}
self.styles = {}
self.colors = {}
self.indexes = {}
self.postProcessors = []
self.filename = '<unknown>'
self.cropMarks = False
self.pageLayout = None
self.pageMode = None
self.logger = None
def _indexAdd(self, canvas, name, label):
self.indexes[name](canvas, name, label)
def _beforeDocument(self):
self._initCanvas(self.doc.canv)
self.canvas = self.doc.canv
def _initCanvas(self, canvas):
canvas._indexAdd = self._indexAdd
canvas.manager = self
if self.pageLayout:
canvas._doc._catalog.setPageLayout(self.pageLayout)
if self.pageMode:
canvas._doc._catalog.setPageMode(self.pageMode)
def process(self, outputFile=None):
"""Process document"""
# Reset all reportlab global variables. This is very important for
# ReportLab not to fail.
reportlab.rl_config._reset()
# Add our colors mapping to the default ones.
colors.toColor.setExtraColorsNameSpace(self.colors)
if outputFile is None:
# TODO: This is relative to the input file *not* the CWD!!!
outputFile = open(self.element.get('filename'), 'wb')
# Create a temporary output file, so that post-processors can
# massage the output
self.outputFile = tempOutput = cStringIO.StringIO()
# Process common sub-directives
self.processSubDirectives(select=('docinit', 'stylesheet'))
# Handle Page Drawing Documents
if self.element.find('pageDrawing') is not None:
kwargs = dict(self.getAttributeValues(
select=('compression', 'debug'),
attrMapping={'compression': 'pageCompression',
'debug': 'verbosity'}
))
kwargs['cropMarks'] = self.cropMarks
self.canvas = reportlab.pdfgen.canvas.Canvas(tempOutput, **kwargs)
self._initCanvas(self.canvas)
self.processSubDirectives(select=('pageInfo', 'pageDrawing'))
self.canvas.save()
# Handle Flowable-based documents.
elif self.element.find('template') is not None:
self.processSubDirectives(select=('template', 'story'))
self.doc.beforeDocument = self._beforeDocument
self.doc.multiBuild(self.flowables)
# Process all post processors
for name, processor in self.postProcessors:
tempOutput.seek(0)
tempOutput = processor.process(tempOutput)
# Save the result into our real output file
tempOutput.seek(0)
outputFile.write(tempOutput.getvalue())
# Cleanup.
colors.toColor.setExtraColorsNameSpace({})
|