w32font.c 51.2 KB
Newer Older
Jason Rumney's avatar
Jason Rumney committed
1
/* Font backend for the Microsoft W32 API.
2
   Copyright (C) 2007, 2008 Free Software Foundation, Inc.
Jason Rumney's avatar
Jason Rumney committed
3 4 5 6 7

This file is part of GNU Emacs.

GNU Emacs is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 3, or (at your option)
Jason Rumney's avatar
Jason Rumney committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.  */

#include <config.h>
#include <windows.h>

#include "lisp.h"
#include "w32term.h"
#include "frame.h"
#include "dispextern.h"
#include "character.h"
#include "charset.h"
#include "fontset.h"
#include "font.h"
Jason Rumney's avatar
Jason Rumney committed
32
#include "w32font.h"
Jason Rumney's avatar
Jason Rumney committed
33

34 35 36 37 38 39 40 41 42 43 44
/* Cleartype available on Windows XP, cleartype_natural from XP SP1.
   The latter does not try to fit cleartype smoothed fonts into the
   same bounding box as the non-antialiased version of the font.
 */
#ifndef CLEARTYPE_QUALITY
#define CLEARTYPE_QUALITY 5
#endif
#ifndef CLEARTYPE_NATURAL_QUALITY
#define CLEARTYPE_NATURAL_QUALITY 6
#endif

Jason Rumney's avatar
Jason Rumney committed
45 46
extern struct font_driver w32font_driver;

47
Lisp_Object Qgdi;
48 49 50
static Lisp_Object Qmonospace, Qsansserif, Qmono, Qsans, Qsans_serif;
static Lisp_Object Qserif, Qscript, Qdecorative;
static Lisp_Object Qraster, Qoutline, Qunknown;
51

52 53 54 55 56
/* antialiasing  */
extern Lisp_Object QCantialias; /* defined in font.c  */
extern Lisp_Object Qnone; /* reuse from w32fns.c  */
static Lisp_Object Qstandard, Qsubpixel, Qnatural;

57 58 59 60 61 62 63 64 65 66 67 68 69 70
/* scripts */
static Lisp_Object Qlatin, Qgreek, Qcoptic, Qcyrillic, Qarmenian, Qhebrew;
static Lisp_Object Qarabic, Qsyriac, Qnko, Qthaana, Qdevanagari, Qbengali;
static Lisp_Object Qgurmukhi, Qgujarati, Qoriya, Qtamil, Qtelugu;
static Lisp_Object Qkannada, Qmalayalam, Qsinhala, Qthai, Qlao;
static Lisp_Object Qtibetan, Qmyanmar, Qgeorgian, Qhangul, Qethiopic;
static Lisp_Object Qcherokee, Qcanadian_aboriginal, Qogham, Qrunic;
static Lisp_Object Qkhmer, Qmongolian, Qsymbol, Qbraille, Qhan;
static Lisp_Object Qideographic_description, Qcjk_misc, Qkana, Qbopomofo;
static Lisp_Object Qkanbun, Qyi, Qbyzantine_musical_symbol;
static Lisp_Object Qmusical_symbol, Qmathematical;

/* Font spacing symbols - defined in font.c.  */
extern Lisp_Object Qc, Qp, Qm;
Jason Rumney's avatar
Jason Rumney committed
71 72 73 74

static void fill_in_logfont P_ ((FRAME_PTR f, LOGFONT *logfont,
                                 Lisp_Object font_spec));

75 76 77
static BYTE w32_antialias_type P_ ((Lisp_Object type));
static Lisp_Object lispy_antialias_type P_ ((BYTE type));

78
static Lisp_Object font_supported_scripts P_ ((FONTSIGNATURE * sig));
Jason Rumney's avatar
Jason Rumney committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

/* From old font code in w32fns.c */
char * w32_to_x_charset P_ ((int charset, char * matching));

static Lisp_Object w32_registry P_ ((LONG w32_charset));

/* EnumFontFamiliesEx callbacks.  */
static int CALLBACK add_font_entity_to_list P_ ((ENUMLOGFONTEX *,
                                                 NEWTEXTMETRICEX *,
                                                 DWORD, LPARAM));
static int CALLBACK add_one_font_entity_to_list P_ ((ENUMLOGFONTEX *,
                                                     NEWTEXTMETRICEX *,
                                                     DWORD, LPARAM));
static int CALLBACK add_font_name_to_list P_ ((ENUMLOGFONTEX *,
                                               NEWTEXTMETRICEX *,
                                               DWORD, LPARAM));

96 97 98 99 100 101 102 103 104 105 106 107 108 109
/* struct passed in as LPARAM arg to EnumFontFamiliesEx, for keeping track
   of what we really want.  */
struct font_callback_data
{
  /* The logfont we are matching against. EnumFontFamiliesEx only matches
     face name and charset, so we need to manually match everything else
     in the callback function.  */
  LOGFONT pattern;
  /* The original font spec or entity.  */
  Lisp_Object orig_font_spec;
  /* The frame the font is being loaded on.  */
  Lisp_Object frame;
  /* The list to add matches to.  */
  Lisp_Object list;
Jason Rumney's avatar
Jason Rumney committed
110 111
  /* Whether to match only opentype fonts.  */
  int opentype_only;
112 113 114 115
};

/* Handles the problem that EnumFontFamiliesEx will not return all
   style variations if the font name is not specified.  */
116
static void list_all_matching_fonts P_ ((struct font_callback_data *match));
117 118


Jason Rumney's avatar
Jason Rumney committed
119 120
/* MingW headers only define this when _WIN32_WINNT >= 0x0500, but we
   target older versions.  */
121
#ifndef GGI_MARK_NONEXISTING_GLYPHS
Jason Rumney's avatar
Jason Rumney committed
122
#define GGI_MARK_NONEXISTING_GLYPHS 1
123
#endif
Jason Rumney's avatar
Jason Rumney committed
124 125 126 127 128 129 130 131 132 133 134 135 136

static int
memq_no_quit (elt, list)
     Lisp_Object elt, list;
{
  while (CONSP (list) && ! EQ (XCAR (list), elt))
    list = XCDR (list);
  return (CONSP (list));
}

/* w32 implementation of get_cache for font backend.
   Return a cache of font-entities on FRAME.  The cache must be a
   cons whose cdr part is the actual cache area.  */
Jason Rumney's avatar
Jason Rumney committed
137
Lisp_Object
138 139
w32font_get_cache (f)
     FRAME_PTR f;
Jason Rumney's avatar
Jason Rumney committed
140
{
141
  struct w32_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
Jason Rumney's avatar
Jason Rumney committed
142 143 144 145 146 147 148 149

  return (dpyinfo->name_list_element);
}

