def readMarkColors(): settings = QSettings() size = settings.beginReadArray("misc/markColors") markColors = OrderedDict() if not size: # serialized in UFO form markColors["Red"] = colorToQColor("1,0,0,1") markColors["Yellow"] = colorToQColor("1,1,0,1") markColors["Green"] = colorToQColor("0,1,0,1") for i in range(size): settings.setArrayIndex(i) markColorName = settings.value("name", type=str) markColor = settings.value("color", type=str) markColors[markColorName] = colorToQColor(markColor) settings.endArray() return markColors
def _updateLayerAttributes(self, *_): font = self._font if font is None or font.layers is None: self.layerSetView.blockSignals(True) self.layerSetView.setList([[None, None, None]]) self.layerSetView.blockSignals(False) return layers = [] currentLayer = self._glyph.layer if self._glyph is not None else None currentRow = -1 for index, layer in enumerate(font.layers): color = layer.color if color is not None: color = colorToQColor(color) else: color = QColor() # XXX: add layer.visible layers.append([True, layer.name, color]) if layer == currentLayer: currentRow = index self.layerSetView.blockSignals(True) self.layerSetView.setList(layers) self.layerSetView.setCurrentItem(currentRow, 0) self.layerSetView.blockSignals(False) if self._shouldEditLastName: self.layerSetView.editItem(len(layers) - 1, 1) self._shouldEditLastName = False
def drawCellHeaderBackground(self, painter, rect): xMin, yMin, width, height = rect # background if self.shouldDrawMarkColor and self.glyph.markColor is not None: color = colorToQColor(self.glyph.markColor) elif self.glyph.dirty: color = cellDirtyColor else: color = Qt.white painter.fillRect(xMin, yMin, width, height, color)
def drawCellBackground(self, painter, rect): if self.shouldDrawMarkColor: markColor = self.glyph.markColor if markColor is not None: color = colorToQColor(markColor) markGradient = QLinearGradient( 0, 0, 0, self.height - GlyphCellHeaderHeight) markGradient.setColorAt(1.0, color) markGradient.setColorAt(0.0, color.lighter(115)) painter.fillRect(*(rect + (markGradient, )))
def _updateLayerAttributes(self, notification=None): self.layerSetView.setList([]) font = self._font if None in (font, font.layers): return layers = [] for layer in font.layers: color = layer.color if color is not None: color = colorToQColor(color) else: color = QColor() layers.append([color, layer.name]) self.layerSetView.setList(layers)
def drawCellBackground(self, painter, rect): if self.shouldDrawMarkColor: markColor = self.glyph.markColor if markColor is not None: color = colorToQColor(markColor) color.setAlphaF(.7 * color.alphaF()) painter.fillRect(*(rect+(color,))) if self.shouldDrawHeader: if self.glyph.dirty: x, y, w, h = rect painter.fillRect(*(rect+(cellDirtyColor,))) path = QPainterPath() path.moveTo(x + w - 12, 0) path.lineTo(x + w, 0) path.lineTo(x + w, 12) path.closeSubpath() painter.fillPath(path, QColor(255, 0, 0, 170))
def QPixmapFactory(image): font = image.font if font is None: return None layer = image.layer images = font.images if image.fileName not in images: return None imageColor = image.color data = images[image.fileName] pixmap = QPixmap() pixmap.loadFromData(data) if imageColor is not None: colorEffect = QGraphicsColorizeEffect() colorEffect.setColor(colorToQColor(imageColor)) colorEffect.setStrength(.8) return applyEffectToPixmap(pixmap, colorEffect) return pixmap
def readMarkColors(): settings = QSettings() size = settings.beginReadArray("misc/markColors") if not size: markColors = [ [QColor(255, 0, 0), "Red"], [QColor(255, 255, 0), "Yellow"], [QColor(0, 255, 0), "Green"], ] else: markColors = [] for i in range(size): settings.setArrayIndex(i) markColor = settings.value("color", type=str) markColorName = settings.value("name", type=str) markColors.append([colorToQColor(markColor), markColorName]) settings.endArray() return markColors
def QPixmapFactory(image): font = image.font if font is None: return None _ = image.layer images = font.images if image.fileName not in images: return None imageColor = image.color data = images[image.fileName] pixmap = QPixmap() pixmap.loadFromData(data) if imageColor is not None: colorEffect = QGraphicsColorizeEffect() colorEffect.setColor(colorToQColor(imageColor)) colorEffect.setStrength(0.8) return applyEffectToPixmap(pixmap, colorEffect) return pixmap
def drawCellGlyph(self, painter): if self.shouldDrawLayers: layers = self.font.layers for layerName in reversed(layers.layerOrder): layer = layers[layerName] if self.glyph.name not in layer: continue layerColor = None if layer.color is not None: layerColor = colorToQColor(layer.color) if layerColor is None: layerColor = Qt.black glyph = layer[self.glyph.name] path = glyph.getRepresentation("defconQt.QPainterPath") painter.fillPath(path, layerColor) else: path = self.glyph.getRepresentation("defconQt.QPainterPath") painter.fillPath(path, Qt.black)
def drawGlyphAnchors(painter, glyph, scale, drawAnchors=True, drawSelection=True, drawText=True, color=None): if not glyph.anchors: return if color is None: color = defaultColor("glyphAnchor") fallbackColor = color anchorSize = 9 * scale selectedAnchorSize = 11 * scale for anchor in glyph.anchors: if anchor.color is not None: color = colorToQColor(anchor.color) else: color = fallbackColor x, y = anchor.x, anchor.y name = anchor.name painter.save() if drawAnchors: if drawSelection and anchor.selected: size = selectedAnchorSize else: size = anchorSize path = lozengePath(x, y, size) painter.fillPath(path, color) if drawText and name and drawSelection and anchor.selected: painter.setPen(color) # TODO: we're using + before we shift to top, ideally this should # be abstracted w drawTextAtPoint taking a dy parameter that will # offset the drawing region from origin regardless of whether we # are aligning to top or bottom. y += 6 * scale drawTextAtPoint(painter, name, x, y, scale, xAlign="center", yAlign="top") painter.restore()
def drawGlyphFillAndStroke(painter, glyph, scale, drawFill=True, drawStroke=True, drawSelection=True, drawComponentFill=True, drawComponentStroke=False, contourFillColor=None, contourStrokeColor=None, componentFillColor=None, componentStrokeColor=None, selectionColor=None, partialAliasing=True): if glyph.template: if glyph.unicode is None: return text = chr(glyph.unicode) font = glyph.font height = 750 if font is not None and font.info.ascender: height = font.info.ascender painter.save() font = platformSpecific.otherUIFont() font.setPointSize(height) painter.setFont(font) color = QColor(Qt.lightGray) color.setAlphaF(.4) painter.setPen(color) metrics = painter.fontMetrics() xOffset = -(metrics.width(text) - glyph.width) / 2 painter.translate(xOffset, 0) painter.scale(1, -1) painter.drawText(0, 0, text) painter.restore() return # get the layer color layer = glyph.layer layerColor = None if layer is not None and layer.color is not None: layerColor = colorToQColor(layer.color) if selectionColor is None: selectionColor = defaultColor("glyphSelection") # get the paths contourPath = glyph.getRepresentation("defconQt.NoComponentsQPainterPath") componentPath, selectedComponentPath, originPts = glyph.getRepresentation( "TruFont.SelectedComponentsQPainterPath") painter.save() # fill # contours if drawFill: if contourFillColor is None: if layerColor is not None: contourFillColor = layerColor else: contourFillColor = defaultColor("glyphContourFill") painter.fillPath(contourPath, QBrush(contourFillColor)) # components if drawComponentFill: if componentFillColor is None: if layerColor is not None: componentFillColor = layerColor else: componentFillColor = defaultColor("glyphComponentFill") selectedComponentFillColor = QColor(componentFillColor) selectedComponentFillColor.setRed(0) selectedComponentFillColor.setGreen(0) selectedComponentFillColor.setBlue(0) painter.fillPath(componentPath, QBrush(componentFillColor)) if drawSelection: painter.fillPath(selectedComponentPath, QBrush(selectedComponentFillColor)) else: painter.fillPath(selectedComponentPath, QBrush(componentFillColor)) # components origin # TODO: make this a parameter, disable on sizes < MinDetails if drawSelection: painter.save() pen = QPen(componentFillColor) pen.setWidth(0) painter.setPen(pen) for x, y in originPts: painter.drawLine(x, y + 5 * scale, x, y) painter.drawLine(x, y, x + 4.5 * scale, y) painter.restore() # selection if drawSelection: selectionPath = glyph.getRepresentation( "TruFont.SelectedContoursQPainterPath") pen = QPen(selectionColor) pen.setWidthF(3.5 * scale) painter.setPen(pen) painter.drawPath(selectionPath) # stroke if drawStroke: # work out the color if contourStrokeColor is None: if layerColor is not None: contourStrokeColor = layerColor else: contourStrokeColor = defaultColor("glyphContourStroke") # contours pen = QPen(contourStrokeColor) pen.setWidth(0) painter.setPen(pen) if partialAliasing: drawGlyphWithAliasedLines(painter, glyph) else: painter.drawPath(contourPath) # components if drawComponentStroke: if componentStrokeColor is None: if layerColor is not None: componentStrokeColor = layerColor else: componentStrokeColor = defaultColor("glyphContourStroke") pen = QPen(componentStrokeColor) pen.setWidth(0) painter.setPen(pen) painter.drawPath(selectedComponentPath) painter.drawPath(componentPath) painter.restore()
def _drawGuidelines(painter, glyph, scale, rect, guidelines, drawLines=True, drawText=True, drawSelection=True, color=None): if not (drawLines or drawText): return xMin, yMin, width, height = rect xMax = xMin + width yMax = yMin + height for line in guidelines: color_ = color if color_ is None: if line.color: color_ = colorToQColor(line.color) else: color_ = defaultColor("glyphGuideline") painter.save() painter.setPen(color) line1 = None if None not in (line.x, line.y): if line.angle is not None: # make an infinite line that intersects *(line.x, line.y)* # 1. make horizontal line from *(line.x, line.y)* of length # *diagonal* diagonal = math.sqrt(width**2 + height**2) line1 = QLineF(line.x, line.y, line.x + diagonal, line.y) # 2. set the angle # defcon guidelines are clockwise line1.setAngle(line.angle) # 3. reverse the line and set length to 2 * *diagonal* line1.setPoints(line1.p2(), line1.p1()) line1.setLength(2 * diagonal) else: line1 = QLineF(xMin, line.y, xMax, line.y) textX = 0 textY = 0 if drawLines: if line1 is not None: # line drawLine(painter, line1.x1(), line1.y1(), line1.x2(), line1.y2()) # point x, y = line.x, line.y smoothWidth = 8 * scale smoothHalf = smoothWidth / 2.0 painter.save() pointPath = QPainterPath() x -= smoothHalf y -= smoothHalf pointPath.addEllipse(x, y, smoothWidth, smoothWidth) pen = QPen(color_) pen.setWidthF(1 * scale) painter.setPen(pen) if drawSelection and line.selected: painter.fillPath(pointPath, color_) painter.drawPath(pointPath) painter.restore() else: if line.y is not None: drawLine(painter, xMin, line.y, xMax, line.y) elif line.x is not None: drawLine(painter, line.x, yMin, line.x, yMax) if drawText and line.name: if line1 is not None: textX = line.x textY = line.y - 6 * scale xAlign = "center" else: if line.y is not None: fontSize = painter.font().pointSize() textX = glyph.width + 6 * scale textY = line.y - (fontSize / 3.5) * scale elif line.x is not None: textX = line.x + 6 * scale textY = 0 xAlign = "left" drawTextAtPoint(painter, line.name, textX, textY, scale, xAlign=xAlign) painter.restore()