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 _plotTextOverlayTokenCountTable(self, theSvg, theDatumL, theTpt): """Plots the token count table as text+alternate text.""" assert (not self.isRoot) self.commentFunctionBegin(theSvg, File=self._fileName) myDatumL = self._bb.plotPointSelf(theDatumL) # Move to center myDatumL = Coord.newPt( myDatumL, incX=self._bb.width, #.scale(0.5), incY=None) # incY=self._bb.depth.scale(0.5)) triggerBoxL = self._bb.box if self.__mustPlotSelfHistogram(): myDatumL = Coord.newPt(myDatumL, None, self.HIST_DEPTH) triggerBoxL = Coord.Box(triggerBoxL.width, triggerBoxL.depth - self.HIST_DEPTH) if self.__mustPlotChildHistogram(): myDatumL = Coord.newPt(myDatumL, None, self.HIST_DEPTH) triggerBoxL = Coord.Box(triggerBoxL.width, triggerBoxL.depth - self.HIST_DEPTH) myDatumP = theTpt.pt(myDatumL) altTextS = self._altTextsForTokenCount() self.writeAltTextAndMouseOverRect( theSvg, theSvg.id, myDatumP, altTextS, myDatumP, theTpt.boxP(triggerBoxL), ) self.commentFunctionEnd(theSvg, File=self._fileName)
def _plotHistogram(self, theSvg, theHistDl, theTpt, theTokCounter): myTokCountTotal = theTokCounter.totalAllUnconditional # Avoid divide by zero errors assert (theTokCounter.tokenCountNonWs(isAll=False) > 0) assert (myTokCountTotal > 0) #myPos = Coord.Dim(0, self.COMMON_UNITS) myHistDl = theHistDl #self._bb.plotPointSelf(theDl) for k, myFill in self.HIST_PP_TOKEN_TYPES_COLOURS: tCount = theTokCounter.tokenCount(k, isAll=False) if tCount > 0: myWidth = self._bb.width.scale(tCount / (1.0 * myTokCountTotal)) myBox = Coord.Box(myWidth, self.HIST_DEPTH) # Convert to physical and plot with SVGWriter.SVGRect( theSvg, theTpt.boxDatumP(myHistDl, myBox), theTpt.boxP(myBox), { 'fill': myFill, 'stroke': self.HIST_RECT_COLOUR_STROKE, 'stroke-width': self.HIST_RECT_STROKE_WIDTH, }, ): pass # Increment the datum myHistDl = Coord.newPt(myHistDl, incX=myWidth, incY=None)
def _plotFileNameStackPopup(self, theSvg, theDatumL, theTpt, idStack): """Writes out the file name at the top with a pop-up with the absolute path.""" self.commentFunctionBegin(theSvg, File=self._fileName) if self._bb.hasSetArea: textPointP = self._fileNamePoint(theDatumL, theTpt) assert textPointP is not None # Make the trigger box 14 points high to cover the 12 pt text and # 12pts per char in the triggerBoxP = Coord.Box( Coord.baseUnitsDim(12 * len(os.path.basename(self.nodeName))), Coord.baseUnitsDim(14)) triggerPointP = Coord.newPt( textPointP, triggerBoxP.width.scale(-0.5), triggerBoxP.depth.scale(-0.5), ) self.writeAltTextAndMouseOverRect( theSvg, theSvg.id, textPointP, self._fileIdStackToListOfStr(idStack), triggerPointP, triggerBoxP, ) self.commentFunctionEnd(theSvg, File=self._fileName)
def _plotTextOverlay(self, theSvg, theDatumL, theTpt, idStack): """Plots all the text associated with the parent and child. We write the hidden objects first then the visible objects. This is because the hidden objects are controlled onmouseover/onmouseout on the visible objects and they have to be later in the SVG file for this to work.""" self.commentFunctionBegin(theSvg, File=self._fileName, Node=self.nodeName) # TODO: Re-think this as it is non-intuitive. Essentially it should not # matter whether the child or the parent node is plotted first but it is # essential that all the 'hidden' DHTML text for any node is written # first, _then_ the 'visible' text that controls that 'hidden' text. # # Child information first if len(self._children) > 0 and not self.isRoot: self._plotTextOverlayChildren(theSvg, theDatumL, theTpt) # Histogram if self.__mustPlotSelfHistogram(): # Write a single '?' in the middle myHistDl = self._bb.plotPointSelf(theDatumL) self._plotTextOverlayHistogram(theSvg, myHistDl, theTpt) if self.__mustPlotChildHistogram(): # Shuffle down a bit myHistDl = Coord.newPt(myHistDl, None, self.HIST_DEPTH) self._plotTextOverlayHistogram(theSvg, myHistDl, theTpt) if not self.isRoot: self._plotTextOverlayTokenCountTable(theSvg, theDatumL, theTpt) self._plotFileNameStackPopup(theSvg, theDatumL, theTpt, idStack) self.commentFunctionEnd(theSvg, File=self._fileName, Node=self.nodeName)
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 _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 _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 _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 _plotChevron(self, theSvg, theDl, theTpt): """Plots a wedge to represent the relative number of tokens in me and my children. D------------------.------------------| | | |------------------.------------------| | | A-----------B------.------D-----------| | \ . / | | \ . / | | \ . / | | \ . / | | \ . / | ------------------\C/------------------ We plot in the order D moveto A moveto B lineto C lineto D lineto B """ mySelfTokCount = self.tokenCounter.tokenCountNonWs(isAll=False) if mySelfTokCount == 0 and self._numChildSigTokens == 0: return # Find D myDl = self._bb.plotPointSelf(theDl) # Point C, all use this myPtC = Coord.newPt(myDl, self._bb.width.scale(0.5), self._bb.depth) # Increment by one or two histogram depths to point A if self.__mustPlotSelfHistogram(): myDl = Coord.newPt(myDl, None, self.HIST_DEPTH) if self.__mustPlotChildHistogram(): myDl = Coord.newPt(myDl, None, self.HIST_DEPTH) # Figure out move to B if self._numChildSigTokens == 0: # Chevron takes full width polyLogicalPtS = [ myDl, myPtC, Coord.newPt(myDl, self._bb.width, None), ] else: ratioChevron = 1.0 * mySelfTokCount / (self._numChildSigTokens + mySelfTokCount) myChevronOffset = self._bb.width.scale(0.5 * ratioChevron) #theSvg.comment(' Chevron offset: %s ' % str(myChevronOffset)) # Offset to B myDl = Coord.newPt(myDl, self._bb.width.scale(0.5) - myChevronOffset, None) polyLogicalPtS = [ myDl, myPtC, Coord.newPt(myDl, myChevronOffset.scale(2.0), None), ] polyPhysicalPtS = [theTpt.pt(p) for p in polyLogicalPtS] #theSvg.comment(' \npolyPhysicalPtS: %s \n' % str([str(p) for p in polyPhysicalPtS])) j = 1 while j < len(polyPhysicalPtS): with SVGWriter.SVGLine( theSvg, polyPhysicalPtS[j - 1], polyPhysicalPtS[j], { 'stroke-width': "2", 'stroke': "black", }, ): pass j += 1
def _plotHistogramLegend(self, theSvg, theTpt): """Plot a standardised legend. This is plotted as a group within a defs.""" myDatumP = Coord.Pt( Coord.Dim(0.0, self.COMMON_UNITS), Coord.Dim(0.0, self.COMMON_UNITS), ) with SVGWriter.SVGGroup(theSvg, { 'id': self.HIST_LEGEND_ID, 'opacity': '0.0' }): idVal = 0 # Outline rectangle with SVGWriter.SVGRect( theSvg, myDatumP, Coord.Box( Coord.Dim(48.0, self.COMMON_UNITS), Coord.Dim(40.0, self.COMMON_UNITS), ), { 'fill': self.ALT_RECT_FILL, 'id': '%d' % idVal, }, ): idVal += 2 myDatumP = Coord.newPt( myDatumP, incX=Coord.Dim(2.0, self.COMMON_UNITS), incY=Coord.Dim(2.0, self.COMMON_UNITS), ) myTokIdxS = list(range(len(self.HIST_PP_TOKEN_TYPES_COLOURS))) if theTpt.positiveSweepDir: myTokIdxS.reverse() for iC in myTokIdxS: myBox = Coord.Box(self.HIST_DEPTH, self.HIST_DEPTH) # Convert to physical and plot with SVGWriter.SVGRect( theSvg, myDatumP, myBox, { 'fill': self.HIST_PP_TOKEN_TYPES_COLOURS[iC][1], 'stroke': self.HIST_RECT_COLOUR_STROKE, 'stroke-width': self.HIST_RECT_STROKE_WIDTH, 'id': '%d' % idVal }, ): idVal += 2 myTextDatumP = Coord.newPt( myDatumP, incX=self.HIST_DEPTH + Coord.Dim(2.0, self.COMMON_UNITS), incY=self.HIST_DEPTH.scale(0.5), ) with SVGWriter.SVGText( theSvg, myTextDatumP, None, None, { 'font-family': 'Verdana', 'font-size': '10', 'dominant-baseline': 'middle', 'id': '%d' % idVal, }): theSvg.characters(self.HIST_PP_TOKEN_TYPES_COLOURS[iC][0]) idVal += 2 # Increment the datum myDatumP = Coord.newPt(myDatumP, incX=None, incY=self.HIST_DEPTH)
def _plotWhereWhyHow(self, theSvg, iChild, theDatumL, theTpt): """Plot description of Where/Why/How inclusion of a single child to a stream at the (self) logical datum point.""" assert (not self.isRoot) assert (len(self._children) > 0) assert (iChild >= 0 and iChild < len(self._children)) self.commentFunctionBegin(theSvg, File=self._fileName) #myDatumL = theDatumL #myDatumL = Coord.newPt(theDatumL, incX=self.FILE_PADDING.prev, incY=None)#self.FILE_PADDING.parent.scale(-1.0)) myDatumL = self._children[iChild].bb.plotPointSelf(theDatumL) # Move logical datum logically 'up' and 'right' by half # myDatumL = Coord.newPt( # myDatumL, # incX=self._children[iChild].bb.width.scale(0.5), # incY=self.FILE_PADDING.parent.scale(-0.5), # ) myAltTxtPointP = theTpt.pt( Coord.newPt( myDatumL, incX=self._children[iChild].bb.width.scale(0.5), incY=self.FILE_PADDING.parent.scale(-0.5), )) altTextS = [] altTextS.append('Where: %s#%d ' \ % (self.nodeName, self._children[iChild].lineNum)) if len(self._children[iChild].condComp) > 0: # altTextS.append( # ' Why: %s since: %s ' \ # % ( # str(self._children[iChild].condCompState), # self._children[iChild].condComp # ) # ) assert self._children[iChild].condCompState altTextS.append( ' Why: %s ' \ % ( self._children[iChild].condComp ) ) else: altTextS.append( ' Why: %s ' \ % (str(self._children[iChild].condCompState) ) ) altTextS.append(' How: #include %s' % ', '.join(self._children[iChild].findLogic)) # self.writeAltTextAndMouseOverText( # theSvg, myPointP, theSvg.id, # self.POPUP_TEXT, altTextS, Coord.Dim(20, 'pt')) triggerBoxP = theTpt.boxP( Coord.Box(self._children[iChild].bb.width, self.FILE_PADDING.parent)) # triggerPointP = theTpt.pt(myDatumL) triggerPointP = theTpt.pt( Coord.newPt( myDatumL, incX=self._children[iChild].bb.width, incY=self.FILE_PADDING.parent.scale(-1.0), )) self.writeAltTextAndMouseOverRect( theSvg, theSvg.id, myAltTxtPointP, altTextS, triggerPointP, triggerBoxP, ) self.commentFunctionEnd(theSvg, File=self._fileName)
def plotToFileObj(self, theFileObj, theTpt): """Root level call to plot to a file object. The SVG stream is created here.""" if self._numPassesToPlotSelf < 1: raise ValueError('No self._numPassesToPlotSelf set!') # Make viewBox user coordinates * self.VIEWBOX_SCALE myRootAttrs = { # 'viewBox' : '0 0 %d %d' \ # % ( # theTpt.canvasP().width.value * self.VIEWBOX_SCALE, # theTpt.canvasP().depth.value * self.VIEWBOX_SCALE, # ), 'xmlns:xlink' : self.NAMESPACE_XLINK, } # Bit of a hacky way to add enough margin for the pop-ups or rather # drop downs. This adds space for the bottom most boxes. canvasY = theTpt.canvasP().depth + Coord.Dim(60, 'mm') + Coord.Dim(8, 'mm') myCanvas = Coord.Box( theTpt.canvasP().width + Coord.Dim(60, 'mm'), canvasY, ) # Shrink canvas if it is a large plot yOffsetForScalingText = Coord.Dim(10, 'mm') scaleIdx = self.SCALE_FACTORS.index(1) assert scaleIdx >= 0 while scaleIdx > 0 and canvasY > self.SCALE_MAX_Y: canvasY = canvasY.scale(0.5) scaleIdx -= 1 self._scale = self.SCALE_FACTORS[scaleIdx] with SVGWriter.SVGWriter(theFileObj, myCanvas, myRootAttrs, mustIndent=cpip.INDENT_ML) as myS: # yOffsetForScalingText is applied wrong, should respect theTpt myDatum = Coord.Pt( CANVAS_PADDING.prev - yOffsetForScalingText, CANVAS_PADDING.parent, ) self.writePreamble(myS) myS.comment(' Root position: %s, Sweep direction: %s canvas=%s datum=%s' \ % (theTpt.rootPos, theTpt.sweepDir, theTpt.canvasP(), myDatum), newLine=True) # Shift the drawing down a bit to make way for the scale buttons. with SVGWriter.SVGGroup(myS, {'transform' : "translate(0, 24)"}): with SVGWriter.SVGGroup(myS, { 'id' : 'graphic', 'transform' : "scale(%s)" % self.SCALE_FACTORS[scaleIdx] }): # Apply a group element for scaling the plot # More hackery: yOffsetForScalingText is applied wrong, should respect theTpt with SVGWriter.SVGRect( myS, Coord.newPt( Coord.zeroBaseUnitsPt(), incX=None, incY=yOffsetForScalingText), theTpt.canvasP(), { 'fill' : 'none', 'stroke' : 'grey', 'stroke-width' : '2', }, ): pass # Start the plot self.plotInitialise(myS, myDatum, theTpt) # Now plot all contents for p in range(self._numPassesToPlotSelf): self.plotToSVGStream(myS, myDatum, theTpt, p, []) # Finish the plot self.plotFinalise(myS, myDatum, theTpt)