File: fo-length.c

package info (click to toggle)
xmlroff 0.6.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 36,456 kB
  • sloc: ansic: 178,247; xml: 109,155; sh: 8,973; makefile: 1,331; perl: 30
file content (599 lines) | stat: -rw-r--r-- 12,490 bytes parent folder | download | duplicates (5)
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
/* Fo
 * fo-length.c: Length datatype
 *
 * Copyright (C) 2001 Sun Microsystems
 * Copyright (C) 2007 Menteith Consulting Ltd
 *
 * See COPYING for the status of this software.
 */

#include "config.h"
#include "fo-utils.h"
#include "fo-datatype.h"
#include "fo-datatype-private.h"
#include "fo-numeric.h"
#include "fo-length.h"
#include "fo-length-conditional.h"
#include "fo-length-range.h"
#include "fo-space.h"

#ifndef FO_LENGTH_WIDTH_THIN
#define FO_LENGTH_WIDTH_THIN	0.5
#endif

#ifndef FO_LENGTH_WIDTH_MEDIUM
#define FO_LENGTH_WIDTH_MEDIUM	2.0
#endif

#ifndef FO_LENGTH_WIDTH_THICK
#define FO_LENGTH_WIDTH_THICK	4.0
#endif

#define FO_LENGTH_A4_WIDTH ((210.0 / 25.4) * 72.0)
#define FO_LENGTH_A4_HEIGHT ((297.0 / 25.4) * 72.0)

#ifndef PIXEL2DEVICE
#define PIXEL2DEVICE(x)		((x) * 72 / PIXELS_PER_INCH)
#endif

enum {
  PROP_0,
  PROP_VALUE
};

struct _FoLength
{
  FoDatatype parent_instance;

  gdouble value;
};

struct _FoLengthClass
{
  FoDatatypeClass parent_class;
  
  FoDatatype *length_zero;
};

static FoDatatype * fo_length_new             (void);

static void fo_length_init          (FoLength      *length);
static void fo_length_class_init    (FoLengthClass *klass);
static void fo_length_set_property  (GObject       *object,
				     guint          prop_id,
				     const GValue  *value,
				     GParamSpec    *pspec);
static void fo_length_get_property  (GObject       *object,
				     guint          prop_id,
				     GValue        *value,
				     GParamSpec    *pspec);
static void fo_length_finalize      (GObject       *object);

static FoDatatype * fo_length_copy    (FoDatatype *datatype);
static gchar*       fo_length_sprintf (FoObject   *object);
static void fo_length_set_value (FoDatatype *length,
				 gdouble new_value);

static gpointer parent_class;

static FoDatatype *width_thin = NULL;
static FoDatatype *width_medium = NULL;
static FoDatatype *width_thick = NULL;

/**
 * fo_length_get_type:
 * 
 * Register the #FoLength object type.
 * 
 * Return value: #GType value of the #FoLength object type.
 **/
GType
fo_length_get_type (void)
{
  static GType object_type = 0;

  if (!object_type)
    {
      static const GTypeInfo object_info =
      {
        sizeof (FoLengthClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) fo_length_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (FoLength),
        0,              /* n_preallocs */
        (GInstanceInitFunc) fo_length_init,
	NULL		/* value_table */
      };

      object_type = g_type_register_static (FO_TYPE_DATATYPE,
                                            "FoLength",
                                            &object_info, 0);
    }

  return object_type;
}

/**
 * fo_length_init:
 * @length: #FoLength object to initialise.
 * 
 * Implements #GInstanceInitFunc for #FoLength.
 **/
void
fo_length_init (FoLength *length)
{
  length->value = 0;
}

/**
 * fo_length_class_init:
 * @klass: #FoLengthClass object to initialise.
 * 
 * Implements #GClassInitFunc for #FoLengthClass.
 **/
void
fo_length_class_init (FoLengthClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  
  parent_class = g_type_class_peek_parent (klass);
  
  object_class->finalize = fo_length_finalize;

  object_class->set_property = fo_length_set_property;
  object_class->get_property = fo_length_get_property;

  FO_OBJECT_CLASS (klass)->print_sprintf = fo_length_sprintf;

  FO_DATATYPE_CLASS (klass)->copy = fo_length_copy;

  g_object_class_install_property (object_class,
                                   PROP_VALUE,
                                   g_param_spec_double ("value",
						       _("Value"),
						       _("Length value"),
						       -G_MAXDOUBLE,
						       G_MAXDOUBLE,
						       0.0,
						       G_PARAM_READWRITE |
						       G_PARAM_CONSTRUCT_ONLY));
}

