Beispiel #1
0
	def fill_radial_tr_gradient(self, obj, pdfpath, fill_trafo, gradient):
		if not fill_trafo:
			fill_trafo = [1.0, 0.0, 0.0, 1.0, 0.0, 0.0]
		stops = gradient[2]
		sp, ep = gradient[1]
		dx, dy = sp
		l = libgeom.distance(sp, ep)
		trafo = [1.0, 0.0, 0.0, 1.0, dx, dy]
		inv_trafo = libgeom.multiply_trafo(libgeom.invert_trafo(fill_trafo),
										libgeom.invert_trafo(trafo))
		cv_trafo = libgeom.multiply_trafo(trafo, fill_trafo)
		paths = libgeom.apply_trafo_to_paths(obj.paths, obj.trafo)
		paths = libgeom.apply_trafo_to_paths(paths, inv_trafo)
		bbox = libgeom.sum_bbox(libgeom.get_paths_bbox(paths),
							[0.0, 0.0, l, 0.0])
		bbox = libgeom.normalize_bbox(bbox)
		d = libgeom.distance(*libgeom.apply_trafo_to_points([[0.0, 0.0],
													[0.0, 1.0]], inv_trafo))

		circle_paths = libgeom.get_circle_paths(0.0, 0.0, sk2_const.ARC_CHORD)
		trafo = [2.0, 0.0, 0.0, 2.0, -1.0, -1.0]
		circle_paths = libgeom.apply_trafo_to_paths(circle_paths, trafo)

		inner_paths = []
		r = 0.0
		self.canvas.saveState()
		self.canvas.clipPath(pdfpath, 0, 0)
		self.canvas.transform(*cv_trafo)
		while r < l:
			point = r / l
			self.canvas.setFillColor(self.get_grcolor_at_point(stops, point))
			if r + d < l: coef = (r + d)
			else: coef = l
			trafo = [coef, 0.0, 0.0, coef, 0.0, 0.0]
			paths = libgeom.apply_trafo_to_paths(circle_paths, trafo)
			ring = self.make_pdfpath(inner_paths + paths)[0]
			inner_paths = paths
			self.canvas.drawPath(ring, stroke=0, fill=1)
			r += d

		self.canvas.setFillColor(self.get_grcolor_at_point(stops, 1.0))
		r = max(bbox[2] - bbox[0], bbox[3] - bbox[1])
		trafo = [2.0 * r, 0.0, 0.0, 2.0 * r, 0.0, 0.0]
		paths = libgeom.apply_trafo_to_paths(circle_paths, trafo)
		ring = self.make_pdfpath(inner_paths + paths)[0]
		self.canvas.drawPath(ring, stroke=0, fill=1)

		self.canvas.restoreState()
Beispiel #2
0
	def tr_arc(self, chunk, arc_type=sk2_const.ARC_ARC):
		ye, xe, ys, xs, bottom, right, top, left = get_data('<hhhhhhhh',
																chunk)
		left, top = apply_trafo_to_point([left, top], self.get_trafo())
		right, bottom = apply_trafo_to_point([right, bottom], self.get_trafo())
		xs, ys = apply_trafo_to_point([xs, ys], self.get_trafo())
		xe, ye = apply_trafo_to_point([xe, ye], self.get_trafo())

		if left != right and top != bottom:
			t = [(right - left) / 2, 0, 0, (bottom - top) / 2,
						(right + left) / 2, (top + bottom) / 2]
			t = libgeom.invert_trafo(t)
			xs, ys = apply_trafo_to_point([xs, ys], t)
			xe, ye = apply_trafo_to_point([xe, ye], t)
			end_angle = libgeom.get_point_angle([xs, ys], [0.0, 0.0])
			start_angle = libgeom.get_point_angle([xe, ye], [0.0, 0.0])
		else:
			start_angle = end_angle = 0.0

		cfg = self.layer.config
		sk2_style = self.get_style()
		if arc_type == sk2_const.ARC_ARC: sk2_style[0] = []
		rect = [left, top, right - left, bottom - top]
		ellipse = sk2_model.Circle(cfg, self.layer, rect, start_angle,
								end_angle, arc_type, sk2_style)
		self.layer.childs.append(ellipse)
Beispiel #3
0
	def get_path(self):
		ret = [[], [], self.closed]
		inv_trafo = libgeom.invert_trafo(self.trafo)
		ret[0] = libgeom.apply_trafo_to_point(self.start_point.point, inv_trafo)
		for item in self.points:
			ret[1].append(libgeom.apply_trafo_to_point(item.point, inv_trafo))
		return ret
