def test_letter_spacing(self): # Modified letter spacing font = QFont(self._TestFont) font.setLetterSpacing(QFont.AbsoluteSpacing, 3.5) font.setPointSizeF(30) self.lyr.textFont = font self.checkTest()
def test_word_spacing(self): # Modified word spacing font = QFont(self._TestFont) font.setPointSizeF(30) font.setWordSpacing(20.5) self.lyr.textFont = font self.checkTest()
def test_text_size_map_unit(self): # Label text size in map units self.lyr.fontSizeInMapUnits = True font = QFont(self._TestFont) font.setPointSizeF(460) self.lyr.textFont = font self._Mismatches['TestCanvasPoint'] = 776 self._ColorTols['TestComposerPdfPoint'] = 2 self.checkTest()
def test_partials_labels_disabled(self): # Set Big font size font = QFont(self._TestFont) font.setPointSizeF(84) self.lyr.textFont = font # Disable partials labels self._Pal.setShowingPartialsLabels(False) self._Pal.saveEngineSettings() self.checkTest()
def test_partials_labels_enabled(self): # Set Big font size font = QFont(self._TestFont) font.setPointSizeF(84) self.lyr.textFont = font # Enable partials labels self._Pal.setShowingPartialsLabels(True) self._Pal.saveEngineSettings() self._Mismatches['TestCanvasPoint'] = 779 self._ColorTols['TestComposerPdfPoint'] = 2 self.checkTest()
def test_background_svg(self): # Label SVG background self.lyr.fontSizeInMapUnits = True font = QFont(self._TestFont) font.setPointSizeF(460) self.lyr.textFont = font self.lyr.shapeDraw = True self.lyr.shapeType = QgsPalLayerSettings.ShapeSVG svg = os.path.join(svgSymbolsPath(), "backgrounds", "background_square.svg") self.lyr.shapeSVGFile = svg self.lyr.shapeSizeUnits = QgsPalLayerSettings.MapUnits self.lyr.shapeSizeType = QgsPalLayerSettings.SizeBuffer self.lyr.shapeSize = QPointF(100.0, 0.0) self._Mismatches["TestComposerPdfVsComposerPoint"] = 580 self._Mismatches["TestCanvasPoint"] = 776 self._ColorTols["TestComposerPdfPoint"] = 2 self.checkTest()
def test_background_rect_w_offset(self): # Label rectangular background self._Mismatches['TestComposerImageVsCanvasPoint'] = 800 self._Mismatches['TestComposerImagePoint'] = 800 # verify fix for issues # http://hub.qgis.org/issues/9057 # http://gis.stackexchange.com/questions/86900 self.lyr.fontSizeInMapUnits = True font = QFont(self._TestFont) font.setPointSizeF(460) self.lyr.textFont = font self.lyr.shapeDraw = True self.lyr.shapeOffsetUnits = QgsPalLayerSettings.MapUnits self.lyr.shapeOffset = QPointF(-2900.0, -450.0) self._Mismatches['TestCanvasPoint'] = 774 self._ColorTols['TestComposerPdfPoint'] = 2 self.checkTest()
def test_background_svg(self): # Label SVG background self.lyr.fontSizeInMapUnits = True font = QFont(self._TestFont) font.setPointSizeF(460) self.lyr.textFont = font self.lyr.shapeDraw = True self.lyr.shapeType = QgsPalLayerSettings.ShapeSVG svg = os.path.join( svgSymbolsPath(), 'backgrounds', 'background_square.svg') self.lyr.shapeSVGFile = svg self.lyr.shapeSizeUnits = QgsPalLayerSettings.MapUnits self.lyr.shapeSizeType = QgsPalLayerSettings.SizeBuffer self.lyr.shapeSize = QPointF(100.0, 0.0) self._Mismatches['TestComposerPdfVsComposerPoint'] = 580 self._Mismatches['TestCanvasPoint'] = 776 self._ColorTols['TestComposerPdfPoint'] = 2 self.checkTest()
def std_font_to_qfont(font): # pylint: disable=too-many-branches """ Converts STD font to QFont """ name = font.font_name style_name = None # we need to sometimes strip 'Italic' or 'Bold' suffixes from the font name stored in the ESRI object # in order to match against actual font families keep_scanning = True while name not in QFontDatabase().families() and keep_scanning: keep_scanning = False if name.lower().endswith(' italic'): name = name[:-len(' italic')] keep_scanning = True elif name.lower().endswith(' bold'): name = name[:-len(' bold')] keep_scanning = True elif name.lower().endswith(' black'): name = name[:-len(' black')] style_name = 'Black' res = QFont(name) res.setWeight(font.weight) if font.weight > 400: res.setBold(True) if font.italic: res.setItalic(True) # pretty annoying, but because qgis relies on style strings, we need to convert the raw bools to style names if possible... if res.italic() and not res.bold(): styles = QFontDatabase().styles(res.family()) for s in styles: if s.lower() in ['oblique', 'italic']: res.setStyleName(s) break elif res.italic() and res.bold(): styles = QFontDatabase().styles(res.family()) for s in styles: if ('oblique' in s.lower() or 'italic' in s.lower()) and 'bold' in s.lower(): res.setStyleName(s) break elif res.bold(): styles = QFontDatabase().styles(res.family()) for s in styles: if s.lower() == 'bold': res.setStyleName(s) break if style_name is not None and style_name in QFontDatabase().styles( res.family()): res.setStyleName(style_name) if font.underline: res.setUnderline(True) if font.strikethrough: res.setStrikeOut(True) res.setPointSizeF(font.size) return res
def append_CharacterMarkerSymbolLayerAsSvg(symbol, layer, context: Context): # pylint: disable=too-many-locals """ Appends a CharacterMarkerSymbolLayer to a symbol, rendering the font character to an SVG file. """ font_family = layer.font character = chr(layer.unicode) color = symbol_color_to_qcolor(layer.color) angle = convert_angle(layer.angle) font = QFont(font_family) font.setPointSizeF(layer.size) # Using the rect of a painter path gives better results then using font metrics path = QPainterPath() path.setFillRule(Qt.WindingFill) path.addText(0, 0, font, character) rect = path.boundingRect() font_bounding_rect = QFontMetricsF(font).boundingRect(character) # adjust size -- marker size in esri is the font size, svg marker size in qgis is the svg rect size scale = rect.width() / font_bounding_rect.width() gen = QSvgGenerator() svg_path = symbol_name_to_filename(context.symbol_name, context.picture_folder, 'svg') gen.setFileName(svg_path) gen.setViewBox(rect) painter = QPainter(gen) painter.setFont(font) # todo -- size! if context.parameterise_svg: painter.setBrush(QBrush(QColor(255, 0, 0))) else: painter.setBrush(QBrush(color)) painter.setPen(Qt.NoPen) painter.drawPath(path) painter.end() if context.parameterise_svg: with open(svg_path, 'r') as f: t = f.read() t = t.replace('#ff0000', 'param(fill)') t = t.replace('fill-opacity="1" ', 'fill-opacity="param(fill-opacity)"') t = t.replace( 'stroke="none"', 'stroke="param(outline)" stroke-opacity="param(outline-opacity) 1" stroke-width="param(outline-width) 0"' ) with open(svg_path, 'w') as f: f.write(t) svg_path = context.convert_path(svg_path) out = QgsSvgMarkerSymbolLayer(svg_path) out.setSizeUnit(context.units) out.setSize(context.convert_size(scale * rect.width())) out.setAngle(angle) out.setFillColor(color) out.setStrokeWidth(0) out.setEnabled(layer.enabled) out.setLocked(layer.locked) out.setOffset( adjust_offset_for_rotation( QPointF(context.convert_size(layer.x_offset), -context.convert_size(layer.y_offset)), layer.angle)) out.setOffsetUnit(context.units) symbol.appendSymbolLayer(out)