Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revise toc calculation #2047

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
10 changes: 5 additions & 5 deletions pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@
extract_as_dict = self._render(extract_record, value[1])
feature_geometry = mapping(extract_record.real_estate.limit)

if Config.get('print', {}).get('compute_toc_pages', False):
extract_as_dict['nbTocPages'] = TocPages(extract_as_dict).getNbPages()
else:
extract_as_dict['nbTocPages'] = 1

# set the global_datetime variable so that it can be used later for the archive
self.set_global_datetime(extract_as_dict['CreationDate'])
self.convert_to_printable_extract(extract_as_dict, feature_geometry)
Expand All @@ -96,6 +91,11 @@
'display_qrcode', False
)

if print_config.get('compute_toc_pages', False):
extract_as_dict['nbTocPages'] = TocPages(extract_as_dict).getNbPages()
else:
extract_as_dict['nbTocPages'] = 1

Check warning on line 97 in pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py

View check run for this annotation

Codecov / codecov/patch

pyramid_oereb/contrib/print_proxy/mapfish_print/mapfish_print.py#L97

Added line #L97 was not covered by tests

spec = {
'layout': Config.get('print', {})['template_name'],
'outputFormat': 'pdf',
Expand Down
225 changes: 155 additions & 70 deletions pyramid_oereb/contrib/print_proxy/mapfish_print/toc_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,116 +5,201 @@
log = logging.getLogger(__name__)


class TocPages():
class TocPages:

def __init__(self, extract):
# variables taken from template toc.jrxml
self.disposable_height = 842 - 70 # A4 size - (footer + header)
self.d1_height = 77
self.d2_height = 29
self.d3_height = 61
self.d4_height = 44
self.d5_height = 15
self.d6_height = 90
self.d6_right_height = 23
self.d6_right_width = 233
self.d6_stuff_y_location = 39
self.d6_left_height = 0 # FIXME: compute this
self.title_size = 62
self.toc_title_height = 15 + 62 + 12 # height + location + item starting position
self.toc_item_height = 20
self.not_concerned_themes_title_height = 15 + 26 # height + location
self.not_concerned_themes_item_height = 12
self.theme_without_data_title_height = 12
self.theme_without_data_item_height = 12
self.total_height = 842
self.header_height = self.compute_header()
self.footer_height = self.compute_footer()
self.disposable_height = (
842 - self.header_height - self.footer_height
) # A4 size - (footer + header)
self.d1_height = 77 # toc.jrxml
self.d2_height = 29 # toc.jrxml
self.d3_height = 61 # toc.jrxml
self.d4_height = 44 # toc.jrxml
self.d5_height = 93 # toc.jrxml
self.d6_height = 38 # toc.jrxml
self.d6_left_height = 38 # toc.jrxml
self.d6_right_height = 20 # toc.jrxml
self.extract = extract
self.display_qrcode = self.extract["Display_QRCode"]
self.total_length = self.compute_total_lenght()

def compute_header(self):
total_size = 0
page_top_margin = 28 # toc.jrxml
header_height = 60 # toc.jrxml
total_size += page_top_margin + header_height
log.debug(f"header total_size: {total_size}")
return total_size

def compute_footer(self):
total_size = 0
page_bottom_margin = 20 # toc.jrxml
footer_height = 10 # toc.jrxml
total_size += page_bottom_margin + footer_height
log.debug(f"header total_size: {total_size}")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

total_size is always zero.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well spotted, thanks! I just commited a bugfix.

return total_size

def compute_d1(self):
# The ConcernedTheme-Heading cannot be calculated at runtime. The used label
# is defined in the mfp-templates which are not accessible here. Therefore
# we assume one line without a line break.
log.debug(f"d1 total_size: {self.d1_height}")
return self.d1_height

def compute_d2(self):
x = len(self.extract['ConcernedTheme'] * self.toc_item_height)
if x > self.d2_height:
return x
total_size = 0
blank_space_above = 2 # toc.jrxml
page_label_height = 10 # toc.jrxml
total_size += blank_space_above + page_label_height
toc_item_height = 17 # toc.jrxml (20 in tocConcernedTheme.jrxml)
unique_concerned_themes = []
for concerned_theme in self.extract["ConcernedTheme"]:
if concerned_theme not in unique_concerned_themes:
unique_concerned_themes.append(concerned_theme)
total_size += len(unique_concerned_themes) * toc_item_height
log.debug(f"d2 total_size: {total_size}")
if total_size > self.d2_height:
return total_size
else:
return self.d2_height

def compute_d3(self):
x = self.not_concerned_themes_title_height + len(self.extract['NotConcernedTheme'] * self.not_concerned_themes_item_height) # noqa
if x > self.d3_height:
return x
total_size = 0
blank_space_above = 26 # toc.jrxml
not_concerned_themes_title_height = 15 # toc.jrxml
blank_space_between = 5 # toc.jrxml
not_concerned_themes_first_item_height = 15 # toc.jrxml
not_concerned_themes_further_items_height = 12 # themelist.jrxml
total_size += (
blank_space_above + not_concerned_themes_title_height + blank_space_between
)
total_size += not_concerned_themes_first_item_height + (
(len(self.extract["NotConcernedTheme"]) - 1)
* not_concerned_themes_further_items_height
)
log.debug(f"d3 total_size: {total_size}")
if total_size > self.d3_height:
return total_size
else:
return self.d3_height

def compute_d4(self):
# The NoDataTheme-Heading cannot be calculated at runtime. The used label
# is defined in the mfp-templates which are not accessible here. Therefore
# we assume one line without a line break.
log.debug(f"d4 total_size: {self.d4_height}")
return self.d4_height

def compute_d5(self):
x = len(self.extract['ThemeWithoutData'] * self.theme_without_data_item_height)
if x > self.d5_height:
return x
total_size = 0
theme_without_data_first_item_height = 15 # toc.jrxml
theme_without_data_further_items_height = 12 # themelist.jrxml
total_size += theme_without_data_first_item_height + (
(len(self.extract["ThemeWithoutData"]) - 1)
* theme_without_data_further_items_height
)
log.debug(f"d5 total_size: {total_size}")
if total_size > self.d5_height:
return total_size

Check warning on line 106 in pyramid_oereb/contrib/print_proxy/mapfish_print/toc_pages.py

View check run for this annotation

Codecov / codecov/patch

pyramid_oereb/contrib/print_proxy/mapfish_print/toc_pages.py#L106

Added line #L106 was not covered by tests
else:
return self.d5_height

def compute_d6_left(self):
# TODO: compute height when QR-Code is integrated
content_min_size = 10 + 10 + 5 + 10 + 10 # spacing between paragraphs
total_size = 39
paragraph_space = 11
for i in self.extract.get('GeneralInformation', []):
total_size += paragraph_space
total_size += self.compute_length_of_wrapped_text(i[0]['Text'],
78,
10)
log.debug('d6 left total_size : {}'.format(total_size))
if total_size > content_min_size:
total_size = 0

# General Information (1 title, multiple items)
general_information_title_height = 8 # general_info_and_disclaimer.jrxml
general_information_item_line_heigth = 8 # general_info_and_disclaimer.jrxml
total_size += general_information_title_height
for i in self.extract.get("GeneralInformation", []):
total_size += self.compute_length_of_wrapped_text(
i["Info"], 78, general_information_item_line_heigth
)
# LandRegister-Disclaimer (1 title, 1 item)
land_register_disclaimer_title_line_height = (
8 # general_info_and_disclaimer.jrxml
)
land_register_disclaimer_item_line_height = (
8 # general_info_and_disclaimer.jrxml
)

total_size += self.compute_length_of_wrapped_text(
self.extract.get("DisclaimerLandRegister_Title", ""),
65,
land_register_disclaimer_title_line_height,
)
total_size += self.compute_length_of_wrapped_text(
self.extract.get("DisclaimerLandRegister_Content", ""),
78,
land_register_disclaimer_item_line_height,
)
log.debug("d6 left total_size : {}".format(total_size))
if total_size > self.d6_left_height:
return total_size
else:
return content_min_size
return self.d6_left_height

@staticmethod
def compute_length_of_wrapped_text(text, nb_char, font_size):
def compute_length_of_wrapped_text(text, nb_char, line_height):
t = textwrap.wrap(text, nb_char)
return len(t) * font_size
return len(t) * line_height

def compute_d6_right(self):
# variables taken from template disclaimer.jrxml
space_above = 4
space_title_content = 2
content_min_size = 23
total_size = 0
for i in self.extract.get('Disclaimer', []):
total_size += space_above
total_size += self.compute_length_of_wrapped_text(i['Title'][0]['Text'],
65,
14)
total_size += space_title_content
total_size += self.compute_length_of_wrapped_text(i['Content'][0]['Text'],
78,
10)
log.debug('d6 ritght total_size : {}'.format(total_size))
if total_size > content_min_size:

blank_space_below_disclaimers = 6 # disclaimer_and_qrcode.jrxml
disclaimer_title_line_height = 8 # disclaimer_and_qrcode.jrxml
disclaimer_item_line_height = 8 # disclaimer_and_qrcode.jrxml
blank_space_above_qrcode = 13 # disclaimer_and_qrcode.jrxml
qr_code_size = 56 # disclaimer_and_qrcode.jrxml

# Disclaimers (multiple items)
for i in self.extract.get("Disclaimer", []):
total_size += self.compute_length_of_wrapped_text(
i["Title"], 65, disclaimer_title_line_height
)
total_size += self.compute_length_of_wrapped_text(
i["Content"], 78, disclaimer_item_line_height
)
total_size += blank_space_below_disclaimers

# QR-Code (optional)
if self.display_qrcode:
total_size += blank_space_above_qrcode + qr_code_size

Check warning on line 171 in pyramid_oereb/contrib/print_proxy/mapfish_print/toc_pages.py

View check run for this annotation

Codecov / codecov/patch

pyramid_oereb/contrib/print_proxy/mapfish_print/toc_pages.py#L171

Added line #L171 was not covered by tests

log.debug("d6 right total_size : {}".format(total_size))
if total_size > self.d6_right_height:
return total_size
else:
return content_min_size
return self.d6_right_height

def compute_d6(self):
x = max(self.compute_d6_left(), self.compute_d6_right()) + self.d6_stuff_y_location
if x > self.d6_height:
return x
total_size = max(self.compute_d6_left(), self.compute_d6_right())
log.debug(f"d6 total_size: {total_size}")
if total_size > self.d6_height:
return total_size
else:
return self.d6_height

def compute_total_lenght(self):
x = self.compute_d1() + \
self.compute_d2() + \
self.compute_d3() + \
self.compute_d4() + \
self.compute_d5() + \
self.compute_d6()
log.debug('TOC total page length : {}'.format(x))
x = (
self.compute_d1()
+ self.compute_d2()
+ self.compute_d3()
+ self.compute_d4()
+ self.compute_d5()
+ self.compute_d6()
)
log.debug("TOC total page length : {}".format(x))
log.debug(f"disposable height: {self.disposable_height}")
return x

def getNbPages(self):
return -(-self.total_length // self.disposable_height) # ceil number of pages needed
number_of_pages = -(
-self.total_length // self.disposable_height
) # ceil number of pages needed
log.debug(f"number of pages: {number_of_pages}")
return number_of_pages
Loading
Loading