1 /* Fo
2  * fo-property-scaling-method.c: 'scaling-method' property
3  *
4  * Copyright (C) 2001 Sun Microsystems
5  * Copyright (C) 2007 Menteith Consulting Ltd
6  *
7  * See COPYING for the status of this software.
8  */
9 
10 #include <string.h>
11 #include "fo-utils.h"
12 #include "fo-context.h"
13 #include "datatype/fo-datatype.h"
14 #include "property/fo-property-private.h"
15 #include "property/fo-property-font-size.h"
16 #include "property/fo-property-scaling-method.h"
17 
18 /* scaling-method */
19 /* Inherited: FALSE */
20 /* Shorthand: FALSE */
21 /* auto | integer-pixels | resample-any-method | inherit */
22 /* Initial value: auto */
23 
24 struct _FoPropertyScalingMethod
25 {
26   FoProperty parent_instance;
27 };
28 
29 struct _FoPropertyScalingMethodClass
30 {
31   FoPropertyClass parent_class;
32 };
33 
34 static void fo_property_scaling_method_init         (FoPropertyScalingMethod      *property_scaling_method);
35 static void fo_property_scaling_method_class_init   (FoPropertyScalingMethodClass *klass);
36 static void fo_property_scaling_method_finalize     (GObject       *object);
37 
38 static FoDatatype* fo_property_scaling_method_resolve_enum (const gchar *token,
39                                                             FoContext   *context,
40                                                             GError     **error);
41 static FoDatatype* fo_property_scaling_method_validate (FoDatatype *datatype,
42                                                         FoContext  *context,
43                                                         GError    **error);
44 
45 static const gchar class_name[] = "scaling-method";
46 static gpointer parent_class;
47 
48 /**
49  * fo_property_scaling_method_get_type:
50  *
51  * Register the #FoPropertyScalingMethod type if not already registered and
52  * return its #GType value.
53  *
54  * Return value: #GType of #FoPropertyScalingMethod.
55  **/
56 GType
fo_property_scaling_method_get_type(void)57 fo_property_scaling_method_get_type (void)
58 {
59   static GType object_type = 0;
60 
61   if (!object_type)
62     {
63       static const GTypeInfo object_info =
64       {
65         sizeof (FoPropertyScalingMethodClass),
66         NULL,           /* base_init */
67         NULL,           /* base_finalize */
68         (GClassInitFunc) fo_property_scaling_method_class_init,
69         NULL,           /* class_finalize */
70         NULL,           /* class_data */
71         sizeof (FoPropertyScalingMethod),
72         0,              /* n_preallocs */
73         (GInstanceInitFunc) fo_property_scaling_method_init,
74 	NULL		/* value_table */
75       };
76 
77       object_type = g_type_register_static (FO_TYPE_PROPERTY,
78                                             class_name,
79                                             &object_info, 0);
80     }
81 
82   return object_type;
83 }
84 
85 /**
86  * fo_property_scaling_method_init:
87  * @scaling_method: #FoPropertyScalingMethod object to initialise.
88  *
89  * Implements #GInstanceInitFunc for #FoPropertyScalingMethod.
90  **/
91 void
fo_property_scaling_method_init(FoPropertyScalingMethod * scaling_method)92 fo_property_scaling_method_init (FoPropertyScalingMethod *scaling_method)
93 {
94   FO_PROPERTY (scaling_method)->value =
95     g_object_ref (fo_enum_factory_get_enum_by_value (FO_ENUM_ENUM_AUTO));
96 }
97 
98 /**
99  * fo_property_scaling_method_class_init:
100  * @klass: #FoPropertyScalingMethodClass object to initialise.
101  *
102  * Implements #GClassInitFunc for #FoPropertyScalingMethodClass.
103  **/
104 void
fo_property_scaling_method_class_init(FoPropertyScalingMethodClass * klass)105 fo_property_scaling_method_class_init (FoPropertyScalingMethodClass *klass)
106 {
107   GObjectClass *object_class = G_OBJECT_CLASS (klass);
108   FoPropertyClass *property_class = FO_PROPERTY_CLASS (klass);
109 
110   parent_class = g_type_class_peek_parent (klass);
111 
112   object_class->finalize = fo_property_scaling_method_finalize;
113 
114   property_class->is_inherited = FALSE;
115   property_class->is_shorthand = FALSE;
116   property_class->resolve_enum =
117     fo_property_scaling_method_resolve_enum;
118   property_class->validate =
119     fo_property_scaling_method_validate;
120   property_class->get_initial =
121     fo_property_scaling_method_get_initial;
122 }
123 
124 /**
125  * fo_property_scaling_method_finalize:
126  * @object: #FoPropertyScalingMethod object to finalize.
127  *
128  * Implements #GObjectFinalizeFunc for #FoPropertyScalingMethod.
129  **/
130 void
fo_property_scaling_method_finalize(GObject * object)131 fo_property_scaling_method_finalize (GObject *object)
132 {
133   FoPropertyScalingMethod *scaling_method;
134 
135   scaling_method = FO_PROPERTY_SCALING_METHOD (object);
136 
137   G_OBJECT_CLASS (parent_class)->finalize (object);
138 }
139 
140 
141 /**
142  * fo_property_scaling_method_new:
143  *
144  * Creates a new #FoPropertyScalingMethod initialized to default value.
145  *
146  * Return value: the new #FoPropertyScalingMethod.
147  **/
148 FoProperty*
fo_property_scaling_method_new(void)149 fo_property_scaling_method_new (void)
150 {
151   FoProperty* scaling_method;
152 
153   scaling_method =
154     FO_PROPERTY (g_object_new (fo_property_scaling_method_get_type (),
155                                NULL));
156 
157   return scaling_method;
158 }
159 
160 /**
161  * fo_property_scaling_method_resolve_enum:
162  * @token:   Token from the XML attribute value to be evaluated as an
163  *           enumeration token.
164  * @context: #FoContext object from which to possibly inherit values.
165  * @error:   Information about any error that has occurred.
166  *
167  * Compare @token against the enumeration tokens that are valid for the
168  * current FO property.  If @token is valid, returns either an #FoEnum datatype
169  * representing the enumeration token or a different datatype representing
170  * the enumeration token's resolved value.  If @token is not valid,
171  * sets @error and returns NULL.
172  *
173  * Return value: Resolved enumeration value or NULL.
174  **/
175 FoDatatype*
fo_property_scaling_method_resolve_enum(const gchar * token,FoContext * context,GError ** error)176 fo_property_scaling_method_resolve_enum (const gchar *token,
177                                          FoContext   *context,
178                                          GError     **error)
179 {
180   g_return_val_if_fail (token != NULL, NULL);
181   g_return_val_if_fail (FO_IS_CONTEXT (context), NULL);
182   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
183 
184   if ((strcmp (token, "auto") == 0) ||
185       (strcmp (token, "integer-pixels") == 0) ||
186       (strcmp (token, "resample-any-method") == 0))
187     {
188       return g_object_ref (fo_enum_factory_get_enum_by_nick (token));
189     }
190   else
191     {
192       g_set_error (error,
193 		   FO_FO_ERROR,
194 		   FO_FO_ERROR_ENUMERATION_TOKEN,
195 		   _(fo_fo_error_messages[FO_FO_ERROR_ENUMERATION_TOKEN]),
196 		   class_name,
197 		   token);
198       return NULL;
199     }
200 }
201 
202 /**
203  * fo_property_scaling_method_validate:
204  * @datatype: #FoDatatype to be validated against allowed datatypes and
205  *            values for current property.
206  * @context:  #FoContext object from which to possibly inherit values.
207  * @error:    Information about any error that has occurred.
208  *
209  * Validates @datatype against allowed values.  Returns @datatype, a
210  * replacement datatype value, or NULL if validation failed.
211  *
212  * Return value: Valid datatype value or NULL.
213  **/
214 FoDatatype*
fo_property_scaling_method_validate(FoDatatype * datatype,FoContext * context,GError ** error)215 fo_property_scaling_method_validate (FoDatatype *datatype,
216                                      FoContext  *context,
217                                      GError    **error)
218 {
219   FoDatatype *new_datatype;
220   GError     *tmp_error = NULL;
221   gchar      *token;
222 
223   g_return_val_if_fail (datatype != NULL, NULL);
224   g_return_val_if_fail (FO_IS_DATATYPE (datatype), NULL);
225   g_return_val_if_fail (context != NULL, NULL);
226   g_return_val_if_fail (FO_IS_CONTEXT (context), NULL);
227   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
228 
229   if (FO_IS_ENUM (datatype))
230     {
231       return datatype;
232     }
233   else if (FO_IS_STRING (datatype))
234     {
235       token = fo_string_get_value (datatype);
236 
237       new_datatype =
238         fo_property_scaling_method_resolve_enum (token, context, &tmp_error);
239 
240       g_object_unref (datatype);
241 
242       if (tmp_error != NULL)
243 	{
244 	  g_propagate_error (error, tmp_error);
245 	  return NULL;
246 	}
247 
248       return new_datatype;
249     }
250   else if (FO_IS_NAME (datatype))
251     {
252       token = fo_name_get_value (datatype);
253 
254       new_datatype =
255         fo_property_scaling_method_resolve_enum (token, context, &tmp_error);
256 
257       g_object_unref (datatype);
258 
259       if (tmp_error != NULL)
260 	{
261 	  g_propagate_error (error, tmp_error);
262 	  return NULL;
263 	}
264 
265       return new_datatype;
266     }
267   else
268     {
269       gchar *datatype_sprintf = fo_object_sprintf (datatype);
270 
271       g_set_error (error,
272 		   FO_FO_ERROR,
273 		   FO_FO_ERROR_DATATYPE,
274 		   _(fo_fo_error_messages[FO_FO_ERROR_DATATYPE]),
275 		   class_name,
276 		   datatype_sprintf,
277 		   g_type_name (G_TYPE_FROM_INSTANCE (datatype)));
278 
279       g_object_unref (datatype);
280 
281       g_free (datatype_sprintf);
282 
283       return NULL;
284     }
285 }
286 
287 /**
288  * fo_property_scaling_method_get_initial:
289  *
290  * Get an instance of the property with the correct initial value.
291  *
292  * Return value: An instance of the property.
293  **/
294 FoProperty*
fo_property_scaling_method_get_initial(void)295 fo_property_scaling_method_get_initial (void)
296 {
297   static FoProperty *scaling_method = NULL;
298 
299   if (scaling_method == NULL)
300     {
301       scaling_method =
302 	fo_property_scaling_method_new ();
303     }
304 
305   return scaling_method;
306 }
307