3.1. Color functions

Each color function takes an image from the previous layer as input and returns an altered image as result. Each function is actually a class that implements a few function(s):

  • __init__ (optional)
  • set_dimensions (optional)
  • apply

The class must be decorated with the ColorFunction decorator.

3.1.1. Example

A simple example which preallocates an image and, when called, serves out portions of that image:

import numpy as np
from fonteffects import ColorFunction, ConvertColorsToUnits
import editor.properties as prop

@ColorFunction
class Solid(object):
    """ Creates solid fill with a single color

    :param color: A color as (r,g,b) [0,255]
    """
    color = prop.ColorProperty( (255,255,255) )

    def __init__(self, *k, **kw):
        for name, value in kw.iteritems():
            try:
                setattr(self, name, eval(value) )
            except NameError:
                setattr(self, name, value )
            except TypeError, e:
                raise fu.FontException("Solid: Error while evaluating '%s':\n%s" % (value, str(e)))
        self.color = _convert_color_to_units( self.color )
        if len(self.color) == 3:
            self.color = (self.color[0], self.color[1], self.color[2], 1.0)

    def apply(self, info, glyph, startx, starty, size, maxsize, glyphimage, previmage):
        return previmage * self.color

3.1.2. Reference

fonteffects.ColorFunction(cls)[source]

Registers a class as a color function

cls.apply(self, info, glyph, startx, starty, size, maxsize, glyphimage, previmage)

(mandatory)

Renders a an image given the start offset, size and the glyph image. Returns a numpy array with same shape as the previmage.shape

Parameters:
  • info (SFontInfo) – The info struct for the current font
  • glyph (SGlyph) – The current glyph being rendered
  • startx (integer) – The offset into the “max glyph image”. Used with any preallocated image.
  • starty (integer) – The offset into the “max glyph image”. Used with any preallocated image.
  • size (2-tuple) – The size of the current glyph
  • maxsize (2-tuple) – The max size of any glyph in the font
  • glyphimage (np.array) – The gray scale numpy array with shape (1 channel)
  • previmage (np.array) – The image rendered by the previous layer. If this color function is the first, the previmage is the composite of the glyphimage. (4 channels)
Returns:

(np.array) The rendered image with same shape as previmage

cls.__init__(self, *k, **kw)

(optional)

Handle the input arguments given to the function in the font info file

Parameters:
  • k – -not used-
  • kw – The named attributes found in the function’s section in the font info file
cls.set_dimensions(self, maxwidth, maxheight)

(optional)

This call is made after init, to let the class preallocate or create data. Only called once per font.

Parameters:
  • maxwidth (integer) – The maximum expected width of any character in the font
  • maxheight (integer) – The maximum expected height of any character in the font