Subversion Repositories Scribus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
14952 fschmid 1
/* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
2
/* cairo - a vector graphics library with display and print output
3
 *
4
 * Copyright © 2005 Red Hat, Inc
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it either under the terms of the GNU Lesser General Public
8
 * License version 2.1 as published by the Free Software Foundation
9
 * (the "LGPL") or, at your option, under the terms of the Mozilla
10
 * Public License Version 1.1 (the "MPL"). If you do not alter this
11
 * notice, a recipient may use your version of this file under either
12
 * the MPL or the LGPL.
13
 *
14
 * You should have received a copy of the LGPL along with this library
15
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
 * You should have received a copy of the MPL along with this library
18
 * in the file COPYING-MPL-1.1
19
 *
20
 * The contents of this file are subject to the Mozilla Public License
21
 * Version 1.1 (the "License"); you may not use this file except in
22
 * compliance with the License. You may obtain a copy of the License at
23
 * http://www.mozilla.org/MPL/
24
 *
25
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27
 * the specific language governing rights and limitations.
28
 *
29
 * The Original Code is the cairo graphics library.
30
 *
31
 * The Initial Developer of the Original Code is Red Hat, Inc.
32
 *
33
 * Contributor(s):
34
 */
35
 
36
#define WIN32_LEAN_AND_MEAN
37
/* We require Windows 2000 features such as GetGlyphIndices */
38
#if !defined(WINVER) || (WINVER < 0x0500)
39
# define WINVER 0x0500
40
#endif
41
#if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500)
42
# define _WIN32_WINNT 0x0500
43
#endif
44
 
45
#include "cairoint.h"
46
 
47
#include "cairo-win32-private.h"
48
 
49
#ifndef SPI_GETFONTSMOOTHINGTYPE
50
#define SPI_GETFONTSMOOTHINGTYPE 0x200a
51
#endif
52
#ifndef FE_FONTSMOOTHINGCLEARTYPE
53
#define FE_FONTSMOOTHINGCLEARTYPE 2
54
#endif
55
#ifndef CLEARTYPE_QUALITY
56
#define CLEARTYPE_QUALITY 5
57
#endif
58
#ifndef TT_PRIM_CSPLINE
59
#define TT_PRIM_CSPLINE 3
60
#endif
61
 
62
#define CMAP_TAG 0x70616d63
63
 
64
const cairo_scaled_font_backend_t _cairo_win32_scaled_font_backend;
65
 
66
typedef struct {
67
    cairo_scaled_font_t base;
68
 
69
    LOGFONTW logfont;
70
 
71
    BYTE quality;
72
 
73
    /* We do drawing and metrics computation in a "logical space" which
74
     * is similar to font space, except that it is scaled by a factor
75
     * of the (desired font size) * (WIN32_FONT_LOGICAL_SCALE). The multiplication
76
     * by WIN32_FONT_LOGICAL_SCALE allows for sub-pixel precision.
77
     */
78
    double logical_scale;
79
 
80
    /* The size we should actually request the font at from Windows; differs
81
     * from the logical_scale because it is quantized for orthogonal
82
     * transformations
83
     */
84
    double logical_size;
85
 
86
    /* Transformations from device <=> logical space
87
     */
88
    cairo_matrix_t logical_to_device;
89
    cairo_matrix_t device_to_logical;
90
 
91
    /* We special case combinations of 90-degree-rotations, scales and
92
     * flips ... that is transformations that take the axes to the
93
     * axes. If preserve_axes is true, then swap_axes/swap_x/swap_y
94
     * encode the 8 possibilities for orientation (4 rotation angles with
95
     * and without a flip), and scale_x, scale_y the scale components.
96
     */
97
    cairo_bool_t preserve_axes;
98
    cairo_bool_t swap_axes;
99
    cairo_bool_t swap_x;
100
    cairo_bool_t swap_y;
101
    double x_scale;
102
    double y_scale;
103
 
104
    /* The size of the design unit of the font
105
     */
106
    int em_square;
107
 
108
    HFONT scaled_hfont;
109
    HFONT unscaled_hfont;
110
 
111
    cairo_bool_t is_bitmap;
112
    cairo_bool_t is_type1;
113
    cairo_bool_t delete_scaled_hfont;
114
} cairo_win32_scaled_font_t;
115
 
116
static cairo_status_t
117
_cairo_win32_scaled_font_set_metrics (cairo_win32_scaled_font_t *scaled_font);
118
 
119
static cairo_status_t
120
_cairo_win32_scaled_font_init_glyph_metrics (cairo_win32_scaled_font_t *scaled_font,
121
                                             cairo_scaled_glyph_t      *scaled_glyph);
122
 
123
static cairo_status_t
124
_cairo_win32_scaled_font_init_glyph_surface (cairo_win32_scaled_font_t *scaled_font,
125
                                             cairo_scaled_glyph_t      *scaled_glyph);
126
 
127
static cairo_status_t
128
_cairo_win32_scaled_font_init_glyph_path (cairo_win32_scaled_font_t *scaled_font,
129
                                          cairo_scaled_glyph_t      *scaled_glyph);
130
 
131
#define NEARLY_ZERO(d) (fabs(d) < (1. / 65536.))
132
 
133
static HDC
134
_get_global_font_dc (void)
135
{
136
    static HDC hdc;
137
 
138
    if (!hdc) {
139
        hdc = CreateCompatibleDC (NULL);
140
        if (!hdc) {
141
            _cairo_win32_print_gdi_error ("_get_global_font_dc");
142
            return NULL;
143
        }
144
 
145
        if (!SetGraphicsMode (hdc, GM_ADVANCED)) {
146
            _cairo_win32_print_gdi_error ("_get_global_font_dc");
147
            DeleteDC (hdc);
148
            return NULL;
149
        }
150
    }
151
 
152
    return hdc;
153
}
154
 
155
static cairo_status_t
156
_compute_transform (cairo_win32_scaled_font_t *scaled_font,
157
                    cairo_matrix_t            *sc)
158
{
159
    cairo_status_t status;
160
 
161
    if (NEARLY_ZERO (sc->yx) && NEARLY_ZERO (sc->xy) &&
162
            !NEARLY_ZERO(sc->xx) && !NEARLY_ZERO(sc->yy)) {
163
        scaled_font->preserve_axes = TRUE;
164
        scaled_font->x_scale = sc->xx;
165
        scaled_font->swap_x = (sc->xx < 0);
166
        scaled_font->y_scale = sc->yy;
167
        scaled_font->swap_y = (sc->yy < 0);
168
        scaled_font->swap_axes = FALSE;
169
 
170
    } else if (NEARLY_ZERO (sc->xx) && NEARLY_ZERO (sc->yy) &&
171
            !NEARLY_ZERO(sc->yx) && !NEARLY_ZERO(sc->xy)) {
172
        scaled_font->preserve_axes = TRUE;
173
        scaled_font->x_scale = sc->yx;
174
        scaled_font->swap_x = (sc->yx < 0);
175
        scaled_font->y_scale = sc->xy;
176
        scaled_font->swap_y = (sc->xy < 0);
177
        scaled_font->swap_axes = TRUE;
178
 
179
    } else {
180
        scaled_font->preserve_axes = FALSE;
181
        scaled_font->swap_x = scaled_font->swap_y = scaled_font->swap_axes = FALSE;
182
    }
183
 
184
    if (scaled_font->preserve_axes) {
185
        if (scaled_font->swap_x)
186
            scaled_font->x_scale = - scaled_font->x_scale;
187
        if (scaled_font->swap_y)
188
            scaled_font->y_scale = - scaled_font->y_scale;
189
 
190
        scaled_font->logical_scale = WIN32_FONT_LOGICAL_SCALE * scaled_font->y_scale;
191
        scaled_font->logical_size = WIN32_FONT_LOGICAL_SCALE *
192
                                    _cairo_lround (scaled_font->y_scale);
193
    }
194
 
195
    /* The font matrix has x and y "scale" components which we extract and
196
     * use as character scale values.
197
     */
198
    cairo_matrix_init (&scaled_font->logical_to_device,
199
                       sc->xx, sc->yx, sc->xy, sc->yy, 0, 0);
200
 
201
    if (!scaled_font->preserve_axes) {
202
        status = _cairo_matrix_compute_basis_scale_factors (&scaled_font->logical_to_device,
203
                                                      &scaled_font->x_scale, &scaled_font->y_scale,
204
                                                      TRUE);    /* XXX: Handle vertical text */
205
        if (status)
206
            return status;
207
 
208
        scaled_font->logical_size = _cairo_lround (WIN32_FONT_LOGICAL_SCALE *
209
                                                   scaled_font->y_scale);
210
        scaled_font->logical_scale = WIN32_FONT_LOGICAL_SCALE * scaled_font->y_scale;
211
    }
212
 
213
    cairo_matrix_scale (&scaled_font->logical_to_device,
214
                        1.0 / scaled_font->logical_scale, 1.0 / scaled_font->logical_scale);
215
 
216
    scaled_font->device_to_logical = scaled_font->logical_to_device;
217
 
218
    status = cairo_matrix_invert (&scaled_font->device_to_logical);
219
    if (status)
220
        cairo_matrix_init_identity (&scaled_font->device_to_logical);
221
 
222
    return CAIRO_STATUS_SUCCESS;
223
}
224
 
225
static cairo_bool_t
226
_have_cleartype_quality (void)
227
{
228
    OSVERSIONINFO version_info;
229
 
230
    version_info.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
231
 
232
    if (!GetVersionEx (&version_info)) {
233
        _cairo_win32_print_gdi_error ("_have_cleartype_quality");
234
        return FALSE;
235
    }
236
 
237
    return (version_info.dwMajorVersion > 5 ||
238
            (version_info.dwMajorVersion == 5 &&
239
             version_info.dwMinorVersion >= 1));        /* XP or newer */
240
}
241
 
242
static BYTE
243
_get_system_quality (void)
244
{
245
    BOOL font_smoothing;
246
    UINT smoothing_type;
247
 
248
    if (!SystemParametersInfo (SPI_GETFONTSMOOTHING, 0, &font_smoothing, 0)) {
249
        _cairo_win32_print_gdi_error ("_get_system_quality");
250
        return DEFAULT_QUALITY;
251
    }
252
 
253
    if (font_smoothing) {
254
        if (_have_cleartype_quality ()) {
255
            if (!SystemParametersInfo (SPI_GETFONTSMOOTHINGTYPE,
256
                                       0, &smoothing_type, 0)) {
257
                _cairo_win32_print_gdi_error ("_get_system_quality");
258
                return DEFAULT_QUALITY;
259
            }
260
 
261
            if (smoothing_type == FE_FONTSMOOTHINGCLEARTYPE)
262
                return CLEARTYPE_QUALITY;
263
        }
264
 
265
        return ANTIALIASED_QUALITY;
266
    } else {
267
        return DEFAULT_QUALITY;
268
    }
269
}
270
 
271
/* If face_hfont is non-%NULL then font_matrix must be a simple scale by some
272
 * factor S, ctm must be the identity, logfont->lfHeight must be -S,
273
 * logfont->lfWidth, logfont->lfEscapement, logfont->lfOrientation must
274
 * all be 0, and face_hfont is the result of calling CreateFontIndirectW on
275
 * logfont.
276
 */
277
static cairo_status_t
278
_win32_scaled_font_create (LOGFONTW                   *logfont,
279
                           HFONT                      face_hfont,
280
                           cairo_font_face_t          *font_face,
281
                           const cairo_matrix_t       *font_matrix,
282
                           const cairo_matrix_t       *ctm,
283
                           const cairo_font_options_t *options,
284
                           cairo_scaled_font_t       **font_out)
