Example #1
0
 def update_objects_grid(self):
     self.objects_grid = [[], []]
     layers = self.presenter.get_visible_layers()
     for layer in layers:
         for obj in layer.childs:
             points = libgeom.bbox_middle_points(obj.cache_bbox)
             for point in points:
                 self.objects_grid[0].append(point[0])
                 self.objects_grid[1].append(point[1])
Example #2
0
	def update_objects_grid(self):
		self.objects_grid = [[], []]
		layers = self.presenter.get_visible_layers()
		for layer in layers:
			for obj in layer.childs:
				points = libgeom.bbox_middle_points(obj.cache_bbox)
				for point in points:
					self.objects_grid[0].append(point[0])
					self.objects_grid[1].append(point[1])
Example #3
0
    def _snap(self, bbox, trafo):
        result = [] + trafo
        points = libgeom.bbox_middle_points(bbox)
        tr_points = libgeom.apply_trafo_to_points(points, trafo)
        active_snap = [None, None]

        shift_x = []
        snap_x = []
        for point in [tr_points[0], tr_points[2], tr_points[1]]:
            flag, wp, dp = self.snap.snap_point(point, False, snap_y=False)
            if flag:
                shift_x.append(dp[0] - point[0])
                snap_x.append(dp[0])
        if shift_x:
            if len(shift_x) > 1:
                if abs(shift_x[0]) < abs(shift_x[1]):
                    dx = shift_x[0]
                    active_snap[0] = snap_x[0]
                else:
                    dx = shift_x[1]
                    active_snap[0] = snap_x[1]
            else:
                dx = shift_x[0]
                active_snap[0] = snap_x[0]
            result[4] += dx

        shift_y = []
        snap_y = []
        pnts = [tr_points[1], tr_points[3], tr_points[2]]
        if len(self.selection.objs) == 1 and self.selection.objs[0].is_text:
            line_points = self.selection.objs[0].get_line_points()
            pnts = libgeom.apply_trafo_to_points(line_points, trafo) + pnts
        for point in pnts:
            flag, wp, dp = self.snap.snap_point(point, False, snap_x=False)
            if flag:
                shift_y.append(dp[1] - point[1])
                snap_y.append(dp[1])
        if shift_y:
            if len(shift_y) > 1:
                if abs(shift_y[0]) < abs(shift_y[1]):
                    dy = shift_y[0]
                    active_snap[1] = snap_y[0]
                else:
                    dy = shift_y[1]
                    active_snap[1] = snap_y[1]
            else:
                dy = shift_y[0]
                active_snap[1] = snap_y[0]
            result[5] += dy

        self.snap.active_snap = [] + active_snap
        return result
Example #4
0
	def _snap(self, bbox, trafo):
		result = [] + trafo
		points = libgeom.bbox_middle_points(bbox)
		tr_points = libgeom.apply_trafo_to_points(points, trafo)
		active_snap = [None, None]

		shift_x = []
		snap_x = []
		for point in [tr_points[0], tr_points[2], tr_points[1]]:
			flag, wp, dp = self.snap.snap_point(point, False, snap_y=False)
			if flag:
				shift_x.append(dp[0] - point[0])
				snap_x.append(dp[0])
		if shift_x:
			if len(shift_x) > 1:
				if abs(shift_x[0]) < abs(shift_x[1]):
					dx = shift_x[0]
					active_snap[0] = snap_x[0]
				else:
					dx = shift_x[1]
					active_snap[0] = snap_x[1]
			else:
				dx = shift_x[0]
				active_snap[0] = snap_x[0]
			result[4] += dx

		shift_y = []
		snap_y = []
		pnts = [tr_points[1], tr_points[3], tr_points[2]]
		if len(self.selection.objs) == 1 and self.selection.objs[0].is_text():
			line_points = self.selection.objs[0].get_line_points()
			pnts = libgeom.apply_trafo_to_points(line_points, trafo) + pnts
		for point in pnts:
			flag, wp, dp = self.snap.snap_point(point, False, snap_x=False)
			if flag:
				shift_y.append(dp[1] - point[1])
				snap_y.append(dp[1])
		if shift_y:
			if len(shift_y) > 1:
				if abs(shift_y[0]) < abs(shift_y[1]):
					dy = shift_y[0]
					active_snap[1] = snap_y[0]
				else:
					dy = shift_y[1]
					active_snap[1] = snap_y[1]
			else:
				dy = shift_y[0]
				active_snap[1] = snap_y[0]
			result[5] += dy

		self.snap.active_snap = [] + active_snap
		return result
