def extendLegend(self): """Grow the legend pixmap enough to accommodate one more legend entry. Args: None Returns: None Raises: Any exceptions raised by the InaSAFE library will be propogated. """ LOGGER.debug('InaSAFE Map Legend extendLegend called') if self.legendImage is None: self.legendImage = QtGui.QImage(self.legendWidth, 95, QtGui.QImage.Format_RGB32) self.legendImage.setDotsPerMeterX(dpiToMeters(self.dpi)) self.legendImage.setDotsPerMeterY(dpiToMeters(self.dpi)) # Only works in Qt4.8 #self.legendImage.fill(QtGui.QColor(255, 255, 255)) # Works in older Qt4 versions self.legendImage.fill(255 + 255 * 256 + 255 * 256 * 256) myPainter = QtGui.QPainter(self.legendImage) myFontWeight = QtGui.QFont.Bold myItalicsFlag = False myFont = QtGui.QFont('verdana', self.legendFontSize, myFontWeight, myItalicsFlag) myPainter.setFont(myFont) myPainter.drawText(10, 25, self.legendTitle) if self.legendUnits is not None: myFontWeight = QtGui.QFont.StyleNormal myItalicsFlag = True legendUnitsFontSize = self.legendFontSize / 2 myFont = QtGui.QFont('verdana', legendUnitsFontSize, myFontWeight, myItalicsFlag) myPainter.setFont(myFont) myPainter.drawText(10, 45, self.legendUnits) else: # extend the existing legend down for the next class myImage = QtGui.QImage( self.legendWidth, self.legendImage.height() + self.legendIncrement, QtGui.QImage.Format_RGB32) myImage.setDotsPerMeterX(dpiToMeters(self.dpi)) myImage.setDotsPerMeterY(dpiToMeters(self.dpi)) # Only works in Qt4.8 #myImage.fill(QtGui.qRgb(255, 255, 255)) # Works in older Qt4 versions myImage.fill(255 + 255 * 256 + 255 * 256 * 256) myPainter = QtGui.QPainter(myImage) myRect = QtCore.QRectF(0, 0, self.legendImage.width(), self.legendImage.height()) myPainter.drawImage(myRect, self.legendImage, myRect) self.legendImage = myImage
def test_windowsDrawingArtifacts(self): """Test that windows rendering does not make artifacts""" # sometimes spurious lines are drawn on the layout LOGGER.info('Testing windowsDrawingArtifacts') myPath = unique_filename(prefix='artifacts', suffix='.pdf', dir=temp_dir('test')) myMap = Map(IFACE) setupPrinter(myPath) myMap.setupComposition() myImage = QtGui.QImage(10, 10, QtGui.QImage.Format_RGB32) myImage.setDotsPerMeterX(dpiToMeters(300)) myImage.setDotsPerMeterY(dpiToMeters(300)) #myImage.fill(QtGui.QColor(250, 250, 250)) # Look at the output, you will see antialiasing issues around some # of the boxes drawn... # myImage.fill(QtGui.QColor(200, 200, 200)) myImage.fill(200 + 200 * 256 + 200 * 256 * 256) myFilename = os.path.join(temp_dir(), 'greyBox') myImage.save(myFilename, 'PNG') for i in range(10, 190, 10): myPicture = QgsComposerPicture(myMap.composition) myPicture.setPictureFile(myFilename) if qgisVersion() >= 10800: # 1.8 or newer myPicture.setFrameEnabled(False) else: myPicture.setFrame(False) myPicture.setItemPosition(i, # x i, # y 10, # width 10) # height myMap.composition.addItem(myPicture) # Same drawing drawn directly as a pixmap myPixmapItem = myMap.composition.addPixmap( QtGui.QPixmap.fromImage(myImage)) myPixmapItem.setOffset(i, i + 20) # Same drawing using our drawImage Helper myWidthMM = 1 myMap.drawImage(myImage, myWidthMM, i, i + 40) myImagePath, _, _ = myMap.renderComposition() # when this test no longer matches our broken render hash # we know the issue is fixed myControlImages = ['windowsArtifacts.png'] myTolerance = 0 myFlag, myMessage = checkImages(myControlImages, myImagePath, myTolerance) myMessage += ('\nWe want these images to match, if they do not ' 'there may be rendering artifacts in windows.\n') assert myFlag, myMessage
def test_windowsDrawingArtifacts(self): """Test that windows rendering does not make artifacts""" # sometimes spurious lines are drawn on the layout LOGGER.info('Testing windowsDrawingArtifacts') myPath = unique_filename(prefix='artifacts', suffix='.pdf', dir=temp_dir('test')) myMap = Map(IFACE) setupPrinter(myPath) myMap.setupComposition() myImage = QtGui.QImage(10, 10, QtGui.QImage.Format_RGB32) myImage.setDotsPerMeterX(dpiToMeters(300)) myImage.setDotsPerMeterY(dpiToMeters(300)) #myImage.fill(QtGui.QColor(250, 250, 250)) # Look at the output, you will see antialiasing issues around some # of the boxes drawn... # myImage.fill(QtGui.QColor(200, 200, 200)) myImage.fill(200 + 200 * 256 + 200 * 256 * 256) myFilename = os.path.join(temp_dir(), 'greyBox') myImage.save(myFilename, 'PNG') for i in range(10, 190, 10): myPicture = QgsComposerPicture(myMap.composition) myPicture.setPictureFile(myFilename) if qgisVersion() >= 10800: # 1.8 or newer myPicture.setFrameEnabled(False) else: myPicture.setFrame(False) myPicture.setItemPosition(i, # x i, # y 10, # width 10) # height myMap.composition.addItem(myPicture) # Same drawing drawn directly as a pixmap myPixmapItem = myMap.composition.addPixmap( QtGui.QPixmap.fromImage(myImage)) myPixmapItem.setOffset(i, i + 20) # Same drawing using our drawImage Helper myWidthMM = 1 myMap.drawImage(myImage, myWidthMM, i, i + 40) myImagePath, _, _ = myMap.renderComposition() # when this test no longer matches our broken render hash # we know the issue is fixed myTolerance = 0 myFlag, myMessage = checkImages('windowsArtifacts.png', myImagePath, myTolerance) myMessage += ('\nWe want these images to match, if they do not ' 'there may be rendering artifacts in windows.\n') assert myFlag, myMessage
def extendLegend(self): """Grow the legend pixmap enough to accommodate one more legend entry. Args: None Returns: None Raises: Any exceptions raised by the InaSAFE library will be propogated. """ LOGGER.debug('InaSAFE Map Legend extendLegend called') if self.legendImage is None: self.legendImage = QtGui.QImage(self.legendWidth, 80, QtGui.QImage.Format_RGB32) self.legendImage.setDotsPerMeterX(dpiToMeters(self.dpi)) self.legendImage.setDotsPerMeterY(dpiToMeters(self.dpi)) # Only works in Qt4.8 #self.legendImage.fill(QtGui.QColor(255, 255, 255)) # Works in older Qt4 versions self.legendImage.fill(255 + 255 * 256 + 255 * 256 * 256) myPainter = QtGui.QPainter(self.legendImage) myFontWeight = QtGui.QFont.Bold myItalicsFlag = False myFont = QtGui.QFont('verdana', self.legendFontSize, myFontWeight, myItalicsFlag) myPainter.setFont(myFont) myPainter.drawText(10, 25, self.tr('Legend')) else: # extend the existing legend down for the next class myImage = QtGui.QImage(self.legendWidth, self.legendImage.height() + self.legendIncrement, QtGui.QImage.Format_RGB32) myImage.setDotsPerMeterX(dpiToMeters(self.dpi)) myImage.setDotsPerMeterY(dpiToMeters(self.dpi)) # Only works in Qt4.8 #myImage.fill(QtGui.qRgb(255, 255, 255)) # Works in older Qt4 versions myImage.fill(255 + 255 * 256 + 255 * 256 * 256) myPainter = QtGui.QPainter(myImage) myRect = QtCore.QRectF(0, 0, self.legendImage.width(), self.legendImage.height()) myPainter.drawImage(myRect, self.legendImage, myRect) self.legendImage = myImage
def testDpiToMeters(self): """Test conversion from dpi to dpm.""" myDpi = 300 myDpm = dpiToMeters(myDpi) myExpectedDpm = 11811.023622 myMessage = ('Conversion from dpi to dpm failed\n' ' Got: %s Expected: %s\n' % (myDpm, myExpectedDpm)) self.assertAlmostEqual(myDpm, myExpectedDpm, msg=myMessage)
def renderHtmlToImage(self, theHtml, theWidthMM): """Render some HTML to a pixmap. Args: * theHtml - HTML to be rendered. It is assumed that the html is a snippet only, containing no body element - a standard header and footer will be appended. * theWidthMM- width of the table in mm - will be converted to points based on the resolution of our page. Returns: QImage Raises: Any exceptions raised by the InaSAFE library will be propagated. """ LOGGER.debug('InaSAFE Map renderHtmlToImage called') myWidthPx = mmToPoints(theWidthMM, self.pageDpi) self.loadAndWait(theHtmlSnippet=theHtml) myFrame = self.webView.page().mainFrame() # Using 150dpi as the baseline, work out a standard text size # multiplier so that page renders equally well at different print # resolutions. #myBaselineDpi = 150 #myFactor = float(self.pageDpi) / myBaselineDpi #myFrame.setTextSizeMultiplier(myFactor) mySize = myFrame.contentsSize() mySize.setWidth(myWidthPx) self.webView.page().setViewportSize(mySize) myImage = QtGui.QImage(mySize, QtGui.QImage.Format_RGB32) myImage.setDotsPerMeterX(dpiToMeters(self.pageDpi)) myImage.setDotsPerMeterY(dpiToMeters(self.pageDpi)) # Only works in Qt4.8 #myImage.fill(QtGui.qRgb(255, 255, 255)) # Works in older Qt4 versions myImage.fill(255 + 255 * 256 + 255 * 256 * 256) myPainter = QtGui.QPainter(myImage) myFrame.render(myPainter) myPainter.end() myImage.save('/tmp/test.png') return myImage
def renderComposition(self): """Render the map composition to an image and save that to disk. Args: None Returns: tuple: * str: myImagePath - absolute path to png of rendered map * QImage: myImage - in memory copy of rendered map * QRectF: myTargetArea - dimensions of rendered map str: Absolute file system path to the rendered image. Raises: None """ LOGGER.debug('InaSAFE Map renderComposition called') # NOTE: we ignore self.composition.printAsRaster() and always rasterise myWidth = (int)(self.pageDpi * self.pageWidth / 25.4) myHeight = (int)(self.pageDpi * self.pageHeight / 25.4) myImage = QtGui.QImage(QtCore.QSize(myWidth, myHeight), QtGui.QImage.Format_ARGB32) myImage.setDotsPerMeterX(dpiToMeters(self.pageDpi)) myImage.setDotsPerMeterY(dpiToMeters(self.pageDpi)) # Only works in Qt4.8 #myImage.fill(QtGui.qRgb(255, 255, 255)) # Works in older Qt4 versions myImage.fill(55 + 255 * 256 + 255 * 256 * 256) myImagePainter = QtGui.QPainter(myImage) mySourceArea = QtCore.QRectF(0, 0, self.pageWidth, self.pageHeight) myTargetArea = QtCore.QRectF(0, 0, myWidth, myHeight) self.composition.render(myImagePainter, myTargetArea, mySourceArea) myImagePainter.end() myImagePath = unique_filename(prefix='mapRender_', suffix='.png', dir=temp_dir()) myImage.save(myImagePath) return myImagePath, myImage, myTargetArea