Ejemplo n.º 1
0
class FillParameters:
    '''
    Stores parameters for filling a shape, and can apply them to a context
    '''
    def __init__(self, pattern=Color(0), fill_rule=WINDING):
        '''
        Initialise the fill parameters
        :param pattern: the fill pattern or color to use, None for default
        :param fill_rule: the fill rule to use, None for default
        '''
        self.pattern = Color(0) if pattern is None else pattern
        self.fill_rule = WINDING if fill_rule is None else fill_rule

    def apply(self, ctx):
        '''
        Apply the settings to a context. After this, any fill operation using the context will use the
        settings.
        :param ctx: The context to apply the settinsg to.
        '''
        if isinstance(self.pattern, Color):
            ctx.set_source_rgba(*self.pattern)
        else:
            ctx.set_source(self.pattern.get_pattern())

        if self.fill_rule == WINDING:
            ctx.set_fill_rule(cairo.FillRule.WINDING)
        else:
            ctx.set_fill_rule(cairo.FillRule.EVEN_ODD)
Ejemplo n.º 2
0
class StrokeParameters:
    '''
    Stores parameters for stroking a shape, and can apply them to a context
    '''
    def __init__(self,
                 pattern=Color(0),
                 line_width=None,
                 dash=None,
                 cap=None,
                 join=None,
                 miter_limit=None):
        '''
        Initialise the stroke parameters
        :param pattern:  the fill pattern or color to use for the outline, None for default
        :param line_width: width of stroke line, None for default
        :param dash: dash patter of line, as for Pycairo, None for default
        :param cap: line end style, None for default
        :param join: line join style, None for default
        :param miter_limit: mitre limit, None for default
        '''
        self.pattern = Color(0) if pattern is None else pattern
        self.line_width = 1 if line_width is None else line_width
        self.dash = [] if dash is None else dash
        self.cap = SQUARE if cap is None else cap
        self.join = MITER if join is None else join
        self.miter_limit = 10 if miter_limit is None else miter_limit

    def apply(self, ctx):
        '''
        Apply the settings to a context. After this, any stroke operation using the context will use the
        settings.
        :param ctx: The context to apply the settinsg to.
        '''
        if isinstance(self.pattern, Color):
            ctx.set_source_rgba(*self.pattern)
        else:
            ctx.set_source(self.pattern.get_pattern())

        ctx.set_line_width(self.line_width)

        ctx.set_dash(self.dash)

        if self.cap == ROUND:
            ctx.set_line_cap(cairo.LineCap.ROUND)
        elif self.cap == BUTT:
            ctx.set_line_cap(cairo.LineCap.BUTT)
        else:
            ctx.set_line_cap(cairo.LineCap.SQUARE)

        if self.join == ROUND:
            ctx.set_line_join(cairo.LineJoin.ROUND)
        elif self.join == BEVEL:
            ctx.set_line_join(cairo.LineJoin.BEVEL)
        else:
            ctx.set_line_join(cairo.LineJoin.MITER)

        ctx.set_miter_limit(self.miter_limit)