Skip to content

Commit

Permalink
* Initial support for forms. Unfortunately ReportLab's support for forms
Browse files Browse the repository at this point in the history
  is very weak, so we cannot do too much.

* Latest version of RML Reference showing all the new features I have 
  mentioned in the previous checkins.
  • Loading branch information
strichter committed Apr 16, 2007
1 parent 37a3606 commit ae9d70a
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/z3c/rml/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ def process(self):
class ITextAnnotation(interfaces.IRMLDirectiveSignature):
"""Writes a low-level text annotation into the PDF."""
occurence.containing(
occurence.ZeroOrMore('param', IParam))
occurence.ZeroOrMore('', IParam))

contents = attr.FirstLevelTextNode(
title=u'Contents',
Expand Down Expand Up @@ -742,6 +742,9 @@ class IDrawing(interfaces.IRMLDirectiveSignature):
occurence.ZeroOrMore('lineMode', ILineMode),
# Form Field Elements
occurence.ZeroOrMore('barCode', form.IBarCode),
occurence.ZeroOrMore('textField', form.ITextField),
occurence.ZeroOrMore('buttonField', form.IButtonField),
occurence.ZeroOrMore('selectField', form.ISelectField),
# Charts
occurence.ZeroOrMore('barChart', chart.IBarChart),
occurence.ZeroOrMore('barChart3D', chart.IBarChart3D),
Expand Down Expand Up @@ -773,6 +776,9 @@ class Drawing(directive.RMLDirective):
'path': Path,
# Form Field Elements
'barCode': form.BarCode,
'textField': form.TextField,
'buttonField': form.ButtonField,
'selectField': form.SelectField,
# State Change Operations
'fill': Fill,
'stroke': Stroke,
Expand Down
133 changes: 132 additions & 1 deletion src/z3c/rml/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"""
__docformat__ = "reStructuredText"
import types
from z3c.rml import attr, directive, interfaces
import reportlab.pdfbase.pdfform
from z3c.rml import attr, directive, interfaces, occurence

try:
import reportlab.graphics.barcode
Expand Down Expand Up @@ -225,3 +226,133 @@ def process(self):
code = reportlab.graphics.barcode.createBarcodeDrawing(name, **kw)
manager = attr.getManager(self, interfaces.ICanvasManager)
code.drawOn(manager.canvas, x, y)


class IField(interfaces.IRMLDirectiveSignature):
"""A field."""

title = attr.Text(
title=u'Title',
description=u'The title of the field.',
required=True)

x = attr.Measurement(
title=u'X-Position',
description=u'The x-position of the lower-left corner of the field.',
default=0,
required=True)

y = attr.Measurement(
title=u'Y-Position',
description=u'The y-position of the lower-left corner of the field.',
default=0,
required=True)


class Field(directive.RMLDirective):
signature = IField
callable = None
attrMapping = {}

def process(self):
kwargs = dict(self.getAttributeValues(attrMapping=self.attrMapping))
canvas = attr.getManager(self, interfaces.ICanvasManager).canvas
getattr(reportlab.pdfbase.pdfform, self.callable)(canvas, **kwargs)


class ITextField(IField):
"""A text field within the PDF"""

width = attr.Measurement(
title=u'Width',
description=u'The width of the text field.',
required=True)

height = attr.Measurement(
title=u'Height',
description=u'The height of the text field.',
required=True)

value = attr.Text(
title=u'Value',
description=u'The default text value of the field.',
required=False)

maxLength = attr.Integer(
title=u'Maximum Length',
description=u'The maximum amount of characters allowed in the field.',
required=False)

multiline = attr.Boolean(
title=u'Multiline',
description=u'A flag when set allows multiple lines within the field.',
required=False)

class TextField(Field):
signature = ITextField
callable = 'textFieldAbsolute'
attrMapping = {'maxLength': 'maxlen'}


class IButtonField(IField):
"""A button field within the PDF"""

value = attr.Choice(
title=u'Value',
description=u'The value of the button.',
choices=('Yes', 'Off'),
required=True)

class ButtonField(Field):
signature = IButtonField
callable = 'buttonFieldAbsolute'


class IOption(interfaces.IRMLDirectiveSignature):
"""An option in the select field."""

value = attr.TextNode(
title=u'Value',
description=u'The value of the option.',
required=True)

class Option(directive.RMLDirective):
signature = IOption

def process(self):
value = self.getAttributeValues(valuesOnly=True)[0]
self.parent.options.append(value)


class ISelectField(IField):
"""A selection field within the PDF"""
occurence.containing(
occurence.ZeroOrMore('option', IOption))

width = attr.Measurement(
title=u'Width',
description=u'The width of the select field.',
required=True)

height = attr.Measurement(
title=u'Height',
description=u'The height of the select field.',
required=True)

value = attr.Text(
title=u'Value',
description=u'The default value of the field.',
required=False)

class SelectField(Field):
signature = ISelectField
callable = 'selectFieldAbsolute'
factories = {'option': Option}

def process(self):
self.options = []
self.processSubDirectives()
kwargs = dict(self.getAttributeValues(attrMapping=self.attrMapping))
kwargs['options'] = self.options
canvas = attr.getManager(self, interfaces.ICanvasManager).canvas
getattr(reportlab.pdfbase.pdfform, self.callable)(canvas, **kwargs)
Binary file modified src/z3c/rml/rml-reference.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion src/z3c/rml/rml-reference.pt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
name="sub-directive"
fontName="Times-Roman"
fontSize="10"
leftIndent="0.3cm"
leftIndent="0.5cm"
/>
<paraStyle
name="example-info"
Expand Down
21 changes: 21 additions & 0 deletions src/z3c/rml/tests/input/tag-buttonField.rml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE document SYSTEM "rml.dtd">

<document
filename="tag-textField.pdf"
xmlns:doc="http://namespaces.zope.org/rml/doc">
<pageDrawing>

<drawCenteredString x="10.5cm" y="27cm">
"buttonField" Tag Demo
</drawCenteredString>

<drawString x="2cm" y="23cm">
Button 1:
</drawString>
<!-- buttonField
title="button1" value="Off" x="3.5cm" y="22.9cm"
doc:example="" / -->

</pageDrawing>
</document>
37 changes: 37 additions & 0 deletions src/z3c/rml/tests/input/tag-selectField.rml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE document SYSTEM "rml.dtd">

<document
filename="tag-textField.pdf"
xmlns:doc="http://namespaces.zope.org/rml/doc">
<pageDrawing>

<drawCenteredString x="10.5cm" y="27cm">
"selectField" Tag Demo
</drawCenteredString>

<drawString x="2cm" y="23cm">
Select 1:
</drawString>
<selectField
title="select1" value="Option 2"
x="4cm" y="22.9cm" width="5cm" height="15"
doc:example="">
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</selectField>

<drawString x="2cm" y="20cm">
Select 2:
</drawString>
<selectField
title="select2" value="Option 2"
x="4cm" y="18.5cm" width="5cm" height="2cm">
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</selectField>

</pageDrawing>
</document>
31 changes: 31 additions & 0 deletions src/z3c/rml/tests/input/tag-textField.rml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE document SYSTEM "rml.dtd">

<document
filename="tag-textField.pdf"
xmlns:doc="http://namespaces.zope.org/rml/doc">
<pageDrawing>

<drawCenteredString x="10.5cm" y="27cm">
"textField" Tag Demo
</drawCenteredString>

<drawString x="2cm" y="23cm">
Input 1:
</drawString>
<textField
title="input1"
x="3.5cm" y="22.9cm" width="5cm" height="14"
doc:example="" />

<drawString x="2cm" y="21cm">
Input 2:
</drawString>
<textField
title="input2" value="Default Value"
x="3.5cm" y="18.4cm" width="5cm" height="3cm"
multiline="yes" maxLength="30"
doc:example="" />

</pageDrawing>
</document>

0 comments on commit ae9d70a

Please sign in to comment.