def parseRaw(data, node): """ Parse and process a simple DSL to handle creation of flowables. Supported (can add others on request): * PageBreak * Spacer width, height """ elements = [] lines = data.splitlines() for line in lines: lexer = shlex.shlex(line) lexer.whitespace += ',' tokens = list(lexer) if not tokens: continue # Empty line command = tokens[0] if command == 'PageBreak': if len(tokens) == 1: elements.append(MyPageBreak()) else: elements.append(MyPageBreak(tokens[1])) elif command == 'EvenPageBreak': if len(tokens) == 1: elements.append(MyPageBreak(breakTo='even')) else: elements.append(MyPageBreak(tokens[1], breakTo='even')) elif command == 'OddPageBreak': if len(tokens) == 1: elements.append(MyPageBreak(breakTo='odd')) else: elements.append(MyPageBreak(tokens[1], breakTo='odd')) # TODO: Where does CondPageBreak come from? # elif command == 'FrameBreak': # if len(tokens) == 1: # elements.append(CondPageBreak(99999)) # else: # elements.append(CondPageBreak(float(tokens[1]))) elif command == 'Spacer': elements.append( MySpacer(adjustUnits(tokens[1]), adjustUnits(tokens[2]))) elif command == 'Transition': elements.append(Transition(*tokens[1:])) elif command == 'SetPageCounter': elements.append(PageCounter(*tokens[1:])) else: log.error('Unknown command %s in raw pdf directive [%s]' % (command, nodeid(node))) return elements
def parseRaw(data, node): """ Parse and process a simple DSL to handle creation of flowables. Supported (can add others on request): * PageBreak * Spacer width, height """ elements = [] lines = data.splitlines() for line in lines: lexer = shlex.shlex(line) lexer.whitespace += "," tokens = list(lexer) if not tokens: continue # Empty line command = tokens[0] if command == "PageBreak": if len(tokens) == 1: elements.append(MyPageBreak()) else: elements.append(MyPageBreak(tokens[1])) elif command == "EvenPageBreak": if len(tokens) == 1: elements.append(MyPageBreak(breakTo="even")) else: elements.append(MyPageBreak(tokens[1], breakTo="even")) elif command == "OddPageBreak": if len(tokens) == 1: elements.append(MyPageBreak(breakTo="odd")) else: elements.append(MyPageBreak(tokens[1], breakTo="odd")) # TODO: Where does CondPageBreak come from? # elif command == 'FrameBreak': # if len(tokens) == 1: # elements.append(CondPageBreak(99999)) # else: # elements.append(CondPageBreak(float(tokens[1]))) elif command == "Spacer": elements.append(MySpacer(adjustUnits(tokens[1]), adjustUnits(tokens[2]))) elif command == "Transition": elements.append(Transition(*tokens[1:])) elif command == "SetPageCounter": elements.append(PageCounter(*tokens[1:])) else: log.error("Unknown command %s in raw pdf directive [%s]" % (command, nodeid(node))) return elements
def wrap(self, availWidth, availHeight): """ If we need more width than we have, complain, keep a scale. """ self.pad = self.border_padding(True, 0.1) maxWidth = float( min( styles.adjustUnits(self.maxWidth, availWidth) or availWidth, availWidth)) self.maxWidth = maxWidth maxWidth -= (self.pad[1] + self.pad[3]) # Paragraphs always say they can fit, so check the dims instead dims = [] _listWrapOn(self.content, maxWidth, None, dims=dims) self.width, self.height = min(dims, key=lambda i: i[0]) if self.width > maxWidth: if self.mode != 'shrink': self.scale = 1.0 log.warning( "BoundByWidth too wide to fit in frame (%s > %s): %s", self.width, maxWidth, self.identity()) if self.mode == 'shrink' and not self.scale: self.scale = ((maxWidth + self.pad[1] + self.pad[3]) / (self.width + self.pad[1] + self.pad[3])) else: self.scale = 1.0 self.height *= self.scale self.width *= self.scale return self.width, self.height + (self.pad[0] + self.pad[2]) * self.scale
def wrap(self, availWidth, availHeight): """ If we need more width than we have, complain, keep a scale. """ self.pad = self.border_padding(True, 0.1) maxWidth = float(min( styles.adjustUnits(self.maxWidth, availWidth) or availWidth, availWidth)) self.maxWidth = maxWidth maxWidth -= (self.pad[1] + self.pad[3]) # Paragraphs always say they can fit, so check the dims instead dims = [] _listWrapOn(self.content, maxWidth, None, dims=dims) self.width, self.height = min(dims, key=lambda i: i[0]) if self.width > maxWidth: if self.mode != 'shrink': self.scale = 1.0 log.warning("BoundByWidth too wide to fit in frame (%s > %s): %s", self.width, maxWidth, self.identity()) if self.mode == 'shrink' and not self.scale: self.scale = ((maxWidth + self.pad[1] + self.pad[3]) / (self.width + self.pad[1] + self.pad[3])) else: self.scale = 1.0 self.height *= self.scale self.width *= self.scale return self.width, self.height + (self.pad[0] + self.pad[2]) * self.scale
def gather_elements(self, client, node, style): # Create image calling plantuml tfile = tempfile.NamedTemporaryFile(dir='.', delete=False, suffix='.' + node['format']) args = 'plantuml -pipe -charset utf-8' if node['format'].lower() == 'svg': args += ' -tsvg' client.to_unlink.append(tfile.name) try: p = subprocess.Popen( args.split(), stdout=tfile, stdin=subprocess.PIPE, stderr=subprocess.PIPE, ) except OSError as err: if err.errno != errno.ENOENT: raise raise PlantUmlError('plantuml command %r cannot be run' % self.builder.config.plantuml) serr = p.communicate(node['uml'].encode('utf-8'))[1] if p.returncode != 0: raise PlantUmlError('error while running plantuml\n\n' + serr) # Convert width and height if necessary w = node['width'] if w is not None: w = adjustUnits(w) h = node['height'] if h is not None: h = adjustUnits(h) # Add Image node with the right image return [MyImage(tfile.name, client=client, width=w, height=h)]
def adjust(*args, **kwargs): kwargs['total'] = w return styles.adjustUnits(*args, **kwargs)