Example #5
0
    def _calc_bottom_scale_trafo(self, event):
        is_centering = event.is_shift()
        is_constraining = event.is_ctrl()
        is_snapping = not is_constraining

        m11, m21, m12, m22, dx, dy = sk2const.NORMAL_TRAFO
        start_point = self.canvas.win_to_doc(self.start)
        end_point = self.canvas.win_to_doc(self.end)
        bbox = self.presenter.selection.bbox
        middle_points = libgeom.bbox_middle_points(bbox)
        point = middle_points[3]
        base_y = bbox[3]
        h = libgeom.bbox_size(bbox)[1]

        if is_centering:
            shift_y = h / 2.0 + self.selection.center_offset[1]
            ratio_y = h / shift_y if shift_y else 1.0
            ratio_y = ratio_y if abs(ratio_y) < MAX_RATIO_TRAFO else 1.0
            shift_y = h - shift_y
        else:
            shift_y, ratio_y = 0.0, 1.0

        change_y = h + (start_point[1] - end_point[1]) * ratio_y
        if is_constraining and h and change_y:
            if -h < change_y < h:
                change_y = 1.0 / (h // change_y) * h
            else:
                change_y = h + (change_y - h / 2) // h * h

        m22 = change_y / h if h and change_y else 1.0
        dy = base_y - base_y * m22 + shift_y * (m22 - 1.0)

        if is_snapping:
            trafo = [m11, m21, m12, m22, dx, dy]
            p = libgeom.apply_trafo_to_point(point, trafo)
            flag, _wp, end_point = self.snap.snap_point(p, False)
            start_point = point
            if flag:
                change_y = h + (start_point[1] - end_point[1]) * ratio_y
                m22 = change_y / h if h else 1.0
                dy = base_y - base_y * m22 + shift_y * (m22 - 1.0)

        return [m11 or EPSILON, m21, m12, m22 or EPSILON, dx, dy]
Example #6
0
    def _calc_left_scale_trafo(self, event):
        is_centering = event.is_shift()
        is_constraining = event.is_ctrl()
        is_snapping = not is_constraining

        m11, m21, m12, m22, dx, dy = sk2const.NORMAL_TRAFO
        start_point = self.canvas.win_to_doc(self.start)
        end_point = self.canvas.win_to_doc(self.end)
        bbox = self.presenter.selection.bbox
        middle_points = libgeom.bbox_middle_points(bbox)
        point = middle_points[0]
        base_x = bbox[2]
        w = libgeom.bbox_size(bbox)[0]

        if is_centering:
            shift_x = w / 2.0 + self.selection.center_offset[0]
            ratio_x = w / shift_x if shift_x else 1.0
            ratio_x = ratio_x if abs(ratio_x) < MAX_RATIO_TRAFO else 1.0
            shift_x = w - shift_x
        else:
            shift_x, ratio_x = 0.0, 1.0

        change_x = w + (start_point[0] - end_point[0]) * ratio_x
        if is_constraining and w and change_x:
            if -w < change_x < w:
                change_x = 1.0 / (w // change_x) * w
            else:
                change_x = w + (change_x - w / 2) // w * w

        m11 = change_x / w if w and change_x else 1.0
        dx = base_x - base_x * m11 + shift_x * (m11 - 1.0)

        if is_snapping:
            trafo = [m11, m21, m12, m22, dx, dy]
            p = libgeom.apply_trafo_to_point(point, trafo)
            flag, _wp, end_point = self.snap.snap_point(p, False)
            start_point = point
            if flag:
                change_x = w + (start_point[0] - end_point[0]) * ratio_x
                m11 = change_x / w if w else 1.0
                dx = base_x - base_x * m11 + shift_x * (m11 - 1.0)

        return [m11 or EPSILON, m21, m12, m22 or EPSILON, dx, dy]
Example #7
0
	def _calc_trafo(self, event):
		control = shift = False
		if event.state & gtk.gdk.CONTROL_MASK:
			control = True
		if event.state & gtk.gdk.SHIFT_MASK:
			shift = True
		mark = self.canvas.resize_marker
		start_point = self.canvas.win_to_doc(self.start)
		end_point = self.canvas.win_to_doc(self.end)
		bbox = self.presenter.selection.bbox
		middle_points = libgeom.bbox_middle_points(bbox)
		w = bbox[2] - bbox[0]
		h = bbox[3] - bbox[1]
		m11 = m22 = 1.0
		m12 = m21 = 0.0
		dx = dy = 0.0
		snap = [None, None]
		if mark == 0:
			dx = start_point[0] - end_point[0]
			dy = end_point[1] - start_point[1]
			if shift:
				if control:
					m11 = (w + 2.0 * dx) / w
					m22 = (h + 2.0 * dy) / h
					dx = -(bbox[2] * m11 - bbox[2]) + w * (m11 - 1.0) / 2.0
					dy = -(bbox[1] * m22 - bbox[1]) - h * (m22 - 1.0) / 2.0
					#---- snapping
					point = middle_points[0]
					trafo = [m11, m21, m12, m22, dx, dy]
					p = libgeom.apply_trafo_to_point(point, trafo)
					f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
					if f:
						dx = point[0] - p_doc[0]
						m11 = (w + 2.0 * dx) / w
						dx = -(bbox[2] * m11 - bbox[2]) + w * (m11 - 1.0) / 2.0
						snap[0] = self.snap.active_snap[0]
					point = middle_points[1]
					trafo = [m11, m21, m12, m22, dx, dy]
					p = libgeom.apply_trafo_to_point(point, trafo)
					f, p, p_doc = self.snap.snap_point(p, False, snap_x=False)
					if f:
						dy = p_doc[1] - point[1]
						m22 = (h + 2.0 * dy) / h
						dy = -(bbox[1] * m22 - bbox[1]) - h * (m22 - 1.0) / 2.0
						snap[1] = self.snap.active_snap[1]
					self.snap.active_snap = snap
					#---- snapping
				else:
					if abs(dx) < abs(dy):
						m11 = m22 = (w + 2.0 * dx) / w
					else:
						m11 = m22 = (h + 2.0 * dy) / h
					dx = -(bbox[2] * m11 - bbox[2]) + w * (m11 - 1.0) / 2.0
					dy = -(bbox[1] * m22 - bbox[1]) - h * (m22 - 1.0) / 2.0
					#---- snapping
					point = middle_points[0]
					trafo = [m11, m21, m12, m22, dx, dy]
					p = libgeom.apply_trafo_to_point(point, trafo)
					f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
					if f:
						dx = point[0] - p_doc[0]
						m11 = m22 = (w + 2.0 * dx) / w
						dx = -(bbox[2] * m11 - bbox[2]) + w * (m11 - 1.0) / 2.0
						dy = -(bbox[1] * m22 - bbox[1]) - h * (m22 - 1.0) / 2.0
						snap[0] = self.snap.active_snap[0]
					else:
						point = middle_points[1]
						trafo = [m11, m21, m12, m22, dx, dy]
						p = libgeom.apply_trafo_to_point(point, trafo)
						f, p, p_doc = self.snap.snap_point(p, False, snap_x=False)
						if f:
							dy = p_doc[1] - point[1]
							m11 = m22 = (h + 2.0 * dy) / h
							dx = -(bbox[2] * m11 - bbox[2]) + w * (m11 - 1.0) / 2.0
							dy = -(bbox[1] * m22 - bbox[1]) - h * (m22 - 1.0) / 2.0
							snap[1] = self.snap.active_snap[1]
					self.snap.active_snap = snap
					#---- snapping
			else:
				if control:
					m11 = (w + dx) / w
					m22 = (h + dy) / h
					dx = -(bbox[2] * m11 - bbox[2])
					dy = -(bbox[1] * m22 - bbox[1])
					#---- snapping
					point = middle_points[0]
					trafo = [m11, m21, m12, m22, dx, dy]
					p = libgeom.apply_trafo_to_point(point, trafo)
					f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
					if f:
						dx = point[0] - p_doc[0]
						m11 = (w + dx) / w
						dx = -(bbox[2] * m11 - bbox[2])
						snap[0] = self.snap.active_snap[0]
					point = middle_points[1]
					trafo = [m11, m21, m12, m22, dx, dy]
					p = libgeom.apply_trafo_to_point(point, trafo)
					f, p, p_doc = self.snap.snap_point(p, False, snap_x=False)
					if f:
						dy = p_doc[1] - point[1]
						m22 = (h + dy) / h
						dy = -(bbox[1] * m22 - bbox[1])
						snap[1] = self.snap.active_snap[1]
					self.snap.active_snap = snap
					#---- snapping
				else:
					if abs(dx) < abs(dy):
						m11 = m22 = (w + dx) / w
					else:
						m11 = m22 = (h + dy) / h
					dx = -(bbox[2] * m11 - bbox[2])
					dy = -(bbox[1] * m22 - bbox[1])
					#---- snapping
					point = middle_points[0]
					trafo = [m11, m21, m12, m22, dx, dy]
					p = libgeom.apply_trafo_to_point(point, trafo)
					f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
					if f:
						dx = point[0] - p_doc[0]
						m11 = m22 = (w + dx) / w
						dx = -(bbox[2] * m11 - bbox[2])
						dy = -(bbox[1] * m22 - bbox[1])
						snap[0] = self.snap.active_snap[0]
					else:
						point = middle_points[1]
						trafo = [m11, m21, m12, m22, dx, dy]
						p = libgeom.apply_trafo_to_point(point, trafo)
						f, p, p_doc = self.snap.snap_point(p, False, snap_x=False)
						if f:
							dy = p_doc[1] - point[1]
							m11 = m22 = (h + dy) / h
							dx = -(bbox[2] * m11 - bbox[2])
							dy = -(bbox[1] * m22 - bbox[1])
							snap[1] = self.snap.active_snap[1]
					self.snap.active_snap = snap
					#---- snapping
		if mark == 1:
			dy = end_point[1] - start_point[1]
			if shift:
				m22 = (h + 2.0 * dy) / h
				dy = -(bbox[1] * m22 - bbox[1]) - h * (m22 - 1.0) / 2.0
				#---- snapping
				point = middle_points[1]
				trafo = [m11, m21, m12, m22, dx, dy]
				p = libgeom.apply_trafo_to_point(point, trafo)
				f, p, p_doc = self.snap.snap_point(p, False)
				dy = p_doc[1] - point[1]
				m22 = (h + 2.0 * dy) / h
				dy = -(bbox[1] * m22 - bbox[1]) - h * (m22 - 1.0) / 2.0
				#---- snapping
			else:
				m22 = (h + dy) / h
				dy = -(bbox[1] * m22 - bbox[1])
				#---- snapping
				point = middle_points[1]
				trafo = [m11, m21, m12, m22, dx, dy]
				p = libgeom.apply_trafo_to_point(point, trafo)
				f, p, p_doc = self.snap.snap_point(p, False)
				dy = p_doc[1] - point[1]
				m22 = (h + dy) / h
				dy = -(bbox[1] * m22 - bbox[1])
				#---- snapping
		if mark == 2:
			dx = end_point[0] - start_point[0]
			dy = end_point[1] - start_point[1]
			if shift:
				if control:
					m11 = (w + 2.0 * dx) / w
					m22 = (h + 2.0 * dy) / h
					dx = -(bbox[0] * m11 - bbox[0]) - w * (m11 - 1.0) / 2.0
					dy = -(bbox[1] * m22 - bbox[1]) - h * (m22 - 1.0) / 2.0
					#---- snapping
					point = middle_points[2]
					trafo = [m11, m21, m12, m22, dx, dy]
					p = libgeom.apply_trafo_to_point(point, trafo)
					f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
					if f:
						dx = p_doc[0] - point[0]
						m11 = (w + 2.0 * dx) / w
						dx = -(bbox[0] * m11 - bbox[0]) - w * (m11 - 1.0) / 2.0
						snap[0] = self.snap.active_snap[0]
					point = middle_points[1]
					trafo = [m11, m21, m12, m22, dx, dy]
					p = libgeom.apply_trafo_to_point(point, trafo)
					f, p, p_doc = self.snap.snap_point(p, False, snap_x=False)
					if f:
						dy = p_doc[1] - point[1]
						m22 = (h + 2.0 * dy) / h
						dy = -(bbox[1] * m22 - bbox[1]) - h * (m22 - 1.0) / 2.0
						snap[1] = self.snap.active_snap[1]
					self.snap.active_snap = snap
					#---- snapping
				else:
					if abs(dx) < abs(dy):
						m11 = m22 = (w + 2.0 * dx) / w
					else:
						m11 = m22 = (h + 2.0 * dy) / h
					dx = -(bbox[0] * m11 - bbox[0]) - w * (m11 - 1.0) / 2.0
					dy = -(bbox[1] * m22 - bbox[1]) - h * (m22 - 1.0) / 2.0
					#---- snapping
					point = middle_points[2]
					trafo = [m11, m21, m12, m22, dx, dy]
					p = libgeom.apply_trafo_to_point(point, trafo)
					f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
					if f:
						dx = p_doc[0] - point[0]
						m11 = m22 = (w + 2.0 * dx) / w
						dx = -(bbox[0] * m11 - bbox[0]) - w * (m11 - 1.0) / 2.0
						dy = -(bbox[1] * m22 - bbox[1]) - h * (m22 - 1.0) / 2.0
						snap[0] = self.snap.active_snap[0]
					else:
						point = middle_points[1]
						trafo = [m11, m21, m12, m22, dx, dy]
						p = libgeom.apply_trafo_to_point(point, trafo)
						f, p, p_doc = self.snap.snap_point(p, False, snap_x=False)
						if f:
							dy = p_doc[1] - point[1]
							m11 = m22 = (h + 2.0 * dy) / h
							dx = -(bbox[0] * m11 - bbox[0]) - w * (m11 - 1.0) / 2.0
							dy = -(bbox[1] * m22 - bbox[1]) - h * (m22 - 1.0) / 2.0
							snap[1] = self.snap.active_snap[1]
					self.snap.active_snap = snap
					#---- snapping
			else:
				if control:
					m11 = (w + dx) / w
					m22 = (h + dy) / h
					dx = -(bbox[0] * m11 - bbox[0])
					dy = -(bbox[1] * m22 - bbox[1])
					#---- snapping
					point = middle_points[2]
					trafo = [m11, m21, m12, m22, dx, dy]
					p = libgeom.apply_trafo_to_point(point, trafo)
					f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
					if f:
						dx = p_doc[0] - point[0]
						m11 = (w + dx) / w
						dx = -(bbox[0] * m11 - bbox[0])
						snap[0] = self.snap.active_snap[0]
					point = middle_points[1]
					trafo = [m11, m21, m12, m22, dx, dy]
					p = libgeom.apply_trafo_to_point(point, trafo)
					f, p, p_doc = self.snap.snap_point(p, False, snap_x=False)
					if f:
						dy = p_doc[1] - point[1]
						m22 = (h + dy) / h
						dy = -(bbox[1] * m22 - bbox[1])
						snap[1] = self.snap.active_snap[1]
					self.snap.active_snap = snap
					#---- snapping
				else:
					if abs(dx) < abs(dy):
						m11 = m22 = (w + dx) / w
					else:
						m11 = m22 = (h + dy) / h
					dx = -(bbox[0] * m11 - bbox[0])
					dy = -(bbox[1] * m22 - bbox[1])
					#---- snapping
					point = middle_points[2]
					trafo = [m11, m21, m12, m22, dx, dy]
					p = libgeom.apply_trafo_to_point(point, trafo)
					f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
					if f:
						dx = p_doc[0] - point[0]
						m11 = m22 = (w + dx) / w
						dx = -(bbox[0] * m11 - bbox[0])
						dy = -(bbox[1] * m22 - bbox[1])
						snap[0] = self.snap.active_snap[0]
					else:
						point = middle_points[1]
						trafo = [m11, m21, m12, m22, dx, dy]
						p = libgeom.apply_trafo_to_point(point, trafo)
						f, p, p_doc = self.snap.snap_point(p, False, snap_x=False)
						if f:
							dy = p_doc[1] - point[1]
							m11 = m22 = (h + dy) / h
							dx = -(bbox[0] * m11 - bbox[0])
							dy = -(bbox[1] * m22 - bbox[1])
							snap[1] = self.snap.active_snap[1]
					self.snap.active_snap = snap
					#---- snapping
		if mark == 3:
			dx = start_point[0] - end_point[0]
			if shift:
				m11 = (w + 2.0 * dx) / w
				dx = -(bbox[2] * m11 - bbox[2]) + w * (m11 - 1.0) / 2.0
				#---- snapping
				point = middle_points[0]
				trafo = [m11, m21, m12, m22, dx, dy]
				p = libgeom.apply_trafo_to_point(point, trafo)
				f, p, p_doc = self.snap.snap_point(p, False)
				dx = point[0] - p_doc[0]
				m11 = (w + 2.0 * dx) / w
				dx = -(bbox[2] * m11 - bbox[2]) + w * (m11 - 1.0) / 2.0
				#---- snapping
			else:
				m11 = (w + dx) / w
				dx = -(bbox[2] * m11 - bbox[2])
				#---- snapping
				point = middle_points[0]
				trafo = [m11, m21, m12, m22, dx, dy]
				p = libgeom.apply_trafo_to_point(point, trafo)
				f, p, p_doc = self.snap.snap_point(p, False)
				dx = point[0] - p_doc[0]
				m11 = (w + dx) / w
				dx = -(bbox[2] * m11 - bbox[2])
				#---- snapping
		if mark == 5:
			dx = end_point[0] - start_point[0]
			if shift:
				m11 = (w + 2.0 * dx) / w
				dx = -(bbox[0] * m11 - bbox[0]) - w * (m11 - 1.0) / 2.0
				#---- snapping
				point = middle_points[2]
				trafo = [m11, m21, m12, m22, dx, dy]
				p = libgeom.apply_trafo_to_point(point, trafo)
				f, p, p_doc = self.snap.snap_point(p, False)
				dx = p_doc[0] - point[0]
				m11 = (w + 2.0 * dx) / w
				dx = -(bbox[0] * m11 - bbox[0]) - w * (m11 - 1.0) / 2.0
				#---- snapping
			else:
				m11 = (w + dx) / w
				dx = -(bbox[0] * m11 - bbox[0])
				#---- snapping
				point = middle_points[2]
				trafo = [m11, m21, m12, m22, dx, dy]
				p = libgeom.apply_trafo_to_point(point, trafo)
				f, p, p_doc = self.snap.snap_point(p, False)
				dx = p_doc[0] - point[0]
				m11 = (w + dx) / w
				dx = -(bbox[0] * m11 - bbox[0])
				#---- snapping
		if mark == 6:
			dx = start_point[0] - end_point[0]
			dy = start_point[1] - end_point[1]
			if shift:
				if control:
					m11 = (w + 2.0 * dx) / w
					m22 = (h + 2.0 * dy) / h
					dx = -(bbox[2] * m11 - bbox[2]) + w * (m11 - 1.0) / 2.0
					dy = -(bbox[3] * m22 - bbox[3]) + h * (m22 - 1.0) / 2.0
					#---- snapping
					point = middle_points[0]
					trafo = [m11, m21, m12, m22, dx, dy]
					p = libgeom.apply_trafo_to_point(point, trafo)
					f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
					if f:
						dx = point[0] - p_doc[0]
						m11 = (w + 2.0 * dx) / w
						dx = -(bbox[2] * m11 - bbox[2]) + w * (m11 - 1.0) / 2.0
						snap[0] = self.snap.active_snap[0]
					point = middle_points[3]
					trafo = [m11, m21, m12, m22, dx, dy]
					p = libgeom.apply_trafo_to_point(point, trafo)
					f, p, p_doc = self.snap.snap_point(p, False, snap_x=False)
					if f:
						dy = point[1] - p_doc[1]
						m22 = (h + 2.0 * dy) / h
						dy = -(bbox[3] * m22 - bbox[3]) + h * (m22 - 1.0) / 2.0
						snap[1] = self.snap.active_snap[1]
					self.snap.active_snap = snap
					#---- snapping
				else:
					if abs(dx) < abs(dy):
						m11 = m22 = (w + 2.0 * dx) / w
					else:
						m11 = m22 = (h + 2.0 * dy) / h
					dx = -(bbox[2] * m11 - bbox[2]) + w * (m11 - 1.0) / 2.0
					dy = -(bbox[3] * m22 - bbox[3]) + h * (m22 - 1.0) / 2.0
					#---- snapping
					point = middle_points[0]
					trafo = [m11, m21, m12, m22, dx, dy]
					p = libgeom.apply_trafo_to_point(point, trafo)
					f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
					if f:
						dx = point[0] - p_doc[0]
						m11 = m22 = (w + 2.0 * dx) / w
						dx = -(bbox[2] * m11 - bbox[2]) + w * (m11 - 1.0) / 2.0
						dy = -(bbox[3] * m22 - bbox[3]) + h * (m22 - 1.0) / 2.0
						snap[0] = self.snap.active_snap[0]
					else:
						point = middle_points[3]
						trafo = [m11, m21, m12, m22, dx, dy]
						p = libgeom.apply_trafo_to_point(point, trafo)
						f, p, p_doc = self.snap.snap_point(p, False, snap_x=False)
						if f:
							dy = point[1] - p_doc[1]
							m11 = m22 = (h + 2.0 * dy) / h
							dx = -(bbox[2] * m11 - bbox[2]) + w * (m11 - 1.0) / 2.0
							dy = -(bbox[3] * m22 - bbox[3]) + h * (m22 - 1.0) / 2.0
							snap[1] = self.snap.active_snap[1]
					self.snap.active_snap = snap
					#---- snapping
			else:
				if control:
					m11 = (w + dx) / w
					m22 = (h + dy) / h
					dx = -(bbox[2] * m11 - bbox[2])
					dy = -(bbox[3] * m22 - bbox[3])
					#---- snapping
					point = middle_points[0]
					trafo = [m11, m21, m12, m22, dx, dy]
					p = libgeom.apply_trafo_to_point(point, trafo)
					f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
					if f:
						dx = point[0] - p_doc[0]
						m11 = (w + dx) / w
						dx = -(bbox[2] * m11 - bbox[2])
						snap[0] = self.snap.active_snap[0]
					point = middle_points[3]
					trafo = [m11, m21, m12, m22, dx, dy]
					p = libgeom.apply_trafo_to_point(point, trafo)
					f, p, p_doc = self.snap.snap_point(p, False, snap_x=False)
					if f:
						dy = point[1] - p_doc[1]
						m22 = (h + dy) / h
						dy = -(bbox[3] * m22 - bbox[3])
						snap[1] = self.snap.active_snap[1]
					self.snap.active_snap = snap
					#---- snapping
				else:
					if abs(dx) < abs(dy):
						m11 = m22 = (w + dx) / w
					else:
						m11 = m22 = (h + dy) / h
					dx = -(bbox[2] * m11 - bbox[2])
					dy = -(bbox[3] * m22 - bbox[3])
					#---- snapping
					point = middle_points[0]
					trafo = [m11, m21, m12, m22, dx, dy]
					p = libgeom.apply_trafo_to_point(point, trafo)
					f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
					if f:
						dx = point[0] - p_doc[0]
						m11 = m22 = (w + dx) / w
						dx = -(bbox[2] * m11 - bbox[2])
						dy = -(bbox[3] * m22 - bbox[3])
						snap[0] = self.snap.active_snap[0]
					else:
						point = middle_points[3]
						trafo = [m11, m21, m12, m22, dx, dy]
						p = libgeom.apply_trafo_to_point(point, trafo)
						f, p, p_doc = self.snap.snap_point(p, False, snap_x=False)
						if f:
							dy = point[1] - p_doc[1]
							m11 = m22 = (h + dy) / h
							dx = -(bbox[2] * m11 - bbox[2])
							dy = -(bbox[3] * m22 - bbox[3])
							snap[1] = self.snap.active_snap[1]
					self.snap.active_snap = snap
					#---- snapping
		if mark == 7:
			dy = start_point[1] - end_point[1]
			if shift:
				m22 = (h + 2.0 * dy) / h
				dy = -(bbox[3] * m22 - bbox[3]) + h * (m22 - 1.0) / 2.0
				#---- snapping
				point = middle_points[3]
				trafo = [m11, m21, m12, m22, dx, dy]
				p = libgeom.apply_trafo_to_point(point, trafo)
				f, p, p_doc = self.snap.snap_point(p, False)
				dy = point[1] - p_doc[1]
				m22 = (h + 2.0 * dy) / h
				dy = -(bbox[3] * m22 - bbox[3]) + h * (m22 - 1.0) / 2.0
				#---- snapping
			else:
				m22 = (h + dy) / h
				dy = -(bbox[3] * m22 - bbox[3])
				#---- snapping
				point = middle_points[3]
				trafo = [m11, m21, m12, m22, dx, dy]
				p = libgeom.apply_trafo_to_point(point, trafo)
				f, p, p_doc = self.snap.snap_point(p, False)
				dy = point[1] - p_doc[1]
				m22 = (h + dy) / h
				dy = -(bbox[3] * m22 - bbox[3])
				#---- snapping
		if mark == 8:
			dx = end_point[0] - start_point[0]
			dy = start_point[1] - end_point[1]
			if shift:
				if control:
					m11 = (w + 2.0 * dx) / w
					m22 = (h + 2.0 * dy) / h
					dx = -(bbox[0] * m11 - bbox[0]) - w * (m11 - 1.0) / 2.0
					dy = -(bbox[3] * m22 - bbox[3]) + h * (m22 - 1.0) / 2.0
					#---- snapping
					point = middle_points[2]
					trafo = [m11, m21, m12, m22, dx, dy]
					p = libgeom.apply_trafo_to_point(point, trafo)
					f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
					if f:
						dx = p_doc[0] - point[0]
						m11 = (w + 2.0 * dx) / w
						dx = -(bbox[0] * m11 - bbox[0]) - w * (m11 - 1.0) / 2.0
						snap[0] = self.snap.active_snap[0]
					point = middle_points[3]
					trafo = [m11, m21, m12, m22, dx, dy]
					p = libgeom.apply_trafo_to_point(point, trafo)
					f, p, p_doc = self.snap.snap_point(p, False, snap_x=False)
					if f:
						dy = point[1] - p_doc[1]
						m22 = (h + 2.0 * dy) / h
						dy = -(bbox[3] * m22 - bbox[3]) + h * (m22 - 1.0) / 2.0
						snap[1] = self.snap.active_snap[1]
					self.snap.active_snap = snap
					#---- snapping
				else:
					if abs(dx) < abs(dy):
						m11 = m22 = (w + 2.0 * dx) / w
					else:
						m11 = m22 = (h + 2.0 * dy) / h
					dx = -(bbox[0] * m11 - bbox[0]) - w * (m11 - 1.0) / 2.0
					dy = -(bbox[3] * m22 - bbox[3]) + h * (m22 - 1.0) / 2.0
					#---- snapping
					point = middle_points[2]
					trafo = [m11, m21, m12, m22, dx, dy]
					p = libgeom.apply_trafo_to_point(point, trafo)
					f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
					if f:
						dx = p_doc[0] - point[0]
						m11 = m22 = (w + 2.0 * dx) / w
						dx = -(bbox[0] * m11 - bbox[0]) - w * (m11 - 1.0) / 2.0
						dy = -(bbox[3] * m22 - bbox[3]) + h * (m22 - 1.0) / 2.0
						snap[0] = self.snap.active_snap[0]
					else:
						point = middle_points[3]
						trafo = [m11, m21, m12, m22, dx, dy]
						p = libgeom.apply_trafo_to_point(point, trafo)
						f, p, p_doc = self.snap.snap_point(p, False, snap_x=False)
						if f:
							dy = point[1] - p_doc[1]
							m11 = m22 = (h + 2.0 * dy) / h
							dx = -(bbox[0] * m11 - bbox[0]) - w * (m11 - 1.0) / 2.0
							dy = -(bbox[3] * m22 - bbox[3]) + h * (m22 - 1.0) / 2.0
							snap[1] = self.snap.active_snap[1]
					self.snap.active_snap = snap
					#---- snapping
			else:
				if control:
					m11 = (w + dx) / w
					m22 = (h + dy) / h
					dx = -(bbox[0] * m11 - bbox[0])
					dy = -(bbox[3] * m22 - bbox[3])
					#---- snapping
					point = middle_points[2]
					trafo = [m11, m21, m12, m22, dx, dy]
					p = libgeom.apply_trafo_to_point(point, trafo)
					f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
					if f:
						dx = p_doc[0] - point[0]
						m11 = (w + dx) / w
						dx = -(bbox[0] * m11 - bbox[0])
						snap[0] = self.snap.active_snap[0]
					point = middle_points[3]
					trafo = [m11, m21, m12, m22, dx, dy]
					p = libgeom.apply_trafo_to_point(point, trafo)
					f, p, p_doc = self.snap.snap_point(p, False, snap_x=False)
					if f:
						dy = point[1] - p_doc[1]
						m22 = (h + dy) / h
						dy = -(bbox[3] * m22 - bbox[3])
						snap[1] = self.snap.active_snap[1]
					self.snap.active_snap = snap
					#---- snapping
				else:
					if abs(dx) < abs(dy):
						m11 = m22 = (w + dx) / w
					else:
						m11 = m22 = (h + dy) / h
					dx = -(bbox[0] * m11 - bbox[0])
					dy = -(bbox[3] * m22 - bbox[3])
					#---- snapping
					point = middle_points[2]
					trafo = [m11, m21, m12, m22, dx, dy]
					p = libgeom.apply_trafo_to_point(point, trafo)
					f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
					if f:
						dx = p_doc[0] - point[0]
						m11 = m22 = (w + dx) / w
						dx = -(bbox[0] * m11 - bbox[0])
						dy = -(bbox[3] * m22 - bbox[3])
						snap[0] = self.snap.active_snap[0]
					else:
						point = middle_points[3]
						trafo = [m11, m21, m12, m22, dx, dy]
						p = libgeom.apply_trafo_to_point(point, trafo)
						f, p, p_doc = self.snap.snap_point(p, False, snap_x=False)
						if f:
							dy = point[1] - p_doc[1]
							m11 = m22 = (h + dy) / h
							dx = -(bbox[0] * m11 - bbox[0])
							dy = -(bbox[3] * m22 - bbox[3])
							snap[1] = self.snap.active_snap[1]
					self.snap.active_snap = snap
					#---- snapping

		if mark == 11:
			change_x = end_point[0] - start_point[0]
			m12 = change_x / h
			dx = -bbox[1] * m12
		if mark == 16:
			change_x = start_point[0] - end_point[0]
			m12 = change_x / h
			dx = -bbox[3] * m12
		if mark == 13:
			change_y = start_point[1] - end_point[1]
			m21 = change_y / w
			dy = -bbox[2] * m21
		if mark == 14:
			change_y = end_point[1] - start_point[1]
			m21 = change_y / w
			dy = -bbox[0] * m21

		if mark in (10, 12, 15, 17):
			x0, y0 = bbox[:2]
			shift_x, shift_y = self.selection.center_offset
			center_x = x0 + w / 2.0 + shift_x
			center_y = y0 + h / 2.0 + shift_y
			a1 = math.atan2(start_point[1] - center_y, start_point[0] - center_x)
			a2 = math.atan2(end_point[1] - center_y, end_point[0] - center_x)
			angle = a2 - a1
			if control:
				step = config.rotation_step * math.pi / 180.0
				angle = round(angle / step) * step
			m21 = math.sin(angle)
			m11 = m22 = math.cos(angle)
			m12 = -m21
			dx = center_x - m11 * center_x + m21 * center_y;
			dy = center_y - m21 * center_x - m11 * center_y;

		if not m11: m11 = .0000000001
		if not m22: m22 = .0000000001
		return [m11, m21, m12, m22, dx, dy]
Example #8
0
    def _calc_trafo(self, event):
        control = event.is_ctrl()
        shift = event.is_shift()
        mark = self.canvas.resize_marker
        start_point = self.canvas.win_to_doc(self.start)
        end_point = self.canvas.win_to_doc(self.end)
        bbox = self.presenter.selection.bbox
        middle_points = libgeom.bbox_middle_points(bbox)
        w = bbox[2] - bbox[0]
        h = bbox[3] - bbox[1]
        m11 = m22 = 1.0
        m12 = m21 = 0.0
        dx = dy = 0.0
        snap = [None, None]
        if mark == 0:
            dx = start_point[0] - end_point[0]
            dy = end_point[1] - start_point[1]
            if shift:
                if control:
                    m11 = (w + 2.0 * dx) / w
                    m22 = (h + 2.0 * dy) / h
                    dx = -(bbox[2] * m11 - bbox[2]) + w * (m11 - 1.0) / 2.0
                    dy = -(bbox[1] * m22 - bbox[1]) - h * (m22 - 1.0) / 2.0
                    # ---- snapping
                    point = middle_points[0]
                    trafo = [m11, m21, m12, m22, dx, dy]
                    p = libgeom.apply_trafo_to_point(point, trafo)
                    f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
                    if f:
                        dx = point[0] - p_doc[0]
                        m11 = (w + 2.0 * dx) / w
                        dx = -(bbox[2] * m11 - bbox[2]) + w * (m11 - 1.0) / 2.0
                        snap[0] = self.snap.active_snap[0]
                    point = middle_points[1]
                    trafo = [m11, m21, m12, m22, dx, dy]
                    p = libgeom.apply_trafo_to_point(point, trafo)
                    f, p, p_doc = self.snap.snap_point(p, False, snap_x=False)
                    if f:
                        dy = p_doc[1] - point[1]
                        m22 = (h + 2.0 * dy) / h
                        dy = -(bbox[1] * m22 - bbox[1]) - h * (m22 - 1.0) / 2.0
                        snap[1] = self.snap.active_snap[1]
                    self.snap.active_snap = snap
                # ---- snapping
                else:
                    if abs(dx) < abs(dy):
                        m11 = m22 = (w + 2.0 * dx) / w
                    else:
                        m11 = m22 = (h + 2.0 * dy) / h
                    dx = -(bbox[2] * m11 - bbox[2]) + w * (m11 - 1.0) / 2.0
                    dy = -(bbox[1] * m22 - bbox[1]) - h * (m22 - 1.0) / 2.0
                    # ---- snapping
                    point = middle_points[0]
                    trafo = [m11, m21, m12, m22, dx, dy]
                    p = libgeom.apply_trafo_to_point(point, trafo)
                    f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
                    if f:
                        dx = point[0] - p_doc[0]
                        m11 = m22 = (w + 2.0 * dx) / w
                        dx = -(bbox[2] * m11 - bbox[2]) + w * (m11 - 1.0) / 2.0
                        dy = -(bbox[1] * m22 - bbox[1]) - h * (m22 - 1.0) / 2.0
                        snap[0] = self.snap.active_snap[0]
                    else:
                        point = middle_points[1]
                        trafo = [m11, m21, m12, m22, dx, dy]
                        p = libgeom.apply_trafo_to_point(point, trafo)
                        f, p, p_doc = self.snap.snap_point(p,
                                                           False,
                                                           snap_x=False)
                        if f:
                            dy = p_doc[1] - point[1]
                            m11 = m22 = (h + 2.0 * dy) / h
                            dx = -(bbox[2] * m11 - bbox[2]) + w * (m11 -
                                                                   1.0) / 2.0
                            dy = -(bbox[1] * m22 - bbox[1]) - h * (m22 -
                                                                   1.0) / 2.0
                            snap[1] = self.snap.active_snap[1]
                    self.snap.active_snap = snap
                    # ---- snapping
            else:
                if control:
                    m11 = (w + dx) / w
                    m22 = (h + dy) / h
                    dx = -(bbox[2] * m11 - bbox[2])
                    dy = -(bbox[1] * m22 - bbox[1])
                    # ---- snapping
                    point = middle_points[0]
                    trafo = [m11, m21, m12, m22, dx, dy]
                    p = libgeom.apply_trafo_to_point(point, trafo)
                    f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
                    if f:
                        dx = point[0] - p_doc[0]
                        m11 = (w + dx) / w
                        dx = -(bbox[2] * m11 - bbox[2])
                        snap[0] = self.snap.active_snap[0]
                    point = middle_points[1]
                    trafo = [m11, m21, m12, m22, dx, dy]
                    p = libgeom.apply_trafo_to_point(point, trafo)
                    f, p, p_doc = self.snap.snap_point(p, False, snap_x=False)
                    if f:
                        dy = p_doc[1] - point[1]
                        m22 = (h + dy) / h
                        dy = -(bbox[1] * m22 - bbox[1])
                        snap[1] = self.snap.active_snap[1]
                    self.snap.active_snap = snap
                # ---- snapping
                else:
                    if abs(dx) < abs(dy):
                        m11 = m22 = (w + dx) / w
                    else:
                        m11 = m22 = (h + dy) / h
                    dx = -(bbox[2] * m11 - bbox[2])
                    dy = -(bbox[1] * m22 - bbox[1])
                    # ---- snapping
                    point = middle_points[0]
                    trafo = [m11, m21, m12, m22, dx, dy]
                    p = libgeom.apply_trafo_to_point(point, trafo)
                    f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
                    if f:
                        dx = point[0] - p_doc[0]
                        m11 = m22 = (w + dx) / w
                        dx = -(bbox[2] * m11 - bbox[2])
                        dy = -(bbox[1] * m22 - bbox[1])
                        snap[0] = self.snap.active_snap[0]
                    else:
                        point = middle_points[1]
                        trafo = [m11, m21, m12, m22, dx, dy]
                        p = libgeom.apply_trafo_to_point(point, trafo)
                        f, p, p_doc = self.snap.snap_point(p,
                                                           False,
                                                           snap_x=False)
                        if f:
                            dy = p_doc[1] - point[1]
                            m11 = m22 = (h + dy) / h
                            dx = -(bbox[2] * m11 - bbox[2])
                            dy = -(bbox[1] * m22 - bbox[1])
                            snap[1] = self.snap.active_snap[1]
                    self.snap.active_snap = snap
                    # ---- snapping
        if mark == 1:
            dy = end_point[1] - start_point[1]
            if shift:
                m22 = (h + 2.0 * dy) / h
                dy = -(bbox[1] * m22 - bbox[1]) - h * (m22 - 1.0) / 2.0
                # ---- snapping
                point = middle_points[1]
                trafo = [m11, m21, m12, m22, dx, dy]
                p = libgeom.apply_trafo_to_point(point, trafo)
                f, p, p_doc = self.snap.snap_point(p, False)
                dy = p_doc[1] - point[1]
                m22 = (h + 2.0 * dy) / h
                dy = -(bbox[1] * m22 - bbox[1]) - h * (m22 - 1.0) / 2.0
            # ---- snapping
            else:
                m22 = (h + dy) / h
                dy = -(bbox[1] * m22 - bbox[1])
                # ---- snapping
                point = middle_points[1]
                trafo = [m11, m21, m12, m22, dx, dy]
                p = libgeom.apply_trafo_to_point(point, trafo)
                f, p, p_doc = self.snap.snap_point(p, False)
                dy = p_doc[1] - point[1]
                m22 = (h + dy) / h
                dy = -(bbox[1] * m22 - bbox[1])
                # ---- snapping
        if mark == 2:
            dx = end_point[0] - start_point[0]
            dy = end_point[1] - start_point[1]
            if shift:
                if control:
                    m11 = (w + 2.0 * dx) / w
                    m22 = (h + 2.0 * dy) / h
                    dx = -(bbox[0] * m11 - bbox[0]) - w * (m11 - 1.0) / 2.0
                    dy = -(bbox[1] * m22 - bbox[1]) - h * (m22 - 1.0) / 2.0
                    # ---- snapping
                    point = middle_points[2]
                    trafo = [m11, m21, m12, m22, dx, dy]
                    p = libgeom.apply_trafo_to_point(point, trafo)
                    f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
                    if f:
                        dx = p_doc[0] - point[0]
                        m11 = (w + 2.0 * dx) / w
                        dx = -(bbox[0] * m11 - bbox[0]) - w * (m11 - 1.0) / 2.0
                        snap[0] = self.snap.active_snap[0]
                    point = middle_points[1]
                    trafo = [m11, m21, m12, m22, dx, dy]
                    p = libgeom.apply_trafo_to_point(point, trafo)
                    f, p, p_doc = self.snap.snap_point(p, False, snap_x=False)
                    if f:
                        dy = p_doc[1] - point[1]
                        m22 = (h + 2.0 * dy) / h
                        dy = -(bbox[1] * m22 - bbox[1]) - h * (m22 - 1.0) / 2.0
                        snap[1] = self.snap.active_snap[1]
                    self.snap.active_snap = snap
                # ---- snapping
                else:
                    if abs(dx) < abs(dy):
                        m11 = m22 = (w + 2.0 * dx) / w
                    else:
                        m11 = m22 = (h + 2.0 * dy) / h
                    dx = -(bbox[0] * m11 - bbox[0]) - w * (m11 - 1.0) / 2.0
                    dy = -(bbox[1] * m22 - bbox[1]) - h * (m22 - 1.0) / 2.0
                    # ---- snapping
                    point = middle_points[2]
                    trafo = [m11, m21, m12, m22, dx, dy]
                    p = libgeom.apply_trafo_to_point(point, trafo)
                    f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
                    if f:
                        dx = p_doc[0] - point[0]
                        m11 = m22 = (w + 2.0 * dx) / w
                        dx = -(bbox[0] * m11 - bbox[0]) - w * (m11 - 1.0) / 2.0
                        dy = -(bbox[1] * m22 - bbox[1]) - h * (m22 - 1.0) / 2.0
                        snap[0] = self.snap.active_snap[0]
                    else:
                        point = middle_points[1]
                        trafo = [m11, m21, m12, m22, dx, dy]
                        p = libgeom.apply_trafo_to_point(point, trafo)
                        f, p, p_doc = self.snap.snap_point(p,
                                                           False,
                                                           snap_x=False)
                        if f:
                            dy = p_doc[1] - point[1]
                            m11 = m22 = (h + 2.0 * dy) / h
                            dx = -(bbox[0] * m11 - bbox[0]) - w * (m11 -
                                                                   1.0) / 2.0
                            dy = -(bbox[1] * m22 - bbox[1]) - h * (m22 -
                                                                   1.0) / 2.0
                            snap[1] = self.snap.active_snap[1]
                    self.snap.active_snap = snap
                    # ---- snapping
            else:
                if control:
                    m11 = (w + dx) / w
                    m22 = (h + dy) / h
                    dx = -(bbox[0] * m11 - bbox[0])
                    dy = -(bbox[1] * m22 - bbox[1])
                    # ---- snapping
                    point = middle_points[2]
                    trafo = [m11, m21, m12, m22, dx, dy]
                    p = libgeom.apply_trafo_to_point(point, trafo)
                    f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
                    if f:
                        dx = p_doc[0] - point[0]
                        m11 = (w + dx) / w
                        dx = -(bbox[0] * m11 - bbox[0])
                        snap[0] = self.snap.active_snap[0]
                    point = middle_points[1]
                    trafo = [m11, m21, m12, m22, dx, dy]
                    p = libgeom.apply_trafo_to_point(point, trafo)
                    f, p, p_doc = self.snap.snap_point(p, False, snap_x=False)
                    if f:
                        dy = p_doc[1] - point[1]
                        m22 = (h + dy) / h
                        dy = -(bbox[1] * m22 - bbox[1])
                        snap[1] = self.snap.active_snap[1]
                    self.snap.active_snap = snap
                # ---- snapping
                else:
                    if abs(dx) < abs(dy):
                        m11 = m22 = (w + dx) / w
                    else:
                        m11 = m22 = (h + dy) / h
                    dx = -(bbox[0] * m11 - bbox[0])
                    dy = -(bbox[1] * m22 - bbox[1])
                    # ---- snapping
                    point = middle_points[2]
                    trafo = [m11, m21, m12, m22, dx, dy]
                    p = libgeom.apply_trafo_to_point(point, trafo)
                    f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
                    if f:
                        dx = p_doc[0] - point[0]
                        m11 = m22 = (w + dx) / w
                        dx = -(bbox[0] * m11 - bbox[0])
                        dy = -(bbox[1] * m22 - bbox[1])
                        snap[0] = self.snap.active_snap[0]
                    else:
                        point = middle_points[1]
                        trafo = [m11, m21, m12, m22, dx, dy]
                        p = libgeom.apply_trafo_to_point(point, trafo)
                        f, p, p_doc = self.snap.snap_point(p,
                                                           False,
                                                           snap_x=False)
                        if f:
                            dy = p_doc[1] - point[1]
                            m11 = m22 = (h + dy) / h
                            dx = -(bbox[0] * m11 - bbox[0])
                            dy = -(bbox[1] * m22 - bbox[1])
                            snap[1] = self.snap.active_snap[1]
                    self.snap.active_snap = snap
                    # ---- snapping
        if mark == 3:
            dx = start_point[0] - end_point[0]
            if shift:
                m11 = (w + 2.0 * dx) / w
                dx = -(bbox[2] * m11 - bbox[2]) + w * (m11 - 1.0) / 2.0
                # ---- snapping
                point = middle_points[0]
                trafo = [m11, m21, m12, m22, dx, dy]
                p = libgeom.apply_trafo_to_point(point, trafo)
                f, p, p_doc = self.snap.snap_point(p, False)
                dx = point[0] - p_doc[0]
                m11 = (w + 2.0 * dx) / w
                dx = -(bbox[2] * m11 - bbox[2]) + w * (m11 - 1.0) / 2.0
            # ---- snapping
            else:
                m11 = (w + dx) / w
                dx = -(bbox[2] * m11 - bbox[2])
                # ---- snapping
                point = middle_points[0]
                trafo = [m11, m21, m12, m22, dx, dy]
                p = libgeom.apply_trafo_to_point(point, trafo)
                f, p, p_doc = self.snap.snap_point(p, False)
                dx = point[0] - p_doc[0]
                m11 = (w + dx) / w
                dx = -(bbox[2] * m11 - bbox[2])
                # ---- snapping
        if mark == 5:
            dx = end_point[0] - start_point[0]
            if shift:
                m11 = (w + 2.0 * dx) / w
                dx = -(bbox[0] * m11 - bbox[0]) - w * (m11 - 1.0) / 2.0
                # ---- snapping
                point = middle_points[2]
                trafo = [m11, m21, m12, m22, dx, dy]
                p = libgeom.apply_trafo_to_point(point, trafo)
                f, p, p_doc = self.snap.snap_point(p, False)
                dx = p_doc[0] - point[0]
                m11 = (w + 2.0 * dx) / w
                dx = -(bbox[0] * m11 - bbox[0]) - w * (m11 - 1.0) / 2.0
            # ---- snapping
            else:
                m11 = (w + dx) / w
                dx = -(bbox[0] * m11 - bbox[0])
                # ---- snapping
                point = middle_points[2]
                trafo = [m11, m21, m12, m22, dx, dy]
                p = libgeom.apply_trafo_to_point(point, trafo)
                f, p, p_doc = self.snap.snap_point(p, False)
                dx = p_doc[0] - point[0]
                m11 = (w + dx) / w
                dx = -(bbox[0] * m11 - bbox[0])
                # ---- snapping
        if mark == 6:
            dx = start_point[0] - end_point[0]
            dy = start_point[1] - end_point[1]
            if shift:
                if control:
                    m11 = (w + 2.0 * dx) / w
                    m22 = (h + 2.0 * dy) / h
                    dx = -(bbox[2] * m11 - bbox[2]) + w * (m11 - 1.0) / 2.0
                    dy = -(bbox[3] * m22 - bbox[3]) + h * (m22 - 1.0) / 2.0
                    # ---- snapping
                    point = middle_points[0]
                    trafo = [m11, m21, m12, m22, dx, dy]
                    p = libgeom.apply_trafo_to_point(point, trafo)
                    f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
                    if f:
                        dx = point[0] - p_doc[0]
                        m11 = (w + 2.0 * dx) / w
                        dx = -(bbox[2] * m11 - bbox[2]) + w * (m11 - 1.0) / 2.0
                        snap[0] = self.snap.active_snap[0]
                    point = middle_points[3]
                    trafo = [m11, m21, m12, m22, dx, dy]
                    p = libgeom.apply_trafo_to_point(point, trafo)
                    f, p, p_doc = self.snap.snap_point(p, False, snap_x=False)
                    if f:
                        dy = point[1] - p_doc[1]
                        m22 = (h + 2.0 * dy) / h
                        dy = -(bbox[3] * m22 - bbox[3]) + h * (m22 - 1.0) / 2.0
                        snap[1] = self.snap.active_snap[1]
                    self.snap.active_snap = snap
                # ---- snapping
                else:
                    if abs(dx) < abs(dy):
                        m11 = m22 = (w + 2.0 * dx) / w
                    else:
                        m11 = m22 = (h + 2.0 * dy) / h
                    dx = -(bbox[2] * m11 - bbox[2]) + w * (m11 - 1.0) / 2.0
                    dy = -(bbox[3] * m22 - bbox[3]) + h * (m22 - 1.0) / 2.0
                    # ---- snapping
                    point = middle_points[0]
                    trafo = [m11, m21, m12, m22, dx, dy]
                    p = libgeom.apply_trafo_to_point(point, trafo)
                    f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
                    if f:
                        dx = point[0] - p_doc[0]
                        m11 = m22 = (w + 2.0 * dx) / w
                        dx = -(bbox[2] * m11 - bbox[2]) + w * (m11 - 1.0) / 2.0
                        dy = -(bbox[3] * m22 - bbox[3]) + h * (m22 - 1.0) / 2.0
                        snap[0] = self.snap.active_snap[0]
                    else:
                        point = middle_points[3]
                        trafo = [m11, m21, m12, m22, dx, dy]
                        p = libgeom.apply_trafo_to_point(point, trafo)
                        f, p, p_doc = self.snap.snap_point(p,
                                                           False,
                                                           snap_x=False)
                        if f:
                            dy = point[1] - p_doc[1]
                            m11 = m22 = (h + 2.0 * dy) / h
                            dx = -(bbox[2] * m11 - bbox[2]) + w * (m11 -
                                                                   1.0) / 2.0
                            dy = -(bbox[3] * m22 - bbox[3]) + h * (m22 -
                                                                   1.0) / 2.0
                            snap[1] = self.snap.active_snap[1]
                    self.snap.active_snap = snap
                    # ---- snapping
            else:
                if control:
                    m11 = (w + dx) / w
                    m22 = (h + dy) / h
                    dx = -(bbox[2] * m11 - bbox[2])
                    dy = -(bbox[3] * m22 - bbox[3])
                    # ---- snapping
                    point = middle_points[0]
                    trafo = [m11, m21, m12, m22, dx, dy]
                    p = libgeom.apply_trafo_to_point(point, trafo)
                    f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
                    if f:
                        dx = point[0] - p_doc[0]
                        m11 = (w + dx) / w
                        dx = -(bbox[2] * m11 - bbox[2])
                        snap[0] = self.snap.active_snap[0]
                    point = middle_points[3]
                    trafo = [m11, m21, m12, m22, dx, dy]
                    p = libgeom.apply_trafo_to_point(point, trafo)
                    f, p, p_doc = self.snap.snap_point(p, False, snap_x=False)
                    if f:
                        dy = point[1] - p_doc[1]
                        m22 = (h + dy) / h
                        dy = -(bbox[3] * m22 - bbox[3])
                        snap[1] = self.snap.active_snap[1]
                    self.snap.active_snap = snap
                # ---- snapping
                else:
                    if abs(dx) < abs(dy):
                        m11 = m22 = (w + dx) / w
                    else:
                        m11 = m22 = (h + dy) / h
                    dx = -(bbox[2] * m11 - bbox[2])
                    dy = -(bbox[3] * m22 - bbox[3])
                    # ---- snapping
                    point = middle_points[0]
                    trafo = [m11, m21, m12, m22, dx, dy]
                    p = libgeom.apply_trafo_to_point(point, trafo)
                    f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
                    if f:
                        dx = point[0] - p_doc[0]
                        m11 = m22 = (w + dx) / w
                        dx = -(bbox[2] * m11 - bbox[2])
                        dy = -(bbox[3] * m22 - bbox[3])
                        snap[0] = self.snap.active_snap[0]
                    else:
                        point = middle_points[3]
                        trafo = [m11, m21, m12, m22, dx, dy]
                        p = libgeom.apply_trafo_to_point(point, trafo)
                        f, p, p_doc = self.snap.snap_point(p,
                                                           False,
                                                           snap_x=False)
                        if f:
                            dy = point[1] - p_doc[1]
                            m11 = m22 = (h + dy) / h
                            dx = -(bbox[2] * m11 - bbox[2])
                            dy = -(bbox[3] * m22 - bbox[3])
                            snap[1] = self.snap.active_snap[1]
                    self.snap.active_snap = snap
                    # ---- snapping
        if mark == 7:
            dy = start_point[1] - end_point[1]
            if shift:
                m22 = (h + 2.0 * dy) / h
                dy = -(bbox[3] * m22 - bbox[3]) + h * (m22 - 1.0) / 2.0
                # ---- snapping
                point = middle_points[3]
                trafo = [m11, m21, m12, m22, dx, dy]
                p = libgeom.apply_trafo_to_point(point, trafo)
                f, p, p_doc = self.snap.snap_point(p, False)
                dy = point[1] - p_doc[1]
                m22 = (h + 2.0 * dy) / h
                dy = -(bbox[3] * m22 - bbox[3]) + h * (m22 - 1.0) / 2.0
            # ---- snapping
            else:
                m22 = (h + dy) / h
                dy = -(bbox[3] * m22 - bbox[3])
                # ---- snapping
                point = middle_points[3]
                trafo = [m11, m21, m12, m22, dx, dy]
                p = libgeom.apply_trafo_to_point(point, trafo)
                f, p, p_doc = self.snap.snap_point(p, False)
                dy = point[1] - p_doc[1]
                m22 = (h + dy) / h
                dy = -(bbox[3] * m22 - bbox[3])
                # ---- snapping
        if mark == 8:
            dx = end_point[0] - start_point[0]
            dy = start_point[1] - end_point[1]
            if shift:
                if control:
                    m11 = (w + 2.0 * dx) / w
                    m22 = (h + 2.0 * dy) / h
                    dx = -(bbox[0] * m11 - bbox[0]) - w * (m11 - 1.0) / 2.0
                    dy = -(bbox[3] * m22 - bbox[3]) + h * (m22 - 1.0) / 2.0
                    # ---- snapping
                    point = middle_points[2]
                    trafo = [m11, m21, m12, m22, dx, dy]
                    p = libgeom.apply_trafo_to_point(point, trafo)
                    f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
                    if f:
                        dx = p_doc[0] - point[0]
                        m11 = (w + 2.0 * dx) / w
                        dx = -(bbox[0] * m11 - bbox[0]) - w * (m11 - 1.0) / 2.0
                        snap[0] = self.snap.active_snap[0]
                    point = middle_points[3]
                    trafo = [m11, m21, m12, m22, dx, dy]
                    p = libgeom.apply_trafo_to_point(point, trafo)
                    f, p, p_doc = self.snap.snap_point(p, False, snap_x=False)
                    if f:
                        dy = point[1] - p_doc[1]
                        m22 = (h + 2.0 * dy) / h
                        dy = -(bbox[3] * m22 - bbox[3]) + h * (m22 - 1.0) / 2.0
                        snap[1] = self.snap.active_snap[1]
                    self.snap.active_snap = snap
                # ---- snapping
                else:
                    if abs(dx) < abs(dy):
                        m11 = m22 = (w + 2.0 * dx) / w
                    else:
                        m11 = m22 = (h + 2.0 * dy) / h
                    dx = -(bbox[0] * m11 - bbox[0]) - w * (m11 - 1.0) / 2.0
                    dy = -(bbox[3] * m22 - bbox[3]) + h * (m22 - 1.0) / 2.0
                    # ---- snapping
                    point = middle_points[2]
                    trafo = [m11, m21, m12, m22, dx, dy]
                    p = libgeom.apply_trafo_to_point(point, trafo)
                    f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
                    if f:
                        dx = p_doc[0] - point[0]
                        m11 = m22 = (w + 2.0 * dx) / w
                        dx = -(bbox[0] * m11 - bbox[0]) - w * (m11 - 1.0) / 2.0
                        dy = -(bbox[3] * m22 - bbox[3]) + h * (m22 - 1.0) / 2.0
                        snap[0] = self.snap.active_snap[0]
                    else:
                        point = middle_points[3]
                        trafo = [m11, m21, m12, m22, dx, dy]
                        p = libgeom.apply_trafo_to_point(point, trafo)
                        f, p, p_doc = self.snap.snap_point(p,
                                                           False,
                                                           snap_x=False)
                        if f:
                            dy = point[1] - p_doc[1]
                            m11 = m22 = (h + 2.0 * dy) / h
                            dx = -(bbox[0] * m11 - bbox[0]) - w * (m11 -
                                                                   1.0) / 2.0
                            dy = -(bbox[3] * m22 - bbox[3]) + h * (m22 -
                                                                   1.0) / 2.0
                            snap[1] = self.snap.active_snap[1]
                    self.snap.active_snap = snap
                    # ---- snapping
            else:
                if control:
                    m11 = (w + dx) / w
                    m22 = (h + dy) / h
                    dx = -(bbox[0] * m11 - bbox[0])
                    dy = -(bbox[3] * m22 - bbox[3])
                    # ---- snapping
                    point = middle_points[2]
                    trafo = [m11, m21, m12, m22, dx, dy]
                    p = libgeom.apply_trafo_to_point(point, trafo)
                    f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
                    if f:
                        dx = p_doc[0] - point[0]
                        m11 = (w + dx) / w
                        dx = -(bbox[0] * m11 - bbox[0])
                        snap[0] = self.snap.active_snap[0]
                    point = middle_points[3]
                    trafo = [m11, m21, m12, m22, dx, dy]
                    p = libgeom.apply_trafo_to_point(point, trafo)
                    f, p, p_doc = self.snap.snap_point(p, False, snap_x=False)
                    if f:
                        dy = point[1] - p_doc[1]
                        m22 = (h + dy) / h
                        dy = -(bbox[3] * m22 - bbox[3])
                        snap[1] = self.snap.active_snap[1]
                    self.snap.active_snap = snap
                # ---- snapping
                else:
                    if abs(dx) < abs(dy):
                        m11 = m22 = (w + dx) / w
                    else:
                        m11 = m22 = (h + dy) / h
                    dx = -(bbox[0] * m11 - bbox[0])
                    dy = -(bbox[3] * m22 - bbox[3])
                    # ---- snapping
                    point = middle_points[2]
                    trafo = [m11, m21, m12, m22, dx, dy]
                    p = libgeom.apply_trafo_to_point(point, trafo)
                    f, p, p_doc = self.snap.snap_point(p, False, snap_y=False)
                    if f:
                        dx = p_doc[0] - point[0]
                        m11 = m22 = (w + dx) / w
                        dx = -(bbox[0] * m11 - bbox[0])
                        dy = -(bbox[3] * m22 - bbox[3])
                        snap[0] = self.snap.active_snap[0]
                    else:
                        point = middle_points[3]
                        trafo = [m11, m21, m12, m22, dx, dy]
                        p = libgeom.apply_trafo_to_point(point, trafo)
                        f, p, p_doc = self.snap.snap_point(p,
                                                           False,
                                                           snap_x=False)
                        if f:
                            dy = point[1] - p_doc[1]
                            m11 = m22 = (h + dy) / h
                            dx = -(bbox[0] * m11 - bbox[0])
                            dy = -(bbox[3] * m22 - bbox[3])
                            snap[1] = self.snap.active_snap[1]
                    self.snap.active_snap = snap
                    # ---- snapping

        if mark == 11:
            change_x = end_point[0] - start_point[0]
            m12 = change_x / h
            dx = -bbox[1] * m12
        if mark == 16:
            change_x = start_point[0] - end_point[0]
            m12 = change_x / h
            dx = -bbox[3] * m12
        if mark == 13:
            change_y = start_point[1] - end_point[1]
            m21 = change_y / w
            dy = -bbox[2] * m21
        if mark == 14:
            change_y = end_point[1] - start_point[1]
            m21 = change_y / w
            dy = -bbox[0] * m21

        if mark in (10, 12, 15, 17):
            x0, y0 = bbox[:2]
            shift_x, shift_y = self.selection.center_offset
            center_x = x0 + w / 2.0 + shift_x
            center_y = y0 + h / 2.0 + shift_y
            a1 = math.atan2(start_point[1] - center_y,
                            start_point[0] - center_x)
            a2 = math.atan2(end_point[1] - center_y, end_point[0] - center_x)
            angle = a2 - a1
            if control:
                step = config.rotation_step * math.pi / 180.0
                angle = round(angle / step) * step
            m21 = math.sin(angle)
            m11 = m22 = math.cos(angle)
            m12 = -m21
            dx = center_x - m11 * center_x + m21 * center_y
            dy = center_y - m21 * center_x - m11 * center_y

        m11 = m11 or .000001
        m22 = m22 or .000001
        return [m11, m21, m12, m22, dx, dy]
Example #9
0
    def process_fill(self, ctx, obj):
        fill = obj.style[0]
        fill_rule = fill[0]
        if fill_rule & sk2const.FILL_CLOSED_ONLY and not obj.is_closed():
            ctx.set_source_rgba(0.0, 0.0, 0.0, 0.0)
            return
        if fill_rule & sk2const.FILL_EVENODD:
            ctx.set_fill_rule(cairo.FILL_RULE_EVEN_ODD)
        else:
            ctx.set_fill_rule(cairo.FILL_RULE_WINDING)
        if fill[1] == sk2const.FILL_SOLID:
            if obj.fill_trafo:
                obj.fill_trafo = []
            color = fill[2]
            ctx.set_source_rgba(*self.get_color(color))
        elif fill[1] == sk2const.FILL_GRADIENT:
            if not obj.fill_trafo:
                obj.fill_trafo = [] + sk2const.NORMAL_TRAFO
            gradient = fill[2]
            points = gradient[1]
            if not points:
                obj.fill_trafo = [] + sk2const.NORMAL_TRAFO
                points = libgeom.bbox_middle_points(obj.cache_bbox)
                if gradient[0] == sk2const.GRADIENT_LINEAR:
                    points = [points[0], points[2]]
                else:
                    points = [[points[1][0], points[2][1]], points[2]]
                gradient[1] = points
            coords = points[0] + points[1]
            if gradient[0] == sk2const.GRADIENT_LINEAR:
                grd = cairo.LinearGradient(*coords)
            else:
                x0, y0 = coords[:2]
                radius = libgeom.distance(*points)
                grd = cairo.RadialGradient(x0, y0, 0, x0, y0, radius)
            for stop in gradient[2]:
                grd.add_color_stop_rgba(stop[0], *self.get_color(stop[1]))
            matrix = cairo.Matrix(*obj.fill_trafo)
            matrix.invert()
            extend = cairo.EXTEND_PAD
            if len(gradient) > 3:
                extend = EXTEND[gradient[3]]
            grd.set_extend(extend)
            grd.set_matrix(matrix)
            ctx.set_source(grd)
        elif fill[1] == sk2const.FILL_PATTERN:
            if not obj.fill_trafo:
                obj.fill_trafo = [] + sk2const.NORMAL_TRAFO
                obj.fill_trafo = obj.fill_trafo[:4] + [
                    obj.cache_bbox[0], obj.cache_bbox[3]
                ]
            pattern_fill = fill[2]
            sp = cairo.SurfacePattern(self.get_pattern_surface(obj))
            sp.set_extend(cairo.EXTEND_REPEAT)
            flip_matrix = cairo.Matrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0)
            if len(pattern_fill) > 3:
                pattern_matrix = cairo.Matrix(*pattern_fill[3])
                pattern_matrix.invert()
                flip_matrix = flip_matrix * pattern_matrix
            trafo_matrix = cairo.Matrix(*obj.fill_trafo)
            trafo_matrix.invert()
            flip_matrix = flip_matrix * trafo_matrix
            sp.set_matrix(flip_matrix)
            ctx.set_source(sp)

            canvas_matrix = ctx.get_matrix()
            canvas_trafo = libcairo.get_trafo_from_matrix(canvas_matrix)
            zoom = canvas_trafo[0]
            if zoom * abs(obj.fill_trafo[0]) > .98:
                ctx.get_source().set_filter(cairo.FILTER_NEAREST)
Example #10
0
	def process_fill(self, ctx, obj):
		fill = obj.style[0]
		fill_rule = fill[0]
		if fill_rule & sk2_const.FILL_CLOSED_ONLY and not obj.is_closed():
			ctx.set_source_rgba(0.0, 0.0, 0.0, 0.0)
			return
		if fill_rule & sk2_const.FILL_EVENODD:
			ctx.set_fill_rule(cairo.FILL_RULE_EVEN_ODD)
		else:
			ctx.set_fill_rule(cairo.FILL_RULE_WINDING)
		if fill[1] == sk2_const.FILL_SOLID:
			if obj.fill_trafo: obj.fill_trafo = []
			color = fill[2]
			ctx.set_source_rgba(*self.get_color(color))
		elif fill[1] == sk2_const.FILL_GRADIENT:
			if not obj.fill_trafo:
				obj.fill_trafo = [] + sk2_const.NORMAL_TRAFO
			gradient = fill[2]
			points = gradient[1]
			if not points:
				obj.fill_trafo = [] + sk2_const.NORMAL_TRAFO
				points = libgeom.bbox_middle_points(obj.cache_bbox)
				if gradient[0] == sk2_const.GRADIENT_LINEAR:
					points = [points[0], points[2]]
				else:
					points = [[points[1][0], points[2][1]], points[2]]
				gradient[1] = points
			coords = points[0] + points[1]
			if gradient[0] == sk2_const.GRADIENT_LINEAR:
				grd = cairo.LinearGradient(*coords)
			else:
				x0, y0 = coords[:2]
				radius = libgeom.distance(*points)
				grd = cairo.RadialGradient(x0, y0, 0, x0, y0, radius)
			for stop in gradient[2]:
				grd.add_color_stop_rgba(stop[0], *self.get_color(stop[1]))
			matrix = cairo.Matrix(*obj.fill_trafo)
			matrix.invert()
			grd.set_matrix(matrix)
			ctx.set_source(grd)
		elif fill[1] == sk2_const.FILL_PATTERN:
			if not obj.fill_trafo:
				obj.fill_trafo = [] + sk2_const.NORMAL_TRAFO
				obj.fill_trafo = obj.fill_trafo[:4] + \
					[obj.cache_bbox[0], obj.cache_bbox[3]]
			pattern_fill = fill[2]
			if not obj.cache_pattern_img:
				bmpstr = b64decode(pattern_fill[1])
				image_obj = model.Pixmap(obj.config)
				libimg.set_image_data(self.cms, image_obj, bmpstr)
				libimg.flip_top_to_bottom(image_obj)
				if pattern_fill[0] == sk2_const.PATTERN_IMG and \
				 len(pattern_fill) > 2:
					image_obj.style[3] = deepcopy(pattern_fill[2])
				libimg.update_image(self.cms, image_obj)
				obj.cache_pattern_img = image_obj.cache_cdata
				image_obj.cache_cdata = None
			sp = cairo.SurfacePattern(obj.cache_pattern_img)
			sp.set_extend(cairo.EXTEND_REPEAT)
			flip_matrix = cairo.Matrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0)
			if len(pattern_fill) > 3:
				pattern_matrix = cairo.Matrix(*pattern_fill[3])
				pattern_matrix.invert()
				flip_matrix = flip_matrix * pattern_matrix
			trafo_matrix = cairo.Matrix(*obj.fill_trafo)
			trafo_matrix.invert()
			flip_matrix = flip_matrix * trafo_matrix
			sp.set_matrix(flip_matrix)
			ctx.set_source(sp)

			canvas_matrix = ctx.get_matrix()
			canvas_trafo = libcairo.get_trafo_from_matrix(canvas_matrix)
			zoom = canvas_trafo[0]
			if zoom * abs(obj.fill_trafo[0]) > .98:
				ctx.get_source().set_filter(cairo.FILTER_NEAREST)