285
{
286
    HDC hdc;
287
    cairo_win32_scaled_font_t *f;
288
    cairo_matrix_t scale;
289
    cairo_status_t status;
290
 
291
    hdc = _get_global_font_dc ();
292
    if (hdc == NULL)
293
        return _cairo_error (CAIRO_STATUS_NO_MEMORY);
294
 
295
    f = malloc (sizeof(cairo_win32_scaled_font_t));
296
    if (f == NULL)
297
        return _cairo_error (CAIRO_STATUS_NO_MEMORY);
298
 
299
    f->logfont = *logfont;
300
 
301
    /* We don't have any control over the hinting style or subpixel
302
     * order in the Win32 font API, so we ignore those parts of
303
     * cairo_font_options_t. We use the 'antialias' field to set
304
     * the 'quality'.
305
     *
306
     * XXX: The other option we could pay attention to, but don't
307
     *      here is the hint_metrics options.
308
     */
309
    if (options->antialias == CAIRO_ANTIALIAS_DEFAULT)
310
        f->quality = _get_system_quality ();
311
    else {
312
        switch (options->antialias) {
313
        case CAIRO_ANTIALIAS_NONE:
314
            f->quality = NONANTIALIASED_QUALITY;
315
            break;
316
        case CAIRO_ANTIALIAS_GRAY:
317
            f->quality = ANTIALIASED_QUALITY;
318
            break;
319
        case CAIRO_ANTIALIAS_SUBPIXEL:
320
            if (_have_cleartype_quality ())
321
                f->quality = CLEARTYPE_QUALITY;
322
            else
323
                f->quality = ANTIALIASED_QUALITY;
324
            break;
325
        case CAIRO_ANTIALIAS_DEFAULT:
326
            ASSERT_NOT_REACHED;
327
        }
328
    }
329
 
330
    f->em_square = 0;
331
    f->scaled_hfont = NULL;
332
    f->unscaled_hfont = NULL;
333
 
334
    if (f->quality == logfont->lfQuality ||
335
        (logfont->lfQuality == DEFAULT_QUALITY &&
336
         options->antialias == CAIRO_ANTIALIAS_DEFAULT)) {
337
        /* If face_hfont is non-NULL, then we can use it to avoid creating our
338
         * own --- because the constraints on face_hfont mentioned above
339
         * guarantee it was created in exactly the same way that
340
         * _win32_scaled_font_get_scaled_hfont would create it.
341
         */
342
        f->scaled_hfont = face_hfont;
343
    }
344
    /* don't delete the hfont if we're using the one passed in to us */
345
    f->delete_scaled_hfont = !f->scaled_hfont;
346
 
347
    cairo_matrix_multiply (&scale, font_matrix, ctm);
348
    status = _compute_transform (f, &scale);
349
    if (status)
350
        goto FAIL;
351
 
352
    status = _cairo_scaled_font_init (&f->base, font_face,
353
                                      font_matrix, ctm, options,
354
                                      &_cairo_win32_scaled_font_backend);
355
    if (status)
356
        goto FAIL;
357
 
358
    status = _cairo_win32_scaled_font_set_metrics (f);
359
    if (status) {
360
        _cairo_scaled_font_fini (&f->base);
361
        goto FAIL;
362
    }
363
 
364
    *font_out = &f->base;
365
    return CAIRO_STATUS_SUCCESS;
366
 
367
 FAIL:
368
    free (f);
369
    return status;
370
}
371
 
372
static cairo_status_t
373
_win32_scaled_font_set_world_transform (cairo_win32_scaled_font_t *scaled_font,
374
                                        HDC                        hdc)
375
{
376
    XFORM xform;
377
 
378
    _cairo_matrix_to_win32_xform (&scaled_font->logical_to_device, &xform);
379
 
380
    if (!SetWorldTransform (hdc, &xform))
381
        return _cairo_win32_print_gdi_error ("_win32_scaled_font_set_world_transform");
382
 
383
    return CAIRO_STATUS_SUCCESS;
384
}
385
 
386
static cairo_status_t
387
_win32_scaled_font_set_identity_transform (HDC hdc)
388
{
389
    if (!ModifyWorldTransform (hdc, NULL, MWT_IDENTITY))
390
        return _cairo_win32_print_gdi_error ("_win32_scaled_font_set_identity_transform");
391
 
392
    return CAIRO_STATUS_SUCCESS;
393
}
394
 
395
static cairo_status_t
396
_win32_scaled_font_get_scaled_hfont (cairo_win32_scaled_font_t *scaled_font,
397
                                     HFONT *hfont_out)
398
{
399
    if (!scaled_font->scaled_hfont) {
400
        LOGFONTW logfont = scaled_font->logfont;
401
        logfont.lfHeight = -scaled_font->logical_size;
402
        logfont.lfWidth = 0;
403
        logfont.lfEscapement = 0;
404
        logfont.lfOrientation = 0;
405
        logfont.lfQuality = scaled_font->quality;
406
 
407
        scaled_font->scaled_hfont = CreateFontIndirectW (&logfont);
408
        if (!scaled_font->scaled_hfont)
409
            return _cairo_win32_print_gdi_error ("_win32_scaled_font_get_scaled_hfont");
410
    }
411
 
412
    *hfont_out = scaled_font->scaled_hfont;
413
    return CAIRO_STATUS_SUCCESS;
414
}
415
 
416
static cairo_status_t
417
_win32_scaled_font_get_unscaled_hfont (cairo_win32_scaled_font_t *scaled_font,
418
                                       HDC                        hdc,
419
                                       HFONT                     *hfont_out)
420
{
421
    if (scaled_font->unscaled_hfont == NULL) {
422
        OUTLINETEXTMETRIC *otm;
423
        unsigned int otm_size;
424
        HFONT scaled_hfont;
425
        LOGFONTW logfont;
426
        cairo_status_t status;
427
 
428
        status = _win32_scaled_font_get_scaled_hfont (scaled_font,
429
                                                      &scaled_hfont);
430
        if (status)
431
            return status;
432
 
433
        if (! SelectObject (hdc, scaled_hfont))
434
            return _cairo_win32_print_gdi_error ("_win32_scaled_font_get_unscaled_hfont:SelectObject");
435
 
436
        otm_size = GetOutlineTextMetrics (hdc, 0, NULL);
437
        if (! otm_size)
438
            return _cairo_win32_print_gdi_error ("_win32_scaled_font_get_unscaled_hfont:GetOutlineTextMetrics");
439
 
440
        otm = malloc (otm_size);
441
        if (otm == NULL)
442
            return _cairo_error (CAIRO_STATUS_NO_MEMORY);
443
 
444
        if (! GetOutlineTextMetrics (hdc, otm_size, otm)) {
445
            status = _cairo_win32_print_gdi_error ("_win32_scaled_font_get_unscaled_hfont:GetOutlineTextMetrics");
446
            free (otm);
447
            return status;
448
        }
449
 
450
        scaled_font->em_square = otm->otmEMSquare;
451
        free (otm);
452
 
453
        logfont = scaled_font->logfont;
454
        logfont.lfHeight = -scaled_font->em_square;
455
        logfont.lfWidth = 0;
456
        logfont.lfEscapement = 0;
457
        logfont.lfOrientation = 0;
458
        logfont.lfQuality = scaled_font->quality;
459
 
460
        scaled_font->unscaled_hfont = CreateFontIndirectW (&logfont);
461
        if (! scaled_font->unscaled_hfont)
462
            return _cairo_win32_print_gdi_error ("_win32_scaled_font_get_unscaled_hfont:CreateIndirect");
463
    }
464
 
465
    *hfont_out = scaled_font->unscaled_hfont;
466
    return CAIRO_STATUS_SUCCESS;
467
}
468
 
469
static cairo_status_t
470
_cairo_win32_scaled_font_select_unscaled_font (cairo_scaled_font_t *scaled_font,
471
                                               HDC                  hdc)
472
{
473
    cairo_status_t status;
474
    HFONT hfont;
475
    HFONT old_hfont = NULL;
476
 
477
    status = _win32_scaled_font_get_unscaled_hfont ((cairo_win32_scaled_font_t *)scaled_font, hdc, &hfont);
478
    if (status)
479
        return status;
480
 
481
    old_hfont = SelectObject (hdc, hfont);
482
    if (!old_hfont)
483
        return _cairo_win32_print_gdi_error ("_cairo_win32_scaled_font_select_unscaled_font");
484
 
485
    status = _win32_scaled_font_set_identity_transform (hdc);
486
    if (status) {
487
        SelectObject (hdc, old_hfont);
488
        return status;
489
    }
490
 
491
    SetMapMode (hdc, MM_TEXT);
492
 
493
    return CAIRO_STATUS_SUCCESS;
494
}
495
 
496
cairo_bool_t
497
_cairo_win32_scaled_font_is_type1 (cairo_scaled_font_t *scaled_font)
498
{
499
    cairo_win32_scaled_font_t *win32_scaled_font;
500
 
501
    win32_scaled_font = (cairo_win32_scaled_font_t *) scaled_font;
502
 
503
    return win32_scaled_font->is_type1;
504
}
505
 
506
cairo_bool_t
507
_cairo_win32_scaled_font_is_bitmap (cairo_scaled_font_t *scaled_font)
508
{
509
    cairo_win32_scaled_font_t *win32_scaled_font;
510
 
511
    win32_scaled_font = (cairo_win32_scaled_font_t *) scaled_font;
512
 
513
    return win32_scaled_font->is_bitmap;
514
}
515
 
516
static void
517
_cairo_win32_scaled_font_done_unscaled_font (cairo_scaled_font_t *scaled_font)
518
{
519
}
520
 
521
/* implement the font backend interface */
522
 
523
static cairo_status_t
524
_cairo_win32_font_face_create_for_toy (cairo_toy_font_face_t   *toy_face,
525
                                       cairo_font_face_t      **font_face)
526
{
527
    LOGFONTW logfont;
528
    uint16_t *face_name;
529
    int face_name_len;
530
    cairo_status_t status;
531
 
532
    status = _cairo_utf8_to_utf16 (toy_face->family, -1,
533
                                   &face_name, &face_name_len);
534
    if (status)
535
        return status;
536
 
537
    if (face_name_len > LF_FACESIZE - 1)
538
        face_name_len = LF_FACESIZE - 1;
539
 
540
    memcpy (logfont.lfFaceName, face_name, sizeof (uint16_t) * face_name_len);
541
    logfont.lfFaceName[face_name_len] = 0;
542
    free (face_name);
543
 
544
    logfont.lfHeight = 0;       /* filled in later */
545
    logfont.lfWidth = 0;        /* filled in later */
546
    logfont.lfEscapement = 0;   /* filled in later */
547
    logfont.lfOrientation = 0;  /* filled in later */
548
 
549
    switch (toy_face->weight) {
550
    case CAIRO_FONT_WEIGHT_NORMAL:
551
    default:
552
        logfont.lfWeight = FW_NORMAL;
553
        break;
554
    case CAIRO_FONT_WEIGHT_BOLD:
555
        logfont.lfWeight = FW_BOLD;
556
        break;
557
    }
558
 
559
    switch (toy_face->slant) {
560
    case CAIRO_FONT_SLANT_NORMAL:
561
    default:
562
        logfont.lfItalic = FALSE;
563
        break;
564
    case CAIRO_FONT_SLANT_ITALIC:
565
    case CAIRO_FONT_SLANT_OBLIQUE:
566
        logfont.lfItalic = TRUE;
567
        break;
568
    }
569
 
570
    logfont.lfUnderline = FALSE;
571
    logfont.lfStrikeOut = FALSE;
572
    /* The docs for LOGFONT discourage using this, since the
573
     * interpretation is locale-specific, but it's not clear what
574
     * would be a better alternative.
575
     */
576
    logfont.lfCharSet = DEFAULT_CHARSET;
577
    logfont.lfOutPrecision = OUT_DEFAULT_PRECIS;
578
    logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
579
    logfont.lfQuality = DEFAULT_QUALITY; /* filled in later */
580
    logfont.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
581
 
582
    *font_face = cairo_win32_font_face_create_for_logfontw (&logfont);
583
 
584
    return CAIRO_STATUS_SUCCESS;
585
}
586
 
