w32font.c 91.8 KB
Newer Older
1
/* Font backend for the Microsoft Windows API.
Paul Eggert's avatar
Paul Eggert committed
2
   Copyright (C) 2007-2020 Free Software Foundation, Inc.
Jason Rumney's avatar
Jason Rumney committed
3 4 5

This file is part of GNU Emacs.

6
GNU Emacs is free software: you can redistribute it and/or modify
Jason Rumney's avatar
Jason Rumney committed
7
it under the terms of the GNU General Public License as published by
8 9
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
Jason Rumney's avatar
Jason Rumney committed
10 11 12 13 14 15 16

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
Paul Eggert's avatar
Paul Eggert committed
17
along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
Jason Rumney's avatar
Jason Rumney committed
18 19 20

#include <config.h>
#include <windows.h>
Daniel Colascione's avatar
Daniel Colascione committed
21
#include <stdio.h>
Juanma Barranquero's avatar
Juanma Barranquero committed
22
#include <math.h>
Juanma Barranquero's avatar
Juanma Barranquero committed
23
#include <ctype.h>
24
#include <commdlg.h>
Jason Rumney's avatar
Jason Rumney committed
25 26 27 28

#include "lisp.h"
#include "w32term.h"
#include "frame.h"
29
#include "coding.h"	/* for ENCODE_SYSTEM, DECODE_SYSTEM */
Jason Rumney's avatar
Jason Rumney committed
30
#include "w32font.h"
31
#ifdef WINDOWSNT
32
#include "w32common.h"
33 34
#include "w32.h"
#endif
Jason Rumney's avatar
Jason Rumney committed
35

Daniel Colascione's avatar
Daniel Colascione committed
36 37
#include "pdumper.h"

38 39 40 41 42 43 44 45 46 47 48
/* 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

49 50 51 52 53 54 55 56 57
/* VIETNAMESE_CHARSET and JOHAB_CHARSET are not defined in some versions
   of MSVC headers.  */
#ifndef VIETNAMESE_CHARSET
#define VIETNAMESE_CHARSET 163
#endif
#ifndef JOHAB_CHARSET
#define JOHAB_CHARSET 130
#endif

Dmitry Antipov's avatar
Dmitry Antipov committed
58
static void fill_in_logfont (struct frame *, LOGFONT *, Lisp_Object);
Jason Rumney's avatar
Jason Rumney committed
59

60 61
static BYTE w32_antialias_type (Lisp_Object);
static Lisp_Object lispy_antialias_type (BYTE);
62

63 64 65 66
static Lisp_Object font_supported_scripts (FONTSIGNATURE *);
static int w32font_full_name (LOGFONT *, Lisp_Object, int, char *, int);
static void compute_metrics (HDC, struct w32font_info *, unsigned int,
                             struct w32_metric_cache *);
Jason Rumney's avatar
Jason Rumney committed
67

68
static Lisp_Object w32_registry (LONG, DWORD);
Jason Rumney's avatar
Jason Rumney committed
69 70

/* EnumFontFamiliesEx callbacks.  */
71 72 73 74 75 76 77 78 79
static int CALLBACK ALIGN_STACK add_font_entity_to_list (ENUMLOGFONTEX *,
							 NEWTEXTMETRICEX *,
							 DWORD, LPARAM);
static int CALLBACK ALIGN_STACK add_one_font_entity_to_list (ENUMLOGFONTEX *,
							     NEWTEXTMETRICEX *,
							     DWORD, LPARAM);
static int CALLBACK ALIGN_STACK add_font_name_to_list (ENUMLOGFONTEX *,
						       NEWTEXTMETRICEX *,
						       DWORD, LPARAM);
Jason Rumney's avatar
Jason Rumney committed
80

81 82 83 84 85 86 87 88 89 90 91 92
/* 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;
93 94
  /* Fonts known to support the font spec, or nil if none.  */
  Lisp_Object known_fonts;
95 96
  /* The list to add matches to.  */
  Lisp_Object list;
Jason Rumney's avatar
Jason Rumney committed
97
  /* Whether to match only opentype fonts.  */
98
  bool opentype_only;
99 100 101 102
};

/* Handles the problem that EnumFontFamiliesEx will not return all
   style variations if the font name is not specified.  */
103
static void list_all_matching_fonts (struct font_callback_data *);
104

105 106
#ifdef WINDOWSNT

107 108 109
static BOOL g_b_init_get_outline_metrics_w;
static BOOL g_b_init_get_text_metrics_w;
static BOOL g_b_init_get_glyph_outline_w;
110
static BOOL g_b_init_get_char_width_32_w;
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

typedef UINT (WINAPI * GetOutlineTextMetricsW_Proc) (
   HDC hdc,
   UINT cbData,
   LPOUTLINETEXTMETRICW lpotmw);
typedef BOOL (WINAPI * GetTextMetricsW_Proc) (
   HDC hdc,
   LPTEXTMETRICW lptmw);
typedef DWORD (WINAPI * GetGlyphOutlineW_Proc) (
   HDC hdc,
   UINT uChar,
   UINT uFormat,
   LPGLYPHMETRICS lpgm,
   DWORD cbBuffer,
   LPVOID lpvBuffer,
   const MAT2 *lpmat2);
127 128 129 130 131
typedef BOOL (WINAPI * GetCharWidth32W_Proc) (
   HDC hdc,
   UINT uFirstChar,
   UINT uLastChar,
   LPINT lpBuffer);
132 133 134 135 136 137 138 139 140 141 142 143

/* Several "wide" functions we use to support the font backends are
   unavailable on Windows 9X, unless UNICOWS.DLL is installed (their
   versions in the default libraries are non-functional stubs).  On NT
   and later systems, these functions are in GDI32.DLL.  The following
   helper function attempts to load UNICOWS.DLL on Windows 9X, and
   refuses to let Emacs start up if that library is not found.  On NT
   and later versions, it simply loads GDI32.DLL, which should always
   be available.  */
static HMODULE
w32_load_unicows_or_gdi32 (void)
{
144
  return maybe_load_unicows_dll ();
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
}

