w32font.c 88.7 KB
Newer Older
1
/* Font backend for the Microsoft Windows API.
Paul Eggert's avatar
Paul Eggert committed
2
   Copyright (C) 2007-2018 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

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

47 48 49 50 51 52 53 54 55
/* 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
56
static void fill_in_logfont (struct frame *, LOGFONT *, Lisp_Object);
Jason Rumney's avatar
Jason Rumney committed
57

58 59
static BYTE w32_antialias_type (Lisp_Object);
static Lisp_Object lispy_antialias_type (BYTE);
60

61 62 63 64
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
65

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

/* EnumFontFamiliesEx callbacks.  */
69 70 71 72 73 74 75 76 77
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
78

79 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;
  /* The list to add matches to.  */
  Lisp_Object list;
Jason Rumney's avatar
Jason Rumney committed
93
  /* Whether to match only opentype fonts.  */
94
  bool opentype_only;
95 96 97 98
};

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

101 102
#ifdef WINDOWSNT

103 104 105
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;
106
static BOOL g_b_init_get_char_width_32_w;
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

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);
123 124 125 126 127
typedef BOOL (WINAPI * GetCharWidth32W_Proc) (
   HDC hdc,
   UINT uFirstChar,
   UINT uLastChar,
   LPINT lpBuffer);
128 129 130 131 132 133 134 135 136 137 138 139

/* 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)
{
140
  return maybe_load_unicows_dll ();
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
}

/* 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)
157
	  get_proc_addr (hm_unicows, "GetOutlineTextMetricsW");
158
    }
159
  eassert (s_pfn_Get_Outline_Text_MetricsW != NULL);
160 161 162 163 164 165 166 167 168 169 170 171 172 173
  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)
174
	  get_proc_addr (hm_unicows, "GetTextMetricsW");
175
    }
176
  eassert (s_pfn_Get_Text_MetricsW != NULL);
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
  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)
192
	  get_proc_addr (hm_unicows, "GetGlyphOutlineW");
193
    }
194
  eassert (s_pfn_Get_Glyph_OutlineW != NULL);
195 196 197
  return s_pfn_Get_Glyph_OutlineW (hdc, uChar, uFormat, lpgm, cbBuffer,
				   lpvBuffer, lpmat2);
}
Jason Rumney's avatar
Jason Rumney committed
198

199 200
static DWORD WINAPI
get_char_width_32_w (HDC hdc, UINT uFirstChar, UINT uLastChar, LPINT lpBuffer)
201 202 203 204 205 206 207 208 209
{
  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)
210
	  get_proc_addr (hm_unicows, "GetCharWidth32W");
211 212 213 214 215
    }
  eassert (s_pfn_Get_Char_Width_32W != NULL);
  return s_pfn_Get_Char_Width_32W (hdc, uFirstChar, uLastChar, lpBuffer);
}

216 217 218 219 220 221 222 223 224 225 226 227
#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
228
static int
229
memq_no_quit (Lisp_Object elt, Lisp_Object list)
Jason Rumney's avatar
Jason Rumney committed
230 231 232 233 234 235
{
  while (CONSP (list) && ! EQ (XCAR (list), elt))
    list = XCDR (list);
  return (CONSP (list));
}

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

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

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

/* 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
272
   FRAME.  The closeness is determined by the font backend, thus
Jason Rumney's avatar
Jason Rumney committed
273
   `face-font-selection-order' is ignored here.  */
274
static Lisp_Object
275
w32font_match (struct frame *f, Lisp_Object font_spec)
Jason Rumney's avatar
Jason Rumney committed
276
{
277
  Lisp_Object entity = w32font_match_internal (f, font_spec, 0);
278
  FONT_ADD_LOG ("w32font-match", font_spec, entity);
279
  return entity;
Jason Rumney's avatar
Jason Rumney committed
280 281 282 283 284
}

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

293
  memset (&font_match_pattern, 0, sizeof (font_match_pattern));
