Magick::Drawable

Drawable 提供了一個方便的介面,用於為 Image::draw() 方法準備向量、影像或文字參數。Drawable 子類別的每個實例都代表一個單獨的可繪製物件。可繪製物件可以透過多次呼叫 Image draw() 方法「逐個」繪製,也可以透過將可繪製物件清單傳遞給 Image draw() 方法「一次性」繪製。逐個繪製的方法適用於簡單的繪圖,而基於清單的方法則適用於需要更高複雜性的繪圖。

以下範例使用 Drawable 子類別,採用逐個繪製的方法繪製下圖

example

#include <string> #include <iostream> #include <Magick++.h> using namespace std; using namespace Magick; int main(int /*argc*/,char **argv) { try { InitializeMagick(*argv); // 建立基礎影像(300 x 200 像素的白色影像) Image image( Geometry(300,200), Color("white") ); // 設定繪圖選項 image.strokeColor("red"); // 輪廓顏色 image.fillColor("green"); // 填充顏色 image.strokeWidth(5); // 繪製圓形 image.draw( DrawableCircle(100,100, 50,100) ); // 繪製矩形 image.draw( DrawableRectangle(200,200, 270,170) ); // 顯示結果 image.display( ); } catch( exception & error_ ) { cout << "捕捉到例外狀況:" << error_.what() << endl; return 1; } return 0; }

由於 Drawable 是一個物件,因此可以將其儲存在陣列或清單中,以便稍後(可能重複)使用。以下範例顯示如何使用基於清單的方法繪製相同的圖形

#include <string> #include <iostream> #include <list> #include <Magick++.h> using namespace std; using namespace Magick; int main(int /*argc*/,char **/*argv*/) { try { InitializeMagick(*argv); // 建立基礎影像(300 x 200 像素的白色影像) Image image( Geometry(300,200), Color("white") ); // 建構繪圖清單 std::list<Magick::Drawable> drawList; // 將一些繪圖選項新增到繪圖清單 drawList.push_back(DrawableStrokeColor("red")); // 輪廓顏色 drawList.push_back(DrawableStrokeWidth(5)); // 筆劃寬度 drawList.push_back(DrawableFillColor("green")); // 填充顏色 // 將圓形新增到繪圖清單 drawList.push_back(DrawableCircle(100,100, 50,100)); // 將矩形新增到繪圖清單 drawList.push_back(DrawableRectangle(200,100, 270,170)); // 使用完整的繪圖清單繪製所有內容 image.draw(drawList); // 顯示結果 image.display( ); } catch( exception & error_ ) { cout << "捕捉到例外狀況:" << error_.what() << endl; return 1; } return 0; }

Drawable 仰賴簡單的 Coordinate 結構,該結構表示一對 x,y 座標。Coordinate 結構提供的方法如下表所示

Coordinate 結構方法

方法/成員

簽章

說明

Coordinate

void

預設建構函式

double x_, double y_

建構函式,設定 第一個 & 第二個

x

double x_

x 座標成員

y

double y_

y 座標成員

Drawable 類別如下表所示。這裡僅記錄建構函式的簽章。每個 Drawable 類別還提供了一些方法,可以透過這些方法調整每個參數。

Drawable 類別

子類別

建構函式簽章

說明

DrawableAffine

double sx_, double sy_, double rx_, double ry_, double tx_, double ty_

指定一個變換矩陣,以調整相同或後代繪圖上下文中後續繪製物件的縮放、旋轉和平移(座標變換)。sx_ & sy_ 參數代表 x & y 縮放因子,rx_ & ry_ 參數代表 x & y 旋轉,tx_ & ty_ 參數代表 x & y 平移。

void

指定一個變換矩陣,以調整相同或後代繪圖上下文中後續繪製物件的縮放、旋轉和平移(座標變換)。初始化為單位(無效)仿射值。使用類別方法(目前未記錄)從其單位值調整個別參數。

DrawableAngle

double angle_

設定繪圖角度

DrawableArc

double startX_, double startY_, double endX_, double endY_, double startDegrees, double endDegrees_