/* The following 3 functions call the problematic "wide" APIs via
   function pointers, to avoid linking against the non-standard
   libunicows on W9X.  */
static UINT WINAPI
get_outline_metrics_w(HDC hdc, UINT cbData, LPOUTLINETEXTMETRICW lpotmw)
{
  static GetOutlineTextMetricsW_Proc s_pfn_Get_Outline_Text_MetricsW = NULL;
  HMODULE hm_unicows = NULL;
  if (g_b_init_get_outline_metrics_w == 0)
    {
      g_b_init_get_outline_metrics_w = 1;
      hm_unicows = w32_load_unicows_or_gdi32 ();
      if (hm_unicows)
	s_pfn_Get_Outline_Text_MetricsW = (GetOutlineTextMetricsW_Proc)
161
	  get_proc_addr (hm_unicows, "GetOutlineTextMetricsW");
162
    }
163
  eassert (s_pfn_Get_Outline_Text_MetricsW != NULL);
164 165 166 167 168 169 170 171 172 173 174 175 176 177
  return s_pfn_Get_Outline_Text_MetricsW (hdc, cbData, lpotmw);
}

static BOOL WINAPI
get_text_metrics_w(HDC hdc, LPTEXTMETRICW lptmw)
{
  static GetTextMetricsW_Proc s_pfn_Get_Text_MetricsW = NULL;
  HMODULE hm_unicows = NULL;
  if (g_b_init_get_text_metrics_w == 0)
    {
      g_b_init_get_text_metrics_w = 1;
      hm_unicows = w32_load_unicows_or_gdi32 ();
      if (hm_unicows)
	s_pfn_Get_Text_MetricsW = (GetTextMetricsW_Proc)
178
	  get_proc_addr (hm_unicows, "GetTextMetricsW");
179
    }
180
  eassert (s_pfn_Get_Text_MetricsW != NULL);
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
  return s_pfn_Get_Text_MetricsW (hdc, lptmw);
}

static DWORD WINAPI
get_glyph_outline_w (HDC hdc, UINT uChar, UINT uFormat, LPGLYPHMETRICS lpgm,
		     DWORD cbBuffer, LPVOID lpvBuffer, const MAT2 *lpmat2)
{
  static GetGlyphOutlineW_Proc s_pfn_Get_Glyph_OutlineW = NULL;
  HMODULE hm_unicows = NULL;
  if (g_b_init_get_glyph_outline_w == 0)
    {
      g_b_init_get_glyph_outline_w = 1;
      hm_unicows = w32_load_unicows_or_gdi32 ();
      if (hm_unicows)
	s_pfn_Get_Glyph_OutlineW = (GetGlyphOutlineW_Proc)
196
	  get_proc_addr (hm_unicows, "GetGlyphOutlineW");
197
    }
198
  eassert (s_pfn_Get_Glyph_OutlineW != NULL);
199 200 201
  return s_pfn_Get_Glyph_OutlineW (hdc, uChar, uFormat, lpgm, cbBuffer,
				   lpvBuffer, lpmat2);
}
Jason Rumney's avatar
Jason Rumney committed
202

203 204
static DWORD WINAPI
get_char_width_32_w (HDC hdc, UINT uFirstChar, UINT uLastChar, LPINT lpBuffer)
205 206 207 208 209 210 211 212 213
{
  static GetCharWidth32W_Proc s_pfn_Get_Char_Width_32W = NULL;
  HMODULE hm_unicows = NULL;
  if (g_b_init_get_char_width_32_w == 0)
    {
      g_b_init_get_char_width_32_w = 1;
      hm_unicows = w32_load_unicows_or_gdi32 ();
      if (hm_unicows)
	s_pfn_Get_Char_Width_32W = (GetCharWidth32W_Proc)
214
	  get_proc_addr (hm_unicows, "GetCharWidth32W");
215 216 217 218 219
    }
  eassert (s_pfn_Get_Char_Width_32W != NULL);
  return s_pfn_Get_Char_Width_32W (hdc, uFirstChar, uLastChar, lpBuffer);
}

220 221 222 223 224 225 226 227 228 229 230 231
#else  /* Cygwin */

/* Cygwin doesn't support Windows 9X, and links against GDI32.DLL, so
   it can just call these functions directly.  */
#define get_outline_metrics_w(h,d,o)   GetOutlineTextMetricsW(h,d,o)
#define get_text_metrics_w(h,t)        GetTextMetricsW(h,t)
#define get_glyph_outline_w(h,uc,f,gm,b,v,m) \
                                       GetGlyphOutlineW(h,uc,f,gm,b,v,m)
#define get_char_width_32_w(h,fc,lc,b) GetCharWidth32W(h,fc,lc,b)

#endif	/* Cygwin */

Jason Rumney's avatar
Jason Rumney committed
232
static int
233
memq_no_quit (Lisp_Object elt, Lisp_Object list)
Jason Rumney's avatar
Jason Rumney committed
234 235 236 237 238 239
{
  while (CONSP (list) && ! EQ (XCAR (list), elt))
    list = XCDR (list);
  return (CONSP (list));
}

240
Lisp_Object
241
intern_font_name (char * string)
242
{
243
  Lisp_Object str = DECODE_SYSTEM (build_string (string));
244
  ptrdiff_t len = SCHARS (str);
245
  Lisp_Object obarray = check_obarray (Vobarray);
246
  Lisp_Object tem = oblookup (obarray, SSDATA (str), len, len);
247
  /* This code is similar to intern function from lread.c.  */
248
  return SYMBOLP (tem) ? tem : intern_driver (str, obarray, tem);
249 250
}

Jason Rumney's avatar
Jason Rumney committed
251 252 253
/* 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
254
Lisp_Object
Dmitry Antipov's avatar
Dmitry Antipov committed
255
w32font_get_cache (struct frame *f)
Jason Rumney's avatar
Jason Rumney committed
256
{
257
  struct w32_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
Jason Rumney's avatar
Jason Rumney committed
258 259 260 261 262 263 264 265

  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.  */
