Exemplo n.º 1
0
    def write_text(self, text, x, y, font, *args, **kwargs):
        # Set font family and size
        if font:
            write_font = font
        elif self.native.font:
            write_font = self.native.font
        else:
            raise ValueError("No font to write with")

        width, height = write_font.measure(text)
        textAttributes = NSMutableDictionary.alloc().init()
        textAttributes[NSFontAttributeName] = write_font._impl.native

        if "stroke_color" in kwargs and "fill_color" in kwargs:
            textAttributes[NSStrokeColorAttributeName] = native_color(
                kwargs["stroke_color"])
            # Apply negative NSStrokeWidthAttributeName to get stroke and fill
            textAttributes[
                NSStrokeWidthAttributeName] = -1 * kwargs["text_line_width"]
            textAttributes[NSForegroundColorAttributeName] = native_color(
                kwargs["fill_color"])
        elif "stroke_color" in kwargs:
            textAttributes[NSStrokeColorAttributeName] = native_color(
                kwargs["stroke_color"])
            textAttributes[NSStrokeWidthAttributeName] = kwargs[
                "text_line_width"]
        elif "fill_color" in kwargs:
            textAttributes[NSForegroundColorAttributeName] = native_color(
                kwargs["fill_color"])
        else:
            raise ValueError("No stroke or fill of write text")

        text_string = NSAttributedString.alloc().initWithString_attributes_(
            text, textAttributes)
        text_string.drawAtPoint(NSPoint(x, y - height))
Exemplo n.º 2
0
    def write_text(self, text, x, y, font, *args, **kwargs):
        width, height = self.measure_text(text, font)
        textAttributes = NSMutableDictionary.alloc().init()
        textAttributes[NSFontAttributeName] = font.bind(
            self.interface.factory).native

        if "stroke_color" in kwargs and "fill_color" in kwargs:
            textAttributes[NSStrokeColorAttributeName] = native_color(
                kwargs["stroke_color"])
            # Apply negative NSStrokeWidthAttributeName to get stroke and fill
            textAttributes[
                NSStrokeWidthAttributeName] = -1 * kwargs["text_line_width"]
            textAttributes[NSForegroundColorAttributeName] = native_color(
                kwargs["fill_color"])
        elif "stroke_color" in kwargs:
            textAttributes[NSStrokeColorAttributeName] = native_color(
                kwargs["stroke_color"])
            textAttributes[NSStrokeWidthAttributeName] = kwargs[
                "text_line_width"]
        elif "fill_color" in kwargs:
            textAttributes[NSForegroundColorAttributeName] = native_color(
                kwargs["fill_color"])
        else:
            raise ValueError("No stroke or fill of write text")

        text_string = NSAttributedString.alloc().initWithString_attributes_(
            text, textAttributes)
        text_string.drawAtPoint(NSPoint(x, y - height))
Exemplo n.º 3
0
Arquivo: fonts.py Projeto: pybee/toga
 def measure(self, text, tight=False):
     textAttributes = NSMutableDictionary.alloc().init()
     textAttributes[NSFontAttributeName] = self.native
     text_string = NSAttributedString.alloc().initWithString_attributes_(text, textAttributes)
     size = text_string.size()
     size.width += 3
     return size.width, size.height
Exemplo n.º 4
0
 def measure(self, text, tight=False):
     textAttributes = NSMutableDictionary.alloc().init()
     textAttributes[NSFontAttributeName] = self.native
     text_string = NSAttributedString.alloc().initWithString_attributes_(
         text, textAttributes)
     size = text_string.size()
     size.width += 3
     return size.width, size.height