/**
 * fo_length_finalize:
 * @object: #FoLength object to finalize.
 * 
 * Implements #GObjectFinalizeFunc for #FoLength.
 **/
void
fo_length_finalize (GObject *object)
{
  FoLength *length;

  length = FO_LENGTH (object);

  /* This was giving "assertion `object->ref_count > 0' failed" messages.
  if (width_thin != NULL)
    {
      g_object_unref (width_thin);
      width_thin = NULL;
    }

  if (width_medium != NULL)
    {
      g_object_unref (width_medium);
      width_medium = NULL;
    }

  if (width_thick != NULL)
    {
      g_object_unref (width_thick);
      width_thick = NULL;
    }
  */
  G_OBJECT_CLASS (parent_class)->finalize (object);
}

/**
 * fo_length_get_property:
 * @object:  #GObject whose property will be retrieved.
 * @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 #FoLength.
 **/
void
fo_length_get_property (GObject         *object,
                         guint            prop_id,
                         GValue          *value,
                         GParamSpec      *pspec)
{
  FoDatatype *length;

  length = FO_DATATYPE (object);

  switch (prop_id)
    {
    case PROP_VALUE:
      g_value_set_double (value, fo_length_get_value (length));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

/**
 * fo_length_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 #FoLength.
 **/
void
fo_length_set_property (GObject         *object,
                         guint            prop_id,
                         const GValue    *value,
                         GParamSpec      *pspec)
{
  FoDatatype *length;

  length = FO_DATATYPE (object);

  switch (prop_id)
    {
    case PROP_VALUE:
      fo_length_set_value (length, g_value_get_double (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

/**
 * fo_length_new:
 * 
 * Creates a new #FoLength initialized to default value.
 * 
 * Return value: The new #FoLength.
 **/
static FoDatatype*
fo_length_new (void)
{
  FoDatatype* length;

  length = FO_DATATYPE (g_object_new (fo_length_get_type (), NULL));
  
  return length;
}

/**
 * fo_length_new_with_value:
 * @value: Length of the new #FoLength.
 * 
 * Creates a new #FoLength set to @value.
 * 
 * Return value: The new #FoLength.
 **/
FoDatatype*
fo_length_new_with_value (gdouble value)
{
  FoDatatype *length = fo_length_new ();

  fo_length_set_value (length, value);

  return length;
}

/**
 * fo_length_new_from_pixels:
 * @count: Number of pixels of the new #FoLength.
 * 
 * Creates a new #FoLength set to the intrinsic length of @count pixels.
 * 
 * Return value: The new #FoLength.
 **/
FoDatatype*
fo_length_new_from_pixels (gint count)
{
  FoDatatype *length = fo_length_new ();

  fo_length_set_value (length, PIXEL2DEVICE(((gdouble) count)));

  return length;
}

/**
 * fo_length_get_value:
 * @length: #FoLength.
 * 
 * Get the value of @length.
 * 
 * Return value: Numeric value of @length.
 **/
gdouble
fo_length_get_value (FoDatatype *length)
{
  g_return_val_if_fail (length != NULL, 0);
  g_return_val_if_fail (FO_IS_LENGTH (length) ||
			FO_IS_LENGTH_RANGE (length) ||
			FO_IS_LENGTH_COND (length) ||
			FO_IS_SPACE (length), 0);

  if (FO_IS_LENGTH (length))
    return FO_LENGTH (length)->value;
  else if (FO_IS_LENGTH_COND (length))
    return fo_length_cond_get_length (length);
  else if (FO_IS_LENGTH_RANGE (length))
    return fo_length_get_value (fo_length_range_get_optimum (length));
  else if (FO_IS_SPACE (length))
    return fo_space_get_optimum (length);
  else
    {
      g_assert_not_reached ();
      return 0.0;
    }
}

/**
 * fo_length_set_value:
 * @length:    #FoLength.
 * @new_value: New value for @length.
 * 
 * Set the value of @length.
 **/
void
fo_length_set_value (FoDatatype *length,
		     gdouble new_value)
{
  g_return_if_fail (length != NULL);
  g_return_if_fail (FO_IS_LENGTH (length));

  ((FoLength *) length)->value = new_value;
  /*g_object_notify (G_OBJECT (length), "value");*/
}

/**
 * fo_length_copy:
 * @datatype: #FoLength to be copied.
 * 
 * Create a copy of @datatype.
 * 
 * Return value: New #FoLength.
 **/
FoDatatype*
fo_length_copy (FoDatatype *datatype)
{
  FoDatatype* length;

  g_return_val_if_fail (datatype != NULL, NULL);
  g_return_val_if_fail (FO_IS_LENGTH (datatype), NULL);

  length = fo_length_new ();
  FO_LENGTH (length)->value = FO_LENGTH (datatype)->value;

  return length;
}

/**
 * fo_length_sprintf:
 * @object: #FoLength to be printed.
 * 
 * Creates string representation of value of @object.
 *
 * String must be freed by caller.
 * 
 * Return value: String representation of @object.
 **/
gchar*
fo_length_sprintf (FoObject *object)
{
  g_return_val_if_fail (object != NULL, NULL);
  g_return_val_if_fail (FO_IS_LENGTH (object), NULL);

  return g_strdup_printf("%0gpt",
			 FO_LENGTH (object)->value);
}

/**
 * fo_length_get_length_minus_3pt:
 * 
 * Get an #FoLength with the well-known value of 3pt.
 * 
 * Return value: The #FoLength.
 **/
FoDatatype*
fo_length_get_length_minus_3pt (void)
{
  static FoDatatype *length = NULL;

  if (length == NULL)
    {
      length = fo_length_new_with_value (-3.0);
    }

  return length;
}

/**
 * fo_length_get_length_zero:
 * 
 * Get an #FoLength with the well-known value of 0pt.
 * 
 * Return value: The #FoLength.
 **/
FoDatatype*
fo_length_get_length_zero (void)
{
  static FoDatatype *length = NULL;

  if (length == NULL)
    {
      length = fo_length_new ();
    }

  return length;
}

/**
 * fo_length_get_length_3pt:
 * 
 * Get an #FoLength with the well-known value of 3pt.
 * 
 * Return value: The #FoLength.
 **/
FoDatatype*
fo_length_get_length_3pt (void)
{
  static FoDatatype *length = NULL;

  if (length == NULL)
    {
      length = fo_length_new_with_value (3.0);
    }

  return length;
}

/**
 * fo_length_get_length_6pt:
 * 
 * Get an #FoLength with the well-known value of 6pt.
 * 
 * Return value: The #FoLength.
 **/
FoDatatype*
fo_length_get_length_6pt (void)
{
  static FoDatatype *length = NULL;

  if (length == NULL)
    {
      length = fo_length_new_with_value (6.0);
    }

  return length;
}

/**
 * fo_length_get_length_24pt:
 * 
 * Get an #FoLength with the well-known value of 24pt.
 * 
 * Return value: The #FoLength.
 **/
FoDatatype*
fo_length_get_length_24pt (void)
{
  static FoDatatype *length = NULL;

  if (length == NULL)
    {
      length = fo_length_new_with_value (24.0);
    }

  return length;
}

/**
 * fo_length_get_length_A4_width:
 * 
 * Get an #FoLength with the well-known value of the width of an A4
 * page.
 * 
 * Return value: The #FoLength.
 **/
FoDatatype*
fo_length_get_length_A4_width (void)
{
  static FoDatatype *length = NULL;

  if (length == NULL)
    {
      length = fo_length_new_with_value (FO_LENGTH_A4_WIDTH);
    }

  return length;
}

/**
 * fo_length_get_length_A4_width:
 * 
 * Get an #FoLength with the well-known value of the width of an A4
 * page.
 * 
 * Return value: The #FoLength.
 **/
FoDatatype*
fo_length_get_length_A4_height (void)
{
  static FoDatatype *length = NULL;

  if (length == NULL)
    {
      length = fo_length_new_with_value (FO_LENGTH_A4_HEIGHT);
    }

  return length;
}

/**
 * fo_length_get_width_thin:
 * 
 * Get an #FoLength for the well-known width value of 'thin'.
 * 
 * Return value: The #FoLength.
 **/
FoDatatype*
fo_length_get_width_thin (void)
{
  if (width_thin == NULL)
    {
      width_thin = fo_length_new_with_value (FO_LENGTH_WIDTH_THIN);
    }

  return width_thin;
}

/**
 * fo_length_get_width_medium:
 * 
 * Get an #FoLength for the well-known width value of 'medium'.
 * 
 * Return value: The #FoLength.
 **/
FoDatatype*
fo_length_get_width_medium (void)
{
  if (width_medium == NULL)
    {
      width_medium = fo_length_new_with_value (FO_LENGTH_WIDTH_MEDIUM);
    }

  return width_medium;
}

/**
 * fo_length_get_width_thick:
 * 
 * Get an #FoLength for the well-known width value of 'thick'.
 * 
 * Return value: The #FoLength.
 **/
FoDatatype*
fo_length_get_width_thick (void)
{
  if (width_thick == NULL)
    {
      width_thick = fo_length_new_with_value (FO_LENGTH_WIDTH_THICK);
    }

  return width_thick;
}