266
static Lisp_Object
267
w32font_list (struct frame *f, Lisp_Object font_spec)
Jason Rumney's avatar
Jason Rumney committed
268
{
269
  Lisp_Object fonts = w32font_list_internal (f, font_spec, 0);
270
  FONT_ADD_LOG ("w32font-list", font_spec, fonts);
271
  return fonts;
Jason Rumney's avatar
Jason Rumney committed
272 273 274 275
}

/* w32 implementation of match for font backend.
   Return a font entity most closely matching with FONT_SPEC on
Paul Eggert's avatar
Paul Eggert committed
276
   FRAME.  The closeness is determined by the font backend, thus
Jason Rumney's avatar
Jason Rumney committed
277
   `face-font-selection-order' is ignored here.  */
278
static Lisp_Object
279
w32font_match (struct frame *f, Lisp_Object font_spec)
Jason Rumney's avatar
Jason Rumney committed
280
{
281
  Lisp_Object entity = w32font_match_internal (f, font_spec, 0);
282
  FONT_ADD_LOG ("w32font-match", font_spec, entity);
283
  return entity;
Jason Rumney's avatar
Jason Rumney committed
284 285 286 287 288
}

/* w32 implementation of list_family for font backend.
   List available families.  The value is a list of family names
   (symbols).  */
289
static Lisp_Object
290
w32font_list_family (struct frame *f)
Jason Rumney's avatar
Jason Rumney committed
291 292
{
  Lisp_Object list = Qnil;
293
  Lisp_Object prev_quit = Vinhibit_quit;
Jason Rumney's avatar
Jason Rumney committed
294 295 296
  LOGFONT font_match_pattern;
  HDC dc;

297
  memset (&font_match_pattern, 0, sizeof (font_match_pattern));
298
  font_match_pattern.lfCharSet = DEFAULT_CHARSET;
Jason Rumney's avatar
Jason Rumney committed
299

300 301 302 303 304
  /* Prevent quitting while EnumFontFamiliesEx runs and conses the
     list it will return.  That's because get_frame_dc acquires the
     critical section, so we cannot quit before we release it in
     release_frame_dc.  */
  Vinhibit_quit = Qt;
Jason Rumney's avatar
Jason Rumney committed
305 306 307 308 309 310
  dc = get_frame_dc (f);

  EnumFontFamiliesEx (dc, &font_match_pattern,
                      (FONTENUMPROC) add_font_name_to_list,
                      (LPARAM) &list, 0);
  release_frame_dc (f, dc);
311
  Vinhibit_quit = prev_quit;
Jason Rumney's avatar
Jason Rumney committed
312 313 314 315 316 317 318

  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.  */
319
static Lisp_Object
Dmitry Antipov's avatar
Dmitry Antipov committed
320
w32font_open (struct frame *f, Lisp_Object font_entity, int pixel_size)
Jason Rumney's avatar
Jason Rumney committed
321
{
322 323 324 325 326
  Lisp_Object font_object
    = font_make_object (VECSIZE (struct w32font_info),
                        font_entity, pixel_size);
  struct w32font_info *w32_font
    = (struct w32font_info *) XFONT_OBJECT (font_object);
Jason Rumney's avatar
Jason Rumney committed
327

328
  ASET (font_object, FONT_TYPE_INDEX, Qgdi);
Jason Rumney's avatar
Jason Rumney committed
329

330
  if (!w32font_open_internal (f, font_entity, pixel_size, font_object))
Jason Rumney's avatar
Jason Rumney committed
331
    {
332
      return Qnil;
Jason Rumney's avatar
Jason Rumney committed
333 334
    }

335 336 337
  /* GDI backend does not use glyph indices.  */
  w32_font->glyph_idx = 0;

338
  return font_object;
Jason Rumney's avatar
Jason Rumney committed
339 340
}

341
/* w32 implementation of close for font_backend.  */
Jason Rumney's avatar
Jason Rumney committed
342
void
343
w32font_close (struct font *font)
Jason Rumney's avatar
Jason Rumney committed
344
{
345
  struct w32font_info *w32_font = (struct w32font_info *) font;
346

347
  if (w32_font->hfont)
348
    {
349 350 351 352 353 354 355
      /* Delete the GDI font object.  */
      DeleteObject (w32_font->hfont);
      w32_font->hfont = NULL;

      /* Free all the cached metrics.  */
      if (w32_font->cached_metrics)
	{
356 357
	  int i;

358 359 360 361 362
	  for (i = 0; i < w32_font->n_cache_blocks; i++)
	      xfree (w32_font->cached_metrics[i]);
	  xfree (w32_font->cached_metrics);
	  w32_font->cached_metrics = NULL;
	}
363
    }
Jason Rumney's avatar
Jason Rumney committed
364 365 366 367 368 369 370
}

/* 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
371
int
372
w32font_has_char (Lisp_Object entity, int c)
Jason Rumney's avatar
Jason Rumney committed
373
{
374 375 376 377 378 379
  /* We can't be certain about which characters a font will support until
     we open it.  Checking the scripts that the font supports turns out
     to not be reliable.  */
  return -1;

#if 0
380
  Lisp_Object supported_scripts, extra, script;
Jason Rumney's avatar
Jason Rumney committed
381 382
  DWORD mask;

383 384 385 386
  extra = AREF (entity, FONT_EXTRA_INDEX);
  if (!CONSP (extra))
    return -1;

387
  supported_scripts = assq_no_quit (QCscript, extra);
388 389
  /* If font doesn't claim to support any scripts, then we can't be certain
     until we open it.  */
390
  if (!CONSP (supported_scripts))
Jason Rumney's avatar
Jason Rumney committed
391 392
    return -1;

393
  supported_scripts = XCDR (supported_scripts);
Jason Rumney's avatar
Jason Rumney committed
394

395
  script = CHAR_TABLE_REF (Vchar_script_table, c);
Jason Rumney's avatar
Jason Rumney committed
396

397 398 399 400 401 402 403 404
  /* If we don't know what script the character is from, then we can't be
     certain until we open it.  Also if the font claims support for the script
     the character is from, it may only have partial coverage, so we still
     can't be certain until we open the font.  */
  if (NILP (script) || memq_no_quit (script, supported_scripts))
    return -1;

  /* Font reports what scripts it supports, and none of them are the script
405 406 407 408 409
     the character is from. But we still can't be certain, as some fonts
     will contain some/most/all of the characters in that script without
     claiming support for it.  */
  return -1;
#endif
Jason Rumney's avatar
Jason Rumney committed
410 411 412
}

