Exemplo n.º 1
0
    def wrap(self,availWidth,availHeight):
        from doctemplate import LayoutError
        mode = self.mode
        maxWidth = float(min(self.maxWidth or availWidth,availWidth))
        maxHeight = float(min(self.maxHeight or availHeight,availHeight))
        W, H = _listWrapOn(self._content,maxWidth,self.canv)
        if (mode=='error' and (W>maxWidth+_FUZZ or H>maxHeight+_FUZZ)):
            ident = 'content %sx%s too large for %s' % (W,H,self.identity(30))
            #leave to keep apart from the raise
            raise LayoutError(ident)
        elif W<=maxWidth+_FUZZ and H<=maxHeight+_FUZZ:
            self.width = W-_FUZZ      #we take what we get
            self.height = H-_FUZZ
        elif mode in ('overflow','truncate'):   #we lie
            self.width = min(maxWidth,W)-_FUZZ
            self.height = min(maxHeight,H)-_FUZZ
        else:
            def func(x):
                W, H = _listWrapOn(self._content,x*maxWidth,self.canv)
                W /= x
                H /= x
                return W, H
            W0 = W
            H0 = H
            s0 = 1
            if W>maxWidth+_FUZZ:
                #squeeze out the excess width and or Height
                s1 = W/maxWidth
                W, H = func(s1)
                if H<=maxHeight+_FUZZ:
                    self.width = W-_FUZZ
                    self.height = H-_FUZZ
                    self._scale = s1
                    return W,H
                s0 = s1
                H0 = H
                W0 = W
            s1 = H/maxHeight
            W, H = func(s1)
            self.width = W-_FUZZ
            self.height = H-_FUZZ
            self._scale = s1
            if H<min(0.95*maxHeight,maxHeight-10) or H>=maxHeight+_FUZZ:
                #the standard case W should be OK, H is short we want
                #to find the smallest s with H<=maxHeight
                H1 = H
                for f in 0, 0.01, 0.05, 0.10, 0.15:
                    #apply the quadratic model
                    s = _qsolve(maxHeight*(1-f),_hmodel(s0,s1,H0,H1))
                    W, H = func(s)
                    if H<=maxHeight+_FUZZ and W<=maxWidth+_FUZZ:
                        self.width = W-_FUZZ
                        self.height = H-_FUZZ
                        self._scale = s
                        break

        return self.width, self.height
Exemplo n.º 2
0
    def wrap(self, aW, aH):
        frame = self._frame
        from reportlab.platypus.doctemplate import NextPageTemplate, CurrentFrameFlowable, LayoutError
        G = [NextPageTemplate(self.nextTemplate)]
        if aH < self.gap + self.required - _FUZZ:
            #we are going straight to the nextTemplate with no attempt to modify the frames
            G.append(PageBreak())
        else:
            #we are going to modify the incoming templates
            templates = self._doctemplateAttr('pageTemplates')
            if templates is None:
                raise LayoutError('%s called in non-doctemplate environment' %
                                  self.identity())
            T = [t for t in templates if t.id == self.nextTemplate]
            if not T:
                raise LayoutError('%s.nextTemplate=%s not found' %
                                  (self.identity(), self.nextTemplate))
            T = T[0]
            F = [f for f in T.frames if f.id in self.nextFrames]
            N = [f.id for f in F]
            N = [f for f in self.nextFrames if f not in N]
            if N:
                raise LayoutError(
                    '%s frames=%r not found in pageTemplate(%s)\n%r has frames %r'
                    % (self.identity(), N, T.id, T, [f.id for f in T.frames]))
            T = self._doctemplateAttr('pageTemplate')

            def unwrap(canv, doc, T=T, onPage=T.onPage, oldFrames=T.frames):
                T.frames = oldFrames
                T.onPage = onPage
                onPage(canv, doc)

            T.onPage = unwrap
            h = aH - self.gap
            for i, f in enumerate(F):
                f = copy(f)
                f.height = h
                f._reset()
                F[i] = f
            T.frames = F
            G.append(CurrentFrameFlowable(F[0].id))
        frame.add_generated_content(*G)
        return 0, 0