587
static void
588
_cairo_win32_scaled_font_fini (void *abstract_font)
589
{
590
    cairo_win32_scaled_font_t *scaled_font = abstract_font;
591
 
592
    if (scaled_font == NULL)
593
        return;
594
 
595
    if (scaled_font->scaled_hfont && scaled_font->delete_scaled_hfont)
596
        DeleteObject (scaled_font->scaled_hfont);
597
 
598
    if (scaled_font->unscaled_hfont)
599
        DeleteObject (scaled_font->unscaled_hfont);
600
}
601
 
602
static cairo_int_status_t
603
_cairo_win32_scaled_font_type1_text_to_glyphs (cairo_win32_scaled_font_t *scaled_font,
604
                                               double                     x,
605
                                               double                     y,
606
                                               const char                *utf8,
607
                                               cairo_glyph_t            **glyphs,
608
                                               int                       *num_glyphs)
609
{
610
    uint16_t *utf16;
611
    int n16;
612
    int i;
613
    WORD *glyph_indices = NULL;
614
    cairo_status_t status;
615
    double x_pos, y_pos;
616
    HDC hdc = NULL;
617
    cairo_matrix_t mat;
618
 
619
    status = _cairo_utf8_to_utf16 (utf8, -1, &utf16, &n16);
620
    if (status)
621
        return status;
622
 
623
    glyph_indices = _cairo_malloc_ab (n16 + 1, sizeof (WORD));
624
    if (!glyph_indices) {
625
        status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
626
        goto FAIL1;
627
    }
628
 
629
    hdc = _get_global_font_dc ();
630
    assert (hdc != NULL);
631
 
632
    status = cairo_win32_scaled_font_select_font (&scaled_font->base, hdc);
633
    if (status)
634
        goto FAIL2;
635
 
636
    if (GetGlyphIndicesW (hdc, utf16, n16, glyph_indices, 0) == GDI_ERROR) {
637
        status = _cairo_win32_print_gdi_error ("_cairo_win32_scaled_font_type1_text_to_glyphs:GetGlyphIndicesW");
638
        goto FAIL3;
639
    }
640
 
641
    *num_glyphs = n16;
642
    *glyphs = _cairo_malloc_ab (n16, sizeof (cairo_glyph_t));
643
    if (!*glyphs) {
644
        status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
645
        goto FAIL3;
646
    }
647
 
648
    x_pos = x;
649
    y_pos = y;
650
 
651
    mat = scaled_font->base.ctm;
652
    status = cairo_matrix_invert (&mat);
653
    assert (status == CAIRO_STATUS_SUCCESS);
654
 
655
    _cairo_scaled_font_freeze_cache (&scaled_font->base);
656
 
657
    for (i = 0; i < n16; i++) {
658
        cairo_scaled_glyph_t *scaled_glyph;
659
 
660
        (*glyphs)[i].index = glyph_indices[i];
661
        (*glyphs)[i].x = x_pos;
662
        (*glyphs)[i].y = y_pos;
663
 
664
        status = _cairo_scaled_glyph_lookup (&scaled_font->base,
665
                                             glyph_indices[i],
666
                                             CAIRO_SCALED_GLYPH_INFO_METRICS,
667
                                             &scaled_glyph);
668
        if (status) {
669
            free (*glyphs);
670
            *glyphs = NULL;
671
            break;
672
        }
673
 
674
        x = scaled_glyph->x_advance;
675
        y = scaled_glyph->y_advance;
676
        cairo_matrix_transform_distance (&mat, &x, &y);
677
        x_pos += x;
678
        y_pos += y;
679
    }
680
 
681
    _cairo_scaled_font_thaw_cache (&scaled_font->base);
682
 
683
FAIL3:
684
    cairo_win32_scaled_font_done_font (&scaled_font->base);
685
FAIL2:
686
    free (glyph_indices);
687
FAIL1:
688
    free (utf16);
689
 
690
    return status;
691
}
692
 
693
static cairo_int_status_t
694
_cairo_win32_scaled_font_text_to_glyphs (void           *abstract_font,
695
                                         double         x,
696
                                         double         y,
697
                                         const char     *utf8,
698
                                         cairo_glyph_t **glyphs,
699
                                         int            *num_glyphs)
700
{
701
    cairo_win32_scaled_font_t *scaled_font = abstract_font;
702
    uint16_t *utf16;
703
    int n16;
704
    GCP_RESULTSW gcp_results;
705
    unsigned int buffer_size, i;
706
    WCHAR *glyph_indices = NULL;
707
    int *dx = NULL;
708
    cairo_status_t status;
709
    double x_pos, y_pos;
710
    double x_incr, y_incr;
711
    HDC hdc = NULL;
712
 
713
    /* GetCharacterPlacement() returns utf16 instead of glyph indices
714
     * for Type 1 fonts. Use GetGlyphIndices for Type 1 fonts. */
715
    if (scaled_font->is_type1)
716
         return _cairo_win32_scaled_font_type1_text_to_glyphs (scaled_font,
717
                                                               x,
718
                                                               y,
719
                                                               utf8,
720
                                                               glyphs,
721
                                                               num_glyphs);
722
 
723
    /* Compute a vector in user space along the baseline of length one logical space unit */
724
    x_incr = 1;
725
    y_incr = 0;
726
    cairo_matrix_transform_distance (&scaled_font->base.font_matrix, &x_incr, &y_incr);
727
    x_incr /= scaled_font->logical_scale;
728
    y_incr /= scaled_font->logical_scale;
729
 
730
    status = _cairo_utf8_to_utf16 (utf8, -1, &utf16, &n16);
731
    if (status)
732
        return status;
733
 
734
    gcp_results.lStructSize = sizeof (GCP_RESULTS);
735
    gcp_results.lpOutString = NULL;
736
    gcp_results.lpOrder = NULL;
737
    gcp_results.lpCaretPos = NULL;
738
    gcp_results.lpClass = NULL;
739
 
740
    buffer_size = MAX (n16 * 1.2, 16);          /* Initially guess number of chars plus a few */
741
    if (buffer_size > INT_MAX) {
742
        status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
743
        goto FAIL1;
744
    }
745
 
746
    hdc = _get_global_font_dc ();
747
    assert (hdc != NULL);
748
 
749
    status = cairo_win32_scaled_font_select_font (&scaled_font->base, hdc);
750
    if (status)
751
        goto FAIL1;
752
 
753
    while (TRUE) {
754
        if (glyph_indices) {
755
            free (glyph_indices);
756
            glyph_indices = NULL;
757
        }
758
        if (dx) {
759
            free (dx);
760
            dx = NULL;
761
        }
762
 
763
        glyph_indices = _cairo_malloc_ab (buffer_size, sizeof (WCHAR));
764
        dx = _cairo_malloc_ab (buffer_size, sizeof (int));
765
        if (!glyph_indices || !dx) {
766
            status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
767
            goto FAIL2;
768
        }
769
 
770
        gcp_results.nGlyphs = buffer_size;
771
        gcp_results.lpDx = dx;
772
        gcp_results.lpGlyphs = glyph_indices;
773
 
774
        if (!GetCharacterPlacementW (hdc, utf16, n16,
775
                                     0,
776
                                     &gcp_results,
777
                                     GCP_DIACRITIC | GCP_LIGATE | GCP_GLYPHSHAPE | GCP_REORDER)) {
778
            status = _cairo_win32_print_gdi_error ("_cairo_win32_scaled_font_text_to_glyphs");
779
            goto FAIL2;
780
        }
781
 
782
        if (gcp_results.lpDx && gcp_results.lpGlyphs)
783
            break;
784
 
785
        /* Too small a buffer, try again */
786
 
787
        buffer_size += buffer_size / 2;
788
        if (buffer_size > INT_MAX) {
789
            status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
790
            goto FAIL2;
791
        }
792
    }
793
 
794
    *num_glyphs = gcp_results.nGlyphs;
795
    *glyphs = _cairo_malloc_ab (gcp_results.nGlyphs, sizeof (cairo_glyph_t));
796
    if (!*glyphs) {
797
        status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
798
        goto FAIL2;
799
    }
800
 
801
    x_pos = x;
802
    y_pos = y;
803
 
804
    for (i = 0; i < gcp_results.nGlyphs; i++) {
805
        (*glyphs)[i].index = glyph_indices[i];
806
        (*glyphs)[i].x = x_pos ;
807
        (*glyphs)[i].y = y_pos;
808
 
809
        x_pos += x_incr * dx[i];
810
        y_pos += y_incr * dx[i];
811
    }
812
 
813
 FAIL2:
814
    if (glyph_indices)
815
        free (glyph_indices);
816
    if (dx)
817
        free (dx);
818
 
819
    cairo_win32_scaled_font_done_font (&scaled_font->base);
820
 
821
 FAIL1:
822
    free (utf16);
823
 
824
    return status;
825
}
826
 
827
static unsigned long
828
_cairo_win32_scaled_font_ucs4_to_index (void            *abstract_font,
829
                                        uint32_t         ucs4)
830
{
831
    cairo_win32_scaled_font_t *scaled_font = abstract_font;
832
    wchar_t unicode[2];
833
    WORD glyph_index;
834
    HDC hdc = NULL;
835
    cairo_status_t status;
836
 
837
    hdc = _get_global_font_dc ();
838
    assert (hdc != NULL);
839
 
840
    status = cairo_win32_scaled_font_select_font (&scaled_font->base, hdc);
841
    if (status)
842
        return 0;
843
 
844
    unicode[0] = ucs4;
845
    unicode[1] = 0;
846
    if (GetGlyphIndicesW (hdc, unicode, 1, &glyph_index, 0) == GDI_ERROR) {
847
        _cairo_win32_print_gdi_error ("_cairo_win32_scaled_font_ucs4_to_index:GetGlyphIndicesW");
848
        glyph_index = 0;
849
    }
850
 
851
    cairo_win32_scaled_font_done_font (&scaled_font->base);
852
 
853
    return glyph_index;
854
}
855
 