Beispiel #4
0
	def fill_linear_tr_gradient(self, obj, pdfpath, fill_trafo, gradient):
		if not fill_trafo:
			fill_trafo = [1.0, 0.0, 0.0, 1.0, 0.0, 0.0]
		stops = gradient[2]
		sp, ep = gradient[1]
		dx, dy = sp
		l = libgeom.distance(sp, ep)
		angle = libgeom.get_point_angle(ep, sp)
		m21 = math.sin(angle)
		m11 = m22 = math.cos(angle)
		m12 = -m21
		trafo = [m11, m21, m12, m22, dx, dy]
		inv_trafo = libgeom.multiply_trafo(libgeom.invert_trafo(fill_trafo),
										libgeom.invert_trafo(trafo))
		cv_trafo = libgeom.multiply_trafo(trafo, fill_trafo)
		paths = libgeom.apply_trafo_to_paths(obj.paths, obj.trafo)
		paths = libgeom.apply_trafo_to_paths(paths, inv_trafo)
		bbox = libgeom.sum_bbox(libgeom.get_paths_bbox(paths),
							[0.0, 0.0, l, 0.0])
		bbox = libgeom.normalize_bbox(bbox)

		y = bbox[1]
		d = libgeom.distance(*libgeom.apply_trafo_to_points([[0.0, 0.0],
													[0.0, 1.0]], inv_trafo))
		height = bbox[3] - bbox[1]

		self.canvas.saveState()
		self.canvas.clipPath(pdfpath, 0, 0)
		self.canvas.transform(*cv_trafo)

		self.canvas.setFillColor(self.get_grcolor_at_point(stops, 0.0))
		self.canvas.rect(bbox[0], y, 0.0 - bbox[0], height, stroke=0, fill=1)

		x = 0.0
		while x < l:
			point = x / l
			self.canvas.setFillColor(self.get_grcolor_at_point(stops, point))
			if x + d < l: width = d
			else: width = l - x
			self.canvas.rect(x, y, width, height, stroke=0, fill=1)
			x += d

		self.canvas.setFillColor(self.get_grcolor_at_point(stops, 1.0))
		self.canvas.rect(l, y, bbox[2] - l, height, stroke=0, fill=1)

		self.canvas.restoreState()
Beispiel #5
0
	def _set_target_vector(self, temp=True):
		itrafo = libgeom.invert_trafo(self.target.fill_trafo)
		vector = [self.vector[0].point, self.vector[1].point]
		self.new_style[0][2][1] = libgeom.apply_trafo_to_points(vector, itrafo)
		if temp:
			self.api.set_temp_style(self.target, self.new_style)
		else:
			self.api.set_fill_style(deepcopy(self.new_style[0]))
		events.emit(events.APP_STATUS, self.msg)
Beispiel #6
0
 def apply_moving(self, event, start=False, final=False):
     point = event.get_point()
     wpoint = self.canvas.point_win_to_doc(point)
     invtrafo = libgeom.invert_trafo(self.target.trafo)
     x, y = libgeom.apply_trafo_to_point(wpoint, invtrafo)
     x -= 0.5
     y -= 0.5
     angle = 0.0
     if x > 0 and y > 0:
         angle = math.atan(y / x)
     elif x == 0 and y > 0:
         angle = math.pi / 2.0
     elif x < 0 < y:
         angle = math.atan(-x / y) + math.pi / 2.0
     elif x < 0 and y == 0:
         angle = math.pi
     elif x < 0 and y < 0:
         angle = math.atan(y / x) + math.pi
     elif x == 0 and y < 0:
         angle = 1.5 * math.pi
     elif x > 0 > y:
         angle = math.atan(x / -y) + 1.5 * math.pi
     elif x > 0 and y == 0:
         angle = 0.0
     elif x == 0 and y == 0:
         return
     if event.is_ctrl():
         fixed_angle = math.pi * config.ellipse_fixed_angle / 180.0
         angle //= fixed_angle
         angle *= fixed_angle
     else:
         contra_point = self.start_point
         if start:
             contra_point = self.end_point
         if contra_point.is_pressed(point):
             angle = contra_point.get_angle()
     circle_type = self.orig_type
     if event.is_alt() and not circle_type == sk2const.ARC_ARC:
         circle_type = sk2const.ARC_CHORD
         if libgeom.distance([x, y]) < 0.5:
             circle_type = sk2const.ARC_PIE_SLICE
     angle1 = self.orig_angle1
     angle2 = self.orig_angle2
     if start:
         angle1 = angle
     else:
         angle2 = angle
     if final:
         self.api.set_circle_properties_final(circle_type, angle1, angle2,
                                              self.orig_type,
                                              self.orig_angle1,
                                              self.orig_angle2, self.target)
     else:
         self.api.set_circle_properties(circle_type, angle1, angle2,
                                        self.target)
     self.update_points()
