1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
|
/* Fo
* fo-property.h: FoProperty superclass of all property classes
*
* Copyright (C) 2001 Sun Microsystems
* Copyright (C) 2007-2008 Menteith Consulting Ltd
*
* See COPYING for the status of this software.
*/
#include "fo-utils.h"
#include "fo-object.h"
#include "datatype/fo-all-datatype.h"
#include "fo-context.h"
#include "fo-property-private.h"
#include "fo-property-font-size.h"
/*#include <expression-parser.h>*/
/**
* SECTION:fo-property
* @short_description: Abstract FO property type
*
* Every property is a subtype of #FoProperty.
*/
const gchar *fo_property_error_messages [] = {
N_("Property error."),
N_("Property expression evaluation failed."),
N_("Attempt to convert to enumeration value when property has no values defined."),
N_("Values of '%s' property cannot be negative: %s"),
N_("Value of property '%s' should not be zero-length.")
};
enum {
PROP_0,
PROP_VALUE,
PROP_IS_INHERITED,
PROP_IS_SHORTHAND
};
static void fo_property_base_init (FoPropertyClass *klass);
static void fo_property_base_finalize (FoPropertyClass *klass);
static void fo_property_class_init (FoPropertyClass *klass);
static void fo_property_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void fo_property_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static void fo_property_finalize (GObject *object);
static FoDatatype* fo_property_get_value_default (FoProperty *property);
static void fo_property_set_value_default (FoProperty *property,
FoDatatype *new_value);
static FoDatatype* fo_property_resolve_enum_default (const gchar *token,
FoContext *context,
GError **error);
static FoDatatype* fo_property_resolve_percent_default (gdouble percentage,
const FoDatatype *font_size,
const FoFo *fo_node,
const FoContext *context,
GError **err);
static FoProperty* fo_property_new_from_expr_default (FoPropertyClass *property_class,
const gchar *expr,
FoContext *context,
FoProperty *current_font_size,
FoFo *fo_node,
GError **error);
gchar* fo_property_sprintf (FoObject *object);
static void fo_property_debug_dump (FoObject *object,
gint depth);
static gpointer parent_class;
GQuark
fo_property_error_quark (void)
{
static GQuark quark = 0;
if (quark == 0)
quark = g_quark_from_static_string ("Property error");
return quark;
}
/**
* fo_property_get_type:
*
* Register the #FoProperty object type if not already registered and
* return its #GType value.
*
* Return value: #GType value of the #FoProperty object type.
**/
GType
fo_property_get_type (void)
{
static GType object_type = 0;
if (!object_type)
{
static const GTypeInfo object_info =
{
sizeof (FoPropertyClass),
(GBaseInitFunc) fo_property_base_init,
(GBaseFinalizeFunc) fo_property_base_finalize,
(GClassInitFunc) fo_property_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (FoProperty),
0, /* n_preallocs */
NULL, /* instance_init */
NULL /* value_table */
};
object_type = g_type_register_static (FO_TYPE_OBJECT,
"FoProperty",
&object_info,
G_TYPE_FLAG_ABSTRACT);
}
return object_type;
}
/**
* fo_property_base_init:
* @klass: #FoPropertyClass object to initialise.
*
* Implements #GBaseInitFunc for #FoPropertyClass.
**/
void
fo_property_base_init (FoPropertyClass *klass)
{
FoObjectClass *fo_object_class = FO_OBJECT_CLASS (klass);
klass->is_inherited = FALSE;
klass->is_shorthand = FALSE;
klass->expr_env_list = fo_expr_env_list_new ();
klass->new_from_expr = fo_property_new_from_expr_default;
klass->expr_eval = fo_expr_eval;
klass->set_value = fo_property_set_value_default;
klass->get_value = fo_property_get_value_default;
klass->resolve_percent = fo_property_resolve_percent_default;
fo_object_class->print_sprintf = fo_property_sprintf;
fo_object_class->debug_dump = fo_property_debug_dump;
}
/**
* fo_property_base_finalize:
* @klass: #FoPropertyClass object to finalise.
*
* Implements #GBaseFinalizeFunc for #FoPropertyClass.
**/
void
fo_property_base_finalize (FoPropertyClass *klass)
{
fo_expr_env_list_free (klass->expr_env_list);
}
/**
* fo_property_class_init:
* @klass: #FoPropertyClass object to initialise.
*
* Implements #GClassInitFunc for #FoPropertyClass.
**/
void
fo_property_class_init (FoPropertyClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
object_class->finalize = fo_property_finalize;
object_class->set_property = fo_property_set_property;
object_class->get_property = fo_property_get_property;
g_object_class_install_property (object_class,
PROP_VALUE,
g_param_spec_object ("value",
_("property value"),
_("Property value"),
FO_TYPE_DATATYPE,
G_PARAM_READWRITE));
g_object_class_install_property
(object_class,
PROP_IS_INHERITED,
g_param_spec_boolean ("is-inherited",
_("Is inherited?"),
_("Is this an inherited property?"),
TRUE,
G_PARAM_READABLE));
g_object_class_install_property
(object_class,
PROP_IS_SHORTHAND,
g_param_spec_boolean ("is-shorthand",
_("Is shorthand?"),
_("Is this a shorthand property?"),
TRUE,
G_PARAM_READABLE));
klass->resolve_enum = fo_property_resolve_enum_default;
}
/**
* fo_property_finalize:
* @object: #FoProperty object to finalize.
*
* Implements #GObjectFinalizeFunc for #FoProperty.
**/
void
fo_property_finalize (GObject *object)
{
FoProperty *property;
property = FO_PROPERTY (object);
if (property->value != NULL)
{
g_object_unref (property->value);
property->value = NULL;
}
G_OBJECT_CLASS (parent_class)->finalize (object);
}
/**
* fo_property_set_property:
* @object: #GObject whose property will be set.
* @prop_id: Property ID assigned when property registered.
* @value: New value for property.
* @pspec: Parameter specification for this property type.
*
* Implements #GObjectSetPropertyFunc for #FoProperty.
**/
void
fo_property_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
FoProperty *property;
property = FO_PROPERTY (object);
switch (prop_id)
{
case PROP_VALUE:
fo_property_set_value (property, g_value_get_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/**
* fo_property_get_property:
* @object: #GObject whose property will be retreived.
* @prop_id: Property ID assigned when property registered.
* @value: GValue to set with property value.
* @pspec: Parameter specification for this property type.
*
* Implements #GObjectGetPropertyFunc for #FoProperty.
**/
void
fo_property_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
FoProperty *property;
property = FO_PROPERTY (object);
switch (prop_id)
{
case PROP_VALUE:
g_value_set_instance (value,
fo_property_get_value (property));
break;
case PROP_IS_INHERITED:
g_value_set_boolean (value,
fo_property_is_inherited (property));
break;
case PROP_IS_SHORTHAND:
g_value_set_boolean (value,
fo_property_is_shorthand (property));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/**
* fo_property_get_value:
* @property: #FoProperty object whose value is to be retrieved.
*
* Returns the current value property value of @property.
*
* Return value: The current 'value' property value.
**/
FoDatatype*
fo_property_get_value (FoProperty *property)
{
g_return_val_if_fail (property != NULL, NULL);
g_return_val_if_fail (FO_IS_PROPERTY (property), NULL);
return FO_PROPERTY_GET_CLASS (property)->get_value (property);
}
/**
* fo_property_set_value:
* @property: #FoProperty object whose value is to be set.
* @new_value: New value for the property.
*
* Sets the 'value' property of @property.
**/
void
fo_property_set_value (FoProperty *property,
FoDatatype *new_value)
{
g_return_if_fail (property != NULL);
g_return_if_fail (FO_IS_PROPERTY (property));
FO_PROPERTY_GET_CLASS (property)->set_value (property, new_value);
}
/**
* fo_property_get_value_default:
* @property: FoProperty object whose value is to be retrieved
*
* Returns the current #value property value of @property
*
* Return value: The current #value property value
**/
FoDatatype*
fo_property_get_value_default (FoProperty *property)
{
g_return_val_if_fail (property != NULL, NULL);
g_return_val_if_fail (FO_IS_PROPERTY (property), NULL);
return property->value;
}
/**
* fo_property_set_value_default:
* @property: FoProperty object whose value is to be set
* @new_value: New value for the property
*
* Sets the #value property of @property
**/
void
fo_property_set_value_default (FoProperty *property,
FoDatatype *new_value)
{
g_return_if_fail (property != NULL);
g_return_if_fail (FO_IS_PROPERTY (property));
g_return_if_fail (new_value == NULL || FO_IS_DATATYPE (new_value));
if (new_value != NULL)
g_object_ref (G_OBJECT (new_value));
if (property->value != NULL)
g_object_unref (G_OBJECT (property->value));
property->value = new_value;
/*g_object_notify(G_OBJECT(property), "value");*/
}
/**
* fo_property_resolve_enum_default:
* @token:
* @context:
* @error:
*
*
*
* Return value:
**/
FoDatatype*
fo_property_resolve_enum_default (const gchar *token G_GNUC_UNUSED,
FoContext *context G_GNUC_UNUSED,
GError **error)
{
g_warning ("This property doesn't have any enumerated values.\n");
g_set_error (error,
FO_PROPERTY_ERROR,
FO_PROPERTY_ERROR_NO_ENUMERATION,
"%s",
fo_property_error_messages[FO_PROPERTY_ERROR_NO_ENUMERATION]);
return NULL;
}
/**
* fo_property_resolve_percent_default:
* @percentage: Percentage value to resolve.
* @font_size: Font size to use if a percentage of font-size.
* @fo_node: Current #FoFo.
* @context: #FoContext of parent #FoFo.
* @err: Indicates whether an error occurs.
*
* Resolves @percentage as a percentage of a known value which,
* depending on the property, could be the current font-size value or
* some property value of the current #FoFo or its first #FoArea.
*
* Return value: #FoDatatype representing resolved value.
**/
FoDatatype*
fo_property_resolve_percent_default (gdouble percentage,
const FoDatatype *font_size G_GNUC_UNUSED,
const FoFo *fo_node G_GNUC_UNUSED,
const FoContext *context G_GNUC_UNUSED,
GError **err G_GNUC_UNUSED)
{
FoDatatype *percentage_datatype;
percentage_datatype = fo_percentage_new ();
fo_percentage_set_value (percentage_datatype,
percentage);
return percentage_datatype;
}
/**
* fo_property_new_from_string:
* @property_class: #FoPropertyClass or subclass of property.
* @expr: Expression to be evaluated as a string.
* @context: #FoContext for inherited values.
* @current_font_size: Current font size for resolving 'em' values.
* @fo_node: The current #FoFo node.
* @error: #GError for indicating any error that occurs.
*
* Evaluate @expr and create a new instance of @property_class with
* value of @expr.
*
* Other parameters (except @error) provide context for evaluating
* @expr.
*
* Return value: A new instance of @property_class, or NULL if error.
**/
FoProperty*
fo_property_new_from_string (FoPropertyClass *property_class,
const gchar *expr,
FoContext *context,
FoProperty *current_font_size,
FoFo *fo_node G_GNUC_UNUSED,
GError **error)
{
FoProperty *property;
FoDatatype *datatype;
GError *tmp_error = NULL;
const gchar *property_name = NULL;
g_return_val_if_fail (FO_IS_PROPERTY_CLASS (property_class), NULL);
g_return_val_if_fail (expr != NULL, NULL);
g_return_val_if_fail (context != NULL, NULL);
g_return_val_if_fail (FO_IS_CONTEXT (context), NULL);
g_return_val_if_fail
(current_font_size == NULL || FO_IS_PROPERTY_FONT_SIZE (current_font_size),
NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
property_name = G_OBJECT_CLASS_NAME (property_class);
datatype = fo_string_new_with_value (expr);
datatype = property_class->validate (datatype, context, &tmp_error);
if (tmp_error != NULL)
{
g_propagate_error (error, tmp_error);
return NULL;
}
property =
(FoProperty *) g_object_new (G_OBJECT_CLASS_TYPE (property_class),
"value",
datatype,
NULL);
return property;
}
/**
* fo_property_new_from_expr_default:
* @property_class: #FoPropertyClass or subclass of property.
* @expr: Expression to be evaluated.
* @context: #FoContext for inherited values.
* @current_font_size: Current font size for resolving 'em' values.
* @fo_node: The current #FoFo node.
* @error: #GError for indicating any error that occurs.
*
* Evaluate @expr and create a new instance of @property_class with
* value of @expr.
*
* Other parameters (except @error) provide context for evaluating
* @expr.
*
* Return value: A new instance of @property_class, or NULL if error.
**/
FoProperty*
fo_property_new_from_expr_default (FoPropertyClass *property_class,
const gchar *expr,
FoContext *context,
FoProperty *current_font_size,
FoFo *fo_node,
GError **error)
{
FoProperty *property;
FoDatatype *datatype;
GError *tmp_error = NULL;
const gchar *property_name = NULL;
g_return_val_if_fail (FO_IS_PROPERTY_CLASS (property_class), NULL);
g_return_val_if_fail (expr != NULL, NULL);
g_return_val_if_fail (context != NULL, NULL);
g_return_val_if_fail (FO_IS_CONTEXT (context), NULL);
g_return_val_if_fail
(current_font_size == NULL || FO_IS_PROPERTY_FONT_SIZE (current_font_size),
NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
property_name = G_OBJECT_CLASS_NAME (property_class);
datatype =
property_class->expr_eval (expr,
property_name,
property_class->resolve_enum,
property_class->resolve_percent,
current_font_size,
fo_node,
context,
property_class->expr_env_list,
&tmp_error);
if (tmp_error != NULL)
{
g_propagate_error (error, tmp_error);
return NULL;
}
datatype = property_class->validate (datatype, context, &tmp_error);
if (tmp_error != NULL)
{
g_propagate_error (error, tmp_error);
return NULL;
}
property =
(FoProperty *) g_object_new (G_OBJECT_CLASS_TYPE (property_class),
"value",
datatype,
NULL);
return property;
}
/**
* fo_property_new_from_expr:
* @property_class: Class of property for which to evaluate expression.
* @expr: Expression to evaluate.
* @context: Current #FoContext.
* @current_font_size: Current "font-size" value
* @fo_node: Current FO
* @error: Indication of any error that occurred
*
* Evaluates @expr as a value of an instance of @property_class.
* The other parameters provide the context for evaluating @expr.
*
* Returns: New instance of @property_class, or %NULL if an
* error ocurred.
**/
FoProperty*
fo_property_new_from_expr (FoPropertyClass *property_class,
const gchar *expr,
FoContext *context,
FoProperty *current_font_size,
FoFo *fo_node,
GError **error)
{
g_return_val_if_fail (FO_IS_PROPERTY_CLASS (property_class), NULL);
if (*expr == '\0')
{
const gchar *property_name = G_OBJECT_CLASS_NAME (property_class);
g_set_error (error,
FO_PROPERTY_ERROR,
FO_PROPERTY_ERROR_ZERO_LENGTH,
fo_property_error_messages[FO_PROPERTY_ERROR_ZERO_LENGTH],
property_name);
return NULL;
}
return property_class->new_from_expr (property_class,
expr,
context,
current_font_size,
fo_node,
error);
}
/**
* fo_property_class_is_inherited:
* @property_class: #FoPropertyClass or a subclass of #FoPropertyClass.
*
* Indicates whether the XSL property represented by @property_class
* is defined in the XSL 1.0 Recommendation as an inherited property.
*
* Return value: TRUE if @property_class is inherited, FALSE if not.
**/
gboolean
fo_property_class_is_inherited (FoPropertyClass *property_class)
{
g_return_val_if_fail (FO_IS_PROPERTY_CLASS (property_class), FALSE);
return (FO_PROPERTY_CLASS (property_class)->is_inherited);
}
/**
* fo_property_is_inherited:
* @property: #FoProperty or subclass of #FoProperty
*
* Indicates whether the XSL property represented by @property is
* defined in the XSL 1.0 Recommendation as an inherited property.
*
* Return value: TRUE if @property is inherited, FALSE otherwise
**/
gboolean
fo_property_is_inherited (FoProperty *property)
{
g_return_val_if_fail (FO_IS_PROPERTY (property), FALSE);
return fo_property_class_is_inherited (FO_PROPERTY_GET_CLASS (property));
}
/**
* fo_property_class_is_shorthand:
* @property_class: #FoPropertyClass or a subclass of #FoPropertyClass.
*
* Indicates whether the XSL property represented by @property_class
* is defined in the XSL 1.0 Recommendation as a shorthand property.
*
* Return value: TRUE if @property_class is a shorthand, FALSE if not.
**/
gboolean
fo_property_class_is_shorthand (FoPropertyClass *property_class)
{
g_return_val_if_fail (FO_IS_PROPERTY_CLASS (property_class), FALSE);
return (FO_PROPERTY_CLASS (property_class)->is_shorthand);
}
/**
* fo_property_is_shorthand:
* @property: #FoProperty or a subclass of #FoProperty.
*
* Indicates whether the XSL property represented by @property is
* defined in the XSL 1.0 Recommendation as a shorthand property.
*
* Return value: TRUE if @property is a shorthand, FALSE otherwise
**/
gboolean
fo_property_is_shorthand (FoProperty *property)
{
g_return_val_if_fail (FO_IS_PROPERTY (property), FALSE);
return fo_property_class_is_shorthand (FO_PROPERTY_GET_CLASS (property));
}
/**
* fo_property_sprintf:
* @object: Object to be named
*
* Returns a string representing the property and its current value.
* The returned value should be freed when no longer needed.
*
* Return value: A newly allocated string
**/
gchar*
fo_property_sprintf (FoObject *object)
{
g_return_val_if_fail (object != NULL, NULL);
g_return_val_if_fail (FO_IS_PROPERTY (object), NULL);
return
g_strdup_printf ("%s: %s",
g_type_name (G_TYPE_FROM_INSTANCE (object)),
fo_object_sprintf (FO_PROPERTY (object)->value));
}
/**
* fo_property_debug_dump:
* @object:
* @depth:
*
*
**/
void
fo_property_debug_dump (FoObject *object, gint depth)
{
gchar *indent = g_strnfill (depth * 2, ' ');
gchar* object_sprintf;
g_return_if_fail (object != NULL);
g_return_if_fail (FO_IS_PROPERTY (object));
object_sprintf = fo_object_debug_sprintf (object);
g_log (G_LOG_DOMAIN,
G_LOG_LEVEL_DEBUG,
"%s%s",
indent,
object_sprintf);
g_free (object_sprintf);
g_free (indent);
fo_object_debug_dump (fo_property_get_value (FO_PROPERTY (object)),
depth + 2);
}
|