Lenard Lindstrom avatar Lenard Lindstrom committed 27c1a4b

Add pygame.freetype.Face.render_raw_to method and enhance render_raw (refs #75)

The render_raw_to method draws glyphs in an array object exporting the
array struct interface. The render_raw method now has an 'invert' argument,
which makes the foreground alpha transparent, background opaque.

Additional unit tests needed.

Comments (0)

Files changed (9)

docs/_sources/ref/freetype.txt

 ======================
 
 .. module:: pygame.freetype
-   :synopsis: Enhanced Pygame module for loading and rendering fonts
+   :synopsis: Enhanced Pygame module for loading and rendering font faces
 
-| :sl:`Enhanced Pygame module for loading and rendering fonts`
+| :sl:`Enhanced Pygame module for loading and rendering font faces`
 
 --- Note that some features may change before a formal release
 
-This module allows for rendering all font formats supported by FreeType, namely
+This module allows for rendering all face formats supported by FreeType, namely
 ``TTF``, Type1, ``CFF``, OpenType, ``SFNT``, ``PCF``, ``FNT``, ``BDF``, ``PFR``
-and Type42 fonts. It can render any UTF-32 character in a font file.
+and Type42 faces. It can render any UTF-32 character in a font file.
 
 This module is optional, and replaces all of the functionality of the original
 'font' module, whilst expanding it. This module depends in no way on the
 use the module; if the module is available and loaded, it will be automatically
 initialized by ``pygame.init()``
 
-Most of the work done with fonts is done by using the actual Font objects. The
-module by itself only has routines to initialize itself and create Font objects
-with ``pygame.freetype.Font()``.
+Most of the work done with faces is done by using the actual Face objects. The
+module by itself only has routines to initialize itself and create Face objects
+with ``pygame.freetype.Face()``.
 
-You can load fonts from the system by using the ``pygame.freetype.SysFont()``
+You can load faces from the system by using the ``pygame.freetype.SysFont()``
 function. There are a few other functions to help lookup the system fonts.
 
+For now undefined character codes are replaced with the ``undefined character``.
+How undefined codes are handled may become configurable in a future release.
+
 Pygame comes with a builtin default font. This can always be accessed by
-passing None as the font name to the Font constructor.
+passing None as the font name to the Face constructor.
+
+New in Pygame 1.9.2
 
 .. function:: get_error
 
    Returns the description of the last error which occurred in the FreeType
    library, or None if no errors have occurred.
 
-   .. ## pygame.freetype.get_error ##
-
 .. function:: get_version
 
    | :sl:`Get the FreeType version`
    not compile with the original FreeType 1.0, hence the first element of the
    tuple will always be 2.
 
-   .. ## pygame.freetype.get_version ##
-
 .. function:: init
 
    | :sl:`Initialize the underlying FreeType 2 library.`
-   | :sg:`init(default_cache_size=64) -> None`
+   | :sg:`init(cache_size=64, resolution=72) -> None`
 
    This function initializes the underlying FreeType 2 library and must be
    called before trying to use any of the functionality of the 'freetype'
    Optionally, you may specify a default size for the Glyph cache: this is the
    maximum amount of glyphs that will be cached at any given time by the
    module. Exceedingly small values will be automatically tuned for
-   performance.
-
-   .. ## pygame.freetype.init ##
+   performance. Also a default pixel resolution, in dots per inch, can
+   be given to adjust face scaling.
 
 .. function:: quit
 
    results. It is safe to call this function even if the module hasn't been
    initialized yet.
 
-   .. ## pygame.freetype.quit ##
-
 .. function:: was_init
 
    | :sl:`Returns whether the the FreeType 2 library is initialized.`
 
    Returns whether the the FreeType 2 library is initialized.
 
-   .. ## pygame.freetype.was_init ##
+.. function:: get_default_resolution
 
-.. class:: Font
+   | :sl:`Return the default pixel size in dots per inch`
+   | :sg:`get_default_resolution() -> long`
 
-   | :sl:`Creates a new Font from a supported font file.`
-   | :sg:`Font(file, style=STYLE_NONE, ptsize=-1, face_index=0) -> Font`
+   Returns the default pixel size, in dots per inch for the module. At
+   initial module load time the value is 72.
+
+.. function:: set_default_resolution
+
+   | :sl:`Set the default pixel size in dots per inch for the module`
+   | :sg:`set_default_resolution([resolution]) -> None`
+
+   Set the default pixel size, in dots per inch, for the module. If the
+   optional argument is omitted or zero the resolution is reset to 72.
+
+.. function:: get_default_font
+
+   | :sl:`Get the filename of the default font`
+   | :sg:`get_default_font() -> string`
+
+   Return the filename of the system font. This is not the full path to the
+   file. This file can usually be found in the same directory as the font
+   module, but it can also be bundled in separate archives.
+
+.. class:: Face
+
+   | :sl:`Creates a new Face instance from a supported font file.`
+   | :sg:`Face(file, style=STYLE_NONE, ptsize=-1, face_index=0, vertical=0, ucs4=0, resolution=0) -> Face`
 
    'file' can be either a string representing the font's filename, a file-like
    object containing the font, or None; in this last case the default, built-in
    font will be used.
 
    Optionally, a \*ptsize* argument may be specified to set the default size in
-   points which will be used to render the font. Such size can also be
+   points which will be used to render the face. Such size can also be
    specified manually on each method call. Because of the way the caching
    system works, specifying a default size on the constructor doesn't imply a
    performance gain over manually passing the size on each function call.
 
    If the font file has more than one face, the \*index* argument may be
-   specified to specify which face index to load. Defaults to 0; font loading
+   specified to specify which face index to load. Defaults to 0; face loading
    will fail if the given index is not contained in the font.
 
-   The 'style' argument will set the default style (italic, underline, bold)
-   used to draw this font. This style may be overriden on any ``Font.render()``
+   The 'style' argument will set the default style (oblique, underline, strong)
+   used to draw this face. This style may be overriden on any ``Face.render()``
    call.
 
+   The optional vertical argument, an integer, sets the default orientation
+   for the face: 0 (False) for horizontal, any other value (True) for vertical.
+   See :attr:`Face.vertical`.
+
+   The optional ucs4 argument, an integer, sets the default text translation
+   mode: 0 (False) recognize UTF-16 surrogate pairs, any other value (True),
+   to treat unicode text as UCS-4, with no surrogate pairs. See
+   :attr:`Face.ucs4`.
+
+   The optional resolution argument sets the pixel size, in dots per inch,
+   to use for scaling glyphs for this Face instance. If 0 then the default
+   module value, set by :meth:`freetype.init`, is used. The Face object's
+   resolution can only be changed by reinitializing the instance.
+
    .. attribute:: name
 
       | :sl:`Gets the name of the font face.`
       Read only. Returns the real (long) name of the font type face, as
       specified on the font file.
 
-      .. ## Font.name ##
+   .. attribute:: path
 
-   .. method:: get_size
+      | :sl:`Gets the path of the font file`
+      | :sg:`path -> unicode`
 
-      | :sl:`Gets the size of rendered text`
-      | :sg:`get_size(text, style=STYLE_DEFAULT, rotation=0, ptsize=default, surrogates=True) -> (int, int)`
+      Read only. Returns the path of the loaded font file
 
-      Gets the size in pixels which 'text' will occupy when rendered using this
-      Font. The calculations will take into account the font's default style
-      (e.g. underlined fonts take extra height for the underline), or the style
-      may be overridden by the 'style' parameter.
+   .. method:: get_rect
 
-      Returns a tuple containing the width and height of the text's bounding
-      box.
+      | :sl:`Gets the size and offset of rendered text`
+      | :sg:`get_rect(text, style=STYLE_DEFAULT, rotation=0, ptsize=default) -> rect`
 
-      The calculations are done using the font's default size in points,
-      without any rotation, and taking into account fonts which are set to be
-      drawn vertically via the :meth:`Font.vertical` attribute. Optionally you
-      may specify another point size to use via the 'ptsize' argument, or a
-      text rotation via the 'rotation' argument.
+      Gets the final dimensions and origin, in pixels, of 'text' using the
+      current point size, style, rotation and orientation. These are either
+      taken from the arguments, if given, else from the default values set
+      for the face object.
 
-      The surrogates arguement controls Unicode text decoding. By default, the
-      freetype module performs UTF-16 surrogate pair decoding on Unicode text.
-      This allows 32-bit escape sequences ('\Uxxxxxxxx') between 0x10000 and
-      0x10FFFF to represent their corresponding UTF-32 code points on Python
-      interpreters built with a UCS-2 unicode type (on Windows, for instance).
-      It also means character values within the UTF-16 surrogate area (0xD800
-      to 0xDFFF) are considered part of a surrogate pair. A malformed surrogate
-      pair will raise an UnicodeEncodeError. Setting surrogates False turns
-      surrogate pair decoding off, letting interpreters with a UCS-4 unicode
-      type access the full UCS-4 character range.
+      Returns a rect containing the width and height of the text's bounding
+      box and the position of the text's origin. The origin can be used
+      to align separately rendered pieces of text. It gives the baseline
+      position and bearing at the start of the text.
 
       If text is a char (byte) string, then its encoding is assumed to be
       ``LATIN1``.
 
-      .. ## Font.get_size ##
-
    .. method:: get_metrics
 
-      | :sl:`Gets glyph metrics for the font's characters`
-      | :sg:`get_metrics(text, bbmode=BBOX_PIXEL_GRIDFIT, ptsize=default, surrogates=True) -> [(...), ...]`
+      | :sl:`Gets glyph metrics for the face's characters`
+      | :sg:`get_metrics(text, ptsize=default) -> [(...), ...]`
 
       Returns the glyph metrics for each character in 'text'.
 
 
       ::
 
-          (min_x, max_x, min_y, max_y, horizontal_advance)
+          (min_x, max_x, min_y, max_y, horizontal_advance_x, horizontal_advance_y)
 
-      By default, these values are returned as grid-fitted pixel coordinates
-      (ints) but one of the following constants may be passed as the bbmode
-      argument to change this:
+      The bounding box min_x, max_y, min_y, and max_y values are returned as
+      grid-fitted pixel coordinates of type int. The advance values are 
+      float values.
 
-      ::
-
-          BBOX_EXACT: Return accurate floating point values.
-          BBOX_EXACT_GRIDFIT: Return accurate floating point values aligned
-          to the drawing grid.
-          BBOX_PIXEL: Return pixel coordinates (ints).
-          BBOX_PIXEL_GRIDFIT (default): Return grid-aligned pixel coordinates.
-
-      The calculations are done using the font's default size in points.
+      The calculations are done using the face's default size in points.
       Optionally you may specify another point size to use.
 
-      Setting surrogates False turns off surrogate pair decoding (see
-      Font.get_size).
+      The metrics are adjusted for the current rotation, strong, and oblique
+      settings.
 
       If text is a char (byte) string, then its encoding is assumed to be
       ``LATIN1``.
 
-      .. ## Font.get_metrics ##
-
    .. attribute:: height
 
-      | :sl:`Gets the height of the Font`
+      | :sl:`Gets the unscaled height of the face in font units`
       | :sg:`height -> int`
 
-      Read only. Gets the height of the Font. This is the average value of all
-      glyphs in the font.
+      Read only. Gets the height of the face. This is the average value of all
+      glyphs in the face.
 
-      .. ## Font.height ##
+   .. method:: ascender
+
+      | :sl:`get the unscaled ascent of the face in font units`
+      | :sg:`ascender -> int`
+
+      Read only. Return the number of units from the face's baseline to
+      the top of the bounding box.
+
+   .. attribute:: descender
+
+      | :sl:`get the unscaled descent of the face in font units`
+      | :sg:`descender -> int`
+
+      Read only. Return the height in font units for the face descent.
+      The descent is the number of units from the face's baseline to the
+      bottom of the bounding box.
+
+   .. attribute:: get_sized_ascender
+
+      | :sl:`Gets the scaled ascent the face in pixels`
+      | :sg:`get_sized_ascender() -> int`
+
+      Return the number of units from the face's baseline to the top of the
+      bounding box. It is not adjusted for strong or rotation.
+
+   .. method:: get_sized_descender
+
+      | :sl:`Gets the scaled descent the face in pixels`
+      | :sg:`get_sized_descender() -> int`
+
+      Return the number of pixels from the face's baseline to the top of the
+      bounding box. It is not adjusted for strong or rotation.
+
+   .. attribute:: get_sized_height
+
+      | :sl:`Gets the scaled height of the face in pixels`
+      | :sg:`get_sized_height() -> int`
+
+      Read only. Gets the height of the face. This is the average value of all
+      glyphs in the face. It is not adjusted for strong or rotation.
+
+   .. method:: get_sized_glyph_height
+
+      | :sl:`Gets the scaled height of the face in pixels`
+      | :sg:`get_sized_glyph_height() -> int`
+
+      Return the glyph bounding box height of the face in pixels.
+      This is the average value of all glyphs in the face.
+      It is not adjusted for strong or rotation.
 
    .. method:: render
 
       | :sl:`Renders text on a surface`
-      | :sg:`render(dest, text, fgcolor, bgcolor=None, style=STYLE_DEFAULT, rotation=0, ptsize=default, surrogates=True) -> (Surface, Rect)`
+      | :sg:`render(text, fgcolor, bgcolor=None, style=STYLE_DEFAULT, rotation=0, ptsize=default) -> (Surface, Rect)`
 
-      Renders the string 'text' to a :mod:`pygame.Surface`, using the color
-      'fgcolor'.
+      Renturns a new :mod:`pygame.Surface`, with the text rendered to it
+      in the color given by 'fgcolor'. If ``bgcolor`` is given, the surface
+      will be filled with this color. If no background color is given,
+      the surface is filled with zero alpha opacity. Normally the returned
+      surface has a 32 bit pixel size. However, if ``bgcolor`` is ``None``
+      and antialiasing is disabled a two color 8 bit surface with colorkey
+      set for the background color is returned.
 
-      The 'dest' parameter is supposed to be a sequence containing the surface
-      and the coordinates at which the text will be rendered, in that order.
-      The sequence may be either (surf, posn) or (surf, x, y), where x and y
-      are numbers. posn can be any sequence, including Rect, for which the
-      first two elements are positions x and y. If x and y are not integers
-      they will be cast to int: ``int(x)``, ``int(y)``.
-
-      If such a sequence exists, and the destination surface is a valid
-      :mod:`pygame.Surface` (independently of its bit depth), the text will be
-      rendered directly on top of it at the passed coordinates, using the given
-      'fgcolor', and painting the background of the text with the given
-      'bgcolor', if available. The alpha values for both colors are always
-      taken into account.
-
-      If 'None' is passed instead of a destination sequence, a new 32 bit
-      :mod:`pygame.Surface` will be created with the required size to contain
-      the drawn text, and using \*bgcolor* as its background color. If a
-      background color is not available, the surface will be filled with zero
-      alpha opacity.
-
-      The return value is a tuple: the target surface and the bounding
-      rectangle giving the size and position of the rendered text within the
-      surface.
+      The return value is a tuple: the new surface and the bounding
+      rectangle giving the size and origin of the rendered text.
 
       If an empty string is passed for text then the returned Rect is zero
-      width and the height of the font. If dest is None the returned surface is
+      width and the height of the face. If dest is None the returned surface is
       the same dimensions as the boundary rect. The rect will test False.
 
-      The rendering is done using the font's default size in points and its
-      default style, without any rotation, and taking into account fonts which
-      are set to be drawn vertically via the :meth:`Font.vertical` attribute.
+      The rendering is done using the face's default size in points and its
+      default style, without any rotation, and taking into account faces which
+      are set to be drawn vertically via the :meth:`Face.vertical` attribute.
       Optionally you may specify another point size to use via the 'ptsize'
       argument, a text rotation via the 'rotation' argument, or a new text
       style via the 'style' argument.
 
-      Setting surrogates False turns off surrogate pair decoding (see
-      Font.get_size).
+      If text is a char (byte) string, then its encoding is assumed to be
+      ``LATIN1``.
+
+   .. method:: render_to
+
+      | :sl:`Renders text to an existing surface`
+      | :sg:`render(surf, dest, text, fgcolor, bgcolor=None, style=STYLE_DEFAULT, rotation=0, ptsize=default) -> Rect`
+
+      Renders the string 'text' to a :mod:`pygame.Surface` 'surf',
+      using the color 'fgcolor'.
+
+      Argument 'dest' is an (x, y) surface coordinate pair. If either x
+      or y is not an integer it is converted to one if possible.
+      Any sequence, including Rect, for which the first two elements are
+      positions x and y is accepted.
+
+      If a background color is given, the surface is first filled with that
+      color. The text is blitted next. Both the background fill and text
+      rendering involve full alpha blits. That is, the alpha values of
+      both the foreground and background colors, as well as those of the
+      destination surface if it has per-pixel alpha.
+
+      The return value is a rectangle giving the size and position of the
+      rendered text within the surface.
+
+      If an empty string is passed for text then the returned Rect is zero
+      width and the height of the face. The rect will test False.
+
+      By default, the point size and style set for the face are used
+      if not passed as arguments. The text is unrotated unless a non-zero
+      rotation value is given.
 
       If text is a char (byte) string, then its encoding is assumed to be
       ``LATIN1``.
 
-      .. ## Font.render ##
-
    .. method:: render_raw
 
       | :sl:`Renders text as a string of bytes`
-      | :sg:`render_raw(text, ptsize=default, surrogates=True) -> (bytes, (int, int))`
+      | :sg:`render_raw(text, style=STYLE_DEFAULT, rotation=0, ptsize=default, invert=False) -> (bytes, (int, int))`
 
-      Like ``Font.render(None, ...)`` but the tuple returned is an 8 bit
+      Like ``Face.render()`` but the tuple returned is an 8 bit
       monochrome string of bytes and its size. The forground color is 255, the
       background 0, useful as an alpha mask for a foreground pattern.
 
-      .. ## Font.render_raw ##
+   .. method:: render_raw_to
+
+      | :sl:`Renders text as a string of ints to an array`
+      | :sg:`render_raw_to(array, text, dest=None, style=STYLE_DEFAULT, rotation=0, ptsize=default, invert=False) -> (int, int)`
+
+      Render to an array object exposing an array struct interface. The array
+      must be two dimensional with integer items. The default dest value, None,
+      is equivalent to (0, 0).
 
    .. attribute:: style
 
-      | :sl:`Gets or sets the font's style`
+      | :sl:`Gets or sets the face's style`
       | :sg:`style -> int`
 
-      Gets or sets the default style of the Font. This default style will be
+      Gets or sets the default style of the Face. This default style will be
       used for all text rendering and size calculations unless overriden
       specifically in the \`render()` or \`get_size()` calls. The style value
       may be a bitwise ``OR`` of one or more of the following constants:
 
           STYLE_NONE
           STYLE_UNDERLINE
-          STYLE_ITALIC
-          STYLE_BOLD
+          STYLE_OBLIQUE
+          STYLE_STRONG
+	  STYLE_WIDE
 
       These constants may be found on the FreeType constants module.
       Optionally, the default style can be modified or obtained accessing the
-      individual style attributes (underline, italic, bold).
-
-      .. ## Font.style ##
+      individual style attributes (underline, oblique, strong).
 
    .. attribute:: underline
 
-      | :sl:`Gets or sets the font's underline style`
+      | :sl:`Gets or sets the face's underline style`
       | :sg:`underline -> bool`
 
-      Gets or sets whether the font will be underlined when drawing text. This
+      Gets or sets whether the face will be underlined when drawing text. This
       default style value will be used for all text rendering and size
       calculations unless overriden specifically in the \`render()` or
       \`get_size()` calls, via the 'style' parameter.
 
-      .. ## Font.underline ##
+   .. attribute:: strong
 
-   .. attribute:: bold
+      | :sl:`Gets or sets the face's strong style`
+      | :sg:`strong -> bool`
 
-      | :sl:`Gets or sets the font's bold style`
-      | :sg:`bold -> bool`
-
-      Gets or sets whether the font will be bold when drawing text. This
+      Gets or sets whether the face will be bold when drawing text. This
       default style value will be used for all text rendering and size
       calculations unless overriden specifically in the \`render()` or
       \`get_size()` calls, via the 'style' parameter.
 
-      .. ## Font.bold ##
+   .. attribute:: oblique
 
-   .. attribute:: italic
+      | :sl:`Gets or sets the face's oblique style`
+      | :sg:`oblique -> bool`
 
-      | :sl:`Gets or sets the font's italic style`
-      | :sg:`italic -> bool`
-
-      Gets or sets whether the font will be in italics when drawing text. This
+      Gets or sets whether the face will be rendered as oblique. This
       default style value will be used for all text rendering and size
       calculations unless overriden specifically in the \`render()` or
       \`get_size()` calls, via the 'style' parameter.
 
-      .. ## Font.italic ##
+   .. attribute:: wide
+
+      | :sl:`Gets or sets the face's wide style`
+      | :sg:`wide -> bool`
+
+      Gets or sets whether the face will be stretched horizontally
+      when drawing text. It produces a result simular to font.Font's
+      bold. This style is only available for unrotated text.
+
+   .. attribute:: strength
+
+      | :sl:`Gets or sets the strength of the strong or wide styles`
+      | :sg:`strength -> float`
+
+      The amount by which a face glyph's size is enlarged for the
+      strong or wide transformations, as a fraction of the untransformed
+      size. For the wide style only the horizontal dimension is
+      increased. For strong text both the horizontal and vertical
+      dimensions are enlarged. A wide style of strength 1/12 is
+      equivalent to the font.Font bold style. The default is 1/36.
+
+   .. attribute:: underline_adjustment
+
+      | :sl:`Gets or sets an adjustment factor for the underline position`
+      | :sg:`underline_adjustment -> float`
+
+      Gets or sets a factor which, when positive, is multiplied with the
+      face's underline offset to adjust the underline position. A negative
+      value turns an underline into a strikethrough or overline. It is
+      multiplied with the ascender. Accepted values are between -2.0 and 2.0
+      inclusive. A value of 0.5 closely matches Tango underlining. A value of
+      1.0 mimics SDL_ttf.
 
    .. attribute:: fixed_width
 
-      | :sl:`Gets whether the font is fixed-width`
+      | :sl:`Gets whether the face is fixed-width`
       | :sg:`fixed_width -> bool`
 
-      Read only. Returns whether this Font is a fixed-width (bitmap) font.
+      Read only. Returns whether this Face is a fixed-width (bitmap) face.
 
-      Note that scalable fonts whose glyphs are all the same width (i.e.
+      Note that scalable faces whose glyphs are all the same width (i.e.
       monospace ``TTF`` fonts used for programming) are not considered fixed
       width.
 
-      .. ## Font.fixed_width ##
-
    .. attribute:: antialiased
 
-      | :sl:`Font antialiasing mode`
+      | :sl:`Face antialiasing mode`
       | :sg:`antialiased -> bool`
 
-      Gets or sets the font's antialiasing mode. This defaults to True on all
-      fonts, which will be rendered by default antialiased.
+      Gets or sets the face's antialiasing mode. This defaults to ``True`` on
+      all faces, which are rendered with full 8 bit blending.
 
-      Setting this to true will change all rendering methods to use glyph
-      bitmaps without antialiasing, which supposes a small speed gain and a
-      significant memory gain because of the way glyphs are cached.
+      Setting this to ``False`` will enable monochrome rendering. This should
+      provide a small speed gain and reduce cache memory size.
 
-      .. ## Font.antialiased ##
+   .. attribute:: kerning
+
+      | :sl:`Character kerning mode`
+      | :sg:`kerning -> bool`
+
+      Gets or sets the face's kerning mode. This defaults to False on all
+      faces, which will be rendered by default without kerning.
+
+      Setting this to true will change all rendering methods to do kerning
+      between character pairs for surface size calculation and all
+      render operations.
 
    .. attribute:: vertical
 
-      | :sl:`Font vertical mode`
+      | :sl:`Face vertical mode`
       | :sg:`vertical -> bool`
 
-      Gets or sets whether the font is a vertical font such as fonts
+      Gets or sets whether the face is a vertical face such as faces in fonts
       representing Kanji glyphs or other styles of vertical writing.
 
-      Changing this attribute will cause the font to be rendering vertically,
+      Changing this attribute will cause the face to be rendering vertically,
       and affects all other methods which manage glyphs or text layouts to use
       vertical metrics accordingly.
 
       Note that the FreeType library doesn't automatically detect whether a
-      font contains glyphs which are always supposed to be drawn vertically, so
+      face contains glyphs which are always supposed to be drawn vertically, so
       this attribute must be set manually by the user.
 
-      Also note that several font formats (specially bitmap based ones) don't
+      Also note that several face formats (specially bitmap based ones) don't
       contain the necessary metrics to draw glyphs vertically, so drawing in
       those cases will give unspecified results.
 
-      .. ## Font.vertical ##
+   .. attribute:: origin
 
-   .. ##  ##
+      | :sl:`Face render to text origin mode`
+      | :sg:`vertical -> bool`
 
-   .. ## pygame.freetype.Font ##
+      If set True, then when rendering to an existing surface, the position
+      is taken to be that of the text origin. Otherwise the render position is
+      the top-left corner of the text bounding box.
 
-.. ## pygame.freetype ##
+   .. attribute:: pad
+
+      | :sl:`padded boundary mode`
+      | :sg:`pad -> bool`
+
+      If set True, then the text boundary rectangle will be inflated to match
+      that of font.Font. Otherwise, the boundary rectangle is just large
+      enough for the text.
+
+   .. attribute:: ucs4
+
+      | :sl:`Enables UCS-4 mode`
+      | :sg:`ucs4 -> bool`
+
+      Gets or sets the decoding of Unicode textdecoding. By default, the
+      freetype module performs UTF-16 surrogate pair decoding on Unicode text.
+      This allows 32-bit escape sequences ('\Uxxxxxxxx') between 0x10000 and
+      0x10FFFF to represent their corresponding UTF-32 code points on Python
+      interpreters built with a UCS-2 unicode type (on Windows, for instance).
+      It also means character values within the UTF-16 surrogate area (0xD800
+      to 0xDFFF) are considered part of a surrogate pair. A malformed surrogate
+      pair will raise an UnicodeEncodeError. Setting ucs4 True turns surrogate
+      pair decoding off, letting interpreters with a UCS-4 unicode type access
+      the full UCS-4 character range.
+
+   .. attribute:: resolution
+
+      | :sl:`Output pixel resolution in dots per inch`
+      | :sg:`resolution -> int`
+
+      Gets the pixel size used in scaling face glyphs for this Face instance.
+
+   .. method:: set_transform
+
+      | :sl:`assign a glyph transformation matrix`
+      | :sg:`set_transform(xx, xy, yx, yy) -> None`
+
+      Set a transform matrix for the face. If None, no matrix assigned.
+      The arguments can be any numeric type that can be converted
+      to a double. The matrix is applied after the strong transformation,
+      but before oblique and rotation.
+
+   .. method:: delete_transform
+
+      | :sl:`delete a glyph transformation matrix`
+      | :sg:`set_transform(xx, xy, yx, yy) -> None`
+
+      Remove the transformation matrix, if any.
+
+   .. method:: get_transform
+
+      | :sl:`return the user assigned transformation matrix, or None`
+      | :sg:`get_transform() -> (double, double, double, double) or None`
+
+      Return the transform matrix for the face. If None, no matrix is assigned.

docs/reST/ref/freetype.rst

    .. method:: render_raw
 
       | :sl:`Renders text as a string of bytes`
-      | :sg:`render_raw(text, style=STYLE_DEFAULT, rotation=0, ptsize=default) -> (bytes, (int, int))`
+      | :sg:`render_raw(text, style=STYLE_DEFAULT, rotation=0, ptsize=default, invert=False) -> (bytes, (int, int))`
 
-      Like ``Face.render(None, ...)`` but the tuple returned is an 8 bit
+      Like ``Face.render()`` but the tuple returned is an 8 bit
       monochrome string of bytes and its size. The forground color is 255, the
       background 0, useful as an alpha mask for a foreground pattern.
 
+   .. method:: render_raw_to
+
+      | :sl:`Renders text as a string of ints to an array`
+      | :sg:`render_raw_to(array, text, dest=None, style=STYLE_DEFAULT, rotation=0, ptsize=default, invert=False) -> (int, int)`
+
+      Render to an array object exposing an array struct interface. The array
+      must be two dimensional with integer items. The default dest value, None,
+      is equivalent to (0, 0).
+
    .. attribute:: style
 
       | :sl:`Gets or sets the face's style`

docs/ref/freetype.html

 <td>—</td>
 <td>Renders text as a string of bytes</td>
 </tr>
+<tr><td><a class="toc reference external" href="freetype.html#pygame.freetype.Face.render_raw_to">pygame.freetype.Face.render_raw_to</a></td>
+<td>—</td>
+<td>Renders text as a string of ints to an array</td>
+</tr>
 <tr><td><a class="toc reference external" href="freetype.html#pygame.freetype.Face.style">pygame.freetype.Face.style</a></td>
 <td>—</td>
 <td>Gets or sets the face&#8217;s style</td>
 <tt class="descname">render_raw</tt><big>(</big><big>)</big><a class="headerlink" href="#pygame.freetype.Face.render_raw" title="Permalink to this definition">¶</a></dt>
 <dd><div class="line-block">
 <div class="line"><span class="summaryline">Renders text as a string of bytes</span></div>
-<div class="line"><span class="signature">render_raw(text, style=STYLE_DEFAULT, rotation=0, ptsize=default) -&gt; (bytes, (int, int))</span></div>
+<div class="line"><span class="signature">render_raw(text, style=STYLE_DEFAULT, rotation=0, ptsize=default, invert=False) -&gt; (bytes, (int, int))</span></div>
 </div>
-<p>Like <tt class="docutils literal"><span class="pre">Face.render(None,</span> <span class="pre">...)</span></tt> but the tuple returned is an 8 bit
+<p>Like <tt class="docutils literal"><span class="pre">Face.render()</span></tt> but the tuple returned is an 8 bit
 monochrome string of bytes and its size. The forground color is 255, the
 background 0, useful as an alpha mask for a foreground pattern.</p>
 </dd></dl>
 
+<dl class="definition method">
+<dt class="title" id="pygame.freetype.Face.render_raw_to">
+<tt class="descname">render_raw_to</tt><big>(</big><big>)</big><a class="headerlink" href="#pygame.freetype.Face.render_raw_to" title="Permalink to this definition">¶</a></dt>
+<dd><div class="line-block">
+<div class="line"><span class="summaryline">Renders text as a string of ints to an array</span></div>
+<div class="line"><span class="signature">render_raw_to(array, text, dest=None, style=STYLE_DEFAULT, rotation=0, ptsize=default, invert=False) -&gt; (int, int)</span></div>
+</div>
+<p>Render to an array object exposing an array struct interface. The array
+must be two dimensional with integer items. The default dest value, None,
+is equivalent to (0, 0).</p>
+</dd></dl>
+
 <dl class="definition attribute">
 <dt class="title" id="pygame.freetype.Face.style">
 <tt class="descname">style</tt><a class="headerlink" href="#pygame.freetype.Face.style" title="Permalink to this definition">¶</a></dt>

src/doc/freetype_doc.h

 
 #define DOC_FACERENDERTO "render(surf, dest, text, fgcolor, bgcolor=None, style=STYLE_DEFAULT, rotation=0, ptsize=default) -> Rect\nRenders text to an existing surface"
 
-#define DOC_FACERENDERRAW "render_raw(text, style=STYLE_DEFAULT, rotation=0, ptsize=default) -> (bytes, (int, int))\nRenders text as a string of bytes"
+#define DOC_FACERENDERRAW "render_raw(text, style=STYLE_DEFAULT, rotation=0, ptsize=default, invert=False) -> (bytes, (int, int))\nRenders text as a string of bytes"
+
+#define DOC_FACERENDERRAWTO "render_raw_to(array, text, dest=None, style=STYLE_DEFAULT, rotation=0, ptsize=default, invert=False) -> (int, int)\nRenders text as a string of ints to an array"
 
 #define DOC_FACESTYLE "style -> int\nGets or sets the face's style"
 
 Renders text to an existing surface
 
 pygame.freetype.Face.render_raw
- render_raw(text, style=STYLE_DEFAULT, rotation=0, ptsize=default) -> (bytes, (int, int))
+ render_raw(text, style=STYLE_DEFAULT, rotation=0, ptsize=default, invert=False) -> (bytes, (int, int))
 Renders text as a string of bytes
 
+pygame.freetype.Face.render_raw_to
+ render_raw_to(array, text, dest=None, style=STYLE_DEFAULT, rotation=0, ptsize=default, invert=False) -> (int, int)
+Renders text as a string of ints to an array
+
 pygame.freetype.Face.style
  style -> int
 Gets or sets the face's style
 #include "freetype.h"
 #include "freetype/ft_wrap.h"
 #include "doc/freetype_doc.h"
+#include "pgview.h"
 
 #define MODULE_NAME "freetype"
 #define FACE_TYPE_NAME "Face"
 static PyObject *_ftface_render(PgFaceObject *, PyObject *, PyObject *);
 static PyObject *_ftface_render_to(PgFaceObject *, PyObject *, PyObject *);
 static PyObject *_ftface_render_raw(PgFaceObject *, PyObject *, PyObject *);
+static PyObject *_ftface_render_raw_to(PgFaceObject *, PyObject *, PyObject *);
 static PyObject *_ftface_getsizedascender(PgFaceObject *, PyObject *);
 static PyObject *_ftface_getsizeddescender(PgFaceObject *, PyObject *);
 static PyObject *_ftface_getsizedheight(PgFaceObject *, PyObject *);
         DOC_FACERENDERRAW
     },
     {
+        "render_raw_to",
+        (PyCFunction)_ftface_render_raw_to,
+        METH_VARARGS | METH_KEYWORDS,
+        DOC_FACERENDERRAWTO
+    },
+    {
         "get_transform",
         (PyCFunction)_ftface_gettransform,
         METH_NOARGS,
 {
     /* keyword list */
     static char *kwlist[] =  {
-        "text", "style", "rotation", "ptsize", 0
+        "text", "style", "rotation", "ptsize", "invert", 0
     };
 
     FaceRenderMode mode;
     /* input arguments */
     PyObject *textobj;
     PGFT_String *text;
+    int style = FT_STYLE_DEFAULT;
     int rotation = 0;
     int ptsize = -1;
-    int style = FT_STYLE_DEFAULT;
+    int invert = 0;
 
     /* output arguments */
     PyObject *rbuffer = 0;
     FreeTypeInstance *ft;
     ASSERT_GRAB_FREETYPE(ft, 0);
 
-    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|iii", kwlist,
-                                     &textobj, &style, &rotation, &ptsize)) {
+    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|iiii", kwlist,
+                                     &textobj,
+                                     &style, &rotation, &ptsize, &invert)) {
         return 0;
     }
 
         return 0;
     }
 