294
  font_match_pattern.lfCharSet = DEFAULT_CHARSET;
Jason Rumney's avatar
Jason Rumney committed
295

296 297 298 299 300
  /* 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
301 302 303 304 305 306
  dc = get_frame_dc (f);

  EnumFontFamiliesEx (dc, &font_match_pattern,
                      (FONTENUMPROC) add_font_name_to_list,
                      (LPARAM) &list, 0);
  release_frame_dc (f, dc);
307
  Vinhibit_quit = prev_quit;
Jason Rumney's avatar
Jason Rumney committed
308 309 310 311 312 313 314

  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.  */
315
static Lisp_Object
Dmitry Antipov's avatar
Dmitry Antipov committed
316
w32font_open (struct frame *f, Lisp_Object font_entity, int pixel_size)
Jason Rumney's avatar
Jason Rumney committed
317
{
318 319 320 321 322
  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
323

324
  ASET (font_object, FONT_TYPE_INDEX, Qgdi);
Jason Rumney's avatar
Jason Rumney committed
325

326
  if (!w32font_open_internal (f, font_entity, pixel_size, font_object))
Jason Rumney's avatar
Jason Rumney committed
327
    {
328
      return Qnil;
Jason Rumney's avatar
Jason Rumney committed
329 330
    }

331 332 333
  /* GDI backend does not use glyph indices.  */
  w32_font->glyph_idx = 0;

334
  return font_object;
Jason Rumney's avatar
Jason Rumney committed
335 336
}

337
/* w32 implementation of close for font_backend.  */
Jason Rumney's avatar
Jason Rumney committed
338
void
339
w32font_close (struct font *font)
Jason Rumney's avatar
Jason Rumney committed
340
{
341
  struct w32font_info *w32_font = (struct w32font_info *) font;
342

343
  if (w32_font->hfont)
344
    {
345 346 347 348 349 350 351
      /* Delete the GDI font object.  */
      DeleteObject (w32_font->hfont);
      w32_font->hfont = NULL;

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

354 355 356 357 358
	  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;
	}
359
    }
Jason Rumney's avatar
Jason Rumney committed
360 361 362 363 364 365 366
}

/* 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
367
int
368
w32font_has_char (Lisp_Object entity, int c)
Jason Rumney's avatar
Jason Rumney committed
369
{
370 371 372 373 374 375
  /* 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
376
  Lisp_Object supported_scripts, extra, script;
Jason Rumney's avatar
Jason Rumney committed
377 378
  DWORD mask;

379 380 381 382
  extra = AREF (entity, FONT_EXTRA_INDEX);
  if (!CONSP (extra))
    return -1;

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

389
  supported_scripts = XCDR (supported_scripts);
Jason Rumney's avatar
Jason Rumney committed
390

391
  script = CHAR_TABLE_REF (Vchar_script_table, c);
Jason Rumney's avatar
Jason Rumney committed
392

393 394 395 396 397 398 399 400
  /* 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
401 402 403 404 405
     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
406 407 408
}

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

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

421 422 423
  if (c < w32_font->metrics.tmFirstChar
      || c > w32_font->metrics.tmLastChar)
    return FONT_INVALID_CODE;
424
  else
425
    return c;
Jason Rumney's avatar
Jason Rumney committed
426 427 428 429 430
}

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

447 448
  struct w32font_info *w32_font = (struct w32font_info *) font;

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

451
  for (i = 0, first = true; i < nglyphs; i++)
452 453 454 455
    {
      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
456

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
      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
473

474 475 476 477 478
      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
479

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

482 483 484
      if (char_metric->status == W32METRIC_NO_ATTEMPT)
	{
	  if (dc == NULL)
Jason Rumney's avatar
Jason Rumney committed
485
	    {
486 487 488 489 490 491
	      /* 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);

492 493 494 495 496 497
	      /* 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;
498 499
	      dc = get_frame_dc (f);
	      old_font = SelectObject (dc, w32_font->hfont);
Jason Rumney's avatar
Jason Rumney committed
500
	    }
501 502
	  compute_metrics (dc, w32_font, *(code + i), char_metric);
	}
503

504 505
      if (char_metric->status == W32METRIC_SUCCESS)
	{
506 507 508 509 510 511 512 513 514 515 516 517 518
	  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;
519
	  metrics->width += char_metric->width;
520 521 522 523
	  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
524
	}
525 526 527 528 529 530 531 532 533 534 535 536 537
      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);
538
	  Vinhibit_quit = prev_quit;
539 540
	}
      return;
Jason Rumney's avatar
Jason Rumney committed
541
    }
542

543 544 545
  /* For non-truetype fonts, GetGlyphOutlineW is not supported, so
     fallback on other methods that will at least give some of the metric
     information.  */