使用 *筆畫* 顏色繪製圓弧,圓弧基於起點坐標為 *startX_*、*startY_*、終點坐標為 *endX_*、*endY_* 的圓形,並由旋轉弧度 *startDegrees_*、*endDegrees_* 限定範圍。

DrawableBezier

const std::list<Magick::Coordinate> &coordinates_

使用 *筆畫* 顏色繪製貝茲曲線,曲線基於 *coordinates_* 列表指定的坐標。

DrawableClipPath

const std::string &id_

選擇與 *id_* 相符的繪圖剪裁路徑。

DrawableCircle

double originX_, double originY_, double perimX_, double perimY_

使用 *筆畫* 顏色和粗細繪製圓形,使用指定的圓心和周長坐標。如果指定了 *填充* 顏色,則會填充物件。

DrawableColor

double x_, double y_, PaintMethod paintMethod_

根據 paintMethod 對影像進行著色。點方法會重新著色目標像素。替換方法會重新著色任何與目標像素顏色相符的像素。Floodfill 會重新著色任何與目標像素顏色相符且相鄰的像素,而 filltoborder 會重新著色任何不是邊框顏色的相鄰像素。最後,Reset 會重新著色所有像素。

DrawableCompositeImage

double x_, double y_, const std::string &filename_

在指定的坐標處,將目前影像與指定影像的內容合成。如果 *matte* 屬性設定為 *true*,則影像合成會考慮影像檔案中存在的 Alpha 色板或透明度,以便非不透明部分允許部分(或全部)合成影像顯示出來。

double x_, double y_, const Image &image_

double x_, double y_, double width_, double height_, const std::string &filename_

在指定的坐標處,將目前影像與以指定寬度和高度呈現的指定影像的內容合成。如果 *matte* 屬性設定為 *true*,則影像合成會考慮影像檔案中存在的 Alpha 色板或透明度,以便非不透明部分允許部分(或全部)合成影像顯示出來。如果指定的 *width* 或 *height* 為零,則影像將以其原始大小合成,不會放大或縮小。

double x_, double y_, double width_, double height_, const Image &image_

double x_, double y_, double width_, double height_, const std::string &filename_, CompositeOperator composition_

在指定的坐標處,使用指定的合成演算法,將目前影像與以指定寬度和高度呈現的指定影像的內容合成。如果 *matte* 屬性設定為 *true*,則影像合成會考慮影像檔案中存在的 Alpha 色板或透明度,以便非不透明部分允許部分(或全部)合成影像顯示出來。如果指定的 *width* 或 *height* 為零,則影像將以其原始大小合成,不會放大或縮小。

double x_, double y_, double width_, double height_, const Image &image_, CompositeOperator composition_

DrawableDashArray

const double* dasharray_

指定用於描繪路徑的虛線和間隙樣式。strokeDashArray 表示一個以零結尾的數字陣列,這些數字指定了以像素為單位的虛線和間隙的交替長度。如果提供了奇數個值,則會重複值列表以產生偶數個值。典型的 strokeDashArray_ 陣列可能包含成員 5 3 2 0,其中零值表示樣式陣列的結尾。

DrawableDashOffset

double offset_

指定開始虛線的虛線樣式的距離。有關使用細節,請參閱 SVG 的 stroke-dashoffset 屬性的說明文件。

DrawableEllipse

double originX_, double originY_, double radiusX_, double radiusY_, double arcStart_, double arcEnd_

使用指定的筆畫顏色和粗細、指定的原點、x 和 y 半徑,以及指定的圓弧起點和終點(以度為單位)繪製橢圓。如果指定了填充顏色,則對象將被填充。

DrawableFillColor

const Color &color_

指定繪圖物件的填充顏色。

DrawableFillRule

FillRule fillRule_

指定用於確定畫布哪些部分包含在形狀內的演算法。請參閱 SVG 文件中的 fill-rule 屬性的說明文件。

DrawableFillOpacity

double opacity_

指定使用填充顏色繪製時要使用的透明度。

DrawableFont

const std::string &font_

指定繪製文字時要使用的字體名稱。

const std::string &family_,
StyleType style_,
unsigned int weight_,
StretchType stretch_

