我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用cairo.PDFSurface()。
def barCoord(n): ''' returns ((x-left-top, y-left-top), (x-left-buttom, y-right-buttom), (x-right-top, y-right-top), (x-right-buttom, y-right-buttom)) coordinate of a bar area ''' return ((100 + (n % 6) * 380, 430 + (n // 6) * 331), # left x-axis 100pt for margin blank (100 + (n % 6) * 380, 430 + (n // 6) * 331 + 252), # top y-axis 430pt for title (100 + (n % 6) * 380 + 380, 430 + (n // 6) * 331), # 252 is 1.5em for chord 1em * 3 for melody 56pt per em (100 + (n % 6) * 380 + 380, 430 + (n // 6) * 331 + 252)) # ctx = cairo.Context(cairo.PDFSurface("haha.pdf", 2480.0, 3508.0)) # ctx.set_font_size(30) # ctx.select_font_face("FreeSerif", cairo.FONT_SLANT_NORMAL, # cairo.FONT_WEIGHT_NORMAL)
def main(): ps = cairo.PDFSurface("pdffile.pdf", 504, 648) cr = cairo.Context(ps) cr.set_source_rgb(0, 0, 0) cr.select_font_face("FreeSerif", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) cr.set_font_size(40) cr.move_to(10, 50) cr.show_text(chr(119046) +'1 2 3 4 5' + chr(119047) ) cr.set_line_width(11) cr.move_to(100, 100) cr.line_to(20, 300) cr.show_page()
def create_cairo_surface(self, fobj, width_in_points, height_in_points): return cairo.PDFSurface(fobj, width_in_points, height_in_points) #------------------------------------------------------------------------ # # PsDoc class # #------------------------------------------------------------------------
def __save(self, target_path, pages, progress_cb=dummy_export_progress_cb): # XXX(Jflesch): This is a problem. It will fails if someone tries # to export to a non-local directory. We should use # cairo_pdf_surface_create_for_stream() target_path = self.doc.fs.unsafe(target_path) pdf_surface = cairo.PDFSurface(target_path, self.__page_format[0], self.__page_format[1]) pdf_context = cairo.Context(pdf_surface) pages = [self.doc.pages[x] for x in range(pages[0], pages[1])] for page_idx, page in enumerate(pages): progress_cb(page_idx, len(pages)) img = page.img if (img.size[0] < img.size[1]): (x, y) = (min(self.__page_format[0], self.__page_format[1]), max(self.__page_format[0], self.__page_format[1])) else: (x, y) = (max(self.__page_format[0], self.__page_format[1]), min(self.__page_format[0], self.__page_format[1])) pdf_surface.set_size(x, y) logger.info("Adding text to PDF page {} ...".format(page)) self.__paint_txt(pdf_surface, (x, y), pdf_context, page) logger.info("Adding image to PDF page {} ...".format(page)) self.__paint_img(pdf_surface, (x, y), pdf_context, page) pdf_context.show_page() logger.info("Page {} ready".format(page)) progress_cb(len(pages), len(pages)) return self.doc.fs.safe(target_path)
def _take_screenshot(self, dummy_button): #print "Cheese!" file_name = self._get_export_file_name() if file_name is None: return # figure out the correct bounding box for what is visible on screen x1 = self._scrolled_window.get_hadjustment().value y1 = self._scrolled_window.get_vadjustment().value x2 = x1 + self._scrolled_window.get_hadjustment().page_size y2 = y1 + self._scrolled_window.get_vadjustment().page_size bounds = goocanvas.Bounds() bounds.x1, bounds.y1 = self.canvas.convert_from_pixels(x1, y1) bounds.x2, bounds.y2 = self.canvas.convert_from_pixels(x2, y2) dest_width = bounds.x2 - bounds.x1 dest_height = bounds.y2 - bounds.y1 #print bounds.x1, bounds.y1, " -> ", bounds.x2, bounds.y2 dummy, extension = os.path.splitext(file_name) extension = extension.lower() if extension == '.eps': surface = cairo.PSSurface(file_name, dest_width, dest_height) elif extension == '.pdf': surface = cairo.PDFSurface(file_name, dest_width, dest_height) elif extension == '.svg': surface = cairo.SVGSurface(file_name, dest_width, dest_height) else: dialog = gtk.MessageDialog(parent = self.canvas.get_toplevel(), flags = gtk.DIALOG_DESTROY_WITH_PARENT, type = gtk.MESSAGE_ERROR, buttons = gtk.BUTTONS_OK, message_format = "Unknown extension '%s' (valid extensions are '.eps', '.svg', and '.pdf')" % (extension,)) dialog.run() dialog.destroy() return # draw the canvas to a printing context cr = cairo.Context(surface) cr.translate(-bounds.x1, -bounds.y1) self.canvas.render(cr, bounds, self.zoom.value) cr.show_page() surface.finish()
def main(): parser = argparse.ArgumentParser(description='Make a hexaflexagon with a picture printed on each of the six faces.') parser.add_argument('pics', type=str, nargs='+', help='Filenames to pictures (only png).') parser.add_argument('--output', type=str, help='Output filename (pdf).', default="out.pdf") parser.add_argument('--paper', type=str, help='Paper size', default="A4"); args = parser.parse_args() # The units for pdf size is a point=1/72inch if (args.paper.upper() == 'A4'): WIDTH, HEIGHT = 595, 842 elif (args.paper.upper() == 'LETTER'): WIDTH, HEIGHT = 612, 792 elif (args.paper.upper() == 'LEGAL'): WIDTH, HEIGHT = 612, 1008 elif (args.paper.upper() == 'TABLOID'): WIDTH, HEIGHT = 792, 1224 else: print("Paper type not understood: '"+args.paper+"'") sys.exit(1) surface = cairo.PDFSurface(args.output, WIDTH, HEIGHT) ctx = cairo.Context(surface) border = .75 * 72.0 / 2.54 # border = 3/4cm a = (HEIGHT - 2*border)/10 # eq. triangle side, we need 10 down the spine b = a/2 # half-side of eq. triangles h = sqrt(3)/2 * a # height of eq.triangles n = int(WIDTH / (h*2)); # this is how many will fit in the paper's width for i in range(n): ctx.move_to( border + i*(2*h), border ) commonfaces = list(zip(range(0,3), ["scissor"] * 3)) hiddenfaces = list(zip(range(3,6), ["scissor", "scissor", "stone"])) transparentfaces = list(zip(range(0,3), ["paper"] * 3)) for pic_fn, (face, ori) in zip(args.pics, commonfaces + hiddenfaces + transparentfaces): img = cairo.ImageSurface.create_from_png(pic_fn) drawPicture(ctx, a, b, h, face, ori, img) drawOutline(ctx, a, b, h) surface.show_page()
def render(self, canvas, transparent=False): x0,y0,x1,y1 = canvas.bbox('all') self.markers = canvas.markers W = int((x1 - x0 + 2*self.padding) * self.scale) H = int((y1 - y0 + 2*self.padding) * self.scale) ext = os.path.splitext(self.fname)[1].lower() if ext == '.svg': surf = cairo.SVGSurface(self.fname, W, H) elif ext == '.pdf': surf = cairo.PDFSurface(self.fname, W, H) elif ext in ('.ps', '.eps'): surf = cairo.PSSurface(self.fname, W, H) if ext == '.eps': surf.set_eps(True) else: # Bitmap surf = cairo.ImageSurface(cairo.FORMAT_ARGB32, W, H) self.ctx = cairo.Context(surf) ctx = self.ctx if not transparent: # Fill background ctx.rectangle(0,0, W,H) ctx.set_source_rgba(1.0,1.0,1.0) ctx.fill() ctx.scale(self.scale, self.scale) ctx.translate(-x0 + self.padding, -y0 + self.padding) if self.draw_bbox: last = len(canvas.shapes) for s in canvas.shapes[:last]: bbox = s.bbox r = canvas.create_rectangle(*bbox, line_color=(255,0,0, 127), fill=(0,255,0,90)) for s in canvas.shapes: self.draw_shape(s) if ext in ('.svg', '.pdf', '.ps', '.eps'): surf.show_page() else: surf.write_to_png(self.fname)