-    rbuffer = _PGFT_Render_PixelArray(ft, self, &mode, text, &width, &height);
+    rbuffer = _PGFT_Render_PixelArray(ft, self, &mode, text, invert,
+                                      &width, &height);
     _PGFT_FreeString(text);
 
     if (!rbuffer) {
 }
 
 static PyObject *
+_ftface_render_raw_to(PgFaceObject *self, PyObject *args, PyObject *kwds)
+{
+    /* keyword list */
+    static char *kwlist[] =  {
+        "array", "text", "dest", "style", "rotation", "ptsize", "invert", 0
+    };
+
+    FaceRenderMode mode;
+
+    /* input arguments */
+    PyObject *arrayobj;
+    PyObject *textobj;
+    PGFT_String *text;
+    PyObject *dest = 0;
+    int xpos = 0;
+    int ypos = 0;
+    int style = FT_STYLE_DEFAULT;
+    int rotation = 0;
+    int ptsize = -1;
+    int invert = 0;
+
+    /* output arguments */
+    SDL_Rect r;
+
+    /* internal */
+    int rcode;
+    FreeTypeInstance *ft;
+    ASSERT_GRAB_FREETYPE(ft, 0);
+
+    if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|Oiiii", kwlist,
+                                     &arrayobj, &textobj,
+                                     &dest, &style, &rotation,
+                                     &ptsize, &invert)) {
+        return 0;
+    }
+
+    if (dest && dest != Py_None) {
+        if (parse_dest(dest, &xpos, &ypos)) {
+            return 0;
+        }
+    }
+
+    /* Encode text */
+    text = _PGFT_EncodePyString(textobj, self->render_flags & FT_RFLAG_UCS4);
+    if (!text) {
+        return 0;
+    }
+
+    ASSERT_SELF_IS_ALIVE(self);
+
+    /*
+     * Build the render mode with the given size and no
+     * rotation/styles/vertical text
+     */
+    if (_PGFT_BuildRenderMode(ft, self, &mode, ptsize, style, rotation)) {
+        _PGFT_FreeString(text);
+        return 0;
+    }
+
+    rcode = _PGFT_Render_Array(ft, self, &mode,
+                               arrayobj, text, invert, xpos, ypos, &r);
+    _PGFT_FreeString(text);
+    if (rcode) {
+        return 0;
+    }
+    return PyRect_New(&r);
+}
+
+static PyObject *
 _ftface_render(PgFaceObject *self, PyObject *args, PyObject *kwds)
 {
 #ifndef HAVE_PYGAME_SDL_VIDEO
         MODINIT_ERROR;
     }
 