546

547
  /* Make array big enough to hold surrogates.  */
548
  eassume (0 <= nglyphs);	/* pacify GCC warning on next line */
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
  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++;
        }
    }

568 569
  if (dc == NULL)
    {
Jason Rumney's avatar
Jason Rumney committed
570 571 572 573 574 575
      /* 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);

576 577 578 579 580 581
      /* 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;
582
      dc = get_frame_dc (f);
583
      old_font = SelectObject (dc, w32_font->hfont);
584 585
    }

586
  if (GetTextExtentPoint32W (dc, wcode, nglyphs, &size))
Jason Rumney's avatar
Jason Rumney committed
587
    {
588
      total_width = size.cx;
Jason Rumney's avatar
Jason Rumney committed
589 590
    }

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

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

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

/* w32 implementation of draw for font backend.
   Optional.
   Draw glyphs between FROM and TO of S->char2b at (X Y) pixel
618 619 620
   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).
621 622 623 624 625 626 627

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

  options = w32font->glyph_idx;
638 639 640 641 642

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

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

651 652 653 654 655 656 657 658 659 660 661
      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
662

663 664 665
  /* 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
666 667
  if (with_background)
    {
668 669
      HBRUSH brush;
      RECT rect;
670
      struct font *font = s->font;
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
      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
	 true, and that's in x_draw_glyph_string_foreground, when
	 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;
	}
691 692
      brush = CreateSolidBrush (s->gc->background);
      rect.left = x;
693
      rect.top = y - ascent;
694
      rect.right = x + s->width;
695
      rect.bottom = y + descent;
696
      FillRect (s->hdc, &rect, brush);
697
      DeleteObject (brush);
Jason Rumney's avatar
Jason Rumney committed
698
    }
699

700 701
  if (s->padding_p)
    {
702
      int i;
703 704 705

      for (i = 0; i < len; i++)
	ExtTextOutW (s->hdc, x + i, y, options, NULL,
706
		     s->char2b + from + i, 1, NULL);
707 708
    }
  else
709
    ExtTextOutW (s->hdc, x, y, options, NULL, s->char2b + from, len, NULL);
710 711 712

  /* Restore clip region.  */
  if (s->num_clips > 0)
713 714 715 716
    SelectClipRgn (s->hdc, orig_clip);

  if (orig_clip)
    DeleteObject (orig_clip);
717 718

  return len;
Jason Rumney's avatar
Jason Rumney committed
719 720 721
}

/* w32 implementation of free_entity for font backend.
Paul Eggert's avatar
Paul Eggert committed
722
   Optional.
Jason Rumney's avatar
Jason Rumney committed
723
   Free FONT_EXTRA_INDEX field of FONT_ENTITY.
724 725
static void
w32font_free_entity (Lisp_Object entity);
Jason Rumney's avatar
Jason Rumney committed
726 727 728 729 730 731 732
  */

/* 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.
733
static int
Dmitry Antipov's avatar
Dmitry Antipov committed
734
w32font_prepare_face (struct frame *f, struct face *face);
Jason Rumney's avatar
Jason Rumney committed
735 736 737 738
  */
/* w32 implementation of done_face for font backend.
   Optional.
   Done FACE for displaying characters by FACE->font on frame F.
739
static void
Dmitry Antipov's avatar
Dmitry Antipov committed
740
w32font_done_face (struct frame *f, struct face *face);  */
Jason Rumney's avatar
Jason Rumney committed
741 742 743 744

/* w32 implementation of get_bitmap for font backend.
   Optional.
   Store bitmap data for glyph-code CODE of FONT in BITMAP.  It is
745
   intended that this method is called from the other font-driver
Jason Rumney's avatar
Jason Rumney committed
746
   for actual drawing.
747 748 749
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
750 751 752 753
  */
/* w32 implementation of free_bitmap for font backend.
   Optional.
   Free bitmap data in BITMAP.
754 755
static void
w32font_free_bitmap (struct font *font, struct font_bitmap *bitmap);
Jason Rumney's avatar
Jason Rumney committed
756 757 758 759 760
  */
/* 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
761
   the operations was successful.  Otherwise return -1.
762 763
static int
w32font_anchor_point (struct font *font, unsigned code,
Jason Rumney's avatar
Jason Rumney committed
764 765 766 767 768 769
                                 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.
770 771
static Lisp_Object
w32font_otf_capability (struct font *font);
Jason Rumney's avatar
Jason Rumney committed
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
  */
/* 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.
789 790 791 792
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,
793
                   bool alternate_subst);
Jason Rumney's avatar
Jason Rumney committed
794 795
  */

Jason Rumney's avatar
Jason Rumney committed
796 797 798 799
/* 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
800 801
w32font_list_internal (struct frame *f, Lisp_Object font_spec,
		       bool opentype_only)
Jason Rumney's avatar
Jason Rumney committed
802 803 804 805 806 807
{
  struct font_callback_data match_data;
  HDC dc;

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

810
  memset (&match_data.pattern, 0, sizeof (LOGFONT));
Jason Rumney's avatar
Jason Rumney committed
811 812
  fill_in_logfont (f, &match_data.pattern, font_spec);

813 814 815 816 817 818 819 820 821
  /* 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)
822 823
	  && !EQ (spec_charset, Qunknown)
	  && !EQ (spec_charset, Qascii_0))
824 825 826
	return Qnil;
    }

Jason Rumney's avatar
Jason Rumney committed
827 828 829 830 831 832 833 834 835 836 837 838
  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
    {
839 840 841 842 843 844 845
      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
846 847 848 849 850 851
      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);
852
      Vinhibit_quit = prev_quit;
Jason Rumney's avatar
Jason Rumney committed
853 854
    }

855
  return match_data.list;
Jason Rumney's avatar
Jason Rumney committed
856 857 858 859 860 861
}

/* 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
862 863
w32font_match_internal (struct frame *f, Lisp_Object font_spec,
			bool opentype_only)
Jason Rumney's avatar
Jason Rumney committed
864 865 866 867 868
{
  struct font_callback_data match_data;
  HDC dc;

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

872
  memset (&match_data.pattern, 0, sizeof (LOGFONT));
Jason Rumney's avatar
Jason Rumney committed
873 874 875 876 877 878
  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;

879 880 881 882 883 884
  /* 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
885 886 887 888 889 890
  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);
891
  Vinhibit_quit = prev_quit;
Jason Rumney's avatar
Jason Rumney committed
892 893 894 895

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

896
int
Dmitry Antipov's avatar
Dmitry Antipov committed
897
w32font_open_internal (struct frame *f, Lisp_Object font_entity,
898
		       int pixel_size, Lisp_Object font_object)
899
{
900
  int len, size;
901 902 903
  LOGFONT logfont;
  HDC dc;
  HFONT hfont, old_font;
904
  Lisp_Object val;
905 906
  struct w32font_info *w32_font;
  struct font * font;
907
  OUTLINETEXTMETRICW* metrics = NULL;
908 909 910

  w32_font = (struct w32font_info *) XFONT_OBJECT (font_object);
  font = (struct font *) w32_font;
911 912 913 914

  if (!font)
    return 0;

915
  memset (&logfont, 0, sizeof (logfont));
916 917
  fill_in_logfont (f, &logfont, font_entity);

918 919 920 921 922 923
  /* 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
924
  size = XFIXNUM (AREF (font_entity, FONT_SIZE_INDEX));
925 926 927 928 929 930 931 932 933 934 935 936 937
  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);

938
  /* Try getting the outline metrics (only works for truetype fonts).  */