Beispiel #7
0
	def apply_moving(self, event, start=False, final=False):
		point = event.get_point()
		wpoint = self.canvas.point_win_to_doc(point)
		invtrafo = libgeom.invert_trafo(self.target.trafo)
		x, y = libgeom.apply_trafo_to_point(wpoint, invtrafo)
		x -= 0.5
		y -= 0.5
		angle = 0.0
		if x > 0 and y > 0:
			angle = math.atan(y / x)
		elif x == 0 and y > 0:
			angle = math.pi / 2.0
		elif x < 0 and y > 0:
			angle = math.atan(-x / y) + math.pi / 2.0
		elif x < 0 and y == 0:
			angle = math.pi
		elif x < 0 and y < 0:
			angle = math.atan(y / x) + math.pi
		elif x == 0 and y < 0:
			angle = 1.5 * math.pi
		elif x > 0 and y < 0:
			angle = math.atan(x / -y) + 1.5 * math.pi
		elif x > 0 and y == 0:
			angle = 0.0
		elif x == 0 and y == 0:
			return
		if event.is_ctrl():
			fixed_angle = math.pi * config.ellipse_fixed_angle / 180.0
			angle //= fixed_angle
			angle *= fixed_angle
		else:
			contra_point = self.start_point
			if start:contra_point = self.end_point
			if contra_point.is_pressed(point):
				angle = contra_point.get_angle()
		circle_type = self.orig_type
		if event.is_alt() and not circle_type == sk2_const.ARC_ARC:
			circle_type = sk2_const.ARC_CHORD
			if libgeom.distance([x, y]) < 0.5:
				circle_type = sk2_const.ARC_PIE_SLICE
		angle1 = self.orig_angle1
		angle2 = self.orig_angle2
		if start: angle1 = angle
		else: angle2 = angle
		if final:
			self.api.set_circle_properties_final(circle_type, angle1, angle2,
				self.orig_type, self.orig_angle1, self.orig_angle2, self.target)
		else:
			self.api.set_circle_properties(circle_type, angle1,
										angle2, self.target)
		self.update_points()
Beispiel #8
0
    def fill_pattern(self, obj, pdfpath, fill_trafo, pattern):
        if not fill_trafo:
            fill_trafo = [1.0, 0.0, 0.0, 1.0, 0.0, 0.0]
        inv_ptrn_trafo = libgeom.invert_trafo(pattern[3])
        inv_trafo = libgeom.multiply_trafo(
            libgeom.invert_trafo(fill_trafo),
            libgeom.invert_trafo(inv_ptrn_trafo))
        paths = libgeom.apply_trafo_to_paths(obj.paths, obj.trafo)
        paths = libgeom.apply_trafo_to_paths(paths, inv_trafo)
        bbox = libgeom.get_paths_bbox(paths)
        cv_trafo = libgeom.multiply_trafo(pattern[3], fill_trafo)

        bmpstr = b64decode(pattern[1])
        image_obj = sk2_model.Pixmap(obj.config)
        libimg.set_image_data(self.cms, image_obj, bmpstr)
        if pattern[0] == sk2const.PATTERN_IMG and \
                        len(pattern) > 2:
            image_obj.style[3] = deepcopy(pattern[2])
        libimg.update_image(self.cms, image_obj)

        self.canvas.saveState()
        self.canvas.clipPath(pdfpath, 0, 0)
        self.canvas.transform(*cv_trafo)

        w, h = image_obj.get_size()
        x = bbox[0]
        y = bbox[3]
        while y > bbox[1] - h:
            while x < bbox[2]:
                self.canvas.saveState()
                self.canvas.transform(1.0, 0.0, 0.0, 1.0, x, y)
                self.draw_pixmap_obj(image_obj)
                self.canvas.restoreState()
                x += w
            y -= h
            x = bbox[0]
        self.canvas.restoreState()
Beispiel #9
0
	def fill_pattern(self, obj, pdfpath, fill_trafo, pattern):
		if not fill_trafo:
			fill_trafo = [1.0, 0.0, 0.0, 1.0, 0.0, 0.0]
		inv_ptrn_trafo = libgeom.invert_trafo(pattern[3])
		inv_trafo = libgeom.multiply_trafo(libgeom.invert_trafo(fill_trafo),
										libgeom.invert_trafo(inv_ptrn_trafo))
		paths = libgeom.apply_trafo_to_paths(obj.paths, obj.trafo)
		paths = libgeom.apply_trafo_to_paths(paths, inv_trafo)
		bbox = libgeom.get_paths_bbox(paths)
		cv_trafo = libgeom.multiply_trafo(pattern[3], fill_trafo)

		bmpstr = b64decode(pattern[1])
		image_obj = sk2_model.Pixmap(obj.config)
		libimg.set_image_data(self.cms, image_obj, bmpstr)
		if pattern[0] == sk2_const.PATTERN_IMG and \
		 len(pattern) > 2:
			image_obj.style[3] = deepcopy(pattern[2])
		libimg.update_image(self.cms, image_obj)

		self.canvas.saveState()
		self.canvas.clipPath(pdfpath, 0, 0)
		self.canvas.transform(*cv_trafo)

		w, h = image_obj.get_size()
		x = bbox[0]
		y = bbox[3]
		while y > bbox[1] - h:
			while x < bbox[2]:
				self.canvas.saveState()
				self.canvas.transform(1.0, 0.0, 0.0, 1.0, x, y)
				self.draw_pixmap_obj(image_obj)
				self.canvas.restoreState()
				x += w
			y -= h
			x = bbox[0]
		self.canvas.restoreState()