856
static cairo_status_t
857
_cairo_win32_scaled_font_set_metrics (cairo_win32_scaled_font_t *scaled_font)
858
{
859
    cairo_status_t status;
860
    cairo_font_extents_t extents;
861
 
862
    TEXTMETRIC metrics;
863
    HDC hdc;
864
 
865
    hdc = _get_global_font_dc ();
866
    assert (hdc != NULL);
867
 
868
    if (scaled_font->preserve_axes || scaled_font->base.options.hint_metrics == CAIRO_HINT_METRICS_OFF) {
869
        /* For 90-degree rotations (including 0), we get the metrics
870
         * from the GDI in logical space, then convert back to font space
871
         */
872
        status = cairo_win32_scaled_font_select_font (&scaled_font->base, hdc);
873
        if (status)
874
            return status;
875
        GetTextMetrics (hdc, &metrics);
876
        cairo_win32_scaled_font_done_font (&scaled_font->base);
877
 
878
        extents.ascent = metrics.tmAscent / scaled_font->logical_scale;
879
        extents.descent = metrics.tmDescent / scaled_font->logical_scale;
880
 
881
        extents.height = (metrics.tmHeight + metrics.tmExternalLeading) / scaled_font->logical_scale;
882
        extents.max_x_advance = metrics.tmMaxCharWidth / scaled_font->logical_scale;
883
        extents.max_y_advance = 0;
884
 
885
    } else {
886
        /* For all other transformations, we use the design metrics
887
         * of the font. The GDI results from GetTextMetrics() on a
888
         * transformed font are inexplicably large and we want to
889
         * avoid them.
890
         */
891
        status = _cairo_win32_scaled_font_select_unscaled_font (&scaled_font->base, hdc);
892
        if (status)
893
            return status;
894
        GetTextMetrics (hdc, &metrics);
895
        _cairo_win32_scaled_font_done_unscaled_font (&scaled_font->base);
896
 
897
        extents.ascent = (double)metrics.tmAscent / scaled_font->em_square;
898
        extents.descent = (double)metrics.tmDescent / scaled_font->em_square;
899
        extents.height = (double)(metrics.tmHeight + metrics.tmExternalLeading) / scaled_font->em_square;
900
        extents.max_x_advance = (double)(metrics.tmMaxCharWidth) / scaled_font->em_square;
901
        extents.max_y_advance = 0;
902
 
903
    }
904
 
905
    scaled_font->is_bitmap = !(metrics.tmPitchAndFamily & TMPF_VECTOR);
906
 
907
    /* Need to determine if this is a Type 1 font for the special
908
     * handling in _text_to_glyphs.  Unlike TrueType or OpenType,
909
     * Type1 fonts do not have a "cmap" table (or any other table).
910
     * However GetFontData() will retrieve a Type1 font when
911
     * requesting that GetFontData() retrieve data from the start of
912
     * the file. This is to distinguish Type1 from stroke fonts such
913
     * as "Script" and "Modern". The TMPF_TRUETYPE test is redundant
914
     * but improves performance for the most common fonts.
915
     */
916
    scaled_font->is_type1 = FALSE;
917
    if (!(metrics.tmPitchAndFamily & TMPF_TRUETYPE) &&
918
        (metrics.tmPitchAndFamily & TMPF_VECTOR))
919
    {
920
         if ((GetFontData (hdc, CMAP_TAG, 0, NULL, 0) == GDI_ERROR) &&
921
             (GetFontData (hdc, 0, 0, NULL, 0) != GDI_ERROR))
922
         {
923
              scaled_font->is_type1 = TRUE;
924
         }
925
    }
926
 
927
    return _cairo_scaled_font_set_metrics (&scaled_font->base, &extents);
928
}
929
 
930
static cairo_status_t
931
_cairo_win32_scaled_font_init_glyph_metrics (cairo_win32_scaled_font_t *scaled_font,
932
                                             cairo_scaled_glyph_t      *scaled_glyph)
933
{
934
    static const MAT2 matrix = { { 0, 1 }, { 0, 0 }, { 0, 0 }, { 0, 1 } };
935
    GLYPHMETRICS metrics;
936
    cairo_status_t status;
937
    cairo_text_extents_t extents;
938
    HDC hdc;
939
 
940
    hdc = _get_global_font_dc ();
941
    assert (hdc != NULL);
942
 
943
    if (scaled_font->is_bitmap) {
944
        /* GetGlyphOutline will not work. Assume that the glyph does not extend outside the font box. */
945
        cairo_font_extents_t font_extents;
946
        INT width = 0;
947
        UINT charIndex = _cairo_scaled_glyph_index (scaled_glyph);
948
 
949
        cairo_scaled_font_extents (&scaled_font->base, &font_extents);
950
 
951
        status = cairo_win32_scaled_font_select_font (&scaled_font->base, hdc);
952
        if (status)
953
            return status;
954
 
955
        if (!GetCharWidth32(hdc, charIndex, charIndex, &width)) {
956
            status = _cairo_win32_print_gdi_error ("_cairo_win32_scaled_font_init_glyph_metrics:GetCharWidth32");
957
            width = 0;
958
        }
959
        cairo_win32_scaled_font_done_font (&scaled_font->base);
960
        if (status)
961
            return status;
962
 
963
        extents.x_bearing = 0;
964
        extents.y_bearing = scaled_font->base.ctm.yy * (-font_extents.ascent / scaled_font->y_scale);
965
        extents.width = width / (WIN32_FONT_LOGICAL_SCALE * scaled_font->x_scale);
966
        extents.height = scaled_font->base.ctm.yy * (font_extents.ascent + font_extents.descent) / scaled_font->y_scale;
967
        extents.x_advance = extents.width;
968
        extents.y_advance = 0;
969
    } else if (scaled_font->preserve_axes && scaled_font->base.options.hint_style != CAIRO_HINT_METRICS_OFF) {
970
        /* If we aren't rotating / skewing the axes, then we get the metrics
971
         * from the GDI in device space and convert to font space.
972
         */
973
        status = cairo_win32_scaled_font_select_font (&scaled_font->base, hdc);
974
        if (status)
975
            return status;
976
 
977
        if (GetGlyphOutlineW (hdc, _cairo_scaled_glyph_index (scaled_glyph),
978
                              GGO_METRICS | GGO_GLYPH_INDEX,
979
                              &metrics, 0, NULL, &matrix) == GDI_ERROR) {
980
            memset (&metrics, 0, sizeof (GLYPHMETRICS));
981
        }
982
        cairo_win32_scaled_font_done_font (&scaled_font->base);
983
 
984
        if (scaled_font->swap_axes) {
985
            extents.x_bearing = - metrics.gmptGlyphOrigin.y / scaled_font->y_scale;
986
            extents.y_bearing = metrics.gmptGlyphOrigin.x / scaled_font->x_scale;
987
            extents.width = metrics.gmBlackBoxY / scaled_font->y_scale;
988
            extents.height = metrics.gmBlackBoxX / scaled_font->x_scale;
989
            extents.x_advance = metrics.gmCellIncY / scaled_font->x_scale;
990
            extents.y_advance = metrics.gmCellIncX / scaled_font->y_scale;
991
        } else {
992
            extents.x_bearing = metrics.gmptGlyphOrigin.x / scaled_font->x_scale;
993
            extents.y_bearing = - metrics.gmptGlyphOrigin.y / scaled_font->y_scale;
994
            extents.width = metrics.gmBlackBoxX / scaled_font->x_scale;
995
            extents.height = metrics.gmBlackBoxY / scaled_font->y_scale;
996
            extents.x_advance = metrics.gmCellIncX / scaled_font->x_scale;
997
            extents.y_advance = metrics.gmCellIncY / scaled_font->y_scale;
998
        }
999
 
1000
        if (scaled_font->swap_x) {
1001
            extents.x_bearing = (- extents.x_bearing - extents.width);
1002
            extents.x_advance = - extents.x_advance;
1003
        }
1004
 
1005
        if (scaled_font->swap_y) {
1006
            extents.y_bearing = (- extents.y_bearing - extents.height);
1007
            extents.y_advance = - extents.y_advance;
1008
        }
1009
 
1010
    } else {
1011
        /* For all other transformations, we use the design metrics
1012
         * of the font.
1013
         */
1014
        status = _cairo_win32_scaled_font_select_unscaled_font (&scaled_font->base, hdc);
1015
        if (status)
1016
            return status;
1017
 
1018
        if (GetGlyphOutlineW (hdc, _cairo_scaled_glyph_index (scaled_glyph),
1019
                              GGO_METRICS | GGO_GLYPH_INDEX,
1020
                              &metrics, 0, NULL, &matrix) == GDI_ERROR) {
1021
            memset (&metrics, 0, sizeof (GLYPHMETRICS));
1022
        }
1023
        _cairo_win32_scaled_font_done_unscaled_font (&scaled_font->base);
1024
 
1025
        extents.x_bearing = (double)metrics.gmptGlyphOrigin.x / scaled_font->em_square;
1026
        extents.y_bearing = - (double)metrics.gmptGlyphOrigin.y / scaled_font->em_square;
1027
        extents.width = (double)metrics.gmBlackBoxX / scaled_font->em_square;
1028
        extents.height = (double)metrics.gmBlackBoxY / scaled_font->em_square;
1029
        extents.x_advance = (double)metrics.gmCellIncX / scaled_font->em_square;
1030
        extents.y_advance = (double)metrics.gmCellIncY / scaled_font->em_square;
1031
    }
1032
 
1033
    _cairo_scaled_glyph_set_metrics (scaled_glyph,
1034
                                     &scaled_font->base,
1035
                                     &extents);
1036
 
1037
    return CAIRO_STATUS_SUCCESS;
1038
}
1039
 
1040
/* Not currently used code, but may be useful in the future if we add
1041
 * back the capability to the scaled font backend interface to get the
1042
 * actual device space bbox rather than computing it from the
1043
 * font-space metrics.
1044
 */
1045
#if 0
1046
static cairo_status_t
1047
_cairo_win32_scaled_font_glyph_bbox (void                *abstract_font,
1048
                                     const cairo_glyph_t *glyphs,
1049
                                     int                  num_glyphs,
1050
                                     cairo_box_t         *bbox)
1051
{
1052
    static const MAT2 matrix = { { 0, 1 }, { 0, 0 }, { 0, 0 }, { 0, 1 } };
1053
    cairo_win32_scaled_font_t *scaled_font = abstract_font;
1054
    int x1 = 0, x2 = 0, y1 = 0, y2 = 0;
1055
 
1056
    if (num_glyphs > 0) {
1057
        HDC hdc;
1058
        GLYPHMETRICS metrics;
1059
        cairo_status_t status;
1060
        int i;
1061
 
1062
        hdc = _get_global_font_dc ();
1063
        assert (hdc != NULL);
1064
 
1065
        status = cairo_win32_scaled_font_select_font (&scaled_font->base, hdc);
1066
        if (status)
1067
            return status;
1068
 
1069
        for (i = 0; i < num_glyphs; i++) {
1070
            int x = _cairo_lround (glyphs[i].x);
1071
            int y = _cairo_lround (glyphs[i].y);
1072
 
1073
            GetGlyphOutlineW (hdc, glyphs[i].index, GGO_METRICS | GGO_GLYPH_INDEX,
1074
                             &metrics, 0, NULL, &matrix);
1075
 
1076
            if (i == 0 || x1 > x + metrics.gmptGlyphOrigin.x)
1077
                x1 = x + metrics.gmptGlyphOrigin.x;
1078
            if (i == 0 || y1 > y - metrics.gmptGlyphOrigin.y)
1079
                y1 = y - metrics.gmptGlyphOrigin.y;
1080
            if (i == 0 || x2 < x + metrics.gmptGlyphOrigin.x + (int)metrics.gmBlackBoxX)
1081
                x2 = x + metrics.gmptGlyphOrigin.x + (int)metrics.gmBlackBoxX;
1082
            if (i == 0 || y2 < y - metrics.gmptGlyphOrigin.y + (int)metrics.gmBlackBoxY)
1083
                y2 = y - metrics.gmptGlyphOrigin.y + (int)metrics.gmBlackBoxY;
1084
        }
1085
 
1086
        cairo_win32_scaled_font_done_font (&scaled_font->base);
1087
    }
1088
 
1089
    bbox->p1.x = _cairo_fixed_from_int (x1);
1090
    bbox->p1.y = _cairo_fixed_from_int (y1);
1091
    bbox->p2.x = _cairo_fixed_from_int (x2);
1092
    bbox->p2.y = _cairo_fixed_from_int (y2);
1093
 
1094
    return CAIRO_STATUS_SUCCESS;
1095
}
1096
#endif
1097
 
