--- /usr/local/lib/sketch-0.6.14/Plugins/Filters/cgmsaver.py 2002-10-24 14:18:44.000000000 +0200 +++ cgmsaver.py 2002-11-08 13:33:53.000000000 +0100 @@ -1,6 +1,6 @@ # Sketch - A Python-based interactive drawing program # Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 by Bernhard Herzog -# This CGMsaver from Antoon Pardon (2002) +# This CGMsaver mostly by Antoon Pardon (2002) # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU Libary General Public License as published @@ -16,7 +16,7 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. # -# $Id: cgmsaver.py,v 1.1.2.1 2002/08/31 19:21:30 bherzog Exp $ +# $Id: cgmsaver.py,v 1.1.2.8 2002/11/08 12:33:52 apardon Exp apardon $ ###Sketch Config #type = Export @@ -30,9 +30,12 @@ import os.path -from Sketch import Scale, Translation, Bezier, CreateRGBColor, EmptyPattern +from Sketch import Scale, Translation, Bezier, CreateRGBColor, EmptyPattern, \ + Point +from math import sqrt, sin, cos # Fault Tolerance for flattening Beziers. +# If you find the curves not smooth enough, lower this value. EPS = 2 @@ -114,14 +117,14 @@ if lng % 2 == 0: self.pack("!B" , 0) - def putintseq(self , Id , seq): + def putlongseq(self, Id , seq): lng = len(seq) - if 2 * lng < 31: - self.pack("!H" , Id | 2 * lng) + if 4 * lng < 31: + self.pack("!H" , Id | 4 * lng) else: - self.pack("!H" , Id | 31) - self.pack("!H" , 2 * lng) - fmt = '!' + `lng` + "h" + self.pack("!H" , Id | 31) + self.pack("!H" , 4 * lng) + fmt = '!' + `lng` + "i" args = (fmt,) + tuple(seq) apply(self.pack , args) @@ -135,69 +138,196 @@ def close(self): self.file.close() - def PolyBezier(self, Paths, Properties): - - line_pattern = Properties.line_pattern - fill_pattern = Properties.fill_pattern + def PathToSeq(self, Path): + parlst = () + for i in range(Path.len): + type, control, p, cont = Path.Segment(i) + if type == Bezier: + p1 , p2 = control + tmplst = FlattenPath(p0, p1, p2, p) + for tp in tmplst: + parlst = parlst + tuple(self.trafo(tp)) + else: + parlst = parlst + tuple(self.trafo(p)) + p0 = p + return parlst + + def LineStyle(self, Props): + # Line width + self.pack("!Hi" , 0x5064 , rndtoint(self.Scale * Props.line_width)) + # Line color + self.putcol(0x5083 , Props.line_pattern.Color()) + + def FillStyle(self, Props): + line_pattern = Props.line_pattern + fill_pattern = Props.fill_pattern + line_width = rndtoint(self.Scale * Props.line_width) if line_pattern is EmptyPattern: - # Edge Visibility Off self.pack("!HH" , 0x53c2 , 0x0000) - else: - + else: # Edge Visibility On self.pack("!HH" , 0x53c2 , 0x0001) - line_width = rndtoint(self.Scale * Properties.line_width) - - # Sketch doesn't distinghuish between Polylines and - # Polygons. Instead of trying to figure out what - # kind we are dealing with, we set the cgm linewidth - # as well as the cgm edgewidth - # Edge width - self.pack("!HH" , 0x5382 , line_width) - - # Line width - self.pack("!HH" , 0x5062 , line_width) - if line_pattern.is_Solid: - self.putcol(0x53a3 , line_pattern.Color()) - self.putcol(0x5083 , line_pattern.Color()) - + self.pack("!Hi" , 0x5384 , line_width) + # Edge color + self.putcol(0x53a3 , line_pattern.Color()) if fill_pattern is EmptyPattern: - # Fill type is Hollow self.pack("!HH" , 0x52c2 , 0x0004) else: - # Fill type is Solid self.pack("!HH" , 0x52c2 , 0x0001) - if fill_pattern.is_Solid: - self.putcol(0x52e3 , fill_pattern.Color()) - for path in Paths: - if path.closed: + #if fill_pattern.is_Solid: + self.putcol(0x52e3 , fill_pattern.Color()) + + + def PolyBezier(self, Paths, Properties): + + line_pattern = Properties.line_pattern + fill_pattern = Properties.fill_pattern + line_width = rndtoint(self.Scale * Properties.line_width) + if len(Paths) == 1: + path = Paths[0] + if fill_pattern is EmptyPattern and not path.closed: + Id = 0x4020 # Polyline + self.LineStyle(Properties) + lst = self.PathToSeq(path) + else: Id = 0x40e0 # Polygon + self.FillStyle(Properties) + lst = self.PathToSeq(path) + if path.closed: + lst = lst[:-2] + self.putlongseq(Id , map(rndtoint , lst)) + elif fill_pattern is EmptyPattern: + self.LineStyle(Properties) + self.FillStyle(Properties) + for path in Paths: + lst = self.PathToSeq(path) + if path.closed: + Id = 0x40e0 # Polygon + lst = lst[:-2] + else: + Id = 0x4020 # Polyline + self.putlongseq(Id , map(rndtoint , lst)) + else: # The polygonset case + self.FillStyle(Properties) + set = [] + size = 0 + for path in Paths: + lst = self.PathToSeq(path) + if path.closed: + lst = lst[:-2] + size = size + 5 * len(lst) + set.append(lst) + if size < 31: + self.pack("!H" , 0x4100 | size) else: - Id = 0x4020 # Polyline - parlst = () - for i in range(path.len): - type, control, p, cont = path.Segment(i) - if type == Bezier: - p1 , p2 = control - tmplst = FlattenPath(p0, p1, p2, p) - for tp in tmplst: - parlst = parlst + tuple(self.trafo(tp)) + self.pack("!H" , 0x4100 | 31) + self.pack("!H" , size) + for lst in set: + while lst <> (): + Arg = tuple(map(rndtoint , lst[:2])) + #Arg = lst[:2] + lst = lst[2:] + if lst == (): + Arg = Arg + (3,) + else: + Arg = Arg + (1,) + Arg = ("!iiH",) + Arg + apply(self.pack , Arg) + + + def Rectangle(self, rct): + trf = rct.trafo + if rct.radius1 != 0 or rct.radius2 != 0: + self.PolyBezier(rct.Paths(), rct.Properties()) + elif (trf.m12 == 0 and trf.m21 == 0) or (trf.m11 == 0 and trf.m22 == 0): + self.FillStyle(rct.Properties()) + P1 = trf(Point(0,0)) + P2 = trf(Point(1,1)) + self.putlongseq(0x4160 , map(rndtoint , tuple(self.trafo(P1)) + tuple(self.trafo(P2)))) + else: + self.PolyBezier(rct.Paths(), rct.Properties()) + + def Ellipse(self, ell): + trf = ell.trafo + if (abs(trf.m11 - trf.m22) < 0.001 and abs(trf.m21 + trf.m12) < 0.001) \ + or (abs(trf.m11 + trf.m22) < 0.001 and abs(trf.m21 - trf.m12) < 0.001): + if ell.start_angle == ell.end_angle: + self.FillStyle(ell.Properties()) + C = trf(Point(0,0)) + R = sqrt(trf.m11 * trf.m11 + trf.m12 * trf.m12) + self.putlongseq(0x4180 , map(rndtoint , tuple(self.trafo(C)) + (R * self.Scale,))) + else: + C = trf(Point(0,0)) + S = Point(cos(ell.start_angle) , sin(ell.start_angle)) + E = Point(cos(ell.end_angle) , sin(ell.end_angle)) + R = sqrt(trf.m11 * trf.m11 + trf.m12 * trf.m12) + if trf.m11 * trf.m22 - trf.m12 * trf.m21 > 0: + S,E = trf.DTransform(S) , trf.DTransform(E) + else: + S,E = trf.DTransform(E) , trf.DTransform(S) + S = 1000000 * S / abs(S) + E = 1000000 * E / abs(E) + if ell.arc_type == 0 and ell.Properties().fill_pattern == EmptyPattern: + self.LineStyle(ell.Properties()) + self.putlongseq(0x41e0 , map(rndtoint , tuple(self.trafo(C)) + tuple(S) + tuple(E) + (R * self.Scale,))) + else: + #self.PolyBezier(ell.Paths(), ell.Properties()) + self.FillStyle(ell.Properties()) + if ell.arc_type == 0: + cp = 1 + else: + cp = 2 - ell.arc_type + Args = ["!H7iH" , 0x4200 + 30] + map(rndtoint , tuple(self.trafo(C)) \ + + tuple(S) + tuple(E) + (R * self.Scale , cp)) + apply(self.pack , Args) + else: + if ell.start_angle == ell.end_angle: + self.FillStyle(ell.Properties()) + C = trf(Point(0,0)) + P1 = trf(Point(1,0)) + P2 = trf(Point(0,1)) + self.putlongseq(0x4220 , map(rndtoint , tuple(self.trafo(C)) + tuple(self.trafo(P1)) + tuple(self.trafo(P2)))) + else: + C = trf(Point(0,0)) + P1 = trf(Point(1,0)) + P2 = trf(Point(0,1)) + S = trf.DTransform(Point(cos(ell.start_angle) , sin(ell.start_angle))) + E = trf.DTransform(Point(cos(ell.end_angle) , sin(ell.end_angle))) + S = 1000000 * S / abs(S) + E = 1000000 * E / abs(E) + if ell.arc_type == 0 and ell.Properties().fill_pattern == EmptyPattern: + self.LineStyle(ell.Properties()) + self.putlongseq(0x4240 , map(rndtoint , tuple(self.trafo(C)) + tuple(self.trafo(P1)) \ + + tuple(self.trafo(P2)) + tuple(S) + tuple(E))) else: - parlst = parlst + tuple(self.trafo(p)) - p0 = p - if Id == 0x40e0: - parlst = parlst[:-2] - self.putintseq(Id , map(rndtoint , parlst)) + #self.PolyBezier(ell.Paths(), ell.Properties()) + self.FillStyle(ell.Properties()) + if ell.arc_type == 0: + cp = 1 + else: + cp = 2 - ell.arc_type + Args = ["!HH10iH" , 0x4260 + 31 , 42] + map(rndtoint , tuple(self.trafo(C)) \ + + tuple(self.trafo(P1)) + tuple(self.trafo(P2)) + tuple(S) + tuple(E) + (cp,)) + apply(self.pack , Args) + + def Text(self, object): + self.PolyBezier(object.Paths(), object.Properties()) def SaveObjects(self, Objects): for object in Objects: if object.is_Compound: self.SaveObjects(object.GetObjects()) + elif object.is_Rectangle: + self.Rectangle(object) + elif object.is_Ellipse: + self.Ellipse(object) + elif object.is_Text: + self.Text(object) elif object.is_curve: self.PolyBezier(object.Paths(), object.Properties()) @@ -221,14 +351,14 @@ self.pack("!HH" , 0x2062 , 0x0000) # VDC Extend - self.putintseq(0x20c0 , self.extend) + self.putlongseq(0x20c0 , self.extend) # Background Colour self.putcol(0x20e3 , self.white) # Begin Picture Body self.pack("!H" , 0x0080) - + self.SaveObjects(layer.GetObjects()) # End Picture @@ -252,8 +382,10 @@ width = doc.page_layout.width hight = doc.page_layout.height right , top = width , hight - sc = 65534 / max(width , hight) - self.trafo = Translation(-32767,-32767)(Scale(sc)(Translation(-left , -bottom))) + #sc = 65534 / max(width , hight) + sc = 1000 + #self.trafo = Translation(-32767,-32767)(Scale(sc)(Translation(-left , -bottom))) + self.trafo = Scale(sc)(Translation(-left , -bottom)) self.Scale = sc self.extend = map(rndtoint , tuple(self.trafo(left,bottom)) + tuple(self.trafo(right,top))) @@ -268,11 +400,16 @@ self.pack("!H" , 0x0001) # Metafile Description - self.putstr(0x1040 , filename + "created by sketch") + self.putstr(0x1040 , filename + " created by sketch") # Metafile Element List self.pack("!HHHH" , 0x1166 , 0x0001 , 0xffff , 0x0001) + # Default Replacements + self.pack("!H" , 0x1184) + # VDC Integer precision 32 bits + self.pack("!Hh" , 0x3022 , 32) + #Font List #