1
0
Fork 0

compass.nas: add font to style hash; efis-canvas.nas: improve font mapping

This commit is contained in:
Henning Stahlke 2020-05-04 23:44:34 +02:00 committed by James Turner
parent 67c83d020f
commit d1d3b6559e
2 changed files with 49 additions and 34 deletions

View file

@ -10,15 +10,17 @@ var CompassRose = {
new: func() {
var obj = {
parents: [CompassRose.Style, canvas.draw.marksStyle.new()],
mark_count: 36, # number of marks, count = 360 / interval
label_count: 12, # number of text labels (degrees), e.g. 12
label_div: 10, # >0 divide degrees by this number for text label, e.g. 10
mark_count: 36, # number of marks, count = 360 / interval
label_count: 12, # number of text labels (degrees)
label_div: 10, # >0 div. degr. by this for text labels
circle_color: [255,255,255,1],
mark_color: [255,255,255,1],
label_color: [255,255,255,1],
center_mark: 0, # draw a mark in the center of the rose
fontsize: 0, # fontsize for labels
nesw: 1, # replace labels 0,90,180,270 by N,E,S,W
center_mark: 0, # draw a mark in the center of the rose
font: "sans", # fontsize for labels
font_weight: "bold", # fontsize for labels
fontsize: 0, # fontsize for labels
nesw: 1, # replace labels 0,90,180,270 by N,E,S,W
};
obj.setMarkLength(0.1);
obj.setSubdivisionLength(0.5);
@ -80,6 +82,7 @@ CompassRose.draw = func(cgroup, radius, style=nil) {
.createChildren("text", style.label_count);
var offset = (style.mark_offset < 0 ? -1 : 1) * style.mark_length * radius;
var rot = 2*math.pi/style.label_count;
var font = canvas.font_mapper(style.font, style.font_weight);
forindex (i; labels) {
var t = n = int(i*360 / style.label_count);
if (style.label_div > 0) t = int(n / style.label_div);
@ -93,7 +96,7 @@ CompassRose.draw = func(cgroup, radius, style=nil) {
labels[i]
.setText(txt)
.setFontSize(fontsize)
.setFont(font_mapper("LiberationSans","bold"))
.setFont(font)
.setColor(style.label_color)
.setAlignment("center-"~(style.mark_offset < 0 ? "top" : "bottom"))
.setTranslation(0,-radius-offset)

View file

@ -82,11 +82,13 @@ var EFISCanvas = {
},
loadsvg: func(file) {
var font_mapper = func(family, weight) {
return "LiberationFonts/LiberationSans-Regular.ttf";
logprint(LOG_INFO, "EFIS loading "~file);
var options = {
"default-font-family": "LiberationSans",
"default-font-weight": "",
"default-font-style": "",
};
print("EFIS loading "~file);
canvas.parsesvg(me._root, file, {'font-mapper': font_mapper});
canvas.parsesvg(me._root, file, options);
# create nasal variables for SVG elements;
# in a class derived from EFISCanvas, add IDs to the svg_keys member in the constructor (new)
@ -107,21 +109,21 @@ var EFISCanvas = {
# defaults to EFISCanvas instance calling this method, useful if "f"
# is a member of the EFISCanvas instance
addUpdateFunction: func(f, interval, f_me = nil) {
if (typeof(f) != "func") {
print("EFISCanvas.addUpdateFunction: Error, argument is not a function.");
if (!isfunc(f)) {
logprint(DEV_ALERT, "EFISCanvas.addUpdateFunction: argument is not a function.");
return;
}
interval = num(interval);
f_me = (f_me == nil) ? me : f_me;
if (interval != nil and interval >= 0) {
#the updateCountN is meant for debug/performance monitoring
var timer = maketimer(interval, me, func {
if (me.updateN != nil and me.updateN.getValue()) {
var err = [];
call(f, [], f_me, nil, err);
if (size(err))
debug.printerror(err);
me.updateCountN.setValue(me.updateCountN.getValue() + 1);
# debug/performance monitoring
me.updateCountN.increment();
}
});
append(me._timers, timer);
@ -137,8 +139,9 @@ var EFISCanvas = {
# stop all registered update functions
stopUpdates: func() {
foreach (var t; me._timers)
foreach (var t; me._timers) {
t.stop();
}
},
# getInstr - get props from /instrumentation/<sys>[i]/<prop>
@ -165,7 +168,7 @@ var EFISCanvas = {
}
me[svgkey].setText(text);
if (color != nil and me[svgkey].setColor != nil) {
if (typeof(color) == "vector") me[svgkey].setColor(color);
if (isvec(color)) me[svgkey].setColor(color);
else me[svgkey].setColor(me.colors[color]);
}
},
@ -186,32 +189,36 @@ var EFISCanvas = {
# value: optional value to trigger show(); otherwise node.value will be implicitly treated as bool
_makeListener_showHide: func(svgkeys, value=nil) {
if (value == nil) {
if (typeof(svgkeys) == "vector")
if (isvec(svgkeys)) {
return func(n) {
if (n.getValue())
foreach (var key; svgkeys) me[key].show();
else
foreach (var key; svgkeys) me[key].hide();
}
else
}
else {
return func(n) {
if (n.getValue()) me[svgkeys].show();
else me[svgkeys].hide();
}
}
}
else {
if (typeof(svgkeys) == "vector")
if (isvec(svgkeys)) {
return func(n) {
if (n.getValue() == value)
foreach (var key; svgkeys) me[key].show();
else
foreach (var key; svgkeys) me[key].hide();
};
else
}
else {
return func(n) {
if (n.getValue() == value) me[svgkeys].show();
else me[svgkeys].hide();
};
}
}
},
@ -222,21 +229,23 @@ var EFISCanvas = {
# {"svgkey" : factor}, missing keys will be treated as 1
_makeListener_rotate: func(svgkeys, factors=nil) {
if (factors == nil) {
if (typeof(svgkeys) == "vector")
if (isvec(svgkeys)) {
return func(n) {
var value = n.getValue() or 0;
foreach (var key; svgkeys) {
me[key].setRotation(value);
}
}
else
}
else {
return func(n) {
var value = n.getValue() or 0;
me[svgkeys].setRotation(value);
}
}
}
else {
if (typeof(svgkeys) == "vector")
if (isvec(svgkeys)) {
return func(n) {
var value = n.getValue() or 0;
foreach (var key; svgkeys) {
@ -244,12 +253,14 @@ var EFISCanvas = {
me[key].setRotation(value * factor);
}
};
else
}
else {
return func(n) {
var value = n.getValue() or 0;
var factor = num(factors) or 1;
me[svgkeys].setRotation(value * factor);
};
}
}
},
@ -259,15 +270,15 @@ var EFISCanvas = {
# factors: number (if svgkeys is a single key) or hash of numbers
# {"svgkey" : factor}, missing keys will be treated as 0 (=no op)
_makeListener_translate: func(svgkeys, fx, fy) {
if (typeof(svgkeys) == "vector") {
if (isvec(svgkeys)) {
var x = num(fx) or 0;
var y = num(fy) or 0;
if (typeof(fx) == "hash" or typeof(fy) == "hash") {
if (ishash(fx) or ishash(fy)) {
return func(n) {
foreach (var key; svgkeys) {
var value = n.getValue() or 0;
if (typeof(fx) == "hash") x = fx[key] or 0;
if (typeof(fy) == "hash") y = fy[key] or 0;
if (ishash(fx)) x = fx[key] or 0;
if (ishash(fy)) y = fy[key] or 0;
me[key].setTranslation(value * x, value * y);
}
};
@ -300,9 +311,9 @@ var EFISCanvas = {
# (hint: putting elements in a SVG group (if possible) might be easier)
# colors can be either a vector e.g. [r,g,b] or "name" from me.colors
_makeListener_setColor: func(svgkeys, color_true, color_false) {
var col_0 = (typeof(color_false) == "vector") ? color_false : me.colors[color_false];
var col_1 = (typeof(color_true) == "vector") ? color_true : me.colors[color_true];
if (typeof(svgkeys) == "vector") {
var col_0 = isvec(color_false) ? color_false : me.colors[color_false];
var col_1 = isvec(color_true) ? color_true : me.colors[color_true];
if (isvec(svgkeys) ) {
return func(n) {
if (n.getValue())
foreach (var key; svgkeys) me[key].setColor(col_1);
@ -319,10 +330,11 @@ var EFISCanvas = {
},
_makeListener_updateText: func(svgkeys, format="%s", default="") {
if (typeof(svgkeys) == "vector") {
if (isvec(svgkeys)) {
return func(n) {
foreach (var key; svgkeys)
foreach (var key; svgkeys) {
me.updateTextElement(key, sprintf(format, n.getValue() or default));
}
};
}
else {