/* w32 implementation of list for font backend.
   List fonts exactly matching with FONT_SPEC on FRAME.  The value
   is a vector of font-entities.  This is the sole API that
   allocates font-entities.  */
150 151 152
static Lisp_Object
w32font_list (frame, font_spec)
     Lisp_Object frame, font_spec;
Jason Rumney's avatar
Jason Rumney committed
153
{
Jason Rumney's avatar
Jason Rumney committed
154
  return w32font_list_internal (frame, font_spec, 0);
Jason Rumney's avatar
Jason Rumney committed
155 156 157 158 159 160
}

/* w32 implementation of match for font backend.
   Return a font entity most closely matching with FONT_SPEC on
   FRAME.  The closeness is detemined by the font backend, thus
   `face-font-selection-order' is ignored here.  */
161 162 163
static Lisp_Object
w32font_match (frame, font_spec)
     Lisp_Object frame, font_spec;
Jason Rumney's avatar
Jason Rumney committed
164
{
Jason Rumney's avatar
Jason Rumney committed
165
  return w32font_match_internal (frame, font_spec, 0);
Jason Rumney's avatar
Jason Rumney committed
166 167 168 169 170
}

/* w32 implementation of list_family for font backend.
   List available families.  The value is a list of family names
   (symbols).  */
171 172 173
static Lisp_Object
w32font_list_family (frame)
     Lisp_Object frame;
Jason Rumney's avatar
Jason Rumney committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
{
  Lisp_Object list = Qnil;
  LOGFONT font_match_pattern;
  HDC dc;
  FRAME_PTR f = XFRAME (frame);

  bzero (&font_match_pattern, sizeof (font_match_pattern));

  dc = get_frame_dc (f);

  EnumFontFamiliesEx (dc, &font_match_pattern,
                      (FONTENUMPROC) add_font_name_to_list,
                      (LPARAM) &list, 0);
  release_frame_dc (f, dc);

  return list;
}

/* w32 implementation of open for font backend.
   Open a font specified by FONT_ENTITY on frame F.
   If the font is scalable, open it with PIXEL_SIZE.  */
195
static struct font *
196 197 198 199
w32font_open (f, font_entity, pixel_size)
     FRAME_PTR f;
     Lisp_Object font_entity;
     int pixel_size;
Jason Rumney's avatar
Jason Rumney committed
200 201 202
{
  struct w32font_info *w32_font = xmalloc (sizeof (struct w32font_info));

203
  if (w32_font == NULL)
Jason Rumney's avatar
Jason Rumney committed
204 205
    return NULL;

206
  if (!w32font_open_internal (f, font_entity, pixel_size, w32_font))
Jason Rumney's avatar
Jason Rumney committed
207 208 209 210 211
    {
      xfree (w32_font);
      return NULL;
    }

212
  return (struct font *) w32_font;
Jason Rumney's avatar
Jason Rumney committed
213 214 215 216
}

/* w32 implementation of close for font_backend.
   Close FONT on frame F.  */
Jason Rumney's avatar
Jason Rumney committed
217
void
218 219 220
w32font_close (f, font)
     FRAME_PTR f;
     struct font *font;
Jason Rumney's avatar
Jason Rumney committed
221 222 223 224
{
  if (font->font.font)
    {
      W32FontStruct *old_w32_font = (W32FontStruct *)font->font.font;
225
      DeleteObject (old_w32_font->hfont);
Jason Rumney's avatar
Jason Rumney committed
226 227 228 229
      xfree (old_w32_font);
      font->font.font = 0;
    }

230 231 232
  if (font->font.full_name && font->font.full_name != font->font.name)
    xfree (font->font.full_name);

Jason Rumney's avatar
Jason Rumney committed
233 234
  if (font->font.name)
    xfree (font->font.name);
235

Jason Rumney's avatar
Jason Rumney committed
236 237 238 239 240 241 242 243
  xfree (font);
}

/* w32 implementation of has_char for font backend.
   Optional.
   If FONT_ENTITY has a glyph for character C (Unicode code point),
   return 1.  If not, return 0.  If a font must be opened to check
   it, return -1.  */
Jason Rumney's avatar
Jason Rumney committed
244
int
245 246 247
w32font_has_char (entity, c)
     Lisp_Object entity;
     int c;
Jason Rumney's avatar
Jason Rumney committed
248
{
249
  Lisp_Object supported_scripts, extra, script;
Jason Rumney's avatar
Jason Rumney committed
250 251
  DWORD mask;

252 253 254 255
  extra = AREF (entity, FONT_EXTRA_INDEX);
  if (!CONSP (extra))
    return -1;

256 257
  supported_scripts = assq_no_quit (QCscript, extra);
  if (!CONSP (supported_scripts))
Jason Rumney's avatar
Jason Rumney committed
258 259
    return -1;

260
  supported_scripts = XCDR (supported_scripts);
Jason Rumney's avatar
Jason Rumney committed
261

262
  script = CHAR_TABLE_REF (Vchar_script_table, c);
Jason Rumney's avatar
Jason Rumney committed
263

264
  return (memq_no_quit (script, supported_scripts)) ? 1 : 0;
Jason Rumney's avatar
Jason Rumney committed
265 266 267 268 269
}

/* w32 implementation of encode_char for font backend.
   Return a glyph code of FONT for characer C (Unicode code point).
   If FONT doesn't have such a glyph, return FONT_INVALID_CODE.  */
Jason Rumney's avatar
Jason Rumney committed
270
unsigned
271 272 273
w32font_encode_char (font, c)
     struct font *font;
     int c;
Jason Rumney's avatar
Jason Rumney committed
274
{
275 276
  /* Avoid unneccesary conversion - all the Win32 APIs will take a unicode
     character.  */
Jason Rumney's avatar
Jason Rumney committed
277 278 279 280 281 282
  return c;
}

/* w32 implementation of text_extents for font backend.
   Perform the size computation of glyphs of FONT and fillin members
   of METRICS.  The glyphs are specified by their glyph codes in
283
   CODE (length NGLYPHS).  Apparently metrics can be NULL, in this
Jason Rumney's avatar
Jason Rumney committed
284
   case just return the overall width.  */
Jason Rumney's avatar
Jason Rumney committed
285
int
286 287 288 289 290
w32font_text_extents (font, code, nglyphs, metrics)
     struct font *font;
     unsigned *code;
     int nglyphs;
     struct font_metrics *metrics;
