## File: Drawable.tex

package info (click to toggle)
imagemagick 6:6.0.6.2-2.9
 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955 \section{Magick::Drawable} \scriptsize{ \begin{verbatim} Drawable provides a convenient interface for preparing vector, image, or text arguments for the Image::draw() method. Each instance of a Drawable sub-class represents a single drawable object. Drawable objects may be drawn "one-by-one" via multiple invocations of the Image draw() method, or may be drawn "all-at-once" by passing a list of Drawable objects to the Image draw() method. The one-by-one approach is convenient for simple drawings, while the list-based approach is appropriate for drawings which require more sophistication. The following is an example of using the Drawable subclasses with the one-by-one approach to draw the following figure: [Drawable_example_1.png] #include #include #include using namespace std; using namespace Magick; int main(int /*argc*/,char **/*argv*/) { try { // Create base image (white image of 300 by 200 pixels) Image image( Geometry(300,200), Color("white") ); // Set draw options image.strokeColor("red"); // Outline color image.fillColor("green"); // Fill color image.strokeWidth(5); // Draw a circle image.draw( DrawableCircle(100,100, 50,100) ); // Draw a rectangle image.draw( DrawableRectangle(200,200, 270,170) ); // Display the result image.display( ); } catch( exception &error_ ) { cout << "Caught exception: " << error_.what() << endl; return 1; } return 0; } Since Drawable is an object it may be saved in an array or a list for later (perhaps repeated) use. The following example shows how to draw the same figure using the list-based approach #include #include #include #include using namespace std; using namespace Magick; int main(int /*argc*/,char **/*argv*/) { try { // Create base image (white image of 300 by 200 pixels) Image image( Geometry(300,200), Color("white") ); // Construct drawing list std::list drawList; // Add some drawing options to drawing list drawList.push_back(DrawableStrokeColor("red")); // Outline color drawList.push_back(DrawableStrokeWidth(5)); // Stroke width drawList.push_back(DrawableFillColor("green")); // Fill color // Add a Circle to drawing list drawList.push_back(DrawableCircle(100,100, 50,100)); // Add a Rectangle to drawing list drawList.push_back(DrawableRectangle(200,100, 270,170)); // Draw everything using completed drawing list image.draw(drawList); // Display the result image.display( ); } catch( exception &error_ ) { cout << "Caught exception: " << error_.what() << endl; return 1; } return 0; } Drawable depends on the simple Coordinate structure which represents a pair of x,y coodinates. The methods provided by the Coordinate structure are shown in the following table: Coordinate Structure Methods Method/Member Signature Description Coordinate void Default Constructor double x_, double y_ Constructor, setting first & second x double x_ x coordinate member y double y_ y coordinate member The Drawable classes are shown in the following table: Drawable Classes Sub-Class Constructor Signature Description double sx_, double sy_, double Set scaling, rotation, and DrawableAffine rx_, double ry_, double tx_, translation (coordinate double ty_ transformation). DrawableAngle double angle_ Set drawing angle Draw an arc using the stroke double startX_, double color and based on the circle starting at coordinates DrawableArc startY_, double endX_, double startX_,startY_, and ending endY_, double startDegrees, double endDegrees_ with coordinates endX_,endY_, and bounded by the rotational arc startDegrees_,endDegrees_ const Draw a Bezier curve using the DrawableBezier std::list stroke color and based on the &coordinates_ coordinates specified by the coordinates_ list. Draw a circle using the double originX_, double stroke color and thickness DrawableCircle originY_, double perimX_, using specified origin and double perimY_ perimeter coordinates. If a fill color is specified, then the object is filled. Color image according to paintMethod. The point method recolors the target pixel. The replace method recolors any pixel that matches the color of the target pixel. DrawableColor double x_, double y_, Floodfill recolors any pixel PaintMethod paintMethod_ that matches the color of the target pixel and is a neighbor, whereas filltoborder recolors any neighbor pixel that is not the border color. Finally, reset recolors all pixels. Composite current image with contents of specified image, at specified coordinates. If the matte attribute is set to true, then the image DrawableCompositeImage double x_, double y_, const composition will consider an std::string &filename_ alpha channel, or transparency, present in the image file so that non-opaque portions allow part (or all) of the composite image to show through. double x_, double y_, const Image &image_ Composite current image with contents of specified image, rendered with specified width and height, at specified coordinates. If the matte attribute is set to true, then the image composition double x_, double y_, double will consider an alpha width_, double height_, const channel, or transparency, std::string &filename_ present in the image file so that non-opaque portions allow part (or all) of the composite image to show through. If the specified width or height is zero, then the image is composited at its natural size, without enlargement or reduction. double x_, double y_, double width_, double height_, const Image &image_ Composite current image with contents of specified image, rendered with specified width and height, using specified composition algorithm, at specified coordinates. If the matte attribute is set to true, then the image double x_, double y_, double composition will consider an width_, double height_, const alpha channel, or std::string &filename_, transparency, present in the CompositeOperator composition_ image file so that non-opaque portions allow part (or all) of the composite image to show through. If the specified width or height is zero, then the image is composited at its natural size, without enlargement or reduction. double x_, double y_, double width_, double height_, const Image &image_, CompositeOperator composition_ DrawableTextDecoration DecorationType decoration_ Specify decoration to apply to text. Specify the pattern of dashes and gaps used to stroke paths. The strokeDashArray represents a zero-terminated array of numbers that specify the lengths of alternating dashes and gaps in pixels. If DrawableDashArray const unsigned int* dasharray_ an odd number of values is provided, then the list of values is repeated to yield an even number of values. A typical strokeDashArray_ array might contain the members 5 3 2 0, where the zero value indicates the end of the pattern array. Specify the distance into the dash pattern to start the DrawableDashOffset unsigned int offset_ dash. See documentation on SVG's stroke-dashoffset property for usage details. Draw an ellipse using the stroke color and thickness, double originX_, double specified origin, x & y DrawableEllipse originY_, double radiusX_, radius, as well as specified double radiusY_, double start and end of arc in arcStart_, double arcEnd_ degrees. If a fill color is specified, then the object is filled. DrawableFillColor const Color &color_ Specify drawing object fill color. Specify the algorithm which is to be used to determine what parts of the canvas are DrawableFillRule FillRule fillRule_ included inside the shape. See documentation on SVG's fill-rule property for usage details. DrawableFillOpacity double opacity_ Specify opacity to use when drawing using fill color. DrawableFont const std::string &font_ Specify font name to use when drawing text. Specify font family, style, weight (one of the set { 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900 } with 400 const std::string &family_, being the normal size), and stretch to be used to select StyleType style_, the font used when drawing unsigned long weight_, StretchType stretch_ text. Wildcard matches may be applied to style via the AnyStyle enumeration, applied to weight if weight is zero, and applied to stretch via the AnyStretch enumeration. DrawableGravity GravityType gravity_ Specify text positioning gravity. double startX_, double Draw a line using stroke DrawableLine startY_, double endX_, double color and thickness using endY_ starting and ending coordinates Change the pixel matte value to transparent. The point method changes the matte value of the target pixel. The replace method changes the matte value of any pixel that matches the color of the target pixel. Floodfill DrawableMatte double x_, double y_, changes the matte value of PaintMethod paintMethod_ any pixel that matches the color of the target pixel and is a neighbor, whereas filltoborder changes the matte value of any neighbor pixel that is not the border color, Finally reset changes the matte value of all pixels. Specify miter limit. When two line segments meet at a sharp angle and miter joins have been specified for 'lineJoin', it is possible for the miter to extend far DrawableMiterLimit unsigned int miterLimit_ beyond the thickness of the line stroking the path. The miterLimit' imposes a limit on the ratio of the miter length to the 'lineWidth'. The default value of this parameter is 4. DrawablePath const std::list Draw on image using vector &path_ path. Draw a point using stroke DrawablePoint double x_, double y_ color and thickness at coordinate DrawablePointSize double pointSize_ Set font point size. Draw an arbitrary polygon using stroke color and const thickness consisting of three DrawablePolygon std::list or more coordinates contained &coordinates_ in an STL list. If a fill color is specified, then the object is filled. Draw an arbitrary polyline using stroke color and const thickness consisting of three DrawablePolyline std::list or more coordinates contained &coordinates_ in an STL list. If a fill color is specified, then the object is filled. Pop Graphic Context. Removing the current graphic context from the graphic context DrawablePopGraphicContext void stack restores the options to the values they had prior to the preceding DrawablePushGraphicContext operation. Push Graphic Context. When a graphic context is pushed, options set after the context is pushed (such as coordinate transformations, color settings, etc.) are saved to a new graphic context. This allows related options to be DrawablePushGraphicContextvoid saved on a graphic context "stack" in order to support heirarchical nesting of options. When DrawablePopGraphicContext is used to pop the current graphic context, the options in effect during the last DrawablePushGraphicContext operation are restored. Start a pattern definition with arbitrary pattern name specified by id_, pattern offset specified by x_ and y_, and pattern size specified by width_ and height_. The pattern is defined within the coordinate system defined by the specified offset and size. std::string &id_, long x_, Arbitrary drawing objects DrawablePushPattern long y_, long width_, long (including height_ DrawableCompositeImage) may be specified between DrawablePushPattern and DrawablePopPattern in order to draw the pattern. Normally the pair DrawablePushGraphicContext & DrawablePopGraphicContext are used to enclose a pattern definition. Pattern definitions are terminated by a DrawablePopPattern object. Terminate a pattern DrawablePopPattern void definition started via DrawablePushPattern. Draw a rectangle using stroke double upperLeftX_, double color and thickness from DrawableRectangle upperLeftY_, double upper-left coordinates to lowerRightX_, double lower-right coordinates. If lowerRightY a fill color is specified, then the object is filled. Set rotation to use when DrawableRotation double angle_ drawing (coordinate transformation). Draw a rounded rectangle using stroke color and double centerX_, double thickness, with specified centerY_, double width_, center coordinate, specified DrawableRoundRectangle double hight_, double width and height, and cornerWidth_, double specified corner width and cornerHeight_ height. If a fill color is specified, then the object is filled. Apply scaling in x and y DrawableScaling double x_, double y_ direction while drawing objects (coordinate transformation). DrawableSkewX double angle_ Apply Skew in X direction (coordinate transformation) DrawableSkewY double angle_ Apply Skew in Y direction DrawableStrokeAntialias bool flag_ Antialias while drawing lines or object outlines. DrawableStrokeColor const Color &color_ Set color to use when drawing lines or object outlines. Specify the shape to be used at the end of open subpaths DrawableStrokeLineCap LineCap linecap_ when they are stroked. Values of LineCap are UndefinedCap, ButtCap, RoundCap, and SquareCap. Specify the shape to be used at the corners of paths (or other vector shapes) when DrawableStrokeLineJoin LineJoin linejoin_ they are stroked. Values of LineJoin are UndefinedJoin, MiterJoin, RoundJoin, and BevelJoin. DrawableStrokeOpacity double opacity_ Opacity to use when drawing lines or object outlines. DrawableStrokeWidth double width_ Set width to use when drawing lines or object outlines. Annotate image with text using stroke color, font, font pointsize, and box color (text background color), at specified coordinates. If DrawableText double x_, double y_, text contains special format std::string text_ characters the image filename, type, width, height, or other image attributes may be incorporated in the text (see label()). DrawableTranslation double x_, double y_ Apply coordinate translation (set new coordinate origin). DrawableTextAntialias bool flag_ Antialias while drawing text. Dimensions of the output viewbox. If the image is to be written to a vector format (e.g. MVG or SVG), then a DrawablePushGraphicContext() unsigned long x1_, unsigned object should be pushed to DrawableViewbox long y1_, unsigned long x2_, the head of the list, unsigned long y2_ followed by a DrawableViewbox() statement to establish the output canvas size. A matching DrawablePopGraphicContext() object should be pushed to the tail of the list. Vector Path Classes The vector paths supported by Magick++ are based on those supported by the SVG XML specification. Vector paths are not directly drawable, they must first be supplied as a constructor argument to the DrawablePath class in order to create a drawable object. The DrawablePath class effectively creates a drawable compound component which may be replayed as desired. If the drawable compound component consists only of vector path objects using relative coordinates then the object may be positioned on the image by preceding it with a DrawablePath which sets the current drawing coordinate. Alternatively coordinate transforms may be used to translate the origin in order to position the object, rotate it, skew it, or scale it. The "moveto" commands The "moveto" commands establish a new current point. The effect is as if the "pen" were lifted and moved to a new location. A path data segment must begin with either one of the "moveto" commands or one of the "arc" commands. Subsequent "moveto" commands (i.e., when the "moveto" is not the first command) represent the start of a new subpath: Moveto Classes Sub-Class Constructor Signature Description Start a new sub-path at the given coordinate. PathMovetoAbs indicates that absolute coordinates will follow; PathMovetoRel indicates that relative coordinates will follow. If a relative PathMovetoAbs const Magick::Coordinate moveto appears as the &coordinate_ first element of the path, then it is treated as a pair of absolute coordinates. If a moveto is followed by multiple pairs of coordinates, the subsequent pairs are treated as implicit lineto commands. const std::list &coordinates_ PathMovetoRel const Magick::Coordinate &coordinate_ const std::list &coordinates_ The "closepath" command The "closepath" command causes an automatic straight line to be drawn from the current point to the initial point of the current subpath: Closepath Classes Sub-Class Constructor Description Signature Close the current subpath by drawing a straight line from the current point to PathClosePath void current subpath's most recent starting point (usually, the most recent moveto point). The "lineto" commands The various "lineto" commands draw straight lines from the current point to a new point: Lineto Classes Sub-Class Constructor Signature Description Draw a line from the current point to the given coordinate which becomes the new current point. PathLinetoAbs indicates that absolute coordinates are used; PathLinetoRel indicates PathLinetoAbs const Magick::Coordinate& that relative coordinate_ coordinates are used. A number of coordinates pairs may be specified in a list to draw a polyline. At the end of the command, the new current point is set to the final set of coordinates provided. const std::list &coordinates_ PathLinetoRel const Magick::Coordinate& coordinate_ const std::list &coordinates_ Draws a horizontal line from the current point (cpx, cpy) to (x, cpy). PathLinetoHorizontalAbs indicates that absolute coordinates are supplied; PathLinetoHorizontalAbsdouble x_ PathLinetoHorizontalRel indicates that relative coordinates are supplied. At the end of the command, the new current point becomes (x, cpy) for the final value of x. PathLinetoHorizontalRel double x_ Draws a vertical line from the current point (cpx, cpy) to (cpx, y). PathLinetoVerticalAbs indicates that absolute coordinates are supplied; PathLinetoVerticalAbs double y_ PathLinetoVerticalRel indicates that relative coordinates are supplied. At the end of the command, the new current point becomes (cpx, y) for the final value of y. PathLinetoVerticalRel double y_ The curve commands These three groups of commands draw curves: * Cubic Bezier commands. A cubic Bezier segment is defined by a start point, an end point, and two control points. * Quadratic Bezier commands. A quadratic Bezier segment is defined by a start point, an end point, and one control point. * Elliptical arc commands. An elliptical arc segment draws a segment of an ellipse. The cubic Bezier curve commands The cubic Bezier commands depend on the PathCurvetoArgs argument class, which has the constructor signature PathCurvetoArgs( double x1_, double y1_, double x2_, double y2_, double x_, double y_ ); The commands are as follows: Cubic Bezier Curve Classes Sub-Class Constructor Signature Description Draws a cubic Bezier curve from the current point to (x,y) using (x1,y1) as the control point at the beginning of the curve and (x2,y2) as the control point at the end of the curve. PathCurvetoAbs indicates that absolutecoordinates PathCurvetoAbs const Magick::PathCurvetoArgs will follow; &args_ PathCurvetoRel indicates that relative coordinates will follow. Multiple sets of coordinates may be specified to draw a polyBezier. At the end of the command, the new current point becomes the final (x,y) coordinate pair used in the polyBezier. const std::list &args_ PathCurvetoRel const Magick::PathCurvetoArgs &args_ const std::list &args_ Draws a cubic Bezier curve from the current point to (x,y). The first control point is assumed to be the reflection of the second control point on the previous command relative to the current point. (If there is no previous command or if the previous command was not an PathCurvetoAbs, PathCurvetoRel, PathSmoothCurvetoAbs or PathSmoothCurvetoRel, assume the first control point is coincident with the PathSmoothCurvetoAbsconst Magick::Coordinate current point.) &coordinates_ (x2,y2) is the second control point (i.e., the control point at the end of the curve). PathSmoothCurvetoAbs indicates that absolute coordinates will follow; PathSmoothCurvetoRel indicates that relative coordinates will follow. Multiple sets of coordinates may be specified to draw a polyBezier. At the end of the command, the new current point becomes the final (x,y) coordinate pair used in the polyBezier. const std::list &coordinates_ PathSmoothCurvetoRelconst Magick::Coordinate &coordinates_ const std::list &coordinates_ The quadratic Bezier curve commands The quadratic Bezier commands depend on the PathQuadraticCurvetoArgs argument class, which has the constructor signature: PathQuadraticCurvetoArgs( double x1_, double y1_, double x_, double y_ ); The quadratic Bezier commands are as follows: Quadratic Bezier Curve Classes Sub-Class Constructor Signature Description Draws a quadratic Bezier curve from the current point to (x,y) using (x1,y1) as the control point. PathQuadraticCurvetoAbs indicates that absolute coordinates will follow; PathQuadraticCurvetoRel PathQuadraticCurvetoAbs const Magick::PathQuadraticCurvetoArgs indicates that relative &args_ coordinates will follow. Multiple sets of coordinates may be specified to draw a polyBezier. At the end of the command, the new current point becomes the final (x,y) coordinate pair used in the polyBezier. const std::list &args_ PathQuadraticCurvetoRel const Magick::PathQuadraticCurvetoArgs &args_ const std::list &args_ Draws a quadratic Bezier curve from the current point to (x,y). The control point is assumed to be the reflection of the control point on the previous command relative to the current point. (If there is no previous command or if the previous command was not a PathQuadraticCurvetoAbs, PathQuadraticCurvetoRel, PathSmoothQuadraticCurvetoAbs PathSmoothQuadraticCurvetoAbsconst Magick::Coordinate &coordinate_ or PathSmoothQuadraticCurvetoRel, assume the control point is coincident with the current point.) PathSmoothQuadraticCurvetoAbs indicates that absolute coordinates will follow; PathSmoothQuadraticCurvetoRel indicates that relative coordinates will follow. At the end of the command, the new current point becomes the final (x,y) coordinate pair used in the polyBezier. const std::list &coordinates_ PathSmoothQuadraticCurvetoRelconst Magick::Coordinate &coordinate_ const std::list &coordinates_ The elliptical arc curve commands The elliptical arc curve commands depend on the PathArcArgs argument class, which has the constructor signature: PathArcArgs( double radiusX_, double radiusY_, double xAxisRotation_, bool largeArcFlag_, bool sweepFlag_, double x_, double y_ ); The elliptical arc commands are as follows: Elliptical Arc Curve Classes Sub-Class Constructor Signature Description Draws an elliptical arc from the current point to (x, y). The size and orientation of the ellipse are defined by two radii (radiusX, radiusY) and an xAxisRotation, which indicates how the ellipse as a whole is rotated relative to the current coordinate system. The center (cx, cy) of the ellipse is PathArcAbs const Magick::PathArcArgs calculated automatically &coordinates_ to satisfy the constraints imposed by the other parameters. largeArcFlag and sweepFlag contribute to the automatic calculations and help determine how the arc is drawn. If largeArcFlag is true then draw the larger of the available arcs. If sweepFlag is true, then draw the arc matching a clock-wise rotation. const std::list &coordinates_ PathArcRel const Magick::PathArcArgs &coordinates_ const std::list &coordinates_ \end{verbatim} }