[lasem] svg_text: support for style, stretch and weight properties.



commit 6ae9c5047108bf293f401402d4e09cf0a32cc17b
Author: Emmanuel Pacaud <emmanuel gnome org>
Date:   Mon Aug 9 22:24:35 2010 +0200

    svg_text: support for style, stretch and weight properties.

 src/lsmsvgenums.c  |   85 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/lsmsvgenums.h  |   35 +++++++++++++++++++++
 src/lsmsvgstyle.c  |    6 ++--
 src/lsmsvgstyle.h  |   21 +++++++++++--
 src/lsmsvgtraits.c |   82 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/lsmsvgtraits.h |   27 +++++++++-------
 src/lsmsvgview.c   |   49 ++++++++++++++++++++++++++++++
 7 files changed, 287 insertions(+), 18 deletions(-)
---
diff --git a/src/lsmsvgenums.c b/src/lsmsvgenums.c
index b1f6c09..97d7e0a 100644
--- a/src/lsmsvgenums.c
+++ b/src/lsmsvgenums.c
@@ -275,3 +275,88 @@ lsm_svg_comp_op_from_string (const char *string)
 					  G_N_ELEMENTS (lsm_svg_comp_op_strings));
 }
 
+static const char *lsm_svg_font_stretch_strings[] = {
+	"normal",
+	"ultra-condensed",
+	"extra-condensed",
+	"condensed",
+	"semi-condensed",
+	"semi-expanded",
+	"expanded",
+	"extra-expanded",
+	"ultra-expanded"
+};
+
+const char *
+lsm_svg_font_stretch_to_string (LsmSvgFontStretch font_stretch)
+{
+	if (font_stretch < 0 || font_stretch > LSM_SVG_FONT_STRETCH_ULTRA_EXPANDED)
+		return NULL;
+
+	return lsm_svg_font_stretch_strings[font_stretch];
+}
+
+LsmSvgFontStretch
+lsm_svg_font_stretch_from_string (const char *string)
+{
+	return lsm_enum_value_from_string (string, lsm_svg_font_stretch_strings,
+					  G_N_ELEMENTS (lsm_svg_font_stretch_strings));
+}
+
+static const char *lsm_svg_font_style_strings[] = {
+	"normal",
+	"oblique",
+	"italic"
+};
+
+const char *
+lsm_svg_font_style_to_string (LsmSvgFontStyle font_style)
+{
+	if (font_style < 0 || font_style > LSM_SVG_FONT_STYLE_ITALIC)
+		return NULL;
+
+	return lsm_svg_font_style_strings[font_style];
+}
+
+LsmSvgFontStyle
+lsm_svg_font_style_from_string (const char *string)
+{
+	return lsm_enum_value_from_string (string, lsm_svg_font_style_strings,
+					  G_N_ELEMENTS (lsm_svg_font_style_strings));
+}
+
+static const char *lsm_svg_font_weight_strings[] = {
+	"normal",
+	"bold"
+};
+
+const char *
+lsm_svg_font_weight_to_string (LsmSvgFontWeight font_weight)
+{
+	switch (font_weight) {
+		case LSM_SVG_FONT_WEIGHT_NORMAL:
+			return lsm_svg_font_weight_strings[0];
+		case LSM_SVG_FONT_WEIGHT_BOLD:
+			return lsm_svg_font_weight_strings[1];
+		default:
+			return NULL;
+	}
+}
+
+LsmSvgFontWeight
+lsm_svg_font_weight_from_string (const char *string)
+{
+	int index;
+
+	index = lsm_enum_value_from_string (string, lsm_svg_font_weight_strings,
+					   G_N_ELEMENTS (lsm_svg_font_weight_strings));
+
+	switch (index) {
+		case 0:
+			return LSM_SVG_FONT_WEIGHT_NORMAL;
+		case 1:
+			return LSM_SVG_FONT_WEIGHT_BOLD;
+		default:
+			return -1;
+	}
+}
diff --git a/src/lsmsvgenums.h b/src/lsmsvgenums.h
index 1337be1..af75d2a 100644
--- a/src/lsmsvgenums.h
+++ b/src/lsmsvgenums.h
@@ -196,6 +196,41 @@ const char * 		lsm_svg_comp_op_to_string 	(LsmSvgCompOp comp_op);
 LsmSvgCompOp		lsm_svg_comp_op_from_string	(const char *string);
 
 typedef enum {
+	LSM_SVG_FONT_STRETCH_ERROR = -1,
+	LSM_SVG_FONT_STRETCH_NORMAL,
+	LSM_SVG_FONT_STRETCH_ULTRA_CONDENSED,
+	LSM_SVG_FONT_STRETCH_EXTRA_CONDENSED,
+	LSM_SVG_FONT_STRETCH_CONDENSED,
+	LSM_SVG_FONT_STRETCH_SEMI_CONDENSED,
+	LSM_SVG_FONT_STRETCH_SEMI_EXPANDED,
+	LSM_SVG_FONT_STRETCH_EXPANDED,
+	LSM_SVG_FONT_STRETCH_EXTRA_EXPANDED,
+	LSM_SVG_FONT_STRETCH_ULTRA_EXPANDED
+} LsmSvgFontStretch;
+
+const char * 		lsm_svg_font_stretch_to_string 		(LsmSvgFontStretch font_stretch);
+LsmSvgFontStretch	lsm_svg_font_stretch_from_string	(const char *string);
+
+typedef enum {
+	LSM_SVG_FONT_STYLE_ERROR = -1,
+	LSM_SVG_FONT_STYLE_NORMAL,
+	LSM_SVG_FONT_STYLE_OBLIQUE,
+	LSM_SVG_FONT_STYLE_ITALIC
+} LsmSvgFontStyle;
+
+const char * 		lsm_svg_font_style_to_string 		(LsmSvgFontStyle font_style);
+LsmSvgFontStyle		lsm_svg_font_style_from_string		(const char *string);
+
+typedef enum {
+	LSM_SVG_FONT_WEIGHT_ERROR = -1,
+	LSM_SVG_FONT_WEIGHT_NORMAL = 400,
+	LSM_SVG_FONT_WEIGHT_BOLD = 700
+} LsmSvgFontWeight;
+
+const char * 		lsm_svg_font_weight_to_string 	(LsmSvgFontWeight font_weight);
+LsmSvgFontWeight	lsm_svg_font_weight_from_string	(const char *string);
+
+typedef enum {
 	LSM_SVG_ELEMENT_CATEGORY_NONE			= 1 << 0,
 	LSM_SVG_ELEMENT_CATEGORY_DESCRIPTIVE		= 1 << 1,
 	LSM_SVG_ELEMENT_CATEGORY_CONTAINER		= 1 << 2,
diff --git a/src/lsmsvgstyle.c b/src/lsmsvgstyle.c
index 73b9b38..707f9a5 100644
--- a/src/lsmsvgstyle.c
+++ b/src/lsmsvgstyle.c
@@ -230,13 +230,13 @@ static const LsmPropertyInfos lsm_svg_property_infos[] = {
 	{
 		.name = "font-stretch",
 		.id = LSM_PROPERTY_OFFSET_TO_ID (LsmSvgStyle, font_stretch),
-		.trait_class = &lsm_null_trait_class,
+		.trait_class = &lsm_svg_font_stretch_trait_class,
 		.trait_default = "normal"
 	},
 	{
 		.name = "font-style",
 		.id = LSM_PROPERTY_OFFSET_TO_ID (LsmSvgStyle, font_style),
-		.trait_class = &lsm_null_trait_class,
+		.trait_class = &lsm_svg_font_style_trait_class,
 		.trait_default = "normal"
 	},
 	{
@@ -248,7 +248,7 @@ static const LsmPropertyInfos lsm_svg_property_infos[] = {
 	{
 		.name = "font-weight",
 		.id = LSM_PROPERTY_OFFSET_TO_ID (LsmSvgStyle, font_weight),
-		.trait_class = &lsm_null_trait_class,
+		.trait_class = &lsm_svg_font_weight_trait_class,
 		.trait_default = "normal"
 	},
 	{
diff --git a/src/lsmsvgstyle.h b/src/lsmsvgstyle.h
index 2c0aa45..aebad9a 100644
--- a/src/lsmsvgstyle.h
+++ b/src/lsmsvgstyle.h
@@ -60,6 +60,21 @@ typedef struct {
 
 typedef struct {
 	LsmProperty base;
+	LsmSvgFontStretch value;
+} LsmSvgFontStretchProperty;
+
+typedef struct {
+	LsmProperty base;
+	LsmSvgFontStyle value;
+} LsmSvgFontStyleProperty;
+
+typedef struct {
+	LsmProperty base;
+	LsmSvgFontWeight value;
+} LsmSvgFontWeightProperty;
+
+typedef struct {
+	LsmProperty base;
 	LsmSvgLineJoin value;
 } LsmSvgLineJoinProperty;
 
@@ -112,10 +127,10 @@ struct _LsmSvgStyle {
 	LsmProperty *			font_family;
 	LsmSvgLengthProperty *		font_size;
 	LsmProperty *			font_size_adjust;
-	LsmProperty *			font_stretch;
-	LsmProperty *			font_style;
+	LsmSvgFontStretchProperty *	font_stretch;
+	LsmSvgFontStyleProperty *	font_style;
 	LsmProperty *			font_variant;
-	LsmProperty *			font_weight;
+	LsmSvgFontWeightProperty *	font_weight;
 	LsmProperty *			glyph_orientation_horizontal;
 	LsmProperty *			glyph_orientation_vertical;
 	LsmProperty *			image_rendering;
diff --git a/src/lsmsvgtraits.c b/src/lsmsvgtraits.c
index 135c915..7c0348d 100644
--- a/src/lsmsvgtraits.c
+++ b/src/lsmsvgtraits.c
@@ -25,6 +25,7 @@
 #include <lsmsvgcolors.h>
 #include <math.h>
 #include <string.h>
+#include <stdlib.h>
 
 const LsmSvgColor lsm_svg_color_null = {0.0, 0.0, 0.0};
 const LsmSvgDashArray lsm_svg_dash_array_null = {0, NULL};
@@ -403,6 +404,87 @@ const LsmTraitClass lsm_svg_fill_rule_trait_class = {
 };
 
 static gboolean
+lsm_svg_font_style_trait_from_string (LsmTrait *abstract_trait, char *string)
+{
+	LsmSvgFontStyle *trait = (LsmSvgFontStyle *) abstract_trait;
+
+	*trait = lsm_svg_font_style_from_string (string);
+
+	return *trait >= 0;
+}
+
+char *
+lsm_svg_font_style_trait_to_string (LsmTrait *abstract_trait)
+{
+	LsmSvgFontStyle *trait = (LsmSvgFontStyle *) abstract_trait;
+
+	return g_strdup (lsm_svg_font_style_to_string (*trait));
+}
+
+const LsmTraitClass lsm_svg_font_style_trait_class = {
+	.size = sizeof (LsmSvgFontStyle),
+	.from_string = lsm_svg_font_style_trait_from_string,
+	.to_string = lsm_svg_font_style_trait_to_string
+};
+
+static gboolean
+lsm_svg_font_stretch_trait_from_string (LsmTrait *abstract_trait, char *string)
+{
+	LsmSvgFontStretch *trait = (LsmSvgFontStretch *) abstract_trait;
+
+	*trait = lsm_svg_font_stretch_from_string (string);
+
+	return *trait >= 0;
+}
+
+char *
+lsm_svg_font_stretch_trait_to_string (LsmTrait *abstract_trait)
+{
+	LsmSvgFontStretch *trait = (LsmSvgFontStretch *) abstract_trait;
+
+	return g_strdup (lsm_svg_font_stretch_to_string (*trait));
+}
+
+const LsmTraitClass lsm_svg_font_stretch_trait_class = {
+	.size = sizeof (LsmSvgFontStretch),
+	.from_string = lsm_svg_font_stretch_trait_from_string,
+	.to_string = lsm_svg_font_stretch_trait_to_string
+};
+
+static gboolean
+lsm_svg_font_weight_trait_from_string (LsmTrait *abstract_trait, char *string)
+{
+	LsmSvgFontWeight *trait = (LsmSvgFontWeight *) abstract_trait;
+
+	*trait = lsm_svg_font_weight_from_string (string);
+
+	if (*trait < 0)
+		*trait = strtol (string, NULL, 10);
+
+	return *trait >= 100 && *trait <= 1000;
+}
+
+char *
+lsm_svg_font_weight_trait_to_string (LsmTrait *abstract_trait)
+{
+	LsmSvgFontWeight *trait = (LsmSvgFontWeight *) abstract_trait;
+	const char *string;
+
+	string = lsm_svg_font_weight_to_string (*trait);
+
+	if (string != NULL)
+		return g_strdup (string);
+
+	return g_strdup_printf ("%d", *trait);
+}
+
+const LsmTraitClass lsm_svg_font_weight_trait_class = {
+	.size = sizeof (LsmSvgFontWeight),
+	.from_string = lsm_svg_font_weight_trait_from_string,
+	.to_string = lsm_svg_font_weight_trait_to_string
+};
+
+static gboolean
 lsm_svg_line_join_trait_from_string (LsmTrait *abstract_trait, char *string)
 {
 	LsmSvgLineJoin *trait = (LsmSvgLineJoin *) abstract_trait;
diff --git a/src/lsmsvgtraits.h b/src/lsmsvgtraits.h
index 3f19e3e..76ee1e7 100644
--- a/src/lsmsvgtraits.h
+++ b/src/lsmsvgtraits.h
@@ -30,6 +30,11 @@
 G_BEGIN_DECLS
 
 typedef struct {
+	LsmSvgAngleType type;
+	double angle;
+} LsmSvgAngle;
+
+typedef struct {
 	double red;
 	double green;
 	double blue;
@@ -61,24 +66,22 @@ typedef struct {
 	LsmSvgMeetOrSlice meet_or_slice;
 } LsmSvgPreserveAspectRatio;
 
-typedef struct {
-	LsmSvgAngleType type;
-	double angle;
-} LsmSvgAngle;
-
-extern const LsmTraitClass lsm_svg_length_trait_class;
-extern const LsmTraitClass lsm_svg_matrix_trait_class;
-extern const LsmTraitClass lsm_svg_paint_trait_class;
+extern const LsmTraitClass lsm_svg_angle_trait_class;
+extern const LsmTraitClass lsm_svg_color_trait_class;
+extern const LsmTraitClass lsm_svg_dash_array_trait_class;
 extern const LsmTraitClass lsm_svg_fill_rule_trait_class;
+extern const LsmTraitClass lsm_svg_font_stretch_trait_class;
+extern const LsmTraitClass lsm_svg_font_style_trait_class;
+extern const LsmTraitClass lsm_svg_font_weight_trait_class;
+extern const LsmTraitClass lsm_svg_length_trait_class;
 extern const LsmTraitClass lsm_svg_line_join_trait_class;
 extern const LsmTraitClass lsm_svg_line_cap_trait_class;
-extern const LsmTraitClass lsm_svg_dash_array_trait_class;
-extern const LsmTraitClass lsm_svg_color_trait_class;
-extern const LsmTraitClass lsm_svg_pattern_units_trait_class;
 extern const LsmTraitClass lsm_svg_marker_units_trait_class;
+extern const LsmTraitClass lsm_svg_matrix_trait_class;
+extern const LsmTraitClass lsm_svg_paint_trait_class;
+extern const LsmTraitClass lsm_svg_pattern_units_trait_class;
 extern const LsmTraitClass lsm_svg_preserve_aspect_ratio_trait_class;
 extern const LsmTraitClass lsm_svg_spread_method_trait_class;
-extern const LsmTraitClass lsm_svg_angle_trait_class;
 
 G_END_DECLS
 
diff --git a/src/lsmsvgview.c b/src/lsmsvgview.c
index 8e639a7..2f4c314 100644
--- a/src/lsmsvgview.c
+++ b/src/lsmsvgview.c
@@ -1258,6 +1258,8 @@ lsm_svg_view_show_text (LsmSvgView *view, char const *string, double x, double y
 	const LsmSvgStyle *style;
 	PangoLayout *pango_layout;
 	PangoFontDescription *font_description;
+	PangoStretch font_stretch;
+	PangoStyle font_style;
 	PangoLayoutIter *iter;
 	PangoRectangle ink_rect;
 	double font_size;
@@ -1281,6 +1283,53 @@ lsm_svg_view_show_text (LsmSvgView *view, char const *string, double x, double y
 
 	pango_font_description_set_family (font_description, style->font_family->value);
 	pango_font_description_set_size (font_description, font_size * PANGO_SCALE);
+	pango_font_description_set_weight (font_description, style->font_weight->value);
+
+	switch (style->font_stretch->value) {
+		case LSM_SVG_FONT_STRETCH_ULTRA_CONDENSED:
+			font_stretch = PANGO_STRETCH_ULTRA_CONDENSED;
+			break;
+		case LSM_SVG_FONT_STRETCH_EXTRA_CONDENSED:
+			font_stretch = PANGO_STRETCH_EXTRA_CONDENSED;
+			break;
+		case LSM_SVG_FONT_STRETCH_CONDENSED:
+			font_stretch = PANGO_STRETCH_CONDENSED;
+			break;
+		case LSM_SVG_FONT_STRETCH_SEMI_CONDENSED:
+			font_stretch = PANGO_STRETCH_SEMI_CONDENSED;
+			break;
+		case LSM_SVG_FONT_STRETCH_SEMI_EXPANDED:
+			font_stretch = PANGO_STRETCH_SEMI_EXPANDED;
+			break;
+		case LSM_SVG_FONT_STRETCH_EXPANDED:
+			font_stretch = PANGO_STRETCH_EXPANDED;
+			break;
+		case LSM_SVG_FONT_STRETCH_EXTRA_EXPANDED:
+			font_stretch = PANGO_STRETCH_EXTRA_EXPANDED;
+			break;
+		case LSM_SVG_FONT_STRETCH_ULTRA_EXPANDED:
+			font_stretch = PANGO_STRETCH_ULTRA_EXPANDED;
+			break;
+		case LSM_SVG_FONT_STRETCH_NORMAL:
+		default:
+			font_stretch = PANGO_STRETCH_NORMAL;
+			break;
+	}
+	pango_font_description_set_stretch (font_description, font_stretch);
+
+	switch (style->font_style->value) {
+		case LSM_SVG_FONT_STYLE_OBLIQUE:
+			font_style = PANGO_STYLE_OBLIQUE;
+			break;
+		case LSM_SVG_FONT_STYLE_ITALIC:
+			font_style = PANGO_STYLE_ITALIC;
+			break;
+		case LSM_SVG_FONT_STYLE_NORMAL:
+		default:
+			font_style = PANGO_STYLE_NORMAL;
+			break;
+	}
+	pango_font_description_set_style (font_description, font_style);
 
 	pango_layout_set_text (pango_layout, string, -1);
 	pango_layout_set_font_description (pango_layout, font_description);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]