diff options
author | RazrFalcon <razrfalcon@gmail.com> | 2018-11-05 20:35:00 +0200 |
---|---|---|
committer | RazrFalcon <razrfalcon@gmail.com> | 2018-11-05 20:40:46 +0200 |
commit | d7c1a3d80463c06b7f4e4d3448d620e4f8324c47 (patch) | |
tree | 62ad28147875e6e767e6cc3d52e04676b4fc7dd8 /capi | |
parent | 28d0d3dce9a08d97854b7dbbcfe87de7fba71d38 (diff) |
Added 'systemLanguage' attribute support.
Diffstat (limited to 'capi')
-rw-r--r-- | capi/include/ResvgQt.h | 67 | ||||
-rw-r--r-- | capi/include/resvg.h | 43 | ||||
-rw-r--r-- | capi/src/lib.rs | 29 |
3 files changed, 111 insertions, 28 deletions
diff --git a/capi/include/ResvgQt.h b/capi/include/ResvgQt.h index 2096c9e..2be031a 100644 --- a/capi/include/ResvgQt.h +++ b/capi/include/ResvgQt.h @@ -31,15 +31,11 @@ extern "C" { namespace ResvgPrivate { -static void initOptions(resvg_options &opt) +static const char* toCStr(const QString &text) { - resvg_init_options(&opt); - - const auto screens = qApp->screens(); - if (!screens.isEmpty()) { - const auto screen = screens.at(0); - opt.dpi = screen->logicalDotsPerInch() * screen->devicePixelRatio(); - } + const auto utf8 = text.toUtf8(); + const auto data = utf8.constData(); + return qstrdup(data); } class Data @@ -47,16 +43,45 @@ class Data public: Data() { - resvg_init_options(&opt); + init(); } ~Data() { - reset(); + clear(); } void reset() { + clear(); + init(); + } + + resvg_render_tree *tree = nullptr; + resvg_options opt; + QRectF viewBox; + QString errMsg; + +private: + void init() + { + resvg_init_options(&opt); + + QFont font; + opt.font_family = toCStr(font.family()); + opt.font_size = font.pointSize(); + + opt.languages = toCStr(QLocale().bcp47Name()); + + const auto screens = qApp->screens(); + if (!screens.isEmpty()) { + const auto screen = screens.at(0); + opt.dpi = screen->logicalDotsPerInch() * screen->devicePixelRatio(); + } + } + + void clear() + { if (tree) { resvg_tree_destroy(tree); tree = nullptr; @@ -67,15 +92,19 @@ public: opt.path = NULL; } - initOptions(opt); + if (opt.font_family) { + delete[] opt.font_family; // do not use free() because was allocated via qstrdup() + opt.font_family = NULL; + } + + if (opt.languages) { + delete[] opt.languages; // do not use free() because was allocated via qstrdup() + opt.languages = NULL; + } + viewBox = QRectF(); errMsg = QString(); } - - resvg_render_tree *tree = nullptr; - resvg_options opt; - QRectF viewBox; - QString errMsg; }; static QString errorToString(const int err) @@ -269,11 +298,9 @@ inline bool ResvgRenderer::load(const QString &filePath) d->reset(); - const auto utf8Str = filePath.toUtf8(); - const auto rawFilePath = utf8Str.constData(); - d->opt.path = qstrdup(rawFilePath); + d->opt.path = ResvgPrivate::toCStr(filePath); - const auto err = resvg_parse_tree_from_file(rawFilePath, &d->opt, &d->tree); + const auto err = resvg_parse_tree_from_file(d->opt.path, &d->opt, &d->tree); if (err != RESVG_OK) { d->errMsg = ResvgPrivate::errorToString(err); return false; diff --git a/capi/include/resvg.h b/capi/include/resvg.h index 90c05be..61f3035 100644 --- a/capi/include/resvg.h +++ b/capi/include/resvg.h @@ -99,27 +99,60 @@ typedef struct resvg_fit_to { * @brief Rendering options. */ typedef struct resvg_options { - /** SVG image path. Used to resolve relative image paths. */ + /** SVG image path. Used to resolve relative image paths. + * + * Default: NULL + */ const char *path; - /** Output DPI. Default: 96. */ + + /** Output DPI. + * + * Default: 96. + */ double dpi; - /** Default font family. Default: 'Timer New Roman'. */ + + /** Default font family. + * + * Default: NULL. + */ const char *font_family; - /** Default font size. Default: 12. */ + + /** Default font size. + * + * Default: 12. + */ double font_size; + + /** + * Sets a comma-separated list of languages that will be used + * during the 'systemLanguage' attribute resolving. + * Examples: 'en-US', 'en-US, ru-RU', 'en, ru' + * + * Default: NULL. + */ + const char *languages; + /** * Fits the image using specified options. * * Default: \b RESVG_FIT_TO_ORIGINAL. */ resvg_fit_to fit_to; - /** Draw background. Default: false. */ + + /** Draw background. + * + * Default: false. + */ bool draw_background; + /** Background color. */ resvg_color background; + /** * Keep named groups. If set to \b true, all non-empty * groups with \b id attribute will not be removed. + * + * Default: false */ bool keep_named_groups; } resvg_options; diff --git a/capi/src/lib.rs b/capi/src/lib.rs index 9db8ab0..5869330 100644 --- a/capi/src/lib.rs +++ b/capi/src/lib.rs @@ -38,6 +38,7 @@ pub struct resvg_options { pub dpi: f64, pub font_family: *const c_char, pub font_size: f64, + pub languages: *const c_char, pub fit_to: resvg_fit_to, pub draw_background: bool, pub background: resvg_color, @@ -153,8 +154,9 @@ pub extern fn resvg_init_options(opt: *mut resvg_options) { unsafe { (*opt).path = ptr::null(); (*opt).dpi = 96.0; - (*opt).font_family = b"Times New Roman\0".as_ptr() as *const _; + (*opt).font_family = ptr::null(); (*opt).font_size = 12.0; + (*opt).languages = ptr::null(); (*opt).fit_to = resvg_fit_to { kind: resvg_fit_to_type::RESVG_FIT_TO_ORIGINAL, value: 0.0, @@ -666,24 +668,45 @@ fn to_native_opt(opt: &resvg_options) -> resvg::Options { let font_family = match cstr_to_str(opt.font_family) { Some(v) => { if v.is_empty() { - warn!("Provided 'font_family' is empty. Fallback to '{}'.", ff); + warn!("Provided 'font_family' option is empty. Fallback to '{}'.", ff); ff } else { v } } None => { - warn!("Provided 'font_family' is no an UTF-8 string. Fallback to '{}'.", ff); + warn!("Provided 'font_family' option is no an UTF-8 string. Fallback to '{}'.", ff); ff } }; + + let languages_str = match cstr_to_str(opt.languages) { + Some(v) => v, + None => { + warn!("Provided 'languages' option is no an UTF-8 string. Fallback to 'en'."); + "en" + } + }; + + let mut languages = Vec::new(); + for lang in languages_str.split(',') { + languages.push(lang.trim().to_string()); + } + + if languages.is_empty() { + warn!("Provided 'languages' option is empty. Fallback to 'en'."); + languages = vec!["en".to_string()] + } + + resvg::Options { usvg: usvg::Options { path, dpi: opt.dpi, font_family: font_family.to_string(), font_size: opt.font_size, + languages, keep_named_groups: opt.keep_named_groups, }, fit_to, |