/* w32 implementation of encode_char for font backend.
413
   Return a glyph code of FONT for character C (Unicode code point).
414 415
   If FONT doesn't have such a glyph, return FONT_INVALID_CODE.

Juanma Barranquero's avatar
Juanma Barranquero committed
416
   For speed, the gdi backend uses Unicode (Emacs calls encode_char
417 418 419
   far too often for it to be efficient). But we still need to detect
   which characters are not supported by the font.
  */
420
static unsigned
421
w32font_encode_char (struct font *font, int c)
Jason Rumney's avatar
Jason Rumney committed
422
{
423
  struct w32font_info * w32_font = (struct w32font_info *)font;
424

425 426 427
  if (c < w32_font->metrics.tmFirstChar
      || c > w32_font->metrics.tmLastChar)
    return FONT_INVALID_CODE;
428
  else
429
    return c;
Jason Rumney's avatar
Jason Rumney committed
430 431 432 433 434
}

/* 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
435
   CODE (length NGLYPHS).  Apparently metrics can be NULL, in this
Jason Rumney's avatar
Jason Rumney committed
436
   case just return the overall width.  */
437
void
438
w32font_text_extents (struct font *font, const unsigned *code,
439
		      int nglyphs, struct font_metrics *metrics)
Jason Rumney's avatar
Jason Rumney committed
440 441
{
  int i;
442 443
  HFONT old_font = NULL;
  HDC dc = NULL;
Eli Zaretskii's avatar
Eli Zaretskii committed
444
  struct frame * f UNINIT;
Jason Rumney's avatar
Jason Rumney committed
445
  int total_width = 0;
446
  WORD *wcode;
447
  SIZE size;
448
  bool first;
449
  Lisp_Object prev_quit = Vinhibit_quit;
Jason Rumney's avatar
Jason Rumney committed
450

451 452
  struct w32font_info *w32_font = (struct w32font_info *) font;

453
  memset (metrics, 0, sizeof (struct font_metrics));
Jason Rumney's avatar
Jason Rumney committed
454

455
  for (i = 0, first = true; i < nglyphs; i++)
456 457 458 459
    {
      struct w32_metric_cache *char_metric;
      int block = *(code + i) / CACHE_BLOCKSIZE;
      int pos_in_block = *(code + i) % CACHE_BLOCKSIZE;
Jason Rumney's avatar
Jason Rumney committed
460

461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
      if (block >= w32_font->n_cache_blocks)
	{
	  if (!w32_font->cached_metrics)
	    w32_font->cached_metrics
	      = xmalloc ((block + 1)
			 * sizeof (struct w32_metric_cache *));
	  else
	    w32_font->cached_metrics
	      = xrealloc (w32_font->cached_metrics,
			  (block + 1)
			  * sizeof (struct w32_metric_cache *));
	  memset (w32_font->cached_metrics + w32_font->n_cache_blocks, 0,
		  ((block + 1 - w32_font->n_cache_blocks)
		   * sizeof (struct w32_metric_cache *)));
	  w32_font->n_cache_blocks = block + 1;
	}
Jason Rumney's avatar
Jason Rumney committed
477

478 479 480 481 482
      if (!w32_font->cached_metrics[block])
	{
	  w32_font->cached_metrics[block]
	    = xzalloc (CACHE_BLOCKSIZE * sizeof (struct w32_metric_cache));
	}
Jason Rumney's avatar
Jason Rumney committed
483

484
      char_metric = w32_font->cached_metrics[block] + pos_in_block;
Jason Rumney's avatar
Jason Rumney committed
485

486 487 488
      if (char_metric->status == W32METRIC_NO_ATTEMPT)
	{
	  if (dc == NULL)
Jason Rumney's avatar
Jason Rumney committed
489
	    {
490 491 492 493 494 495
	      /* 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.  */
	      f = XFRAME (selected_frame);

496 497 498 499 500 501
	      /* Prevent quitting while EnumFontFamiliesEx runs and
		 conses the list it will return.  That's because
		 get_frame_dc acquires the critical section, so we
		 cannot quit before we release it in release_frame_dc.  */
	      prev_quit = Vinhibit_quit;
	      Vinhibit_quit = Qt;
502 503
	      dc = get_frame_dc (f);
	      old_font = SelectObject (dc, w32_font->hfont);
Jason Rumney's avatar
Jason Rumney committed
504
	    }
505 506
	  compute_metrics (dc, w32_font, *(code + i), char_metric);
	}
507

508 509
      if (char_metric->status == W32METRIC_SUCCESS)
	{
510 511 512 513 514 515 516 517 518 519 520 521 522
	  if (first)
	    {
	      metrics->lbearing = char_metric->lbearing;
	      metrics->rbearing = char_metric->rbearing;
	      metrics->width    = 0;
	      metrics->ascent   = char_metric->ascent;
	      metrics->descent  = char_metric->descent;
	      first = false;
	    }
	  if (metrics->lbearing > char_metric->lbearing)
	    metrics->lbearing = char_metric->lbearing;
	  if (metrics->rbearing < char_metric->rbearing)
	    metrics->rbearing = char_metric->rbearing;
523
	  metrics->width += char_metric->width;
524 525 526 527
	  if (metrics->ascent < char_metric->ascent)
	    metrics->ascent = char_metric->ascent;
	  if (metrics->descent < char_metric->descent)
	    metrics->descent = char_metric->descent;
Jason Rumney's avatar
Jason Rumney committed
528
	}
529 530 531 532 533 534 535 536 537 538 539 540 541
      else
	/* If we couldn't get metrics for a char,
	   use alternative method.  */
	break;
    }
  /* If we got through everything, return.  */
  if (i == nglyphs)
    {
      if (dc != NULL)
	{
	  /* Restore state and release DC.  */
	  SelectObject (dc, old_font);
	  release_frame_dc (f, dc);
542
	  Vinhibit_quit = prev_quit;
543 544
	}
      return;
Jason Rumney's avatar
Jason Rumney committed
545
    }
546

547 548 549
  /* For non-truetype fonts, GetGlyphOutlineW is not supported, so
     fallback on other methods that will at least give some of the metric
     information.  */
550

551
  /* Make array big enough to hold surrogates.  */
552
  eassume (0 <= nglyphs);	/* pacify GCC warning on next line */
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
  wcode = alloca (nglyphs * sizeof (WORD) * 2);
  for (i = 0; i < nglyphs; i++)
    {
      if (code[i] < 0x10000)
        wcode[i] = code[i];
      else
        {
          DWORD surrogate = code[i] - 0x10000;

          /* High surrogate: U+D800 - U+DBFF.  */
          wcode[i++] = 0xD800 + ((surrogate >> 10) & 0x03FF);
          /* Low surrogate: U+DC00 - U+DFFF.  */
          wcode[i] = 0xDC00 + (surrogate & 0x03FF);
          /* An extra glyph. wcode is already double the size of code to
             cope with this.  */
          nglyphs++;
        }
    }

572 573
  if (dc == NULL)
    {
Jason Rumney's avatar
Jason Rumney committed
574 575 576 577 578 579
      /* 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.  */
      f = XFRAME (selected_frame);

580 581 582 583 584 585
      /* Prevent quitting while EnumFontFamiliesEx runs and conses the
	 list it will return.  That's because get_frame_dc acquires
	 the critical section, so we cannot quit before we release it
	 in release_frame_dc.  */
      prev_quit = Vinhibit_quit;
      Vinhibit_quit = Qt;
586
      dc = get_frame_dc (f);
587
      old_font = SelectObject (dc, w32_font->hfont);
588 589
    }

590
  if (GetTextExtentPoint32W (dc, wcode, nglyphs, &size))
Jason Rumney's avatar
Jason Rumney committed
591
    {
592
      total_width = size.cx;
Jason Rumney's avatar
Jason Rumney committed
593 594
    }

Juanma Barranquero's avatar
Juanma Barranquero committed
595
  /* On 95/98/ME, only some Unicode functions are available, so fallback
596
     on doing a dummy draw to find the total width.  */
597
  if (!total_width)
Jason Rumney's avatar
Jason Rumney committed
598 599
    {
      RECT rect;
600
      rect.top = 0; rect.bottom = font->height; rect.left = 0; rect.right = 1;
Jason Rumney's avatar
Jason Rumney committed
601 602 603 604
      DrawTextW (dc, wcode, nglyphs, &rect,
                 DT_CALCRECT | DT_NOPREFIX | DT_SINGLELINE);
      total_width = rect.right;
    }
605

606
  /* Give our best estimate of the metrics, based on what we know.  */
607 608 609
  metrics->width = total_width - w32_font->metrics.tmOverhang;
  metrics->lbearing = 0;
  metrics->rbearing = total_width;
610 611
  metrics->ascent = font->ascent;
  metrics->descent = font->descent;
612

Jason Rumney's avatar
Jason Rumney committed
613 614
  /* Restore state and release DC.  */
  SelectObject (dc, old_font);
Jason Rumney's avatar
Jason Rumney committed
615
  release_frame_dc (f, dc);
616
  Vinhibit_quit = prev_quit;
Jason Rumney's avatar
Jason Rumney committed
617 618 619 620 621
}

