def test_type(): assert cairo.Rectangle assert issubclass(cairo.Rectangle, tuple) with pytest.raises(TypeError): cairo.Rectangle() r = cairo.Rectangle(0.0, 0.5, 0.25, 0.75) assert hash(r) == hash(cairo.Rectangle(0.0, 0.5, 0.25, 0.75)) assert isinstance(r, tuple) assert r == (0.0, 0.5, 0.25, 0.75) assert r == cairo.Rectangle(0.0, 0.5, 0.25, 0.75) assert r[1] == 0.5 assert r.width == 0.25 assert r.height == 0.75 assert r.x == 0.0 assert r.y == 0.5 with pytest.raises(AttributeError): assert r.z assert repr(r) == "cairo.Rectangle(x=0.0, y=0.5, width=0.25, height=0.75)" assert str(r) == repr(r) assert eval(repr(r)) == r assert cairo.Rectangle.x assert cairo.Rectangle.y assert cairo.Rectangle.width assert cairo.Rectangle.height
def surface(self, flap_w: float, flap_halfh: float) -> cairo.RecordingSurface: # Create cairo Surface to write to rect = cairo.Rectangle(0, 0, int(mm_to_pt(flap_w)), int(mm_to_pt(flap_halfh * 2))) sfc = cairo.RecordingSurface(cairo.Content.COLOR_ALPHA, rect) # Prepare ctx = cairo.Context(sfc) ctx.scale(mm_to_pt(1), mm_to_pt(1)) ctx.select_font_face(self.font) font_h = int(flap_halfh * 1.5) ctx.set_font_size(int(font_h * self.scale)) # Trial run to get the width of the drawn character ctx.move_to(0, int(flap_halfh + font_h / 2)) ctx.show_text(self.char) x, y, width, height = sfc.ink_extents() # Clear surface ctx.save() ctx.set_operator(cairo.Operator.CLEAR) ctx.paint() ctx.restore() # Draw for real ctx.move_to(int((flap_w - pt_to_mm(width)) / 2 - pt_to_mm(x)), int(flap_halfh + font_h / 2) + self.offset) ctx.show_text(self.char) return sfc
def _vertical_lines_pattern(color: Color): import cairo pat = cairo.RecordingSurface(cairo.CONTENT_COLOR_ALPHA, cairo.Rectangle(0, 0, 3, 100)) ctx = cairo.Context(pat) ctx.set_line_width(.4) ctx.move_to(.5, 0) ctx.line_to(.5, 100) ctx.set_source_rgba(*color) ctx.stroke() return pat
def north_west_lines_pattern(color: Color): import cairo pat = cairo.RecordingSurface(cairo.CONTENT_COLOR_ALPHA, cairo.Rectangle(0, 0, 3, 3)) ctx = cairo.Context(pat) ctx.set_line_width(.4) ctx.move_to(-.2, 1.7) ctx.line_to(1.7, -.2) ctx.move_to(1.3, 3.2) ctx.line_to(3.2, 1.3) ctx.set_source_rgba(*color) ctx.stroke() return pat
def main(chute, args): pattern, size = chute.get_pattern() surface = cairo.RecordingSurface( cairo.CONTENT_COLOR_ALPHA, cairo.Rectangle(0, 0, util.mm_to_pt(size[0]), util.mm_to_pt(size[1]))) ctx = cairo.Context(surface) ctx.push_group() ctx.set_source(pattern) ctx.paint() pattern2 = ctx.pop_group() if args.paper_size: if args.typ == "svg": print( "ERROR: svg does not support multiple pages. Don't specify a paper size if you want to export svg file" ) if args.paper_size not in PAPER_SIZES.keys(): print(args.paper_size) print("Known Paper Sizes:") print(PAPER_SIZES) return tiler = CairoTiler(pattern2, size, overlap=10, paper_size=PAPER_SIZES[args.paper_size]) tiler.tile(args.output) else: if args.typ == "svg": surface = cairo.SVGSurface(args.output, util.mm_to_pt(size[0]), util.mm_to_pt(size[1])) elif args.typ == "pdf": surface = cairo.PDFSurface(args.output, util.mm_to_pt(size[0]), util.mm_to_pt(size[1])) ctx = cairo.Context(surface) ctx.set_source(pattern2) ctx.paint()
from typing import List
def test_rectangle(x, y, width, height): r = cairo.Rectangle(x, y, width, height) assert r.x == x assert r.y == y assert r.width == width assert r.height == height
def test_recording_surface(): surface = cairo.RecordingSurface(cairo.CONTENT_COLOR, cairo.Rectangle(1, 1, 10, 10)) assert isinstance(surface.get_extents(), cairo.Rectangle)
def create_surface(zoom=0): surface = cairo.RecordingSurface( cairo.Content.COLOR_ALPHA, cairo.Rectangle(0, 0, Renderer.BASE_WIDTH, Renderer.BASE_HEIGHT)) return surface
def main(): parser = argparse.ArgumentParser( description='Add ToUnicode tables to PDF files.') parser.add_argument('--outdir', default='tmp/sfd', type=str, help='Output .sfd files to this directory') parser.add_argument('pdfs', type=str, nargs='+', help='PDF files to process') args = parser.parse_args() fontnum = 0 for pdf in args.pdfs: print("Adding ToUnicode tables to PDF file {}".format(pdf)) with open(pdf, 'rb') as fobj: pdfdata = fobj.read() doc = PdfReader(fdata=pdfdata) doc.read_all() fonts = [ o for o in doc.indirect_objects.values() if hasattr(o, 'Type') and o.Type == '/Font' ] fonts = { font.FontDescriptor.FontName[1:]: font for font in fonts if font.FontDescriptor is not None } embedded_fonts = fontforge.fontsInFile(pdf) for fontname in embedded_fonts: if fontname not in fonts: print( "WARNING: font {} not found in pdf file".format(fontname)) continue print("Adding ToUnicode table to font {}".format(fontname)) font = fontforge.open('{}({})'.format(pdf, fontname)) fonts[fontname].ToUnicode = PdfDict() fonts[fontname].ToUnicode.stream = generate_tounicode( font, fonts[fontname]) # Need to save the modified font because fontforge won't read # ToUnicode when it converts to woff later. font.fontname = 'pretex{:06d}'.format(fontnum) font.save( os.path.join( args.outdir, '[{}]{}.sfd'.format(os.path.basename(pdf)[:-4], fontname))) fontnum += 1 PdfWriter(pdf, trailer=doc).write() # Measure extents for displayed equations pdfpath = os.path.realpath(os.path.dirname(pdf)) doc = poppler.document_new_from_file( 'file://{}'.format(os.path.realpath(pdf)), None) boxsize = os.path.join(pdfpath, 'boxsize.txt') with open(boxsize) as fobj: lines = fobj.readlines() with open(boxsize, 'w') as fobj: pageno = 0 for line in lines: if not (line.startswith('inline:') or line.startswith('display:')): fobj.write(line) continue pageno += 1 if not line.startswith('display:'): fobj.write(line) continue page = doc.get_page(pageno - 1) width, height = page.get_size() surf = cairo.RecordingSurface( cairo.Content.COLOR_ALPHA, cairo.Rectangle(0, 0, width, height)) ctx = cairo.Context(surf) page.render_for_printing(ctx) x, y, w, h = surf.ink_extents() fobj.write(line.strip() + '{},{},{},{}\n'.format(x, y, w, h))
def get_pattern(self): pattern_lines = self._get_pattern_path() line_right = spg.LineString(pattern_lines["right"]) line_top = spg.LineString(pattern_lines["top"]) line_left = spg.LineString(pattern_lines["left"]) line_bottom = spg.LineString(pattern_lines["bottom"]) coords = list() coords.extend(line_right.coords) coords.extend(line_top.coords) coords.extend(line_left.coords) coords.extend(line_bottom.coords) polygon = spg.Polygon(coords) ui, li = polygon.exterior.xy ui = np.array(ui) li = np.array(li) if self.joint_style == MitreType.none: if self.seam_allowance[0] > 0: polygon = self.add_seamallowance(polygon, line_right, self.seam_allowance[0]) if self.seam_allowance[1] > 0: polygon = self.add_seamallowance(polygon, line_top, self.seam_allowance[1]) if self.seam_allowance[2] > 0: polygon = self.add_seamallowance(polygon, line_left, self.seam_allowance[2]) if self.seam_allowance[3] > 0: polygon = self.add_seamallowance(polygon, line_bottom, self.seam_allowance[3]) elif len(set(self.seam_allowance)) == 1: if self.joint_style == MitreType.miter: jt = JOIN_STYLE.mitre elif self.joint_style == MitreType.bevel: jt = JOIN_STYLE.bevel elif self.joint_style == MitreType.round: jt = JOIN_STYLE.round polygon = polygon.buffer(self.seam_allowance[0], join_style=jt, mitre_limit=2) elif self.joint_style == MitreType.bevel: polygon = self.add_seamallowance_bevel( [line_right, line_top, line_left, line_bottom], self.seam_allowance) elif self.joint_style == MitreType.round: print( "ERROR: joint style \"round\" not supported for non uniform seam allowance. Please use bevel or none" ) elif self.joint_style == MitreType.miter: print( "ERROR: joint style \"miter\" not supported for non uniform seam allowance. Please use bevel or none" ) u, l = polygon.exterior.xy u = np.array(u) l = np.array(l) margins = (10, 10, 10, 10) l = l * -1 li = li * -1 pattern_extend = (np.min(u), np.min(l), np.max(u), np.max(l)) pattern_width = pattern_extend[2] - pattern_extend[0] pattern_height = pattern_extend[3] - pattern_extend[1] document_width = pattern_width + margins[0] + margins[1] document_height = pattern_height + margins[2] + margins[3] surface = cairo.RecordingSurface( cairo.CONTENT_COLOR_ALPHA, cairo.Rectangle(0, 0, mm_to_pt(document_width), mm_to_pt(document_height))) ctx = cairo.Context(surface) ctx.save() ctx.set_line_join(cairo.LINE_JOIN_ROUND) ctx.set_line_cap(cairo.LINE_CAP_ROUND) ctx.push_group() ctx.scale(mm_to_pt(1), mm_to_pt(1)) ctx.save() if self.grid: draw_grid(ctx, (0, 0), 10, 1, document_height, document_width) #draw seam allowance ctx.translate( -pattern_extend[0] + (document_width / 2 - pattern_width / 2), -pattern_extend[1] + (document_height / 2 - pattern_height / 2)) ctx.move_to(u[0], l[0]) for x, y in zip(u, l): ctx.line_to(x, y) ctx.close_path() ctx.set_source_rgb(.0, .0, .0) ctx.set_line_width(0.3) ctx.stroke() #draw pattern ctx.move_to(ui[0], li[0]) for x, y in zip(ui, li): ctx.line_to(x, y) ctx.close_path() ctx.set_source_rgb(1, .0, .0) ctx.set_line_width(0.3) ctx.stroke() ctx.restore() pattern = ctx.pop_group() return (pattern, (document_width, document_height))