Example #11
0
    def process_fill(self, ctx, obj):
        fill = obj.style[0]
        fill_rule = fill[0]
        if fill_rule & sk2_const.FILL_CLOSED_ONLY and not obj.is_closed():
            ctx.set_source_rgba(0.0, 0.0, 0.0, 0.0)
            return
        if fill_rule & sk2_const.FILL_EVENODD:
            ctx.set_fill_rule(cairo.FILL_RULE_EVEN_ODD)
        else:
            ctx.set_fill_rule(cairo.FILL_RULE_WINDING)
        if fill[1] == sk2_const.FILL_SOLID:
            if obj.fill_trafo: obj.fill_trafo = []
            color = fill[2]
            ctx.set_source_rgba(*self.get_color(color))
        elif fill[1] == sk2_const.FILL_GRADIENT:
            if not obj.fill_trafo:
                obj.fill_trafo = [] + sk2_const.NORMAL_TRAFO
            gradient = fill[2]
            points = gradient[1]
            if not points:
                obj.fill_trafo = [] + sk2_const.NORMAL_TRAFO
                points = libgeom.bbox_middle_points(obj.cache_bbox)
                if gradient[0] == sk2_const.GRADIENT_LINEAR:
                    points = [points[0], points[2]]
                else:
                    points = [[points[1][0], points[2][1]], points[2]]
                gradient[1] = points
            coords = points[0] + points[1]
            if gradient[0] == sk2_const.GRADIENT_LINEAR:
                grd = cairo.LinearGradient(*coords)
            else:
                x0, y0 = coords[:2]
                radius = libgeom.distance(*points)
                grd = cairo.RadialGradient(x0, y0, 0, x0, y0, radius)
            for stop in gradient[2]:
                grd.add_color_stop_rgba(stop[0], *self.get_color(stop[1]))
            matrix = cairo.Matrix(*obj.fill_trafo)
            matrix.invert()
            grd.set_matrix(matrix)
            ctx.set_source(grd)
        elif fill[1] == sk2_const.FILL_PATTERN:
            if not obj.fill_trafo:
                obj.fill_trafo = [] + sk2_const.NORMAL_TRAFO
                obj.fill_trafo = obj.fill_trafo[:4] + \
                 [obj.cache_bbox[0], obj.cache_bbox[3]]
            pattern_fill = fill[2]
            if not obj.cache_pattern_img:
                bmpstr = b64decode(pattern_fill[1])
                image_obj = model.Pixmap(obj.config)
                libimg.set_image_data(self.cms, image_obj, bmpstr)
                libimg.flip_top_to_bottom(image_obj)
                if pattern_fill[0] == sk2_const.PATTERN_IMG and \
                 len(pattern_fill) > 2:
                    image_obj.style[3] = deepcopy(pattern_fill[2])
                libimg.update_image(self.cms, image_obj)
                obj.cache_pattern_img = image_obj.cache_cdata
                image_obj.cache_cdata = None
            sp = cairo.SurfacePattern(obj.cache_pattern_img)
            sp.set_extend(cairo.EXTEND_REPEAT)
            flip_matrix = cairo.Matrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0)
            if len(pattern_fill) > 3:
                pattern_matrix = cairo.Matrix(*pattern_fill[3])
                pattern_matrix.invert()
                flip_matrix = flip_matrix * pattern_matrix
            trafo_matrix = cairo.Matrix(*obj.fill_trafo)
            trafo_matrix.invert()
            flip_matrix = flip_matrix * trafo_matrix
            sp.set_matrix(flip_matrix)
            ctx.set_source(sp)

            canvas_matrix = ctx.get_matrix()
            canvas_trafo = libcairo.get_trafo_from_matrix(canvas_matrix)
            zoom = canvas_trafo[0]
            if zoom * abs(obj.fill_trafo[0]) > .98:
                ctx.get_source().set_filter(cairo.FILTER_NEAREST)