• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / z3c.rml / 16098868126

14 Apr 2025 06:50AM UTC coverage: 87.385%. Remained the same
16098868126

push

github

icemac
Back to development: 5.1

561 of 792 branches covered (70.83%)

Branch coverage included in aggregate %.

3990 of 4416 relevant lines covered (90.35%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

52.04
/src/z3c/rml/rlfix.py
1
##############################################################################
2
#
3
# Copyright (c) 2012 Zope Foundation and Contributors.
4
# All Rights Reserved.
5
#
6
# This software is subject to the provisions of the Zope Public License,
7
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11
# FOR A PARTICULAR PURPOSE.
12
#
13
##############################################################################
14
"""ReportLab fixups.
15
"""
16
from reportlab.lib.sequencer import _type2formatter
1✔
17
from reportlab.platypus.flowables import LIIndenter
1✔
18
from reportlab.platypus.flowables import ListFlowable
1✔
19
from reportlab.platypus.flowables import _computeBulletWidth
1✔
20
from reportlab.platypus.flowables import _LIParams
1✔
21
from reportlab.rl_config import register_reset
1✔
22

23
from z3c.rml import num2words
1✔
24

25

26
__docformat__ = "reStructuredText"
1✔
27
import copy
1✔
28

29
from reportlab.graphics import testshapes
1✔
30
from reportlab.lib import fonts
1✔
31
from reportlab.pdfbase import pdfform
1✔
32
from reportlab.pdfbase import pdfmetrics
1✔
33
from reportlab.pdfbase import ttfonts
1✔
34
from reportlab.pdfbase.pdfpattern import PDFPattern
1✔
35

36

37
_ps2tt_map_original = copy.deepcopy(fonts._ps2tt_map)
1✔
38
_tt2ps_map_original = copy.deepcopy(fonts._tt2ps_map)
1✔
39

40

41
def resetPdfForm():
1✔
42
    pdfform.PDFDOCENC = PDFPattern(pdfform.PDFDocEncodingPattern)
1✔
43
    pdfform.ENCODING = PDFPattern(
1✔
44
        pdfform.EncodingPattern, PDFDocEncoding=pdfform.PDFDOCENC)
45
    pdfform.GLOBALFONTSDICTIONARY = pdfform.FormFontsDictionary()
1✔
46
    pdfform.GLOBALRESOURCES = pdfform.FormResources()
1✔
47
    pdfform.ZADB = PDFPattern(pdfform.ZaDbPattern)
1✔
48

49

50
def resetFonts():
1✔
51
    # testshapes._setup registers the Vera fonts every time which is a little
52
    # slow on all platforms. On Windows it lists the entire system font
53
    # directory and registers them all which is very slow.
54
    pdfmetrics.registerFont(ttfonts.TTFont("Vera", "Vera.ttf"))
1✔
55
    pdfmetrics.registerFont(ttfonts.TTFont("VeraBd", "VeraBd.ttf"))
1✔
56
    pdfmetrics.registerFont(ttfonts.TTFont("VeraIt", "VeraIt.ttf"))
1✔
57
    pdfmetrics.registerFont(ttfonts.TTFont("VeraBI", "VeraBI.ttf"))
1✔
58
    for f in (
1✔
59
        'Times-Roman',
60
        'Courier',
61
        'Helvetica',
62
        'Vera',
63
        'VeraBd',
64
        'VeraIt',
65
            'VeraBI'):
66
        if f not in testshapes._FONTS:
1!
67
            testshapes._FONTS.append(f)
×
68
    fonts._ps2tt_map = copy.deepcopy(_ps2tt_map_original)
1✔
69
    fonts._tt2ps_map = copy.deepcopy(_tt2ps_map_original)
1✔
70

71

72
def setSideLabels():
1✔
73
    from reportlab.graphics.charts import piecharts
1✔
74
    piecharts.Pie3d.sideLabels = 0
1✔
75

76

77
setSideLabels()
1✔
78

79

80
register_reset(resetPdfForm)
1✔
81
register_reset(resetFonts)
1✔
82
del register_reset
1✔
83

84
# Support more enumeration formats.
85

86

87
_type2formatter.update({
1✔
88
    'l': lambda v: num2words.num2words(v),
89
    'L': lambda v: num2words.num2words(v).upper(),
90
    'o': lambda v: num2words.num2words(v, ordinal=True),
91
    'O': lambda v: num2words.num2words(v, ordinal=True).upper(),
92
    'r': lambda v: num2words.toOrdinal(v),
93
    'R': lambda v: num2words.toOrdinal(v).upper(),
94
})
95

96

97
# Make sure that the counter gets increased for our new formatters as well.
98

99

100
ListFlowable._numberStyles += ''.join(_type2formatter.keys())
1✔
101

102

103
def ListFlowable_getContent(self):
1✔
104
    bt = self._bulletType
1✔
105
    value = self._start
1✔
106
    if isinstance(value, (list, tuple)):
1!
107
        values = value
×
108
        value = values[0]
×
109
    else:
110
        values = [value]
1✔
111
    autov = values[0]
1✔
112
    # FIX TO ALLOW ALL FORMATTERS!!!
113
    inc = int(bt in _type2formatter.keys())
1✔
114
    if inc:
1✔
115
        try:
1✔
116
            value = int(value)
1✔
117
        except BaseException:
1✔
118
            value = 1
1✔
119

120
    bd = self._bulletDedent
1✔
121
    if bd == 'auto':
1✔
122
        align = self._bulletAlign
1✔
123
        dir = self._bulletDir
1✔
124
        if dir == 'ltr' and align == 'left':
1!
125
            bd = self._leftIndent
1✔
126
        elif align == 'right':
×
127
            bd = self._rightIndent
×
128
        else:
129
            # we need to work out the maximum width of any of the labels
130
            tvalue = value
×
131
            maxW = 0
×
132
            for d, f in self._flowablesIter():
×
133
                if d:
×
134
                    maxW = max(maxW, _computeBulletWidth(self, tvalue))
×
135
                    if inc:
×
136
                        tvalue += inc
×
137
                elif isinstance(f, LIIndenter):
×
138
                    b = f._bullet
×
139
                    if b:
×
140
                        if b.bulletType == bt:
×
141
                            maxW = max(maxW, _computeBulletWidth(b, b.value))
×
142
                            tvalue = int(b.value)
×
143
                    else:
144
                        maxW = max(maxW, _computeBulletWidth(self, tvalue))
×
145
                    if inc:
×
146
                        tvalue += inc
×
147
            if dir == 'ltr':
×
148
                if align == 'right':
×
149
                    bd = self._leftIndent - maxW
×
150
                else:
151
                    bd = self._leftIndent - maxW * 0.5
×
152
            elif align == 'left':
×
153
                bd = self._rightIndent - maxW
×
154
            else:
155
                bd = self._rightIndent - maxW * 0.5
×
156

157
    self._calcBulletDedent = bd
1✔
158

159
    S = []
1✔
160
    aS = S.append
1✔
161
    i = 0
1✔
162
    for d, f in self._flowablesIter():
1✔
163
        if isinstance(f, ListFlowable):
1!
164
            fstart = f._start
×
165
            if isinstance(fstart, (list, tuple)):
×
166
                fstart = fstart[0]
×
167
            if fstart in values:
×
168
                # my kind of ListFlowable
169
                if f._auto:
×
170
                    autov = values.index(autov) + 1
×
171
                    f._start = values[autov:] + values[:autov]
×
172
                    autov = f._start[0]
×
173
                    if inc:
×
174
                        f._bulletType = autov
×
175
                else:
176
                    autov = fstart
×
177
        fparams = {}
1✔
178
        if not i:
1✔
179
            i += 1
1✔
180
            spaceBefore = getattr(self, 'spaceBefore', None)
1✔
181
            if spaceBefore is not None:
1!
182
                fparams['spaceBefore'] = spaceBefore
×
183
        if d:
1!
184
            aS(self._makeLIIndenter(
×
185
                f, bullet=self._makeBullet(value), params=fparams))
186
            if inc:
×
187
                value += inc
×
188
        elif isinstance(f, LIIndenter):
1!
189
            b = f._bullet
×
190
            if b:
×
191
                if b.bulletType != bt:
×
192
                    raise ValueError(
×
193
                        'Included LIIndenter bulletType=%s != OrderedList'
194
                        ' bulletType=%s' % (b.bulletType, bt))
195
                value = int(b.value)
×
196
            else:
197
                f._bullet = self._makeBullet(
×
198
                    value, params=getattr(f, 'params', None))
199
            if fparams:
×
200
                f.__dict__['spaceBefore'] = max(
×
201
                    f.__dict__.get('spaceBefore', 0), spaceBefore)
202
            aS(f)
×
203
            if inc:
×
204
                value += inc
×
205
        elif isinstance(f, _LIParams):
1!
206
            fparams.update(f.params)
1✔
207
            z = self._makeLIIndenter(f.flowable, bullet=None, params=fparams)
1✔
208
            if f.first:
1✔
209
                if f.value is not None:
1✔
210
                    value = f.value
1✔
211
                    if inc:
1✔
212
                        value = int(value)
1✔
213
                z._bullet = self._makeBullet(value, f.params)
1✔
214
                if inc:
1✔
215
                    value += inc
1✔
216
            aS(z)
1✔
217
        else:
218
            aS(self._makeLIIndenter(f, bullet=None, params=fparams))
×
219

220
    spaceAfter = getattr(self, 'spaceAfter', None)
1✔
221
    if spaceAfter is not None:
1!
222
        f = S[-1]
×
223
        f.__dict__['spaceAfter'] = max(
×
224
            f.__dict__.get('spaceAfter', 0), spaceAfter)
225
    return S
1✔
226

227

228
ListFlowable._getContent = ListFlowable_getContent
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc