Support all ODS units for creating schedules (e.g. LibreOffice and Microsoft Excel have different defaults from a quick test)

This commit is contained in:
Dion Moult
2020-08-28 18:31:45 +10:00
parent c368ae6596
commit e7aa8d0e27
2 changed files with 29 additions and 3 deletions
@@ -33,7 +33,7 @@ class Scheduler():
if not style_name or 'column-width' not in styles[style_name]:
column_widths.append(50)
else:
column_widths.append(self.convert_to_si(styles[style_name]['column-width']))
column_widths.append(self.convert_to_mm(styles[style_name]['column-width']))
y = self.margin
for tri, tr in enumerate(table.getElementsByType(TableRow)):
@@ -67,6 +67,28 @@ class Scheduler():
'dominant-baseline': 'hanging'
}))
def convert_to_si(self, value):
if 'in' in value:
def convert_to_mm(self, value):
# XSL is what defines the units of measurements in ODF
# https://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html#datatype-positiveLength
# https://www.w3.org/TR/2001/REC-xsl-20011015/slice5.html#section-N8185-Definitions-of-Units-of-Measure
if 'cm' in value:
return float(value[0:-2]) * 10
elif 'mm' in value:
return float(value[0:-2])
elif 'in' in value:
return float(value[0:-2]) * 25.4
elif 'pt' in value:
return float(value[0:-2]) * (1/72) * 25.4
elif 'pc' in value:
return float(value[0:-2]) * 12 * (1/72) * 25.4
elif 'px' in value:
# implementors may instead simply pick a fixed conversion factor,
# treating 'px' as an absolute unit of measurement (such as 1/92" or
# 1/72"). <-- We're picking 1/96 to match SVG. Let me know if it
# breaks anything.
return float(value[0:-2]) * (1/96) * 2.54 * 10
elif 'em' in value:
# This is a funny one. Since the scheduler at the moment enforces a
# font size of 2.5mm (vertically, FWIW), I'm writing this. Hopefully
# this code doesn't hurt anybody.
return float(value[0:-2]) * (1/96) * 2.54 * 10
@@ -209,6 +209,10 @@ class SheetBuilder:
return group
def convert_to_mm(self, value):
# CSS is what defines these possibilities
# https://www.w3.org/TR/SVG/refs.html#ref-css-values-3
# https://www.w3.org/TR/css-values-3/#absolute-lengths
# The relative units are not implemented. Go fish.
if 'cm' in value:
return float(value[0:-2]) * 10
elif 'mm' in value: