Drawing Graphics and Text

Rendering 2D shapes, text and images on paint surfaces. More...

Classes

class  Bitmap
 Off-screen image that can be painted. More...
class  ColorStop
 Color stop for a gradient brush. More...
class  ColorStops
 Ordered collection of gradient color stops. More...
class  Brush
 Fill color, gradient or texture. More...
class  Canvas
 Backend that executes drawing commands. More...
class  CompositionMode
 How new pixels combine with existing pixels. More...
class  Font
 Font family, size and style. More...
class  FontFace
 Resolved font family and style. More...
class  FontMetrics
 Ascent, descent and line height of a font. More...
class  FontProvider
 Backend that loads font files. More...
class  FontRegistry
 Registry of font files. More...
class  Paint
 Pen, brush, font and composition mode. More...
class  PaintContext
 Active painting session on a surface. More...
class  Painter
 Painter for a surface or a paint context. More...
class  PainterBase
 Drawing commands and paint state. More...
class  PaintSurface
 Target for drawing operations. More...
class  Path
 Sequence of lines and curves. More...
class  PathElement
 Read-only view of a path element. More...
class  PathIterator
 Forward iterator over path elements. More...
class  Pen
 Outline color, width and style. More...
class  Scaling
 Conversion between logical units and pixels. More...
class  TextMetrics
 Width and bounds of a text run. More...
class  Transform
 2D affine transform. More...

Enumerations

enum class  FillRule {
  NonZero ,
  EvenOdd
}
 Determines how overlapping subpaths of a filled shape are painted. More...

Detailed Description

Drawing always targets a PaintSurface. The surface reports format, physical size, and Scaling, and it supplies the backend resources used to rasterize commands. Bitmap is the portable in-memory surface. After painting, Bitmap::image() returns the pixels so they can be copied, encoded, or drawn again. Other modules provide further surfaces: a Forms paint surface is the same abstraction on a window or control. Layouting and Painting documents when Forms paints; this chapter documents the drawing commands.

A PaintContext is an active session on a surface. It can install a default clip that every painter on that session intersects. Constructing a context attaches it to the surface; destroying it detaches it. The surface must outlive the context.

Painter is the type a caller constructs. It begins painting on a surface or on an existing context, through a constructor or begin(), and finish() ends the session. PainterBase is the command API: pens, brushes, fonts, transforms, clips, and the draw and fill operations. Painter does not own the surface or the context. One painter is bound to at most one target at a time.

Paint state is independent of geometry. Pen strokes outlines, Brush fills closed shapes, Font selects a typeface for text, and CompositionMode chooses source-copy or source-over blending. Paint bundles those four so the same state can be reused. setTransform() maps user coordinates before they reach the surface. setClip() restricts drawing to a rectangle. Coordinates passed to drawing commands are logical; the surface Scaling converts them to physical pixels.

Path stores moves, lines, and curves independently of a painter. A painter can stroke or fill its current path, or stroke or fill a path passed to drawPath() and fillPath(). Containment tests on the path use a FillRule and do not require a surface.

Text uses one font request for measurement and drawing. Font names a family, size, and style; it is not a loaded face. fontMetrics() returns ascent, descent, and line height for the current font. textMetrics() measures one string. drawText() draws that string at a baseline origin. Font::addFont() and Font::addFonts() register font files for later requests. Bitmap lists the families and faces the backend can resolve.

drawImage() and drawBitmap() composite image content onto the current target. The image and bitmap APIs remain the storage model; these operations only sample them.

Canvas is the backend that executes commands. Its constructor is protected. Ordinary callers do not create a canvas; the surface creates one while a painter is active.

The example paints into a bitmap, then reads the result as an image. The painter does not own the bitmap.

Pt::Gfx::Bitmap bitmap(Pt::Gfx::SizeF(320, 240));
Pt::Gfx::Painter painter(bitmap);
painter.setBrush(Pt::Gfx::Brush(Pt::Gfx::Color(255, 255, 255)));
painter.fillRect(Pt::Gfx::RectF(Pt::Gfx::SizeF(320, 240)));
painter.setPen(Pt::Gfx::Pen(Pt::Gfx::Color(0, 0, 0)));
painter.drawLine(Pt::Gfx::PointF(0, 0), Pt::Gfx::PointF(320, 240));
painter.setFont(Pt::Gfx::Font("Sans", 12));
painter.drawText(Pt::Gfx::PointF(10, 24), Pt::String("Hello"));
painter.finish();
const Pt::Gfx::Image& image = bitmap.image();
Off-screen image that can be painted.
Definition Bitmap.h:71
Fill color, gradient or texture.
Definition Brush.h:151
8-bit ARGB color.
Definition Color.h:65
Font family, size and style.
Definition Font.h:63
Painter for a surface or a paint context.
Definition Painter.h:62
Outline color, width and style.
Definition Pen.h:59
Unicode capable basic_string.
Definition Api-String.h:67

Enumeration Type Documentation

◆ FillRule

enum class FillRule
strong

When a Path contains overlapping subpaths, FillRule controls which enclosed regions are considered inside and therefore filled.

Enumerator
NonZero 

Fills regions enclosed by a non-zero net winding count.

For each point, a horizontal ray is cast and the signed number of path crossings is counted. If the result is non-zero the point is inside. This is the SVG and PostScript default.

EvenOdd 

Fills regions enclosed by an odd number of path crossings.

For each point, a horizontal ray is cast and the total number of path crossings is counted. If the result is odd the point is inside. Also known as the alternating rule.