Jason Rumney's avatar
Jason Rumney committed
291 292
{
  int i;
293 294
  HFONT old_font = NULL;
  HDC dc = NULL;
Jason Rumney's avatar
Jason Rumney committed
295
  struct frame * f;
Jason Rumney's avatar
Jason Rumney committed
296 297
  int total_width = 0;
  WORD *wcode = alloca(nglyphs * sizeof (WORD));
298
  SIZE size;
Jason Rumney's avatar
Jason Rumney committed
299

300 301 302
  /* TODO: Frames can come and go, and their fonts outlive them. So we
     can't cache the frame in the font structure.  Use selected_frame
     until the API is updated to pass in a frame.  */
303
  f = XFRAME (selected_frame);
Jason Rumney's avatar
Jason Rumney committed
304

Jason Rumney's avatar
Jason Rumney committed
305 306 307
  if (metrics)
    {
      GLYPHMETRICS gm;
308 309 310 311 312 313
      MAT2 transform;

      /* Set transform to the identity matrix.  */
      bzero (&transform, sizeof (transform));
      transform.eM11.value = 1;
      transform.eM22.value = 1;
314 315 316
      metrics->width = 0;
      metrics->ascent = 0;
      metrics->descent = 0;
317
      metrics->lbearing = 0;
Jason Rumney's avatar
Jason Rumney committed
318 319 320

      for (i = 0; i < nglyphs; i++)
        {
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
          if (*(code + i) < 128 && *(code + i) > 32)
            {
              /* Use cached metrics for ASCII.  */
              struct font_metrics *char_metric
                = &((struct w32font_info *)font)->ascii_metrics[*(code+i)-32];

              /* If we couldn't get metrics when caching, use fallback.  */
              if (char_metric->width == 0)
                break;

              metrics->lbearing = max (metrics->lbearing,
                                       char_metric->lbearing - metrics->width);
              metrics->rbearing = max (metrics->rbearing,
                                       metrics->width + char_metric->rbearing);
              metrics->width += char_metric->width;
              metrics->ascent = max (metrics->ascent, char_metric->ascent);
              metrics->descent = max (metrics->descent, char_metric->descent);
            }
Jason Rumney's avatar
Jason Rumney committed
339 340
          else
            {
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
              if (dc == NULL)
                {
                  dc = get_frame_dc (f);
                  old_font = SelectObject (dc, ((W32FontStruct *)
                                                (font->font.font))->hfont);
                }
              if (GetGlyphOutlineW (dc, *(code + i), GGO_METRICS, &gm, 0,
                                    NULL, &transform) != GDI_ERROR)
                {
                  int new_val = metrics->width + gm.gmBlackBoxX
                    + gm.gmptGlyphOrigin.x;
                  metrics->rbearing = max (metrics->rbearing, new_val);
                  new_val = -gm.gmptGlyphOrigin.x - metrics->width;
                  metrics->lbearing = max (metrics->lbearing, new_val);
                  metrics->width += gm.gmCellIncX;
                  new_val = -gm.gmptGlyphOrigin.y;
                  metrics->ascent = max (metrics->ascent, new_val);
                  new_val = gm.gmBlackBoxY + gm.gmptGlyphOrigin.y;
                  metrics->descent = max (metrics->descent, new_val);
                }
              else
                {
                  /* Rely on an estimate based on the overall font metrics.  */
                  break;
                }
Jason Rumney's avatar
Jason Rumney committed
366 367
            }
        }
368 369 370 371

      /* If we got through everything, return.  */
      if (i == nglyphs)
        {
372 373 374 375 376 377
          if (dc != NULL)
            {
              /* Restore state and release DC.  */
              SelectObject (dc, old_font);
              release_frame_dc (f, dc);
            }
378 379 380

          return metrics->width;
        }
Jason Rumney's avatar
Jason Rumney committed
381
    }
382 383

  for (i = 0; i < nglyphs; i++)
Jason Rumney's avatar
Jason Rumney committed
384
    {
385 386 387 388 389 390 391
      if (code[i] < 0x10000)
        wcode[i] = code[i];
      else
        {
          /* TODO: Convert to surrogate, reallocating array if needed */
          wcode[i] = 0xffff;
        }
Jason Rumney's avatar
Jason Rumney committed
392 393
    }

394 395 396 397 398 399 400
  if (dc == NULL)
    {
      dc = get_frame_dc (f);
      old_font = SelectObject (dc, ((W32FontStruct *)
                                    (font->font.font))->hfont);
    }

401
  if (GetTextExtentPoint32W (dc, wcode, nglyphs, &size))
Jason Rumney's avatar
Jason Rumney committed
402
    {
403
      total_width = size.cx;
Jason Rumney's avatar
Jason Rumney committed
404 405
    }

406
  if (!total_width)
Jason Rumney's avatar
Jason Rumney committed
407 408 409 410 411 412 413
    {
      RECT rect;
      rect.top = 0; rect.bottom = font->font.height; rect.left = 0; rect.right = 1;
      DrawTextW (dc, wcode, nglyphs, &rect,
                 DT_CALCRECT | DT_NOPREFIX | DT_SINGLELINE);
      total_width = rect.right;
    }
414

415 416 417 418 419 420 421 422 423 424
  if (metrics)
    {
      metrics->width = total_width;
      metrics->ascent = font->ascent;
      metrics->descent = font->descent;
      metrics->lbearing = 0;
      metrics->rbearing = total_width
        + ((struct w32font_info *) font)->metrics.tmOverhang;
    }

Jason Rumney's avatar
Jason Rumney committed
425 426
  /* Restore state and release DC.  */
  SelectObject (dc, old_font);
Jason Rumney's avatar
Jason Rumney committed
427
  release_frame_dc (f, dc);
Jason Rumney's avatar
Jason Rumney committed
428 429 430 431 432 433 434 435 436

  return total_width;
}

/* w32 implementation of draw for font backend.
   Optional.
   Draw glyphs between FROM and TO of S->char2b at (X Y) pixel
   position of frame F with S->FACE and S->GC.  If WITH_BACKGROUND
   is nonzero, fill the background in advance.  It is assured that
437 438 439 440 441 442 443 444
   WITH_BACKGROUND is zero when (FROM > 0 || TO < S->nchars).

   TODO: Currently this assumes that the colors and fonts are already
   set in the DC. This seems to be true now, but maybe only due to
   the old font code setting it up. It may be safer to resolve faces
   and fonts in here and set them explicitly
*/

Jason Rumney's avatar
Jason Rumney committed
445
int
446 447 448
w32font_draw (s, from, to, x, y, with_background)
     struct glyph_string *s;
     int from, to, x, y, with_background;
