def testCtorFail_RootPos(self): """TreePlotTransform() constructor rootPos out of range.""" self.assertRaises( TreePlotTransform.ExceptionTreePlotTransformRangeCtor, TreePlotTransform.TreePlotTransform, Coord.zeroBaseUnitsBox(), rootPos='whatever')
def setUp(self): self._canvas = Coord.Box( Coord.Dim(300, None), Coord.Dim(500, None), ) self._box = Coord.Box( Coord.Dim(80, None), Coord.Dim(20, None), ) self._pt = Coord.Pt( Coord.Dim(17, None), Coord.Dim(29, None), )
def setUp(self): self._canvas = Coord.Box( Coord.Dim(300, None), Coord.Dim(800, None), ) self._pt = Coord.Pt( Coord.Dim(400, None), Coord.Dim(250, None), ) self._childIncS = [ Coord.Dim(36, None), Coord.Dim(101, None), Coord.Dim(74, None), ]
def _writeScaleControls(self, theSvg): """Write the text elements that control re-scaling.""" myAttrs = { 'class': self.CLASS_TEXT_SCALE, } myPointP = Coord.Pt( Coord.Dim(8.0, self.COMMON_UNITS), Coord.Dim(4.0, self.COMMON_UNITS), ) with SVGWriter.SVGGroup(theSvg, {'id': 'scaleGroup'}): with SVGWriter.SVGText(theSvg, myPointP, None, None, myAttrs): theSvg.characters('Select scale (bold selected):') myAttrs['text-decoration'] = "underline" myPointP = Coord.newPt(myPointP, incX=Coord.Dim(64, 'mm'), incY=None) for scale in self.SCALE_FACTORS: myAttrs['onclick'] = "scaleGraphic(%s, '%s')" % (scale, scale) myAttrs['id'] = str(scale) if scale == self._scale: myAttrs['font-weight'] = 'bold' else: myAttrs['font-weight'] = 'normal' text = '%d%%' % int(scale * 100) with SVGWriter.SVGText(theSvg, myPointP, None, None, myAttrs): theSvg.characters(text) myPointP = Coord.newPt(myPointP, incX=Coord.Dim(5 * len(text), 'mm'), incY=None)
def testConvert(self): """Dim() convert().""" myObj = Coord.Dim(72, 'px') myObj = myObj.convert('px') self.assertEqual(myObj.value, 72) self.assertEqual(myObj.units, 'px') myObj = myObj.convert('in') self.assertEqual(myObj.value, 1.0) self.assertEqual(myObj.units, 'in')
def _plotTextOverlayHistogram(self, theSvg, theHistDl, theTpt): """Plot the text associated with a histogram.""" myCentreL = Coord.newPt(theHistDl, self._bb.width.scale(0.5), self.HIST_DEPTH.scale(0.5)) myPointP = theTpt.pt(myCentreL) # TODO: The myPointP.x.value + 2, myPointP.y.value - 2 # looks wrong. It is not using theTpt. myAttrs = { 'class' : self.CLASS_RECT_INVIS, 'onmouseover' : "showHistogram(%s, %s)" \ % (myPointP.x.value + 3, myPointP.y.value + 2), 'onmouseout' : "hideHistogram()", } myWidth = self._bb.width myBox = Coord.Box(myWidth, self.HIST_DEPTH) with SVGWriter.SVGRect(theSvg, theTpt.boxDatumP(theHistDl, myBox), theTpt.boxP(myBox), myAttrs): pass
def _fileNamePoint(self, theDatumL, theTpt): """Returns the point to plot the file name or None.""" if self._bb.hasSetArea: myDatumL = self._bb.plotPointSelf(theDatumL) textPointL = theTpt.tdcL(myDatumL, self._bb.box) # Logical increment to previous child is 'logical left' i.e. -x textPointL = Coord.newPt(textPointL, incX=self.FILE_PADDING.prev.scale(0.5), incY=None) return theTpt.pt(textPointL)
def _writeAlternateText(self, theSvg, thePoint, theId, theText, theAltS, yOffs=Coord.Dim(0, 'pt')): """Composes and writes the (pop-up) alternate text. thePoint is the physical point to locate both texts.""" # Write a grouping element and give it the alternate ID with SVGWriter.SVGGroup(theSvg, {'id' : 't%s%s' % (theId, self.ALT_ID_SUFFIX), 'opacity' : '0.0'}): altFontSize = self.ALT_FONT_PROPERTIES[self.ALT_FONT_FAMILY]['size'] altFontLenFactor = self.ALT_FONT_PROPERTIES[self.ALT_FONT_FAMILY]['lenFactor'] altFontHeightFactor = self.ALT_FONT_PROPERTIES[self.ALT_FONT_FAMILY]['heightFactor'] # Compute masking box for alternate maxChars = max([len(s) for s in theAltS]) # Take around 80% of character length boxWidth = Coord.Dim(altFontSize * maxChars * altFontLenFactor, 'pt') if len(theAltS) < 2: boxHeight = Coord.Dim(altFontSize * 2, 'pt') else: boxHeight = Coord.Dim(altFontSize * len(theAltS) * altFontHeightFactor, 'pt') boxAttrs = { 'fill' : self.ALT_RECT_FILL } with SVGWriter.SVGRect( theSvg, # Edge the plot point up and left by a bit Coord.newPt( thePoint, incX=Coord.Dim(-1 * altFontSize * (1 + len(theText) * altFontLenFactor / 2.0), 'pt'), incY=Coord.Dim(-1*altFontHeightFactor * altFontSize, 'pt') + yOffs, ), Coord.Box(boxWidth, boxHeight), boxAttrs, ): pass # As the main text is centered and the alt text is left # justified we need to move the text plot point left by a bit. myAltTextPt = Coord.newPt( thePoint, incX=Coord.Dim(-1 * altFontSize * len(theText) * altFontLenFactor / 2.0, 'pt'), incY=yOffs, ) with SVGWriter.SVGText(theSvg, myAltTextPt, 'Courier', altFontSize, { 'font-weight' : "normal", } ): self._writeStringListToTspan(theSvg, myAltTextPt, theAltS)
def writeAltTextAndMouseOverRect(self, theSvg, theId, theAltPt, theAltS, theTrigPt, theTrigRect): """Composes and writes the (pop-up) alternate text. Also writes a trigger rectangle.""" # Write a grouping element and give it the alternate ID with SVGWriter.SVGGroup(theSvg, { 'id': 't%s%s' % (theId, self.ALT_ID_SUFFIX), 'opacity': '0.0' }): altFontSize = self.ALT_FONT_PROPERTIES[ self.ALT_FONT_FAMILY]['size'] altFontLenFactor = self.ALT_FONT_PROPERTIES[ self.ALT_FONT_FAMILY]['lenFactor'] altFontHeightFactor = self.ALT_FONT_PROPERTIES[ self.ALT_FONT_FAMILY]['heightFactor'] # Compute masking box for alternate maxChars = max([len(s) for s in theAltS]) # Take around 80% of character length boxWidth = Coord.Dim(altFontSize * maxChars * altFontLenFactor, 'pt') if len(theAltS) < 2: boxHeight = Coord.Dim(altFontSize * 2, 'pt') else: boxHeight = Coord.Dim( altFontSize * len(theAltS) * altFontHeightFactor, 'pt') boxAttrs = {'fill': self.ALT_RECT_FILL} with SVGWriter.SVGRect( theSvg, theAltPt, Coord.Box(boxWidth, boxHeight), boxAttrs, ): pass # As the main text is centered and the alt text is left # justified we need to move the text plot point left by a bit. myAltTextPt = Coord.newPt( theAltPt, incX=Coord.Dim(1 * altFontSize * 3 * altFontLenFactor / 2.0, 'pt'), incY=Coord.Dim(12, 'pt'), ) with SVGWriter.SVGText(theSvg, myAltTextPt, 'Courier', altFontSize, { 'font-weight': "normal", }): self._writeStringListToTspan(theSvg, myAltTextPt, theAltS) # Add the trigger rectangle for writing on finalise boxAttrs = { 'class' : self.CLASS_RECT_INVIS, 'id' : 't%s' % theId, 'onmouseover' : "swapOpacity('t%s', 't%s')" \ % (theId, theId+self.ALT_ID_SUFFIX), 'onmouseout' : "swapOpacity('t%s', 't%s')" \ % (theId, theId+self.ALT_ID_SUFFIX), } self._triggerS.append((theTrigPt, theTrigRect, boxAttrs))
def test_pcTo(self): """TestPlotNodeBboxBoxyChildren.test_pcTo() - test pcTo().""" self.assertEqual(self._pnbcObj.pcTo(self._logicalDatum, 0), Coord.Pt( Coord.Dim(23, 'mm'), Coord.Dim(195, 'mm'), )) self.assertEqual(self._pnbcObj.pcTo(self._logicalDatum, 1), Coord.Pt( Coord.Dim(27, 'mm'), Coord.Dim(195, 'mm'), )) self.assertEqual(self._pnbcObj.pcTo(self._logicalDatum, 2), Coord.Pt( Coord.Dim(31, 'mm'), Coord.Dim(195, 'mm'), ))
def processIncGraphToSvg(theLex, theFilePath, theClass, tptPos, tptSweep): """Convert a Include graph from a PpLexer to SVG in theFilePath.""" myVis = FileIncludeGraph.FigVisitorTree(theClass) theLex.fileIncludeGraphRoot.acceptVisitor(myVis) # Tree is now a graph of: theClass myIgs = myVis.tree() # Pad the canvass myWidth = CANVAS_PADDING.prev \ + myIgs.plotCanvas.width \ + CANVAS_PADDING.next myDepth = CANVAS_PADDING.parent \ + myIgs.plotCanvas.depth \ + CANVAS_PADDING.child # Round up myWidth = Coord.Dim(int(myWidth.value+0.5), myWidth.units) myDepth = Coord.Dim(int(myDepth.value+0.5), myDepth.units) myCanvas = Coord.Box(myWidth, myDepth) #Create a plot configuration myTpt = TreePlotTransform.TreePlotTransform(myCanvas, tptPos, tptSweep) # Write to file myIgs.plotToFilePath(theFilePath, myTpt)
def test_convertPt(self): myPt = Coord.Pt( Coord.Dim(72, 'px'), Coord.Dim(36, 'px'), ) myResult = Coord.convertPt(myPt, 'in') expected = Coord.Pt( Coord.Dim(1, 'in'), Coord.Dim(0.5, 'in'), ) # print() # print(myResult) self.assertEqual(myResult, expected)
def test_zeroBaseUnitsPad(self): myPad = Coord.zeroBaseUnitsPad() # print() # print(myPad) self.assertEqual( myPad, Coord.Pad(prev=Coord.Dim(0.0, 'px'), next=Coord.Dim(0.0, 'px'), parent=Coord.Dim(0.0, 'px'), child=Coord.Dim(0.0, 'px')))
def testCmp_01(self): """Pt() cmp() is -1 for x.""" myPt_0 = Coord.Pt( Coord.Dim(1, 'in'), Coord.Dim(2, 'in'), ) myPt_1 = Coord.Pt( Coord.Dim(73, 'px'), Coord.Dim(12, 'pc'), ) self.assertTrue(myPt_0 < myPt_1)
def testCmp_04(self): """Pt() cmp() is +1 for y.""" myPt_0 = Coord.Pt( Coord.Dim(1, 'in'), Coord.Dim(2, 'in'), ) myPt_1 = Coord.Pt( Coord.Dim(72, 'px'), Coord.Dim(11, 'pc'), ) self.assertTrue(myPt_0 > myPt_1)
def testCmp_00(self): """Pt() cmp() is 0.""" myPt_0 = Coord.Pt( Coord.Dim(1, 'in'), Coord.Dim(2, 'in'), ) myPt_1 = Coord.Pt( Coord.Dim(72, 'px'), Coord.Dim(12, 'pc'), ) self.assertEqual(myPt_0, myPt_1) self.assertTrue(myPt_0 == myPt_1)
def testBbChildren(self): """Tests PlotNode() set/get bbChildren.""" myObj = PlotNode.PlotNodeBbox() myObj.bbChildren = Coord.Box( Coord.Dim(1, 'px'), Coord.Dim(2, 'px'), ) self.assertEqual(myObj.bbChildren, Coord.Box( Coord.Dim(1, 'px'), Coord.Dim(2, 'px'), )) self.assertEqual( myObj.bbChildrenWidth, Coord.Dim(1, 'px'), ) self.assertEqual( myObj.bbChildrenDepth, Coord.Dim(2, 'px'), )
def test_pcRoll(self): """TestPlotNodeBboxBoxyChildren.test_pcRoll() - test pcRoll().""" #parentChildTakeoffPoint #print #print 'myObj.parentChildTakeoffPoint[0]:', myObj.parentChildTakeoffPoint(myD, 0) self.assertEqual(self._pnbcObj.pcRoll(self._logicalDatum, 0), Coord.Pt( Coord.Dim(23, 'mm'), Coord.Dim(188, 'mm'), )) #print #print 'myObj.parentChildTakeoffPoint[1]:', myObj.parentChildTakeoffPoint(myD, 1) self.assertEqual(self._pnbcObj.pcRoll(self._logicalDatum, 1), Coord.Pt( Coord.Dim(27, 'mm'), Coord.Dim(188, 'mm'), )) #print #print 'myObj.parentChildTakeoffPoint[2]:', myObj.parentChildTakeoffPoint(myD, 2) self.assertEqual(self._pnbcObj.pcRoll(self._logicalDatum, 2), Coord.Pt( Coord.Dim(31, 'mm'), Coord.Dim(188, 'mm'), ))
def _plotSelfInternals(self, theSvg, theDl, theTpt): """Plot structures inside the box and the static text that is the file name.""" # Histograms of token types if self.__mustPlotSelfHistogram(): # First the histogram of just me myHistDl = self._bb.plotPointSelf(theDl) self._plotHistogram(theSvg, myHistDl, theTpt, self.tokenCounter) # Now the histogram of my children if self.__mustPlotChildHistogram(): # Shuffle down a bit myHistDl = Coord.newPt(myHistDl, None, self.HIST_DEPTH) self._plotHistogram(theSvg, myHistDl, theTpt, self._tokenCounterChildren) # Now the Chevron self._plotChevron(theSvg, theDl, theTpt) # The filename as display text (no animation) if not self.isRoot: self._plotFileName(theSvg, theDl, theTpt)
def testConvert_01(self): """Pt() convert() [01].""" myPt_0 = Coord.Pt( Coord.Dim(20, 'mm'), Coord.Dim(10, 'mm'), ) myPt_1 = Coord.Pt( Coord.Dim(20 * 72 / 25.4, 'px'), Coord.Dim(10 * 72 / 25.4, 'px'), ) myPt_0 = myPt_0.convert('px') #print myPt_1 self.assertEqual(myPt_0, myPt_1) self.assertTrue(myPt_0 == myPt_1)
def testConvert_00(self): """Pt() convert() [00].""" myPt_0 = Coord.Pt( Coord.Dim(1, 'in'), Coord.Dim(2, 'in'), ) myPt_1 = Coord.Pt( Coord.Dim(72, 'px'), Coord.Dim(12, 'pc'), ) myPt_1 = myPt_1.convert('in') #print myPt_1 self.assertEqual(myPt_0, myPt_1) self.assertTrue(myPt_0 == myPt_1)
def test_scale(self): """Pt() scale() [01].""" myPt = Coord.Pt( Coord.Dim(8, 'mm'), Coord.Dim(4, 'mm'), ) expected = Coord.Pt( Coord.Dim(4, 'mm'), Coord.Dim(2, 'mm'), ) myPt_scaled = myPt.scale(0.5) #print myPt_1 self.assertEqual(myPt_scaled, expected) self.assertTrue(myPt_scaled == expected)
def testIncWD_right(self): """TreePlotTransform.tdcL()/.tdcP(): right.""" myObj = TreePlotTransform.TreePlotTransform(self._canvas, rootPos='right') self.assertEqual( myObj.tdcL(self._pt, self._box), Coord.Pt( Coord.Dim(17, None), Coord.Dim(39, None), ), ) self.assertEqual( myObj.tdcP(self._pt, self._box), Coord.Pt( Coord.Dim(461, None), Coord.Dim(17, None), ), )
def testIncWD_left(self): """TreePlotTransform.tdcL()/.tdcP(): left.""" myObj = TreePlotTransform.TreePlotTransform(self._canvas, rootPos='left') self.assertEqual( myObj.tdcL(self._pt, self._box), Coord.Pt( Coord.Dim(97, None), Coord.Dim(39, None), ), ) self.assertEqual( myObj.tdcP(self._pt, self._box), Coord.Pt( Coord.Dim(39, None), Coord.Dim(203, None), ), )
def testIncWD_bottom(self): """TreePlotTransform.tdcL()/.tdcP(): bottom.""" myObj = TreePlotTransform.TreePlotTransform(self._canvas, rootPos='bottom') self.assertEqual( myObj.tdcL(self._pt, self._box), Coord.Pt( Coord.Dim(57, None), Coord.Dim(49, None), ), ) self.assertEqual( myObj.tdcP(self._pt, self._box), Coord.Pt( Coord.Dim(243, None), Coord.Dim(451, None), ), )
def testIncWD_top(self): """TreePlotTransform.tdcL()/.tdcP(): top.""" myObj = TreePlotTransform.TreePlotTransform(self._canvas, rootPos='top') self.assertEqual( myObj.tdcL(self._pt, self._box), Coord.Pt( Coord.Dim(57, None), Coord.Dim(29, None), ), ) self.assertEqual( myObj.tdcP(self._pt, self._box), Coord.Pt( Coord.Dim(57, None), Coord.Dim(29, None), ), )
def test_bdcL_bdcP_left(self): """TreePlotTransform.bdcL()/.bdcP(): left.""" myObj = TreePlotTransform.TreePlotTransform(self._canvas, rootPos='left') self.assertEqual( myObj.bdcL(self._pt, self._box), Coord.Pt( Coord.Dim(17, None), Coord.Dim(39, None), ), ) self.assertEqual( myObj.bdcP(self._pt, self._box), Coord.Pt( Coord.Dim(39, None), Coord.Dim(283, None), ), )
def setUp(self): self._boxDefault = Coord.Box( Coord.Dim(300, None), Coord.Dim(500, None), )
def test_right_pos(self): """TestTreePlotTransform.incPhysicalChildPos(): right, +""" myObj = TreePlotTransform.TreePlotTransform(self._canvas, rootPos='right', sweepDir='+') childPhysicalPt = Coord.Pt( Coord.Dim(550, None), Coord.Dim(400, None), ) #print myObj.pt(self._pt) # Starting point self.assertEqual(myObj.pt(self._pt), childPhysicalPt) #return # First increment childPhysicalPt = myObj.incPhysicalChildPos(childPhysicalPt, self._childIncS[0]) self.assertEqual( childPhysicalPt, Coord.Pt( Coord.Dim(550, None), Coord.Dim(400 - 36, None), )) # Second increment childPhysicalPt = myObj.incPhysicalChildPos(childPhysicalPt, self._childIncS[1]) self.assertEqual( childPhysicalPt, Coord.Pt( Coord.Dim(550, None), Coord.Dim(400 - 36 - 101, None), )) # Third increment childPhysicalPt = myObj.incPhysicalChildPos(childPhysicalPt, self._childIncS[2]) self.assertEqual( childPhysicalPt, Coord.Pt( Coord.Dim(550, None), Coord.Dim(400 - 36 - 101 - 74, None), ))
def testCtorMt(self): """TreePlotTransform() constructor defaults.""" myO = TreePlotTransform.TreePlotTransform(Coord.zeroBaseUnitsBox()) self.assertEqual(myO.rootPos, 'top') self.assertEqual(myO.sweepDir, '-')