def test_07(self): """TestSVGlWriter.test_07(): text. Based on http://www.w3.org/TR/2003/REC-SVG11-20030114/text.html#TextElement""" myF = io.StringIO() myViewPort = Coord.Box( Coord.Dim(12, 'cm'), Coord.Dim(4, 'cm'), ) with SVGWriter.SVGWriter(myF, myViewPort, {'viewBox': "0 0 1000 300"}) as xS: with XmlWrite.Element(xS, 'desc'): xS.characters("Example text01 - 'Hello, out there' in blue") myPt = Coord.Pt(Coord.baseUnitsDim(250), Coord.baseUnitsDim(150)) with SVGWriter.SVGText(xS, myPt, "Verdans", 55, {'fill': "blue"}): xS.characters('Hello, out there') #xS.comment(" Show outline of canvas using 'rect' element ") myPt = Coord.Pt(Coord.baseUnitsDim(1), Coord.baseUnitsDim(1)) myBx = Coord.Box(Coord.baseUnitsDim(998), Coord.baseUnitsDim(298)) with SVGWriter.SVGRect(xS, myPt, myBx, { 'fill': "none", 'stroke': "blue", 'stroke-width': "2" }): pass # print() # print(myF.getvalue()) self.assertEqual( myF.getvalue(), """<?xml version='1.0' encoding="utf-8"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg height="4.000cm" version="1.1" viewBox="0 0 1000 300" width="12.000cm" xmlns="http://www.w3.org/2000/svg"> <desc>Example text01 - 'Hello, out there' in blue</desc> <text fill="blue" font-family="Verdans" font-size="55" x="250.000px" y="150.000px">Hello, out there</text> <rect fill="none" height="298.000px" stroke="blue" stroke-width="2" width="998.000px" x="1.000px" y="1.000px"/> </svg> """)
def plot(self, xS, theTl, theWsdS=None): """Write the header to the SVG stream at position offset top left. theWsd is a list of records that contain well site data. Will raise ExceptionLogHeader is wrong type of Logical Record.""" self._checkWsd(theWsdS) # self._traceTopLeftAndViewBox(theTl, xS) groupAttrs = { 'transform': "translate({:g},{:g})".format( theTl.x.value * TRANSFORM_UNITS_PER_PLOT_UNITS, theTl.y.value * TRANSFORM_UNITS_PER_PLOT_UNITS, ), } with SVGWriter.SVGGroup(xS, groupAttrs): if self._isTopOfLog: # Make this all one group so it can be rotated etc. groupAttrs = { 'transform': "translate({:g},0) rotate(90)".format( self._width().value * TRANSFORM_UNITS_PER_PLOT_UNITS, ), } with SVGWriter.SVGGroup(xS, groupAttrs): self._plot(xS, theWsdS) else: self._plot(xS, theWsdS)
def test_03(self): """TestSVGlWriter.test_03(): an elipse. Based on http://www.w3.org/TR/2003/REC-SVG11-20030114/shapes.html#EllipseElement""" myF = io.StringIO() myViewPort = Coord.Box( Coord.Dim(12, 'cm'), Coord.Dim(4, 'cm'), ) with SVGWriter.SVGWriter(myF, myViewPort) as xS: with XmlWrite.Element(xS, 'desc'): xS.characters('Example ellipse01 - examples of ellipses') #xS.comment(" Show outline of canvas using 'rect' element ") myPt = Coord.Pt(Coord.baseUnitsDim(1), Coord.baseUnitsDim(1)) myBx = Coord.Box(Coord.baseUnitsDim(1198), Coord.baseUnitsDim(398)) with SVGWriter.SVGRect(xS, myPt, myBx, {'fill':"none", 'stroke':"blue",'stroke-width':"2"}): pass myPt = Coord.Pt(Coord.baseUnitsDim(600), Coord.baseUnitsDim(200)) myRadX = Coord.baseUnitsDim(250) myRadY = Coord.baseUnitsDim(100) with SVGWriter.SVGElipse(xS, myPt, myRadX, myRadY, {'fill':"red", 'stroke':"blue",'stroke-width':"10"}): pass #print #print myF.getvalue() # self.maxDiff = None self.assertEqual(myF.getvalue(), """<?xml version='1.0' encoding="utf-8"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg height="4cm" version="1.1" width="12cm" xmlns="http://www.w3.org/2000/svg"> <desc>Example ellipse01 - examples of ellipses</desc> <rect fill="none" height="398px" stroke="blue" stroke-width="2" width="1198px" x="1px" y="1px"/> <elipse cx="600px" cy="200px" fill="red" rx="250px" ry="100px" stroke="blue" stroke-width="10"/> </svg> """)
def test_05(self): """TestSVGlWriter.test_05(): a polyline. Based on http://www.w3.org/TR/2003/REC-SVG11-20030114/shapes.html#PolylineElement""" myF = io.StringIO() myViewPort = Coord.Box( Coord.Dim(12, 'cm'), Coord.Dim(4, 'cm'), ) with SVGWriter.SVGWriter(myF, myViewPort, {'viewBox' : "0 0 1200 400"}) as xS: with XmlWrite.Element(xS, 'desc'): xS.characters('Example line01 - lines expressed in user coordinates') #xS.comment(" Show outline of canvas using 'rect' element ") myPt = Coord.Pt(Coord.baseUnitsDim(1), Coord.baseUnitsDim(1)) myBx = Coord.Box(Coord.baseUnitsDim(1198), Coord.baseUnitsDim(398)) with SVGWriter.SVGRect(xS, myPt, myBx, {'fill':"none", 'stroke':"blue",'stroke-width':"2"}): pass # Make a group with SVGWriter.SVGPolyline( xS, [ Coord.Pt(Coord.baseUnitsDim(50), Coord.baseUnitsDim(375)), Coord.Pt(Coord.baseUnitsDim(150), Coord.baseUnitsDim(375)), Coord.Pt(Coord.baseUnitsDim(150), Coord.baseUnitsDim(325)), Coord.Pt(Coord.baseUnitsDim(250), Coord.baseUnitsDim(325)), Coord.Pt(Coord.baseUnitsDim(250), Coord.baseUnitsDim(375)), Coord.Pt(Coord.baseUnitsDim(350), Coord.baseUnitsDim(375)), Coord.Pt(Coord.baseUnitsDim(350), Coord.baseUnitsDim(250)), Coord.Pt(Coord.baseUnitsDim(450), Coord.baseUnitsDim(250)), Coord.Pt(Coord.baseUnitsDim(450), Coord.baseUnitsDim(375)), Coord.Pt(Coord.baseUnitsDim(550), Coord.baseUnitsDim(375)), Coord.Pt(Coord.baseUnitsDim(550), Coord.baseUnitsDim(175)), Coord.Pt(Coord.baseUnitsDim(650), Coord.baseUnitsDim(175)), Coord.Pt(Coord.baseUnitsDim(650), Coord.baseUnitsDim(375)), Coord.Pt(Coord.baseUnitsDim(750), Coord.baseUnitsDim(375)), Coord.Pt(Coord.baseUnitsDim(750), Coord.baseUnitsDim(100)), Coord.Pt(Coord.baseUnitsDim(850), Coord.baseUnitsDim(100)), Coord.Pt(Coord.baseUnitsDim(850), Coord.baseUnitsDim(375)), Coord.Pt(Coord.baseUnitsDim(950), Coord.baseUnitsDim(375)), Coord.Pt(Coord.baseUnitsDim(950), Coord.baseUnitsDim(25)), Coord.Pt(Coord.baseUnitsDim(1050), Coord.baseUnitsDim(25)), Coord.Pt(Coord.baseUnitsDim(1050), Coord.baseUnitsDim(375)), Coord.Pt(Coord.baseUnitsDim(1150), Coord.baseUnitsDim(375)), ], {'fill' : 'none', 'stroke' : 'blue', 'stroke-width' : "5"} ): pass # print() # print(myF.getvalue()) # self.maxDiff = None self.assertEqual(myF.getvalue(), """<?xml version='1.0' encoding="utf-8"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg height="4cm" version="1.1" viewBox="0 0 1200 400" width="12cm" xmlns="http://www.w3.org/2000/svg"> <desc>Example line01 - lines expressed in user coordinates</desc> <rect fill="none" height="398px" stroke="blue" stroke-width="2" width="1198px" x="1px" y="1px"/> <polyline fill="none" points="50,375 150,375 150,325 250,325 250,375 350,375 350,250 450,250 450,375 550,375 550,175 650,175 650,375 750,375 750,100 850,100 850,375 950,375 950,25 1050,25 1050,375 1150,375" stroke="blue" stroke-width="5"/> </svg> """)
def test_06(self): """TestSVGlWriter.test_06(): a polygon. Based on http://www.w3.org/TR/2003/REC-SVG11-20030114/shapes.html#PolygonElement""" myF = io.StringIO() myViewPort = Coord.Box( Coord.Dim(12, 'cm'), Coord.Dim(4, 'cm'), ) with SVGWriter.SVGWriter(myF, myViewPort, {'viewBox': "0 0 1200 400"}) as xS: with XmlWrite.Element(xS, 'desc'): xS.characters( 'Example line01 - lines expressed in user coordinates') #xS.comment(" Show outline of canvas using 'rect' element ") myPt = Coord.Pt(Coord.baseUnitsDim(1), Coord.baseUnitsDim(1)) myBx = Coord.Box(Coord.baseUnitsDim(1198), Coord.baseUnitsDim(398)) with SVGWriter.SVGRect(xS, myPt, myBx, { 'fill': "none", 'stroke': "blue", 'stroke-width': "2" }): pass # Make a group with SVGWriter.SVGPolygon(xS, [ Coord.Pt(Coord.baseUnitsDim(350), Coord.baseUnitsDim(75)), Coord.Pt(Coord.baseUnitsDim(379), Coord.baseUnitsDim(161)), Coord.Pt(Coord.baseUnitsDim(469), Coord.baseUnitsDim(161)), Coord.Pt(Coord.baseUnitsDim(397), Coord.baseUnitsDim(215)), Coord.Pt(Coord.baseUnitsDim(423), Coord.baseUnitsDim(301)), Coord.Pt(Coord.baseUnitsDim(350), Coord.baseUnitsDim(250)), Coord.Pt(Coord.baseUnitsDim(277), Coord.baseUnitsDim(301)), Coord.Pt(Coord.baseUnitsDim(303), Coord.baseUnitsDim(215)), Coord.Pt(Coord.baseUnitsDim(231), Coord.baseUnitsDim(161)), Coord.Pt(Coord.baseUnitsDim(321), Coord.baseUnitsDim(161)), ], { 'fill': 'red', 'stroke': 'blue', 'stroke-width': "10" }): pass # print() # print(myF.getvalue()) # self.maxDiff = None self.assertEqual( """<?xml version='1.0' encoding="utf-8"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg height="4.000cm" version="1.1" viewBox="0 0 1200 400" width="12.000cm" xmlns="http://www.w3.org/2000/svg"> <desc>Example line01 - lines expressed in user coordinates</desc> <rect fill="none" height="398.000px" stroke="blue" stroke-width="2" width="1198.000px" x="1.000px" y="1.000px"/> <polygon fill="red" points="350.0,75.0 379.0,161.0 469.0,161.0 397.0,215.0 423.0,301.0 350.0,250.0 277.0,301.0 303.0,215.0 231.0,161.0 321.0,161.0" stroke="blue" stroke-width="10"/> </svg> """, myF.getvalue())
def plotLine(self, ptFrom, ptTo, stroke): """Plots a line. Points are Coord.Pt objects. theStroke is a Plot.Stroke object.""" with SVGWriter.SVGLine(self._svg, ptFrom, ptTo, attrs=self._retSVGAttrsFromStroke(stroke)): pass
def _traceTopLeftAndViewBox(self, theTl, xS): # Plot Top-left box and view box with SVGWriter.SVGRect(xS, Coord.zeroBaseUnitsPt(), Coord.Box(theTl.x, theTl.y), { 'fill': "red", 'stroke': "black", 'stroke-width': "1", }): pass with SVGWriter.SVGRect(xS, Coord.zeroBaseUnitsPt(), self.viewPort(theTl), { 'fill': "none", 'stroke': "green", 'stroke-width': "1", }): pass
def plotText(self, thePoint, theFont, theSize, theText): """Plots text. thePoint - Coord.Pt theFont - string. theSize - integer. theText - string""" with SVGWriter.SVGText(self._svg, thePoint, theFont, theSize): self._svg.characters(theText)
def test_10(self): """TestLogHeader.test_10(): Plot as not top of log with CONS data.""" myLh = LogHeader.APIHeaderLIS(isTopOfLog=False) fp = TestPlotShared.outPath(TEST_SVG_FILE_MAP_HDR[45].fileName) tl = Coord.Pt(Coord.Dim(0.25, 'in'), Coord.Dim(0.5, 'in')) viewPort = myLh.viewPort(tl) with SVGWriter.SVGWriter(open(fp, 'w'), viewPort) as xS: myLh.plot(xS, tl, [self._lrCONS,])
def test_05(self): """TestLogHeaderLAS.test_05(): Plot as not top of log.""" myLh = LogHeader.APIHeaderLAS(isTopOfLog=False) fp = TestPlotShared.outPath(TEST_SVG_FILE_MAP_HDR[50].fileName) tl = Coord.Pt(Coord.Dim(0.0, 'in'), Coord.Dim(0.0, 'in')) viewPort = myLh.viewPort(tl) with SVGWriter.SVGWriter(open(fp, 'w'), viewPort) as xS: myLh.plot(xS, tl)
def test_11(self): """TestLogHeaderLAS.test_11(): Plot as top of log with CONS data.""" myLh = LogHeader.APIHeaderLAS(isTopOfLog=True) fp = TestPlotShared.outPath(TEST_SVG_FILE_MAP_HDR[56].fileName) tl = Coord.Pt(Coord.Dim(0.5, 'in'), Coord.Dim(0.25, 'in')) viewPort = myLh.viewPort(tl) with SVGWriter.SVGWriter(open(fp, 'w'), viewPort) as xS: myLh.plot(xS, tl, self._lasFile)
def _plot(self, xS, theWsdS): # Write background self._plotBackGround(xS) # Page 1 bounding box with SVGWriter.SVGRect(xS, self._ptZero(), self._size(False), { 'fill': "none", 'stroke': "blue", 'stroke-width': ".5", }): pass # Write statics for st in STATICS: self._plotStatic(xS, st) for (x, y, r), stS in STATICS_VERT: with SVGWriter.SVGGroup(xS, self._uprightGroupAttrs(x, y, r)): for st in stS: self._plotStatic(xS, st) self._plotWsd(xS, theWsdS)
def _plotWsd(self, xS, theWsdS): """Plot dynamic information from well site data.""" if theWsdS is not None and len(theWsdS) > 0: for st in STATICS: self._plotStaticIfCONS(xS, theWsdS, st) for (x, y, r), stS in STATICS_VERT: if any([st.mnem is not None for st in stS]): with SVGWriter.SVGGroup(xS, self._uprightGroupAttrs(x, y, r)): for st in stS: self._plotStaticIfCONS(xS, theWsdS, st)
def _plotBackGround(self, xS): """Plot the background stuff.""" # Top black background with SVGWriter.SVGRect( xS, self._ptZero(), Coord.Box(PlotConstants.STANDARD_PAPER_DEPTH, Coord.Dim(2.0, HEADER_PLOT_UNITS)), { 'fill': "black", }): pass # Logo self._plotLogo(xS)
def _plotStatic(self, xS, st): if st.rAttr is not None: # Write the box with SVGWriter.SVGRect( xS, self._pt(st.x, st.y), Coord.Box(Coord.Dim(st.w, HEADER_PLOT_UNITS), Coord.Dim(st.d, HEADER_PLOT_UNITS)), st.rAttr, ): pass # Write the text if st.text is not None: with SVGWriter.SVGText( xS, self._pt(st.x + 0.05, st.y + st.d * 3 / 4), st.font, st.size, st.tAttr, ): xS.characters(st.text)
def _plotLogo(self, xS): """Plots the TotalDepth logo.""" myLogoColour = "blue" with SVGWriter.SVGRect(xS, self._pt(1.2, 2.05), self._box(2.0, 0.3), { 'fill': "none", 'stroke': myLogoColour, 'stroke-width': "2.0", }): pass with SVGWriter.SVGRect(xS, self._pt(1.2, 2.05 + 0.3), self._box(2.0, 0.3), { 'fill': myLogoColour, 'stroke': myLogoColour, 'stroke-width': "2.0", }): pass with SVGWriter.SVGText(xS, self._pt(1.2 + 1.0, 2.05 + 0.3 + 0.25), FONT_PROP, 24, { 'text-anchor': 'middle', 'font-weight': "bold", 'fill': "white", }): xS.characters('TotalDepth')
def test_00(self): """TestSVGWriter.test_00(): construction.""" myF = io.StringIO() myViewPort = Coord.Box( Coord.Dim(100, 'mm'), Coord.Dim(20, 'mm'), ) with SVGWriter.SVGWriter(myF, myViewPort): pass #print #print myF.getvalue() self.assertEqual(myF.getvalue(), """<?xml version='1.0' encoding="utf-8"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg height="20mm" version="1.1" width="100mm" xmlns="http://www.w3.org/2000/svg"/>\n""")
def test_03(self): """TestPlotTrack.test_03(): Construct a log track and plot it in SVG.""" myT = Track.Track( leftPos=Coord.Dim(3.2, 'in'), rightPos=Coord.Dim(5.6, 'in'), gridGn=Track.genLog10Decade2 ) # myCnv = Plot.Canvas(Coord.Dim(6, 'in'), Coord.Dim(8, 'in'), Plot.MarginQtrInch) # myViewPort = Coord.Box( # width=myCnv.width, # depth=myCnv.depth, # ) myViewPort = Coord.Box( width=Coord.Dim(6, 'in'), depth=Coord.Dim(8, 'in'), ) myTl = Coord.Pt(Coord.Dim(0.25, 'in'), Coord.Dim(0.25, 'in')) myF = io.StringIO() with SVGWriter.SVGWriter(myF, myViewPort) as xS: myT.plotSVG(myTl, Coord.Dim(7.5, 'in'), xS) # print() # print(myF.getvalue()) # self.maxDiff = None self.assertEqual("""<?xml version='1.0' encoding="utf-8"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg height="8.000in" version="1.1" width="6.000in" xmlns="http://www.w3.org/2000/svg"> <line stroke="black" stroke-opacity="1.000" stroke-width="0.750" x1="3.450in" x2="3.450in" y1="0.250in" y2="7.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="3.811in" x2="3.811in" y1="0.250in" y2="7.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="4.023in" x2="4.023in" y1="0.250in" y2="7.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="4.172in" x2="4.172in" y1="0.250in" y2="7.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="4.289in" x2="4.289in" y1="0.250in" y2="7.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="4.384in" x2="4.384in" y1="0.250in" y2="7.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="4.464in" x2="4.464in" y1="0.250in" y2="7.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="4.534in" x2="4.534in" y1="0.250in" y2="7.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="4.595in" x2="4.595in" y1="0.250in" y2="7.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.750" x1="4.650in" x2="4.650in" y1="0.250in" y2="7.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="5.011in" x2="5.011in" y1="0.250in" y2="7.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="5.223in" x2="5.223in" y1="0.250in" y2="7.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="5.372in" x2="5.372in" y1="0.250in" y2="7.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="5.489in" x2="5.489in" y1="0.250in" y2="7.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="5.584in" x2="5.584in" y1="0.250in" y2="7.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="5.664in" x2="5.664in" y1="0.250in" y2="7.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="5.734in" x2="5.734in" y1="0.250in" y2="7.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="5.795in" x2="5.795in" y1="0.250in" y2="7.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.750" x1="5.850in" x2="5.850in" y1="0.250in" y2="7.750in"/> </svg> """, myF.getvalue() )
def _plotCONS(self, xS, st, theValStr): """Print a Static() object with an value as a string e.g. EngVal.pStr().""" # No Rectangle myTAttr = st.tAttr myTAttr.update({ 'font-weight': "bold", }) with SVGWriter.SVGText( xS, self._pt(st.x + 0.05 + st.xMnem, st.y + st.d * 3 / 4), st.font, st.size, myTAttr, ): xS.characters(theValStr)
def test_01(self): """TestSVGlWriter.test_01(): <desc> and four rectangles. From second example in http://www.w3.org/TR/2003/REC-SVG11-20030114/struct.html#NewDocumentOverview""" myF = io.StringIO() myViewPort = Coord.Box( Coord.Dim(5, 'cm'), Coord.Dim(4, 'cm'), ) with SVGWriter.SVGWriter(myF, myViewPort) as xS: with XmlWrite.Element(xS, 'desc'): xS.characters('Four separate rectangles') myPt = Coord.Pt(Coord.Dim(0.5, 'cm'), Coord.Dim(0.5, 'cm')) myBx = Coord.Box(Coord.Dim(2.0, 'cm'), Coord.Dim(1.0, 'cm')) with SVGWriter.SVGRect(xS, myPt, myBx): pass myPt = Coord.Pt(Coord.Dim(0.5, 'cm'), Coord.Dim(2.0, 'cm')) myBx = Coord.Box(Coord.Dim(1.0, 'cm'), Coord.Dim(1.5, 'cm')) with SVGWriter.SVGRect(xS, myPt, myBx): pass myPt = Coord.Pt(Coord.Dim(3.0, 'cm'), Coord.Dim(0.5, 'cm')) myBx = Coord.Box(Coord.Dim(1.5, 'cm'), Coord.Dim(2.0, 'cm')) with SVGWriter.SVGRect(xS, myPt, myBx): pass myPt = Coord.Pt(Coord.Dim(3.5, 'cm'), Coord.Dim(3.0, 'cm')) myBx = Coord.Box(Coord.Dim(1.0, 'cm'), Coord.Dim(0.5, 'cm')) with SVGWriter.SVGRect(xS, myPt, myBx): pass myPt = Coord.Pt(Coord.Dim(0.01, 'cm'), Coord.Dim(0.01, 'cm')) myBx = Coord.Box(Coord.Dim(4.98, 'cm'), Coord.Dim(3.98, 'cm')) with SVGWriter.SVGRect( xS, myPt, myBx, attrs= { 'fill' : "none", 'stroke' : "blue", 'stroke-width' : ".02cm", } ): pass #print #print myF.getvalue() self.assertEqual(myF.getvalue(), """<?xml version='1.0' encoding="utf-8"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg height="4cm" version="1.1" width="5cm" xmlns="http://www.w3.org/2000/svg"> <desc>Four separate rectangles</desc> <rect height="1cm" width="2cm" x="0.5cm" y="0.5cm"/> <rect height="1.5cm" width="1cm" x="0.5cm" y="2cm"/> <rect height="2cm" width="1.5cm" x="3cm" y="0.5cm"/> <rect height="0.5cm" width="1cm" x="3.5cm" y="3cm"/> <rect fill="none" height="3.98cm" stroke="blue" stroke-width=".02cm" width="4.98cm" x="0.01cm" y="0.01cm"/> </svg> """)
def plotSVG(self, topLeft, depth, theSVGWriter): """Plot the track gridlines. topLeft - A Coord.Pt() object that is the top left of the canvas. depth - A Coord.Dim() object that is the total depth of the grid below topLeft. drive - The plot driver.""" if self._gridGn is not None: botLeft = Coord.newPt(topLeft, incY=depth) for stroke, pos in self._gridGn(self._lP.value, self._rP.value): # Compute the line position topP = Coord.newPt(topLeft, incX=Coord.Dim(pos, self._lP.units)) botP = Coord.newPt(botLeft, incX=Coord.Dim(pos, self._lP.units)) with SVGWriter.SVGLine( theSVGWriter, topP, botP, attrs=Stroke.retSVGAttrsFromStroke(stroke)): pass
def test_00(self): """TestPlotTrack.test_00(): Construct a lin/log/log track with FEET 1/200 and plot it in SVG.""" myCnv = Plot.Canvas(Coord.Dim(8.5, 'in'), Coord.Dim(12, 'in'), PlotConstants.MarginQtrInch) myViewPort = Coord.Box( width=myCnv.width, depth=myCnv.depth, ) myTopLeft = Coord.Pt(Coord.Dim(0.25, 'in'), Coord.Dim(0.25, 'in')) myF = io.StringIO() myTracks = [ Track.Track(leftPos=Coord.Dim(0.0, 'in'), rightPos=Coord.Dim(2.4, 'in'), gridGn=Track.genLinear10), Track.Track( leftPos=Coord.Dim(2.4, 'in'), rightPos=Coord.Dim(3.2, 'in'), gridGn=None, plotXAxis=True, ), Track.Track(leftPos=Coord.Dim(3.2, 'in'), rightPos=Coord.Dim(5.6, 'in'), gridGn=Track.genLog10Decade2Start2), Track.Track(leftPos=Coord.Dim(5.6, 'in'), rightPos=Coord.Dim(8, 'in'), gridGn=Track.genLog10Decade2Start2), ] with SVGWriter.SVGWriter(myF, myViewPort) as xS: # Do tracks first for t in myTracks: #xS.comment(str(t)) if t.hasGrid: t.plotSVG( myTopLeft, myCnv.depth - myCnv.margins.top - myCnv.margins.bottom, xS, ) # Now XGrid # We are plotting up so xPosStart is at bottom myXposStart = myCnv.depth - myCnv.margins.bottom myXposStop = myCnv.margins.top myXg = XGrid.XGrid(200) # Plot depth lines for pos, stroke in myXg.genXPosStroke(xFrom=4307.5, xInc=False, units=b'FEET'): myXpos = myXposStart + pos if myXpos < myXposStop: break for t in myTracks: if t.hasGrid: with SVGWriter.SVGLine( xS, Coord.Pt(t.left + myCnv.margins.left, myXpos), Coord.Pt(t.right + myCnv.margins.left, myXpos), attrs=Stroke.retSVGAttrsFromStroke(stroke)): pass # Plot depth text textAttrs = { 'text-anchor': 'end', 'dominant-baseline': 'middle', } for pos, val in myXg.genXPosText(xFrom=4307.5, xInc=False, units=b'FEET'): myXpos = myXposStart + pos if myXpos < myXposStop: break for t in myTracks: if t.plotXAxis: myPt = Coord.Pt( t.right + myCnv.margins.left - Coord.Dim(0.05, 'in'), myXpos) with SVGWriter.SVGText(xS, myPt, 'Courier', 16, textAttrs): xS.characters(str(val)) print() print(myF.getvalue())
def plotPolyLine(self, ptS, theStroke): """Plots a polyline. ptS are a list of Coord.Pt objects. theStroke is a Plot.Stroke object.""" with SVGWriter.SVGPolyline(self._svg, ptS, attrs=self._retSVGAttrsFromStroke(stroke)): pass
def test_04(self): """TestPlotTrack.test_04(): Construct a lin/log/log track and plot it in SVG.""" myTracks = [ Track.Track(leftPos=Coord.Dim(0.0, 'in'), rightPos=Coord.Dim(2.4, 'in'), gridGn=Track.genLinear10), Track.Track(leftPos=Coord.Dim(3.2, 'in'), rightPos=Coord.Dim(5.6, 'in'), gridGn=Track.genLog10Decade2Start2), Track.Track(leftPos=Coord.Dim(5.6, 'in'), rightPos=Coord.Dim(8, 'in'), gridGn=Track.genLog10Decade2Start2), ] # myCnv = Plot.Canvas(Coord.Dim(8.5, 'in'), Coord.Dim(6, 'in'), Plot.MarginQtrInch) # myViewPort = Coord.Box( # width=myCnv.width, # depth=myCnv.depth, # ) myViewPort = Coord.Box( width=Coord.Dim(6, 'in'), depth=Coord.Dim(8, 'in'), ) myTl = Coord.Pt(Coord.Dim(0.25, 'in'), Coord.Dim(0.25, 'in')) myF = io.StringIO() with SVGWriter.SVGWriter(myF, myViewPort) as xS: for t in myTracks: #xS.comment(str(t)) t.plotSVG(myTl, Coord.Dim(5.5, 'in'), xS) # print() # print(myF.getvalue()) # self.maxDiff = None self.assertEqual( """<?xml version='1.0' encoding="utf-8"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg height="8.000in" version="1.1" width="6.000in" xmlns="http://www.w3.org/2000/svg"> <line stroke="black" stroke-opacity="1.000" stroke-width="0.750" x1="0.250in" x2="0.250in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="0.490in" x2="0.490in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="0.730in" x2="0.730in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="0.970in" x2="0.970in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="1.210in" x2="1.210in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.500" x1="1.450in" x2="1.450in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="1.690in" x2="1.690in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="1.930in" x2="1.930in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="2.170in" x2="2.170in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="2.410in" x2="2.410in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.750" x1="2.650in" x2="2.650in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.750" x1="3.450in" x2="3.450in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="3.661in" x2="3.661in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="3.811in" x2="3.811in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="3.928in" x2="3.928in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="4.023in" x2="4.023in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="4.103in" x2="4.103in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="4.172in" x2="4.172in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="4.234in" x2="4.234in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.750" x1="4.289in" x2="4.289in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="4.650in" x2="4.650in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="4.861in" x2="4.861in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="5.011in" x2="5.011in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="5.128in" x2="5.128in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="5.223in" x2="5.223in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="5.303in" x2="5.303in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="5.372in" x2="5.372in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="5.434in" x2="5.434in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.750" x1="5.489in" x2="5.489in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.750" x1="5.850in" x2="5.850in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.750" x1="5.850in" x2="5.850in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="6.061in" x2="6.061in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="6.211in" x2="6.211in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="6.328in" x2="6.328in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="6.423in" x2="6.423in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="6.503in" x2="6.503in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="6.572in" x2="6.572in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="6.634in" x2="6.634in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.750" x1="6.689in" x2="6.689in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="7.050in" x2="7.050in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="7.261in" x2="7.261in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="7.411in" x2="7.411in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="7.528in" x2="7.528in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="7.623in" x2="7.623in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="7.703in" x2="7.703in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="7.772in" x2="7.772in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.250" x1="7.834in" x2="7.834in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.750" x1="7.889in" x2="7.889in" y1="0.250in" y2="5.750in"/> <line stroke="black" stroke-opacity="1.000" stroke-width="0.750" x1="8.250in" x2="8.250in" y1="0.250in" y2="5.750in"/> </svg> """, myF.getvalue())
def test_04(self): """TestSVGlWriter.test_04(): a line. Based on http://www.w3.org/TR/2003/REC-SVG11-20030114/shapes.html#LineElement""" myF = io.StringIO() myViewPort = Coord.Box( Coord.Dim(12, 'cm'), Coord.Dim(4, 'cm'), ) with SVGWriter.SVGWriter(myF, myViewPort) as xS: with XmlWrite.Element(xS, 'desc'): xS.characters( 'Example line01 - lines expressed in user coordinates') #xS.comment(" Show outline of canvas using 'rect' element ") myPt = Coord.Pt(Coord.baseUnitsDim(1), Coord.baseUnitsDim(1)) myBx = Coord.Box(Coord.baseUnitsDim(1198), Coord.baseUnitsDim(398)) with SVGWriter.SVGRect(xS, myPt, myBx, { 'fill': "none", 'stroke': "blue", 'stroke-width': "2" }): pass # Make a group with SVGWriter.SVGGroup(xS, {'stroke': 'green'}): with SVGWriter.SVGLine( xS, Coord.Pt(Coord.baseUnitsDim(100), Coord.baseUnitsDim(300)), Coord.Pt(Coord.baseUnitsDim(300), Coord.baseUnitsDim(100)), {'stroke-width': "5"}): pass with SVGWriter.SVGLine( xS, Coord.Pt(Coord.baseUnitsDim(300), Coord.baseUnitsDim(300)), Coord.Pt(Coord.baseUnitsDim(500), Coord.baseUnitsDim(100)), {'stroke-width': "10"}): pass with SVGWriter.SVGLine( xS, Coord.Pt(Coord.baseUnitsDim(500), Coord.baseUnitsDim(300)), Coord.Pt(Coord.baseUnitsDim(700), Coord.baseUnitsDim(100)), {'stroke-width': "15"}): pass with SVGWriter.SVGLine( xS, Coord.Pt(Coord.baseUnitsDim(700), Coord.baseUnitsDim(300)), Coord.Pt(Coord.baseUnitsDim(900), Coord.baseUnitsDim(100)), {'stroke-width': "20"}): pass with SVGWriter.SVGLine( xS, Coord.Pt(Coord.baseUnitsDim(900), Coord.baseUnitsDim(300)), Coord.Pt(Coord.baseUnitsDim(1100), Coord.baseUnitsDim(100)), {'stroke-width': "25"}): pass # print() # print(myF.getvalue()) # self.maxDiff = None self.assertEqual( """<?xml version='1.0' encoding="utf-8"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg height="4.000cm" version="1.1" width="12.000cm" xmlns="http://www.w3.org/2000/svg"> <desc>Example line01 - lines expressed in user coordinates</desc> <rect fill="none" height="398.000px" stroke="blue" stroke-width="2" width="1198.000px" x="1.000px" y="1.000px"/> <g stroke="green"> <line stroke-width="5" x1="100.000px" x2="300.000px" y1="300.000px" y2="100.000px"/> <line stroke-width="10" x1="300.000px" x2="500.000px" y1="300.000px" y2="100.000px"/> <line stroke-width="15" x1="500.000px" x2="700.000px" y1="300.000px" y2="100.000px"/> <line stroke-width="20" x1="700.000px" x2="900.000px" y1="300.000px" y2="100.000px"/> <line stroke-width="25" x1="900.000px" x2="1100.000px" y1="300.000px" y2="100.000px"/> </g> </svg> """, myF.getvalue())