File: gwyentities.c

package info (click to toggle)
gwyddion 2.62-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 51,952 kB
  • sloc: ansic: 398,486; python: 7,877; sh: 5,492; makefile: 4,723; xml: 3,883; cpp: 1,969; pascal: 418; perl: 154; ruby: 130
file content (355 lines) | stat: -rw-r--r-- 9,317 bytes parent folder | download | duplicates (3)
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
/*
 *  $Id: gwyentities.c 21684 2018-11-26 12:31:34Z yeti-dn $
 *  Copyright (C) 2003 David Necas (Yeti), Petr Klapetek.
 *  E-mail: yeti@gwyddion.net, klapetek@gwyddion.net.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 */

#include "config.h"
#include <string.h>
#include <glib-object.h>

#include <libgwyddion/gwymacros.h>
#include <libgwyddion/gwyentities.h>

static const GwyTextEntity entities[] = {
    /* Uppercase Greek letters */
    { "Alpha", "Α" },
    { "Beta", "Β" },
    { "Gamma", "Γ" },
    { "Delta", "Δ" },
    { "Epsilon", "Ε" },
    { "Zeta", "Ζ" },
    { "Eta", "Η" },
    { "Theta", "Θ" },
    { "Iota", "Ι" },
    { "Kappa", "Κ" },
    { "Lambda", "Λ" },
    { "Mu", "Μ" },
    { "Nu", "Ν" },
    { "Xi", "Ξ" },
    { "Omicron", "Ο" },
    { "Pi", "Π" },
    { "Rho", "Ρ" },
    { "Sigma", "Σ" },
    { "Tau", "Τ" },
    { "Upsilon", "Υ" },
    { "Phi", "Φ" },
    { "Chi", "Χ" },
    { "Psi", "Ψ" },
    { "Omega", "Ω" },
    /* Lowercase Greek letters */
    { "alpha", "α" },
    { "beta", "β" },
    { "gamma", "γ" },
    { "delta", "δ" },
    { "epsilon", "ε" },
    { "varepsilon", "ϵ" },
    { "zeta", "ζ" },
    { "eta", "η" },
    { "theta", "θ" },
    { "thetasym", "ϑ" },
    { "iota", "ι" },
    { "kappa", "κ" },
    { "varkappa", "ϰ" },
    { "lambda", "λ" },
    { "mu", "μ" },
    { "nu", "ν" },
    { "xi", "ξ" },
    { "omicron", "ο" },
    { "pi", "π" },
    { "rho", "ρ" },
    { "varrho", "ϱ" },
    { "sigma", "σ" },
    { "sigmaf", "ς" },
    { "tau", "τ" },
    { "upsilon", "υ" },
    { "phi", "φ" },
    { "chi", "χ" },
    { "psi", "ψ" },
    { "omega", "ω" },
    /* Math symbols */
    { "alefsym", "א" },
    { "and", "∧" },
    { "ang", "∠" },
    { "asymp", "≈" },
    { "cap", "∩" },
    { "cong", "≅" },
    { "cup", "∪" },
    { "deg", "°" },
    { "divide", "÷" },
    { "empty", "∅" },
    { "equiv", "≡" },
    { "exist", "∃" },
    { "fnof", "ƒ" },
    { "forall", "∀" },
    { "ge", "≥" },
    { "image", "ℑ" },
    { "infin", "∞" },
    { "int", "∫" },
    { "isin", "∈" },
    { "lArr", "⇐" },
    { "lang", "〈" },
    { "larr", "←" },
    { "le", "≤" },
    { "lfloor", "⌊" },
    { "micro", "µ" },
    { "middot", "·" },
    { "minus", "−" },
    { "nabla", "∇" },
    { "ne", "≠" },
    { "ni", "∋" },
    { "not", "¬" },
    { "notin", "∉" },
    { "nsub", "⊄" },
    { "oplus", "⊕" },
    { "or", "∨" },
    { "otimes", "⊗" },
    { "part", "∂" },
    { "permil", "‰" },
    { "perp", "⊥" },
    { "piv", "ϖ" },
    { "plusmn", "±" },
    { "prod", "∏" },
    { "prop", "∝" },
    { "rArr", "⇒" },
    { "radic", "√" },
    { "rang", "〉" },
    { "rarr", "→" },
    { "rceil", "⌉" },
    { "real", "ℜ" },
    { "rfloor", "⌋" },
    { "sdot", "⋅" },
    { "sim", "∼" },
    { "sub", "⊂" },
    { "sube", "⊆" },
    { "sum", "∑" },
    { "sup", "⊃" },
    { "there4", "∴" },
    { "times", "×" },
    { "weierp", "℘" },
    /* Text stuff */
    { "bull", "•" },
    { "cent", "¢" },
    { "circ", "ˆ" },
    { "copy", "©" },
    { "curren", "¤" },
    { "Dagger", "‡" },
    { "dagger", "†" },
    { "euro", "€" },
    { "hellip", "…" },
    { "pound", "£" },
    { "reg", "®" },
    { "sect", "§" },
    { "trade", "™" },
    { "yen", "¥" },
};

static const gchar*
gwy_entity_get_name(gpointer item)
{
    return ((const GwyTextEntity*)item)->entity;
}

