def createScaledFont(family, size, slant=cairo.FONT_SLANT_NORMAL, weight=cairo.FONT_WEIGHT_NORMAL): face = cairo.ToyFontFace(family, slant, weight) return cairo.ScaledFont(face, cairo.Matrix(xx=size, yy=size), cairo.Matrix(), DEFAULT_FONT_OPTIONS)
def test_scaled_font(self): scaled_font = cairo.ScaledFont(self.context.get_font_face(), cairo.Matrix(), cairo.Matrix(), self.context.get_font_options()) result = self.pass_object_through_signal(scaled_font, self.tester.sig_scaled_font) self.assertTrue(isinstance(result, cairo.ScaledFont))
def _build_csf(self): "Builds and caches a Cairo Scaled Font." fontFace = cr.ToyFontFace(self.name) identityMatrix = cr.Matrix() fontOptions = cr.FontOptions() # get defaults scaling = self.size * self._gschemScalingConstant scalingMatrix = cr.Matrix(xx = scaling, yy = scaling) self._csf = cr.ScaledFont(fontFace, scalingMatrix, identityMatrix, fontOptions)
def createScaledFont(family, size, slant=cairo.FONT_SLANT_NORMAL, weight=cairo.FONT_WEIGHT_NORMAL): """ Simple helper function to create a cairo ScaledFont. """ face = cairo.ToyFontFace(family, slant, weight) DEFAULT_FONT_OPTIONS = cairo.FontOptions() DEFAULT_FONT_OPTIONS.set_antialias(cairo.ANTIALIAS_SUBPIXEL) return cairo.ScaledFont(face, cairo.Matrix(xx=size, yy=size), cairo.Matrix(), DEFAULT_FONT_OPTIONS)
def _cast_inline(self, LINE, runinfo, F, FSTYLE): C = cast_mono_line(LINE, self.char, runinfo, F + self['mathvariant']) C['x'] = 0 C['y'] = FSTYLE['shift'] self._cad = C['advance'] ink = cairo.ScaledFont( C['fstyle']['font'], cairo.Matrix(yy=C['fstyle']['fontsize'], xx=C['fstyle']['fontsize']), cairo.Matrix(), cairo.FontOptions()) self._rise = -ink.text_extents(self.char[-1])[1] self._icorrection = self._rise * self[ 'correct'] * 0.17632698070846498 # tan(10°) charwidth = self._cad + self._icorrection return [ C ], charwidth, C['ascent'], C['descent'], None, (self._draw_annot, 0, 0)
def scaled_font(font_face, font_options): return cairo.ScaledFont(font_face, cairo.Matrix(), cairo.Matrix(), font_options)
def test_scaled_font(scaled_font): with pytest.raises(TypeError): cairo.ScaledFont() assert scaled_font == scaled_font assert scaled_font != object()
def draw_tree(tree, outname): # get the layout of the tree structure = [{} for _ in range(len(tree))] i = 0 for level in tree: j = 0 curr = structure[i] for key in level.keys(): curr.update({key: j}) j += 1 i += 1 typecolors = { "": (150, 150, 150), "Other": (150, 150, 150), "unknown": (150, 150, 150) } typecolors.update({"Image": (230, 230, 20)}) typecolors.update({"Stylesheet": (20, 230, 100)}) typecolors.update({"Script": (255, 20, 20)}) typecolors.update({"EventSource": (20, 230, 100)}) typecolors.update({"Font": (20, 230, 100)}) typecolors.update({"Media": (255, 20, 20)}) typecolors.update({"Fetch": (230, 230, 20)}) typecolors.update({"Document": (230, 230, 20)}) typecolors.update({"XHR": (230, 230, 20)}) # get the height and max width # of the tree h = len(structure) w = 0 for level in structure: if len(level) > w: w = len(level) # picture parameters rad = 200 bufx = 100 bufy = 300 thck = 10 tsize = 50 tbuf = 6 # create a new image while True: try: # get the image size size = (bufx + w * (bufx + (2 * rad)), bufy + h * (bufy + (2 * rad))) surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, size[0], size[1]) break except cairo.Error: rad = int(rad / 2) bufx = int(bufx / 2) bufy = int(bufy / 2) thck = int(thck / 2) tsize = int(tsize / 2) tbuf = int(tbuf / 2) ctx = cairo.Context(surface) ctx.set_source_rgb(1, 1, 1) ctx.rectangle(0, 0, size[0], size[1]) ctx.fill() fnt = cairo.ToyFontFace("Menlo", cairo.FontSlant.NORMAL, cairo.FontWeight.NORMAL) opt = cairo.FontOptions() fnt = cairo.ScaledFont( fnt, cairo.Matrix(size[0] / 60, 0, 0, size[0] / 60, 0, 0), cairo.Matrix(1, 0, 0, 1, 0, 0), opt) ctx.set_scaled_font(fnt) nnt = str(num_nodes(tree)) net = str(num_ad_nodes(tree, True)) nit = str(num_ad_nodes(tree, False)) inf = str(nnt + "," + net + "," + nit) ext = fnt.text_extents(inf) ctx.move_to(0, ext.height) ctx.set_source_rgb(50 / 255, 20 / 255, 255 / 255) ctx.show_text(nnt) ctx.set_source_rgb(0, 0, 0) ctx.show_text(",") ctx.set_source_rgb(255 / 255, 20 / 255, 50 / 255) ctx.show_text(net) ctx.set_source_rgb(0, 0, 0) ctx.show_text(",") ctx.set_source_rgb(255 / 255, 20 / 255, 200 / 255) ctx.show_text(nit) ctx.stroke() fnt = cairo.ToyFontFace("Menlo", cairo.FontSlant.NORMAL, cairo.FontWeight.NORMAL) opt = cairo.FontOptions() fnt = cairo.ScaledFont(fnt, cairo.Matrix(tsize, 0, 0, tsize, 0, 0), cairo.Matrix(1, 0, 0, 1, 0, 0), opt) ctx.set_scaled_font(fnt) ctx.set_line_cap(cairo.LineCap.BUTT) ctx.set_line_join(cairo.LineJoin.ROUND) ctx.set_line_width(thck) # draw! plev = -1 c0xp = 0 c0yp = 0 slen = len(structure) # iterate through the levels of the tree for level in range(0, len(structure)): # get the width of the current level llen = len(structure[level]) # find the x center coordinate ctr = size[0] / 2 rng = llen * (2 * rad) + (llen - 1) * bufx c0x = ctr - (rng / 2) + rad # find the y center coordinate ctr = size[1] / 2 rng = slen * (2 * rad) + (slen - 1) * bufy c0y = ctr - (rng / 2) + rad cy = c0y + level * (2 * rad + bufy) # calculate number of unique parents puni = {} index = 0 for child in tree[level]: for parent in tree[level][child]["parents"]: if parent not in puni: puni.update({parent: index}) index += 1 for item in structure[level].keys(): if tree[level][item]["ad"] == "yes": color = (255 / 255, 20 / 255, 50 / 255) elif tree[level][item]["ad"] == "no": color = (50 / 255, 20 / 255, 255 / 255) else: color = (255 / 255, 20 / 255, 200 / 255) # since we are iterating "horizontally", we # need only update the x position cx = c0x + structure[level][item] * (2 * rad + bufx) # draw centered text try: sqtxt = str(tree[level][item]["squish"]) except KeyError: sqtxt = "" # draw the circle + text ctx.set_source_rgb(color[0], color[1], color[2]) ctx.arc(cx, cy, rad, 0, 2 * math.pi) ctx.fill() if "types" in tree[level][item]: ntypes = len(tree[level][item]["types"]) for idt, t in enumerate(tree[level][item]["types"]): try: color = typecolors[t] except KeyError: print(t) color = typecolors[""] ctx.set_source_rgb(color[0] / 255, color[1] / 255, color[2] / 255) ctx.arc(cx, cy, rad, idt * 2 * math.pi / ntypes, (idt + 1) * 2 * math.pi / ntypes) ctx.stroke() elif "type" in tree[level][item]: try: color = typecolors[tree[level][item]["type"]] except: print(tree[level][item]["type"]) color = typecolors[""] ctx.set_source_rgb(color[0] / 255, color[1] / 255, color[2] / 255) ctx.arc(cx, cy, rad, 0, 2 * math.pi) ctx.stroke() ctx.set_source_rgba(0, 0, 0, 1.0) ext = fnt.text_extents(get_url(item)) ctx.move_to(cx - ext.width / 2, cy - tbuf) ctx.show_text(get_url(item)) ext = fnt.text_extents(sqtxt) ctx.move_to(cx - ext.width / 2, cy + ext.height + tbuf) ctx.show_text(sqtxt) ctx.stroke() # if we aren't at the top level then # draw a line connecting it to its parent(s) if plev != -1: thisp = 0 thisptot = len(tree[level][item]["parents"]) # iterate through parents for parent in tree[level][item]["parents"].keys(): # get the index of the parent pindex = structure[level - 1][parent] cpx = c0xp + (pindex) * (2 * rad + bufx) cpy = c0yp # get the width of the parent layer plsize = len(puni) # calculate on offset so that lines dont intersect offy = int((puni[parent] + 2) * bufy / (plsize + 3)) ofrx = int((thck * (thisptot - 1)) + (2 * thck * (thisptot - 1))) of0x = int(-ofrx / 2) offx = int(of0x + thisp * ofrx / thisptot) # draw 3 lines to connect the parent and child ctx.move_to(cx + offx, cy - rad) ctx.line_to(cx + offx, cy - rad - offy) ctx.line_to(cpx, cy - rad - offy) ctx.line_to(cpx, cpy + rad) ctx.stroke() thisp += 1 # remember the parent level and center # of the circle at the beginning of the parent level plev = level c0xp = c0x c0yp = cy # save the image surface.write_to_png(outname)
def add_text(self, text, fontname, origin, size, color, positioning=FontPositioning.CENTER): ''' Add text with font of name fontname. If centered=True, then the origin denotes the center of the text to be drawn. Otherwise it respresents the top left corner. If target=FontSizing.WIDTH, then size represents the width in pixels for the target text. Similarly for height. Otherwise if target=FontSizing.SCALE the size represents the font size factor passed to cairo. ''' # get font try: font = self.fonts[fontname] except KeyError: font = cairo.ToyFontFace(fontname) self.fonts[fontname] = font # create options opt = cairo.FontOptions() # get size for width or height testfont = cairo.ScaledFont(font, cairo.Matrix(1, 0, 0, 1, 0, 0), cairo.Matrix(1, 0, 0, 1, 0, 0), opt) extents = testfont.text_extents(text) try: size = size / extents.height except ZeroDivisionError: return # create font for drawing usefont = cairo.ScaledFont(font, cairo.Matrix(size,0,0,size,0,0), cairo.Matrix(1,0,0,1,0,0), opt) if positioning == FontPositioning.CENTER: extents = usefont.text_extents(text) origin[0] += extents.height/2 origin[1] -= extents.width/2 origin[1] -= extents.x_bearing elif positioning == FontPositioning.TOP_LEFT: extents = usefont.text_extents(text) origin[0] += extents.height elif positioning == FontPositioning.TOP_RIGHT: extents = usefont.text_extents(text) origin[0] += extents.height origin[1] -= extents.width origin[1] -= extents.x_bearing elif positioning == FontPositioning.TOP_CENTER: extents = usefont.text_extents(text) origin[0] += extents.height origin[1] -= extents.width/2 origin[1] -= extents.x_bearing elif positioning == FontPositioning.BOTTOM_RIGHT: extents = usefont.text_extents(text) origin[1] -= extents.width origin[1] -= extents.x_bearing elif positioning == FontPositioning.BOTTOM_CENTER: extents = usefont.text_extents(text) origin[1] -= extents.width/2 origin[1] -= extents.x_bearing elif positioning == FontPositioning.RIGHT_CENTER: extents = usefont.text_extents(text) origin[0] += extents.height/2 origin[1] -= extents.width origin[1] -= extents.x_bearing elif positioning == FontPositioning.LEFT_CENTER: extents = usefont.text_extents(text) origin[0] += extents.height/2 self.context.set_source_rgb(color[0]/255, color[1]/255, color[2]/255) self.context.set_scaled_font(usefont) self.context.move_to(origin[1], origin[0]) self.context.show_text(str(text)) self.context.fill()
def __new__(cls, *args, **kwargs): import cairo return cairo.ScaledFont(*args, **kwargs)