1098
typedef struct {
1099
    cairo_win32_scaled_font_t *scaled_font;
1100
    HDC hdc;
1101
 
1102
    cairo_array_t glyphs;
1103
    cairo_array_t dx;
1104
 
1105
    int start_x;
1106
    int last_x;
1107
    int last_y;
1108
} cairo_glyph_state_t;
1109
 
1110
static void
1111
_start_glyphs (cairo_glyph_state_t        *state,
1112
               cairo_win32_scaled_font_t  *scaled_font,
1113
               HDC                         hdc)
1114
{
1115
    state->hdc = hdc;
1116
    state->scaled_font = scaled_font;
1117
 
1118
    _cairo_array_init (&state->glyphs, sizeof (WCHAR));
1119
    _cairo_array_init (&state->dx, sizeof (int));
1120
}
1121
 
1122
static cairo_status_t
1123
_flush_glyphs (cairo_glyph_state_t *state)
1124
{
1125
    cairo_status_t status;
1126
    int dx = 0;
1127
    WCHAR * elements;
1128
    int * dx_elements;
1129
 
1130
    status = _cairo_array_append (&state->dx, &dx);
1131
    if (status)
1132
        return status;
1133
 
1134
    elements = _cairo_array_index (&state->glyphs, 0);
1135
    dx_elements = _cairo_array_index (&state->dx, 0);
1136
    if (!ExtTextOutW (state->hdc,
1137
                      state->start_x, state->last_y,
1138
                      ETO_GLYPH_INDEX,
1139
                      NULL,
1140
                      elements,
1141
                      state->glyphs.num_elements,
1142
                      dx_elements)) {
1143
        return _cairo_win32_print_gdi_error ("_flush_glyphs");
1144
    }
1145
 
1146
    _cairo_array_truncate (&state->glyphs, 0);
1147
    _cairo_array_truncate (&state->dx, 0);
1148
 
1149
    return CAIRO_STATUS_SUCCESS;
1150
}
1151
 
1152
static cairo_status_t
1153
_add_glyph (cairo_glyph_state_t *state,
1154
            unsigned long        index,
1155
            double               device_x,
1156
            double               device_y)
1157
{
1158
    cairo_status_t status;
1159
    double user_x = device_x;
1160
    double user_y = device_y;
1161
    WCHAR glyph_index = index;
1162
    int logical_x, logical_y;
1163
 
1164
    cairo_matrix_transform_point (&state->scaled_font->device_to_logical, &user_x, &user_y);
1165
 
1166
    logical_x = _cairo_lround (user_x);
1167
    logical_y = _cairo_lround (user_y);
1168
 
1169
    if (state->glyphs.num_elements > 0) {
1170
        int dx;
1171
 
1172
        if (logical_y != state->last_y) {
1173
            status = _flush_glyphs (state);
1174
            if (status)
1175
                return status;
1176
            state->start_x = logical_x;
1177
        } else {
1178
            dx = logical_x - state->last_x;
1179
            status = _cairo_array_append (&state->dx, &dx);
1180
            if (status)
1181
                return status;
1182
        }
1183
    } else {
1184
        state->start_x = logical_x;
1185
    }
1186
 
1187
    state->last_x = logical_x;
1188
    state->last_y = logical_y;
1189
 
1190
    status = _cairo_array_append (&state->glyphs, &glyph_index);
1191
    if (status)
1192
        return status;
1193
 
1194
    return CAIRO_STATUS_SUCCESS;
1195
}
1196
 
1197
static cairo_status_t
1198
_finish_glyphs (cairo_glyph_state_t *state)
1199
{
1200
    cairo_status_t status;
1201
 
1202
    status = _flush_glyphs (state);
1203
 
1204
    _cairo_array_fini (&state->glyphs);
1205
    _cairo_array_fini (&state->dx);
1206
 
1207
    return status;
1208
}
1209
 
1210
static cairo_status_t
1211
_draw_glyphs_on_surface (cairo_win32_surface_t     *surface,
1212
                         cairo_win32_scaled_font_t *scaled_font,
1213
                         COLORREF                   color,
1214
                         int                        x_offset,
1215
                         int                        y_offset,
1216
                         const cairo_glyph_t       *glyphs,
1217
                         int                        num_glyphs)
1218
{
1219
    cairo_glyph_state_t state;
1220
    cairo_status_t status, status2;
1221
    int i;
1222
 
1223
    if (!SaveDC (surface->dc))
1224
        return _cairo_win32_print_gdi_error ("_draw_glyphs_on_surface:SaveDC");
1225
 
1226
    status = cairo_win32_scaled_font_select_font (&scaled_font->base, surface->dc);
1227
    if (status)
1228
        goto FAIL1;
1229
 
1230
    SetTextColor (surface->dc, color);
1231
    SetTextAlign (surface->dc, TA_BASELINE | TA_LEFT);
1232
    SetBkMode (surface->dc, TRANSPARENT);
1233
 
1234
    _start_glyphs (&state, scaled_font, surface->dc);
1235
 
1236
    for (i = 0; i < num_glyphs; i++) {
1237
        status = _add_glyph (&state, glyphs[i].index,
1238
                             glyphs[i].x - x_offset, glyphs[i].y - y_offset);
1239
        if (status)
1240
            goto FAIL2;
1241
    }
1242
 
1243
 FAIL2:
1244
    status2 = _finish_glyphs (&state);
1245
    if (status == CAIRO_STATUS_SUCCESS)
1246
        status = status2;
1247
 
1248
    cairo_win32_scaled_font_done_font (&scaled_font->base);
1249
 FAIL1:
1250
    RestoreDC (surface->dc, -1);
1251
 
1252
    return status;
1253
}
1254
 
1255
/* Duplicate the green channel of a 4-channel mask in the alpha channel, then
1256
 * invert the whole mask.
1257
 */
1258
static void
1259
_compute_argb32_mask_alpha (cairo_win32_surface_t *mask_surface)
1260
{
1261
    cairo_image_surface_t *image = (cairo_image_surface_t *)mask_surface->image;
1262
    int i, j;
1263
 
1264
    for (i = 0; i < image->height; i++) {
1265
        uint32_t *p = (uint32_t *) (image->data + i * image->stride);
1266
        for (j = 0; j < image->width; j++) {
1267
            *p = 0xffffffff ^ (*p | ((*p & 0x0000ff00) << 16));
1268
            p++;
1269
        }
1270
    }
1271
}
1272
 
1273
/* Invert a mask
1274
 */
1275
static void
1276
_invert_argb32_mask (cairo_win32_surface_t *mask_surface)
1277
{
1278
    cairo_image_surface_t *image = (cairo_image_surface_t *)mask_surface->image;
1279
    int i, j;
1280
 
1281
    for (i = 0; i < image->height; i++) {
1282
        uint32_t *p = (uint32_t *) (image->data + i * image->stride);
1283
        for (j = 0; j < image->width; j++) {
1284
            *p = 0xffffffff ^ *p;
1285
            p++;
1286
        }
1287
    }
1288
}
1289
 
1290
/* Compute an alpha-mask from a monochrome RGB24 image
1291
 */
1292
static cairo_surface_t *
1293
_compute_a8_mask (cairo_win32_surface_t *mask_surface)
1294
{
1295
    cairo_image_surface_t *image24 = (cairo_image_surface_t *)mask_surface->image;
1296
    cairo_image_surface_t *image8;
1297
    int i, j;
1298
 
1299
    if (image24->base.status)
1300
        return cairo_surface_reference (&image24->base);
1301
 
1302
    image8 = (cairo_image_surface_t *)cairo_image_surface_create (CAIRO_FORMAT_A8,
1303
                                                                  image24->width, image24->height);
1304
    if (image8->base.status)
1305
        return &image8->base;
1306
 
1307
    for (i = 0; i < image24->height; i++) {
1308
        uint32_t *p = (uint32_t *) (image24->data + i * image24->stride);
1309
        unsigned char *q = (unsigned char *) (image8->data + i * image8->stride);
1310
 
1311
        for (j = 0; j < image24->width; j++) {
1312
            *q = 255 - ((*p & 0x0000ff00) >> 8);
1313
            p++;
1314
            q++;
1315
        }
1316
    }
1317
 
1318
    return &image8->base;
1319
}
1320
 
1321
static cairo_int_status_t
1322
_cairo_win32_scaled_font_glyph_init (void                      *abstract_font,
1323
                                     cairo_scaled_glyph_t      *scaled_glyph,
1324
                                     cairo_scaled_glyph_info_t  info)
1325
{
1326
    cairo_win32_scaled_font_t *scaled_font = abstract_font;
1327
    cairo_status_t status;
1328
 
1329
    if ((info & CAIRO_SCALED_GLYPH_INFO_METRICS) != 0) {
1330
        status = _cairo_win32_scaled_font_init_glyph_metrics (scaled_font, scaled_glyph);
1331
        if (status)
1332
            return status;
1333
    }
1334
 
1335
    if (info & CAIRO_SCALED_GLYPH_INFO_SURFACE) {
1336
        status = _cairo_win32_scaled_font_init_glyph_surface (scaled_font, scaled_glyph);
1337
        if (status)
1338
            return status;
1339
    }
1340
 
1341
    if ((info & CAIRO_SCALED_GLYPH_INFO_PATH) != 0) {
1342
        status = _cairo_win32_scaled_font_init_glyph_path (scaled_font, scaled_glyph);
1343
        if (status)
1344
            return status;
1345
    }
1346
 
1347
    return CAIRO_STATUS_SUCCESS;
1348
}
1349
 
1350
static cairo_int_status_t
1351
_cairo_win32_scaled_font_show_glyphs (void                      *abstract_font,
1352
                                      cairo_operator_t           op,
1353
                                      const cairo_pattern_t     *pattern,
1354
                                      cairo_surface_t           *generic_surface,
1355
                                      int                        source_x,
1356
                                      int                        source_y,
1357
                                      int                        dest_x,
1358
                                      int                        dest_y,
1359
                                      unsigned int               width,
1360
                                      unsigned int               height,
1361
                                      cairo_glyph_t             *glyphs,
1362
                                      int                        num_glyphs,
1363
                                      cairo_region_t            *clip_region,
1364
                                      int                       *remaining_glyphs)
1365
{
1366
    cairo_win32_scaled_font_t *scaled_font = abstract_font;
1367
    cairo_win32_surface_t *surface = (cairo_win32_surface_t *)generic_surface;
1368
    cairo_status_t status;
1369
 
1370
    if (width == 0 || height == 0)
1371
        return CAIRO_STATUS_SUCCESS;
1372
 
1373
    if (_cairo_surface_is_win32 (generic_surface) &&
1374
        surface->format == CAIRO_FORMAT_RGB24 &&
1375
        op == CAIRO_OPERATOR_OVER &&
1376
        _cairo_pattern_is_opaque_solid (pattern)) {
1377
 
1378
        cairo_solid_pattern_t *solid_pattern = (cairo_solid_pattern_t *)pattern;
1379
 
1380
        /* When compositing OVER on a GDI-understood surface, with a
1381
         * solid opaque color, we can just call ExtTextOut directly.
1382
         */
1383
        COLORREF new_color;
1384
 
1385
        status = _cairo_win32_surface_set_clip_region (surface, clip_region);
1386
        if (unlikely (status))
1387
            return status;
1388
 
1389
        new_color = RGB (((int)solid_pattern->color.red_short) >> 8,
1390
                         ((int)solid_pattern->color.green_short) >> 8,
1391
                         ((int)solid_pattern->color.blue_short) >> 8);
1392
 
1393
        return _draw_glyphs_on_surface (surface, scaled_font, new_color,
1394
                                        0, 0,
1395
                                        glyphs, num_glyphs);
1396
    } else {
1397
        /* Otherwise, we need to draw using software fallbacks. We create a mask
1398
         * surface by drawing the the glyphs onto a DIB, black-on-white then
1399
         * inverting. GDI outputs gamma-corrected images so inverted black-on-white
1400
         * is very different from white-on-black. We favor the more common
1401
         * case where the final output is dark-on-light.
1402
         */
1403
        cairo_win32_surface_t *tmp_surface;
1404
        cairo_surface_t *mask_surface;
1405
        cairo_surface_pattern_t mask;
1406
        RECT r;
1407
 
1408
        tmp_surface = (cairo_win32_surface_t *)cairo_win32_surface_create_with_dib (CAIRO_FORMAT_ARGB32, width, height);
1409
        if (tmp_surface->base.status)
1410
            return tmp_surface->base.status;
1411
 
1412
        r.left = 0;
1413
        r.top = 0;
1414
        r.right = width;
1415
        r.bottom = height;
1416
        FillRect (tmp_surface->dc, &r, GetStockObject (WHITE_BRUSH));
1417
 
1418
        status = _draw_glyphs_on_surface (tmp_surface,
1419
                                          scaled_font, RGB (0, 0, 0),
1420
                                          dest_x, dest_y,
1421
                                          glyphs, num_glyphs);
1422
        if (status) {
1423
            cairo_surface_destroy (&tmp_surface->base);
1424
            return status;
1425
        }
1426
 
1427
        if (scaled_font->quality == CLEARTYPE_QUALITY) {
1428
            /* For ClearType, we need a 4-channel mask. If we are compositing on
1429
             * a surface with alpha, we need to compute the alpha channel of
1430
             * the mask (we just copy the green channel). But for a destination
1431
             * surface without alpha the alpha channel of the mask is ignored
1432
             */
1433
 
1434
            if (surface->format != CAIRO_FORMAT_RGB24)
1435
                _compute_argb32_mask_alpha (tmp_surface);
1436
            else
1437
                _invert_argb32_mask (tmp_surface);
1438
 
1439
            mask_surface = &tmp_surface->base;
1440
 
1441
            /* XXX: Hacky, should expose this in cairo_image_surface */
1442
            pixman_image_set_component_alpha (((cairo_image_surface_t *)tmp_surface->image)->pixman_image, TRUE);
1443
 
1444
        } else {
1445
            mask_surface = _compute_a8_mask (tmp_surface);
1446
            cairo_surface_destroy (&tmp_surface->base);
1447
            status = mask_surface->status;
1448
            if (status)
1449
                return status;
1450
        }
1451
 
1452
        /* For op == OVER, no-cleartype, a possible optimization here is to
1453
         * draw onto an intermediate ARGB32 surface and alpha-blend that with the
1454
         * destination
1455
         */
1456
        _cairo_pattern_init_for_surface (&mask, mask_surface);
1457
 
1458
        status = _cairo_surface_composite (op, pattern,
1459
                                           &mask.base,
1460
                                           &surface->base,
1461
                                           source_x, source_y,
1462
                                           0, 0,
1463
                                           dest_x, dest_y,
1464
                                           width, height,
1465
                                           clip_region);
1466
 
1467
        _cairo_pattern_fini (&mask.base);
1468
 
1469
        cairo_surface_destroy (mask_surface);
1470
 
1471
        return status;
1472
    }
1473
}
1474
 
1475
static cairo_int_status_t
1476
_cairo_win32_scaled_font_load_truetype_table (void             *abstract_font,
1477
                                             unsigned long      tag,
1478
                                             long               offset,
1479
                                             unsigned char     *buffer,
1480
                                             unsigned long     *length)
1481
{
1482
    cairo_win32_scaled_font_t *scaled_font = abstract_font;
1483
    HDC hdc;
1484
    cairo_status_t status;
1485
 
1486
    hdc = _get_global_font_dc ();
1487
    assert (hdc != NULL);
1488
 
1489
    tag = (tag&0x000000ff)<<24 | (tag&0x0000ff00)<<8 | (tag&0x00ff0000)>>8 | (tag&0xff000000)>>24;
1490
    status = cairo_win32_scaled_font_select_font (&scaled_font->base, hdc);
1491
    if (status)
1492
        return status;
1493
 
1494
    *length = GetFontData (hdc, tag, offset, buffer, *length);
1495
    if (*length == GDI_ERROR)
1496
        status = CAIRO_INT_STATUS_UNSUPPORTED;
1497
 
1498
    cairo_win32_scaled_font_done_font (&scaled_font->base);
1499
 
1500
    return status;
1501
}
1502
 
1503
static cairo_int_status_t
1504
_cairo_win32_scaled_font_index_to_ucs4 (void            *abstract_font,
1505
                                        unsigned long    index,
1506
                                        uint32_t        *ucs4)
1507
{
1508
    cairo_win32_scaled_font_t *scaled_font = abstract_font;
1509
    GLYPHSET *glyph_set;
1510
    uint16_t *utf16 = NULL;
1511
    WORD *glyph_indices = NULL;
1512
    HDC hdc = NULL;
1513
    int res;
1514
    unsigned int i, j, num_glyphs;
1515
    cairo_status_t status;
1516
 
1517
    hdc = _get_global_font_dc ();
1518
    assert (hdc != NULL);
1519
 
1520
    status = cairo_win32_scaled_font_select_font (&scaled_font->base, hdc);
1521
    if (status)
1522
        return status;
1523
 
1524
    res = GetFontUnicodeRanges(hdc, NULL);
1525
    if (res == 0) {
1526
        status = _cairo_win32_print_gdi_error (
1527
            "_cairo_win32_scaled_font_index_to_ucs4:GetFontUnicodeRanges");
1528
        goto exit1;
1529
    }
1530
 
1531
    glyph_set = malloc (res);
1532
    if (glyph_set == NULL) {
1533
        status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1534
        goto exit1;
1535
    }
1536
 
1537
    res = GetFontUnicodeRanges(hdc, glyph_set);
1538
    if (res == 0) {
1539
        status = _cairo_win32_print_gdi_error (
1540
            "_cairo_win32_scaled_font_index_to_ucs4:GetFontUnicodeRanges");
1541
        goto exit1;
1542
    }
1543
 
1544
    *ucs4 = (uint32_t) -1;
1545
    for (i = 0; i < glyph_set->cRanges; i++) {
1546
        num_glyphs = glyph_set->ranges[i].cGlyphs;
1547
 
1548
        utf16 = _cairo_malloc_ab (num_glyphs + 1, sizeof (uint16_t));
1549
        if (utf16 == NULL) {
1550
            status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1551
            goto exit1;
1552
        }
1553
 
1554
        glyph_indices = _cairo_malloc_ab (num_glyphs + 1, sizeof (WORD));
1555
        if (glyph_indices == NULL) {
1556
            status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1557
            goto exit2;
1558
        }
1559
 
1560
        for (j = 0; j < num_glyphs; j++)
1561
            utf16[j] = glyph_set->ranges[i].wcLow + j;
1562
        utf16[j] = 0;
1563
 
1564
        if (GetGlyphIndicesW (hdc, utf16, num_glyphs, glyph_indices, 0) == GDI_ERROR) {
1565
            status = _cairo_win32_print_gdi_error (
1566
                "_cairo_win32_scaled_font_index_to_ucs4:GetGlyphIndicesW");
1567
            goto exit2;
1568
        }
1569
 
1570
        for (j = 0; j < num_glyphs; j++) {
1571
            if (glyph_indices[j] == index) {
1572
                *ucs4 = utf16[j];
1573
                goto exit2;
1574
            }
1575
        }
1576
 
1577
        free (glyph_indices);
1578
        glyph_indices = NULL;
1579
        free (utf16);
1580
        utf16 = NULL;
1581
    }
1582
 
1583
exit2:
1584
    if (glyph_indices)
1585
        free (glyph_indices);
1586
    if (utf16)
1587
        free (utf16);
1588
    free (glyph_set);
1589
exit1:
1590
    cairo_win32_scaled_font_done_font (&scaled_font->base);
1591
 
1592
    return status;
1593
}
1594
 
1595
static cairo_status_t
1596
_cairo_win32_scaled_font_init_glyph_surface (cairo_win32_scaled_font_t *scaled_font,
1597
                                             cairo_scaled_glyph_t      *scaled_glyph)
1598
{
1599
    cairo_status_t status;
1600
    cairo_glyph_t glyph;
1601
    cairo_win32_surface_t *surface;
1602
    cairo_t *cr;
1603
    cairo_surface_t *image;
1604
    int width, height;
1605
    int x1, y1, x2, y2;
1606
 
1607
    x1 = _cairo_fixed_integer_floor (scaled_glyph->bbox.p1.x);
1608
    y1 = _cairo_fixed_integer_floor (scaled_glyph->bbox.p1.y);
1609
    x2 = _cairo_fixed_integer_ceil (scaled_glyph->bbox.p2.x);
1610
    y2 = _cairo_fixed_integer_ceil (scaled_glyph->bbox.p2.y);
1611
    width = x2 - x1;
1612
    height = y2 - y1;
1613
 
1614
    surface = (cairo_win32_surface_t *)
1615
        cairo_win32_surface_create_with_dib (CAIRO_FORMAT_RGB24, width, height);
1616
 
1617
    cr = cairo_create (&surface->base);
1618
    cairo_set_source_rgb (cr, 1, 1, 1);
1619
    cairo_paint (cr);
1620
    status = cairo_status (cr);
1621
    cairo_destroy(cr);
1622
    if (status)
1623
        goto FAIL;
1624
 
1625
    glyph.index = _cairo_scaled_glyph_index (scaled_glyph);
1626
    glyph.x = -x1;
1627
    glyph.y = -y1;
1628
    status = _draw_glyphs_on_surface (surface, scaled_font, RGB(0,0,0),
1629
                                      0, 0, &glyph, 1);
1630
    if (status)
1631
        goto FAIL;
1632
 
1633
    GdiFlush();
1634
 
1635
    image = _compute_a8_mask (surface);
1636
    status = image->status;
1637
    if (status)
1638
        goto FAIL;
1639
 
1640
    cairo_surface_set_device_offset (image, -x1, -y1);
1641
    _cairo_scaled_glyph_set_surface (scaled_glyph,
1642
                                     &scaled_font->base,
1643
                                     (cairo_image_surface_t *) image);
1644
 
1645
  FAIL:
1646
    cairo_surface_destroy (&surface->base);
1647
 
1648
    return status;
1649
}
1650
 
1651
static void
1652
_cairo_win32_transform_FIXED_to_fixed (cairo_matrix_t *matrix,
1653
                                       FIXED Fx, FIXED Fy,
1654
                                       cairo_fixed_t *fx, cairo_fixed_t *fy)