+    import_pygame_view();
+    if (PyErr_Occurred())  {
+        MODINIT_ERROR;
+    }
+
     /* import needed modules. Do this first so if there is an error
        the module is not loaded.
     */

src/freetype/ft_render.c

 #define PYGAME_FREETYPE_INTERNAL
 
 #include "ft_wrap.h"
+#include "../pgview.h"
 #include FT_MODULE_H
 #include FT_OUTLINE_H
 
+static const FaceColor mono_opaque = {0, 0, 0, SDL_ALPHA_OPAQUE};
+static const FaceColor mono_transparent = {0, 0, 0, SDL_ALPHA_TRANSPARENT};
+
 static void render(FreeTypeInstance *, FaceText *, const FaceRenderMode *,
-                   FaceColor *, FaceSurface *, unsigned, unsigned,
+                   const FaceColor *, FaceSurface *, unsigned, unsigned,
                    FT_Vector *, FT_Pos, FT_Fixed);
 
 int
 
 PyObject *_PGFT_Render_PixelArray(FreeTypeInstance *ft, PgFaceObject *faceobj,
                                   const FaceRenderMode *mode,
-                                  PGFT_String *text, int *_width, int *_height)
+                                  PGFT_String *text, int invert,
+                                  int *_width, int *_height)
 {
     FT_Byte *buffer = 0;
     PyObject *array = 0;
     FT_Pos underline_top;
     FT_Fixed underline_size;
     int array_size;
-    FaceColor mono_opaque = {0, 0, 0, SDL_ALPHA_OPAQUE};
 
     if (PGFT_String_GET_LENGTH(text) == 0) {
         /* Empty array */
         return 0;
     }
     buffer = (FT_Byte *)Bytes_AS_STRING(array);
-    memset(buffer, 0x00, (size_t)array_size);
+    if (invert) {
+        memset(buffer, SDL_ALPHA_OPAQUE, (size_t)array_size);
+    }
+    else {
+        memset(buffer, SDL_ALPHA_TRANSPARENT, (size_t)array_size);
+    }
     surf.buffer = buffer;
     surf.width = width;
     surf.height = height;
     surf.render_mono = __render_glyph_MONO_as_GRAY1;
     surf.fill = __fill_glyph_GRAY1;
 
-    render(ft, font_text, mode, &mono_opaque, &surf,
-           width, height, &offset, underline_top, underline_size);
+    render(ft, font_text, mode, invert ? &mono_transparent : &mono_opaque,
+           &surf, width, height, &offset, underline_top, underline_size);
 
     *_width = width;
     *_height = height;
     return array;
 }
 
+int
+_PGFT_Render_Array(FreeTypeInstance *ft, PgFaceObject *faceobj,
+                   const FaceRenderMode *mode, PyObject *arrayobj,
+                   PGFT_String *text, int invert,
+                   int x, int y, SDL_Rect *r)
+{
+    static int view_init = 0;
+
+    PyObject *cobj = 0;
+    PyArrayInterface *inter_p;
+
+    unsigned width;
+    unsigned height;
+    int itemsize;
+    FT_Vector offset;
+    FT_Vector array_offset;
+    FT_Pos underline_top;
+    FT_Fixed underline_size;
+
+    FaceSurface font_surf;
+    SDL_PixelFormat format;
+    FaceText *font_text;
+
+    /* Get target buffer */
+    if (!view_init) {
+        import_pygame_view();
+        if (PyErr_Occurred()) {
+            return -1;
+        }
+    }
+    if (Pg_GetArrayInterface(arrayobj, &cobj, &inter_p)) {
+        return -1;
+    }
+    if (inter_p->nd != 2) {
+        Py_DECREF(cobj);
+        PyErr_Format(PyExc_ValueError,
+                     "expecting a 2d target array: got %id array instead",
+                     (int)inter_p->nd);
+        return -1;
+    }
+    switch (inter_p->typekind) {
+
+    case 'i':  /* integer */
+        break;
+    case 'u':  /* unsigned integer */ 
+        break;
+    case 'S':  /* fixed length character field */
+        break;
+    default:
+        Py_DECREF(cobj);
+        PyErr_Format(PyExc_ValueError, "unsupported target array type '%c'",
+                     inter_p->typekind);
+        return -1;
+    }
+
+    width = inter_p->shape[0];
+    height = inter_p->shape[1];
+    itemsize = inter_p->itemsize;
+
+    /* if empty string, then nothing more to do */
+    if (PGFT_String_GET_LENGTH(text) == 0) {
+        Py_DECREF(cobj);
+        r->x = 0;
+        r->y = 0;
+        r->w = 0;
+        r->h = _PGFT_Face_GetHeightSized(ft, faceobj, mode->pt_size);
+        return 0;
+    }
+
+    /* build face text */
+    font_text = _PGFT_LoadFaceText(ft, faceobj, mode, text);
+    if (!font_text) {
+        Py_DECREF(cobj);
+        return -1;
+    }
+
+    _PGFT_GetRenderMetrics(mode, font_text, &width, &height, &offset,
+                           &underline_top, &underline_size);
+    if (width == 0 || height == 0) {
+        /* Nothing more to do. */
+        Py_DECREF(cobj);
+        r->x = 0;
+        r->y = 0;
+        r->w = 0;
+        r->h = _PGFT_Face_GetHeightSized(ft, faceobj, mode->pt_size);
+        return 0;
+    }
+    array_offset.x = INT_TO_FX6(x);
+    array_offset.y = INT_TO_FX6(y);
+    if (mode->render_flags & FT_RFLAG_ORIGIN) {
+        x -= FX6_TRUNC(FX6_CEIL(offset.x));
+        y -= FX6_TRUNC(FX6_CEIL(offset.y));
+    }
+    else {
+        array_offset.x += offset.x;
+        array_offset.y += offset.y;
+    }
+
+    /*
+     * Setup target surface struct
+     */
+    format.BytesPerPixel = itemsize;
+#if SDL_BYTEORDER == SDL_LIL_ENDIAN
+    format.Ashift = ((inter_p->flags & PAI_NOTSWAPPED) ?
+                     0 : (itemsize - 1) * 8);
+#else
+    format.Ashift = ((inter_p->flags & PAI_NOTSWAPPED) ?
+                     (itemsize - 1) * 8 : 0);
+#endif
+    font_surf.buffer = inter_p->data;
+    font_surf.width = inter_p->shape[0];
+    font_surf.height = inter_p->shape[1];
+    font_surf.item_stride = inter_p->strides[0];
+    font_surf.pitch = inter_p->strides[1];
+    font_surf.format = &format;
+    font_surf.render_gray = __render_glyph_INT;
+    font_surf.render_mono = __render_glyph_MONO_as_INT;
+    font_surf.fill = __fill_glyph_INT;
+
+    render(ft, font_text, mode, invert ? &mono_transparent : &mono_opaque,
+           &font_surf, width, height, &array_offset, underline_top,
+           underline_size);
+
+    Py_DECREF(cobj);
+    r->x = -(Sint16)FX6_TRUNC(FX6_FLOOR(offset.x));
+    r->y = (Sint16)FX6_TRUNC(FX6_CEIL(offset.y));
+    r->w = (Uint16)width;
+    r->h = (Uint16)height;
+
+    return 0;
+}
 
 /*********************************************************
  *
  *********************************************************/
 static void
 render(FreeTypeInstance *ft, FaceText *text, const FaceRenderMode *mode,
-       FaceColor *fg_color, FaceSurface *surface,
+       const FaceColor *fg_color, FaceSurface *surface,
        unsigned width, unsigned height, FT_Vector *offset,
        FT_Pos underline_top, FT_Fixed underline_size)
 {

src/freetype/ft_render_cb.c

 #endif
 
 void __render_glyph_GRAY1(int x, int y, FaceSurface *surface,
-                          FT_Bitmap *bitmap, FaceColor *fg_color)
+                          const FT_Bitmap *bitmap, const FaceColor *fg_color)
 {
     FT_Byte *dst = ((FT_Byte *)surface->buffer) + x + (y * surface->pitch);
     FT_Byte *dst_cpy;
 }
 
 void __render_glyph_MONO_as_GRAY1(int x, int y, FaceSurface *surface,
-                                  FT_Bitmap *bitmap, FaceColor *fg_color)
+                                  const FT_Bitmap *bitmap,
+                                  const FaceColor *fg_color)
 {
     const int off_x = (x < 0) ? -x : 0;
     const int off_y = (y < 0) ? -y : 0;
     const int rx = MAX(0, x);
     const int ry = MAX(0, y);
 
-    int             i, j, shift;
-    unsigned char*  src;
-    unsigned char*  dst;
-    unsigned char*  src_cpy;
-    unsigned char*  dst_cpy;
-    FT_UInt32       val;
+    int i, j, shift;
+    const unsigned char* src;
+    unsigned char* dst;
+    const unsigned char*  src_cpy;
+    unsigned char* dst_cpy;
+    FT_UInt32 val;
     FT_Byte shade = fg_color->a;
 
     src  = bitmap->buffer + (off_y * bitmap->pitch) + (off_x >> 3);
     for (j = ry; j < max_y; ++j) {
         src_cpy = src;
         dst_cpy = dst;
-         val = (FT_UInt32)(*src_cpy++ | 0x100) << shift;
+        val = (FT_UInt32)(*src_cpy++ | 0x100) << shift;
 
         for (i = rx; i < max_x; ++i, ++dst_cpy) {
             if (val & 0x10000) {
 }
 
 void __render_glyph_GRAY_as_MONO1(int x, int y, FaceSurface *surface,
-    FT_Bitmap *bitmap, FaceColor *fg_color)
+                                  const FT_Bitmap *bitmap,
+                                  const FaceColor *fg_color)
 {
     FT_Byte *dst = ((FT_Byte *)surface->buffer) + x + (y * surface->pitch);
     FT_Byte *dst_cpy;
 }
 
 void __fill_glyph_GRAY1(int x, int y, int w, int h,
-        FaceSurface *surface, FaceColor *color)
+        FaceSurface *surface, const FaceColor *color)
 {
     int i, j;
     FT_Byte *dst;
     }
 }
 
+void __render_glyph_INT(int x, int y, FaceSurface *surface,
+                        const FT_Bitmap *bitmap, const FaceColor *fg_color)
+{
+    FT_Byte *dst = ((FT_Byte *)surface->buffer +
+                    x * surface->item_stride + y * surface->pitch);
+    int item_size = surface->format->BytesPerPixel;
+    int item_stride = surface->item_stride;
+    FT_Byte *dst_cpy;
+
+    const FT_Byte *src = bitmap->buffer;
+    const FT_Byte *src_cpy;
+    FT_Byte src_byte;
+    FT_Byte dst_byte;
+    FT_Byte mask = ~fg_color->a;
+
+    int j, i;
+    int b, int_offset;
+
+    /*
+     * Assumption, target buffer was filled with the background color before
+     * any rendering.
+     */
+
+    if (item_size == 1) {
+        for (j = 0; j < bitmap->rows; ++j) {
+            src_cpy = src;
+            dst_cpy = dst;
+
+            for (i = 0; i < bitmap->width; ++i) {
+                src_byte = *src_cpy;
+                if (src_byte) {
+                    *dst_cpy = ((src_byte + *dst_cpy -
+                                src_byte * *dst_cpy / 255) ^ mask);
+                }
+                ++src_cpy;
+                dst_cpy += item_stride;
+            }
+
+            dst += surface->pitch;
+            src += bitmap->pitch;
+        }
+    }
+    else {
+        int_offset = surface->format->Ashift / 8;
+
+        for (j = 0; j < bitmap->rows; ++j) {
+            src_cpy = src;
+            dst_cpy = dst;
+
+            for (i = 0; i < bitmap->width; ++i) {
+                dst_byte = dst_cpy[int_offset];
+                for (b = 0; b < item_size; ++b) {
+                    dst_cpy[b] = 0;
+                }
+
+                src_byte = *src_cpy;
+                if (src_byte) {
+                    dst_cpy[int_offset] = ((src_byte + dst_byte -
+                                            src_byte * dst_byte / 255) ^ mask);
+                }
+                ++src_cpy;
+                dst_cpy += item_stride;
+            }
+
+            dst += surface->pitch;
+            src += bitmap->pitch;
+        }
+    }
+}
+
+void __render_glyph_MONO_as_INT(int x, int y, FaceSurface *surface,
+                                const FT_Bitmap *bitmap,
+                                const FaceColor *fg_color)
+{
+    const int off_x = (x < 0) ? -x : 0;
+    const int off_y = (y < 0) ? -y : 0;
+
+    const int max_x = MIN(x + bitmap->width, surface->width);
+    const int max_y = MIN(y + bitmap->rows, surface->height);
+
+    const int rx = MAX(0, x);
+    const int ry = MAX(0, y);
+
+    int i, j, shift;
+    int b, int_offset;
+    int item_stride = surface->item_stride;
+    int item_size = surface->format->BytesPerPixel;
+    unsigned char *src;
+    unsigned char *dst;
+    unsigned char *src_cpy;
+    unsigned char *dst_cpy;
+    FT_UInt32 val;
+    FT_Byte shade = fg_color->a;
+
+    /*
+     * Assumption, target buffer was filled with the background color before
+     * any rendering.
+     */
+
+    src  = bitmap->buffer + (off_y * bitmap->pitch) + (off_x >> 3);
+    dst = ((unsigned char *)surface->buffer +
+           rx * surface->item_stride + ry * surface->pitch);
+
+    shift = off_x & 7;
+
+    if (item_size == 1) {
+        /* Slightly optimized loop for 1 byte target int */
+        for (j = ry; j < max_y; ++j) {
+            src_cpy = src;
+            dst_cpy = dst;
+            val = (FT_UInt32)(*src_cpy++ | 0x100) << shift;
+
+            for (i = rx; i < max_x; ++i, dst_cpy += item_stride) {
+                if (val & 0x10000) {
+                    val = (FT_UInt32)(*src_cpy++ | 0x100);
+                }
+
+                if (val & 0x80) {
+                    *dst_cpy = shade;
+                }
+
+                val   <<= 1;
+            }
+
+            src += bitmap->pitch;
+            dst += surface->pitch;
+        }
+    }
+    else {
+        /* Generic copy for arbitrary target int size */
+        int_offset = surface->format->Ashift / 8;
+
+        for (j = ry; j < max_y; ++j) {
+            src_cpy = src;
+            dst_cpy = dst;
+            val = (FT_UInt32)(*src_cpy++ | 0x100) << shift;
+
+            for (i = rx; i < max_x; ++i, dst_cpy += item_stride) {
+                for (b = 0; b < item_size; ++b) {
+                    dst_cpy[b] = 0;
+                }
+
+                if (val & 0x10000) {
+                    val = (FT_UInt32)(*src_cpy++ | 0x100);
+                }
+
+                if (val & 0x80) {
+                    dst_cpy[int_offset] = shade;
+                }
+
+                val   <<= 1;
+            }
+
+            src += bitmap->pitch;
+            dst += surface->pitch;
+        }
+    }
+}
+
+void __fill_glyph_INT(int x, int y, int w, int h,
+                      FaceSurface *surface, const FaceColor *color)
+{
+    int b, i, j;
+    FT_Byte *dst;
+    int itemsize = surface->format->BytesPerPixel;
+    int item_stride = surface->item_stride;
+    int byteoffset = surface->format->Ashift / 8;
+    FT_Byte *dst_cpy;
+    FT_Byte shade = color->a;
+
+    x = MAX(0, x);
+    y = MAX(0, y);
+
+    if (x + w > surface->width) {
+        w = surface->width - x;
+    }
+    if (y + h > surface->height) {
+        h = surface->height - y;
+    }
+
+    dst = (FT_Byte *)surface->buffer + x + (y * surface->pitch);
+
+    if (itemsize == 1) {
+        for (j = 0; j < h; ++j) {
+            dst_cpy = dst;
+
+            for (i = 0; i < w; ++i, dst_cpy += item_stride) {
+                *dst_cpy = shade;
+            }
+
+            dst += surface->pitch;
+        }
+    }
+    else {
+        for (j = 0; j < h; ++j) {
+            dst_cpy = dst;
+
+            for (i = 0; i < w; ++i, dst_cpy += item_stride) {
+                for (b = 0; b < itemsize; ++b) {
+                    dst_cpy[b] = 0;
+                }
+                dst_cpy[byteoffset] = shade;
+            }
+
+            dst += surface->pitch;
+        }
+    }
+}
+
 #ifdef HAVE_PYGAME_SDL_VIDEO
 
 #define _CREATE_RGB_FILLER(_bpp, _getp, _setp, _blendp)     \
     void __fill_glyph_RGB##_bpp(int x, int y, int w, int h, \
                                 FaceSurface *surface,       \
