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
|
/*
* Copyright (C) Yves Arrouye <Yves.Arrouye@marin.fdn.fr>, 1996.
*
* Use under the GPL version 2. You are not allowed to remove this
* copyright notice.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include "dimen.h"
static struct {
const char* name;
float factor;
} units[] = {
{ "in", 1. },
{ "ft", 12. },
{ "pt", 1. / 72. },
{ "m", 100. / 2.54 },
{ "dm", 10. / 2.54 },
{ "cm", 1. / 2.54 },
{ "mm", .1 / 2.54 },
{ 0 }
};
float unitfactor(const char* unit)
{
int i;
for (i = 0; units[i].name; ++i) {
if (!strcasecmp(units[i].name, unit)) {
return units[i].factor;
}
}
return 0;
}
int psdimension(const char* what, int* dim)
{
const char* unit;
int dot = 0;
if (!what || !*what) return -1;
if (*(unit = what) == '-') ++unit;
for (; isdigit(*unit) || (*unit == '.' && !dot++); ++unit);
if (*unit && !isalpha(*unit)) {
return -1;
} else {
double base = atof(what);
double factor = unitfactor(unit);
if (factor) {
*dim = base * factor * 72;
return 0;
}
return 1;
}
}
|