Jason Rumney's avatar
Jason Rumney committed
449 450
{
  UINT options = 0;
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
  HRGN orig_clip;

  /* Save clip region for later restoration.  */
  GetClipRgn(s->hdc, orig_clip);

  if (s->num_clips > 0)
    {
      HRGN new_clip = CreateRectRgnIndirect (s->clip);

      if (s->num_clips > 1)
        {
          HRGN clip2 = CreateRectRgnIndirect (s->clip + 1);

          CombineRgn (new_clip, new_clip, clip2, RGN_OR);
          DeleteObject (clip2);
        }

      SelectClipRgn (s->hdc, new_clip);
      DeleteObject (new_clip);
    }
Jason Rumney's avatar
Jason Rumney committed
471

472 473 474
  /* Using OPAQUE background mode can clear more background than expected
     when Cleartype is used.  Draw the background manually to avoid this.  */
  SetBkMode (s->hdc, TRANSPARENT);
Jason Rumney's avatar
Jason Rumney committed
475 476
  if (with_background)
    {
477 478
      HBRUSH brush;
      RECT rect;
479
      struct font *font = (struct font *) s->face->font_info;
480 481 482

      brush = CreateSolidBrush (s->gc->background);
      rect.left = x;
483
      rect.top = y - font->ascent;
484
      rect.right = x + s->width;
485
      rect.bottom = y + font->descent;
486
      FillRect (s->hdc, &rect, brush);
487
      DeleteObject (brush);
Jason Rumney's avatar
Jason Rumney committed
488
    }
489

490 491 492 493 494 495
  if (s->padding_p)
    {
      int len = to - from, i;

      for (i = 0; i < len; i++)
	ExtTextOutW (s->hdc, x + i, y, options, NULL,
496
		     s->char2b + from + i, 1, NULL);
497 498 499
    }
  else
    ExtTextOutW (s->hdc, x, y, options, NULL, s->char2b + from, to - from, NULL);
500 501 502 503 504 505

  /* Restore clip region.  */
  if (s->num_clips > 0)
    {
      SelectClipRgn (s->hdc, orig_clip);
    }
Jason Rumney's avatar
Jason Rumney committed
506 507 508 509 510
}

/* w32 implementation of free_entity for font backend.
   Optional (if FONT_EXTRA_INDEX is not Lisp_Save_Value).
   Free FONT_EXTRA_INDEX field of FONT_ENTITY.
511 512
static void
w32font_free_entity (Lisp_Object entity);
Jason Rumney's avatar
Jason Rumney committed
513 514 515 516 517 518 519
  */

/* w32 implementation of prepare_face for font backend.
   Optional (if FACE->extra is not used).
   Prepare FACE for displaying characters by FONT on frame F by
   storing some data in FACE->extra.  If successful, return 0.
   Otherwise, return -1.
520 521
static int
w32font_prepare_face (FRAME_PTR f, struct face *face);
Jason Rumney's avatar
Jason Rumney committed
522 523 524 525
  */
/* w32 implementation of done_face for font backend.
   Optional.
   Done FACE for displaying characters by FACE->font on frame F.
526 527
static void
w32font_done_face (FRAME_PTR f, struct face *face);  */
Jason Rumney's avatar
Jason Rumney committed
528 529 530 531

/* w32 implementation of get_bitmap for font backend.
   Optional.
   Store bitmap data for glyph-code CODE of FONT in BITMAP.  It is
532
   intended that this method is called from the other font-driver
Jason Rumney's avatar
Jason Rumney committed
533
   for actual drawing.
534 535 536
static int
w32font_get_bitmap (struct font *font, unsigned code,
                    struct font_bitmap *bitmap, int bits_per_pixel);
Jason Rumney's avatar
Jason Rumney committed
537 538 539 540
  */
/* w32 implementation of free_bitmap for font backend.
   Optional.
   Free bitmap data in BITMAP.
541 542
static void
w32font_free_bitmap (struct font *font, struct font_bitmap *bitmap);
Jason Rumney's avatar
Jason Rumney committed
543 544 545 546 547
  */
/* w32 implementation of get_outline for font backend.
   Optional.
   Return an outline data for glyph-code CODE of FONT.  The format
   of the outline data depends on the font-driver.
548 549
static void *
w32font_get_outline (struct font *font, unsigned code);
Jason Rumney's avatar
Jason Rumney committed
550 551 552 553
  */
/* w32 implementation of free_outline for font backend.
   Optional.
   Free OUTLINE (that is obtained by the above method).
554 555
static void
w32font_free_outline (struct font *font, void *outline);
Jason Rumney's avatar
Jason Rumney committed
556 557 558 559 560 561
  */
/* w32 implementation of anchor_point for font backend.
   Optional.
   Get coordinates of the INDEXth anchor point of the glyph whose
   code is CODE.  Store the coordinates in *X and *Y.  Return 0 if
   the operations was successfull.  Otherwise return -1.
562 563
static int
w32font_anchor_point (struct font *font, unsigned code,
Jason Rumney's avatar
Jason Rumney committed
564 565 566 567 568 569
                                 int index, int *x, int *y);
  */
/* w32 implementation of otf_capability for font backend.
   Optional.
   Return a list describing which scripts/languages FONT
   supports by which GSUB/GPOS features of OpenType tables.
570 571
static Lisp_Object
w32font_otf_capability (struct font *font);
Jason Rumney's avatar
Jason Rumney committed
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
  */
/* w32 implementation of otf_drive for font backend.
   Optional.
   Apply FONT's OTF-FEATURES to the glyph string.

   FEATURES specifies which OTF features to apply in this format:
      (SCRIPT LANGSYS GSUB-FEATURE GPOS-FEATURE)
   See the documentation of `font-drive-otf' for the detail.

   This method applies the specified features to the codes in the
   elements of GSTRING-IN (between FROMth and TOth).  The output
   codes are stored in GSTRING-OUT at the IDXth element and the
   following elements.

   Return the number of output codes.  If none of the features are
   applicable to the input data, return 0.  If GSTRING-OUT is too
   short, return -1.
589 590 591 592 593
static int
w32font_otf_drive (struct font *font, Lisp_Object features,
                   Lisp_Object gstring_in, int from, int to,
                   Lisp_Object gstring_out, int idx,
                   int alternate_subst);
Jason Rumney's avatar
Jason Rumney committed
594 595
  */

Jason Rumney's avatar
Jason Rumney committed
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
/* Internal implementation of w32font_list.
   Additional parameter opentype_only restricts the returned fonts to
   opentype fonts, which can be used with the Uniscribe backend.  */
Lisp_Object
w32font_list_internal (frame, font_spec, opentype_only)
     Lisp_Object frame, font_spec;
     int opentype_only;
{
  struct font_callback_data match_data;
  HDC dc;
  FRAME_PTR f = XFRAME (frame);

  match_data.orig_font_spec = font_spec;
  match_data.list = Qnil;
  match_data.frame = frame;

  bzero (&match_data.pattern, sizeof (LOGFONT));
  fill_in_logfont (f, &match_data.pattern, font_spec);

  match_data.opentype_only = opentype_only;
  if (opentype_only)
    match_data.pattern.lfOutPrecision = OUT_OUTLINE_PRECIS;

  if (match_data.pattern.lfFaceName[0] == '\0')
    {
      /* EnumFontFamiliesEx does not take other fields into account if
         font name is blank, so need to use two passes.  */
      list_all_matching_fonts (&match_data);
    }
  else
    {
      dc = get_frame_dc (f);

      EnumFontFamiliesEx (dc, &match_data.pattern,
                          (FONTENUMPROC) add_font_entity_to_list,
                          (LPARAM) &match_data, 0);
      release_frame_dc (f, dc);
    }

  return NILP (match_data.list) ? null_vector : Fvconcat (1, &match_data.list);
}

