Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site













SourceForge.net Logo

TextToSvgPath

 

GUI application that converts text to SVG path

 

 

TextToSvgPath.h

 

#ifndef _TextToSvgPath_TextToSvgPath_h

#define _TextToSvgPath_TextToSvgPath_h

 

#include <CtrlLib/CtrlLib.h>

#include <RichEdit/RichEdit.h>

 

using namespace Upp;

 

#define LAYOUTFILE <TextToSvgPath/TextToSvgPath.lay>

#include <CtrlCore/lay.h>

 

String TextToSvgPath(double x, double y, const char *text, Font fnt, bool singleline);

 

struct Preview : Ctrl {

    String svgpath;

    

    virtual void Paint(Draw& w);

};

 

struct TextToSvgPathDlg : public WithTextToSvgPathLayout<TopWindow> {

    Preview preview;

 

    void Render();

    

    TextToSvgPathDlg();

};

 

#endif

 

 

 

main.cpp

 

#include "TextToSvgPath.h"

 

void Preview::Paint(Draw& w)

{

    DrawPainter sw(w, GetSize());

    sw.Clear(SWhite());

    sw.Path(svgpath).Fill(SBlack());

}

 

TextToSvgPathDlg::TextToSvgPathDlg()

{

    CtrlLayout(*this, "Text to SVG path converter");

    for(Ctrl *q = GetFirstChild(); q; q = q->GetNext())

        if(!dynamic_cast<Button *>(q))

            q->WhenAction = [=] { Render(); };

    

    face.Add(Font::ARIAL);

    face.Add(Font::ROMAN);

    face.Add(Font::COURIER);

    for(int i = Font::COURIER + 1; i < Font::GetFaceCount(); i++)

        if(Font::GetFaceInfo(i) & Font::SCALEABLE)

            face.Add(i);

 

    struct DisplayFace : public Display {

        void Paint(Draw& w, const Rect& r, const Value& q, Color ink, Color paper, dword style) const {

            int ii = q;

            Font fnt = StdFont();

            if(!(Font::GetFaceInfo(ii) & Font::SPECIAL))

                fnt.Face(ii);

            w.DrawRect(r, paper);

            w.DrawText(r.left, r.top + (r.Height() - fnt.Info().GetHeight()) / 2,

                       Font::GetFaceName((int)q), fnt, ink);

        }

    };

    face.SetDisplay(Single<DisplayFace>());

    

    face <<= Font::ARIAL;

    height.MinMax(6, 500);

    

    for(int i = 4; i < 500; i += i < 16 ? 1 : i < 32 ? 4 : i < 48 ? 8 : 16)

        height.AddList(i);

    height <<= 128;

    

    svgpath.SetReadOnly();

    preview.SetFrame(ViewFrame());

    

    copy.SetImage(CtrlImg::copy());

    copy << [=] {

        WriteClipboardText(preview.svgpath);

    };

}

 

void TextToSvgPathDlg::Render()

{

    Font fnt(~face, ~height);

    fnt.Bold(~bold);

    fnt.Italic(~italic);

 

    svgpath <<= preview.svgpath = TextToSvgPath(0, 0, (String)~text, fnt, ~singleline);

    preview.Refresh();

}

 

GUI_APP_MAIN

{

    TextToSvgPathDlg().Run();

}

 

 

 

ToSvg.cpp

 

#include "TextToSvgPath.h"

 

struct TextToSvg : FontGlyphConsumer {

    String t;

    

    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];

        if(!singleline)

            t.t << "\n";

    }

    return t.t;

}

 

 

 

TextToSvgPath.lay

 

LAYOUT(TextToSvgPathLayout, 704, 588)

    ITEM(Label, dv___0, SetLabel(t_("Text")).LeftPosZ(8, 33).TopPosZ(28, 19))

    ITEM(EditString, text, LeftPosZ(44, 652).TopPosZ(28, 19))

    ITEM(Label, dv___2, SetLabel(t_("Font")).LeftPosZ(8, 33).TopPosZ(4, 19))

    ITEM(DropList, face, LeftPosZ(44, 188).TopPosZ(4, 19))

    ITEM(Label, dv___4, SetLabel(t_("Height")).LeftPosZ(252, 39).TopPosZ(4, 19))

    ITEM(WithDropChoice<EditInt>, height, LeftPosZ(292, 56).TopPosZ(4, 19))

    ITEM(Option, bold, SetLabel(t_("Bold")).LeftPosZ(356, 52).TopPosZ(4, 18))

    ITEM(Option, italic, SetLabel(t_("Italic")).LeftPosZ(408, 52).TopPosZ(4, 18))

    ITEM(LineEdit, svgpath, LeftPosZ(8, 688).TopPosZ(52, 268))

    UNTYPED(preview, LeftPosZ(8, 688).TopPosZ(324, 260))

    ITEM(Button, copy, SetLabel(t_("Copy path to clipboard")).LeftPosZ(568, 128).TopPosZ(4, 20))

    ITEM(Option, singleline, SetLabel(t_("Single line")).LeftPosZ(484, 80).TopPosZ(4, 20))

END_LAYOUT

 

 

 

 

 

Do you want to contribute?