/* w32 implementation of draw for font backend.
   Optional.
   Draw glyphs between FROM and TO of S->char2b at (X Y) pixel
622 623 624
   position of frame F with S->FACE and S->GC.  If WITH_BACKGROUND,
   fill the background in advance.  It is assured that WITH_BACKGROUND
   is false when (FROM > 0 || TO < S->nchars).
625 626 627 628 629 630 631

   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
632
int
633
w32font_draw (struct glyph_string *s, int from, int to,
634
	      int x, int y, bool with_background)
Jason Rumney's avatar
Jason Rumney committed
635
{
636
  UINT options;
637
  HRGN orig_clip = NULL;
638
  int len = to - from;
639
  struct w32font_info *w32font = (struct w32font_info *) s->font;
640 641

  options = w32font->glyph_idx;
642 643 644 645 646

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

647 648
      /* Save clip region for later restoration.  */
      orig_clip = CreateRectRgn (0, 0, 0, 0);
649
      if (!GetClipRgn (s->hdc, orig_clip))
650 651 652 653 654
	{
	  DeleteObject (orig_clip);
	  orig_clip = NULL;
	}

655 656 657 658 659 660 661 662 663 664 665
      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
666

667 668 669
  /* 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
670 671
  if (with_background)
    {
672 673
      HBRUSH brush;
      RECT rect;
674
      struct font *font = s->font;
675 676 677 678 679 680 681 682
      int ascent = font->ascent, descent = font->descent;

      /* Font's global ascent and descent values might be
	 preposterously large for some fonts.  We fix here the case
	 when those fonts are used for display of glyphless
	 characters, because drawing background with font dimensions
	 in those cases makes the display illegible.  There's only one
	 more call to the draw method with with_background set to
683
	 true, and that's in w32_draw_glyph_string_foreground, when
684 685 686 687 688 689 690 691 692 693 694
	 drawing the cursor, where we have no such heuristics
	 available.  FIXME.  */
      if (s->first_glyph->type == GLYPHLESS_GLYPH
	  && (s->first_glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE
	      || s->first_glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM))
	{
	  ascent =
	    s->first_glyph->slice.glyphless.lower_yoff
	    - s->first_glyph->slice.glyphless.upper_yoff;
	  descent = 0;
	}
695 696
      brush = CreateSolidBrush (s->gc->background);
      rect.left = x;
697
      rect.top = y - ascent;
698
      rect.right = x + s->width;
699
      rect.bottom = y + descent;
700
      FillRect (s->hdc, &rect, brush);
701
      DeleteObject (brush);
Jason Rumney's avatar
Jason Rumney committed
702
    }
703

