A paint device for rendering to a raster image. More...
#include <Wt/WRasterImage>
Public Member Functions | |
WRasterImage (const std::string &type, const WLength &width, const WLength &height, WObject *parent=0) | |
Creates a raster image. | |
~WRasterImage () | |
Destructor. | |
void | addFontCollection (const std::string &directory, bool recursive=true) |
Adds a font collection. | |
virtual WFlags< FeatureFlag > | features () const |
Returns device features. | |
virtual void | setChanged (WFlags< ChangeFlag > flags) |
Indicates changes in painter state. | |
virtual void | drawArc (const WRectF &rect, double startAngle, double spanAngle) |
Draws an arc. | |
virtual void | drawImage (const WRectF &rect, const std::string &imgUri, int imgWidth, int imgHeight, const WRectF &sourceRect) |
Draws an image. | |
virtual void | drawLine (double x1, double y1, double x2, double y2) |
Draws a line. | |
virtual void | drawPath (const WPainterPath &path) |
Draws a path. | |
virtual void | drawText (const WRectF &rect, WFlags< AlignmentFlag > alignmentFlags, TextFlag textFlag, const WString &text) |
Draws text. | |
virtual WTextItem | measureText (const WString &text, double maxWidth=-1, bool wordWrap=false) |
Measures rendered text size. | |
virtual WFontMetrics | fontMetrics () |
Returns font metrics. | |
virtual void | init () |
Initializes the device for painting. | |
virtual void | done () |
Finishes painting on the device. | |
virtual bool | paintActive () const |
Returns whether painting is active. | |
virtual WLength | width () const |
Returns the device width. | |
virtual WLength | height () const |
Returns the device height. | |
virtual void | handleRequest (const Http::Request &request, Http::Response &response) |
Handles a request. | |
void | setPixel (int x, int y, const WColor &color) |
Low-level paint method. | |
WColor | getPixel (int x, int y) |
Low-level paint method. | |
void | clear () |
Clears the image. | |
Protected Member Functions | |
virtual WPainter * | painter () const |
Returns the painter that is currently painting on the device. | |
virtual void | setPainter (WPainter *painter) |
Sets the painter. |
A paint device for rendering to a raster image.
A WRasterImage paint device provides support for creating raster images (such as PNG or GIF images).
It implements two main use-cases:
The rendering is provided by GraphicsMagick, and this class is included in the library only if libgraphicsmagick
was found during the build of the library. If Wt is built to use libpango
for font support, then text rendering is done using this library. Otherwise, you may want to configure TrueType font search directories using addFontCollection().
You can use the image as a resource and specialize handleRequest() to paint the contents on the fly. Alternatively can also use write() to serialize to an image file (std::ostream).
The latter usage is illustrated by the code below:
Wt::Chart::WCartesianChart *chart = ... Wt::WRasterImage pngImage("png", 600, 400); { Wt::WPainter p(&pngImage); chart->paint(p); } std::ofstream f("chart.png", std::ios::out | std::ios::binary); pngImage.write(f);
This paint device has the following limitations:
Wt::WRasterImage::WRasterImage | ( | const std::string & | type, |
const WLength & | width, | ||
const WLength & | height, | ||
WObject * | parent = 0 |
||
) |
Creates a raster image.
type
indicates an image type. The mime type of the resource is "image/"
type
.
Wt supports the following image types (amongst others):
void Wt::WRasterImage::addFontCollection | ( | const std::string & | directory, |
bool | recursive = true |
||
) |
Adds a font collection.
If Wt has been configured to use libpango
, then font matching and character selection is done by libpango, which is seeded with information on installed fonts by fontconfig. In that case, invocations for this method is ignored.
If Wt has not been configured to use libpango
, then this method may be used to indicate the location of true type fonts. The main drawback compared to libpango is that font selection is not steered by the need for particular characters, i.e. font selection is independent from the text's need for specific characters. Most truetype fonts provided only partial unicode support. The provided directory
will be searched for fonts (currently only TrueType ".ttf" or ".ttc" fonts).
void Wt::WRasterImage::clear | ( | ) |
Clears the image.
Fills the image with a white background.
void Wt::WRasterImage::done | ( | ) | [virtual] |
Finishes painting on the device.
This method is called when a WPainter stopped painting.
Implements Wt::WPaintDevice.
void Wt::WRasterImage::drawArc | ( | const WRectF & | rect, |
double | startAngle, | ||
double | spanAngle | ||
) | [virtual] |
Draws an arc.
The arc is defined as in WPainter::drawArc(const WRectF&, startAngle, spanAngle), but the angle is expressed in degrees.
The arc must be stroked, filled, and transformed using the current painter settings.
Implements Wt::WPaintDevice.
void Wt::WRasterImage::drawImage | ( | const WRectF & | rect, |
const std::string & | imageUri, | ||
int | imgWidth, | ||
int | imgHeight, | ||
const WRectF & | sourceRect | ||
) | [virtual] |
Draws an image.
Draws sourceRect from the image with URL imageUri
and original dimensions imgWidth and imgHeight
to the location, into the rectangle defined by rect
.
The image is transformed using the current painter settings.
Implements Wt::WPaintDevice.
void Wt::WRasterImage::drawLine | ( | double | x1, |
double | y1, | ||
double | x2, | ||
double | y2 | ||
) | [virtual] |
Draws a line.
The line must be stroked and transformed using the current painter settings.
Implements Wt::WPaintDevice.
void Wt::WRasterImage::drawPath | ( | const WPainterPath & | path ) | [virtual] |
Draws a path.
The path must be stroked, filled, and transformed using the current painter settings.
Implements Wt::WPaintDevice.
void Wt::WRasterImage::drawText | ( | const WRectF & | rect, |
WFlags< AlignmentFlag > | alignmentFlags, | ||
TextFlag | textFlag, | ||
const WString & | text | ||
) | [virtual] |
Draws text.
The text must be rendered, stroked and transformed using the current painter settings.
Implements Wt::WPaintDevice.
WFontMetrics Wt::WRasterImage::fontMetrics | ( | ) | [virtual] |
Returns font metrics.
This returns font metrics for the current font.
Throws a std::logic_error if the underlying device does not provide font metrics.
Implements Wt::WPaintDevice.
WColor Wt::WRasterImage::getPixel | ( | int | x, |
int | y | ||
) |
Low-level paint method.
Use this method to directly get the color for an individual pixel, when using the paint device without a painter.
void Wt::WRasterImage::handleRequest | ( | const Http::Request & | request, |
Http::Response & | response | ||
) | [virtual] |
Handles a request.
Reimplement this method so that a proper response is generated for the given request. From the request
object you can access request parameters and whether the request is a continuation request. In the response
object, you should set the mime type and stream the output data.
A request may also concern a continuation, indicated in Http::Request::continuation(), in which case the next part for a previously created continuation should be served.
While handling a request, which may happen at any time together with event handling, the library makes sure that the resource is not being concurrently deleted, but multiple requests may happend simultaneously for a single resource.
Implements Wt::WResource.
virtual WLength Wt::WRasterImage::height | ( | ) | const [virtual] |
Returns the device height.
The device height, in pixels, establishes the height of the device coordinate system.
Implements Wt::WPaintDevice.
void Wt::WRasterImage::init | ( | ) | [virtual] |
Initializes the device for painting.
This method is called when a WPainter starts painting.
Implements Wt::WPaintDevice.
WTextItem Wt::WRasterImage::measureText | ( | const WString & | text, |
double | maxWidth = -1 , |
||
bool | wordWrap = false |
||
) | [virtual] |
Measures rendered text size.
Returns the bounding rect of the given text when rendered using the current font.
If maxWidth
!= -1, then the text is truncated to fit in the width.
If wordWrap
= true
then text is truncated only at word boundaries. Note that in this case the whitespace at the truncated position is included in the text but not accounted for by the returned width (since usually you will not render the whitespace at the end of a line).
Throws a std::logic_error if the underlying device does not provide font metrics.
Implements Wt::WPaintDevice.
virtual bool Wt::WRasterImage::paintActive | ( | ) | const [virtual] |
virtual WPainter* Wt::WRasterImage::painter | ( | ) | const [protected, virtual] |
Returns the painter that is currently painting on the device.
Implements Wt::WPaintDevice.
void Wt::WRasterImage::setChanged | ( | WFlags< ChangeFlag > | flags ) | [virtual] |
Indicates changes in painter state.
The flags
argument is the logical OR of one or more change flags.
Implements Wt::WPaintDevice.
void Wt::WRasterImage::setPixel | ( | int | x, |
int | y, | ||
const WColor & | color | ||
) |
Low-level paint method.
Use this method to directly set colors for individual pixels, when using the paint device without a painter.
virtual WLength Wt::WRasterImage::width | ( | ) | const [virtual] |
Returns the device width.
The device width, in pixels, establishes the width of the device coordinate system.
Implements Wt::WPaintDevice.