Пример #1
0
    def test_getCoords_simple(self):

        res = get_coordinates(1, 1, 10, 10, (10, 10))
        self.assertEqual(res, (1, -1, 10, 10))

        # A second time - it's memoized!
        res = get_coordinates(1, 1, 10, 10, (10, 10))
        self.assertEqual(res, (1, -1, 10, 10))
Пример #2
0
    def at_page(self, name, pseudopage, declarations):
        c = self.c
        data = {}
        name = name or "body"
        page_border = None

        if declarations:
            result = self.ruleset([self.selector('*')], declarations)

            if declarations:
                try:
                    data = result[0].values()[0]
                except Exception:
                    data = result[0].popitem()[1]
                page_border = data.get("-pdf-frame-border", None)

        if name in c.templateList:
            log.warn(
                self.c.warning("template '%s' has already been defined", name))

        if "-pdf-page-size" in data:
            c.pageSize = xhtml2pdf.default.PML_PAGESIZES.get(
                str(data["-pdf-page-size"]).lower(), c.pageSize)

        is_landscape = False
        if "size" in data:
            size = data["size"]
            if type(size) is not ListType:
                size = [size]
            size_list = []
            for value in size:
                valueStr = str(value).lower()
                if isinstance(value, tuple):
                    size_list.append(get_size(value))
                elif valueStr == "landscape":
                    is_landscape = True
                elif valueStr == "portrait":
                    is_landscape = False
                elif valueStr in xhtml2pdf.default.PML_PAGESIZES:
                    c.pageSize = xhtml2pdf.default.PML_PAGESIZES[valueStr]
                else:
                    log.warn(c.warning("Unknown size value for @page"))

            if len(size_list) == 2:
                c.pageSize = tuple(size_list)
            if is_landscape:
                c.pageSize = landscape(c.pageSize)

        padding_top = self._get_from_data(data, 'padding-top', 0, get_size)
        padding_left = self._get_from_data(data, 'padding-left', 0, get_size)
        padding_right = self._get_from_data(data, 'padding-right', 0, get_size)
        padding_bottom = self._get_from_data(data, 'padding-bottom', 0,
                                             get_size)
        border_color = self._get_from_data(
            data, ('border-top-color', 'border-bottom-color',
                   'border-left-color', 'border-right-color'), None, get_color)
        border_width = self._get_from_data(
            data, ('border-top-width', 'border-bottom-width',
                   'border-left-width', 'border-right-width'), 0, get_size)

        for prop in ("margin-top", "margin-left", "margin-right",
                     "margin-bottom", "top", "left", "right", "bottom",
                     "width", "height"):
            if prop in data:
                c.frameList.append(
                    self._pisa_add_frame(name,
                                         data,
                                         first=True,
                                         border=page_border,
                                         size=c.pageSize))
                break

        # Frames have to be calculated after we know the pagesize
        frame_list = []
        static_list = []
        for fname, static, border, x, y, w, h, fdata in c.frameList:
            fpadding_top = self._get_from_data(fdata, 'padding-top',
                                               padding_top, get_size)
            fpadding_left = self._get_from_data(fdata, 'padding-left',
                                                padding_left, get_size)
            fpadding_right = self._get_from_data(fdata, 'padding-right',
                                                 padding_right, get_size)
            fpadding_bottom = self._get_from_data(fdata, 'padding-bottom',
                                                  padding_bottom, get_size)
            fborder_color = self._get_from_data(
                fdata, ('border-top-color', 'border-bottom-color',
                        'border-left-color', 'border-right-color'),
                border_color, get_color)
            fborder_width = self._get_from_data(
                fdata, ('border-top-width', 'border-bottom-width',
                        'border-left-width', 'border-right-width'),
                border_width, get_size)

            if border or page_border:
                frame_border = ShowBoundaryValue()
            else:
                frame_border = ShowBoundaryValue(color=fborder_color,
                                                 width=fborder_width)

            #fix frame sizing problem.
            if static:
                x, y, w, h = get_frame_dimensions(fdata, c.pageSize[0],
                                                  c.pageSize[1])
            x, y, w, h = get_coordinates(x, y, w, h, c.pageSize)
            if w <= 0 or h <= 0:
                log.warn(
                    self.c.warning(
                        "Negative width or height of frame. Check @frame definitions."
                    ))

            frame = Frame(x,
                          y,
                          w,
                          h,
                          id=fname,
                          leftPadding=fpadding_left,
                          rightPadding=fpadding_right,
                          bottomPadding=fpadding_bottom,
                          topPadding=fpadding_top,
                          showBoundary=frame_border)

            if static:
                frame.pisaStaticStory = []
                c.frameStatic[static] = [frame] + c.frameStatic.get(static, [])
                static_list.append(frame)
            else:
                frame_list.append(frame)

        background = data.get("background-image", None)
        if background:
            #should be relative to the css file
            background = self.c.get_file(background,
                                         relative=self.c.cssParser.rootPath)

        if not frame_list:
            log.warn(
                c.warning(
                    "missing explicit frame definition for content or just static frames"
                ))
            fname, static, border, x, y, w, h, data = self._pisa_add_frame(
                name, data, first=True, border=page_border, size=c.pageSize)
            x, y, w, h = get_coordinates(x, y, w, h, c.pageSize)
            if w <= 0 or h <= 0:
                log.warn(
                    c.warning(
                        "Negative width or height of frame. Check @page definitions."
                    ))

            if border or page_border:
                frame_border = ShowBoundaryValue()
            else:
                frame_border = ShowBoundaryValue(color=border_color,
                                                 width=border_width)

            frame_list.append(
                Frame(x,
                      y,
                      w,
                      h,
                      id=fname,
                      leftPadding=padding_left,
                      rightPadding=padding_right,
                      bottomPadding=padding_bottom,
                      topPadding=padding_top,
                      showBoundary=frame_border))

        pt = PmlPageTemplate(
            id=name,
            frames=frame_list,
            pagesize=c.pageSize,
        )
        pt.pisaStaticList = static_list
        pt.pisaBackground = background
        pt.pisaBackgroundList = c.pisaBackgroundList

        if is_landscape:
            pt.pageorientation = pt.LANDSCAPE

        c.templateList[name] = pt
        c.template = None
        c.frameList = []
        c.frameStaticList = []

        return {}, {}
Пример #3
0
 def test_getCoords_w_and_h_none(self):
     res = get_coordinates(1, 1, None, None, (10, 10))
     self.assertEqual(res, (1, 9))
Пример #4
0
 def test_getCoords_h_lt_0(self):
     res = get_coordinates(1, 1, 10, -1, (10, 10))
     self.assertEqual(res, (1, 1, 10, 8))
Пример #5
0
 def test_getCoords_x_lt_0(self):
     res = get_coordinates(-1, 1, 10, 10, (10, 10))
     self.assertEqual(res, (9, -1, 10, 10))