939
  len = get_outline_metrics_w (dc, 0, NULL);
940 941
  if (len)
    {
942
      metrics = (OUTLINETEXTMETRICW *) alloca (len);
943
      if (get_outline_metrics_w (dc, len, metrics))
944 945
        memcpy (&w32_font->metrics, &metrics->otmTextMetrics,
		sizeof (TEXTMETRICW));
946 947 948
      else
        metrics = NULL;
    }
949

950
  if (!metrics)
951
    get_text_metrics_w (dc, &w32_font->metrics);
952

Jason Rumney's avatar
Jason Rumney committed
953 954
  w32_font->cached_metrics = NULL;
  w32_font->n_cache_blocks = 0;
955

956 957
  SelectObject (dc, old_font);
  release_frame_dc (f, dc);
958

959
  w32_font->hfont = hfont;
960

961 962 963 964 965 966
  {
    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;
967
    name = alloca (len);
968 969
    while (name && w32font_full_name (&logfont, font_entity, pixel_size,
                                      name, len) < 0)
970
      {
971 972
        len += 32;
        name = alloca (len);
973 974
      }
    if (name)
975
      font->props[FONT_FULLNAME_INDEX]
976
        = DECODE_SYSTEM (build_string (name));
977
    else
978 979
      font->props[FONT_FULLNAME_INDEX]
	= DECODE_SYSTEM (build_string (logfont.lfFaceName));
980
  }
981 982

  font->max_width = w32_font->metrics.tmMaxCharWidth;
983
  /* Parts of Emacs display assume that height = ascent + descent...
984
     so height is defined later, after ascent and descent.
985
  font->height = w32_font->metrics.tmHeight
986
    + w32_font->metrics.tmExternalLeading;
987 988
  */

989 990 991 992 993 994
  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;
995 996 997 998
  font->pixel_size = size;
  font->driver = &w32font_driver;
  font->encoding_charset = -1;
  font->repertory_charset = -1;
999
  /* TODO: do we really want the minimum width here, which could be negative? */
1000
  font->min_width = font->space_width;
1001 1002
  font->ascent = w32_font->metrics.tmAscent;
  font->descent = w32_font->metrics.tmDescent;
1003
  font->height = font->ascent + font->descent;
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014

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

1016 1017 1018 1019
  /* 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
1020
  font->props[FONT_NAME_INDEX] = Ffont_xlfd_name (font_object, Qnil);
1021

1022 1023 1024
  return 1;
}

Jason Rumney's avatar
Jason Rumney committed
1025 1026
/* Callback function for EnumFontFamiliesEx.
 * Adds the name of a font to a Lisp list (passed in as the lParam arg).  */
1027
static int CALLBACK ALIGN_STACK
1028 1029 1030
add_font_name_to_list (ENUMLOGFONTEX *logical_font,
		       NEWTEXTMETRICEX *physical_font,
		       DWORD font_type, LPARAM list_object)
Jason Rumney's avatar
Jason Rumney committed
1031 1032
{
  Lisp_Object* list = (Lisp_Object *) list_object;
1033 1034 1035 1036 1037 1038
  Lisp_Object family;

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

1039
  family = intern_font_name (logical_font->elfLogFont.lfFaceName);
Jason Rumney's avatar
Jason Rumney committed
1040 1041 1042 1043 1044 1045
  if (! memq_no_quit (family, *list))
    *list = Fcons (family, *list);

  return 1;
}

