Now setting the CSS file path as a parameter

This will allow us to use separate CSS and XSLT files (e.g. themes) later
This commit is contained in:
Gergely Polonkai 2013-09-19 23:16:52 +02:00
parent a154118b6f
commit fcb75f672b
2 changed files with 28 additions and 4 deletions

View File

@ -16,7 +16,7 @@
<xsl:variable name="r_aspect" select="240" /> <xsl:variable name="r_aspect" select="240" />
<xsl:template match="/"> <xsl:template match="/">
<xsl:processing-instruction name="xml-stylesheet">href="test.css" type="text/css"</xsl:processing-instruction> <xsl:processing-instruction name="xml-stylesheet">href="<xsl:value-of select="$css_file"/>" type="text/css"</xsl:processing-instruction>
<svg <svg
xmlns="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xlink="http://www.w3.org/1999/xlink"

View File

@ -671,8 +671,12 @@ ag_chart_create_svg(AgChart *chart, GError **err)
node = NULL; node = NULL;
gchar *value, gchar *value,
*stylesheet_path, *stylesheet_path,
*css_path,
*xslt, *xslt,
*save_content = NULL; *save_content = NULL,
*css_uri,
*css_final_uri,
**params;
GList *houses, GList *houses,
*house, *house,
*planet, *planet,
@ -685,7 +689,8 @@ ag_chart_create_svg(AgChart *chart, GError **err)
*antiscia_class; *antiscia_class;
guint xslt_length; guint xslt_length;
gint save_length; gint save_length;
GFile *xslt_file; GFile *xslt_file,
*css_file;
xsltStylesheetPtr xslt_proc; xsltStylesheetPtr xslt_proc;
locale_t current_locale; locale_t current_locale;
@ -838,9 +843,17 @@ ag_chart_create_svg(AgChart *chart, GError **err)
return NULL; return NULL;
} }
css_path = g_strdup_printf("%s/%s", PKGDATADIR, "chart.css");
g_debug("Using %s as a CSS stylesheet", css_path);
css_file = g_file_new_for_path(css_path);
css_uri = g_file_get_uri(css_file);
if ((xslt_doc = xmlReadMemory(xslt, xslt_length, "chart.xsl", NULL, 0)) == NULL) { if ((xslt_doc = xmlReadMemory(xslt, xslt_length, "chart.xsl", NULL, 0)) == NULL) {
g_set_error(err, AG_CHART_ERROR, AG_CHART_ERROR_CORRUPT_FILE, "File '%s' can not be parsed as a stylesheet file.", stylesheet_path); g_set_error(err, AG_CHART_ERROR, AG_CHART_ERROR_CORRUPT_FILE, "File '%s' can not be parsed as a stylesheet file.", stylesheet_path);
g_free(stylesheet_path); g_free(stylesheet_path);
g_free(css_path);
g_free(css_uri);
g_object_unref(css_file);
g_free(xslt); g_free(xslt);
xmlFreeDoc(doc); xmlFreeDoc(doc);
@ -850,21 +863,32 @@ ag_chart_create_svg(AgChart *chart, GError **err)
if ((xslt_proc = xsltParseStylesheetDoc(xslt_doc)) == NULL) { if ((xslt_proc = xsltParseStylesheetDoc(xslt_doc)) == NULL) {
g_set_error(err, AG_CHART_ERROR, AG_CHART_ERROR_CORRUPT_FILE, "File '%s' can not be parsed as a stylesheet file.", stylesheet_path); g_set_error(err, AG_CHART_ERROR, AG_CHART_ERROR_CORRUPT_FILE, "File '%s' can not be parsed as a stylesheet file.", stylesheet_path);
g_free(stylesheet_path); g_free(stylesheet_path);
g_free(css_path);
g_free(xslt); g_free(xslt);
g_free(css_uri);
g_object_unref(css_file);
xmlFreeDoc(xslt_doc); xmlFreeDoc(xslt_doc);
xmlFreeDoc(doc); xmlFreeDoc(doc);
return NULL; return NULL;
} }
css_final_uri = g_strdup_printf("'%s'", css_uri);
g_free(css_uri);
params = g_new0(gchar *, 3);
params[0] = "css_file";
params[1] = css_final_uri;
// libxml2 messes up the output, as it prints decimal floating point // libxml2 messes up the output, as it prints decimal floating point
// numbers in a localized format. It is not good in locales that use a // numbers in a localized format. It is not good in locales that use a
// character for decimal separator other than a dot. So let's just use the // character for decimal separator other than a dot. So let's just use the
// C locale until the SVG is generated. // C locale until the SVG is generated.
current_locale = uselocale(newlocale(LC_ALL, "C", 0)); current_locale = uselocale(newlocale(LC_ALL, "C", 0));
svg_doc = xsltApplyStylesheet(xslt_proc, doc, NULL); svg_doc = xsltApplyStylesheet(xslt_proc, doc, (const char **)params);
uselocale(current_locale); uselocale(current_locale);
g_free(stylesheet_path); g_free(stylesheet_path);
g_free(css_path);
g_object_unref(css_file);
g_free(params);
xsltFreeStylesheet(xslt_proc); xsltFreeStylesheet(xslt_proc);
xmlFreeDoc(doc); xmlFreeDoc(doc);