/* Internal implementation of w32font_match.
   Additional parameter opentype_only restricts the returned fonts to
   opentype fonts, which can be used with the Uniscribe backend.  */
Lisp_Object
w32font_match_internal (frame, font_spec, opentype_only)
     Lisp_Object frame, font_spec;
     int opentype_only;
{
  struct font_callback_data match_data;
  HDC dc;
  FRAME_PTR f = XFRAME (frame);

  match_data.orig_font_spec = font_spec;
  match_data.frame = frame;
  match_data.list = Qnil;

  bzero (&match_data.pattern, sizeof (LOGFONT));
  fill_in_logfont (f, &match_data.pattern, font_spec);

  match_data.opentype_only = opentype_only;
  if (opentype_only)
    match_data.pattern.lfOutPrecision = OUT_OUTLINE_PRECIS;

  dc = get_frame_dc (f);

  EnumFontFamiliesEx (dc, &match_data.pattern,
                      (FONTENUMPROC) add_one_font_entity_to_list,
                      (LPARAM) &match_data, 0);
  release_frame_dc (f, dc);

  return NILP (match_data.list) ? Qnil : XCAR (match_data.list);
}

671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
int
w32font_open_internal (f, font_entity, pixel_size, w32_font)
     FRAME_PTR f;
     Lisp_Object font_entity;
     int pixel_size;
     struct w32font_info *w32_font;
{
  int len, size;
  LOGFONT logfont;
  HDC dc;
  HFONT hfont, old_font;
  Lisp_Object val, extra;
  /* For backwards compatibility.  */
  W32FontStruct *compat_w32_font;

  struct font * font = (struct font *) w32_font;
  if (!font)
    return 0;

  bzero (&logfont, sizeof (logfont));
  fill_in_logfont (f, &logfont, font_entity);

  size = XINT (AREF (font_entity, FONT_SIZE_INDEX));
  if (!size)
    size = pixel_size;

  logfont.lfHeight = -size;
  hfont = CreateFontIndirect (&logfont);

  if (hfont == NULL)
    return 0;

  /* Get the metrics for this font.  */
  dc = get_frame_dc (f);
  old_font = SelectObject (dc, hfont);

  GetTextMetrics (dc, &w32_font->metrics);

709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
  /* Cache ASCII metrics.  */
  {
    GLYPHMETRICS gm;
    MAT2 transform;
    int i;

    bzero (&transform, sizeof (transform));
    transform.eM11.value = 1;
    transform.eM22.value = 1;

    for (i = 0; i < 96; i++)
      {
        struct font_metrics* char_metric = &w32_font->ascii_metrics[i];

        if (GetGlyphOutlineW (dc, i + 32, GGO_METRICS, &gm, 0,
                              NULL, &transform) != GDI_ERROR)
          {
            char_metric->lbearing = -gm.gmptGlyphOrigin.x;
            char_metric->rbearing = gm.gmBlackBoxX + gm.gmptGlyphOrigin.x;
            char_metric->width = gm.gmCellIncX;
            char_metric->ascent = -gm.gmptGlyphOrigin.y;
            char_metric->descent = gm.gmBlackBoxY + gm.gmptGlyphOrigin.y;
          }
        else
          char_metric->width = 0;
      }
  }
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
  SelectObject (dc, old_font);
  release_frame_dc (f, dc);
  /* W32FontStruct - we should get rid of this, and use the w32font_info
     struct for any W32 specific fields. font->font.font can then be hfont.  */
  font->font.font = xmalloc (sizeof (W32FontStruct));
  compat_w32_font = (W32FontStruct *) font->font.font;
  bzero (compat_w32_font, sizeof (W32FontStruct));
  compat_w32_font->font_type = UNICODE_FONT;
  /* Duplicate the text metrics.  */
  bcopy (&w32_font->metrics,  &compat_w32_font->tm, sizeof (TEXTMETRIC));
  compat_w32_font->hfont = hfont;

  len = strlen (logfont.lfFaceName);
  font->font.name = (char *) xmalloc (len + 1);
  bcopy (logfont.lfFaceName, font->font.name, len);
  font->font.name[len] = '\0';
752 753 754 755 756 757 758

  {
    char *name;

    /* We don't know how much space we need for the full name, so start with
       96 bytes and go up in steps of 32.  */
    len = 96;
759
    name = xmalloc (len);
760 761
    while (name && font_unparse_fcname (font_entity, pixel_size, name, len) < 0)
      {
762
        char *new = xrealloc (name, len += 32);
763 764

        if (! new)
765
          xfree (name);
766 767 768 769 770 771 772
        name = new;
      }
    if (name)
      font->font.full_name = name;
    else
      font->font.full_name = font->font.name;
  }
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
  font->font.charset = 0;
  font->font.codepage = 0;
  font->font.size = w32_font->metrics.tmMaxCharWidth;
  font->font.height = w32_font->metrics.tmHeight
    + w32_font->metrics.tmExternalLeading;
  font->font.space_width = font->font.average_width
    = w32_font->metrics.tmAveCharWidth;

  font->font.vertical_centering = 0;
  font->font.encoding_type = 0;
  font->font.baseline_offset = 0;
  font->font.relative_compose = 0;
  font->font.default_ascent = w32_font->metrics.tmAscent;
  font->font.font_encoder = NULL;
  font->entity = font_entity;
  font->pixel_size = size;
  font->driver = &w32font_driver;
  font->format = Qgdi;
  font->file_name = NULL;
  font->encoding_charset = -1;
  font->repertory_charset = -1;
794 795
  /* TODO: do we really want the minimum width here, which could be negative? */
  font->min_width = font->font.space_width;
796 797 798 799
  font->ascent = w32_font->metrics.tmAscent;
  font->descent = w32_font->metrics.tmDescent;
  font->scalable = w32_font->metrics.tmPitchAndFamily & TMPF_VECTOR;

800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
  /* Set global flag fonts_changed_p to non-zero if the font loaded
     has a character with a smaller width than any other character
     before, or if the font loaded has a smaller height than any other
     font loaded before.  If this happens, it will make a glyph matrix
     reallocation necessary.  */
  {
    struct w32_display_info *dpyinfo = FRAME_W32_DISPLAY_INFO (f);
    dpyinfo->n_fonts++;

    if (dpyinfo->n_fonts == 1)
      {
        dpyinfo->smallest_font_height = font->font.height;
        dpyinfo->smallest_char_width = font->min_width;
      }
    else
      {
        if (dpyinfo->smallest_font_height > font->font.height)
          {
            dpyinfo->smallest_font_height = font->font.height;
            fonts_changed_p |= 1;
          }
        if (dpyinfo->smallest_char_width > font->min_width)
          {
            dpyinfo->smallest_char_width = font->min_width;
            fonts_changed_p |= 1;
          }
      }
  }

829 830 831
  return 1;
}