1046 1047
static int w32_decode_weight (int);
static int w32_encode_weight (int);
1048

Jason Rumney's avatar
Jason Rumney committed
1049
/* Convert an enumerated Windows font to an Emacs font entity.  */
1050
static Lisp_Object
1051 1052 1053 1054 1055 1056
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
1057 1058 1059 1060
{
  Lisp_Object entity, tem;
  LOGFONT *lf = (LOGFONT*) logical_font;
  BYTE generic_type;
1061
  DWORD full_type = physical_font->ntmTm.ntmFlags;
Jason Rumney's avatar
Jason Rumney committed
1062

1063
  entity = font_make_entity ();
Jason Rumney's avatar
Jason Rumney committed
1064

1065
  ASET (entity, FONT_TYPE_INDEX, backend);
1066
  ASET (entity, FONT_REGISTRY_INDEX, w32_registry (lf->lfCharSet, font_type));
Jason Rumney's avatar
Jason Rumney committed
1067 1068 1069
  ASET (entity, FONT_OBJLIST_INDEX, Qnil);

  /* Foundry is difficult to get in readable form on Windows.
1070
     But Emacs crashes if it is not set, so set it to something more
1071
     generic.  These values make xlfds compatible with Emacs 22. */
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
  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
1083 1084 1085 1086
  generic_type = physical_font->ntmTm.tmPitchAndFamily & 0xF0;
  if (generic_type == FF_DECORATIVE)
    tem = Qdecorative;
  else if (generic_type == FF_MODERN)
1087
    tem = Qmono;
Jason Rumney's avatar
Jason Rumney committed
1088
  else if (generic_type == FF_ROMAN)
1089
    tem = Qserif;
Jason Rumney's avatar
Jason Rumney committed
1090 1091 1092
  else if (generic_type == FF_SCRIPT)
    tem = Qscript;
  else if (generic_type == FF_SWISS)
1093
    tem = Qsans;
Jason Rumney's avatar
Jason Rumney committed
1094
  else
1095
    tem = Qnil;
1096 1097

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

1099
  if (physical_font->ntmTm.tmPitchAndFamily & 0x01)
1100
    ASET (entity, FONT_SPACING_INDEX, make_fixnum (FONT_SPACING_PROPORTIONAL));
1101
  else
1102
    ASET (entity, FONT_SPACING_INDEX, make_fixnum (FONT_SPACING_CHARCELL));
Jason Rumney's avatar
Jason Rumney committed
1103

1104 1105 1106 1107 1108
  if (requested_font->lfQuality != DEFAULT_QUALITY)
    {
      font_put_extra (entity, QCantialias,
                      lispy_antialias_type (requested_font->lfQuality));
    }
Jason Rumney's avatar
Jason Rumney committed
1109
  ASET (entity, FONT_FAMILY_INDEX,
1110
	intern_font_name (lf->lfFaceName));
Jason Rumney's avatar
Jason Rumney committed
1111

1112
  FONT_SET_STYLE (entity, FONT_WEIGHT_INDEX,
1113
		  make_fixnum (w32_decode_weight (lf->lfWeight)));
1114
  FONT_SET_STYLE (entity, FONT_SLANT_INDEX,
1115
		  make_fixnum (lf->lfItalic ? 200 : 100));
1116 1117
  /* TODO: PANOSE struct has this info, but need to call GetOutlineTextMetrics
     to get it.  */
1118
  FONT_SET_STYLE (entity, FONT_WIDTH_INDEX, make_fixnum (100));
Jason Rumney's avatar
Jason Rumney committed
1119

1120
  if (font_type & RASTER_FONTTYPE)
1121
    ASET (entity, FONT_SIZE_INDEX,
1122
          make_fixnum (physical_font->ntmTm.tmHeight