re-tourched logo design for better quality.
[platform/core/graphics/tizenvg.git] / src / examples / Svg2.cpp
1 #include "Common.h"
2
3 /************************************************************************/
4 /* Drawing Commands                                                     */
5 /************************************************************************/
6
7 static const char* svg = "<svg height=\"1000\" viewBox=\"0 0 1000 1000\" width=\"1000\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M1.171875.25H1004.687475V1005.6094H1.171875Z\" fill=\"#09bbf1\" stroke-width=\"3.910218\"/><g fill=\"#252f35\"><g stroke-width=\"3.864492\"><path d=\"M256.61221 100.51736H752.8963V386.99554H256.61221Z\"/><path d=\"M201.875 100.51736H238.366478V386.99554H201.875Z\"/><path d=\"M771.14203 100.51736H807.633508V386.99554H771.14203Z\"/></g><path d=\"M420.82388 380H588.68467V422.805317H420.82388Z\" stroke-width=\"3.227\"/><path d=\"m420.82403 440.7101v63.94623l167.86079 25.5782V440.7101Z\"/><path d=\"M420.82403 523.07258V673.47362L588.68482 612.59701V548.13942Z\"/></g><g fill=\"#222f35\"><path d=\"M420.82403 691.37851 588.68482 630.5019 589 834H421Z\"/><path d=\"m420.82403 852.52249h167.86079v28.64782H420.82403v-28.64782 0 0\"/><path d=\"m439.06977 879.17031c0 0-14.90282 8.49429-18.24574 15.8161-4.3792 9.59153 0 31.63185 0 31.63185h167.86079c0 0 4.3792-22.04032 0-31.63185-3.34292-7.32181-18.24574-15.8161-18.24574-15.8161z\"/></g><path d=\"m280 140h15v55l8 10 8-10v-55h15v60l-23 25-23-25z\" fill=\"#09bbf1\"/><path d=\"m335 140v80h45v-50h-25v10h10v30h-15v-57h18v-13z\" fill=\"#09bbf1\"/></svg>";
8
9
10 void tvgDrawCmds(tvg::Canvas* canvas)
11 {
12     if (!canvas) return;
13
14     //Background
15     auto shape = tvg::Shape::gen();
16     shape->appendRect(0, 0, WIDTH, HEIGHT, 0, 0);    //x, y, w, h, rx, ry
17     shape->fill(255, 255, 255, 255);                 //r, g, b, a
18
19     if (canvas->push(move(shape)) != tvg::Result::Success) return;
20
21     auto picture = tvg::Picture::gen();
22     if (picture->load(svg, strlen(svg)) != tvg::Result::Success) return;
23
24     float x, y, w, h;
25     picture->viewbox(&x, &y, &w, &h);
26
27     float rate = (WIDTH/(w > h ? w : h));
28     picture->scale(rate);
29
30     x *= rate;
31     y *= rate;
32     w *= rate;
33     h *= rate;
34
35     //Center Align ?
36     if (w > h) {
37          y -= (WIDTH - h) * 0.5f;
38     } else {
39          x -= (WIDTH - w) * 0.5f;
40     }
41
42     picture->translate(-x, -y);
43
44     canvas->push(move(picture));
45 }
46
47
48 /************************************************************************/
49 /* Sw Engine Test Code                                                  */
50 /************************************************************************/
51
52 static unique_ptr<tvg::SwCanvas> swCanvas;
53
54 void tvgSwTest(uint32_t* buffer)
55 {
56     //Create a Canvas
57     swCanvas = tvg::SwCanvas::gen();
58     swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
59
60     /* Push the shape into the Canvas drawing list
61        When this shape is into the canvas list, the shape could update & prepare
62        internal data asynchronously for coming rendering.
63        Canvas keeps this shape node unless user call canvas->clear() */
64     tvgDrawCmds(swCanvas.get());
65 }
66
67 void drawSwView(void* data, Eo* obj)
68 {
69     if (swCanvas->draw() == tvg::Result::Success) {
70         swCanvas->sync();
71     }
72 }
73
74
75 /************************************************************************/
76 /* GL Engine Test Code                                                  */
77 /************************************************************************/
78
79 static unique_ptr<tvg::GlCanvas> glCanvas;
80
81 void initGLview(Evas_Object *obj)
82 {
83     static constexpr auto BPP = 4;
84
85     //Create a Canvas
86     glCanvas = tvg::GlCanvas::gen();
87     glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
88
89     /* Push the shape into the Canvas drawing list
90        When this shape is into the canvas list, the shape could update & prepare
91        internal data asynchronously for coming rendering.
92        Canvas keeps this shape node unless user call canvas->clear() */
93     tvgDrawCmds(glCanvas.get());
94 }
95
96 void drawGLview(Evas_Object *obj)
97 {
98     auto gl = elm_glview_gl_api_get(obj);
99     gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
100     gl->glClear(GL_COLOR_BUFFER_BIT);
101
102     if (glCanvas->draw() == tvg::Result::Success) {
103         glCanvas->sync();
104     }
105 }
106
107
108 /************************************************************************/
109 /* Main Code                                                            */
110 /************************************************************************/
111
112 int main(int argc, char **argv)
113 {
114     tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
115
116     if (argc > 1) {
117         if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
118     }
119
120     //Initialize ThorVG Engine
121     if (tvgEngine == tvg::CanvasEngine::Sw) {
122         cout << "tvg engine: software" << endl;
123     } else {
124         cout << "tvg engine: opengl" << endl;
125     }
126
127     //Threads Count
128     auto threads = std::thread::hardware_concurrency();
129
130     //Initialize ThorVG Engine
131     if (tvg::Initializer::init(tvgEngine, threads) == tvg::Result::Success) {
132
133         elm_init(argc, argv);
134
135         if (tvgEngine == tvg::CanvasEngine::Sw) {
136             createSwView();
137         } else {
138             createGlView();
139         }
140
141         elm_run();
142         elm_shutdown();
143
144         //Terminate ThorVG Engine
145         tvg::Initializer::term(tvg::CanvasEngine::Sw);
146
147     } else {
148         cout << "engine is not supported" << endl;
149     }
150     return 0;
151 }