704 705
  if (s->padding_p)
    {
706
      int i;
707 708

      for (i = 0; i < len; i++)
709 710 711 712
	{
	  WCHAR c = s->char2b[from + i] & 0xFFFF;
	  ExtTextOutW (s->hdc, x + i, y, options, NULL, &c, 1, NULL);
	}
713 714
    }
  else
715 716 717 718 719 720 721 722 723 724 725
    {
      /* The number of glyphs in a glyph_string cannot be larger than
	 the maximum value of the 'used' member of a glyph_row, so we
	 are OK using alloca here.  */
      eassert (len <= SHRT_MAX);
      WCHAR *chars = alloca (len * sizeof (WCHAR));
      int j;
      for (j = 0; j < len; j++)
	chars[j] = s->char2b[from + j] & 0xFFFF;
      ExtTextOutW (s->hdc, x, y, options, NULL, chars, len, NULL);
    }
726 727 728

  /* Restore clip region.  */
  if (s->num_clips > 0)
729 730 731 732
    SelectClipRgn (s->hdc, orig_clip);

  if (orig_clip)
    DeleteObject (orig_clip);
733 734

  return len;
Jason Rumney's avatar
Jason Rumney committed
735 736 737
}

/* w32 implementation of free_entity for font backend.
Paul Eggert's avatar
Paul Eggert committed
738
   Optional.
Jason Rumney's avatar
Jason Rumney committed
739
   Free FONT_EXTRA_INDEX field of FONT_ENTITY.
740 741
static void
w32font_free_entity (Lisp_Object entity);
Jason Rumney's avatar
Jason Rumney committed
742 743 744 745 746 747 748
  */

/* 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.
749
static int
Dmitry Antipov's avatar
Dmitry Antipov committed
750
w32font_prepare_face (struct frame *f, struct face *face);
Jason Rumney's avatar
Jason Rumney committed
751 752 753 754
  */
/* w32 implementation of done_face for font backend.
   Optional.
   Done FACE for displaying characters by FACE->font on frame F.
755
static void
Dmitry Antipov's avatar
Dmitry Antipov committed
756
w32font_done_face (struct frame *f, struct face *face);  */
Jason Rumney's avatar
Jason Rumney committed
757 758 759 760

/* w32 implementation of get_bitmap for font backend.
   Optional.
   Store bitmap data for glyph-code CODE of FONT in BITMAP.  It is
761
   intended that this method is called from the other font-driver
Jason Rumney's avatar
Jason Rumney committed
762
   for actual drawing.
763 764 765
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
766 767 768 769
  */
/* w32 implementation of free_bitmap for font backend.
   Optional.
   Free bitmap data in BITMAP.
770 771
static void
w32font_free_bitmap (struct font *font, struct font_bitmap *bitmap);
Jason Rumney's avatar
Jason Rumney committed
772 773 774 775 776
  */
/* 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
Paul Eggert's avatar
Paul Eggert committed
777
   the operations was successful.  Otherwise return -1.
778 779
static int
w32font_anchor_point (struct font *font, unsigned code,
Jason Rumney's avatar
Jason Rumney committed
780 781 782 783 784 785
                                 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.
786 787
static Lisp_Object
w32font_otf_capability (struct font *font);
Jason Rumney's avatar
Jason Rumney committed
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
  */
/* 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.
805 806 807 808
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,
809
                   bool alternate_subst);
Jason Rumney's avatar
Jason Rumney committed
810 811
  */

Jason Rumney's avatar
Jason Rumney committed
812 813 814 815
/* 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
816 817
w32font_list_internal (struct frame *f, Lisp_Object font_spec,
		       bool opentype_only)
Jason Rumney's avatar
Jason Rumney committed
818 819 820 821 822 823
{
  struct font_callback_data match_data;
  HDC dc;

  match_data.orig_font_spec = font_spec;
  match_data.list = Qnil;
824
  XSETFRAME (match_data.frame, f);
Jason Rumney's avatar
Jason Rumney committed
825

826
  memset (&match_data.pattern, 0, sizeof (LOGFONT));
Jason Rumney's avatar
Jason Rumney committed
827 828
  fill_in_logfont (f, &match_data.pattern, font_spec);

829 830 831 832 833 834 835 836 837
  /* If the charset is unrecognized, then we won't find a font, so don't
     waste time looking for one.  */
  if (match_data.pattern.lfCharSet == DEFAULT_CHARSET)
    {
      Lisp_Object spec_charset = AREF (font_spec, FONT_REGISTRY_INDEX);
      if (!NILP (spec_charset)
	  && !EQ (spec_charset, Qiso10646_1)
	  && !EQ (spec_charset, Qunicode_bmp)
	  && !EQ (spec_charset, Qunicode_sip)
838 839
	  && !EQ (spec_charset, Qunknown)
	  && !EQ (spec_charset, Qascii_0))
840 841 842
	return Qnil;
    }

Jason Rumney's avatar
Jason Rumney committed
843 844 845
  match_data.opentype_only = opentype_only;
  if (opentype_only)
    match_data.pattern.lfOutPrecision = OUT_OUTLINE_PRECIS;
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
  match_data.known_fonts = Qnil;
  Lisp_Object vw32_non_USB_fonts = Fsymbol_value (Qw32_non_USB_fonts), val;
  if (CONSP (vw32_non_USB_fonts))
    {
      Lisp_Object extra;
      for (extra = AREF (font_spec, FONT_EXTRA_INDEX);
	   CONSP (extra); extra = XCDR (extra))
	{
	  Lisp_Object tem = XCAR (extra);
	  if (CONSP (tem)
	      && EQ (XCAR (tem), QCscript)
	      && SYMBOLP (XCDR (tem))
	      && !NILP (val = assq_no_quit (XCDR (tem), vw32_non_USB_fonts)))
	    {
	      match_data.known_fonts = XCDR (val);
	      break;
	    }
	}
    }
Jason Rumney's avatar
Jason Rumney committed
865 866 867 868 869 870 871 872 873

  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
    {
874 875 876 877 878 879 880
      Lisp_Object prev_quit = Vinhibit_quit;

      /* Prevent quitting while EnumFontFamiliesEx runs and conses the
	 list it will return.  That's because get_frame_dc acquires
	 the critical section, so we cannot quit before we release it
	 in release_frame_dc.  */
      Vinhibit_quit = Qt;
Jason Rumney's avatar
Jason Rumney committed
881 882 883 884 885 886
      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);