-                                FaceColor *color)           \
+                                const FaceColor *color)     \
     {                                                       \
         int i, j;                                           \
         unsigned char *dst;                                 \
 #define __MONO_RENDER_INNER_LOOP(_bpp, _code)               \
     for (j = ry; j < max_y; ++j)                            \
     {                                                       \
-        unsigned char* _src = src;                          \
+        const unsigned char* _src = src;                    \
         unsigned char* _dst = dst;                          \
         FT_UInt32 val =                                     \
             (FT_UInt32)(*_src++ | 0x100) << shift;          \
 #define _CREATE_MONO_RENDER(_bpp, _getp, _setp, _blendp)    \
     void __render_glyph_MONO##_bpp(int x, int y,            \
                                    FaceSurface *surface,    \
-                                   FT_Bitmap *bitmap,       \
-                                FaceColor *color)           \
+                                   const FT_Bitmap *bitmap, \
+                                   const FaceColor *color)  \
     {                                                       \
         const int off_x = (x < 0) ? -x : 0;                 \
         const int off_y = (y < 0) ? -y : 0;                 \
         const int rx = MAX(0, x);                           \
         const int ry = MAX(0, y);                           \
                                                             \
-        int             i, j, shift;                                    \
-        unsigned char*  src;                                            \
-        unsigned char*  dst;                                            \
-        FT_UInt32       full_color;                                     \
+        int i, j, shift;                                                \
+        const unsigned char* src;                                       \
+        unsigned char* dst;                                             \
+        FT_UInt32 full_color;                                           \
         FT_UInt32 bgR, bgG, bgB, bgA;                                   \
                                                                         \
         src  = bitmap->buffer + (off_y * bitmap->pitch) + (off_x >> 3); \
 
 #define _CREATE_RGB_RENDER(_bpp, _getp, _setp, _blendp)                 \
     void __render_glyph_RGB##_bpp(int x, int y, FaceSurface *surface,   \
-        FT_Bitmap *bitmap, FaceColor *color)                            \
+                                  const FT_Bitmap *bitmap,              \
+                                  const FaceColor *color)               \
     {                                                                   \
         const int off_x = (x < 0) ? -x : 0;                             \
         const int off_y = (y < 0) ? -y : 0;                             \

src/freetype/ft_wrap.h

 struct facesurface_;
 
 typedef void (* FaceRenderPtr)(int, int, struct facesurface_ *,
-                               FT_Bitmap *, FaceColor *);
+                               const FT_Bitmap *, const FaceColor *);
 typedef void (* FaceFillPtr)(int, int, int, int, struct facesurface_ *,
-                             FaceColor *);
+                             const FaceColor *);
 
 typedef struct facesurface_ {
     void *buffer;
 
     unsigned width;
     unsigned height;
+    int item_stride;
     int pitch;
 
     SDL_PixelFormat *format;
 /**************************************** Rendering **************************/
 PyObject *_PGFT_Render_PixelArray(FreeTypeInstance *, PgFaceObject *,
                                   const FaceRenderMode *,
-                                  PGFT_String *, int *, int *);
+                                  PGFT_String *, int, int *, int *);
 SDL_Surface *_PGFT_Render_NewSurface(FreeTypeInstance *, PgFaceObject *,
                                      const FaceRenderMode *, PGFT_String *,
                                      FaceColor *, FaceColor *, SDL_Rect *);
                                  const FaceRenderMode *, PGFT_String *,
                                  SDL_Surface *, int, int,
                                  FaceColor *, FaceColor *, SDL_Rect *);