Beispiel #10
0
    def apply_rounding(self, point, final=False, inplace=False):
        wpoint = self.canvas.point_win_to_doc(point)
        invtrafo = libgeom.invert_trafo(self.target.trafo)
        wpoint = libgeom.apply_trafo_to_point(wpoint, invtrafo)
        corners = [] + self.target.corners
        name = str(self.rnd_index) + str(self.rnd_subindex)

        if self.stop2:
            val = abs(wpoint[0] - self.start[0])
            val2 = abs(wpoint[1] - self.start[1])
            start = self.start
            if val > val2:
                if self.rnd_index in (0, 2):
                    stop = self.stop2
                    res = (wpoint[0] - start[0]) / (stop[0] - start[0])
                else:
                    stop = self.stop
                    res = (wpoint[0] - start[0]) / (stop[0] - start[0])
            else:
                if self.rnd_index in (0, 2):
                    stop = self.stop
                    res = (wpoint[1] - start[1]) / (stop[1] - start[1])
                else:
                    stop = self.stop2
                    res = (wpoint[1] - start[1]) / (stop[1] - start[1])
        else:
            start = self.start
            stop = self.stop
            if name in H_ORIENT:
                res = (wpoint[0] - start[0]) / (stop[0] - start[0])
            else:
                res = (wpoint[1] - start[1]) / (stop[1] - start[1])

        if res < 0.0:
            res = 0.0
        if res > 1.0:
            res = 1.0

        if inplace:
            corners[self.rnd_index] = res
        else:
            corners = [res, res, res, res]
        if final:
            self.api.set_rect_corners_final(corners, self.orig_corners,
                                            self.target)
        else:
            self.api.set_rect_corners(corners, self.target)
        self.update_points()
Beispiel #11
0
    def get_index_by_point(self, point):
        doc_point = self.canvas.win_to_doc(point)
        inv_trafo = libgeom.invert_trafo(self.target.trafo)
        doc_point = libgeom.apply_trafo_to_point(doc_point, inv_trafo)

        line = -1
        for item in self.target.cache_line_points:
            line += 1
            if doc_point[1] >= item[1]: break

        index = self.lines[line][0]
        for item in range(*self.lines[line]):
            layout = self.target.cache_layout_data
            pos = layout[index][0] + layout[index][2] / 2.0
            if doc_point[0] <= pos: break
            index += 1
        return index
Beispiel #12
0
	def apply_resizing(self, point, final=False):
		wpoint = self.canvas.point_win_to_doc(point)
		invtrafo = libgeom.invert_trafo(self.target.trafo)
		wpoint = libgeom.apply_trafo_to_point(wpoint, invtrafo)
		rect = self.target.get_rect()
		corners = [] + self.target.corners
		if self.res_index == 0:
			rect[2] -= wpoint[0] - rect[0]
			rect[0] = wpoint[0]
			if rect[2] < 0:
				self.res_index = 2
				c0, c1, c2, c3 = corners
				corners = [c3, c2, c1, c0]
		elif self.res_index == 1:
			rect[3] = wpoint[1] - rect[1]
			if rect[3] < 0:
				self.res_index = 3
				c0, c1, c2, c3 = corners
				corners = [c1, c0, c3, c2]
		elif self.res_index == 2:
			rect[2] = wpoint[0] - rect[0]
			if rect[2] < 0:
				self.res_index = 0
				c0, c1, c2, c3 = corners
				corners = [c3, c2, c1, c0]
		elif self.res_index == 3:
			rect[3] -= wpoint[1] - rect[1]
			rect[1] = wpoint[1]
			if rect[3] < 0:
				self.res_index = 1
				c0, c1, c2, c3 = corners
				corners = [c1, c0, c3, c2]
		rect = libgeom.normalize_rect(rect)
		if final:
			self.api.set_rect_final(self.target, rect, self.orig_rect)
			if not corners == self.orig_corners:
				self.api.set_rect_corners_final(corners, self.orig_corners,
										self.target)
				self.orig_corners = [] + self.target.corners
			self.orig_rect = self.target.get_rect()
		else:
			self.api.set_rect(self.target, rect)
			if not corners == self.target.corners:
				self.api.set_rect_corners(corners, self.target)
		self.update_points()