887
      Vinhibit_quit = prev_quit;
Jason Rumney's avatar
Jason Rumney committed
888 889
    }

890
  return match_data.list;
Jason Rumney's avatar
Jason Rumney committed
891 892 893 894 895 896
}

/* 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
897 898
w32font_match_internal (struct frame *f, Lisp_Object font_spec,
			bool opentype_only)
Jason Rumney's avatar
Jason Rumney committed
899 900 901 902 903
{
  struct font_callback_data match_data;
  HDC dc;

  match_data.orig_font_spec = font_spec;
904
  XSETFRAME (match_data.frame, f);
Jason Rumney's avatar
Jason Rumney committed
905 906
  match_data.list = Qnil;

907
  memset (&match_data.pattern, 0, sizeof (LOGFONT));
Jason Rumney's avatar
Jason Rumney committed
908 909 910 911 912 913
  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;

914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
  match_data.known_fonts = Qnil;
  Lisp_Object vw32_non_USB_fonts = Fsymbol_value (Qw32_non_USB_fonts), val;
  if (CONSP (vw32_non_USB_fonts))
    {
      Lisp_Object extra;
      for (extra = AREF (font_spec, FONT_EXTRA_INDEX);
	   CONSP (extra); extra = XCDR (extra))
	{
	  Lisp_Object tem = XCAR (extra);
	  if (CONSP (tem)
	      && EQ (XCAR (tem), QCscript)
	      && SYMBOLP (XCDR (tem))
	      && !NILP (val = assq_no_quit (XCDR (tem), vw32_non_USB_fonts)))
	    {
	      match_data.known_fonts = XCDR (val);
	      break;
	    }
	}
    }

934 935 936 937 938 939
  /* Prevent quitting while EnumFontFamiliesEx runs and conses the
     list it will return.  That's because get_frame_dc acquires the
     critical section, so we cannot quit before we release it in
     release_frame_dc.  */
  Lisp_Object prev_quit = Vinhibit_quit;
  Vinhibit_quit = Qt;
Jason Rumney's avatar
Jason Rumney committed
940 941 942 943 944 945
  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);
946
  Vinhibit_quit = prev_quit;
Jason Rumney's avatar
Jason Rumney committed
947 948 949 950

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

951
int
Dmitry Antipov's avatar
Dmitry Antipov committed
952
w32font_open_internal (struct frame *f, Lisp_Object font_entity,
953
		       int pixel_size, Lisp_Object font_object)
954
{
955
  int len, size;
956 957 958
  LOGFONT logfont;
  HDC dc;
  HFONT hfont, old_font;
959
  Lisp_Object val;
960 961
  struct w32font_info *w32_font;
  struct font * font;
962
  OUTLINETEXTMETRICW* metrics = NULL;
963 964 965

  w32_font = (struct w32font_info *) XFONT_OBJECT (font_object);
  font = (struct font *) w32_font;
966 967 968 969

  if (!font)
    return 0;

970
  memset (&logfont, 0, sizeof (logfont));
971 972
  fill_in_logfont (f, &logfont, font_entity);

973 974 975 976 977 978
  /* Prefer truetype fonts, to avoid known problems with type1 fonts, and
     limitations in bitmap fonts.  */
  val = AREF (font_entity, FONT_FOUNDRY_INDEX);
  if (!EQ (val, Qraster))
    logfont.lfOutPrecision = OUT_TT_PRECIS;

Tom Tromey's avatar
Tom Tromey committed
979
  size = XFIXNUM (AREF (font_entity, FONT_SIZE_INDEX));
980 981 982 983 984 985 986 987 988 989 990 991 992
  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);

993
  /* Try getting the outline metrics (only works for truetype fonts).  */
994
  len = get_outline_metrics_w (dc, 0, NULL);
995 996
  if (len)
    {
997
      metrics = (OUTLINETEXTMETRICW *) alloca (len);
998
      if (get_outline_metrics_w (dc, len, metrics))
999 1000
        memcpy (&w32_font->metrics, &metrics->otmTextMetrics,
		sizeof (TEXTMETRICW));
1001 1002 1003
      else
        metrics = NULL;
    }
1004

1005
  if (!metrics)
1006
    get_text_metrics_w (dc, &w32_font->metrics);
1007

Jason Rumney's avatar
Jason Rumney committed
1008 1009
  w32_font->cached_metrics = NULL;
  w32_font->n_cache_blocks = 0;
1010

1011 1012
  SelectObject (dc, old_font);
  release_frame_dc (f, dc);
1013

1014
  w32_font->hfont = hfont;
1015

1016 1017 1018 1019 1020 1021
  {
    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;
1022
    name = alloca (len);
1023 1024
    while (name && w32font_full_name (&logfont, font_entity, pixel_size,
                                      name, len) < 0)
1025
      {
1026 1027
        len += 32;
        name = alloca (len);
1028 1029
      }
    if (name)
1030
      font->props[FONT_FULLNAME_INDEX]
1031
        = DECODE_SYSTEM (build_string (name));
1032
    else
1033 1034
      font->props[FONT_FULLNAME_INDEX]
	= DECODE_SYSTEM (build_string (logfont.lfFaceName));
1035
  }
1036 1037

  font->max_width = w32_font->metrics.tmMaxCharWidth;
1038
  /* Parts of Emacs display assume that height = ascent + descent...
1039
     so height is defined later, after ascent and descent.
1040
  font->height = w32_font->metrics.tmHeight
1041
    + w32_font->metrics.tmExternalLeading;
1042 1043
  */

1044 1045 1046 1047 1048 1049
  font->space_width = font->average_width = w32_font->metrics.tmAveCharWidth;

  font->vertical_centering = 0;
  font->baseline_offset = 0;
  font->relative_compose = 0;
  font->default_ascent = w32_font->metrics.tmAscent;
1050 1051 1052 1053
  font->pixel_size = size;
  font->driver = &w32font_driver;
  font->encoding_charset = -1;
  font->repertory_charset = -1;
