def draw_line(self, canvas, structure, x_values, y_values, style): ## Cache colors if not style.cached_renderer: style.cache_renderer(self) elif style.cached_renderer != self: style.cache_renderer(self) ## Create a path path = skia.Path() ## Load points into path pointer = 0 for p_count in structure: path.moveTo(x_values[pointer], y_values[pointer]) for index in range(pointer, pointer + p_count): path.lineTo(x_values[index], y_values[index]) pointer += p_count ## Create outline paint paint = skia.Paint(style._outline_color_cache) paint.setStyle(skia.Paint.kStroke_Style) paint.setStrokeWidth(style.weight + style.outline_weight) paint.setAntiAlias(True) ## Draw outline path canvas.drawPath(path, paint) ## Create line paint paint = skia.Paint(style._color_cache) paint.setStyle(skia.Paint.kStroke_Style) paint.setStrokeWidth(style.weight) paint.setAntiAlias(True) ## Draw line path canvas.drawPath(path, paint)
def __init__(self, dat, h): super().__init__() self.dat = dat self.path = skia.Path() tp = TransformPen(self, (1, 0, 0, -1, 0, h)) dat.replay(tp)
def test_Path1DPathEffect_Make(): path = skia.Path() path.addCircle(10, 10, 5) assert isinstance( skia.Path1DPathEffect.Make(path, 1., 0., skia.Path1DPathEffect.kTranslate_Style), skia.PathEffect)
def draw_point(self, canvas, structure, x_values, y_values, style): ## Cache colors if not style.cached_renderer: style.cache_renderer(self) elif style.cached_renderer != self: style.cache_renderer(self) ## Create a path path = skia.Path() ## Add points to path pointer = 0 for p_count in structure: for index in range(pointer, pointer + p_count): path.addCircle(x_values[index], y_values[index], style.weight) pointer += p_count ## Draw background paint = skia.Paint(AntiAlias=True) paint.setColor(style._color_cache) canvas.drawPath(path, paint) ## Draw outline paint.setStyle(skia.Paint.kStroke_Style) paint.setColor(style._outline_color_cache) paint.setStrokeWidth(style.outline_weight) canvas.drawPath(path, paint)
def test_PathEffect_asPoints(patheffect): results = skia.PathEffect.PointData() path = skia.Path() path.addCircle(10, 10, 5) rec = skia.StrokeRec(skia.StrokeRec.kHairline_InitStyle) matrix = skia.Matrix() assert isinstance(patheffect.asPoints(results, path, rec, matrix, None), bool)
def test_Path_Iter_conicWeight(): path = skia.Path() path.addOval((0, 0, 100, 100)) itr = iter(path) verb, points = itr.next() while verb != skia.Path.kDone_Verb: assert isinstance(verb, skia.Path.Verb) assert isinstance(points, list) if verb == skia.Path.kConic_Verb: assert itr.conicWeight() == 0.7071067690849304 verb, points = itr.next()
def draw(canvas): from math import cos, sin scale = 256. R = 0.45 * scale TAU = 6.2831853 path = skia.Path() path.moveTo(R, 0.) for i in range(7): theta = 3 * i * TAU / 7 path.lineTo(R * cos(theta), R * sin(theta)) path.close() paint = skia.Paint() paint.setAntiAlias(True) canvas.clear(0xFFFFFFFF) canvas.translate(0.5 * scale, 0.5 * scale) canvas.drawPath(path, paint)
def test_Region_getBoundaryPath(region): path = skia.Path() assert isinstance(region.getBoundaryPath(path), bool)
def path(): path = skia.Path() path.addCircle(25, 25, 10) path.addRect((50, 60, 70, 70)) return path
def test_Path_Iter_setPath(itr): itr.setPath(skia.Path(), True)
def test_Path_swap(path): path.swap(skia.Path())
@pytest.fixture def rawiter(path): return skia.Path.RawIter(path) @pytest.mark.parametrize('args', [ tuple(), ]) def test_Path_init(args): assert isinstance(skia.Path(*args), skia.Path) @pytest.mark.parametrize('args', [ tuple(), (skia.Path(), True), ]) def test_Path_Iter_init(args): assert isinstance(skia.Path.Iter(*args), skia.Path.Iter) def test_Path_Iter_setPath(itr): itr.setPath(skia.Path(), True) def test_Path_Iter_next(itr): result = itr.next() assert isinstance(result, tuple) assert isinstance(result[0], skia.Path.Verb) assert isinstance(result[1], list)
def test_Paint_setStrokeCap(paint): paint.setStrokeCap(skia.Paint.kButt_Cap) def test_Paint_getStrokeJoin(paint): assert isinstance(paint.getStrokeJoin(), skia.Paint.Join) def test_Paint_setStrokeJoin(paint): paint.setStrokeJoin(skia.Paint.kMiter_Join) @pytest.mark.parametrize('args', [ ( skia.Path(), skia.Path(), ), (skia.Path(), skia.Path(), skia.Rect(100, 100), 1), ]) def test_Paint_getFillPath(paint, args): assert isinstance(paint.getFillPath(*args), bool) def test_Paint_getShader(paint): assert isinstance(paint.getShader(), (skia.Shader, type(None))) def test_Paint_refShader(paint): assert isinstance(paint.refShader(), (skia.Shader, type(None)))
def test_Font_getPath(font, glyphs): path = skia.Path() assert isinstance(font.getPath(glyphs[0], path), bool)
def test_StrokeRec_applyToPath(strokerec): assert isinstance(strokerec.applyToPath(skia.Path(), skia.Path()), bool)
def test_Canvas_drawPath(canvas): canvas.drawPath(skia.Path(), skia.Paint())
def polygons(self): print(f"Saving generated polygons background N=[", end="", flush=True) BASE_GRADIENT_LINEAR = True MUTATION = True LOW_POLY = True RANDOM = True GREYSCALE = False for i in range(self.n_images): if BASE_GRADIENT_LINEAR: if RANDOM: color1 = skia.Color4f(random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1), 1) color2 = skia.Color4f(random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1), 1) if GREYSCALE: gradient1, gradient2 = random.uniform(0, 1), random.uniform( 0, 1) color1 = skia.Color4f(gradient1, gradient1, gradient1, 1) color2 = skia.Color4f(gradient2, gradient2, gradient2, 1) paint = skia.Paint(Shader=skia.GradientShader.MakeLinear( points=[(random.randint(-self.width / 2, 0), random.randint(-self.height / 2, 0)), (self.width + random.randint(0, self.width / 2), self.height + random.randint(0, self.height / 2))], colors=[color1, color2])) self.canvas.drawPaint(paint) colors = self.skia.canvas_array() if LOW_POLY: break_x = 20 break_y = 15 rectangle_widths = np.linspace(-self.width / 4, 1.25 * self.width, num=break_x) rectangle_heights = np.linspace(-self.height / 4, 1.25 * self.height, num=break_y) rectangle_width = self.width / break_x rectangle_height = self.height / break_y xx, yy = np.meshgrid(rectangle_widths, rectangle_heights) pairs = list(np.dstack([xx, yy]).reshape(-1, 2)) if MUTATION: for index in range(len(pairs)): pairs[index][0] = pairs[index][0] + random.uniform( 0, rectangle_width) pairs[index][1] = pairs[index][1] + random.uniform( 0, rectangle_height) # print(pairs) rectangle_points = [] for index, point in enumerate(list(pairs)): samerow = not ((index + 1) % break_x == 0) # print(pairs[index], index, samerow) try: # If they are on the same height if samerow: rectangle_points.append([ list(pairs[index]), list(pairs[index + break_x]), list(pairs[index + break_x + 1]), list(pairs[index + 1]), ]) except IndexError: pass for rectangle in rectangle_points: average_x = int(sum([val[0] for val in rectangle]) / 4) average_y = int(sum([val[1] for val in rectangle]) / 4) # print(rectangle, average_x, average_y) rectangle_color = colors[min(max(average_y, 0), self.height - 1)][min( max(average_x, 0), self.width - 1)] rectangle_color_fill = [x / 255 for x in rectangle_color] rectangle_color_border = [ x / 255 - 0.05 for x in rectangle_color ] # print(rectangle_color) # Make a skia color with the colors list as argument color = skia.Color4f(*rectangle_color_fill) # Make the skia Paint and paint = skia.Paint( AntiAlias=True, Color=color, Style=skia.Paint.kFill_Style, StrokeWidth=2, ) color = skia.Color4f(*rectangle_color_border) border = skia.Paint( AntiAlias=True, Color=color, Style=skia.Paint.kStroke_Style, StrokeWidth=1, # ImageFilter=skia.ImageFilters.DropShadow(3, 3, 5, 5, color) ) path = skia.Path() path.moveTo(*rectangle[0]) rectangle.append(rectangle[0]) for point in rectangle: path.lineTo(*point) self.canvas.drawPath(path, paint) self.canvas.drawPath(path, border) # Save img = self.skia.canvas_array() # Pretty print if not i == self.n_images - 1: print(f"{i}, ", end="", flush=True) else: print(f"{i}]") img = Image.fromarray(img).convert('RGB') img.save(self.output_dir + f"/img{i}.jpg", quality=100)
def path(): return skia.Path()
def draw_poligon(points: list[tuple[float, float]], color, *, fill=True, border_thickness=8): if fill: border_thickness = 0 p = skia.Path() p.addPoly([skia.Point(*p) for p in points], close=True) _canvas.drawPath(p, Color._paint(color, fill, border_thickness))
def __path_from_tokens(self, tokens, transform): #=============================================== moved = False first_point = None current_point = None closed = False path = skia.Path() pos = 0 while pos < len(tokens): if isinstance(tokens[pos], str) and tokens[pos].isalpha(): cmd = tokens[pos] pos += 1 # Else repeat previous command with new coordinates # with `moveTo` becoming `lineTo` elif cmd == 'M': cmd = 'L' elif cmd == 'm': cmd = 'l' if cmd not in ['s', 'S']: second_cubic_control = None if cmd not in ['t', 'T']: second_quad_control = None if cmd in ['a', 'A']: params = [float(x) for x in tokens[pos:pos + 7]] pos += 7 pt = params[5:7] if cmd == 'a': pt[0] += current_point[0] pt[1] += current_point[1] phi = radians(params[2]) if moved: path.moveTo(*transform.transform_point(current_point)) moved = False (rx, ry) = transform.scale_length(params[0:2]) path.arcTo( rx, ry, degrees(transform.rotate_angle(phi)), skia.Path.ArcSize.kSmall_ArcSize if params[3] == 0 else skia.Path.ArcSize.kLarge_ArcSize, skia.PathDirection.kCCW if params[4] == 0 else skia.PathDirection.kCW, *transform.transform_point(pt)) current_point = pt elif cmd in ['c', 'C', 's', 'S']: if moved: path.moveTo(*transform.transform_point(current_point)) moved = False if cmd in ['c', 'C']: n_params = 6 coords = [] else: n_params = 4 if second_cubic_control is None: coords = list(transform.transform_point(current_point)) else: coords = list( transform.transform_point( reflect_point(second_cubic_control, current_point))) params = [float(x) for x in tokens[pos:pos + n_params]] pos += n_params for n in range(0, n_params, 2): pt = params[n:n + 2] if cmd.islower(): pt[0] += current_point[0] pt[1] += current_point[1] if n == (n_params - 4): second_cubic_control = pt coords.extend(transform.transform_point(pt)) path.cubicTo(*coords) current_point = pt elif cmd in ['l', 'L', 'h', 'H', 'v', 'V']: if cmd in ['l', 'L']: params = [float(x) for x in tokens[pos:pos + 2]] pos += 2 pt = params[0:2] if cmd == 'l': pt[0] += current_point[0] pt[1] += current_point[1] else: param = float(tokens[pos]) pos += 1 if cmd == 'h': param += current_point[0] elif cmd == 'v': param += current_point[1] if cmd in ['h', 'H']: pt = [param, current_point[1]] else: pt = [current_point[0], param] if moved: path.moveTo(*transform.transform_point(current_point)) moved = False path.lineTo(*transform.transform_point(pt)) current_point = pt elif cmd in ['m', 'M']: params = [float(x) for x in tokens[pos:pos + 2]] pos += 2 pt = params[0:2] if first_point is None: # First `m` in a path is treated as `M` first_point = pt else: if cmd == 'm': pt[0] += current_point[0] pt[1] += current_point[1] current_point = pt moved = True elif cmd in ['q', 'Q', 't', 'T']: if moved: path.moveTo(*transform.transform_point(current_point)) moved = False if cmd in ['t', 'T']: n_params = 4 coords = [] else: n_params = 2 if second_quad_control is None: coords = list(transform.transform_point(current_point)) else: coords = list( transform.transform_point( reflect_point(second_quad_control, current_point))) params = [float(x) for x in tokens[pos:pos + n_params]] pos += n_params for n in range(0, n_params, 2): pt = params[n:n + 2] if cmd.islower(): pt[0] += current_point[0] pt[1] += current_point[1] if n == (n_params - 4): second_quad_control = pt coords.extend(transform.transform_point(pt)) path.quadTo(*coords) current_point = pt elif cmd in ['z', 'Z']: if first_point is not None and current_point != first_point: pass #path.close() closed = True first_point = None else: print('Unknown path command: {}'.format(cmd)) return path
def test_Path_interpolate(path): out = skia.Path() assert isinstance(path.interpolate(path, .5, out), bool)
def __init__(self, path=None, glyphSet=None): super().__init__(glyphSet) if path is None: path = skia.Path() self.path = path
def test_Path_init(args): assert isinstance(skia.Path(*args), skia.Path)
def reverse(self): path = skia.Path() path.reverseAddPath(self.path) self.path = path
def test_Path_reverseAddPath(path): assert isinstance(path.reverseAddPath(skia.Path()), skia.Path)
def copy(self): path = skia.Path(self.path) return BezierPath(path=path)
def test_Path_RawIter_setPath(rawiter): rawiter.setPath(skia.Path())
def build(self, config, effects): # Get "needed" variables total_steps = self.mmv.context.total_steps completion = self.functions.proportion( total_steps, 1, self.mmv.core.this_step) # Completion from 0-1 means resolution_ratio_multiplier = self.mmv.context.resolution_ratio_multiplier # We push the bar downwards according to the avg amplitude for a nice shaky-blur effect offset_by_amplitude = self.mmv.core.modulators[ "average_value"] * self.config[ "shake_scalar"] * resolution_ratio_multiplier if self.config["position"] == "top": offset_by_amplitude *= (-1) # White full opacity color colors = [1, 1, 1, 1] # Make a skia color with the colors list as argument color = skia.Color4f(*colors) # Make the skia Paint and paint = skia.Paint( AntiAlias=True, Color=color, Style=skia.Paint.kStroke_Style, StrokeWidth=10 * resolution_ratio_multiplier, # + (magnitude/4), # ImageFilter=skia.ImageFilters.DropShadow(3, 3, 5, 5, skia.ColorWHITE), ) # The direction we're walking centered at origin, $\vec{AB} = A - B$ path_vector = np.array( [self.end_x - self.start_x, self.end_y - self.start_y]) # Proportion we already walked path_vector = path_vector * completion # Draw the main line starting at the start coordinates, push down by offset_by_amplitude path = skia.Path() path.moveTo(self.start_x, self.start_y + offset_by_amplitude) path.lineTo(self.start_x + path_vector[0], self.start_y + path_vector[1] + offset_by_amplitude) self.mmv.skia.canvas.drawPath(path, paint) # Borders around image if True: # Distance away from s distance = 9 * resolution_ratio_multiplier colors = [1, 1, 1, 0.7] # Make a skia color with the colors list as argument color = skia.Color4f(*colors) # Make the skia Paint and paint = skia.Paint( AntiAlias=True, Color=color, Style=skia.Paint.kStroke_Style, StrokeWidth=2, ) # Rectangle border border = skia.Rect(self.start_x - distance, self.start_y - distance + offset_by_amplitude, self.end_x + distance, self.end_y + distance + offset_by_amplitude) # Draw the border self.mmv.skia.canvas.drawRect(border, paint)
def test_Region_setPath(region, other): path = skia.Path() path.addRect(skia.Rect(50, 50)) assert isinstance(region.setPath(path, other), bool)
def test_Canvas_androidFramework_setDeviceClipRestriction(canvas): canvas.androidFramework_setDeviceClipRestriction(skia.IRect(320, 240)) @pytest.mark.parametrize('args', [ (skia.RRect.MakeRect(skia.Rect(320, 240)), skia.ClipOp.kIntersect, True), (skia.RRect.MakeRect(skia.Rect(320, 240)), True), (skia.RRect.MakeRect(skia.Rect(320, 240)), skia.ClipOp.kIntersect), (skia.RRect.MakeRect(skia.Rect(320, 240)), ), ]) def test_Canvas_clipRRect(canvas, args): canvas.clipRRect(*args) @pytest.mark.parametrize('args', [ (skia.Path(), skia.ClipOp.kIntersect, True), (skia.Path(), skia.ClipOp.kIntersect), (skia.Path(), True), (skia.Path(), ), ]) def test_Canvas_clipPath(canvas, args): canvas.clipPath(*args) @pytest.mark.parametrize('args', [ (skia.Region(), ), ( skia.Region(), skia.ClipOp.kIntersect, ), ])