Beispiel #13
0
	def apply_rounding(self, point, final=False, inplace=False):
		wpoint = self.canvas.point_win_to_doc(point)
		invtrafo = libgeom.invert_trafo(self.target.trafo)
		wpoint = libgeom.apply_trafo_to_point(wpoint, invtrafo)
		corners = [] + self.target.corners
		name = str(self.rnd_index) + str(self.rnd_subindex)

		res = 0.0
		if self.stop2:
			val = abs(wpoint[0] - self.start[0])
			val2 = abs(wpoint[1] - self.start[1])
			start = self.start
			if val > val2:
				if self.rnd_index in (0, 2):
					stop = self.stop2
					res = (wpoint[0] - start[0]) / (stop[0] - start[0])
				else:
					stop = self.stop
					res = (wpoint[0] - start[0]) / (stop[0] - start[0])
			else:
				if self.rnd_index in (0, 2):
					stop = self.stop
					res = (wpoint[1] - start[1]) / (stop[1] - start[1])
				else:
					stop = self.stop2
					res = (wpoint[1] - start[1]) / (stop[1] - start[1])
		else:
			start = self.start
			stop = self.stop
			if name in H_ORIENT:
				res = (wpoint[0] - start[0]) / (stop[0] - start[0])
			else:
				res = (wpoint[1] - start[1]) / (stop[1] - start[1])

		if res < 0.0: res = 0.0
		if res > 1.0: res = 1.0

		if inplace: corners[self.rnd_index] = res
		else: corners = [res, res, res, res]
		if final:
			self.api.set_rect_corners_final(corners, self.orig_corners,
										self.target)
		else:
			self.api.set_rect_corners(corners, self.target)
		self.update_points()
Beispiel #14
0
 def apply_resizing(self, point, final=False):
     wpoint = self.canvas.point_win_to_doc(point)
     invtrafo = libgeom.invert_trafo(self.target.trafo)
     wpoint = libgeom.apply_trafo_to_point(wpoint, invtrafo)
     rect = self.target.get_rect()
     corners = [] + self.target.corners
     if self.res_index == 0:
         rect[2] -= wpoint[0] - rect[0]
         rect[0] = wpoint[0]
         if rect[2] < 0:
             self.res_index = 2
             c0, c1, c2, c3 = corners
             corners = [c3, c2, c1, c0]
     elif self.res_index == 1:
         rect[3] = wpoint[1] - rect[1]
         if rect[3] < 0:
             self.res_index = 3
             c0, c1, c2, c3 = corners
             corners = [c1, c0, c3, c2]
     elif self.res_index == 2:
         rect[2] = wpoint[0] - rect[0]
         if rect[2] < 0:
             self.res_index = 0
             c0, c1, c2, c3 = corners
             corners = [c3, c2, c1, c0]
     elif self.res_index == 3:
         rect[3] -= wpoint[1] - rect[1]
         rect[1] = wpoint[1]
         if rect[3] < 0:
             self.res_index = 1
             c0, c1, c2, c3 = corners
             corners = [c1, c0, c3, c2]
     rect = libgeom.normalize_rect(rect)
     if final:
         self.api.set_rect_final(self.target, rect, self.orig_rect)
         if not corners == self.orig_corners:
             self.api.set_rect_corners_final(corners, self.orig_corners,
                                             self.target)
             self.orig_corners = [] + self.target.corners
         self.orig_rect = self.target.get_rect()
     else:
         self.api.set_rect(self.target, rect)
         if not corners == self.target.corners:
             self.api.set_rect_corners(corners, self.target)
     self.update_points()
Beispiel #15
0
	def apply_midpoint_change(self, point, index, control=False, final=False):
		wpoint = self.canvas.point_win_to_doc(point)
		invtrafo = libgeom.invert_trafo(self.target.trafo)
		x, y = libgeom.apply_trafo_to_point(wpoint, invtrafo)
		radius = self.target.get_midpoint_radius()
		angle = self.target.get_midpoint_angle(index)
		coef2 = libgeom.get_point_radius([x, y]) / radius
		if control:
			props = [self.orig_angle1, self.orig_angle2,
					self.orig_coef1, coef2]
		else:
			angle2 = libgeom.get_point_angle([x, y]) - angle
			props = [self.orig_angle1, angle2,
					self.orig_coef1, coef2]
		if final:
			props_before = [self.orig_angle1, self.orig_angle2,
					self.orig_coef1, self.orig_coef2]
			self.api.set_polygon_properties_final(props, props_before,
												self.target)
		else:
			self.api.set_polygon_properties(props, self.target)
		self.update_points()
