Update the symbol library, add trivial text API.

This commit is contained in:
Přemysl Eric Janouch 2011-02-11 23:21:04 +01:00
parent 299ce010bd
commit b7875c361f
5 changed files with 195 additions and 3 deletions

View File

@ -447,6 +447,7 @@ foreach(_GTK2_component ${GTK2_FIND_COMPONENTS})
_GTK2_FIND_INCLUDE_DIR(GTK2_PANGO_INCLUDE_DIR pango/pango.h)
_GTK2_FIND_LIBRARY (GTK2_PANGO_LIBRARY pango false true)
_GTK2_FIND_LIBRARY (GTK2_PANGOCAIRO_LIBRARY pangocairo false true)
_GTK2_FIND_INCLUDE_DIR(GTK2_ATK_INCLUDE_DIR atk/atk.h)
_GTK2_FIND_LIBRARY (GTK2_ATK_LIBRARY atk false true)

View File

@ -111,6 +111,7 @@ static int ld_lua_cairo_fill (lua_State *L);
static int ld_lua_cairo_fill_preserve (lua_State *L);
static int ld_lua_cairo_clip (lua_State *L);
static int ld_lua_cairo_clip_preserve (lua_State *L);
static int ld_lua_cairo_show_text (lua_State *L);
static luaL_Reg ld_lua_logdiag_lib[] =
@ -142,6 +143,7 @@ static luaL_Reg ld_lua_cairo_table[] =
{"fill_preserve", ld_lua_cairo_fill_preserve},
{"clip", ld_lua_cairo_clip},
{"clip_preserve", ld_lua_cairo_clip_preserve},
{"show_text", ld_lua_cairo_show_text},
{NULL, NULL}
};
@ -807,3 +809,34 @@ LD_LUA_CAIRO_BEGIN (arc_negative)
cairo_arc_negative (data->cr, xc, yc, radius, angle1, angle2);
LD_LUA_CAIRO_END (0)
LD_LUA_CAIRO_BEGIN (show_text)
const char *text;
GtkStyle *style;
PangoLayout *layout;
int width, height;
double x, y;
data = lua_touserdata (L, lua_upvalueindex (1));
text = luaL_checkstring (L, 1);
layout = pango_cairo_create_layout (data->cr);
pango_layout_set_text (layout, text, -1);
style = gtk_style_new ();
pango_font_description_set_size (style->font_desc, 1 * PANGO_SCALE);
pango_layout_set_font_description (layout, style->font_desc);
g_object_unref (style);
pango_layout_get_size (layout, &width, &height);
cairo_get_current_point (data->cr, &x, &y);
x -= (double) width / PANGO_SCALE / 2;
y -= (double) height / PANGO_SCALE / 2;
cairo_save (data->cr);
cairo_move_to (data->cr, x, y);
pango_cairo_show_layout (data->cr, layout);
cairo_restore (data->cr);
g_object_unref (layout);
LD_LUA_CAIRO_END (0)

View File

@ -1,8 +1,8 @@
-- Symbol name
local names =
{
en = "Power source",
cs = "Zdroj napětí"
en = "Cell",
cs = "Článek"
}
-- Render area in base units (X1, Y1, X2, Y2)
@ -31,6 +31,6 @@ local render = function (cr)
end
-- Register the symbol
logdiag.register ("PowerSource", names, area, terminals, render)
logdiag.register ("Cell", names, area, terminals, render)

View File

@ -0,0 +1,83 @@
-- Symbol names
local names_A =
{
en = "Ammeter",
cs = "Ampérmetr"
}
local names_mA =
{
en = "Milliammeter",
cs = "Miliampérmetr"
}
local names_V =
{
en = "Voltmeter",
cs = "Voltmetr"
}
local names_ohm =
{
en = "Ohmmeter",
cs = "Ohmmetr"
}
local names_W =
{
en = "Wattmeter",
cs = "Wattmetr"
}
-- Render area in base units (X1, Y1, X2, Y2)
local area = {-3, -2, 3, 2}
-- Terminal points
local terminals = {{-3, 0}, {3, 0}}
-- Rendering
local render = function (cr, name)
-- The circle
cr.arc (0, 0, 2, 0, math.pi * 2)
-- The contact
cr.move_to (-3, 0)
cr.line_to (-2, 0)
cr.move_to (2, 0)
cr.line_to (3, 0)
cr.stroke ()
cr.move_to (0, 0)
cr.show_text (name)
end
local render_A = function (cr)
render (cr, "A")
end
local render_mA = function (cr)
render (cr, "mA")
end
local render_V = function (cr)
render (cr, "V")
end
local render_ohm = function (cr)
render (cr, "")
end
local render_W = function (cr)
render (cr, "W")
end
-- Register the symbols
logdiag.register ("Ammeter", names_A, area, terminals, render_A)
logdiag.register ("Milliammeter", names_mA, area, terminals, render_mA)
logdiag.register ("Voltmeter", names_V, area, terminals, render_V)
logdiag.register ("Ohmmeter", names_ohm, area, terminals, render_ohm)
logdiag.register ("Wattmeter", names_W, area, terminals, render_W)

View File

@ -0,0 +1,75 @@
-- Symbol names
local names_ac =
{
en = "AC voltage source",
cs = "Střídavý zdroj napětí"
}
local names_dc =
{
en = "DC voltage source",
cs = "Stejnosměrný zdroj napětí"
}
-- Render area in base units (X1, Y1, X2, Y2)
local area_ac = {-3, -0.5, 3, 0.5}
local area_dc = {-3, -1.25, 3, 0.5}
-- Terminal points
local terminals = {{-3, 0}, {3, 0}}
-- Rendering
local render = function (cr)
-- The circles
cr.arc (-2, 0, 0.3, 0, math.pi * 2)
cr.new_sub_path ()
cr.arc (2, 0, 0.3, 0, math.pi * 2)
-- The terminals
cr.move_to (-3, 0)
cr.line_to (-2.3, 0)
cr.move_to (2.3, 0)
cr.line_to (3, 0)
cr.stroke ()
end
local render_ac = function (cr)
render (cr)
-- The AC symbol
cr.move_to (-1, 0.25)
cr.curve_to (-0.4, -1.5, 0.4, 1.5, 1, -0.25)
cr.stroke ()
end
local render_dc = function (cr)
render (cr)
-- The DC symbol
cr.move_to (-1, -0.25)
cr.line_to (1, -0.25)
cr.move_to (-1, 0.25)
cr.line_to (-0.2, 0.25)
cr.move_to (0.2, 0.25)
cr.line_to (1, 0.25)
-- Polarity sign
cr.move_to (2, -0.75)
cr.line_to (2, -1.25)
cr.move_to (1.75, -1)
cr.line_to (2.25, -1)
cr.stroke ()
end
-- Register the symbol
logdiag.register ("ACSource", names_ac, area_ac, terminals, render_ac)
logdiag.register ("DCSource", names_dc, area_dc, terminals, render_dc)