Jason Rumney's avatar
Jason Rumney committed
832 833
/* Callback function for EnumFontFamiliesEx.
 * Adds the name of a font to a Lisp list (passed in as the lParam arg).  */
834 835 836 837 838 839
static int CALLBACK
add_font_name_to_list (logical_font, physical_font, font_type, list_object)
     ENUMLOGFONTEX *logical_font;
     NEWTEXTMETRICEX *physical_font;
     DWORD font_type;
     LPARAM list_object;
Jason Rumney's avatar
Jason Rumney committed
840 841
{
  Lisp_Object* list = (Lisp_Object *) list_object;
842 843 844 845 846 847 848 849
  Lisp_Object family;

  /* Skip vertical fonts (intended only for printing)  */
  if (logical_font->elfLogFont.lfFaceName[0] == '@')
    return 1;

  family = intern_downcase (logical_font->elfLogFont.lfFaceName,
                            strlen (logical_font->elfLogFont.lfFaceName));
Jason Rumney's avatar
Jason Rumney committed
850 851 852 853 854 855 856
  if (! memq_no_quit (family, *list))
    *list = Fcons (family, *list);

  return 1;
}

/* Convert an enumerated Windows font to an Emacs font entity.  */
857
static Lisp_Object
858 859
w32_enumfont_pattern_entity (frame, logical_font, physical_font,
                             font_type, requested_font)
860
     Lisp_Object frame;
861 862 863
     ENUMLOGFONTEX *logical_font;
     NEWTEXTMETRICEX *physical_font;
     DWORD font_type;
864
     LOGFONT *requested_font;
Jason Rumney's avatar
Jason Rumney committed
865 866 867 868 869
{
  Lisp_Object entity, tem;
  LOGFONT *lf = (LOGFONT*) logical_font;
  BYTE generic_type;

870
  entity = Fmake_vector (make_number (FONT_ENTITY_MAX), Qnil);
Jason Rumney's avatar
Jason Rumney committed
871

Jason Rumney's avatar
Jason Rumney committed
872
  ASET (entity, FONT_TYPE_INDEX, Qgdi);
873
  ASET (entity, FONT_FRAME_INDEX, frame);
Jason Rumney's avatar
Jason Rumney committed
874 875 876 877
  ASET (entity, FONT_REGISTRY_INDEX, w32_registry (lf->lfCharSet));
  ASET (entity, FONT_OBJLIST_INDEX, Qnil);

  /* Foundry is difficult to get in readable form on Windows.
878 879 880 881 882 883 884 885 886 887 888 889 890
     But Emacs crashes if it is not set, so set it to something more
     generic.  Thes values make xflds compatible with Emacs 22. */
  if (lf->lfOutPrecision == OUT_STRING_PRECIS)
    tem = Qraster;
  else if (lf->lfOutPrecision == OUT_STROKE_PRECIS)
    tem = Qoutline;
  else
    tem = Qunknown;

  ASET (entity, FONT_FOUNDRY_INDEX, tem);

  /* Save the generic family in the extra info, as it is likely to be
     useful to users looking for a close match.  */
Jason Rumney's avatar
Jason Rumney committed
891 892 893 894
  generic_type = physical_font->ntmTm.tmPitchAndFamily & 0xF0;
  if (generic_type == FF_DECORATIVE)
    tem = Qdecorative;
  else if (generic_type == FF_MODERN)
895
    tem = Qmono;
Jason Rumney's avatar
Jason Rumney committed
896
  else if (generic_type == FF_ROMAN)
897
    tem = Qserif;
Jason Rumney's avatar
Jason Rumney committed
898 899 900
  else if (generic_type == FF_SCRIPT)
    tem = Qscript;
  else if (generic_type == FF_SWISS)
901
    tem = Qsans;
Jason Rumney's avatar
Jason Rumney committed
902
  else
903 904 905
    tem = null_string;

  ASET (entity, FONT_ADSTYLE_INDEX, tem);
Jason Rumney's avatar
Jason Rumney committed
906

907 908 909 910
  if (physical_font->ntmTm.tmPitchAndFamily & 0x01)
    font_put_extra (entity, QCspacing, make_number (FONT_SPACING_PROPORTIONAL));
  else
    font_put_extra (entity, QCspacing, make_number (FONT_SPACING_MONO));
Jason Rumney's avatar
Jason Rumney committed
911

912 913 914 915 916
  if (requested_font->lfQuality != DEFAULT_QUALITY)
    {
      font_put_extra (entity, QCantialias,
                      lispy_antialias_type (requested_font->lfQuality));
    }
Jason Rumney's avatar
Jason Rumney committed
917 918 919 920 921
  ASET (entity, FONT_FAMILY_INDEX,
        intern_downcase (lf->lfFaceName, strlen (lf->lfFaceName)));

  ASET (entity, FONT_WEIGHT_INDEX, make_number (lf->lfWeight));
  ASET (entity, FONT_SLANT_INDEX, make_number (lf->lfItalic ? 200 : 100));
922 923 924
  /* TODO: PANOSE struct has this info, but need to call GetOutlineTextMetrics
     to get it.  */
  ASET (entity, FONT_WIDTH_INDEX, make_number (100));
Jason Rumney's avatar
Jason Rumney committed
925

926 927 928 929
  if (font_type & RASTER_FONTTYPE)
    ASET (entity, FONT_SIZE_INDEX, make_number (physical_font->ntmTm.tmHeight));
  else
    ASET (entity, FONT_SIZE_INDEX, make_number (0));
Jason Rumney's avatar
Jason Rumney committed
930 931 932

  /* Cache unicode codepoints covered by this font, as there is no other way
     of getting this information easily.  */
933
  if (font_type & TRUETYPE_FONTTYPE)
Jason Rumney's avatar
Jason Rumney committed
934
    {
935 936
      font_put_extra (entity, QCscript,
                      font_supported_scripts (&physical_font->ntmFontSig));
Jason Rumney's avatar
Jason Rumney committed
937
    }
938

Jason Rumney's avatar
Jason Rumney committed
939 940 941
  return entity;
}