指定字體系列、樣式、粗細(集合 { 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900 } 中的一個,其中 400 為正常大小)和拉伸,用於選擇繪製文字時使用的字體。萬用字元匹配可以透過 AnyStyle 列舉套用於樣式,如果粗細為零則套用於粗細,並透過 AnyStretch 列舉套用於拉伸。

DrawableGravity

GravityType gravity_

指定文字定位重力。

DrawableLine

double startX_, double startY_, double endX_, double endY_

使用筆畫顏色和粗細,使用起點和終點坐標繪製一條線

DrawableMatte

double x_, double y_, PaintMethod paintMethod_

將像素遮罩值更改為透明。點方法更改目標像素的遮罩值。替換方法更改與目標像素顏色匹配的任何像素的遮罩值。泛洪填充更改與目標像素顏色匹配且相鄰的任何像素的遮罩值,而填充到邊界更改任何不是邊界顏色的相鄰像素的遮罩值,最後重置更改所有像素的遮罩值。

DrawableMiterLimit

size_t miterLimit_

指定斜接限制。當兩條線段以銳角相交並且為“lineJoin”指定了斜面接合時,斜接可能會延伸到遠遠超出描邊路徑的線條粗細的範圍。“miterLimit”對斜接長度與“lineWidth”的比率施加了限制。此參數的預設值為 4。

DrawablePath

const std::list<Magick::VPath> &path_

使用向量路徑在影像上繪製。

DrawablePoint

double x_, double y_

使用筆畫顏色和粗細在坐標處繪製一個點

DrawablePointSize

double pointSize_

設定字體點大小。

DrawablePolygon

const std::list<Magick::Coordinate> &coordinates_

使用筆畫顏色和粗細繪製一個任意多邊形,該多邊形由包含在 STL 清單中的三個或更多個坐標組成。如果指定了填充顏色,則對象將被填充。

DrawablePolyline

const std::list<Magick::Coordinate> &coordinates_

使用筆畫顏色和粗細繪製一個任意折線,該折線由包含在 STL 清單中的三個或更多個坐標組成。如果指定了填充顏色,則對象將被填充。

DrawablePopClipPath

void

彈出(終止)由 DrawablePushClipPath 開始的剪裁路徑定義。

DrawablePopGraphicContext

void

彈出圖形上下文。從圖形上下文堆疊中移除當前圖形上下文會將選項恢復為先前 DrawablePushGraphicContext 操作之前的狀態。

DrawablePushClipPath

const std::string &id_

使用 id_ 推入(建立)剪裁路徑定義。剪裁區定義由後續繪圖指令組成,並以 DrawablePopClipPath 終止。

DrawablePushGraphicContext

void

推入圖形上下文。推入圖形上下文時,在推入上下文之後設定的選項(例如坐標變換、顏色設定等)會儲存到新的圖形上下文。這允許將相關選項儲存在圖形上下文“堆疊”上,以便支援選項的階層式巢狀。當 DrawablePopGraphicContext 用於彈出當前圖形上下文時,最後一次DrawablePushGraphicContext操作期間生效的選項將被恢復。

DrawablePushPattern

std::string &id_, ssize_t x_, ssize_t y_, size_t width_, size_t height_

使用由 id_ 指定的任意圖案名稱、由 x_y_ 指定的圖案偏移量以及由 width_height_ 指定的圖案大小來開始圖案定義。圖案是在由指定的偏移量和大小定義的坐標系內定義的。可以在 DrawableCompositeImage)之間指定任意繪圖物件(包括 DrawablePushPattern DrawablePopPattern 以繪製圖案。通常使用這一對 DrawablePushGraphicContext & DrawablePopGraphicContext 來括住圖案定義。圖案定義由 DrawablePopPattern 物件終止。

DrawablePopPattern

void

終止透過 DrawablePushPattern.

DrawableRectangle

double upperLeftX_, double upperLeftY_, double lowerRightX_, double lowerRightY

使用從左上角坐標到右下角坐標的 stroke 顏色和粗細繪製矩形。如果指定了 fill 顏色,則會填充物件。

DrawableRotation

double angle_

設定繪製時要使用的旋轉角度(坐標轉換)。

DrawableRoundRectangle

double centerX_, double centerY_, double width_, double hight_, double cornerWidth_, double cornerHeight_

使用 stroke 顏色和粗細繪製圓角矩形,並指定中心坐標、寬度和高度,以及圓角的寬度和高度。如果指定了 fill 顏色,則會填充物件。

DrawableScaling

double x_, double y_

在繪製物件時套用 x 和 y 方向的縮放(坐標轉換)。

DrawableSkewX

double angle_

套用 X 方向的傾斜(坐標轉換)

DrawableSkewY

double angle_

套用 Y 方向的傾斜

DrawableStrokeAntialias

bool flag_

在繪製線條或物件輪廓時進行反鋸齒。

DrawableStrokeColor

const Color &color_

設定繪製線條或物件輪廓時要使用的顏色。

DrawableStrokeLineCap

LineCap linecap_

指定在描邊時用於開放子路徑末端的形狀。LineCap 的值為 UndefinedCap、ButtCap、RoundCap 和 SquareCap。

DrawableStrokeLineJoin

LineJoin linejoin_

指定在描邊時用於路徑(或其他向量形狀)拐角處的形狀。LineJoin 的值為 UndefinedJoin、MiterJoin、RoundJoin 和 BevelJoin。

DrawableStrokeOpacity

double opacity_

繪製線條或物件輪廓時要使用的透明度。

DrawableStrokeWidth

double width_

設定繪製線條或物件輪廓時要使用的寬度。

DrawableText

double x_, double y_, std::string text_

使用 stroke 顏色、字體、字體大小和 box 顏色(文字背景顏色)在指定的坐標處為圖像添加文字註釋。如果文字包含 特殊格式字元 ,則圖像檔名、類型、寬度、高度或其他圖像屬性可能會被併入文字中(請參閱 label())。

const double x_, const double y_, const std::string &text_, const std::string &encoding_

使用目前的 stroke 顏色、字體、字體大小和 box 顏色(文字背景顏色),在指定的坐標處使用文字編碼為圖像添加文字註釋。如果文字包含 特殊格式字元 ,則圖像檔名、類型、寬度、高度或其他圖像屬性可能會被併入文字中(請參閱 label())。

,則文字編碼指定用於文字註釋的代碼集。目前唯一可以在此處指定的字元編碼是 "UTF-8",用於將 Unicode 表示為位元組序列。指定一個空字串會將文字編碼設定為系統預設值。要成功使用 Unicode 進行文字註釋,可能需要使用支援 Unicode 的字體。

DrawableTextAntialias

bool flag_

在繪製文字時進行反鋸齒(預設為 true)。停用文字反鋸齒的主要原因是避免在圖像中添加新顏色。

DrawableTextDecoration

DecorationType decoration_

指定要套用到文字的裝飾(例如 UnderlineDecoration)。

DrawableTextUnderColor

const Color &color_

使用指定的顏色在渲染的文字下方繪製一個方框。

DrawableTranslation

double x_, double y_

套用坐標平移(設定新的坐標原點)。

DrawableViewbox

ssize_t x1_, ssize_t y1_, ssize_t x2_, ssize_t y2_

輸出視窗的維度。如果影像要寫入向量格式(例如 MVG 或 SVG),則應將 DrawablePushGraphicContext() 物件推送到清單的開頭,然後使用 DrawableViewbox() 語句來建立輸出畫布大小。應將相符的 DrawablePopGraphicContext() 物件推送到清單的尾部。

向量路徑類別

Magick++ 支援的向量路徑基於 SVG XML 規範 中支援的路徑。向量路徑無法直接繪製,必須先將其作為建構函數參數提供給 DrawablePath 類別,才能建立可繪製物件。DrawablePath 類別實際上建立了一個可繪製的複合元件,可以根據需要重播。如果可繪製的複合元件僅包含使用相對座標的向量路徑物件,則可以通過在其前面加上一個設定當前繪製座標的 DrawablePath 來將物件放置在影像上。或者,可以使用座標變換來 平移原點 以便定位物件、旋轉 物件、傾斜 物件或 縮放 物件。

「moveto」命令

「moveto」命令建立一個新的當前點。其效果就像「筆」被提起並移動到一個新的位置。路徑數據段必須以「moveto」命令或「arc」命令之一開始。後續的「moveto」命令(即,當「moveto」不是第一個命令時)表示一個新的子路徑的開始

Moveto 類別

子類別

建構函式簽章

說明

PathMovetoAbs

const Magick::Coordinate &coordinate_

在給定的座標處開始一個新的子路徑。PathMovetoAbs 表示接下來是絕對座標;PathMovetoRel 表示接下來是相對座標。如果相對 moveto 出現在路徑的第一個元素,則將其視為一對絕對座標。如果 moveto 後面跟著多對座標,則後續的座標對將被視為隱式的 lineto 命令。

const std::list<Magick::Coordinate> &coordinates_

PathMovetoRel

const Magick::Coordinate &coordinate_

const std::list<Magick::Coordinate> &coordinates_

「closepath」命令

「closepath」命令會導致從當前點到當前子路徑的初始點自動繪製一條直線

Closepath 類別

子類別

建構函式簽章

說明

PathClosePath

void

通過從當前點到當前子路徑的最近起始點(通常是最近的 moveto 點)繪製一條直線來關閉當前子路徑。

「lineto」命令

各種「lineto」命令從當前點到新點繪製直線

Lineto 類別

子類別

建構函式簽章

說明

PathLinetoAbs

const Magick::Coordinate& coordinate_

從當前點到給定座標繪製一條直線,該座標將成為新的當前點。PathLinetoAbs 表示使用絕對座標;PathLinetoRel 表示使用相對座標。可以在列表中指定多個座標對以繪製折線。在命令結束時,新的當前點將設置為提供的最後一組座標。

const std::list<Magick::Coordinate> &coordinates_

PathLinetoRel

const Magick::Coordinate& coordinate_

const std::list<Magick::Coordinate> &coordinates_

PathLinetoHorizontalAbs

double x_

從當前點 (cpx, cpy) 到 (x, cpy) 繪製一條水平線。PathLinetoHorizontalAbs 表示提供絕對座標;PathLinetoHorizontalRel 表示提供相對座標。在命令結束時,新的當前點將變為 (x, cpy),其中 x 為最終值。

PathLinetoHorizontalRel

double x_

PathLinetoVerticalAbs

double y_

從當前點 (cpx, cpy) 到 (cpx, y) 繪製一條垂直線。PathLinetoVerticalAbs 表示提供絕對座標;PathLinetoVerticalRel 表示提供相對座標。在命令結束時,新的當前點將變為 (cpx, y),其中 y 為最終值。

PathLinetoVerticalRel

double y_

曲線命令

這三組命令用於繪製曲線

三次貝茲曲線指令

三次貝茲曲線指令依賴於 PathCurvetoArgs 參數類別,該類別具有以下建構函數簽章

  PathCurvetoArgs( double x1_, double y1_, 
                   double x2_, double y2_, 
                   double x_, double y_ );

指令如下

三次貝茲曲線類別

子類別

建構函式簽章

說明

PathCurvetoAbs

const Magick::PathCurvetoArgs &args_

從當前點到 (x,y) 繪製一條三次貝茲曲線,使用 (x1,y1) 作為曲線起點的控制點,(x2,y2) 作為曲線終點的控制點。PathCurvetoAbs 表示接下來是絕對坐標;PathCurvetoRel 表示接下來是相對坐標。可以指定多組坐標來繪製多重貝茲曲線。指令結束時,新的當前點將成為多重貝茲曲線中使用的最後一個 (x,y) 坐標對。

const std::list<Magick::PathCurvetoArgs> &args_

PathCurvetoRel

const Magick::PathCurvetoArgs &args_

const std::list<Magick::PathCurvetoArgs> &args_

PathSmoothCurvetoAbs

const Magick::Coordinate &coordinates_

從當前點到 (x,y) 繪製一條三次貝茲曲線。第一個控制點假設為前一個指令的第二個控制點相對於當前點的反射點。(如果沒有前一個指令,或者前一個指令不是 PathCurvetoAbsPathCurvetoRelPathSmoothCurvetoAbsPathSmoothCurvetoRel,則假設第一個控制點與當前點重合。)(x2,y2) 是第二個控制點(即曲線終點的控制點)。PathSmoothCurvetoAbs 表示接下來是絕對坐標;PathSmoothCurvetoRel 表示接下來是相對坐標。可以指定多組坐標來繪製多重貝茲曲線。指令結束時,新的當前點將成為多重貝茲曲線中使用的最後一個 (x,y) 坐標對。

const std::list<Magick::Coordinate> &coordinates_

PathSmoothCurvetoRel

const Magick::Coordinate &coordinates_

const std::list<Magick::Coordinate> &coordinates_

二次貝茲曲線指令

二次貝茲曲線指令依賴於 PathQuadraticCurvetoArgs 參數類別,該類別具有以下建構函數簽章

  PathQuadraticCurvetoArgs( double x1_, double y1_, 
                            double x_, double y_ );

二次貝茲曲線指令如下

二次貝茲曲線類別

子類別

建構函式簽章

說明

PathQuadraticCurvetoAbs

const Magick::PathQuadraticCurvetoArgs &args_

從當前點到 (x,y) 繪製一條二次貝茲曲線,使用 (x1,y1) 作為控制點。PathQuadraticCurvetoAbs 表示接下來是絕對坐標;PathQuadraticCurvetoRel 表示接下來是相對坐標。可以指定多組坐標來繪製多重貝茲曲線。指令結束時,新的當前點將成為多重貝茲曲線中使用的最後一個 (x,y) 坐標對。

const std::list<Magick::PathQuadraticCurvetoArgs> &args_

PathQuadraticCurvetoRel

const Magick::PathQuadraticCurvetoArgs &args_

const std::list<Magick::PathQuadraticCurvetoArgs> &args_

PathSmoothQuadraticCurvetoAbs

const Magick::Coordinate &coordinate_

從當前點到 (x,y) 繪製一條二次貝茲曲線。控制點假設為前一個指令的控制點相對於當前點的反射點。(如果沒有前一個指令,或者前一個指令不是 PathQuadraticCurvetoAbsPathQuadraticCurvetoRelPathSmoothQuadraticCurvetoAbsPathSmoothQuadraticCurvetoRel,則假設控制點與當前點重合。)PathSmoothQuadraticCurvetoAbs 表示接下來是絕對坐標;PathSmoothQuadraticCurvetoRel 表示接下來是相對坐標。指令結束時,新的當前點將成為多重貝茲曲線中使用的最後一個 (x,y) 坐標對。
command relative to the current point. (If there is no previous command or if the previous command was not a PathQuadraticCurvetoAbs, PathQuadraticCurvetoRel, PathSmoothQuadraticCurvetoAbs 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<Magick::Coordinate> &coordinates_

PathSmoothQuadraticCurvetoRel

const Magick::Coordinate &coordinate_

const std::list<Magick::Coordinate> &coordinates_

橢圓弧線指令

橢圓弧線指令依賴於 PathArcArgs 參數類別,該類別具有以下建構函數簽章

   PathArcArgs( double radiusX_, double radiusY_, 
                double xAxisRotation_, bool largeArcFlag_, 
                bool sweepFlag_, double x_, double y_ );

橢圓弧線指令如下

橢圓弧線類別

子類別

建構函式簽章

說明

PathArcAbs

const Magick::PathArcArgs &coordinates_

從當前點繪製一個橢圓弧到 (x, y)。橢圓的大小和方向由兩個半徑 (radiusX, radiusY) 和一個 xAxisRotation 定義,它表示整個橢圓相對於當前坐標系的旋轉角度。橢圓的中心 (cx, cy) 會自動計算,以滿足其他參數的約束。largeArcFlagsweepFlag 有助於自動計算,並幫助確定如何繪製弧線。如果 largeArcFlag 為 true,則繪製較大的可用弧線。如果 sweepFlag 為 true,則繪製與順時針旋轉方向匹配的弧線。

const std::list<Magick::PathArcArgs> &coordinates_

PathArcRel

const Magick::PathArcArgs &coordinates_

const std::list<Magick::PathArcArgs> &coordinates_