static const GType*
gwy_entity_get_traits(gint *ntraits)
{
    static const GType traits[] = { G_TYPE_STRING, G_TYPE_STRING };

    if (ntraits)
        *ntraits = G_N_ELEMENTS(traits);

    return traits;
}

static const gchar*
gwy_entity_get_trait_name(gint i)
{
    static const gchar *trait_names[] = { "entity", "utf8" };

    g_return_val_if_fail(i >= 0 && i < G_N_ELEMENTS(trait_names), NULL);
    return trait_names[i];
}

static void
gwy_entity_get_trait_value(gpointer item,
                           gint i,
                           GValue *value)
{
    switch (i) {
        case 0:
        g_value_init(value, G_TYPE_STRING);
        g_value_set_static_string(value, ((const GwyTextEntity*)item)->entity);
        break;

        case 1:
        g_value_init(value, G_TYPE_STRING);
        g_value_set_static_string(value, ((const GwyTextEntity*)item)->utf8);
        break;

        default:
        g_return_if_reached();
        break;
    }
}

static gpointer
ensure_entity_inventory(G_GNUC_UNUSED gpointer arg)
{
    static const GwyInventoryItemType gwy_entity_item_type = {
        0,
        NULL,
        NULL,
        gwy_entity_get_name,
        NULL,
        NULL,
        NULL,
        NULL,
        gwy_entity_get_traits,
        gwy_entity_get_trait_name,
        gwy_entity_get_trait_value,
    };

    return gwy_inventory_new_from_array(&gwy_entity_item_type,
                                        sizeof(GwyTextEntity),
                                        G_N_ELEMENTS(entities),
                                        entities);
}

/**
 * gwy_entities:
 *
 * Returns a constant inventory with all available entities.
 *
 * Returns: The entities as a #GwyInventory.
 **/
GwyInventory*
gwy_entities(void)
{
    static GOnce inventory_once = G_ONCE_INIT;
    return (GwyInventory*)g_once(&inventory_once, ensure_entity_inventory,
                                 NULL);
}

/**
 * gwy_entities_entity_to_utf8:
 * @entity: A single entity name, as a nul-delimited string.
 *
 * Converts a single named entity @entity to UTF-8 representation.
 *
 * The string passed to this function should be a bare entity name, i.e. it
 * should not contain the ampersand and semicolon.
 *
 * Returns: @entity if the name was not recognized, or a valid UTF-8 string.
 *          If the returned string is not equal to @entities, it's owned by
 *          entities and must not be freed nor modified.
 **/
const gchar*
gwy_entities_entity_to_utf8(const gchar *entity)
{
    const GwyTextEntity *ent;

    ent = gwy_inventory_get_item(gwy_entities(), entity);
    return ent ? ent->utf8 : entity;
}

/**
 * gwy_entities_text_to_utf8:
 * @text: A nul-delimited string.
 *
 * Converts entities in a text to UTF-8.
 *
 * Returns: A newly allocated nul-delimited string containing the converted
 *          text.
 **/
gchar*
gwy_entities_text_to_utf8(const gchar *text)
{
    gchar *result, *pos, *amp;
    const gchar *scln, *end, *ent;
    gint i;

    g_return_val_if_fail(text, NULL);

    i = strlen(text) + 1;
    end = text + i - 1;
    /* XXX: we hope no entity -> UTF-8 conversion can lead to a longer string */
    result = g_new(gchar, i);
    pos = result;

    /* if there's no semicolon, the text can't contain entities */
    while ((scln = strchr(text, ';'))) {
        /* find _corresponding_ ampersand.
         * this means the nearest preceeding ampersand.
         * if there's no ampersand, the text can't contain entities */
        i = scln - text;
        memcpy(pos, text, i + 1);
        pos += i;
        text += i + 1;
        for (amp = pos - 1; i; i--)
            if (*(amp--) == '&')
                break;
        amp++;
        if (*amp == '&') {
            /* now there's a possible entity starting at amp
             * and ending at pos */
            *pos = '\0';
            ent = gwy_entities_entity_to_utf8(amp + 1);
            if (ent == amp + 1) {
                /* no entity */
                *(pos++) = ';';
            }
            else {
                /* entity found */
                strcpy(amp, ent);
                pos = amp + strlen(ent);
            }
        }
        else
            pos++;
    }

    memcpy(pos, text, end - text + 1);
    return result;
}

/************************** Documentation ****************************/

/**
 * SECTION:gwyentities
 * @title: gwyentities
 * @short_description: Transform SGML-like symbol entities to UTF-8
 *
 * A subset of named SGML-like symbol entities (e.g.
 * <literal>&amp;alpha;</literal>), to be used namely in #GwySciText.
 *
 * Function gwy_entities_entity_to_utf8() converts a signle entity to UTF-8.
 * Function gwy_entities_text_to_utf8() converts a text containing entities to
 * UTF-8. An #GwyInventory with all available entities can be obtained with
 * gwy_entities().
 **/

/**
 * GwyTextEntity:
 * @entity: Bare entity name, without the leading ampersand and trailing
 *          semicolon.
 * @utf8: The corresponding character, in UTF-8.
 *
 * The type of text entity data.
 **/

/* vim: set cin et ts=4 sw=4 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */