def SkewSelected(self, axisX=0, axisY=0): if self.document.selection: self.document.begin_transaction() try: try: br = self.document.selection.coord_rect hor_sel = br.right - br.left ver_sel = br.top - br.bottom cnt_x, cnt_y = self.Basepoint.get_basepoint( hor_sel, ver_sel, br.left, br.bottom) text = _("Skew") ax, ay = tan(axisX), tan(axisY) sx = 1.0 sy = 1.0 - (ax * ay) tx = cnt_x * ax ty = cnt_y * ax * ay - cnt_y * ay # Move the selection in the coordinates x0 y0 trafo = Trafo(1, 0, 0, 1, -cnt_x, -cnt_y) # Skew and Scaling trafo = Trafo(sx, ay, -ax, sy, 0, 0)(trafo) # Move the selection in the coordinates basepoint trafo = Trafo(1, 0, 0, 1, cnt_x, cnt_y)(trafo) self.document.TransformSelected(trafo, text) except: self.document.abort_transaction() finally: self.document.end_transaction()
def compute_trafo(self, state=0): sel = self.selection if sel in self.selTurn: # rotation vec = self.drag_cur - self.center angle = math.atan2(vec.y, vec.x) angle = angle - self.start_angle + 2 * math.pi if state & const.ConstraintMask: pi12 = math.pi / 12 angle = pi12 * int(angle / pi12 + 0.5) self.trafo = Rotation(angle, self.center) self.trafo_desc = (1, angle) elif sel in self.selShear: if sel in (2, 6): # horiz. shear height = self.drag_start.y - self.reference if height: ratio = self.off.x / height self.trafo = Trafo(1, 0, ratio, 1, -ratio * self.reference, 0) self.trafo_desc = (2, ratio) else: # vert. shear width = self.drag_start.x - self.reference if width: ratio = self.off.y / width self.trafo = Trafo(1, ratio, 0, 1, 0, -ratio * self.reference) self.trafo_desc = (3, ratio)
def __init__(self, file, filename, match): SimplifiedLoader.__init__(self, file, filename, match) self.layout = None self.format_version = atof(match.group('version')) self.trafo = Trafo(1.0, 0.0, 0.0, -1.0, 0.0, 800) self.colors = std_colors + [StandardColors.black] * 512 self.depths = {} # map object ids to depth self.guess_cont()
def make_gradient_pattern(self): name, trafo, start, end = self.gradient_geo self.gradient_geo = None type, array = self.gradients[name] array = array[:] if type == 0: # linear (axial) gradient origdir = end - start start = trafo(start) end = trafo(end) dir = end - start try: # adjust endpoint to accomodate trafo v = trafo.DTransform(origdir.y, -origdir.x).normalized() v = Point(v.y, -v.x) # rotate 90 degrees end = start + (v * dir) * v dir = end - start except ZeroDivisionError: pass trafo2 = Trafo(dir.x, dir.y, dir.y, -dir.x, start.x, start.y) trafo2 = trafo2.inverse() left, bottom, right, top = trafo2(self.current_bounding_rect()) if right > left: factor = 1 / (right - left) offset = -left * factor else: factor = 1 offset = 0 array = fix_gradient(array, factor, offset) pattern = LinearGradient(MultiGradient(array), (start - end).normalized()) elif type == 1: # radial gradient start = trafo(start) end = trafo(end) left, bottom, right, top = self.current_bounding_rect() if left == right or top == bottom: # an empty coord_rect???? center = Point(0, 0) else: center = Point((start.x - left) / (right - left), (start.y - bottom) / (top - bottom)) radius = max(hypot(left - start.x, top - start.y), hypot(right - start.x, top - start.y), hypot(right - start.x, bottom - start.y), hypot(left - start.x, bottom - start.y)) if radius: factor = -abs(start - end) / radius array = fix_gradient(array, factor, 1) pattern = RadialGradient(MultiGradient(array), center) else: self.add_message(_("Unknown gradient type %d"), type) pattern = EmptyPattern return pattern
def Snap(self, p): try: x, y = self.trafo.inverse()(p) minx = self.radius1 maxx = 1 - self.radius1 miny = self.radius2 maxy = 1 - self.radius2 if minx < x < maxx: if miny < y < maxy: ratio = hypot(self.trafo.m11, self.trafo.m21) \ / hypot(self.trafo.m12, self.trafo.m22) if x < 0.5: dx = x else: dx = 1 - x if y < 0.5: dy = y else: dy = 1 - y if dy / dx > ratio: x = round(x) else: y = round(y) elif y > maxy: y = 1 else: y = 0 elif miny < y < maxy: if x > maxx: x = 1 else: x = 0 elif minx > 0 and miny > 0: # the round corners if x < 0.5: cx = minx else: cx = maxx if y < 0.5: cy = miny else: cy = maxy trafo = Trafo(minx, 0, 0, miny, cx, cy) r, phi = trafo.inverse()(x, y).polar() x, y = trafo(Polar(1, phi)) else: # normal corners x = round(min(max(x, 0), 1)) y = round(min(max(y, 0), 1)) p2 = self.trafo(x, y) return (abs(p - p2), p2) except SingularMatrix: return (1e200, p)
def __init__(self, master, callback, width, height, comp=1, color=(0, 0, 0), **kw): self.callback = callback self.comp = comp self.win_to_color = Trafo(1, 0, 0, -1 / float(height - 1), 0, 1) self.color_to_win = self.win_to_color.inverse() apply(ChooseComponent.__init__, (self, master, width, height, color), kw)
def __init__(self, master, width, height, xcomp=0, ycomp=1, color=(0, 0, 0), **kw): self.xcomp = xcomp self.ycomp = ycomp self.win_to_color = Trafo(1 / float(width - 1), 0, 0, -1 / float(height - 1), 0, 1) self.color_to_win = self.win_to_color.inverse() apply(ChooseComponent.__init__, (self, master, width, height, color), kw)
class ChooseRGBXY(ChooseComponent): def __init__(self, master, width, height, xcomp=0, ycomp=1, color=(0, 0, 0), **kw): self.xcomp = xcomp self.ycomp = ycomp self.win_to_color = Trafo(1 / float(width - 1), 0, 0, -1 / float(height - 1), 0, 1) self.color_to_win = self.win_to_color.inverse() apply(ChooseComponent.__init__, (self, master, width, height, color), kw) def SetColor(self, color): color = apply(rgb_to_hsv, tuple(color)) otheridx = 3 - self.xcomp - self.ycomp if color[otheridx] != self.color[otheridx]: self.UpdateWhenIdle() self.hide_mark() self.color = color self.show_mark() def update_ramp(self): _sketch.fill_hsv_xy(self.image.im, self.xcomp, self.ycomp, self.color) self.set_image(self.image) def move_to(self, p, state): x, y = p if state & ConstraintMask: sx = self.drag_start[self.xcomp] sy = self.drag_start[self.ycomp] if abs(sx - x) < abs(sy - y): x = sx else: y = sy if x < 0: x = 0 elif x >= 1.0: x = 1.0 if y < 0: y = 0 elif y >= 1.0: y = 1.0 color = list(self.color) color[self.xcomp] = x color[self.ycomp] = y self.hide_mark() self.color = tuple(color) self.show_mark() self.issue(CHANGED, self.RGBColor()) def draw_mark(self): color = self.color w, h = self.image.size x, y = self.color_to_win(color[self.xcomp], color[self.ycomp]) x = int(x) y = int(y) self.invgc.DrawLine(x, 0, x, h) self.invgc.DrawLine(0, y, w, y)
def get_trafo(self, trfd, scale=1): cdr_version = self.cdr_version ieeestart = 32 if cdr_version >= 13: ieeestart = 40 if cdr_version == 5: ieeestart = 18 (x_shift, y_shift) = self.doc_page [var0] = struct.unpack('<d', trfd.data[ieeestart:ieeestart + 8]) [var1] = struct.unpack('<d', trfd.data[ieeestart + 8:ieeestart + 8 + 8]) [var2 ] = struct.unpack('<d', trfd.data[ieeestart + 2 * 8:ieeestart + 8 + 2 * 8]) [var3 ] = struct.unpack('<d', trfd.data[ieeestart + 3 * 8:ieeestart + 8 + 3 * 8]) [var4 ] = struct.unpack('<d', trfd.data[ieeestart + 4 * 8:ieeestart + 8 + 4 * 8]) [var5 ] = struct.unpack('<d', trfd.data[ieeestart + 5 * 8:ieeestart + 8 + 5 * 8]) self.scale_with = min(var0 * cmp(var0, 0), var4 * cmp(var4, 0)) return Trafo(var0, var3, var1, var4, (var2 + x_shift / 2) * scale, (var5 + y_shift / 2) * scale)
def load_arc(self): param = { '10': None, # X coordinat center '20': None, # Y coordinat center #'30': None, # Z coordinat center '40': 0.0, # radius '50': 0.0, # Start angle '51': 0.0 # End angle } param.update(self.general_param) param = self.read_param(param) cx = param['10'] cy = param['20'] rx = ry = param['40'] start_angle = param['50'] * degrees end_angle = param['51'] * degrees trafo = self.trafo(Trafo(rx, 0, 0, ry, cx, cy)) rx, w1, w2, ry, cx, cy = trafo.coeff() style = self.get_line_style(**param) self.prop_stack.AddStyle(style.Duplicate()) apply(self.ellipse, (rx, w1, w2, ry, cx, cy, start_angle, end_angle, ArcArc))
def gradient_geometry(self, flag, name, xorig, yorig, angle, length, a, b, c, d, tx, ty): trafo = Trafo(a, b, c, d, tx, ty) trafo = artboard_trafo_inv(trafo(artboard_trafo)) start = Point(xorig, yorig) end = start + Polar(length, (pi * angle) / 180.0) self.gradient_geo = (name, trafo, start, end)
def rect(self, attrs): if self.in_defs: return x, y = self.point(attrs.get('x', '0'), attrs.get('y', '0')) wx, wy = self.point(attrs['width'], "0", relative = 1) hx, hy = self.point("0", attrs['height'], relative = 1) t = Trafo(wx, wy, hx, hy, x, y) rx = ry = '0' if attrs.has_key('rx') and attrs.has_key('ry'): rx, ry = attrs['rx'], attrs['ry'] elif attrs.has_key('rx'): rx = ry = attrs['rx'] elif attrs.has_key('ry'): rx = ry = attrs['ry'] rx, ry = self.user_point(rx, ry) width, height = self.user_point(attrs['width'], attrs['height']) if width: rx = min(rx / width, 0.5) else: rx = 0 if height: ry = min(ry / height, 0.5) else: ry = 0 #wx, wy, hx, hy, x, y = t.coeff() self._print('rect', t) self.parse_attrs(attrs) self.set_loader_style() apply(self.loader.rectangle, (wx, wy, hx, hy, x, y, rx, ry))
def rectangle(self, m11, m21, m12, m22, v1, v2, radius1=0, radius2=0): trafo = Trafo(m11, m21, m12, m22, v1, v2) self.append_object( Rectangle(trafo, radius1=radius1, radius2=radius2, properties=self.get_prop_stack()))
def __init__(self, master, width, height, comp=1, color=(0, 0, 0), **kw): self.comp = comp self.win_to_color = Trafo(1, 0, 0, -1 / float(height - 1), 0, 1) self.color_to_win = self.win_to_color.inverse() apply(ChooseComponent.__init__, (self, master, width, height, color), kw)
def RemoveTransformation(self): if self.trafo.matrix() != IdentityMatrix: center = self.coord_rect.center() width, height = self.data.Size() trafo = Trafo(1, 0, 0, 1, center.x - width / 2, center.y - height / 2) return self.set_transformation(trafo) return NullUndo
def BoundingRect(self, pos, dir, width): angle = atan2(dir.y, dir.x) if width < 1.0: width = 1.0 s = width * sin(angle) c = width * cos(angle) trafo = Trafo(c, s, -s, c, pos.x, pos.y) return self.path.accurate_rect(trafo)
class ChooseRGBXY(ChooseComponent): def __init__(self, master, callback, width, height, xcomp=0, ycomp=1, color=(0, 0, 0), **kw): self.callback = callback self.xcomp = xcomp self.ycomp = ycomp self.win_to_color = Trafo(1 / float(width - 1), 0, 0, -1 / float(height - 1), 0, 1) self.color_to_win = self.win_to_color.inverse() apply(ChooseComponent.__init__, (self, master, width, height, color), kw) def SetColor(self, color): color = apply(rgb_to_hsv, tuple(color)) otheridx = 3 - self.xcomp - self.ycomp if color[otheridx] != self.color[otheridx]: self.UpdateWhenIdle() self.hide_mark() self.color = color self.show_mark() def update_ramp(self): _sketch.fill_hsv_xy(self.image.im, self.xcomp, self.ycomp, self.color) self.set_image(self.image) def move_to(self, p, state): x, y = p if state & ConstraintMask: sx = self.drag_start[self.xcomp] sy = self.drag_start[self.ycomp] if abs(sx - x) < abs(sy - y): x = sx else: y = sy if x < 0: x = 0 elif x >= 1.0: x = 1.0 if y < 0: y = 0 elif y >= 1.0: y = 1.0 color = list(self.color) color[self.xcomp] = x color[self.ycomp] = y self.hide_mark() self.color = tuple(color) self.show_mark() self.callback(self.getRGB()) # self.issue(CHANGED, self.RGBColor()) def draw_mark(self): color = self.color w, h = self.image.size x, y = self.color_to_win(color[self.xcomp], color[self.ycomp]) x = int(x) y = int(y) self.invgc.DrawLine(x, 0, x, h) self.invgc.DrawLine(0, y, w, y)
def ButtonUp(self, p, button, state): if self.state & AlternateMask: p = self.apply_constraint(p, state) self.DragStop(p) off = 2 * self.off end = self.trafo.offset() - self.off self.trafo = Trafo(off.x, 0, 0, off.y, end.x, end.y) else: RectangularCreator.ButtonUp(self, p, button, state)
def read_ellipse(self, line): readline = self.readline; tokenize = skread.tokenize_line args = tokenize(line) if len(args) != 19: raise SketchLoadError('Invalid Ellipse specification') sub_type, line_style, thickness, pen_color, fill_color, depth, \ pen_style, area_fill, style, direction, angle, \ cx, cy, rx, ry, sx, sy, ex, ey = args self.fill(fill_color, area_fill) self.line(pen_color, thickness, const.JoinMiter, const.CapButt, line_style, style) center = self.trafo(cx, cy); radius = self.trafo.DTransform(rx, ry) trafo = Trafo(radius.x, 0, 0, radius.y) trafo = Rotation(angle)(trafo) trafo = Translation(center)(trafo) apply(self.ellipse, trafo.coeff()) self.set_depth(depth)
def SaveDocument(self, doc): left, bottom, right, top = doc.BoundingRect() width = right - left height = top - bottom inch = 1440 x = max(width, height) if x * (inch / 72.) > 32767: inch = 32767 / x sc = inch / 72. self.trafo = Trafo(sc, 0, 0, -sc, -sc * left, sc * top) self.Scale = sc self.inch = inch self.extend = map( rndtoint, tuple(self.trafo(left, bottom)) + tuple(self.trafo(right, top))) self.numobj = self.idx = MIN_OBJECT self.objects = [] self.maxrecord = 0 self.cur_pen = -1 self.cur_brush = -1 # Header self.write_headers() # SetWindowOrg self.packrec('<LHhh', 5, 0x020B, self.extend[3], self.extend[0]) # SetWindowExt self.packrec('<LHhh', 5, 0x020C, self.extend[1], self.extend[2]) # SetBkMode to 1 (transparent) self.packrec('<LHh', 4, 0x0102, 1) # SetROP2 to 13 (R2_COPYPEN) # me self.packrec('<LHl', 5, 0x0104, 13) self.packrec('<LHh', 4, 0x0104, 13) # oo # CreatePenIndirect: 5 -- PS_NULL self.add_select_object( struct.pack('<LHhhhBBBx', 8, 0x02FA, 5, 0, 0, 0, 0, 0)) # CreateBrushIndirect: 1 -- BS_NULL self.add_select_object( struct.pack('<LHhBBBxh', 7, 0x02FC, 1, 0, 0, 0, 0)) self.SaveLayers(doc.Layers()) self.DeleteObject(0) self.DeleteObject(1) self.packrec('<LH', 3, 0) # terminator # update some fields self.write_headers()
def parse_transform(self, trafo_string): trafo = self.trafo trafo_string = as_latin1(trafo_string) while trafo_string: match = rx_trafo.match(trafo_string) if match: function = match.group(1) args = argsf = string.translate(match.group(2), commatospace) args = map(str, split(args)) trafo_string = trafo_string[match.end(0):] if function == 'matrix': args = map(float, split(argsf)) trafo = trafo(apply(Trafo, tuple(args))) elif function == 'scale': if len(args) == 1: sx = sy = args[0] else: sx, sy = args sx, sy = self.user_point(sx, sy) trafo = trafo(Scale(sx, sy)) elif function == 'translate': if len(args) == 1: dx, dy = args[0], '0' else: dx, dy = args dx, dy = self.user_point(dx, dy) trafo = trafo(Translation(dx, dy)) elif function == 'rotate': if len(args) == 1: trafo = trafo(Rotation(float(args[0]) * degrees)) else: angle, cx, cy = args cx, cy = self.user_point(cx, cy) trafo = trafo( Rotation(float(angle) * degrees, Point(cx, cy))) elif function == 'skewX': trafo = trafo( Trafo(1, 0, tan(float(args[0]) * degrees), 1, 0, 0)) elif function == 'skewY': trafo = trafo( Trafo(1, tan(float(args[0]) * degrees), 0, 1, 0, 0)) else: trafo_string = '' self.trafo = trafo
def circle(self, attrs): if self.in_defs: return x, y = self.user_point(attrs.get('cx', '0'), attrs.get('cy', '0')) r = self.user_point(attrs['r'], '0').x t = self.trafo(Trafo(r, 0, 0, r, x, y)) self._print('circle', t) self.parse_attrs(attrs) self.set_loader_style() apply(self.loader.ellipse, t.coeff())
def ellipse(self, attrs): if self.in_defs: return x, y = self.user_point(attrs.get('cx', '0'), attrs.get('cy', '0')) rx, ry = self.user_point(attrs['rx'], attrs['ry']) t = self.trafo(Trafo(rx, 0, 0, ry, x, y)) self._print('ellipse', t) self.parse_attrs(attrs) self.set_loader_style() apply(self.loader.ellipse, t.coeff())
def read_gimp_path(context, filename=''): if not filename: filename = context.application.GetOpenFilename() if not filename: return paths = read_path(filename) object = PolyBezier(paths) object.Transform(Trafo(1, 0, 0, -1, 0, 800)) #context.main_window.PlaceObject(object) context.document.Insert(object)
def coord_sys_at(lengths, pos, type): if len(lengths) < 2: return None for idx in range(len(lengths)): if lengths[idx][0] > pos: d2, p2 = lengths[idx] d1, p1 = lengths[idx - 1] if d2 != d1 and p1 != p2: break else: return None t = (pos - d1) / (d2 - d1) p = (1 - t) * p1 + t * p2 diff = (p2 - p1).normalized() del lengths[:idx - 1] if type == PATHTEXT_SKEW: return Trafo(diff.x, diff.y, 0, 1, p.x, p.y) else: return Trafo(diff.x, diff.y, -diff.y, diff.x, p.x, p.y)
def __init__(self, data=None, trafo=None, duplicate=None): if duplicate is not None: data = duplicate.data self.trafo = duplicate.trafo else: if trafo is None: #width, height = data.size trafo = Trafo(1, 0, 0, -1, 0, 0) self.trafo = trafo self.data = data
def Hit(self, p, rect, device, clip=0): a = self.properties llx, lly, urx, ury = a.font.TextBoundingBox(self.text, a.font_size, a) trafo = self.trafo(self.atrafo) trafo = trafo(Trafo(urx - llx, 0, 0, ury - lly, llx, lly)) return device.ParallelogramHit(p, trafo, 1, 1, 1, ignore_outline_mode=1)
def Hit(self, p, rect, device, clip=0): bbox = self.properties.font.text_bbox text = self.text; trafos = self.trafos for idx in range(len(trafos)): llx, lly, urx, ury = bbox(text[idx], self.properties) trafo = trafos[idx](Trafo(urx - llx, 0, 0, ury - lly, llx, lly)) if device.ParallelogramHit(p, trafo, 1, 1, 1, ignore_outline_mode=1): return 1 return 0
def __init__(self, master, callback, width, height, xcomp=0, ycomp=1, color=(0, 0, 0), **kw): self.callback = callback self.xcomp = xcomp self.ycomp = ycomp self.win_to_color = Trafo(1 / float(width - 1), 0, 0, -1 / float(height - 1), 0, 1) self.color_to_win = self.win_to_color.inverse() apply(ChooseComponent.__init__, (self, master, width, height, color), kw)
def update_trafo(self, scale = 1): EXT_hight = self.EXTMAX[0] - self.EXTMIN[0] EXT_width = self.EXTMAX[1] - self.EXTMIN[1] PEXT_hight = self.PEXTMAX[0] - self.PEXTMIN[0] PEXT_width = self.PEXTMAX[1] - self.PEXTMIN[1] if EXT_hight > 0: scale = 840 / max(EXT_hight, EXT_width) self.unit_to_pt = scale x = self.EXTMIN[0] * scale y = self.EXTMIN[1] * scale elif PEXT_hight > 0: scale = 840 / max(PEXT_hight, PEXT_width) self.unit_to_pt = scale x = self.PEXTMIN[0] * scale y = self.PEXTMIN[1] * scale else: x = 0 y = 0 self.trafo = Trafo(scale, 0, 0, scale, -x, -y)
def compute_trafo(self, state): start = self.drag_start end = self.drag_cur if state & AlternateMask: # start is the center of the ellipse if state & ConstraintMask: # end is a point of the periphery of a *circle* centered # at start radius = abs(start - end) self.trafo = Trafo(radius, 0, 0, radius, start.x, start.y) else: # end is a corner of the bounding box d = end - start self.trafo = Trafo(d.x, 0, 0, d.y, start.x, start.y) else: # the ellipse is inscribed into the rectangle with start and # end as opposite corners. end = self.apply_constraint(self.drag_cur, state) d = (end - start) / 2 self.trafo = Trafo(d.x, 0, 0, d.y, start.x + d.x, start.y + d.y)
def __init__(self, trafo = None, radius1 = 0, radius2 = 0, properties = None, duplicate = None): if trafo is not None and trafo.m11==trafo.m21==trafo.m12==trafo.m22==0: trafo=Trafo(1,0,0,-1,trafo.v1,trafo.v2) RectangularPrimitive.__init__(self, trafo, properties = properties, duplicate = duplicate) if duplicate is not None: self.radius1 = duplicate.radius1 self.radius2 = duplicate.radius2 else: self.radius1 = radius1 self.radius2 = radius2
def load_ellips(self): param = { '10': 0.0, # X coordinat center '20': 0.0, # Y coordinat center #'30': 0.0, # Z coordinat center '11': 0.0, # Endpoint of major axis, relative to the center '21': 0.0, #'31': 0.0, '40': 0.0, # Ratio of minor axis to major axis '41': 0.0, # Start parameter (this value is 0.0 for a full ellipse) '42': 0.0, # End parameter (this value is 2pi for a full ellipse) } param.update(self.general_param) param = self.read_param(param) cx = param['10'] cy = param['20'] rx = sqrt(param['21']**2 + param['11']**2) ry = rx * param['40'] start_angle = param['41'] end_angle = param['42'] angle = atan2(param['21'], param['11']) center = self.trafo(cx, cy) radius = self.trafo.DTransform(rx, ry) trafo = Trafo(radius.x, 0, 0, radius.y) trafo = Rotation(angle)(trafo) trafo = Translation(center)(trafo) rx, w1, w2, ry, cx, cy = trafo.coeff() style = self.get_line_style(**param) self.prop_stack.AddStyle(style.Duplicate()) apply(self.ellipse, (rx, w1, w2, ry, cx, cy, start_angle, end_angle, ArcArc))
def load_ellips(self): param={ '10': 0.0, # X coordinat center '20': 0.0, # Y coordinat center #'30': 0.0, # Z coordinat center '11': 0.0, # Endpoint of major axis, relative to the center '21': 0.0, #'31': 0.0, '40': 0.0, # Ratio of minor axis to major axis '41': 0.0, # Start parameter (this value is 0.0 for a full ellipse) '42': 0.0, # End parameter (this value is 2pi for a full ellipse) } param.update(self.general_param) param = self.read_param(param) cx = param['10'] cy = param['20'] rx = sqrt(param['21']**2 + param['11']**2) ry = rx * param['40'] start_angle = param['41'] end_angle = param['42'] angle=atan2(param['21'], param['11']) center = self.trafo(cx, cy) radius = self.trafo.DTransform(rx, ry) trafo = Trafo(radius.x, 0, 0, radius.y) trafo = Rotation(angle)(trafo) trafo = Translation(center)(trafo) rx, w1, w2, ry, cx, cy = trafo.coeff() style = self.get_line_style(**param) self.prop_stack.AddStyle(style.Duplicate()) apply(self.ellipse, (rx, w1, w2, ry, cx, cy, start_angle, end_angle, ArcArc))
class ChooseRGBZ(ChooseComponent): def __init__(self, master, callback, width, height, comp=1, color=(0, 0, 0), **kw): self.callback = callback self.comp = comp self.win_to_color = Trafo(1, 0, 0, -1 / float(height - 1), 0, 1) self.color_to_win = self.win_to_color.inverse() apply(ChooseComponent.__init__, (self, master, width, height, color), kw) def SetColor(self, color): c = self.color; color = apply(rgb_to_hsv, tuple(color)) if ((self.comp == 0 and (color[1] != c[1] or color[2] != c[2])) or (self.comp == 1 and (color[0] != c[0] or color[2] != c[2])) or (self.comp == 2 and (color[0] != c[0] or color[1] != c[1]))): self.hide_mark() self.color = color self.show_mark() self.UpdateWhenIdle() def update_ramp(self): _sketch.fill_hsv_z(self.image.im, self.comp, self.color) self.set_image(self.image) def move_to(self, p, state): y = p.y if y < 0: y = 0 elif y >= 1.0: y = 1.0 color = list(self.color) color[self.comp] = y self.hide_mark() self.color = tuple(color) self.show_mark() self.callback(self.getRGB()) # self.issue(CHANGED, self.RGBColor()) def draw_mark(self): w, h = self.image.size x, y = self.color_to_win(0, self.color[self.comp]) x = int(x) y = int(y) self.invgc.DrawLine(0, y, w, y)
return array # arrays to convert AI join and cap to Sketch's join and cap. In AI # files they're given as small ints so we just use a tuple where we can # use the AI cap/join value as index to get the corresponding value in # Sketch. _ai_join = (const.JoinMiter, const.JoinRound, const.JoinBevel) _ai_cap = (const.CapButt, const.CapRound, const.CapProjecting) # The same for text alignment. The last two values are two variants of # justified text, which Sketch doesn't have, so we're just using # centered for now. _ai_text_align = (text.ALIGN_LEFT, text.ALIGN_CENTER, text.ALIGN_RIGHT, text.ALIGN_CENTER, text.ALIGN_CENTER) artboard_trafo = Trafo(1, 0, 0, -1, 4014, 4716) artboard_trafo_inv = artboard_trafo.inverse() class FontInfo: def __init__(self, psname, newname, encoding): self.psname = psname self.newname = newname self.encoding = encoding self.reencoder = None def Reencode(self, text): if self.reencoder is None: self.reencoder = encoding.Reencoder(self.encoding, encoding.iso_latin_1) return self.reencoder(text)
def resize(self): code = self.selection.x_code trafo = self.trafo; radius1 = self.radius1; radius2 = self.radius2 if code < 0: # a special handle that has to be treated as a normal handle # depending on the direction of the drag width = hypot(trafo.m11, trafo.m21) height = hypot(trafo.m12, trafo.m22) t = Trafo(trafo.m11 / width, trafo.m21 / width, trafo.m12 / height, trafo.m22 / height, 0, 0) dx, dy = t.inverse()(self.off) #print code, dx, dy if code > -5: # one of the corners in a rectangle with sharp corners if abs(dx) > abs(dy): code = 4 - code else: code = (12, 10, 11, 9)[code] else: # the edge handle and the round corner handles coincide if code >= -7: # horizontal edges if abs(dx) > abs(dy): if dx < 0: code = -code else: code = -code + 1 else: code = -4 - code else: # vertical edges if abs(dx) > abs(dy): code = code + 13 else: if dy < 0: code = -code else: code = -code + 1 # # code is now a normal handle # #print '->', code x, y = trafo.inverse()(self.drag_cur) width = hypot(trafo.m11, trafo.m21) height = hypot(trafo.m12, trafo.m22) if code <= 4: # drag one of the edges if code == 1: t = Trafo(1, 0, 0, 1 - y, 0, y) if y != 1: radius2 = radius2 / abs(1 - y) else: radius1 = radius2 = 0 elif code == 2: t = Trafo(x, 0, 0, 1, 0, 0) if x != 0: radius1 = radius1 / abs(x) else: radius1 = radius2 = 0 elif code == 3: t = Trafo(1, 0, 0, y, 0, 0) if y != 0: radius2 = radius2 / abs(y) else: radius1 = radius2 = 0 elif code == 4: t = Trafo(1 - x, 0, 0, 1, x, 0) if x != 1: radius1 = radius1 / abs(1 - x) else: radius1 = radius2 = 0 trafo = trafo(t) if radius1 != 0 or radius2 != 0: ratio = radius1 / radius2 if radius1 > 0.5: radius1 = 0.5 radius2 = radius1 / ratio if radius2 > 0.5: radius2 = 0.5 radius1 = radius2 * ratio else: # modify the round corners if radius1 == radius2 == 0: ratio = height / width else: ratio = radius1 / radius2 if ratio > 1: max1 = 0.5 max2 = max1 / ratio else: max2 = 0.5 max1 = max2 * ratio if code < 9: if code == 6 or code == 8: x = 1 - x radius1 = max(min(x, max1), 0) radius2 = radius1 / ratio else: if code == 10 or code == 12: y = 1 - y radius2 = max(min(y, max2), 0) radius1 = radius2 * ratio return trafo, radius1, radius2