-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathpageNumbering.py
More file actions
227 lines (201 loc) · 9.36 KB
/
pageNumbering.py
File metadata and controls
227 lines (201 loc) · 9.36 KB
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
import logging
from enum import Enum
import reportlab.lib.colors
import reportlab.lib.enums
import reportlab.lib.styles
from reportlab.lib.styles import ParagraphStyle
from ceweInfo import ProductStyle
from colorUtils import ReorderColorBytesMcf2Rl
from fontHandling import getAvailableFont
mcf2rl = reportlab.lib.pagesizes.mm/10 # == 72/254, converts from mcf (unit=0.1mm) to reportlab (unit=inch/72)
# <pagenumbering bgcolor="#00000000" fontbold="0" fontfamily="Crafty Girls" fontitalics="0" fontsize="12" format="0"
# margin="52" position="4" textcolor="#ff000000" textstring="Page %" verticalMargin="53">
# <outline width="1"/>
# </pagenumbering>
class PageNumberPosition(Enum):
LEFT = "left"
RIGHT = "right"
ORIGINAL = "original"
@staticmethod
def ToEnum(value: str):
try:
return PageNumberPosition(value)
except ValueError:
logging.error(f"'{value}' is not a valid PageNumberPosition")
return PageNumberPosition.ORIGINAL
class PageNumberFormat(Enum):
ARABIC = "0"
ROMANLC = "1"
ROMANUC = "2"
ALPHALC = "3"
ALPHAUC = "4"
BINARY = "5"
HEX = "6"
@staticmethod
def ToEnum(value: str):
try:
return PageNumberFormat(value)
except ValueError:
logging.error(f"'{value}' is not a valid PageNumberFormat")
return PageNumberFormat.ARABIC
class PageNumberingInfo:
def __init__(self, pageNumberElement, pdf, availableFonts):
"""
Constructor that initializes the page numbering info from the given lxml element.
"""
# Extract relevant attributes or sub-elements from lxml_element
self.position = int(pageNumberElement.get('position','1'))
if self.position not in [1,2,4,5]:
logging.error(f"Unrecognised pagenumbering position value {self.position}, reset to 1")
self.position = 1
self.format = PageNumberFormat.ToEnum(pageNumberElement.get('format','0'))
self.horizontalMargin = int(pageNumberElement.get('margin','50')) * mcf2rl # * 0.1 mm
self.verticalMargin = int(pageNumberElement.get('verticalMargin','50')) * mcf2rl # * 0.1 mm
fontfamily = pageNumberElement.get('fontfamily','Liberation Sans')
self.fontfamily = getAvailableFont(fontfamily, pdf, availableFonts)
self.fontsize = int(pageNumberElement.get('fontsize','12'))
self.fontbold = int(pageNumberElement.get('fontbold','0'))
self.fontitalics = int(pageNumberElement.get('fontitalics','0'))
self.textstring = pageNumberElement.get('textstring','%')
self.textcolor = ReorderColorBytesMcf2Rl(pageNumberElement.get('textcolor','#ff000000'))
self.bgcolor = ReorderColorBytesMcf2Rl(pageNumberElement.get('bgcolor','#00000000'))
self.paragraphStyle = ParagraphStyle(None, None,
alignment=reportlab.lib.enums.TA_CENTER,
fontSize=self.fontsize,
fontName=self.fontfamily,
leading=self.fontsize,
borderPadding=0,
borderWidth=0,
leftIndent=0,
rightIndent=0,
# backColor=self.bgcolor, # text bg not used since ColorFrame colours the whole bg
textColor=self.textcolor)
def getNumberString(self, pageNumber):
if self.format == PageNumberFormat.ROMANLC:
numberString = self.toRoman(pageNumber, lowerCase=True)
elif self.format == PageNumberFormat.ROMANUC:
numberString = self.toRoman(pageNumber)
elif self.format == PageNumberFormat.ALPHALC:
numberString = self.toAlphabetic(pageNumber, lowerCase=True)
elif self.format == PageNumberFormat.ALPHAUC:
numberString = self.toAlphabetic(pageNumber)
elif self.format == PageNumberFormat.BINARY:
numberString = self.toBinary(pageNumber)
elif self.format == PageNumberFormat.HEX:
numberString = self.toHexadecimal(pageNumber)
else:
numberString = str(pageNumber)
return numberString
def getNumberText(self, pageNumber) -> str:
numberString = self.getNumberString(pageNumber)
numberText = self.textstring.replace("%",numberString)
return numberText
def getParagraphString(self, pageNumber) -> str:
numberText = self.getNumberText(pageNumber)
boldstart = '<b>' if self.fontbold != 0 else ''
boldend = '</b>' if self.fontbold != 0 else ''
italicstart = '<i>' if self.fontitalics != 0 else ''
italicend = '</i>' if self.fontitalics != 0 else ''
paragraphstring = f'{boldstart}{italicstart}{numberText}{italicend}{boldend}'
return paragraphstring
def getParagraphText(self, pageNumber) -> str:
paragraphstring = self.getParagraphString(pageNumber)
paragraphText = f'<para>{paragraphstring}</para>'
return paragraphText
@staticmethod
def toRoman(num, lowerCase=False) -> str:
"""
Convert an integer to a Roman numeral.
"""
roman_numerals = {
1: "I", 4: "IV", 5: "V", 9: "IX",
10: "X", 40: "XL", 50: "L", 90: "XC",
100: "C", 400: "CD", 500: "D", 900: "CM", 1000: "M"
}
if num <= 0:
raise ValueError("Number must be greater than zero")
result = ""
for value, numeral in sorted(roman_numerals.items(), reverse=True):
while num >= value:
result += numeral
num -= value
if lowerCase:
result = result.lower()
return result
@staticmethod
def toAlphabetic(num, lowerCase=False) -> str:
"""
Convert a number to an alphabetic sequence (A-Z, AA-ZZ, etc.).
"""
if num <= 0:
raise ValueError("Number must be greater than zero")
result = ""
while num > 0:
num -= 1 # Adjust for 1-based indexing
result = chr(num % 26 + ord('A')) + result
num //= 26
if lowerCase:
result = result.lower()
return result
@staticmethod
def toBinary(num):
"""
Convert a number to its binary representation.
"""
if num < 0:
raise ValueError("Number must be non-negative")
return bin(num)[2:] # Removes the '0b' prefix
@staticmethod
def toHexadecimal(num, width=2):
"""
Convert a number to its hexadecimal representation.
"""
if num < 0:
raise ValueError("Number must be non-negative")
return hex(num)[2:].upper().zfill(width) # Removes the '0x' prefix, converts to uppercase and pads
def horizontalPageNumberAdjustment(pnp, pageNumberingInfo, sideWidth, frameWidth, productStyle, oddpage):
# We are calculating the horizontal adjustment needed within one side to achieve
# outer edge placement. The adjustment because of the double page width for a double
# sided album output is external to this code, as is the adjustment for centred
# placement. That is, this code is ONLY for outer edge positioning.
# The original outer placement is left on left pages, and right on right pages,
# but in the case where we have converted the album to a single sided pdf (probably
# the most usual case) then it is not unlikely that we would prefer all the pages
# to have their outer edge number in the same place. There is a risk, of course,
# that the repositioned number might clash with some element on the page, but that's
# up to the user to check (and is why the default value is the original placement)
if productStyle == ProductStyle.AlbumSingleSide:
if pnp == PageNumberPosition.RIGHT:
return sideWidth - pageNumberingInfo.horizontalMargin - frameWidth
if pnp == PageNumberPosition.LEFT:
return pageNumberingInfo.horizontalMargin
# all other cases drop through here to original outer edge page number position
if oddpage:
return sideWidth - pageNumberingInfo.horizontalMargin - frameWidth
return pageNumberingInfo.horizontalMargin
def getPageNumberXy(pnp, pageNumberingInfo, pdf, frameWidth, frameHeight, productStyle, oddpage):
pagesize = (pdf._pagesize[0],pdf._pagesize[1]) # pagesize in rl units
sideHeight = pagesize[1]
if productStyle == ProductStyle.AlbumDoubleSide:
sideWidth = pagesize[0] / 2
cx = sideWidth if oddpage else 0 # moving to right hand side for odd pages in double sided
else:
sideWidth = pagesize[0]
cx = 0
# finally can calculate the actual position
if pageNumberingInfo.position == 1: # outer top
cy = sideHeight - pageNumberingInfo.verticalMargin - frameHeight
cx = cx + horizontalPageNumberAdjustment(pnp, pageNumberingInfo, sideWidth, frameWidth, productStyle, oddpage)
elif pageNumberingInfo.position == 4: # outer bottom
cy = pageNumberingInfo.verticalMargin
cx = cx + horizontalPageNumberAdjustment(pnp, pageNumberingInfo, sideWidth, frameWidth, productStyle, oddpage)
elif pageNumberingInfo.position == 2: # centre top
cy = sideHeight - pageNumberingInfo.verticalMargin - frameHeight
cx = cx + 0.5 * (sideWidth - frameWidth)
elif pageNumberingInfo.position == 5: # centre bottom
cy = pageNumberingInfo.verticalMargin
cx = cx + 0.5 * (sideWidth - frameWidth)
else:
# can't actually happen because pageNumberingInfo checks the position
return 0,0
return cx,cy