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 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
|
/* Fo
* fo-space.c: Space datatype
*
* Copyright (C) 2001 Sun Microsystems
* Copyright (C) 2007 Menteith Consulting Ltd
*
* See COPYING for the status of this software.
*/
#include "fo-utils.h"
#include "fo-datatype.h"
#include "fo-datatype-private.h"
#include "fo-space.h"
#include "fo-length.h"
#include "fo-integer.h"
#include "fo-percentage.h"
#include "fo-boolean.h"
enum {
PROP_0,
PROP_MINIMUM,
PROP_OPTIMUM,
PROP_MAXIMUM,
PROP_PRECEDENCE,
PROP_CONDITY
};
struct _FoSpace
{
FoDatatype parent_instance;
gfloat minimum;
gfloat optimum;
gfloat maximum;
gint precedence;
gboolean retain;
};
struct _FoSpaceClass
{
FoDatatypeClass parent_class;
};
static void fo_space_init (FoSpace *space);
static void fo_space_class_init (FoSpaceClass *klass);
static void fo_space_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void fo_space_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static void fo_space_finalize (GObject *object);
gchar* fo_space_sprintf (FoObject *object);
FoDatatype* fo_space_copy (FoDatatype *datatype);
static gpointer parent_class;
/**
* fo_space_get_type:
* @void:
*
* Register the #FoSpace object type.
*
* Return value: GType value of the #FoSpace object type.
**/
GType
fo_space_get_type (void)
{
static GType object_type = 0;
if (!object_type)
{
static const GTypeInfo object_info =
{
sizeof (FoSpaceClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) fo_space_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (FoSpace),
0, /* n_preallocs */
(GInstanceInitFunc) fo_space_init,
NULL /* value_table */
};
object_type = g_type_register_static (FO_TYPE_DATATYPE,
"FoSpace",
&object_info, 0);
}
return object_type;
}
/**
* fo_space_init:
* @space: #FoSpace object to initialise.
*
* Implements #GInstanceInitFunc for #FoSpace.
**/
void
fo_space_init (FoSpace *space)
{
space->minimum = 0.0;
space->optimum = 0.0;
space->maximum = 0.0;
space->precedence = 0;
space->retain = FALSE;
}
/**
* fo_space_class_init:
* @klass: FoSpaceClass object to initialise
*
* Implements GClassInitFunc for FoSpaceClass
**/
void
fo_space_class_init (FoSpaceClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
object_class->finalize = fo_space_finalize;
object_class->set_property = fo_space_set_property;
object_class->get_property = fo_space_get_property;
FO_OBJECT_CLASS (klass)->print_sprintf = fo_space_sprintf;
g_object_class_install_property (object_class,
PROP_MINIMUM,
g_param_spec_float ("minimum",
_("Minimum"),
_("Space range minimum value"),
-G_MAXFLOAT,
G_MAXFLOAT,
0,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_OPTIMUM,
g_param_spec_float ("optimum",
_("Optimum"),
_("Space range optimum value"),
-G_MAXFLOAT,
G_MAXFLOAT,
0,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_MAXIMUM,
g_param_spec_float ("maximum",
_("Maximum"),
_("Space range maximum value"),
-G_MAXFLOAT,
G_MAXFLOAT,
0,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_PRECEDENCE,
g_param_spec_int ("precedence",
_("Precedence"),
_("Precedence of this space compared to others"),
G_MININT,
G_MAXINT,
0,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_CONDITY,
g_param_spec_boolean ("conditionality",
_("Conditionality"),
_("Whether or not the space must always be retained"),
FALSE,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY));
FO_DATATYPE_CLASS (klass)->copy = fo_space_copy;
}
/**
* fo_space_finalize:
* @object: FoSpace object to finalize
*
* Implements GObjectFinalizeFunc for FoSpace
**/
void
fo_space_finalize (GObject *object)
{
FoSpace *space;
space = FO_SPACE (object);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
/**
* fo_space_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 FoSpace
**/
void
fo_space_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
FoDatatype *datatype;
datatype = FO_DATATYPE (object);
switch (prop_id)
{
case PROP_MINIMUM:
g_value_set_float (value, fo_space_get_minimum (datatype));
break;
case PROP_OPTIMUM:
g_value_set_float (value, fo_space_get_optimum (datatype));
break;
case PROP_MAXIMUM:
g_value_set_float (value, fo_space_get_maximum (datatype));
break;
case PROP_PRECEDENCE:
g_value_set_int (value, fo_space_get_precedence (datatype));
break;
case PROP_CONDITY:
g_value_set_boolean (value, fo_space_get_condity (datatype));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/**
* fo_space_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 FoSpace
**/
void
fo_space_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
FoDatatype *space;
space = FO_DATATYPE (object);
switch (prop_id)
{
case PROP_MINIMUM:
fo_space_set_minimum (space, g_value_get_float (value));
break;
case PROP_OPTIMUM:
fo_space_set_optimum (space, g_value_get_float (value));
break;
case PROP_MAXIMUM:
fo_space_set_maximum (space, g_value_get_float (value));
break;
case PROP_PRECEDENCE:
fo_space_set_precedence (space, g_value_get_int (value));
break;
case PROP_CONDITY:
fo_space_set_condity (space, g_value_get_boolean (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/**
* fo_space_new:
*
* Creates a new #FoSpace initialized to default value.
*
* Return value: the new #FoSpace
**/
FoDatatype *
fo_space_new (void)
{
FoDatatype *space;
space = FO_DATATYPE (g_object_new (fo_space_get_type (),
NULL));
return space;
}
/**
* fo_space_new_from_length:
* @length: #FoLength to use as initial value
*
* Creates a new #FoSpace with .minimum, .optimum, and .maximum
* components set to @length and .conditionality and .precedence
* components set to default values.
*
* Return value: New #FoSpace
**/
FoDatatype*
fo_space_new_from_length (FoDatatype *length)
{
FoDatatype *space;
gfloat length_val;
g_return_val_if_fail (FO_IS_LENGTH (length), NULL);
length_val = fo_length_get_value (length);
space = fo_space_new ();
fo_space_set_minimum (space, length_val);
fo_space_set_optimum (space, length_val);
fo_space_set_maximum (space, length_val);
return space;
}
/**
* fo_space_set_minimum:
* @datatype: #FoSpace
* @new_minimum: New .minimum value
*
* Sets the .minimum component of @datatype
**/
void
fo_space_set_minimum (FoDatatype *datatype,
gfloat new_minimum)
{
g_return_if_fail (datatype != NULL);
g_return_if_fail (FO_IS_SPACE (datatype));
FO_SPACE (datatype)->minimum = new_minimum;
/*g_object_notify(G_OBJECT(datatype), "minimum");*/
}
/**
* fo_space_get_minimum:
* @datatype: #FoSpace
*
* Gets the .minimum component value of @datatype
*
* Return value: The .minimum value of @datatype
**/
gfloat
fo_space_get_minimum (FoDatatype *datatype)
{
g_return_val_if_fail (datatype != NULL, 0);
g_return_val_if_fail (FO_IS_SPACE (datatype), 0);
return FO_SPACE (datatype)->minimum;
}
/**
* fo_space_set_optimum:
* @datatype: #FoSpace
* @new_optimum: New .optimum value
*
* Sets the .optimum component of @datatype
**/
void
fo_space_set_optimum (FoDatatype *datatype,
gfloat new_optimum)
{
g_return_if_fail (datatype != NULL);
g_return_if_fail (FO_IS_SPACE (datatype));
FO_SPACE (datatype)->optimum = new_optimum;
/*g_object_notify(G_OBJECT(datatype), "optimum");*/
}
/**
* fo_space_get_optimum:
* @datatype: #FoSpace
*
* Gets the .optimum component value of @datatype
*
* Return value: The .optimum value of @datatype
**/
gfloat
fo_space_get_optimum (FoDatatype *datatype)
{
g_return_val_if_fail (datatype != NULL, 0);
g_return_val_if_fail (FO_IS_SPACE (datatype), 0);
return FO_SPACE (datatype)->optimum;
}
/**
* fo_space_set_maximum:
* @datatype: #FoSpace
* @new_maximum: New .maximum value
*
* Sets the .maximum component of @datatype
**/
void
fo_space_set_maximum (FoDatatype *datatype,
gfloat new_maximum)
{
g_return_if_fail (datatype != NULL);
g_return_if_fail (FO_IS_SPACE (datatype));
FO_SPACE (datatype)->maximum = new_maximum;
/*g_object_notify(G_OBJECT(datatype), "maximum");*/
}
/**
* fo_space_get_maximum:
* @datatype: #FoSpace
*
* Gets the .maximum component value of @datatype
*
* Return value: The .maximum value of @datatype
**/
gfloat
fo_space_get_maximum (FoDatatype *datatype)
{
g_return_val_if_fail (datatype != NULL, 0);
g_return_val_if_fail (FO_IS_SPACE (datatype), 0);
return FO_SPACE (datatype)->maximum;
}
/**
* fo_space_set_precedence:
* @datatype: #FoSpace
* @new_precedence: New .precedence value
*
* Sets the .precedence component of @datatype
**/
void
fo_space_set_precedence (FoDatatype *datatype,
gint new_precedence)
{
g_return_if_fail (datatype != NULL);
g_return_if_fail (FO_IS_SPACE (datatype));
FO_SPACE (datatype)->precedence = new_precedence;
/*g_object_notify(G_OBJECT(datatype), "precedence");*/
}
/**
* fo_space_get_precedence:
* @datatype: #FoSpace
*
* Gets the .precedence component value of @datatype
*
* Return value: The .precedence value of @datatype
**/
gint
fo_space_get_precedence (FoDatatype *datatype)
{
g_return_val_if_fail (datatype != NULL, 0);
g_return_val_if_fail (FO_IS_SPACE (datatype), 0);
return FO_SPACE (datatype)->precedence;
}
/**
* fo_space_set_condity:
* @datatype: #FoSpace.
* @new_condity: New .conditionality component value.
*
* Sets the .conditionality component value of @datatype
**/
void
fo_space_set_condity (FoDatatype *datatype,
gboolean new_condity)
{
g_return_if_fail (datatype != NULL);
g_return_if_fail (FO_IS_SPACE (datatype));
FO_SPACE (datatype)->retain = new_condity;
/*g_object_notify(G_OBJECT(datatype), "conditionality");*/
}
/**
* fo_space_get_condity:
* @datatype: #FoSpace
*
* Gets the .conditionality component value of @datatype
*
* Return value: The .conditionality value of @datatype
**/
gboolean
fo_space_get_condity (FoDatatype *datatype)
{
g_return_val_if_fail (datatype != NULL, 0);
g_return_val_if_fail (FO_IS_SPACE (datatype), 0);
return FO_SPACE (datatype)->retain;
}
/**
* fo_space_sprintf:
* @object: #FoSpace whose value is to be printed
*
* Create and return a string representation of the value of @object,
* which must be an #FoSpace.
*
* String must be freed by caller.
*
* Return value: String representation of value of @object
**/
gchar*
fo_space_sprintf (FoObject *object)
{
g_return_val_if_fail (object != NULL, NULL);
g_return_val_if_fail (FO_IS_SPACE (object), NULL);
return
g_strdup_printf ("min: %0gpt; opt: %0gpt; max: %0gpt; prec: %d; cond: %s",
FO_SPACE (object)->minimum,
FO_SPACE (object)->optimum,
FO_SPACE (object)->maximum,
FO_SPACE (object)->precedence,
FO_SPACE (object)->retain ? "retain" : "discard");
}
/**
* fo_space_copy:
* @datatype: Source #FoSpace
*
* Creates a copy of @datatype
*
* Return value: Copy of @datatype
**/
FoDatatype*
fo_space_copy (FoDatatype *datatype)
{
FoDatatype *space;
g_return_val_if_fail (datatype != NULL, NULL);
g_return_val_if_fail (FO_IS_SPACE (datatype), NULL);
space = fo_space_new ();
FO_SPACE (space)->minimum = FO_SPACE (datatype)->minimum;
FO_SPACE (space)->optimum = FO_SPACE (datatype)->optimum;
FO_SPACE (space)->maximum = FO_SPACE (datatype)->maximum;
FO_SPACE (space)->precedence = FO_SPACE (datatype)->precedence;
FO_SPACE (space)->retain = FO_SPACE (datatype)->retain;
return space;
}
/**
* fo_space_get_space_0pt:
*
* Create and return an #FoSpace with the well-known value 0pt.
*
* This saves creation of multiple #FoSpace objects that all have the
* value 0pt.
*
* Return value: #FoSpace with value 0pt.
**/
FoDatatype*
fo_space_get_space_0pt (void)
{
static FoDatatype *space = NULL;
if (!space)
{
space = fo_space_new ();
}
return space;
}
/**
* fo_space_resolve:
* @shortform: Single-value short form of the space, or NULL
* @minimum: .minimum component of the space, or NULL
* @optimum: .optimum component of the space, or NULL
* @maximum: .maximum component of the space, or NULL
* @precedence: .precedence component of the space, or NULL
* @condity: .conditionality component of the space, or NULL
* @hundred_percent: Length to use as 100% when components expressed as
* percentages
* @error: GError used for reporting errors
*
* Resolve the shortform and components of the space in accordance
* with Section 5.11, Datatypes, of the XSL 1.0 Recommendation.
*
* Return value: Compound space datatype, or NULL if an error occurred
**/
FoDatatype*
fo_space_resolve (FoDatatype *shortform,
FoDatatype *minimum,
FoDatatype *optimum,
FoDatatype *maximum,
FoDatatype *precedence,
FoDatatype *condity,
gfloat hundred_percent,
GError **error G_GNUC_UNUSED)
{
FoDatatype *use_space = NULL;
gfloat use_shortform = 0.0;
gfloat use_optimum = 0.0;
g_return_val_if_fail (shortform == NULL ||
FO_IS_LENGTH (shortform) ||
FO_IS_PERCENTAGE (shortform) ||
FO_IS_SPACE (shortform),
NULL);
g_return_val_if_fail (minimum == NULL ||
FO_IS_LENGTH (minimum) ||
FO_IS_PERCENTAGE (minimum),
NULL);
g_return_val_if_fail (optimum == NULL ||
FO_IS_LENGTH (optimum) ||
FO_IS_PERCENTAGE (optimum),
NULL);
g_return_val_if_fail (maximum == NULL ||
FO_IS_LENGTH (maximum) ||
FO_IS_PERCENTAGE (maximum),
NULL);
g_return_val_if_fail (precedence == NULL || FO_IS_INTEGER (precedence),
NULL);
g_return_val_if_fail (condity == NULL ||
FO_IS_BOOLEAN (condity), NULL);
if (FO_IS_SPACE (shortform) &&
minimum == NULL &&
optimum == NULL &&
maximum == NULL &&
precedence == NULL &&
condity == NULL)
{
return fo_space_copy (shortform);
}
if (shortform == NULL)
{
use_space = fo_space_new ();
}
else if (FO_IS_SPACE (shortform))
{
use_space = fo_space_copy (shortform);
}
else if (FO_IS_PERCENTAGE (shortform))
{
use_shortform =
fo_percentage_get_value (shortform) * 0.01 *
hundred_percent;
use_space = fo_space_new ();
fo_space_set_minimum (use_space, use_shortform);
fo_space_set_optimum (use_space, use_shortform);
fo_space_set_maximum (use_space, use_shortform);
}
else if (FO_IS_LENGTH (shortform))
{
use_shortform = fo_length_get_value (shortform);
use_space = fo_space_new ();
fo_space_set_minimum (use_space, use_shortform);
fo_space_set_optimum (use_space, use_shortform);
fo_space_set_maximum (use_space, use_shortform);
}
else
{
g_assert_not_reached ();
}
if (minimum != NULL)
{
if (FO_IS_LENGTH (minimum))
{
fo_space_set_minimum (use_space, fo_length_get_value (minimum));
}
else if (FO_IS_PERCENTAGE (minimum))
{
fo_space_set_minimum (use_space,
fo_percentage_get_value (minimum) * 0.01 *
hundred_percent);
}
else
{
g_assert_not_reached ();
}
}
if (maximum != NULL)
{
if (FO_IS_LENGTH (maximum))
{
fo_space_set_maximum (use_space, fo_length_get_value (maximum));
}
else if (FO_IS_PERCENTAGE (maximum))
{
fo_space_set_maximum (use_space,
fo_percentage_get_value (maximum) * 0.01 *
hundred_percent);
}
else
{
g_assert_not_reached ();
}
}
if (optimum != NULL)
{
if (FO_IS_LENGTH (optimum))
{
use_optimum = fo_length_get_value (optimum);
}
else if (FO_IS_PERCENTAGE (optimum))
{
use_optimum =
fo_percentage_get_value (optimum) * 0.01 *
hundred_percent;
}
else
{
g_assert_not_reached ();
}
fo_space_set_minimum (use_space,
MIN (fo_space_get_minimum (use_space),
use_optimum));
fo_space_set_optimum (use_space, use_optimum);
fo_space_set_maximum (use_space,
MAX (fo_space_get_maximum (use_space),
use_optimum));
}
if (precedence != NULL)
{
fo_space_set_precedence (use_space,
fo_integer_get_value (precedence));
}
if (condity != NULL)
{
fo_space_set_condity (use_space, fo_boolean_get_value (condity));
}
return use_space;
}
|