1054
  /* TODO: do we really want the minimum width here, which could be negative? */
1055
  font->min_width = font->space_width;
1056 1057
  font->ascent = w32_font->metrics.tmAscent;
  font->descent = w32_font->metrics.tmDescent;
1058
  font->height = font->ascent + font->descent;
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069

  if (metrics)
    {
      font->underline_thickness = metrics->otmsUnderscoreSize;
      font->underline_position = -metrics->otmsUnderscorePosition;
    }
  else
    {
      font->underline_thickness = 0;
      font->underline_position = -1;
    }
1070

1071 1072 1073 1074
  /* For temporary compatibility with legacy code that expects the
     name to be usable in x-list-fonts. Eventually we expect to change
     x-list-fonts and other places that use fonts so that this can be
     an fcname or similar.  */
Kenichi Handa's avatar
Kenichi Handa committed
1075
  font->props[FONT_NAME_INDEX] = Ffont_xlfd_name (font_object, Qnil);
1076

1077 1078 1079
  return 1;
}

Jason Rumney's avatar
Jason Rumney committed
1080 1081
/* Callback function for EnumFontFamiliesEx.
 * Adds the name of a font to a Lisp list (passed in as the lParam arg).  */
1082
static int CALLBACK ALIGN_STACK
1083 1084 1085
add_font_name_to_list (ENUMLOGFONTEX *logical_font,
		       NEWTEXTMETRICEX *physical_font,
		       DWORD font_type, LPARAM list_object)
Jason Rumney's avatar
Jason Rumney committed
1086 1087
{
  Lisp_Object* list = (Lisp_Object *) list_object;
1088 1089 1090 1091 1092 1093
  Lisp_Object family;

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

1094
  family = intern_font_name (logical_font->elfLogFont.lfFaceName);
Jason Rumney's avatar
Jason Rumney committed
1095 1096 1097 1098 1099 1100
  if (! memq_no_quit (family, *list))
    *list = Fcons (family, *list);

  return 1;
}

1101 1102
static int w32_decode_weight (int);
static int w32_encode_weight (int);
1103

Jason Rumney's avatar
Jason Rumney committed
1104
/* Convert an enumerated Windows font to an Emacs font entity.  */
1105
static Lisp_Object
1106 1107 1108 1109 1110 1111
w32_enumfont_pattern_entity (Lisp_Object frame,
			     ENUMLOGFONTEX *logical_font,
			     NEWTEXTMETRICEX *physical_font,
			     DWORD font_type,
			     LOGFONT *requested_font,
			     Lisp_Object backend)
Jason Rumney's avatar
Jason Rumney committed
1112 1113 1114 1115
{
  Lisp_Object entity, tem;
  LOGFONT *lf = (LOGFONT*) logical_font;
  BYTE generic_type;
1116
  DWORD full_type = physical_font->ntmTm.ntmFlags;
Jason Rumney's avatar
Jason Rumney committed
1117

1118
  entity = font_make_entity ();
Jason Rumney's avatar
Jason Rumney committed
1119

1120
  ASET (entity, FONT_TYPE_INDEX, backend);
1121
  ASET (entity, FONT_REGISTRY_INDEX, w32_registry (lf->lfCharSet, font_type));
Jason Rumney's avatar
Jason Rumney committed
1122 1123 1124
  ASET (entity, FONT_OBJLIST_INDEX, Qnil);

  /* Foundry is difficult to get in readable form on Windows.
1125
     But Emacs crashes if it is not set, so set it to something more
1126
     generic.  These values make xlfds compatible with Emacs 22. */
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
  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
1138 1139 1140 1141
  generic_type = physical_font->ntmTm.tmPitchAndFamily & 0xF0;
  if (generic_type == FF_DECORATIVE)
    tem = Qdecorative;
  else if (generic_type == FF_MODERN)
1142
    tem = Qmono;
Jason Rumney's avatar
Jason Rumney committed
1143
  else if (generic_type == FF_ROMAN)
1144
    tem = Qserif;
Jason Rumney's avatar
Jason Rumney committed
1145 1146 1147
  else if (generic_type == FF_SCRIPT)
    tem = Qscript;
  else if (generic_type == FF_SWISS)
1148
    tem = Qsans;
Jason Rumney's avatar
Jason Rumney committed
1149
  else
1150
    tem = Qnil;
1151 1152

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

1154
  if (physical_font->ntmTm.tmPitchAndFamily & 0x01)
1155
    ASET (entity, FONT_SPACING_INDEX, make_fixnum (FONT_SPACING_PROPORTIONAL));
1156
  else
1157
    ASET (entity, FONT_SPACING_INDEX, make_fixnum (FONT_SPACING_CHARCELL));
Jason Rumney's avatar
Jason Rumney committed
1158

1159 1160 1161 1162 1163
  if (requested_font->lfQuality != DEFAULT_QUALITY)
    {
      font_put_extra (entity, QCantialias,
                      lispy_antialias_type (requested_font->lfQuality));
    }
Jason Rumney's avatar
Jason Rumney committed
1164
  ASET (entity, FONT_FAMILY_INDEX,
1165
	intern_font_name (lf->lfFaceName));
Jason Rumney's avatar
Jason Rumney committed
1166

1167
  FONT_SET_STYLE (entity, FONT_WEIGHT_INDEX,
1168
		  make_fixnum (w32_decode_weight (lf->lfWeight)));
1169
  FONT_SET_STYLE (entity, FONT_SLANT_INDEX,
1170
		  make_fixnum (lf->lfItalic ? 200 : 100));
1171 1172
  /* TODO: PANOSE struct has this info, but need to call GetOutlineTextMetrics
     to get it.  */
1173
  FONT_SET_STYLE (entity, FONT_WIDTH_INDEX, make_fixnum (100));
Jason Rumney's avatar
Jason Rumney committed
1174

1175
  if (font_type & RASTER_FONTTYPE)
1176
    ASET (entity, FONT_SIZE_INDEX,
1177
          make_fixnum (physical_font->ntmTm.tmHeight
1178
                       + physical_font->ntmTm.tmExternalLeading));
Jason Rumney's avatar