-rw-r--r-- | agg-plot/agg-parse-trans.cpp | 13 | ||||
-rw-r--r-- | agg-plot/lua-text.cpp | 22 | ||||
-rw-r--r-- | agg-plot/plot.h | 28 | ||||
-rw-r--r-- | agg-plot/text.h | 23 | ||||
-rw-r--r-- | draw.lua | 24 | ||||
-rw-r--r-- | examples/anim.lua | 12 | ||||
-rw-r--r-- | examples/fractals.lua | 17 | ||||
-rw-r--r-- | examples/graphics.lua | 7 | ||||
-rw-r--r-- | examples/text.lua | 2 |
diff --git a/agg-plot/agg-parse-trans.cpp b/agg-plot/agg-parse-trans.cpp index 4ec14748..368405fd 100644 --- a/agg-plot/agg-parse-trans.cpp +++ b/agg-plot/agg-parse-trans.cpp @@ -123,14 +123,11 @@ build_dash (lua_State *L, int specindex, typename context::base_type *obj) lua_pop (L, 1); lua_rawgeti (L, specindex, j+1); - if (lua_isnumber (L, -1)) - { - double b = lua_tonumber (L, -1); - dash.add_dash(a, b); - lua_pop (L,1); - } - else - break; + + double b = (lua_isnumber (L, -1) ? lua_tonumber (L, -1) : a); + + dash.add_dash(a, b); + lua_pop (L,1); } else break; diff --git a/agg-plot/lua-text.cpp b/agg-plot/lua-text.cpp index 91e8f45b..e1c770b0 100644 --- a/agg-plot/lua-text.cpp +++ b/agg-plot/lua-text.cpp @@ -21,6 +21,8 @@ static int agg_text_text_set (lua_State *L); static int agg_text_angle_set (lua_State *L); static int agg_text_justif_set (lua_State *L); +static int agg_text_text_get (lua_State *L); +static int agg_text_angle_get (lua_State *L); static draw::text* check_agg_text (lua_State *L, int index); @@ -34,17 +36,19 @@ static const struct luaL_Reg text_methods[] = { {"__index", agg_text_index}, {"__newindex", agg_text_newindex}, {"set", agg_text_set_point}, + {"justif", agg_text_justif_set }, {NULL, NULL} }; static const struct luaL_Reg text_properties_get[] = { + {"text", agg_text_text_get }, + {"angle", agg_text_angle_get }, {NULL, NULL} }; static const struct luaL_Reg text_properties_set[] = { {"text", agg_text_text_set }, {"angle", agg_text_angle_set }, - {"justif", agg_text_justif_set }, {NULL, NULL} }; @@ -91,6 +95,22 @@ agg_text_angle_set (lua_State *L) } int +agg_text_angle_get (lua_State *L) +{ + draw::text *t = check_agg_text (L, 1); + lua_pushnumber (L, t->angle()); + return 1; +} + +int +agg_text_text_get (lua_State *L) +{ + draw::text *t = check_agg_text (L, 1); + lua_pushstring (L, t->get_text()); + return 1; +} + +int agg_text_justif_set (lua_State *L) { draw::text *t = check_agg_text (L, 1); diff --git a/agg-plot/plot.h b/agg-plot/plot.h index 9cbf7dff..cc717232 100644 --- a/agg-plot/plot.h +++ b/agg-plot/plot.h @@ -31,6 +31,7 @@ #include "units.h" #include "resource-manager.h" +#include "agg_array.h" #include "agg_vcgen_markers_term.h" #include "agg_conv_transform.h" #include "agg_color_rgba.h" @@ -78,10 +79,13 @@ class plot { }; public: - plot() : m_elements(), m_trans(), m_bbox_updated(true), m_use_units(true) { - m_title_size = 32; - m_title = new char[m_title_size]; - m_title[0] = 0; + plot() : + m_elements(), m_trans(), m_bbox_updated(true), + m_title_buf(), m_use_units(true) + { + m_title_buf.capacity(32); + m_title = m_title_buf.data(); + m_title[0] = '0円'; }; ~plot() @@ -91,21 +95,13 @@ public: container& d = m_elements[j]; resource_manager::dispose(d.vs); } - - delete [] m_title; }; void set_title(const char *text) { unsigned int len = strlen(text); - - if (m_title_size < len + 1) - { - delete [] m_title; - m_title = new char[len+1]; - m_title_size = len+1; - } - - memcpy(m_title, text, len+1); + m_title_buf.resize(len+1); + m_title = m_title_buf.data(); + memcpy (m_title, text, len+1); }; const char *get_title() const { return m_title; }; @@ -149,8 +145,8 @@ private: double m_x1, m_y1; double m_x2, m_y2; + agg::pod_vector<char> m_title_buf; char *m_title; - unsigned int m_title_size; bool m_use_units; units m_ux, m_uy; diff --git a/agg-plot/text.h b/agg-plot/text.h index 8ef4ed34..ac594238 100644 --- a/agg-plot/text.h +++ b/agg-plot/text.h @@ -21,6 +21,8 @@ namespace draw { vs_trans_text m_trans; vs_stroked_text m_stroke; + agg::pod_vector<char> m_text_buf; + double m_x, m_y; double m_angle; @@ -33,9 +35,13 @@ namespace draw { public: text(double size = 10.0, double width = 1.0): m_matrix(), m_text(), m_trans(m_text, m_matrix), m_stroke(m_trans), - m_x(0.0), m_y(0.0), m_text_width(0.0), m_text_height(size), + m_text_buf(), m_x(0.0), m_y(0.0), m_angle(0.0), + m_text_width(0.0), m_text_height(size), m_hjustif(0.0), m_vjustif(0.0) { + m_text_buf.capacity(32); + m_text_buf[0] = '0円'; + m_stroke.width(width + 0.5); m_stroke.line_cap(agg::round_cap); m_stroke.line_join(agg::round_join); @@ -45,6 +51,8 @@ namespace draw { void angle(double th) { double c = cos(th), s = sin(th); + + m_angle = th; m_matrix.sx = c; m_matrix.shx = -s; @@ -52,12 +60,23 @@ namespace draw { m_matrix.sy = c; }; + double angle() const { return m_angle; }; + void set_text(const char *txt) - { + { + size_t len = strlen (txt); + m_text_buf.capacity(len+1); + memcpy (m_text_buf.data(), txt, len+1); + m_text.text(txt); m_text_width = m_text.text_width(); }; + const char * get_text() const + { + return m_text_buf.data(); + }; + void set_point(double x, double y) { m_x = x; @@ -57,11 +57,11 @@ function fiplot(f, a, b, color) return p end -local function add_bar(p, lx, rx, y) - p:move_to(lx, 0) - p:line_to(rx, 0) - p:line_to(rx, y) - p:line_to(lx, y) +local function add_square(p, lx, by, rx, ty) + p:move_to(lx, by) + p:line_to(rx, by) + p:line_to(rx, ty) + p:line_to(lx, ty) p:close() end @@ -71,8 +71,8 @@ function ibars(f) local first = true for rx, ry in f do local dx = (rx-lx)/2 - if first then add_bar(b, lx-dx, lx+dx, ly); first = false end - add_bar(b, lx+dx, rx+dx, ry) + if first then add_square(b, lx-dx, 0, lx+dx, ly); first = false end + add_square(b, lx+dx, 0, rx+dx, ry) lx, ly = rx, ry end return b @@ -84,6 +84,16 @@ function segment(x1, y1, x2, y2) return p end +function rect(x1, y1, x2, y2) + local p = path() + add_square(p, x1, y1, x2, y2) + return p +end + +function square(x0, y0, l) + return rect(x0-l/2, y0-l/2, x0+l/2, y0+l/2) +end + local bcolors = {'red', 'blue', 'green', 'magenta', 'cyan', 'yellow'} local mcolors = {'', 'dark', 'light'} diff --git a/examples/anim.lua b/examples/anim.lua index ff27e7db..23ce4abd 100644 --- a/examples/anim.lua +++ b/examples/anim.lua @@ -38,13 +38,10 @@ function demo3() local x1 = 25 local p = plot('rotating sine') - local box = path(-2*pi, -2*pi) - box:line_to(2*pi, -2*pi) - box:line_to(2*pi, 2*pi) - box:line_to(-2*pi, 2*pi) - box:close() + local e = ellipse(pi, 0, pi/2, pi/4) - p:addline(box, 'black') + p:addline(ellipse(0,0, 2*pi, 2*pi), 'black') +-- p:addline(square(0,0, 4*pi), 'black') p.units = false p:show() @@ -57,6 +54,9 @@ function demo3() p:stroke(ln, 'blue', {}, {{'rotate', angle= angle + pi/2}}) p:stroke(ln, 'green', {}, {{'rotate', angle= angle + pi}}) p:stroke(ln, 'yellow', {}, {{'rotate', angle= angle + 3*pi/2}}) + + p:stroke(e, 'red', {}, {{'rotate', angle= angle}}) + p:refresh() end end diff --git a/examples/fractals.lua b/examples/fractals.lua index 33a6aed7..daa960ea 100644 --- a/examples/fractals.lua +++ b/examples/fractals.lua @@ -1,6 +1,6 @@ require 'draw' -function c_generator(n, n_angle, len_frac, g) +local function c_generator(n, n_angle, len_frac, g) local w, r, k = ilist(|| 0, n+1), #g local s = len_frac^n @@ -26,7 +26,7 @@ function c_generator(n, n_angle, len_frac, g) end end -function vonkoch(n) +local function vonkoch(n) local p = plot('Von Koch\'s curve') local b = path() b:move_to (0, -0.05) @@ -36,7 +36,7 @@ function vonkoch(n) return p end -function levyc(n) +local function levyc(n) local p = plot('Levy\'s C curve') local c = ipath(c_generator(n, 4, 1/2, {-1,0,0,1})) p:addline(c, 'red', {}, {{'rotate', angle= -pi/4}}) @@ -45,6 +45,11 @@ function levyc(n) return p end -p1 = vonkoch(5) -p2 = levyc(7) -p2.units = false +demo1 = || vonkoch(5) +demo2 = function() + local p = levyc(7) + p.units = false + end + +print 'demo1() - Von Koch\'s curve' +print 'demo2() - Levy\'s C curve' diff --git a/examples/graphics.lua b/examples/graphics.lua index f813858f..e1f4ef44 100644 --- a/examples/graphics.lua +++ b/examples/graphics.lua @@ -50,7 +50,7 @@ function demo1() local a = path() local n = 12 local t = {{}, {{'curve'}}, {{'stroke'}, {'curve'}}, - {{'stroke'}, {'dash'}, {'curve'}}} + {{'stroke'}, {'dash', 6, 3}, {'curve'}}} local color = {'red', 'yellow', 'blue', 'darkgreen', 'cyan'} local p = plot() local R = 120 @@ -109,5 +109,6 @@ function demo3() return p end -p1 = demo1() -p2 = demo2() +print 'demo1() - path objects with bezier segments and various transformations' +print 'demo2() - example of text object utilisation' +print 'demo3() - example of animation with window and text object' diff --git a/examples/text.lua b/examples/text.lua index 19a630ce..06a98336 100644 --- a/examples/text.lua +++ b/examples/text.lua @@ -9,7 +9,7 @@ p:addline(ln, 'red', {{'dash', 7, 3}}) t = text(12) t.text = 'Hello world!' -t.justif = 'cc' +t:justif('cc') t:set(2*pi, 0) local N = 128 |