942 943 944 945 946 947 948 949

/* Convert generic families to the family portion of lfPitchAndFamily.  */
BYTE
w32_generic_family (Lisp_Object name)
{
  /* Generic families.  */
  if (EQ (name, Qmonospace) || EQ (name, Qmono))
    return FF_MODERN;
950
  else if (EQ (name, Qsans) || EQ (name, Qsans_serif) || EQ (name, Qsansserif))
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
    return FF_SWISS;
  else if (EQ (name, Qserif))
    return FF_ROMAN;
  else if (EQ (name, Qdecorative))
    return FF_DECORATIVE;
  else if (EQ (name, Qscript))
    return FF_SCRIPT;
  else
    return FF_DONTCARE;
}

static int
logfonts_match (font, pattern)
     LOGFONT *font, *pattern;
{
  /* Only check height for raster fonts.  */
  if (pattern->lfHeight && font->lfOutPrecision == OUT_STRING_PRECIS
      && font->lfHeight != pattern->lfHeight)
    return 0;

  /* Have some flexibility with weights.  */
  if (pattern->lfWeight
      && ((font->lfWeight < (pattern->lfWeight - 150))
          || font->lfWeight > (pattern->lfWeight + 150)))
      return 0;

  /* Charset and face should be OK.  Italic has to be checked
     against the original spec, in case we don't have any preference.  */
  return 1;
}

static int
font_matches_spec (type, font, spec)
     DWORD type;
     NEWTEXTMETRICEX *font;
     Lisp_Object spec;
{
  Lisp_Object extra, val;

  /* Check italic. Can't check logfonts, since it is a boolean field,
     so there is no difference between "non-italic" and "don't care".  */
  val = AREF (spec, FONT_SLANT_INDEX);
  if (INTEGERP (val))
    {
      int slant = XINT (val);
      if ((slant > 150 && !font->ntmTm.tmItalic)
          || (slant <= 150 && font->ntmTm.tmItalic))
        return 0;
    }

1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
  /* Check adstyle against generic family.  */
  val = AREF (spec, FONT_ADSTYLE_INDEX);
  if (!NILP (val))
    {
      BYTE family = w32_generic_family (val);
      if (family != FF_DONTCARE
          && family != (font->ntmTm.tmPitchAndFamily & 0xF0))
        return 0;
    }

1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
  /* Check extra parameters.  */
  for (extra = AREF (spec, FONT_EXTRA_INDEX);
       CONSP (extra); extra = XCDR (extra))
    {
      Lisp_Object extra_entry;
      extra_entry = XCAR (extra);
      if (CONSP (extra_entry))
        {
          Lisp_Object key = XCAR (extra_entry);
          val = XCDR (extra_entry);
1021
          if (EQ (key, QCspacing))
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
            {
              int proportional;
              if (INTEGERP (val))
                {
                  int spacing = XINT (val);
                  proportional = (spacing < FONT_SPACING_MONO);
                }
              else if (EQ (val, Qp))
                proportional = 1;
              else if (EQ (val, Qc) || EQ (val, Qm))
                proportional = 0;
              else
                return 0; /* Bad font spec.  */

              if ((proportional && !(font->ntmTm.tmPitchAndFamily & 0x01))
                  || (!proportional && (font->ntmTm.tmPitchAndFamily & 0x01)))
                return 0;
            }
          else if (EQ (key, QCscript) && SYMBOLP (val))
            {
              /* Only truetype fonts will have information about what
                 scripts they support.  This probably means the user
                 will have to force Emacs to use raster, postscript
                 or atm fonts for non-ASCII text.  */
              if (type & TRUETYPE_FONTTYPE)
                {
                  Lisp_Object support
                    = font_supported_scripts (&font->ntmFontSig);
                  if (! memq_no_quit (val, support))
                    return 0;
                }
              else
                {
                  /* Return specific matches, but play it safe. Fonts
                     that cover more than their charset would suggest
                     are likely to be truetype or opentype fonts,
                     covered above.  */
                  if (EQ (val, Qlatin))
                    {
                      /* Although every charset but symbol, thai and
                         arabic contains the basic ASCII set of latin
                         characters, Emacs expects much more.  */
                      if (font->ntmTm.tmCharSet != ANSI_CHARSET)
                        return 0;
                    }
                  else if (EQ (val, Qsymbol))
                    {
                      if (font->ntmTm.tmCharSet != SYMBOL_CHARSET)
                        return 0;
                    }
                  else if (EQ (val, Qcyrillic))
                    {
                      if (font->ntmTm.tmCharSet != RUSSIAN_CHARSET)
                        return 0;
                    }
                  else if (EQ (val, Qgreek))
                    {
                      if (font->ntmTm.tmCharSet != GREEK_CHARSET)
                        return 0;
                    }
                  else if (EQ (val, Qarabic))
                    {
                      if (font->ntmTm.tmCharSet != ARABIC_CHARSET)
                        return 0;
                    }
                  else if (EQ (val, Qhebrew))
                    {
                      if (font->ntmTm.tmCharSet != HEBREW_CHARSET)
                        return 0;
                    }
                  else if (EQ (val, Qthai))
                    {
                      if (font->ntmTm.tmCharSet != THAI_CHARSET)
                        return 0;
                    }
                  else if (EQ (val, Qkana))
                    {
                      if (font->ntmTm.tmCharSet != SHIFTJIS_CHARSET)
                        return 0;
                    }
                  else if (EQ (val, Qbopomofo))
                    {
                      if (font->ntmTm.tmCharSet != CHINESEBIG5_CHARSET)
                        return 0;
                    }
                  else if (EQ (val, Qhangul))
                    {
                      if (font->ntmTm.tmCharSet != HANGUL_CHARSET
                          && font->ntmTm.tmCharSet != JOHAB_CHARSET)
                        return 0;
                    }
                  else if (EQ (val, Qhan))
                    {
                      if (font->ntmTm.tmCharSet != CHINESEBIG5_CHARSET
                          && font->ntmTm.tmCharSet != GB2312_CHARSET
                          && font->ntmTm.tmCharSet != HANGUL_CHARSET
                          && font->ntmTm.tmCharSet != JOHAB_CHARSET
                          && font->ntmTm.tmCharSet != SHIFTJIS_CHARSET)
                        return 0;
                    }
                  else
                    /* Other scripts unlikely to be handled.  */
                    return 0;
                }
            }
        }
    }
  return 1;
}

Jason Rumney's avatar
Jason Rumney committed
1132
/* Callback function for EnumFontFamiliesEx.
1133 1134 1135 1136
 * Checks if a font matches everything we are trying to check agaist,
 * and if so, adds it to a list. Both the data we are checking against
 * and the list to which the fonts are added are passed in via the
 * lparam argument, in the form of a font_callback_data struct. */