+int _PGFT_Render_Array(FreeTypeInstance *, PgFaceObject *,
+                       const FaceRenderMode *, PyObject *,
+                       PGFT_String *, int, int, int, SDL_Rect *);
 int _PGFT_BuildRenderMode(FreeTypeInstance *, PgFaceObject *,
                           FaceRenderMode *, int, int, int);
 int _PGFT_CheckStyle(FT_UInt32);
 
 
 /**************************************** Render callbacks *******************/
-void __fill_glyph_RGB1(int, int, int, int, FaceSurface *, FaceColor *);
-void __fill_glyph_RGB2(int, int, int, int, FaceSurface *, FaceColor *);
-void __fill_glyph_RGB3(int, int, int, int, FaceSurface *, FaceColor *);
-void __fill_glyph_RGB4(int, int, int, int, FaceSurface *, FaceColor *);
+void __fill_glyph_RGB1(int, int, int, int, FaceSurface *, const FaceColor *);
+void __fill_glyph_RGB2(int, int, int, int, FaceSurface *, const FaceColor *);
+void __fill_glyph_RGB3(int, int, int, int, FaceSurface *, const FaceColor *);
+void __fill_glyph_RGB4(int, int, int, int, FaceSurface *, const FaceColor *);
 
-void __fill_glyph_GRAY1(int, int, int, int, FaceSurface *, FaceColor *);
+void __fill_glyph_GRAY1(int, int, int, int, FaceSurface *, const FaceColor *);
 
-void __render_glyph_MONO1(int, int, FaceSurface *, FT_Bitmap *, FaceColor *);
-void __render_glyph_MONO2(int, int, FaceSurface *, FT_Bitmap *, FaceColor *);
-void __render_glyph_MONO3(int, int, FaceSurface *, FT_Bitmap *, FaceColor *);
-void __render_glyph_MONO4(int, int, FaceSurface *, FT_Bitmap *, FaceColor *);
+void __fill_glyph_INT(int, int, int, int, FaceSurface *, const FaceColor *);
 
-void __render_glyph_RGB1(int, int, FaceSurface *, FT_Bitmap *, FaceColor *);
-void __render_glyph_RGB2(int, int, FaceSurface *, FT_Bitmap *, FaceColor *);
-void __render_glyph_RGB3(int, int, FaceSurface *, FT_Bitmap *, FaceColor *);
-void __render_glyph_RGB4(int, int, FaceSurface *, FT_Bitmap *, FaceColor *);
+void __render_glyph_MONO1(int, int, FaceSurface *, const FT_Bitmap *,
+                          const FaceColor *);
+void __render_glyph_MONO2(int, int, FaceSurface *, const FT_Bitmap *,
+                          const FaceColor *);
+void __render_glyph_MONO3(int, int, FaceSurface *, const FT_Bitmap *,
+                          const FaceColor *);
+void __render_glyph_MONO4(int, int, FaceSurface *, const FT_Bitmap *,
+                          const FaceColor *);
 
-void __render_glyph_GRAY1(int, int, FaceSurface *, FT_Bitmap *, FaceColor *);
-void __render_glyph_MONO_as_GRAY1(int, int, FaceSurface *, FT_Bitmap *,
-                                  FaceColor *);
-void __render_glyph_GRAY_as_MONO1(int, int, FaceSurface *, FT_Bitmap *,
-                                  FaceColor *);
+void __render_glyph_RGB1(int, int, FaceSurface *, const FT_Bitmap *,
+                         const FaceColor *);
+void __render_glyph_RGB2(int, int, FaceSurface *, const FT_Bitmap *,
+                         const FaceColor *);
+void __render_glyph_RGB3(int, int, FaceSurface *, const FT_Bitmap *,
+                         const FaceColor *);
+void __render_glyph_RGB4(int, int, FaceSurface *, const FT_Bitmap *,
+                         const FaceColor *);
+
+void __render_glyph_GRAY1(int, int, FaceSurface *, const FT_Bitmap *,
+                          const FaceColor *);
+void __render_glyph_MONO_as_GRAY1(int, int, FaceSurface *, const FT_Bitmap *,
+                                  const FaceColor *);
+void __render_glyph_GRAY_as_MONO1(int, int, FaceSurface *, const FT_Bitmap *,
+                                  const FaceColor *);
+
+void __render_glyph_INT(int, int, FaceSurface *, const FT_Bitmap *,
+                        const FaceColor *);
+void __render_glyph_MONO_as_INT(int, int, FaceSurface *, const FT_Bitmap *,
+                                const FaceColor *);
 
 
 /**************************************** Face text management ***************/

test/freetype_test.py

         text = unicode_("").join([unichr_(i) for i in range(31, 64)])
         rend = face.render_raw(text, ptsize=10)
 
+    def test_freetype_Face_render_raw_to(self):
+
+        # This only checks that blits do not crash. It needs to check:
+        # - int values
+        # - invert option
+        #
+
+        face = self._TEST_FONTS['sans']
+        text = "abc"
+
+        # No frills antialiased render to int1 (__render_glyph_INT)
+        srect = face.get_rect(text, ptsize=24)
+        surf = pygame.Surface(srect.size, 0, 8)
+        rrect = face.render_raw_to(surf.get_view('2'), text, ptsize=24)
+        self.assertEqual(rrect, srect)
+
+        for bpp in [24, 32]:
+            surf = pygame.Surface(srect.size, 0, bpp)
+            rrect = face.render_raw_to(surf.get_view('r'), text, ptsize=24)
+            self.assertEqual(rrect, srect)
+
+        # Underlining to int1 (__fill_glyph_INT)
+        srect = face.get_rect(text, ptsize=24, style=ft.STYLE_UNDERLINE)
+        surf = pygame.Surface(srect.size, 0, 8)
+        rrect = face.render_raw_to(surf.get_view('2'), text, ptsize=24,
+                                  style=ft.STYLE_UNDERLINE)
+        self.assertEqual(rrect, srect)
+
+        for bpp in [24, 32]:
+            surf = pygame.Surface(srect.size, 0, bpp)
+            rrect = face.render_raw_to(surf.get_view('r'), text, ptsize=24,
+                                       style=ft.STYLE_UNDERLINE)
+            self.assertEqual(rrect, srect)
+
+        # Unaliased (mono) rendering to int1 (__render_glyph_MONO_as_INT)
+        face.antialiased = False
+        try:
+            srect = face.get_rect(text, ptsize=24)
+            surf = pygame.Surface(srect.size, 0, 8)
+            rrect = face.render_raw_to(surf.get_view('2'), text, ptsize=24)
+            self.assertEqual(rrect, srect)
+
+            for bpp in [24, 32]:
+                surf = pygame.Surface(srect.size, 0, bpp)
+                rrect = face.render_raw_to(surf.get_view('r'), text, ptsize=24)
+                self.assertEqual(rrect, srect)
+        finally:
+            face.antialiased = True
+
+        # Antialiased render to ints sized greater than 1 byte
+        # (__render_glyph_INT)
+        srect = face.get_rect(text, ptsize=24)
+
+        for bpp in [16, 24, 32]:
+            surf = pygame.Surface(srect.size, 0, bpp)
+            rrect = face.render_raw_to(surf.get_view('2'), text, ptsize=24)
+            self.assertEqual(rrect, srect)
+
+        # Underline render to ints sized greater than 1 byte
+        # (__fill_glyph_INT)
+        srect = face.get_rect(text, ptsize=24, style=ft.STYLE_UNDERLINE)
+
+        for bpp in [16, 24, 32]:
+            surf = pygame.Surface(srect.size, 0, bpp)
+            rrect = face.render_raw_to(surf.get_view('2'), text, ptsize=24,
+                                       style=ft.STYLE_UNDERLINE)
+            self.assertEqual(rrect, srect)
+
+        # Unaliased (mono) rendering to ints greater than 1 byte
+        # (__render_glyph_MONO_as_INT)
+        face.antialiased = False
+        try:
+            srect = face.get_rect(text, ptsize=24)
+
+            for bpp in [16, 24, 32]:
+                surf = pygame.Surface(srect.size, 0, bpp)
+                rrect = face.render_raw_to(surf.get_view('2'), text, ptsize=24)
+                self.assertEqual(rrect, srect)
+        finally:
+            face.antialiased = True
+
     def test_freetype_Face_style(self):
 
         face = self._TEST_FONTS['sans']
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.