1655
{
1656
    double x = Fx.value + Fx.fract / 65536.0;
1657
    double y = Fy.value + Fy.fract / 65536.0;
1658
    cairo_matrix_transform_point (matrix, &x, &y);
1659
    *fx = _cairo_fixed_from_double (x);
1660
    *fy = _cairo_fixed_from_double (y);
1661
}
1662
 
1663
static cairo_status_t
1664
_cairo_win32_scaled_font_init_glyph_path (cairo_win32_scaled_font_t *scaled_font,
1665
                                          cairo_scaled_glyph_t      *scaled_glyph)
1666
{
1667
    static const MAT2 matrix = { { 0, 1 }, { 0, 0 }, { 0, 0 }, { 0, -1 } };
1668
    cairo_status_t status;
1669
    GLYPHMETRICS metrics;
1670
    HDC hdc;
1671
    DWORD bytesGlyph;
1672
    unsigned char *buffer, *ptr;
1673
    cairo_path_fixed_t *path;
1674
    cairo_matrix_t transform;
1675
    cairo_fixed_t x, y;
1676
 
1677
    if (scaled_font->is_bitmap)
1678
        return CAIRO_INT_STATUS_UNSUPPORTED;
1679
 
1680
    hdc = _get_global_font_dc ();
1681
    assert (hdc != NULL);
1682
 
1683
    path = _cairo_path_fixed_create ();
1684
    if (!path)
1685
        return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1686
 
1687
    if (scaled_font->base.options.hint_style == CAIRO_HINT_STYLE_NONE) {
1688
        status = _cairo_win32_scaled_font_select_unscaled_font (&scaled_font->base, hdc);
1689
        transform = scaled_font->base.scale;
1690
        cairo_matrix_scale (&transform, 1.0/scaled_font->em_square, 1.0/scaled_font->em_square);
1691
    } else {
1692
        status = cairo_win32_scaled_font_select_font (&scaled_font->base, hdc);
1693
        cairo_matrix_init_identity(&transform);
1694
    }
1695
    if (status)
1696
        goto CLEANUP_PATH;
1697
 
1698
    bytesGlyph = GetGlyphOutlineW (hdc, _cairo_scaled_glyph_index (scaled_glyph),
1699
                                   GGO_NATIVE | GGO_GLYPH_INDEX,
1700
                                   &metrics, 0, NULL, &matrix);
1701
 
1702
    if (bytesGlyph == GDI_ERROR) {
1703
        status = _cairo_win32_print_gdi_error ("_cairo_win32_scaled_font_glyph_path");
1704
        goto CLEANUP_FONT;
1705
    }
1706
 
1707
    ptr = buffer = malloc (bytesGlyph);
1708
    if (!buffer) {
1709
        status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1710
        goto CLEANUP_FONT;
1711
    }
1712
 
1713
    if (GetGlyphOutlineW (hdc, _cairo_scaled_glyph_index (scaled_glyph),
1714
                          GGO_NATIVE | GGO_GLYPH_INDEX,
1715
                          &metrics, bytesGlyph, buffer, &matrix) == GDI_ERROR) {
1716
        status = _cairo_win32_print_gdi_error ("_cairo_win32_scaled_font_glyph_path");
1717
        goto CLEANUP_BUFFER;
1718
    }
1719
 
1720
    while (ptr < buffer + bytesGlyph) {
1721
        TTPOLYGONHEADER *header = (TTPOLYGONHEADER *)ptr;
1722
        unsigned char *endPoly = ptr + header->cb;
1723
 
1724
        ptr += sizeof (TTPOLYGONHEADER);
1725
 
1726
        _cairo_win32_transform_FIXED_to_fixed (&transform,
1727
                                               header->pfxStart.x,
1728
                                               header->pfxStart.y,
1729
                                               &x, &y);
1730
        status = _cairo_path_fixed_move_to (path, x, y);
1731
        if (status)
1732
            goto CLEANUP_BUFFER;
1733
 
1734
        while (ptr < endPoly) {
1735
            TTPOLYCURVE *curve = (TTPOLYCURVE *)ptr;
1736
            POINTFX *points = curve->apfx;
1737
            int i;
1738
            switch (curve->wType) {
1739
            case TT_PRIM_LINE:
1740
                for (i = 0; i < curve->cpfx; i++) {
1741
                    _cairo_win32_transform_FIXED_to_fixed (&transform,
1742
                                                           points[i].x,
1743
                                                           points[i].y,
1744
                                                           &x, &y);
1745
                    status = _cairo_path_fixed_line_to (path, x, y);
1746
                    if (status)
1747
                        goto CLEANUP_BUFFER;
1748
                }
1749
                break;
1750
            case TT_PRIM_QSPLINE:
1751
                for (i = 0; i < curve->cpfx - 1; i++) {
1752
                    cairo_fixed_t p1x, p1y, p2x, p2y, cx, cy, c1x, c1y, c2x, c2y;
1753
                    if (! _cairo_path_fixed_get_current_point (path, &p1x, &p1y))
1754
                        goto CLEANUP_BUFFER;
1755
                    _cairo_win32_transform_FIXED_to_fixed (&transform,
1756
                                                           points[i].x,
1757
                                                           points[i].y,
1758
                                                           &cx, &cy);
1759
 
1760
                    if (i + 1 == curve->cpfx - 1) {
1761
                        _cairo_win32_transform_FIXED_to_fixed (&transform,
1762
                                                               points[i + 1].x,
1763
                                                               points[i + 1].y,
1764
                                                               &p2x, &p2y);
1765
                    } else {
1766
                        /* records with more than one curve use interpolation for
1767
                           control points, per http://support.microsoft.com/kb/q87115/ */
1768
                        _cairo_win32_transform_FIXED_to_fixed (&transform,
1769
                                                               points[i + 1].x,
1770
                                                               points[i + 1].y,
1771
                                                               &x, &y);
1772
                        p2x = (cx + x) / 2;
1773
                        p2y = (cy + y) / 2;
1774
                    }
1775
 
1776
                    c1x = 2 * cx / 3 + p1x / 3;
1777
                    c1y = 2 * cy / 3 + p1y / 3;
1778
                    c2x = 2 * cx / 3 + p2x / 3;
1779
                    c2y = 2 * cy / 3 + p2y / 3;
1780
 
1781
                    status = _cairo_path_fixed_curve_to (path, c1x, c1y, c2x, c2y, p2x, p2y);
1782
                    if (status)
1783
                        goto CLEANUP_BUFFER;
1784
                }
1785
                break;
1786
            case TT_PRIM_CSPLINE:
1787
                for (i = 0; i < curve->cpfx - 2; i += 2) {
1788
                    cairo_fixed_t x1, y1, x2, y2;
1789
                    _cairo_win32_transform_FIXED_to_fixed (&transform,
1790
                                                           points[i].x,
1791
                                                           points[i].y,
1792
                                                           &x, &y);
1793
                    _cairo_win32_transform_FIXED_to_fixed (&transform,
1794
                                                           points[i + 1].x,
1795
                                                           points[i + 1].y,
1796
                                                           &x1, &y1);
1797
                    _cairo_win32_transform_FIXED_to_fixed (&transform,
1798
                                                           points[i + 2].x,
1799
                                                           points[i + 2].y,
1800
                                                           &x2, &y2);
1801
                    status = _cairo_path_fixed_curve_to (path, x, y, x1, y1, x2, y2);
1802
                    if (status)
1803
                        goto CLEANUP_BUFFER;
1804
                }
1805
                break;
1806
            }
1807
            ptr += sizeof(TTPOLYCURVE) + sizeof (POINTFX) * (curve->cpfx - 1);
1808
        }
1809
        status = _cairo_path_fixed_close_path (path);
1810
        if (status)
1811
            goto CLEANUP_BUFFER;
1812
    }
1813
 
1814
    _cairo_scaled_glyph_set_path (scaled_glyph,
1815
                                  &scaled_font->base,
1816
                                  path);
1817
 
1818
 CLEANUP_BUFFER:
1819
    free (buffer);
1820
 
1821
 CLEANUP_FONT:
1822
    if (scaled_font->base.options.hint_style == CAIRO_HINT_STYLE_NONE)
1823
        _cairo_win32_scaled_font_done_unscaled_font (&scaled_font->base);
1824
    else
1825
        cairo_win32_scaled_font_done_font (&scaled_font->base);
1826
 
1827
 CLEANUP_PATH:
1828
    if (status != CAIRO_STATUS_SUCCESS)
1829
        _cairo_path_fixed_destroy (path);
1830
 
1831
    return status;
1832
}
1833
 
1834
const cairo_scaled_font_backend_t _cairo_win32_scaled_font_backend = {
1835
    CAIRO_FONT_TYPE_WIN32,
1836
    _cairo_win32_scaled_font_fini,
1837
    _cairo_win32_scaled_font_glyph_init,
1838
    NULL, /* _cairo_win32_scaled_font_text_to_glyphs, FIXME */
1839
    _cairo_win32_scaled_font_ucs4_to_index,
1840
    _cairo_win32_scaled_font_show_glyphs,
1841
    _cairo_win32_scaled_font_load_truetype_table,
1842
    _cairo_win32_scaled_font_index_to_ucs4,
1843
};
1844
 
1845
/* #cairo_win32_font_face_t */
1846
 
1847
typedef struct _cairo_win32_font_face cairo_win32_font_face_t;
1848
 
1849
/* If hfont is non-%NULL then logfont->lfHeight must be -S for some S,
1850
 * logfont->lfWidth, logfont->lfEscapement, logfont->lfOrientation must
1851
 * all be 0, and hfont is the result of calling CreateFontIndirectW on
1852
 * logfont.
1853
 */
1854
struct _cairo_win32_font_face {
1855
    cairo_font_face_t base;
1856
    LOGFONTW logfont;
1857
    HFONT hfont;
1858
};
1859
 
1860
/* implement the platform-specific interface */
1861
 
1862
static void
1863
_cairo_win32_font_face_destroy (void *abstract_face)
1864
{
1865
}
1866
 
1867
static cairo_bool_t
1868
_is_scale (const cairo_matrix_t *matrix, double scale)
1869
{
1870
    return matrix->xx == scale && matrix->yy == scale &&
1871
           matrix->xy == 0. && matrix->yx == 0. &&
1872
           matrix->x0 == 0. && matrix->y0 == 0.;
1873
}
1874
 
1875
static cairo_status_t
1876
_cairo_win32_font_face_scaled_font_create (void                 *abstract_face,
1877
                                           const cairo_matrix_t *font_matrix,
1878
                                           const cairo_matrix_t *ctm,
1879
                                           const cairo_font_options_t *options,
1880
                                           cairo_scaled_font_t **font)
1881
{
1882
    HFONT hfont = NULL;
1883
 
1884
    cairo_win32_font_face_t *font_face = abstract_face;
1885
 
1886
    if (font_face->hfont) {
1887
        /* Check whether it's OK to go ahead and use the font-face's HFONT. */
1888
        if (_is_scale (ctm, 1.) &&
1889
            _is_scale (font_matrix, -font_face->logfont.lfHeight)) {
1890
            hfont = font_face->hfont;
1891
        }
1892
    }
1893
 
1894
    return _win32_scaled_font_create (&font_face->logfont,
1895
                                      hfont,
1896
                                      &font_face->base,
1897
                                      font_matrix, ctm, options,
1898
                                      font);
1899
}
1900
 
1901
const cairo_font_face_backend_t _cairo_win32_font_face_backend = {
1902
    CAIRO_FONT_TYPE_WIN32,
1903
    _cairo_win32_font_face_create_for_toy,
1904
    _cairo_win32_font_face_destroy,
1905
    _cairo_win32_font_face_scaled_font_create
1906
};
1907
 
1908
/**
1909
 * cairo_win32_font_face_create_for_logfontw_hfont:
1910
 * @logfont: A #LOGFONTW structure specifying the font to use.
1911
 *   If @font is %NULL then the lfHeight, lfWidth, lfOrientation and lfEscapement
1912
 *   fields of this structure are ignored. Otherwise lfWidth, lfOrientation and
1913
 *   lfEscapement must be zero.
1914
 * @font: An #HFONT that can be used when the font matrix is a scale by
1915
 *   -lfHeight and the CTM is identity.
1916
 *
1917
 * Creates a new font for the Win32 font backend based on a
1918
 * #LOGFONT. This font can then be used with
1919
 * cairo_set_font_face() or cairo_scaled_font_create().
1920
 * The #cairo_scaled_font_t
1921
 * returned from cairo_scaled_font_create() is also for the Win32 backend
1922
 * and can be used with functions such as cairo_win32_scaled_font_select_font().
1923
 *
1924
 * Return value: a newly created #cairo_font_face_t. Free with
1925
 *  cairo_font_face_destroy() when you are done using it.
1926
 **/
1927
cairo_font_face_t *
1928
cairo_win32_font_face_create_for_logfontw_hfont (LOGFONTW *logfont, HFONT font)
1929
{
1930
    cairo_win32_font_face_t *font_face;
1931
 
1932
    font_face = malloc (sizeof (cairo_win32_font_face_t));
1933
    if (!font_face) {
1934
        _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
1935
        return (cairo_font_face_t *)&_cairo_font_face_nil;
1936
    }
1937
 
1938
    font_face->logfont = *logfont;
1939
    font_face->hfont = font;
1940
 
1941
    _cairo_font_face_init (&font_face->base, &_cairo_win32_font_face_backend);
1942
 
1943
    return &font_face->base;
1944
}
1945
 
1946
/**
1947
 * cairo_win32_font_face_create_for_logfontw:
1948
 * @logfont: A #LOGFONTW structure specifying the font to use.
1949
 *   The lfHeight, lfWidth, lfOrientation and lfEscapement
1950
 *   fields of this structure are ignored.
1951
 *
1952
 * Creates a new font for the Win32 font backend based on a
1953
 * #LOGFONT. This font can then be used with
1954
 * cairo_set_font_face() or cairo_scaled_font_create().
1955
 * The #cairo_scaled_font_t
1956
 * returned from cairo_scaled_font_create() is also for the Win32 backend
1957
 * and can be used with functions such as cairo_win32_scaled_font_select_font().
1958
 *
1959
 * Return value: a newly created #cairo_font_face_t. Free with
1960
 *  cairo_font_face_destroy() when you are done using it.
1961
 **/
1962
cairo_font_face_t *
1963
cairo_win32_font_face_create_for_logfontw (LOGFONTW *logfont)
1964
{
1965
    return cairo_win32_font_face_create_for_logfontw_hfont (logfont, NULL);
1966
}
1967
 
1968
/**
1969
 * cairo_win32_font_face_create_for_hfont:
1970
 * @font: An #HFONT structure specifying the font to use.
1971
 *
1972
 * Creates a new font for the Win32 font backend based on a
1973
 * #HFONT. This font can then be used with
1974
 * cairo_set_font_face() or cairo_scaled_font_create().
1975
 * The #cairo_scaled_font_t
1976
 * returned from cairo_scaled_font_create() is also for the Win32 backend
1977
 * and can be used with functions such as cairo_win32_scaled_font_select_font().
1978
 *
1979
 * Return value: a newly created #cairo_font_face_t. Free with
1980
 *  cairo_font_face_destroy() when you are done using it.
1981
 **/
1982
cairo_font_face_t *
1983
cairo_win32_font_face_create_for_hfont (HFONT font)
1984
{
1985
    LOGFONTW logfont;
1986
    GetObjectW (font, sizeof(logfont), &logfont);
1987
 
1988
    if (logfont.lfEscapement != 0 || logfont.lfOrientation != 0 ||
1989
        logfont.lfWidth != 0) {
1990
        /* We can't use this font because that optimization requires that
1991
         * lfEscapement, lfOrientation and lfWidth be zero. */
1992
        font = NULL;
1993
    }
1994
 
1995
    return cairo_win32_font_face_create_for_logfontw_hfont (&logfont, font);
1996
}
1997
 
1998
static cairo_bool_t
1999
_cairo_scaled_font_is_win32 (cairo_scaled_font_t *scaled_font)
2000
{
2001
    return scaled_font->backend == &_cairo_win32_scaled_font_backend;
2002
}
2003
 
2004
/**
2005
 * cairo_win32_scaled_font_select_font:
2006
 * @scaled_font: A #cairo_scaled_font_t from the Win32 font backend. Such an
2007
 *   object can be created with cairo_win32_scaled_font_create_for_logfontw().
2008
 * @hdc: a device context
2009
 *
2010
 * Selects the font into the given device context and changes the
2011
 * map mode and world transformation of the device context to match
2012
 * that of the font. This function is intended for use when using
2013
 * layout APIs such as Uniscribe to do text layout with the
2014
 * cairo font. After finishing using the device context, you must call
2015
 * cairo_win32_scaled_font_done_font() to release any resources allocated
2016
 * by this function.
2017
 *
2018
 * See cairo_win32_scaled_font_get_metrics_factor() for converting logical
2019
 * coordinates from the device context to font space.
2020
 *
2021
 * Normally, calls to SaveDC() and RestoreDC() would be made around
2022
 * the use of this function to preserve the original graphics state.
2023
 *
2024
 * Return value: %CAIRO_STATUS_SUCCESS if the operation succeeded.
2025
 *   otherwise an error such as %CAIRO_STATUS_NO_MEMORY and
2026
 *   the device context is unchanged.
2027
 **/
2028
cairo_status_t
2029
cairo_win32_scaled_font_select_font (cairo_scaled_font_t *scaled_font,
2030
                                     HDC                  hdc)
2031
{
2032
    cairo_status_t status;
2033
    HFONT hfont;
2034
    HFONT old_hfont = NULL;
2035
    int old_mode;
2036
 
2037
    if (! _cairo_scaled_font_is_win32 (scaled_font)) {
2038
        return _cairo_error (CAIRO_STATUS_FONT_TYPE_MISMATCH);
2039
    }
2040
 
2041
    if (scaled_font->status)
2042
        return scaled_font->status;
2043
 
2044
    status = _win32_scaled_font_get_scaled_hfont ((cairo_win32_scaled_font_t *)scaled_font, &hfont);
2045
    if (status)
2046
        return status;
2047
 
2048
    old_hfont = SelectObject (hdc, hfont);
2049
    if (!old_hfont)
2050
        return _cairo_win32_print_gdi_error ("cairo_win32_scaled_font_select_font:SelectObject");
2051
 
2052
    old_mode = SetGraphicsMode (hdc, GM_ADVANCED);
2053
    if (!old_mode) {
2054
        status = _cairo_win32_print_gdi_error ("cairo_win32_scaled_font_select_font:SetGraphicsMode");
2055
        SelectObject (hdc, old_hfont);
2056
        return status;
2057
    }
2058
 
2059
    status = _win32_scaled_font_set_world_transform ((cairo_win32_scaled_font_t *)scaled_font, hdc);
2060
    if (status) {
2061
        SetGraphicsMode (hdc, old_mode);
2062
        SelectObject (hdc, old_hfont);
2063
        return status;
2064
    }
2065
 
2066
    SetMapMode (hdc, MM_TEXT);
2067
 
2068
    return CAIRO_STATUS_SUCCESS;
2069
}
2070
 
2071
/**
2072
 * cairo_win32_scaled_font_done_font:
2073
 * @scaled_font: A scaled font from the Win32 font backend.
2074
 *
2075
 * Releases any resources allocated by cairo_win32_scaled_font_select_font()
2076
 **/
2077
void
2078
cairo_win32_scaled_font_done_font (cairo_scaled_font_t *scaled_font)
2079
{
2080
    if (! _cairo_scaled_font_is_win32 (scaled_font)) {
2081
        _cairo_error_throw (CAIRO_STATUS_FONT_TYPE_MISMATCH);
2082
    }
2083
}
2084
 
2085
/**
2086
 * cairo_win32_scaled_font_get_metrics_factor:
2087
 * @scaled_font: a scaled font from the Win32 font backend
2088
 *
2089
 * Gets a scale factor between logical coordinates in the coordinate
2090
 * space used by cairo_win32_scaled_font_select_font() (that is, the
2091
 * coordinate system used by the Windows functions to return metrics) and
2092
 * font space coordinates.
2093
 *
2094
 * Return value: factor to multiply logical units by to get font space
2095
 *               coordinates.
2096
 **/
2097
double
2098
cairo_win32_scaled_font_get_metrics_factor (cairo_scaled_font_t *scaled_font)
2099
{
2100
    if (! _cairo_scaled_font_is_win32 (scaled_font)) {
2101
        _cairo_error_throw (CAIRO_STATUS_FONT_TYPE_MISMATCH);
2102
        return 1.;
2103
    }
2104
    return 1. / ((cairo_win32_scaled_font_t *)scaled_font)->logical_scale;
2105
}
2106
 
2107
/**
2108
 * cairo_win32_scaled_font_get_logical_to_device:
2109
 * @scaled_font: a scaled font from the Win32 font backend
2110
 * @logical_to_device: matrix to return
2111
 *
2112
 * Gets the transformation mapping the logical space used by @scaled_font
2113
 * to device space.
2114
 *
2115
 * Since: 1.4
2116
 **/
2117
void
2118
cairo_win32_scaled_font_get_logical_to_device (cairo_scaled_font_t *scaled_font,
2119
                                               cairo_matrix_t *logical_to_device)
2120
{
2121
    cairo_win32_scaled_font_t *win_font = (cairo_win32_scaled_font_t *)scaled_font;
2122
    if (! _cairo_scaled_font_is_win32 (scaled_font)) {
2123
        _cairo_error_throw (CAIRO_STATUS_FONT_TYPE_MISMATCH);
2124
        cairo_matrix_init_identity (logical_to_device);
2125
        return;
2126
    }
2127
    *logical_to_device = win_font->logical_to_device;
2128
}
2129
 
2130
/**
2131
 * cairo_win32_scaled_font_get_device_to_logical:
2132
 * @scaled_font: a scaled font from the Win32 font backend
2133
 * @device_to_logical: matrix to return
2134
 *
2135
 * Gets the transformation mapping device space to the logical space
2136
 * used by @scaled_font.
2137
 *
2138
 * Since: 1.4
2139
 **/
2140
void
2141
cairo_win32_scaled_font_get_device_to_logical (cairo_scaled_font_t *scaled_font,
2142
                                               cairo_matrix_t *device_to_logical)
2143
{
2144
    cairo_win32_scaled_font_t *win_font = (cairo_win32_scaled_font_t *)scaled_font;
2145
    if (! _cairo_scaled_font_is_win32 (scaled_font)) {
2146
        _cairo_error_throw (CAIRO_STATUS_FONT_TYPE_MISMATCH);
2147
        cairo_matrix_init_identity (device_to_logical);
2148
        return;
2149
    }
2150
    *device_to_logical = win_font->device_to_logical;
2151
}