Beispiel #16
0
 def apply_midpoint_change(self, point, index, control=False, final=False):
     wpoint = self.canvas.point_win_to_doc(point)
     invtrafo = libgeom.invert_trafo(self.target.trafo)
     x, y = libgeom.apply_trafo_to_point(wpoint, invtrafo)
     radius = self.target.get_midpoint_radius()
     angle = self.target.get_midpoint_angle(index)
     coef2 = libgeom.get_point_radius([x, y]) / radius
     if control:
         props = [self.orig_angle1, self.orig_angle2,
                  self.orig_coef1, coef2]
     else:
         angle2 = libgeom.get_point_angle([x, y]) - angle
         props = [self.orig_angle1, angle2,
                  self.orig_coef1, coef2]
     if final:
         props_before = [self.orig_angle1, self.orig_angle2,
                         self.orig_coef1, self.orig_coef2]
         self.api.set_polygon_properties_final(props, props_before,
                                               self.target)
     else:
         self.api.set_polygon_properties(props, self.target)
     self.update_points()
Beispiel #17
0
def parse_svg_path_cmds(pathcmds):
    index = 0
    last = None
    last_index = 0
    cmds = []
    pathcmds = re.sub('  *', ' ', pathcmds)
    for item in pathcmds:
        if item in 'MmZzLlHhVvCcSsQqTtAa':
            if last:
                coords = parse_svg_coords(pathcmds[last_index + 1:index])
                cmds.append((last, coords))
            last = item
            last_index = index
        index += 1

    coords = parse_svg_coords(pathcmds[last_index + 1:index])
    cmds.append([last, coords])

    paths = []
    path = []
    cpoint = []
    rel_flag = False
    last_cmd = 'M'
    last_quad = None

    for cmd in cmds:
        if cmd[0] in 'Mm':
            if path: paths.append(path)
            path = deepcopy(PATH_STUB)
            rel_flag = cmd[0] == 'm'
            points = [cmd[1][i:i + 2] for i in range(0, len(cmd[1]), 2)]
            for point in points:
                if cpoint and rel_flag:
                    point = add_points(base_point(cpoint), point)
                if not path[0]:
                    path[0] = point
                else:
                    path[1].append(point)
                cpoint = point
        elif cmd[0] in 'Zz':
            p0 = [] + base_point(cpoint)
            p1 = [] + path[0]
            if not libgeom.is_equal_points(p0, p1, 8):
                path[1].append([] + path[0])
            path[2] = sk2const.CURVE_CLOSED
            cpoint = [] + path[0]
        elif cmd[0] in 'Cc':
            rel_flag = cmd[0] == 'c'
            points = [cmd[1][i:i + 2] for i in range(0, len(cmd[1]), 2)]
            points = [points[i:i + 3] for i in range(0, len(points), 3)]
            for point in points:
                if rel_flag:
                    point = [
                        add_points(base_point(cpoint), point[0]),
                        add_points(base_point(cpoint), point[1]),
                        add_points(base_point(cpoint), point[2])
                    ]
                qpoint = [] + point
                qpoint.append(sk2const.NODE_CUSP)
                path[1].append(qpoint)
                cpoint = point
        elif cmd[0] in 'Ll':
            rel_flag = cmd[0] == 'l'
            points = [cmd[1][i:i + 2] for i in range(0, len(cmd[1]), 2)]
            for point in points:
                if rel_flag:
                    point = add_points(base_point(cpoint), point)
                path[1].append(point)
                cpoint = point
        elif cmd[0] in 'Hh':
            rel_flag = cmd[0] == 'h'
            for x in cmd[1]:
                dx, y = base_point(cpoint)
                if rel_flag:
                    point = [x + dx, y]
                else:
                    point = [x, y]
                path[1].append(point)
                cpoint = point
        elif cmd[0] in 'Vv':
            rel_flag = cmd[0] == 'v'
            for y in cmd[1]:
                x, dy = base_point(cpoint)
                if rel_flag:
                    point = [x, y + dy]
                else:
                    point = [x, y]
                path[1].append(point)
                cpoint = point
        elif cmd[0] in 'Ss':
            rel_flag = cmd[0] == 's'
            points = [cmd[1][i:i + 2] for i in range(0, len(cmd[1]), 2)]
            points = [points[i:i + 2] for i in range(0, len(points), 2)]
            for point in points:
                q = cpoint
                p = cpoint
                if len(cpoint) > 2:
                    q = cpoint[1]
                    p = cpoint[2]
                p1 = sub_points(add_points(p, p), q)
                if rel_flag:
                    p2 = add_points(base_point(cpoint), point[0])
                    p3 = add_points(base_point(cpoint), point[1])
                else:
                    p2, p3 = point
                point = [p1, p2, p3]
                qpoint = [] + point
                qpoint.append(sk2const.NODE_CUSP)
                path[1].append(qpoint)
                cpoint = point

        elif cmd[0] in 'Qq':
            rel_flag = cmd[0] == 'q'
            groups = [cmd[1][i:i + 4] for i in range(0, len(cmd[1]), 4)]
            for vals in groups:
                p = base_point(cpoint)
                if rel_flag:
                    q = add_points(p, [vals[0], vals[1]])
                    p3 = add_points(p, [vals[2], vals[3]])
                else:
                    q = [vals[0], vals[1]]
                    p3 = [vals[2], vals[3]]
                p1 = add_points(mult_point(p, F13), mult_point(q, F23))
                p2 = add_points(mult_point(p3, F13), mult_point(q, F23))

                point = [p1, p2, p3]
                qpoint = [] + point
                qpoint.append(sk2const.NODE_CUSP)
                path[1].append(qpoint)
                cpoint = point
                last_quad = q

        elif cmd[0] in 'Tt':
            rel_flag = cmd[0] == 't'
            groups = [cmd[1][i:i + 2] for i in range(0, len(cmd[1]), 2)]
            if last_cmd not in 'QqTt' or last_quad is None:
                last_quad = base_point(cpoint)
            for vals in groups:
                p = base_point(cpoint)
                q = sub_points(mult_point(p, 2.0), last_quad)
                if rel_flag:
                    p3 = add_points(p, [vals[0], vals[1]])
                else:
                    p3 = [vals[0], vals[1]]
                p1 = add_points(mult_point(p, F13), mult_point(q, F23))
                p2 = add_points(mult_point(p3, F13), mult_point(q, F23))

                point = [p1, p2, p3]
                qpoint = [] + point
                qpoint.append(sk2const.NODE_CUSP)
                path[1].append(qpoint)
                cpoint = point
                last_quad = q

        elif cmd[0] in 'Aa':
            rel_flag = cmd[0] == 'a'
            arcs = [cmd[1][i:i + 7] for i in range(0, len(cmd[1]), 7)]

            for arc in arcs:
                cpoint = base_point(cpoint)
                rev_flag = False
                rx, ry, xrot, large_arc_flag, sweep_flag, x, y = arc
                rx = abs(rx)
                ry = abs(ry)
                if rel_flag:
                    x += cpoint[0]
                    y += cpoint[1]
                if cpoint == [x, y]: continue
                if not rx or not ry:
                    path[1].append([x, y])
                    continue

                vector = [[] + cpoint, [x, y]]
                if sweep_flag:
                    vector = [[x, y], [] + cpoint]
                    rev_flag = True
                cpoint = [x, y]

                dir_tr = libgeom.trafo_rotate_grad(-xrot)

                if rx > ry:
                    tr = [1.0, 0.0, 0.0, rx / ry, 0.0, 0.0]
                    r = rx
                else:
                    tr = [ry / rx, 0.0, 0.0, 1.0, 0.0, 0.0]
                    r = ry

                dir_tr = libgeom.multiply_trafo(dir_tr, tr)
                vector = libgeom.apply_trafo_to_points(vector, dir_tr)

                l = libgeom.distance(*vector)

                if l > 2.0 * r: r = l / 2.0

                mp = libgeom.midpoint(*vector)

                tr0 = libgeom.trafo_rotate(math.pi / 2.0, mp[0], mp[1])
                pvector = libgeom.apply_trafo_to_points(vector, tr0)

                k = math.sqrt(r * r - l * l / 4.0)
                if large_arc_flag:
                    center = libgeom.midpoint(mp, pvector[1], 2.0 * k / l)
                else:
                    center = libgeom.midpoint(mp, pvector[0], 2.0 * k / l)

                angle1 = libgeom.get_point_angle(vector[0], center)
                angle2 = libgeom.get_point_angle(vector[1], center)

                da = angle2 - angle1
                start = angle1
                end = angle2
                if large_arc_flag:
                    if -math.pi >= da or da <= math.pi:
                        start = angle2
                        end = angle1
                        rev_flag = not rev_flag
                else:
                    if -math.pi <= da or da >= math.pi:
                        start = angle2
                        end = angle1
                        rev_flag = not rev_flag

                pth = libgeom.get_circle_paths(start, end, sk2const.ARC_ARC)[0]

                if rev_flag:
                    pth = libgeom.reverse_path(pth)

                points = pth[1]
                for point in points:
                    if len(point) == 3:
                        point.append(sk2const.NODE_CUSP)

                tr0 = [1.0, 0.0, 0.0, 1.0, -0.5, -0.5]
                points = libgeom.apply_trafo_to_points(points, tr0)

                tr1 = [2.0 * r, 0.0, 0.0, 2.0 * r, 0.0, 0.0]
                points = libgeom.apply_trafo_to_points(points, tr1)

                tr2 = [1.0, 0.0, 0.0, 1.0, center[0], center[1]]
                points = libgeom.apply_trafo_to_points(points, tr2)

                tr3 = libgeom.invert_trafo(dir_tr)
                points = libgeom.apply_trafo_to_points(points, tr3)

                for point in points:
                    path[1].append(point)

        last_cmd = cmd[0]

    if path: paths.append(path)
    return paths
Beispiel #18
0
 def is_point_in_layout_bbox(self, point):
     bbox = self.target.cache_layout_bbox
     doc_point = self.canvas.win_to_doc(point)
     inv_trafo = libgeom.invert_trafo(self.target.trafo)
     doc_point = libgeom.apply_trafo_to_point(doc_point, inv_trafo)
     return libgeom.is_point_in_bbox(doc_point, bbox)
Beispiel #19
0
def set_sk2_style(sk1_style, dest_obj=None):
    sk2_style = [[], [], [], []]
    line_pattern = sk1_style.line_pattern
    fill_pattern = sk1_style.fill_pattern
    if not line_pattern.is_Empty:
        sk2_line = [
            sk2const.STROKE_MIDDLE, sk1_style.line_width,
            get_sk2_color(line_pattern.color),
            list(sk1_style.line_dashes), SK2_LINE_CAP[sk1_style.line_cap],
            SK2_LINE_JOIN[sk1_style.line_join], 10.0, 0, 0, []
        ]
        sk2_style[1] = sk2_line

    if fill_pattern.is_Solid:
        sk2_fill = []
        if fill_pattern.is_Solid:
            sk2_fill = [
                sk2const.FILL_EVENODD, sk2const.FILL_SOLID,
                get_sk2_color(fill_pattern.color)
            ]
        sk2_style[0] = sk2_fill

    elif fill_pattern.is_AxialGradient:
        stops = get_sk2_stops(fill_pattern.gradient.colors)
        point = [fill_pattern.direction.x, fill_pattern.direction.y]
        angle = libgeom.get_point_angle(point, [0.0, 0.0])
        points = [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]]

        m21 = math.sin(-angle)
        m11 = m22 = math.cos(-angle)
        m12 = -m21
        dx = 0.5 - m11 * 0.5 + m21 * 0.5
        dy = 0.5 - m21 * 0.5 - m11 * 0.5
        trafo = [m11, m21, m12, m22, dx, dy]
        points = libgeom.apply_trafo_to_points(points, trafo)
        bbox = libgeom.bbox_for_points(points)
        w, h = libgeom.bbox_size(bbox)
        vector = [[bbox[0], 0.5], [bbox[2], 0.5]]
        invtrafo = libgeom.invert_trafo(trafo)
        vector = libgeom.apply_trafo_to_points(vector, invtrafo)

        dest_obj.update()
        bbox = dest_obj.cache_bbox
        w, h = libgeom.bbox_size(bbox)
        trafo = [w, 0.0, 0.0, h, bbox[0], bbox[1]]
        vector = libgeom.apply_trafo_to_points(vector, trafo)

        sk2_fill = [
            sk2const.FILL_EVENODD, sk2const.FILL_GRADIENT,
            [sk2const.GRADIENT_LINEAR, vector, stops]
        ]
        sk2_style[0] = sk2_fill
        dest_obj.fill_trafo = [] + sk2const.NORMAL_TRAFO

    elif fill_pattern.is_RadialGradient or fill_pattern.is_ConicalGradient:
        stops = get_sk2_stops(fill_pattern.gradient.colors)
        dest_obj.update()
        bbox = dest_obj.cache_bbox
        cg = [fill_pattern.center.x, fill_pattern.center.y]
        w, h = libgeom.bbox_size(bbox)
        start_point = [bbox[0] + w * cg[0], bbox[1] + h * cg[1]]
        points = libgeom.bbox_points(bbox)
        r = 0
        for point in points:
            dist = libgeom.distance(point, start_point)
            r = max(r, dist)
        end_point = [start_point[0] + r, start_point[1]]
        sk2_fill = [
            sk2const.FILL_EVENODD, sk2const.FILL_GRADIENT,
            [sk2const.GRADIENT_RADIAL, [start_point, end_point], stops]
        ]
        sk2_style[0] = sk2_fill
        dest_obj.fill_trafo = [] + sk2const.NORMAL_TRAFO

    dest_obj.style = sk2_style