1 /* Fo
2  * fo-property-alignment-adjust.c: 'alignment-adjust' property
3  *
4  * Copyright (C) 2001-2006 Sun Microsystems
5  * Copyright (C) 2007-2010 Menteith Consulting Ltd
6  * Copyright (C) 2011 Mentea
7  *
8  * See COPYING for the status of this software.
9  */
10 
11 #include <string.h>
12 #include "fo-utils.h"
13 #include "fo-context.h"
14 #include "datatype/fo-datatype.h"
15 #include "property/fo-property-private.h"
16 /*
17 #include "property/fo-property-text-property.h"
18 */
19 #include "property/fo-property-font-size.h"
20 #include "property/fo-property-alignment-adjust.h"
21 
22 /**
23  * SECTION:fo-property-alignment-adjust
24  * @short_description: 'alignment-adjust' property
25  *
26  * Inherited: FALSE
27  *
28  * Shorthand: FALSE
29  *
30  * Value: auto | baseline | before-edge | text-before-edge | middle | central | after-edge | text-after-edge | ideographic | alphabetic | hanging | mathematical | &lt;percentage> | &lt;length> | inherit
31  *
32  * Initial value: auto
33  *
34  * Definition: <ulink url="http://www.w3.org/TR/xsl11/&num;alignment-adjust">http://www.w3.org/TR/xsl11/&num;alignment-adjust</ulink>
35  */
36 
37 struct _FoPropertyAlignmentAdjust
38 {
39   FoProperty parent_instance;
40 };
41 
42 struct _FoPropertyAlignmentAdjustClass
43 {
44   FoPropertyClass parent_class;
45 };
46 
47 static void _init         (FoPropertyAlignmentAdjust      *property_alignment_adjust);
48 static void _class_init   (FoPropertyAlignmentAdjustClass *klass);
49 /*
50 static void _text_property_init (FoPropertyTextPropertyIface *iface);
51 */
52 static FoDatatype * _resolve_enum (const gchar *token,
53                                    FoContext   *context,
54                                    GError     **error);
55 static FoDatatype * _validate     (FoDatatype  *datatype,
56                                    FoContext   *context,
57                                    GError     **error);
58 /*
59 static PangoAttribute * fo_property_alignment_adjust_new_attr (FoProperty *property);
60 */
61 static const gchar class_name[] = "alignment-adjust";
62 static gpointer parent_class;
63 
64 /**
65  * fo_property_alignment_adjust_get_type:
66  *
67  * Register the #FoPropertyAlignmentAdjust type if not already registered and
68  * return its #GType value.
69  *
70  * Return value: #GType of #FoPropertyAlignmentAdjust.
71  **/
72 GType
fo_property_alignment_adjust_get_type(void)73 fo_property_alignment_adjust_get_type (void)
74 {
75   static GType object_type = 0;
76 
77   if (!object_type)
78     {
79       static const GTypeInfo object_info =
80       {
81         sizeof (FoPropertyAlignmentAdjustClass),
82         NULL,           /* base_init */
83         NULL,           /* base_finalize */
84         (GClassInitFunc) _class_init,
85         NULL,           /* class_finalize */
86         NULL,           /* class_data */
87         sizeof (FoPropertyAlignmentAdjust),
88         0,              /* n_preallocs */
89         (GInstanceInitFunc) _init,
90 	NULL		/* value_table */
91       };
92       /*
93       static const GInterfaceInfo fo_property_text_property_info =
94       {
95 	(GInterfaceInitFunc) _text_property_init, / * interface_init * /
96         NULL,
97         NULL
98       };
99       */
100       object_type = g_type_register_static (FO_TYPE_PROPERTY,
101                                             class_name,
102                                             &object_info, 0);
103       /*
104       g_type_add_interface_static (object_type,
105                                    FO_TYPE_PROPERTY_TEXT_PROPERTY,
106                                    &fo_property_text_property_info);
107       */
108     }
109 
110   return object_type;
111 }
112 
113 /**
114  * _init:
115  * @alignment_adjust: #FoPropertyAlignmentAdjust object to initialise.
116  *
117  * Implements #GInstanceInitFunc for #FoPropertyAlignmentAdjust.
118  **/
119 static void
_init(FoPropertyAlignmentAdjust * alignment_adjust)120 _init (FoPropertyAlignmentAdjust *alignment_adjust)
121 {
122   FO_PROPERTY (alignment_adjust)->value =
123     g_object_ref (fo_enum_factory_get_enum_by_value (FO_ENUM_ENUM_AUTO));
124 }
125 
126 /**
127  * _class_init:
128  * @klass: #FoPropertyAlignmentAdjustClass object to initialise.
129  *
130  * Implements #GClassInitFunc for #FoPropertyAlignmentAdjustClass.
131  **/
132 static void
_class_init(FoPropertyAlignmentAdjustClass * klass)133 _class_init (FoPropertyAlignmentAdjustClass *klass)
134 {
135   FoPropertyClass *property_class = FO_PROPERTY_CLASS (klass);
136 
137   parent_class = g_type_class_peek_parent (klass);
138 
139 
140   property_class->is_inherited = FALSE;
141   property_class->is_shorthand = FALSE;
142   property_class->resolve_enum =
143     _resolve_enum;
144   property_class->validate =
145     _validate;
146   property_class->get_initial =
147     fo_property_alignment_adjust_get_initial;
148 }
149 
150 /**
151  * _text_property_init:
152  * @iface: #FoPropertyTextPropertyIFace structure for this class.
153  *
154  * Initialize #FoPropertyTextPropertyIface interface for this class.
155  **/
156 /*
157 static void
158 _text_property_init (FoPropertyTextPropertyIface *iface)
159 {
160   iface->new_attr = fo_property_alignment_adjust_new_attr;
161 }
162 */
163 
164 /**
165  * fo_property_alignment_adjust_new:
166  *
167  * Creates a new #FoPropertyAlignmentAdjust initialized to default value.
168  *
169  * Return value: the new #FoPropertyAlignmentAdjust.
170  **/
171 FoProperty*
fo_property_alignment_adjust_new(void)172 fo_property_alignment_adjust_new (void)
173 {
174   FoProperty* alignment_adjust;
175 
176   alignment_adjust =
177     FO_PROPERTY (g_object_new (fo_property_alignment_adjust_get_type (),
178                                NULL));
179 
180   return alignment_adjust;
181 }
182 
183 /**
184  * _resolve_enum:
185  * @token:   Token from the XML attribute value to be evaluated as an
186  *           enumeration token.
187  * @context: #FoContext object from which to possibly inherit values.
188  * @error:   Information about any error that has occurred.
189  *
190  * Compare @token against the enumeration tokens that are valid for the
191  * current FO property.  If @token is valid, returns either an #FoEnum datatype
192  * representing the enumeration token or a different datatype representing
193  * the enumeration token's resolved value.  If @token is not valid,
194  * sets @error and returns NULL.
195  *
196  * Return value: Resolved enumeration value or NULL.
197  **/
198 static FoDatatype *
_resolve_enum(const gchar * token,FoContext * context,GError ** error)199 _resolve_enum (const gchar *token,
200                FoContext   *context,
201                GError     **error)
202 {
203   g_return_val_if_fail (token != NULL, NULL);
204   g_return_val_if_fail (FO_IS_CONTEXT (context), NULL);
205   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
206 
207   if ((strcmp (token, "auto") == 0) ||
208       (strcmp (token, "baseline") == 0) ||
209       (strcmp (token, "before-edge") == 0) ||
210       (strcmp (token, "text-before-edge") == 0) ||
211       (strcmp (token, "middle") == 0) ||
212       (strcmp (token, "central") == 0) ||
213       (strcmp (token, "after-edge") == 0) ||
214       (strcmp (token, "text-after-edge") == 0) ||
215       (strcmp (token, "ideographic") == 0) ||
216       (strcmp (token, "alphabetic") == 0) ||
217       (strcmp (token, "hanging") == 0) ||
218       (strcmp (token, "mathematical") == 0))
219     {
220       return g_object_ref (fo_enum_factory_get_enum_by_nick (token));
221     }
222   else
223     {
224       g_set_error (error,
225 		   FO_FO_ERROR,
226 		   FO_FO_ERROR_ENUMERATION_TOKEN,
227 		   _(fo_fo_error_messages[FO_FO_ERROR_ENUMERATION_TOKEN]),
228 		   class_name,
229 		   token);
230       return NULL;
231     }
232 }
233 
234 /**
235  * _validate:
236  * @datatype: #FoDatatype to be validated against allowed datatypes and
237  *            values for current property.
238  * @context:  #FoContext object from which to possibly inherit values.
239  * @error:    Information about any error that has occurred.
240  *
241  * Validates @datatype against allowed values.  Returns @datatype, a
242  * replacement datatype value, or NULL if validation failed.
243  *
244  * Return value: Valid datatype value or NULL.
245  **/
246 FoDatatype*
_validate(FoDatatype * datatype,FoContext * context,GError ** error)247 _validate (FoDatatype *datatype,
248            FoContext  *context,
249            GError    **error)
250 {
251   FoDatatype *new_datatype;
252   GError     *tmp_error = NULL;
253   gchar      *token;
254 
255   g_return_val_if_fail (datatype != NULL, NULL);
256   g_return_val_if_fail (FO_IS_DATATYPE (datatype), NULL);
257   g_return_val_if_fail (context != NULL, NULL);
258   g_return_val_if_fail (FO_IS_CONTEXT (context), NULL);
259   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
260 
261   if (FO_IS_ENUM (datatype))
262     {
263       FoEnumEnum value = fo_enum_get_value (datatype);
264 
265       if ((value == FO_ENUM_ENUM_AUTO) ||
266           (value == FO_ENUM_ENUM_BASELINE) ||
267           (value == FO_ENUM_ENUM_BEFORE_EDGE) ||
268           (value == FO_ENUM_ENUM_TEXT_BEFORE_EDGE) ||
269           (value == FO_ENUM_ENUM_MIDDLE) ||
270           (value == FO_ENUM_ENUM_CENTRAL) ||
271           (value == FO_ENUM_ENUM_AFTER_EDGE) ||
272           (value == FO_ENUM_ENUM_TEXT_AFTER_EDGE) ||
273           (value == FO_ENUM_ENUM_IDEOGRAPHIC) ||
274           (value == FO_ENUM_ENUM_ALPHABETIC) ||
275           (value == FO_ENUM_ENUM_HANGING) ||
276           (value == FO_ENUM_ENUM_MATHEMATICAL))
277 	{
278 	  return datatype;
279 	}
280       else
281 	{
282 	  gchar *datatype_sprintf = fo_object_sprintf (datatype);
283 
284 	  g_set_error (error,
285 		       FO_FO_ERROR,
286 		       FO_FO_ERROR_ENUMERATION_TOKEN,
287 		       _(fo_fo_error_messages[FO_FO_ERROR_ENUMERATION_TOKEN]),
288 		       class_name,
289 		       datatype_sprintf,
290 		       g_type_name (G_TYPE_FROM_INSTANCE (datatype)));
291 
292 	  g_object_unref (datatype);
293 
294 	  g_free (datatype_sprintf);
295 
296 	  return NULL;
297 	}
298     }
299   else if (FO_IS_STRING (datatype))
300     {
301       token = fo_string_get_value (datatype);
302 
303       new_datatype =
304         _resolve_enum (token, context, &tmp_error);
305 
306       g_object_unref (datatype);
307 
308       fo_propagate_and_return_val_if_error (error, tmp_error, NULL);
309 
310       return new_datatype;
311     }
312   else if (FO_IS_NAME (datatype))
313     {
314       token = fo_name_get_value (datatype);
315 
316       new_datatype =
317         _resolve_enum (token, context, &tmp_error);
318 
319       g_object_unref (datatype);
320 
321       fo_propagate_and_return_val_if_error (error, tmp_error, NULL);
322 
323       return new_datatype;
324     }
325   else if (FO_IS_PERCENTAGE (datatype))
326     {
327       return datatype;
328     }
329   else if (FO_IS_LENGTH (datatype))
330     {
331       return datatype;
332     }
333   else
334     {
335       gchar *datatype_sprintf = fo_object_sprintf (datatype);
336 
337       g_set_error (error,
338 		   FO_FO_ERROR,
339 		   FO_FO_ERROR_DATATYPE,
340 		   _(fo_fo_error_messages[FO_FO_ERROR_DATATYPE]),
341 		   class_name,
342 		   datatype_sprintf,
343 		   g_type_name (G_TYPE_FROM_INSTANCE (datatype)));
344 
345       g_object_unref (datatype);
346 
347       g_free (datatype_sprintf);
348 
349       return NULL;
350     }
351 }
352 
353 /**
354  * fo_property_alignment_adjust_get_initial:
355  *
356  * Get an instance of the property with the correct initial value.
357  *
358  * Return value: An instance of the property.
359  **/
360 FoProperty*
fo_property_alignment_adjust_get_initial(void)361 fo_property_alignment_adjust_get_initial (void)
362 {
363   static FoProperty *alignment_adjust = NULL;
364 
365   if (alignment_adjust == NULL)
366     {
367       alignment_adjust =
368 	fo_property_alignment_adjust_new ();
369     }
370 
371   return alignment_adjust;
372 }
373 
374 /**
375  * fo_property_alignment_adjust_to_pango_baseline:
376  * @adjust: #FoEnumAlignmentAdjust to convert to #PangoBaseline value.
377  *
378  * Gets the #PangoBaseline value corresponding to the value of
379  * @adjust.
380  *
381  * Return value: A #PangoBaseline value, which could be
382  * #PANGO_BASELINE_INVALID.
383  **/
384 /*
385 static PangoBaseline
386 fo_property_alignment_adjust_to_pango_baseline (FoEnumEnum adjust)
387 {
388   switch (adjust)
389     {
390     case FO_ENUM_ENUM_AUTO:
391       return PANGO_BASELINE_AUTO;
392     case FO_ENUM_ENUM_BASELINE:
393       return PANGO_BASELINE_BASELINE;
394     case FO_ENUM_ENUM_IDEOGRAPHIC:
395       return PANGO_BASELINE_IDEOGRAPHIC;
396     case FO_ENUM_ENUM_ALPHABETIC:
397       return PANGO_BASELINE_ALPHABETIC;
398     case FO_ENUM_ENUM_HANGING:
399       return PANGO_BASELINE_HANGING;
400     case FO_ENUM_ENUM_MATHEMATICAL:
401       return PANGO_BASELINE_MATHEMATICAL;
402     case FO_ENUM_ENUM_CENTRAL:
403       return PANGO_BASELINE_CENTRAL;
404     case FO_ENUM_ENUM_MIDDLE:
405       return PANGO_BASELINE_MIDDLE;
406     case FO_ENUM_ENUM_TEXT_BEFORE_EDGE:
407       return PANGO_BASELINE_TEXT_BEFORE_EDGE;
408     case FO_ENUM_ENUM_TEXT_AFTER_EDGE:
409       return PANGO_BASELINE_TEXT_AFTER_EDGE;
410     case FO_ENUM_ENUM_BEFORE_EDGE:
411       return PANGO_BASELINE_BEFORE_EDGE;
412     case FO_ENUM_ENUM_AFTER_EDGE:
413       return PANGO_BASELINE_AFTER_EDGE;
414     default:
415       return PANGO_BASELINE_INVALID;
416     }
417 }
418 */
419 /**
420  * fo_property_alignment_adjust_new_attr:
421  * @property: Property from which to create a PangoAttribute
422  *
423  * Makes a new PangoAttribute representing the current property
424  *
425  * Return value: New PangoAttribute
426  **/
427 /*
428 PangoAttribute*
429 fo_property_alignment_adjust_new_attr (FoProperty *property)
430 {
431   PangoBaseline pango_alignment_adjust;
432 
433   g_return_val_if_fail (FO_IS_PROPERTY_ALIGNMENT_ADJUST (property), NULL);
434 
435   pango_alignment_adjust =
436     fo_property_alignment_adjust_to_pango_baseline (fo_enum_get_value (property->value));
437 
438   return pango_attr_alignment_adjust_new (pango_alignment_adjust);
439 }
440 */
441