Simd Library Documentation.

Home | Release Notes | Download | Documentation | Issues | GitHub

The Font class is a C++ wrapper for drawing ASCII text on images. More...

#include <SimdFont.hpp>

Public Types

typedef std::string String
 
typedef Simd::Point< ptrdiff_t > Point
 
typedef Simd::View< Simd::Allocator > View
 

Public Member Functions

 Font (size_t height=16)
 
 ~Font ()
 
bool Resize (size_t height)
 
size_t Height () const
 
Point Measure (const String &text) const
 
template<class Color >
bool Draw (View &canvas, const String &text, const Point &position, const Color &color) const
 
template<class Color >
bool Draw (View &canvas, const String &text, const View::Position &position, const Color &color) const
 
template<class Color >
bool Draw (View &canvas, const String &text, const View::Position &position, const Color &color, const Color &background) const
 

Detailed Description

The Font class is a C++ wrapper for drawing ASCII text on images.

The class wraps C API functions SimdFontInit, SimdFontResize, SimdFontHeight, SimdFontMeasure and SimdFontDraw. It uses a built-in monospace-like font generated from the generic monospace font of Gdiplus. The font supports printable ASCII glyphs. The character '
' starts a new line. Other characters are ignored.

A typical usage creates one Font, optionally resizes it to a fraction of the canvas height (for example canvas.height / 32), and then draws labels at pixel coordinates or at a named Simd::View::Position. Height() is used as a line step when several strings are stacked, or to place a label above a rectangle. Measure() returns the pixel size required to draw a string.

The canvas must have 1, 2, 3 or 4 eight-bit channels. Pixel size of canvas must be equal to sizeof(Color). Text is blended into the canvas through an 8-bit glyph alpha mask. Drawing is clipped to the canvas.

Using example:

#include "Simd/SimdFont.hpp"
int main()
{
typedef Simd::Pixel::Bgra32 Color;
View image(320, 240, View::Bgra32);
Simd::FillPixel(image, Color(128, 128, 0));
Simd::Font font(32);
font.Draw(image, "Hello, Simd!", View::MiddleCenter, Color(0, 0, 255));
String text = "First_string,\nSecond-line.";
font.Draw(image, text, View::BottomRight, Color(0, 0, 0));
font.Resize(24);
font.Draw(image, text, View::TopLeft, Color(0, 0, 0), Color(255, 255, 255));
font.Resize(16);
font.Draw(image, "id=1", Point(8, 8), Color(255, 255, 255));
font.Draw(image, "in 1", Point(8, 8 + (ptrdiff_t)font.Height()), Color(255, 255, 0));
image.Save("HelloSimd.ppm");
return 0;
}
The Font class is a C++ wrapper for drawing ASCII text on images.
Definition: SimdFont.hpp:91
Simd::Point< ptrdiff_t > Point
Definition: SimdFont.hpp:94
Simd::View< Simd::Allocator > View
Definition: SimdFont.hpp:95
std::string String
Definition: SimdFont.hpp:93
SIMD_INLINE void FillPixel(View< A > &dst, const Pixel &pixel)
Fills every image pixel with the given pixel value.
Definition: SimdLib.hpp:2011
32-bit BGRA pixel.
Definition: SimdPixel.hpp:246
@ Bgra32
Definition: SimdView.hpp:224
@ BottomRight
Definition: SimdView.hpp:277
@ TopLeft
Definition: SimdView.hpp:269
@ MiddleCenter
Definition: SimdView.hpp:273
Note
This is a wrapper around the low-level Drawing API.

Member Typedef Documentation

◆ String

typedef std::string String

Text string type used by Measure and Draw.

◆ Point

typedef Simd::Point<ptrdiff_t> Point

Point type used as a text position (x, y) and as a measured text size (width, height).

◆ View

Image type used as a canvas for Draw.

Constructor & Destructor Documentation

◆ Font()

Font ( size_t  height = 16)

Creates a new Font class with the given glyph height.

The constructor creates an internal font context by SimdFontInit and then calls Resize with the given height. The font contains embedded ASCII glyphs (originally generated from the generic monospace font of Gdiplus). Supported glyphs are printable ASCII characters. The character '
' starts a new line. Other characters are ignored.

Parameters
[in]height- initial glyph height in pixels. By default it is equal to 16. The value must be inside the supported range of the embedded font.

◆ ~Font()

~Font ( )

Releases the internal font context.

The destructor calls SimdRelease for the context created by SimdFontInit.

Member Function Documentation

◆ Resize()

bool Resize ( size_t  height)

Sets a new glyph height.

The function recreates internal 8-bit alpha glyph images from embedded font data. It returns false if height is outside the supported range of the embedded font. Reusing the current height is a successful no-op. Typical usage scales the font to the canvas, for example canvas.height / 32.

Parameters
[in]height- a new glyph height in pixels.
Returns
true on success and false on failure.

◆ Height()

size_t Height ( ) const

Gets current glyph height in pixels.

The value is used as a vertical step when several text lines are drawn one under another, and to place a label above a rectangle (top - Height()).

Returns
current glyph height in pixels. It is equal to 0 if the font context is not created.

◆ Measure()

Point Measure ( const String &  text) const

Measures the size of the rectangle required to draw the given text.

Supported glyphs advance the current X position by the current glyph width. The '
' character starts a new line and advances Y by the current glyph height. Unsupported characters are ignored. If the text contains at least one drawable glyph, the returned size also includes the font indentation on all sides.

The measured size is used by Draw overloads that take Simd::View::Position in order to create a destination region with Simd::View::Region.

Parameters
[in]text- a text to measure.
Returns
measured size (width in x, height in y). It is equal to (0, 0) if the font context is not created or the text has no drawable glyphs.

◆ Draw() [1/3]

bool Draw ( View &  canvas,
const String &  text,
const Point &  position,
const Color &  color 
) const

Draws text at the given pixel position.

The position is the top-left corner of the measured text region; glyphs are shifted by the current font indentation inside it. Drawing is clipped to the canvas. Supported glyphs advance X by the current glyph width, '
' starts a new line, and unsupported characters are ignored.

The canvas must have 1, 2, 3 or 4 eight-bit channels. Pixel size of canvas must be equal to sizeof(Color). Text is blended into the canvas through an 8-bit glyph alpha mask.

Note
This function is a C++ wrapper for function SimdFontDraw.
Parameters
[out]canvas- a canvas image.
[in]text- a text to draw.
[in]position- the top-left position of the measured text region.
[in]color- a color of the text. Pixel size of canvas must be equal to sizeof(Color).
Returns
true if the font context exists; otherwise false.

◆ Draw() [2/3]

bool Draw ( View &  canvas,
const String &  text,
const View::Position &  position,
const Color &  color 
) const

Draws text at a named position of the canvas.

The function measures the text, takes a region of this size with Simd::View::Region at the given Simd::View::Position (for example View::TopLeft, View::MiddleCenter, View::BottomRight) and draws the text at the origin of this region.

Parameters
[out]canvas- a canvas image.
[in]text- a text to draw.
[in]position- a named position of the text region (see Simd::View::Position).
[in]color- a color of the text. Pixel size of canvas must be equal to sizeof(Color).
Returns
true if the font context exists; otherwise false.

◆ Draw() [3/3]

bool Draw ( View &  canvas,
const String &  text,
const View::Position &  position,
const Color &  color,
const Color &  background 
) const

Draws text at a named position and fills the text background.

The function measures the text, takes a region of this size with Simd::View::Region at the given Simd::View::Position, fills this region by the background color with Simd::FillPixel and then draws the text.

Parameters
[out]canvas- a canvas image.
[in]text- a text to draw.
[in]position- a named position of the text region (see Simd::View::Position).
[in]color- a color of the text. Pixel size of canvas must be equal to sizeof(Color).
[in]background- a background color of the text region. Pixel size of canvas must be equal to sizeof(Color).
Returns
true if the font context exists; otherwise false.