Exemplo n.º 5
0
    def drawWithFrame_inView_(self, cellFrame: NSRect, view) -> None:
        # The data to display.
        try:
            label = self.objectValue.attrs['label']
            icon = self.objectValue.attrs['icon']
        except AttributeError:
            # Value is a simple string.
            label = self.objectValue
            icon = None

        if icon and icon.native:
            offset = 28.5

            NSGraphicsContext.currentContext.saveGraphicsState()
            yOffset = cellFrame.origin.y
            if view.isFlipped:
                xform = NSAffineTransform.transform()
                xform.translateXBy(8, yBy=cellFrame.size.height)
                xform.scaleXBy(1.0, yBy=-1.0)
                xform.concat()
                yOffset = 0.5 - cellFrame.origin.y

            interpolation = NSGraphicsContext.currentContext.imageInterpolation
            NSGraphicsContext.currentContext.imageInterpolation = NSImageInterpolationHigh

            icon.native.drawInRect(NSRect(NSPoint(cellFrame.origin.x, yOffset),
                                          NSSize(16.0, 16.0)),
                                   fromRect=NSRect(
                                       NSPoint(0, 0),
                                       NSSize(icon.native.size.width,
                                              icon.native.size.height)),
                                   operation=NSCompositingOperationSourceOver,
                                   fraction=1.0)

            NSGraphicsContext.currentContext.imageInterpolation = interpolation
            NSGraphicsContext.currentContext.restoreGraphicsState()
        else:
            # No icon; just the text label
            offset = 5

        if label:
            # Find the right color for the text
            if self.isHighlighted():
                primaryColor = NSColor.alternateSelectedControlTextColor
            else:
                if False:
                    primaryColor = NSColor.disabledControlTextColor
                else:
                    primaryColor = NSColor.textColor

            textAttributes = NSMutableDictionary.alloc().init()
            textAttributes[NSForegroundColorAttributeName] = primaryColor
            textAttributes[NSFontAttributeName] = NSFont.systemFontOfSize(13)

            at(label).drawAtPoint(NSPoint(cellFrame.origin.x + offset,
                                          cellFrame.origin.y),
                                  withAttributes=textAttributes)
Exemplo n.º 6
0
Arquivo: fonts.py Projeto: obulat/toga
    def measure(self, text, tight=False):
        textAttributes = NSMutableDictionary.alloc().init()
        textAttributes[NSFontAttributeName] = self.native
        text_string = NSAttributedString.alloc().initWithString_attributes_(text, textAttributes)
        size = text_string.size()

        # TODO: This is a magic fudge factor...
        # Replace the magic with SCIENCE.
        size.width += 3
        return size.width, size.height
Exemplo n.º 7
0
    def drawInteriorWithFrame_inView_(self, cellFrame: NSRect, view) -> None:
        # The data to display.
        icon = self.objectValue.attrs['icon']
        title = self.objectValue.attrs['title']
        subtitle = self.objectValue.attrs['subtitle']

        if icon and icon.native:
            NSGraphicsContext.currentContext.saveGraphicsState()
            yOffset = cellFrame.origin.y
            if view.isFlipped:
                xform = NSAffineTransform.transform()
                xform.translateXBy(4, yBy=cellFrame.size.height)
                xform.scaleXBy(1.0, yBy=-1.0)
                xform.concat()
                yOffset = 0.5 - cellFrame.origin.y

            interpolation = NSGraphicsContext.currentContext.imageInterpolation
            NSGraphicsContext.currentContext.imageInterpolation = NSImageInterpolationHigh

            icon.native.drawInRect(NSRect(
                NSPoint(cellFrame.origin.x, yOffset + 4), NSSize(40.0, 40.0)),
                                   fromRect=NSRect(
                                       NSPoint(0, 0),
                                       NSSize(icon.native.size.width,
                                              icon.native.size.height)),
                                   operation=NSCompositingOperationSourceOver,
                                   fraction=1.0)

            NSGraphicsContext.currentContext.imageInterpolation = interpolation
            NSGraphicsContext.currentContext.restoreGraphicsState()
        else:
            path = NSBezierPath.bezierPathWithRect(
                NSRect(NSPoint(cellFrame.origin.x, cellFrame.origin.y + 4),
                       NSSize(40.0, 40.0)))
            NSColor.grayColor.set()
            path.fill()

        if title:
            # Find the right color for the text
            if self.isHighlighted():
                primaryColor = NSColor.alternateSelectedControlTextColor
            else:
                if False:
                    primaryColor = NSColor.disabledControlTextColor
                else:
                    primaryColor = NSColor.textColor

            textAttributes = NSMutableDictionary.alloc().init()
            textAttributes[NSForegroundColorAttributeName] = primaryColor
            textAttributes[NSFontAttributeName] = NSFont.systemFontOfSize(15)

            at(title).drawAtPoint(NSPoint(cellFrame.origin.x + 48,
                                          cellFrame.origin.y + 4),
                                  withAttributes=textAttributes)

        if subtitle:
            # Find the right color for the text
            if self.isHighlighted():
                primaryColor = NSColor.alternateSelectedControlTextColor
            else:
                if False:
                    primaryColor = NSColor.disabledControlTextColor
                else:
                    primaryColor = NSColor.textColor

            textAttributes = NSMutableDictionary.alloc().init()
            textAttributes[NSForegroundColorAttributeName] = primaryColor
            textAttributes[NSFontAttributeName] = NSFont.systemFontOfSize(13)

            at(subtitle).drawAtPoint(NSPoint(cellFrame.origin.x + 48,
                                             cellFrame.origin.y + 24),
                                     withAttributes=textAttributes)