LoginSignup
0
0

More than 1 year has passed since last update.

MFC から SVG クラスのテキスト呼び出しコードサンプル

Last updated at Posted at 2022-12-20

MFC から SVG クラスのテキスト呼び出しコードサンプル

MFC(GDI+) の座標系がグラフ座標系の場合、文字を描くと上下反転した状態で表示されます。
一時的に描画領域を上下反転してテキストを描いて元に戻します。

MFC(GDI+) の場合以下のようになります。

	Matrix matrix;
	pGraphics->GetTransform(&matrix);
	pGraphics->ScaleTransform(1, -1);
	FontFamily  fontFamily(L"Times New Roman");
	Gdiplus::Font  font(&fontFamily, 24, FontStyleRegular, UnitPixel);
	PointF      pointF(30.0f, 10.0f);
	SolidBrush  solidBrush(Color(255, 0, 0, 255));

	pGraphics->DrawString(L"Hello", -1, &font, pointF, &solidBrush);

	pGraphics->SetTransform(&matrix);

SVG 出力ではテキストを上下反転させる必要はありません。
位置調整のため、MFC で縦幅だけ取得します。
これでも多少ずれると思います。
計算式を編集してある程度調整することもできますが、フォントにより差分が発生してしまいますので完全に対応するのは難しいです。

SvgImage に書き出すと以下のようになります。

	FontFamily  fontFamily(L"Times New Roman");
	Gdiplus::Font  font(&fontFamily, 24, FontStyleRegular, UnitPixel);
	PointF      pointF(30.0f, 10.0f);
    Gdiplus::RectF boundingBox(0, 0, 0, 0);
	pGraphSrc->MeasureString(L"Hello", -1, &font, pointF, &boundingBox);
    CPoint pn = CPoint(30.0f, 10.0f + boundingBox.Width / 2);
    COLORREF crColor = Color(255, 0, 0, 255);
	pSvgImage->AddText(pn, L"Hello", 24, L"Times New Roman", crColor, 0, 0);  

テキストがずれないMFC から SVG クラス呼び出しコードサンプル

MFC と SVG で出力される場所がずれない方法がパスによるテキスト出力になります。
パスにベクタ画像として文字列の書き出しを行うことで各頂点の座標を固定します。
この方法であれば SVG が対応していないフォントの描画も可能になります。
ただし、フォントの利用規約で「ベクタ形式で出力したデータの配布を禁止」されていることもあります。
安易に使える方法ではありませんが SVG でテキストを表示する際の利便性が高いためサンプルコードを紹介します。

先述の通り、テキストが上下反転するため Gdiplus::GraphicsPath オブジェクトの座標系を書き込み先の座標系に合わせるため Gdiplus::Matrix で変換します。
座標系はグラフ座標系で統一されますので MFC と SVG でパスを作成するところまで同じコードになります

MFC(GDI+) の場合以下のようになります。

    Gdiplus::Matrix matrix;
    matrix.Scale(1.0, -1.0);
	FontFamily  fontFamily(L"Times New Roman");
	Gdiplus::Font  font(&fontFamily, 24, FontStyleRegular, UnitPixel);
	PointF      pointF(30.0f, 10.0f);
	SolidBrush  solidBrush(Color(255, 0, 0, 255));
    Gdiplus::GraphicsPath path;
    path.AddString(L"Hello", -1, &fFontFamily, 0, font.GetSize(),
                   pointF, Gdiplus::StringFormat::GenericDefault());

    path.Transform(&matrix);
	SolidBrush solidBrush(Color(255, 0, 0, 255));
	pGraphics->FillPath(&solidBrush, &path);

SvgImage に書き出すと以下のようになります。

    Gdiplus::Matrix matrix;
    matrix.Scale(1.0, -1.0);
	FontFamily  fontFamily(L"Times New Roman");
	Gdiplus::Font  font(&fontFamily, 24, FontStyleRegular, UnitPixel);
	PointF      pointF(30.0f, 10.0f);
	SolidBrush  solidBrush(Color(255, 0, 0, 255));
    Gdiplus::GraphicsPath path;
    path.AddString(L"Hello", -1, &fFontFamily, 0, font.GetSize(),
                   pointF, Gdiplus::StringFormat::GenericDefault());

    path.Transform(&matrix);
    Gdiplus::PathData pathData;
    path.GetPathData(&pathData);
    pImage->AddPathData(&pathData);
    pImage->AddAttrFill(RGB(0, 0, 255), FALSE);
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0