スポンサーリンク

pugixmlでsvg出力

SVGはXMLで書かれる画像フォーマットなので、pugixmlで出力できる。

以前pugixmlを導入したので、それを使う。

#include "pugixml.hpp"


int main()
{
    pugi::xml_document doc;

    pugi::xml_node mysvg = doc.append_child("svg");
    mysvg.append_attribute("xmlns") = "http://www.w3.org/2000/svg";
    mysvg.append_attribute("width") = "200";
    mysvg.append_attribute("height") = "200";

    // 背景を塗りつぶすための矩形
    pugi::xml_node bgrect = mysvg.append_child("rect");
    bgrect.append_attribute("x") = "0";
    bgrect.append_attribute("y") = "0";
    bgrect.append_attribute("width") = "200";
    bgrect.append_attribute("height") = "200";
    bgrect.append_attribute("fill") = "rgb(200,200,200)";

    // 円
    pugi::xml_node circle = mysvg.append_child("circle");
    circle.append_attribute("cx") = "50";
    circle.append_attribute("cy") = "50";
    circle.append_attribute("r") = "40";
    circle.append_attribute("fill") = "red";

    // 線
    pugi::xml_node line = mysvg.append_child("line");
    line.append_attribute("x1") = "10";
    line.append_attribute("y1") = "10";
    line.append_attribute("x2") = "150";
    line.append_attribute("y2") = "80";
    line.append_attribute("stroke") = "black";
    line.append_attribute("stroke-width") = "4";

    // 矩形
    pugi::xml_node rect = mysvg.append_child("rect");
    rect.append_attribute("x") = "50";
    rect.append_attribute("y") = "50";
    rect.append_attribute("width") = "80";
    rect.append_attribute("height") = "80";
    rect.append_attribute("fill") = "blue";
    rect.append_attribute("stroke") = "green";
    rect.append_attribute("stroke-width") = "6";

    // ファイルに保存
    doc.save_file("test.svg");

}

画像に変換

imagemagickで画像に変換できる

magick test.svg test.png

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)


この記事のトラックバックURL: