ext/xhtml2fo.py
changeset 5195 e8235b2789fc
parent 5184 955ee1b24756
child 5196 d14bfd477c44
equal deleted inserted replaced
5184:955ee1b24756 5195:e8235b2789fc
     1 from xml.etree.ElementTree import QName
       
     2 from pysixt.standard.xhtml_xslfo.transformer import XHTML2FOTransformer
       
     3 from pysixt.utils.xslfo.standard import cm
       
     4 from pysixt.utils.xslfo import SimplePageMaster
       
     5 from pysixt.standard.xhtml_xslfo.default_styling import default_styles
       
     6 from pysixt.standard.xhtml_xslfo import XHTML_NS
       
     7 
       
     8 
       
     9 class ReportTransformer(XHTML2FOTransformer):
       
    10     """
       
    11     Class transforming an XHTML input tree into a FO document
       
    12     displaying reports (one report for each <div class="contentmain">
       
    13     element in the input tree.
       
    14     """
       
    15 
       
    16     def __init__(self, section,
       
    17                  page_width=21.0, page_height=29.7,
       
    18                  margin_top=1.0, margin_bottom=1.0,
       
    19                  margin_left=1.0, margin_right=1.0,
       
    20                  header_footer_height=0.75,
       
    21                  standard_font_size=11.0, default_lang=u"fr" ):
       
    22         """
       
    23         Initializes a transformer turning an XHTML input tree
       
    24         containing <div class="contentmain"> elements representing
       
    25         main content sections into a FO output tree displaying the
       
    26         reports.
       
    27 
       
    28         page_width: float - width of the page (in cm)
       
    29         page_height: float - height of the page (in cm)
       
    30         margin_top: float - top margin of the page (in cm)
       
    31         margin_bottom: float - bottom margin of the page (in cm)
       
    32         margin_left: float - left margin of the page (in cm)
       
    33         margin_right: float - right margin of the page (in cm)
       
    34         header_footer_height: float - height of the header or the footer of the
       
    35                               page that the page number (if any) will be
       
    36                               inserted in.
       
    37         standard_font_size: float - standard size of the font (in pt)
       
    38         default_lang: u"" - default language (used for hyphenation)
       
    39         """
       
    40         self.section = section
       
    41         self.page_width = page_width
       
    42         self.page_height = page_height
       
    43 
       
    44         self.page_tmargin = margin_top
       
    45         self.page_bmargin = margin_bottom
       
    46         self.page_lmargin = margin_left
       
    47         self.page_rmargin = margin_right
       
    48 
       
    49         self.hf_height = header_footer_height
       
    50 
       
    51         self.font_size = standard_font_size
       
    52         self.lang = default_lang
       
    53 
       
    54         XHTML2FOTransformer.__init__(self)
       
    55 
       
    56 
       
    57     def define_pagemasters(self):
       
    58         """
       
    59         Defines the page masters for the FO output document.
       
    60         """
       
    61         pm = SimplePageMaster(u"page-report")
       
    62         pm.set_page_dims( self.page_width*cm, self.page_height*cm )
       
    63         pm.set_page_margins({u'top'   : self.page_tmargin*cm,
       
    64                              u'bottom': self.page_bmargin*cm,
       
    65                              u'left'  : self.page_lmargin*cm,
       
    66                              u'right' : self.page_rmargin*cm })
       
    67         pm.add_peripheral_region(u"end", self.hf_height)
       
    68         dims = {}
       
    69         dims[u"bottom"] = self.hf_height + 0.25
       
    70         pm.set_main_region_margins(dims)
       
    71         return [pm]
       
    72 
       
    73     def _visit_report(self, in_elt, _out_elt, params):
       
    74         """
       
    75         Specific visit function for the input <div> elements whose class is
       
    76         "report". The _root_visit method of this class selects these input
       
    77         elements and asks the process of these elements with this specific
       
    78         visit function.
       
    79         """
       
    80 
       
    81         ps = self.create_pagesequence(u"page-report")
       
    82         props = { u"force-page-count": u"no-force",
       
    83                   u"initial-page-number": u"1",
       
    84                   u"format": u"1", }
       
    85         self._output_properties(ps, props)
       
    86 
       
    87         sc = self.create_staticcontent(ps, u"end")
       
    88         sc_bl = self.create_block(sc)
       
    89         attrs = { u"hyphenate": u"false", }
       
    90         attrs[u"font-size"] = u"%.1fpt" % (self.font_size * 0.7)
       
    91         attrs[u"language"] = self.lang
       
    92         attrs[u"text-align"] = u"center"
       
    93         self._output_properties(sc_bl, attrs)
       
    94         sc_bl.text = u"Page" + u" " # ### Should be localised!
       
    95         pn = self.create_pagenumber(sc_bl)
       
    96         pn.tail = u"/"
       
    97         self.create_pagenumbercitation(
       
    98             sc_bl, u"last-block-of-report-%d" % params[u"context_pos"])
       
    99 
       
   100         fl = self.create_flow(ps, u"body")
       
   101         bl = self.create_block(fl)
       
   102 
       
   103         # Sets on the highest block element the properties of the XHTML body
       
   104         # element. These properties (at the least the inheritable ones) will
       
   105         # be inherited by all the future FO elements.
       
   106         bodies = list(self.in_tree.getiterator(QName(XHTML_NS, u"body")))
       
   107         if len(bodies) > 0:
       
   108             attrs = self._extract_properties([bodies[0]])
       
   109         else:
       
   110             attrs = default_styles[u"body"].copy()
       
   111         attrs[u"font-size"] = u"%.1fpt" % self.font_size
       
   112         attrs[u"language"] = self.lang
       
   113         self._output_properties(bl,attrs)
       
   114 
       
   115         # Processes the report content
       
   116         self._copy_text(in_elt, bl)
       
   117         self._process_nodes(in_elt.getchildren(), bl)
       
   118 
       
   119         # Inserts an empty block at the end of the report in order to be able
       
   120         # to compute the last page number of this report.
       
   121         last_bl = self.create_block(bl)
       
   122         props = { u"keep-with-previous": u"always", }
       
   123         props[u"id"] = u"last-block-of-report-%d" % params[u"context_pos"]
       
   124         self._output_properties(last_bl,props)
       
   125 
       
   126 
       
   127     def _root_visit(self):
       
   128         """
       
   129         Visit function called when starting the process of the input tree.
       
   130         """
       
   131         content = [ d for d in self.in_tree.getiterator(QName(XHTML_NS, u"div"))
       
   132                     if d.get(u"id") == self.section ]
       
   133         # Asks the process of the report elements with a specific visit
       
   134         # function
       
   135         self._process_nodes(content, self.fo_root,
       
   136                             with_function=self._visit_report)
       
   137