ultimatepp/examples/TextToSvgPath/ToSvg.cpp
cxl d400a792fc .examples
git-svn-id: svn://ultimatepp.org/upp/trunk@14205 f0d560ea-af0d-0410-9eb7-867de7ffcac7
2020-03-31 15:04:59 +00:00

63 lines
1.1 KiB
C++

#include "TextToSvgPath.h"
struct TextToSvg : FontGlyphConsumer {
String t; // here we accumulate the SVG path text
void Put(Pointf p);
virtual void Move(Pointf p);
virtual void Line(Pointf p);
virtual void Quadratic(Pointf p1, Pointf p2);
virtual void Cubic(Pointf p1, Pointf p2, Pointf p3);
virtual void Close();
};
void TextToSvg::Put(Pointf p)
{
t << Format("%.2f %.2f ", p.x, p.y);
}
void TextToSvg::Move(Pointf p)
{
t << 'M';
Put(p);
}
void TextToSvg::Line(Pointf p)
{
t << 'L';
Put(p);
}
void TextToSvg::Quadratic(Pointf p1, Pointf p)
{
t << 'Q';
Put(p1);
Put(p);
}
void TextToSvg::Cubic(Pointf p1, Pointf p2, Pointf p)
{
t << 'C';
Put(p1);
Put(p2);
Put(p);
}
void TextToSvg::Close()
{
t << 'Z';
}
String TextToSvgPath(double x, double y, const char *text, Font fnt, bool singleline)
{
WString ws = ToUnicode(text, CHARSET_DEFAULT);
TextToSvg t;
for(const wchar *s = ~ws; *s; s++) {
fnt.Render(t, x, y, *s);
x += fnt[*s]; // move the reference point
if(!singleline)
t.t << "\n";
}
return t.t;
}