1137
static int CALLBACK
1138
add_font_entity_to_list (logical_font, physical_font, font_type, lParam)
1139 1140 1141
     ENUMLOGFONTEX *logical_font;
     NEWTEXTMETRICEX *physical_font;
     DWORD font_type;
1142
     LPARAM lParam;
Jason Rumney's avatar
Jason Rumney committed
1143
{
1144 1145
  struct font_callback_data *match_data
    = (struct font_callback_data *) lParam;
Jason Rumney's avatar
Jason Rumney committed
1146

Jason Rumney's avatar
Jason Rumney committed
1147 1148 1149
  if ((!match_data->opentype_only
       || (physical_font->ntmTm.ntmFlags & NTMFLAGS_OPENTYPE))
      && logfonts_match (&logical_font->elfLogFont, &match_data->pattern)
1150
      && font_matches_spec (font_type, physical_font,
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
                            match_data->orig_font_spec)
      /* Avoid substitutions involving raster fonts (eg Helv -> MS Sans Serif)
         We limit this to raster fonts, because the test can catch some
         genuine fonts (eg the full name of DejaVu Sans Mono Light is actually
         DejaVu Sans Mono ExtraLight). Helvetica -> Arial substitution will
         therefore get through this test.  Since full names can be prefixed
         by a foundry, we accept raster fonts if the font name is found
         anywhere within the full name.  */
      && (logical_font->elfLogFont.lfOutPrecision != OUT_STRING_PRECIS
          || strstr (logical_font->elfFullName,
                     logical_font->elfLogFont.lfFaceName)))
1162 1163 1164
    {
      Lisp_Object entity
        = w32_enumfont_pattern_entity (match_data->frame, logical_font,
1165 1166
                                       physical_font, font_type,
                                       &match_data->pattern);
1167 1168 1169
      if (!NILP (entity))
        match_data->list = Fcons (entity, match_data->list);
    }
Jason Rumney's avatar
Jason Rumney committed
1170 1171 1172 1173
  return 1;
}

/* Callback function for EnumFontFamiliesEx.
1174
 * Terminates the search once we have a match. */
1175
static int CALLBACK
1176
add_one_font_entity_to_list (logical_font, physical_font, font_type, lParam)
1177 1178 1179
     ENUMLOGFONTEX *logical_font;
     NEWTEXTMETRICEX *physical_font;
     DWORD font_type;
1180
     LPARAM lParam;
Jason Rumney's avatar
Jason Rumney committed
1181
{
1182 1183 1184 1185 1186 1187
  struct font_callback_data *match_data
    = (struct font_callback_data *) lParam;
  add_font_entity_to_list (logical_font, physical_font, font_type, lParam);

  /* If we have a font in the list, terminate the search.  */
  return !NILP (match_data->list);
Jason Rumney's avatar
Jason Rumney committed
1188 1189 1190
}

/* Convert a Lisp font registry (symbol) to a windows charset.  */
1191 1192 1193
static LONG
registry_to_w32_charset (charset)
     Lisp_Object charset;
Jason Rumney's avatar
Jason Rumney committed
1194 1195 1196 1197 1198 1199
{
  if (EQ (charset, Qiso10646_1) || EQ (charset, Qunicode_bmp)
      || EQ (charset, Qunicode_sip))
    return DEFAULT_CHARSET; /* UNICODE_CHARSET not defined in MingW32 */
  else if (EQ (charset, Qiso8859_1))
    return ANSI_CHARSET;
1200 1201
  else if (SYMBOLP (charset))
    return x_to_w32_charset (SDATA (SYMBOL_NAME (charset)));
Jason Rumney's avatar
Jason Rumney committed
1202 1203 1204 1205 1206 1207
  else if (STRINGP (charset))
    return x_to_w32_charset (SDATA (charset));
  else
    return DEFAULT_CHARSET;
}

1208 1209 1210
static Lisp_Object
w32_registry (w32_charset)
     LONG w32_charset;
Jason Rumney's avatar
Jason Rumney committed
1211 1212
{
  if (w32_charset == ANSI_CHARSET)
1213
    return Qiso10646_1;
Jason Rumney's avatar
Jason Rumney committed
1214
  else
1215 1216 1217 1218
    {
      char * charset = w32_to_x_charset (w32_charset, NULL);
      return intern_downcase (charset, strlen(charset));
    }
Jason Rumney's avatar
Jason Rumney committed
1219 1220 1221
}

/* Fill in all the available details of LOGFONT from FONT_SPEC.  */
1222 1223 1224 1225 1226
static void
fill_in_logfont (f, logfont, font_spec)
     FRAME_PTR f;
     LOGFONT *logfont;
     Lisp_Object font_spec;
Jason Rumney's avatar
Jason Rumney committed
1227
{
1228
  Lisp_Object tmp, extra;
Jason Rumney's avatar
Jason Rumney committed
1229 1230
  int dpi = FRAME_W32_DISPLAY_INFO (f)->resy;

1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
  extra = AREF (font_spec, FONT_EXTRA_INDEX);
  /* Allow user to override dpi settings.  */
  if (CONSP (extra))
    {
      tmp = assq_no_quit (QCdpi, extra);
      if (CONSP (tmp) && INTEGERP (XCDR (tmp)))
        {
          dpi = XINT (XCDR (tmp));
        }
      else if (CONSP (tmp) && FLOATP (XCDR (tmp)))
        {
          dpi = (int) (XFLOAT_DATA (XCDR (tmp)) + 0.5);
        }
    }
Jason Rumney's avatar
Jason Rumney committed
1245 1246 1247 1248

  /* Height  */
  tmp = AREF (font_spec, FONT_SIZE_INDEX);
  if (INTEGERP (tmp))
1249
    logfont->lfHeight = -1 * XINT (tmp);
Jason Rumney's avatar
Jason Rumney committed
1250
  else if (FLOATP (tmp))
1251
    logfont->lfHeight = (int) (-1.0 *  dpi * XFLOAT_DATA (tmp) / 72.27 + 0.5);
Jason Rumney's avatar
Jason Rumney committed
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276

  /* Escapement  */

  /* Orientation  */

  /* Weight  */
  tmp = AREF (font_spec, FONT_WEIGHT_INDEX);
  if (INTEGERP (tmp))
    logfont->lfWeight = XINT (tmp);

  /* Italic  */
  tmp = AREF (font_spec, FONT_SLANT_INDEX);
  if (INTEGERP (tmp))
    {
      int slant = XINT (tmp);
      logfont->lfItalic = slant > 150 ? 1 : 0;
    }

  /* Underline  */

  /* Strikeout  */

  /* Charset  */
  tmp = AREF (font_spec, FONT_REGISTRY_INDEX);
  if (! NILP (tmp))