make font_mapper more configurable
This commit is contained in:
parent
49c8dcd442
commit
67c83d020f
2 changed files with 49 additions and 22 deletions
|
@ -3,15 +3,49 @@
|
|||
#-------------------------------------------------------------------------------
|
||||
# Class for a text element on a canvas
|
||||
#
|
||||
var font_mapper = func(family = "LiberationSans", weight = "", style = "", custom_mapper=nil)
|
||||
var font_mapper = func(family = nil, weight = nil, style = nil, options = nil)
|
||||
{
|
||||
if (isfunc(custom_mapper)) {
|
||||
var font = custom_mapper(family, weight, style);
|
||||
if (font != nil)
|
||||
return font;
|
||||
var defaults = {
|
||||
"default-font-family": "LiberationSans",
|
||||
"default-font-weight": "",
|
||||
"default-font-style": "",
|
||||
};
|
||||
|
||||
# setup defaults if no options are given
|
||||
if (options == nil) {
|
||||
options = defaults;
|
||||
}
|
||||
if (!ishash(options)) {
|
||||
logprint(LOG_ALERT, "font_mapper: options must be a hash!")
|
||||
}
|
||||
|
||||
if (string.match(family, "Liberation*")) {
|
||||
# use defaults for missing arguments
|
||||
if (family == nil) {
|
||||
family = options["default-font-family"] or defaults["default-font-family"];
|
||||
}
|
||||
if (weight == nil) {
|
||||
weight = options["default-font-weight"] or defaults["default-font-weight"];
|
||||
}
|
||||
if (style == nil) {
|
||||
style = options["default-font-style"] or defaults["default-font-style"];
|
||||
}
|
||||
|
||||
if (isfunc(options["font-mapper"])) {
|
||||
var font = options["font-mapper"](family, weight, style);
|
||||
if (font != nil) {
|
||||
return font;
|
||||
}
|
||||
}
|
||||
|
||||
# Remove '' that Inkscape puts around font names containing spaces
|
||||
if (left(family, 1) == "'") family = substr(family, 1);
|
||||
if (right(family, 1) == "'") family = substr(family, 0, size(family) - 1);
|
||||
|
||||
# map generic Inkscape sans serif to our default font
|
||||
if (string.lc(family) == "sans" or string.lc(family) == "sans-serif") {
|
||||
family = "LiberationSans";
|
||||
}
|
||||
if (left(family, 10) == "Liberation") {
|
||||
style = style == "italic" ? "Italic" : "";
|
||||
weight = weight == "bold" ? "Bold" : "";
|
||||
|
||||
|
@ -20,6 +54,7 @@ var font_mapper = func(family = "LiberationSans", weight = "", style = "", custo
|
|||
|
||||
return "LiberationFonts/"~string.replace(family, " ", "")~"-"~s~".ttf";
|
||||
}
|
||||
|
||||
return "LiberationFonts/LiberationMono-Bold.ttf";
|
||||
};
|
||||
|
||||
|
|
|
@ -13,8 +13,9 @@ var parsesvg = func(group, path, options = nil)
|
|||
if( options == nil )
|
||||
options = {};
|
||||
|
||||
if( typeof(options) != "hash" )
|
||||
if (!ishash(options)) {
|
||||
die("Options need to be of type hash!");
|
||||
}
|
||||
|
||||
# resolve paths using standard SimGear logic
|
||||
var file_path = resolvepath(path);
|
||||
|
@ -28,8 +29,6 @@ var parsesvg = func(group, path, options = nil)
|
|||
logprint(level, "parsesvg: "~msg~" [path='"~ path~"']");
|
||||
};
|
||||
|
||||
var custom_font_mapper = options['font-mapper'];
|
||||
|
||||
# Helper to get number without unit (eg. px)
|
||||
var evalCSSNum = func(css_num)
|
||||
{
|
||||
|
@ -323,7 +322,7 @@ var parsesvg = func(group, path, options = nil)
|
|||
var font_weight = style["font-weight"];
|
||||
var font_style = style["font-style"];
|
||||
if( font_family != nil or font_weight != nil or font_style != nil )
|
||||
stack[-1].set("font", font_mapper(font_family, font_weight, font_style, custom_font_mapper));
|
||||
stack[-1].set("font", font_mapper(font_family, font_weight, font_style, options));
|
||||
|
||||
var font_size = style["font-size"];
|
||||
if( font_size != nil )
|
||||
|
@ -564,10 +563,10 @@ var parsesvg = func(group, path, options = nil)
|
|||
# by adding another backslash - otherwise parse error anywhere below
|
||||
if (ref == nil or find("\\", ref) > -1)
|
||||
{
|
||||
return logpr(LOG_INFO, "Invalid or missing href in image tag: '" ~ ref ~ "'");
|
||||
return logpr(LOG_WARN, "Invalid or missing href in image tag: '" ~ ref ~ "'");
|
||||
}
|
||||
if (substr(ref, 0, 5) == "data:") {
|
||||
return logpr(LOG_INFO, "Unsupported embedded image");
|
||||
return logpr(LOG_WARN, "Unsupported embedded image");
|
||||
}
|
||||
elsif (substr(ref, 0, 5) != "file:") {
|
||||
# absolute paths seem to start with "file:"
|
||||
|
@ -707,7 +706,7 @@ var parsesvg = func(group, path, options = nil)
|
|||
text = nil;
|
||||
tspans = nil;
|
||||
}
|
||||
};
|
||||
}; #end()
|
||||
|
||||
# XML parsers element data callback
|
||||
var data = func(data)
|
||||
|
@ -730,15 +729,8 @@ var parsesvg = func(group, path, options = nil)
|
|||
call(func parsexml(path, start, end, data), nil, var err = []);
|
||||
if( size(err) )
|
||||
{
|
||||
var msg = err[0];
|
||||
for(var i = 1; i + 1 < size(err); i += 2)
|
||||
{
|
||||
# err = ['error message', 'file', line]
|
||||
msg ~= (i == 1 ? "\n at " : "\n called from: ")
|
||||
~ err[i] ~ ", line " ~ err[i + 1]
|
||||
}
|
||||
logpr(LOG_ALERT, msg ~ "\n ");
|
||||
|
||||
logpr(LOG_ALERT, "parse XML failed");
|
||||
debug.printerror(err);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue