summaryrefslogtreecommitdiffstats
path: root/libgimp/gimpunitcache.c
blob: 95bca08768b0b037b29eda02a22ab3cf0031c5f5 (plain)
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
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * gimpunitcache.c
 * Copyright (C) 1999-2000 Michael Natterer <mitch@gimp.org>
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <gio/gio.h>

#include "libgimpbase/gimpbase.h"

#include "gimpunitcache.h"
#include "gimpunit_pdb.h"

#include "libgimp-intl.h"

/*  internal structures  */

typedef struct
{
  gdouble      factor;
  gint         digits;
  const gchar *identifier;
  const gchar *symbol;
  const gchar *abbreviation;
  const gchar *singular;
  const gchar *plural;
} GimpUnitDef;


static GimpUnitDef * gimp_unit_defs         = NULL;
static GimpUnit      gimp_units_initialized = 0;

/*  not a unit at all but kept here to have the strings in one place
 */
static const GimpUnitDef gimp_unit_percent =
{
  0.0, 0, "percent", "%", "%",  N_("percent"), N_("percent")
};


static void  gimp_unit_def_init (GimpUnitDef *unit_def,
                                 GimpUnit     unit);


static gboolean
gimp_unit_init (GimpUnit unit)
{
  gint i, n;

  if (unit < gimp_units_initialized)
    return TRUE;

  n = _gimp_unit_get_number_of_units ();

  if (unit >= n)
    return FALSE;

  gimp_unit_defs = g_renew (GimpUnitDef, gimp_unit_defs, n);

  for (i = gimp_units_initialized; i < n; i++)
    {
      gimp_unit_def_init (&gimp_unit_defs[i], i);
    }

  gimp_units_initialized = n;

  return TRUE;
}

static void
gimp_unit_def_init (GimpUnitDef *unit_def,
                    GimpUnit     unit)
{
  unit_def->factor       = _gimp_unit_get_factor (unit);
  unit_def->digits       = _gimp_unit_get_digits (unit);
  unit_def->identifier   = _gimp_unit_get_identifier (unit);
  unit_def->symbol       = _gimp_unit_get_symbol (unit);
  unit_def->abbreviation = _gimp_unit_get_abbreviation (unit);
  unit_def->singular     = _gimp_unit_get_singular (unit);
  unit_def->plural       = _gimp_unit_get_plural (unit);
}

gint
_gimp_unit_cache_get_number_of_units (void)
{
  return _gimp_unit_get_number_of_units ();
}

gint
_gimp_unit_cache_get_number_of_built_in_units (void)
{
  return GIMP_UNIT_END;
}

GimpUnit
_gimp_unit_cache_new (gchar   *identifier,
                      gdouble  factor,
                      gint     digits,
                      gchar   *symbol,
                      gchar   *abbreviation,
                      gchar   *singular,
                      gchar   *plural)
{
  return _gimp_unit_new (identifier,
                         factor,
                         digits,
                         symbol,
                         abbreviation,
                         singular,
                         plural);
}

gboolean
_gimp_unit_cache_get_deletion_flag (GimpUnit unit)
{
  if (unit < GIMP_UNIT_END)
    return FALSE;

  return _gimp_unit_get_deletion_flag (unit);
}

void
_gimp_unit_cache_set_deletion_flag (GimpUnit unit,
                                    gboolean deletion_flag)
{
  if (unit < GIMP_UNIT_END)
    return;

  _gimp_unit_set_deletion_flag (unit,
                                deletion_flag);
}

gdouble
_gimp_unit_cache_get_factor (GimpUnit unit)
{
  g_return_val_if_fail (unit >= GIMP_UNIT_INCH, 1.0);

  if (unit == GIMP_UNIT_PERCENT)
    return gimp_unit_percent.factor;

  if (!gimp_unit_init (unit))
    return 1.0;

  return gimp_unit_defs[unit].factor;
}

gint
_gimp_unit_cache_get_digits (GimpUnit unit)
{
  g_return_val_if_fail (unit >= GIMP_UNIT_INCH, 0);

  if (unit == GIMP_UNIT_PERCENT)
    return gimp_unit_percent.digits;

  if (!gimp_unit_init (unit))
    return 0;

  return gimp_unit_defs[unit].digits;
}

const gchar *
_gimp_unit_cache_get_identifier (GimpUnit unit)
{
  if (unit == GIMP_UNIT_PERCENT)
    return gimp_unit_percent.identifier;

  if (!gimp_unit_init (unit))
    return NULL;

  return gimp_unit_defs[unit].identifier;
}

const gchar *
_gimp_unit_cache_get_symbol (GimpUnit unit)
{
  if (unit == GIMP_UNIT_PERCENT)
    return gimp_unit_percent.symbol;

  if (!gimp_unit_init (unit))
    return NULL;

  return gimp_unit_defs[unit].symbol;
}

const gchar *
_gimp_unit_cache_get_abbreviation (GimpUnit unit)
{
  if (unit == GIMP_UNIT_PERCENT)
    return gimp_unit_percent.abbreviation;

  if (!gimp_unit_init (unit))
    return NULL;

  return gimp_unit_defs[unit].abbreviation;
}

const gchar *
_gimp_unit_cache_get_singular (GimpUnit unit)
{
  if (unit == GIMP_UNIT_PERCENT)
    return gettext (gimp_unit_percent.singular);

  if (!gimp_unit_init (unit))
    return NULL;

  return gettext (gimp_unit_defs[unit].singular);
}

const gchar *
_gimp_unit_cache_get_plural (GimpUnit unit)
{
  if (unit == GIMP_UNIT_PERCENT)
    return gettext (gimp_unit_percent.plural);

  if (!gimp_unit_init (unit))
    return NULL;

  return gettext (gimp_unit_defs[unit].plural);
}