Welcome to mirror list, hosted at ThFree Co, Russian Federation.

xrdp_font.c « xrdp - github.com/neutrinolabs/xrdp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6fe81d8c46c6cd5f6aace978b1c326e2120aa987 (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
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
/**
 * xrdp: A Remote Desktop Protocol server.
 *
 * Copyright (C) Jay Sorg 2004-2014
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * fonts
 */

/* fv1 files are described in fontutils/README_fv1.txt */

#if defined(HAVE_CONFIG_H)
#include <config_ac.h>
#endif

#include <ctype.h>

#include "xrdp.h"
#include "log.h"
#include "string_calls.h"

#if 0 /* not used */
static char w_char[] =
{
    0x00, 0x00, 0x00, // ........................
    0x00, 0x00, 0x00, // ........................
    0x00, 0x00, 0x00, // ........................
    0x08, 0x20, 0x80, // ....X.....X.....X.......
    0x08, 0x50, 0x80, // ....X....X.X....X.......
    0x04, 0x51, 0x00, // .....X...X.X...X........
    0x04, 0x51, 0x00, // .....X...X.X...X........
    0x04, 0x51, 0x00, // .....X...X.X...X........
    0x02, 0x8a, 0x00, // ......X.X...X.X.........
    0x02, 0x8a, 0x00, // ......X.X...X.X.........
    0x02, 0x8a, 0x00, // ......X.X...X.X.........
    0x01, 0x04, 0x00, // .......X.....X..........
    0x01, 0x04, 0x00, // .......X.....X..........
    0x00, 0x00, 0x00, // ........................
    0x00, 0x00, 0x00, // ........................
    0x00, 0x00, 0x00, // ........................
};
#endif

/*****************************************************************************/
/**
 * Parses the fv1_select configuration value to get the font to use,
 * based on the DPI of the primary monitor
 *
 * @param globals Configuration globals
 * @param dpi DPI of primary monitor. If not known, a suitable
 *            default should be passed in here.
 * @param[out] font_name Name of font to use
 * @param[in] font_name_len Length of font name buffer
 */
static void
get_font_name_from_dpi(const struct xrdp_cfg_globals *globals,
                       unsigned int dpi,
                       char *font_name, int font_name_len)
{
    int bad_selector = 0;

    font_name[0] = '\0';

    const char *fv1_select = globals->fv1_select;
    if (fv1_select == NULL || fv1_select[0] == '\0')
    {
        fv1_select = DEFAULT_FV1_SELECT;
    }

    const char *p = fv1_select;

    while (p != NULL)
    {
        /* DPI value must be next in string */
        if (!isdigit(*p))
        {
            bad_selector = 1;
            break;
        }
        unsigned int field_dpi = g_atoi(p);
        if (field_dpi <= dpi)
        {
            /* Use this font */
            p = g_strchr(p, ':');
            if (p == NULL)
            {
                bad_selector = 1;
            }
            else
            {
                ++p;
                const char *q = g_strchr(p, ',');
                if (q == NULL)
                {
                    q = p + g_strlen(p);
                }
                if (q - p > (font_name_len - 1))
                {
                    q = p + font_name_len - 1;
                }
                g_memcpy(font_name, p, q - p);
                font_name[q - p] = '\0';
            }
            break;
        }
        else
        {
            p = g_strchr(p, ',');
            if (p != NULL)
            {
                ++p;
            }
        }
    }

    if (bad_selector)
    {
        LOG(LOG_LEVEL_WARNING, "Unable to parse fv1_select configuration");
    }

    if (font_name[0] == '\0')
    {
        LOG(LOG_LEVEL_WARNING, "Loading default font " DEFAULT_FONT_NAME);
        g_snprintf(font_name, font_name_len, DEFAULT_FONT_NAME);
    }
}

/*****************************************************************************/
struct xrdp_font *
xrdp_font_create(struct xrdp_wm *wm, unsigned int dpi)
{
    struct xrdp_font *self;
    struct stream *s;
    int fd;
    int b;
    int i;
    int index;
    int datasize;
    int file_size;
    struct xrdp_font_char *f;
    const char *file_path;
    char file_path_buff[256];
    int min_descender;
    char font_name[256];
    const struct xrdp_cfg_globals *globals = &wm->xrdp_config->cfg_globals;
    LOG_DEVEL(LOG_LEVEL_TRACE, "in xrdp_font_create");

    if (dpi == 0)
    {
        LOG(LOG_LEVEL_WARNING, "No DPI value is available to find login font");
        dpi = globals->default_dpi;
        LOG(LOG_LEVEL_WARNING, "Using the default_dpi of %u", dpi);
    }
    get_font_name_from_dpi(globals, dpi, font_name, sizeof(font_name));

    if (font_name[0] == '/')
    {
        /* User specified absolute path */
        file_path = font_name;
    }
    else
    {
        g_snprintf(file_path_buff, sizeof(file_path_buff),
                   XRDP_SHARE_PATH "/%s",
                   font_name);
        file_path = file_path_buff;
    }

    if (!g_file_exist(file_path))
    {
        /* Try to fall back to the default */
        const char *default_file_path = XRDP_SHARE_PATH "/" DEFAULT_FONT_NAME;
        if (g_file_exist(default_file_path))
        {
            LOG(LOG_LEVEL_WARNING,
                "xrdp_font_create: font file [%s] does not exist - using [%s]",
                file_path, default_file_path);
            file_path = default_file_path;
        }
        else
        {
            LOG(LOG_LEVEL_ERROR,
                "xrdp_font_create: Can't load either [%s] or [%s]",
                file_path, default_file_path);
            return 0;
        }
    }

    file_size = g_file_get_size(file_path);

    if (file_size < 1)
    {
        LOG(LOG_LEVEL_ERROR, "xrdp_font_create: error reading font from file [%s]",
            file_path);
        return 0;
    }

    self = (struct xrdp_font *)g_malloc(sizeof(struct xrdp_font), 1);
    self->wm = wm;
    make_stream(s);
    init_stream(s, file_size + 1024);
    fd = g_file_open(file_path);

    if (fd != -1)
    {
        b = g_file_read(fd, s->data, file_size + 1024);
        g_file_close(fd);

        if (b > 0)
        {
            s->end = s->data + b;
            in_uint8s(s, 4);
            in_uint8a(s, self->name, 32);
            in_uint16_le(s, self->size);
            in_uint16_le(s, self->style);
            in_uint16_le(s, self->body_height);
            in_sint16_le(s, min_descender);
            in_uint8s(s, 4);
            index = 32;

            while (s_check_rem(s, 16))
            {
                f = self->font_items + index;
                in_sint16_le(s, i);
                f->width = i;
                in_sint16_le(s, i);
                f->height = i;
                in_sint16_le(s, i);
                /* Move the glyph up so there are no descenders */
                f->baseline = i + min_descender;
                in_sint16_le(s, i);
                f->offset = i;
                in_sint16_le(s, i);
                f->incby = i;
                in_uint8s(s, 6);
                datasize = FONT_DATASIZE(f);

                if (datasize < 0 || datasize > 512)
                {
                    /* shouldn't happen */
                    LOG(LOG_LEVEL_ERROR, "error in xrdp_font_create, datasize wrong "
                        "width %d, height %d, datasize %d, index %d",
                        f->width, f->height, datasize, index);
                    break;
                }

                if (datasize == 0)
                {
                    /* Allocate a single blank pixel for the glyph, so
                     * that it can be added to the glyph cache if required */
                    f->width = 1;
                    f->height = 1;
                    f->data = (char *)g_malloc(1, 1);
                }
                else if (s_check_rem(s, datasize))
                {
                    f->data = (char *)g_malloc(datasize, 0);
                    in_uint8a(s, f->data, datasize);
                }
                else
                {
                    LOG(LOG_LEVEL_ERROR, "error in xrdp_font_create");
                }
                index++;
            }

            if (self->body_height == 0 && index > 32)
            {
                /* Older font made for xrdp v0.9.x. Synthesise this
                 * value from the first glyph */
                self->body_height = -self->font_items[32].baseline + 1;
            }
        }
    }

    free_stream(s);
    /*
      self->font_items[0].offset = -4;
      self->font_items[0].baseline = -16;
      self->font_items[0].width = 24;
      self->font_items[0].height = 16;
      self->font_items[0].data = g_malloc(3 * 16, 0);
      g_memcpy(self->font_items[0].data, w_char, 3 * 16);
    */
    LOG_DEVEL(LOG_LEVEL_TRACE, "out xrdp_font_create");
    return self;
}

/*****************************************************************************/
/* free the font and all the items */
void
xrdp_font_delete(struct xrdp_font *self)
{
    int i;

    if (self == 0)
    {
        return;
    }

    for (i = 0; i < NUM_FONTS; i++)
    {
        g_free(self->font_items[i].data);
    }

    g_free(self);
}

/*****************************************************************************/
/* compare the two font items returns 1 if they match */
int
xrdp_font_item_compare(struct xrdp_font_char *font1,
                       struct xrdp_font_char *font2)
{
    int datasize;

    if (font1 == 0)
    {
        return 0;
    }

    if (font2 == 0)
    {
        return 0;
    }

    if (font1->offset != font2->offset)
    {
        return 0;
    }

    if (font1->baseline != font2->baseline)
    {
        return 0;
    }

    if (font1->width != font2->width)
    {
        return 0;
    }

    if (font1->height != font2->height)
    {
        return 0;
    }

    datasize = FONT_DATASIZE(font1);

    if (g_memcmp(font1->data, font2->data, datasize) == 0)
    {
        return 1;
    }

    return 0;
}