gdkfont-win32.c 55.2 KB
Newer Older
1 2 3 4
/* GDK - The GIMP Drawing Kit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6 7 8 9 10 11
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15 16 17 18 19 20
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
21
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22 23 24 25 26 27 28 29
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

#include <stdio.h>
#include <ctype.h>

Tor Lillqvist's avatar
Tor Lillqvist committed
30 31
#include <pango/pangowin32.h>

32
#include "gdkfont.h"
33 34
#include "gdkinternals.h"
#include "gdkprivate-win32.h"
35 36 37 38 39 40 41 42 43

static GHashTable *font_name_hash = NULL;
static GHashTable *fontset_name_hash = NULL;

static void
gdk_font_hash_insert (GdkFontType  type,
		      GdkFont     *font,
		      const gchar *font_name)
{
Tor Lillqvist's avatar
Tor Lillqvist committed
44
  GdkFontPrivateWin32 *private = (GdkFontPrivateWin32 *) font;
45 46 47 48 49 50 51 52 53 54 55 56 57 58
  GHashTable **hashp = (type == GDK_FONT_FONT) ?
    &font_name_hash : &fontset_name_hash;

  if (!*hashp)
    *hashp = g_hash_table_new (g_str_hash, g_str_equal);

  private->names = g_slist_prepend (private->names, g_strdup (font_name));
  g_hash_table_insert (*hashp, private->names->data, font);
}

static void
gdk_font_hash_remove (GdkFontType type,
		      GdkFont    *font)
{
Tor Lillqvist's avatar
Tor Lillqvist committed
59
  GdkFontPrivateWin32 *private = (GdkFontPrivateWin32 *) font;
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
  GSList *tmp_list;
  GHashTable *hash = (type == GDK_FONT_FONT) ?
    font_name_hash : fontset_name_hash;

  tmp_list = private->names;
  while (tmp_list)
    {
      g_hash_table_remove (hash, tmp_list->data);
      g_free (tmp_list->data);
      
      tmp_list = tmp_list->next;
    }

  g_slist_free (private->names);
  private->names = NULL;
}

static GdkFont *
gdk_font_hash_lookup (GdkFontType  type,
		      const gchar *font_name)
{
  GdkFont *result;
  GHashTable *hash = (type == GDK_FONT_FONT) ?
    font_name_hash : fontset_name_hash;

  if (!hash)
    return NULL;
  else
    {
      result = g_hash_table_lookup (hash, font_name);
      if (result)
	gdk_font_ref (result);
      
      return result;
    }
}

static const char *
charset_name (DWORD charset)
{
  switch (charset)
    {
    case ANSI_CHARSET: return "ansi";
    case DEFAULT_CHARSET: return "default";
    case SYMBOL_CHARSET: return "symbol";
    case SHIFTJIS_CHARSET: return "shiftjis";
    case HANGEUL_CHARSET: return "hangeul";
    case GB2312_CHARSET: return "gb2312";
    case CHINESEBIG5_CHARSET: return "big5";
    case JOHAB_CHARSET: return "johab";
    case HEBREW_CHARSET: return "hebrew";
    case ARABIC_CHARSET: return "arabic";
    case GREEK_CHARSET: return "greek";
    case TURKISH_CHARSET: return "turkish";
    case VIETNAMESE_CHARSET: return "vietnamese";
    case THAI_CHARSET: return "thai";
    case EASTEUROPE_CHARSET: return "easteurope";
    case RUSSIAN_CHARSET: return "russian";
    case MAC_CHARSET: return "mac";
    case BALTIC_CHARSET: return "baltic";
    }
  return "unknown";
}

static gint num_fonts;
static gint font_names_size;
static gchar **xfontnames;

static gchar *
logfont_to_xlfd (const LOGFONT *lfp,
		 int            size,
		 int            res,
		 int            avg_width)
{
  const gchar *weight;
  const gchar *registry, *encoding;
  int point_size;
  static int logpixelsy = 0;
138 139
  gchar facename[LF_FACESIZE*5];
  gchar *utf8_facename;
140 141 142 143 144
  gchar *p;
  const gchar *q;

  if (logpixelsy == 0)
    {
145
      logpixelsy = GetDeviceCaps (gdk_display_hdc, LOGPIXELSY);
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
    }

  if (lfp->lfWeight >= FW_HEAVY)
    weight = "heavy";
  else if (lfp->lfWeight >= FW_EXTRABOLD)
    weight = "extrabold";
  else if (lfp->lfWeight >= FW_BOLD)
    weight = "bold";
#ifdef FW_DEMIBOLD
  else if (lfp->lfWeight >= FW_DEMIBOLD)
    weight = "demibold";
#endif
  else if (lfp->lfWeight >= FW_MEDIUM)
    weight = "medium";
  else if (lfp->lfWeight >= FW_NORMAL)
    weight = "normal";
  else if (lfp->lfWeight >= FW_LIGHT)
    weight = "light";
  else if (lfp->lfWeight >= FW_EXTRALIGHT)
    weight = "extralight";
  else if (lfp->lfWeight >= FW_THIN)
    weight = "thin";
  else
    weight = "regular";

  switch (lfp->lfCharSet)
    {
    case ANSI_CHARSET:
      registry = "iso8859";
      encoding = "1";
      break;
    case SHIFTJIS_CHARSET:
      registry = "jisx0208.1983";
      encoding = "0";
      break;
    case HANGEUL_CHARSET:
      registry = "ksc5601.1987";
      encoding = "0";
      break;
    case GB2312_CHARSET:
      registry = "gb2312.1980";
      encoding = "0";
      break;
    case CHINESEBIG5_CHARSET:
      registry = "big5";
      encoding = "0";
      break;
    case GREEK_CHARSET:
      registry = "iso8859";
      encoding = "7";
      break;
    case TURKISH_CHARSET:
      registry = "iso8859";
      encoding = "9";
      break;
#if 0 /* Not a good idea, I think, to use ISO8859-8 and -6 for the Windows
       * hebrew and arabic codepages, they differ too much.
       */
    case HEBREW_CHARSET:
      registry = "iso8859";
      encoding = "8";
      break;
    case ARABIC_CHARSET:
      registry = "iso8859";
      encoding = "6";
      break;
#endif
    default:
      registry = "microsoft";
      encoding = charset_name (lfp->lfCharSet);
    }
  
  point_size = (int) (((double) size/logpixelsy) * 720.);

  if (res == -1)
    res = logpixelsy;

223 224 225 226 227 228
  /* Convert the facename Windows fives us from the locale-dependent
   * codepage to UTF-8.
   */
  utf8_facename = g_filename_to_utf8 (lfp->lfFaceName);

  /* Replace characters illegal in an XLFD with hex escapes. */
229
  p = facename;
230
  q = utf8_facename;
231 232 233 234 235 236 237 238 239
  while (*q)
    {
      if (*q == '-' || *q == '*' || *q == '?' || *q == '%')
	p += sprintf (p, "%%%.02x", *q);
      else
	*p++ = *q;
      q++;
    }
  *p = '\0';
240
  g_free (utf8_facename);
241

242
  return g_strdup_printf
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
    ("-%s-%s-%s-%s-%s-%s-%d-%d-%d-%d-%s-%d-%s-%s",
     "unknown", 
     facename,
     weight,
     (lfp->lfItalic ?
      ((lfp->lfPitchAndFamily & 0xF0) == FF_ROMAN
       || (lfp->lfPitchAndFamily & 0xF0) == FF_SCRIPT ?
       "i" : "o") : "r"),
     "normal",
     "",
     size,
     point_size,
     res,
     res,
     ((lfp->lfPitchAndFamily & 0x03) == FIXED_PITCH ? "m" : "p"),
     avg_width,
     registry, encoding);
}

gchar *
263
gdk_font_full_name_get (GdkFont *font)
264
{
Tor Lillqvist's avatar
Tor Lillqvist committed
265
  GdkFontPrivateWin32 *private;
266 267 268 269
  GdkWin32SingleFont *singlefont;
  GSList *list;
  GString *string;
  gchar *result;
270
  gchar *xlfd;
271 272 273 274
  LOGFONT logfont;

  g_return_val_if_fail (font != NULL, NULL);

Tor Lillqvist's avatar
Tor Lillqvist committed
275
  private = (GdkFontPrivateWin32 *) font;
276 277 278 279 280 281 282 283

  list = private->fonts;
  string = g_string_new ("");

  while (list)
    {
      singlefont = (GdkWin32SingleFont *) list->data;

284
      if (GetObject (singlefont->hfont, sizeof (LOGFONT), &logfont) == 0)
285
	{
286
	  WIN32_GDI_FAILED ("GetObject");
287 288 289
	  return NULL;
	}

290 291 292
      xlfd = logfont_to_xlfd (&logfont, logfont.lfHeight, -1, 0);
      string = g_string_append (string, xlfd);
      g_free (xlfd);
293 294 295 296 297 298 299 300 301 302
      list = list->next;
      if (list)
	string = g_string_append_c (string, ',');
    }
  result = string->str;
  g_string_free (string, FALSE);
  return result;
}

void
303
gdk_font_full_name_free (gchar *name)
304
{
305
  g_free (name);
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
}

static gboolean
pattern_match (const gchar *pattern,
	       const gchar *string)
{
  const gchar *p = pattern, *n = string;
  gchar c, c1;

  /* Common case first */
  if ((pattern[0] == '*'
       && pattern[1] == '\0')
      || (pattern[0] == '-'
	  && pattern[1] == '*'
	  && pattern[2] == '\0'))
    return TRUE;

  while ((c = *p++) != '\0')
    {
      c = tolower (c);

      switch (c)
	{
	case '?':
	  if (*n == '\0')
	    return FALSE;
	  break;

	case '*':
	  for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
	    if (c == '?' && *n == '\0')
	    return FALSE;

	  if (c == '\0')
	    return TRUE;

	  c1 = tolower (c);
	  for (--p; *n != '\0'; ++n)
	    if (tolower (*n) == c1
		&& pattern_match (p, n))
	      return TRUE;
	  return FALSE;

	default:
	  if (c != tolower (*n))
	    return FALSE;
	}

      ++n;
    }

  if (*n == '\0')
    return TRUE;

  return FALSE;
}

Tor Lillqvist's avatar
Tor Lillqvist committed
363
static int CALLBACK
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
InnerEnumFontFamExProc (const LOGFONT    *lfp,
			const TEXTMETRIC *metrics,
			DWORD	          fontType,
			LPARAM            lParam)
{
  int size;
  gchar *xlfd;

  if (fontType == TRUETYPE_FONTTYPE)
    {
      size = 0;
    }
  else
    {
      size = lfp->lfHeight;
    }

  xlfd = logfont_to_xlfd (lfp, size, 0, 0);

  if (!pattern_match ((gchar *) lParam, xlfd))
    {
      g_free (xlfd);
      return 1;
    }

  num_fonts++;
  if (num_fonts == font_names_size)
    {
      font_names_size *= 2;
      xfontnames = g_realloc (xfontnames, font_names_size * sizeof (gchar *));
    }
  xfontnames[num_fonts-1] = xlfd;
    
  return 1;
}

Tor Lillqvist's avatar
Tor Lillqvist committed
400
static int CALLBACK
401 402 403 404 405 406 407 408 409 410 411
EnumFontFamExProc (const LOGFONT    *lfp,
		   const TEXTMETRIC *metrics,
		   DWORD             fontType,
		   LPARAM            lParam)
{
  if (fontType == TRUETYPE_FONTTYPE)
    {
      LOGFONT lf;

      lf = *lfp;

412
      EnumFontFamiliesEx (gdk_display_hdc, &lf, InnerEnumFontFamExProc, lParam, 0);
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
    }
  else
    InnerEnumFontFamExProc (lfp, metrics, fontType, lParam);

  return 1;
}

gchar **
gdk_font_list_new (const gchar *font_pattern,
		   gint        *n_returned)
{
  LOGFONT logfont;
  gchar **result;

  num_fonts = 0;
  font_names_size = 100;
  xfontnames = g_new (gchar *, font_names_size);
  memset (&logfont, 0, sizeof (logfont));
  logfont.lfCharSet = DEFAULT_CHARSET;
432
  EnumFontFamiliesEx (gdk_display_hdc, &logfont, EnumFontFamExProc,
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
		      (LPARAM) font_pattern, 0);

  result = g_new (gchar *, num_fonts + 1);
  memmove (result, xfontnames, num_fonts * sizeof (gchar *));
  result[num_fonts] = NULL;
  g_free (xfontnames);

  *n_returned = num_fonts;
  return result;
}

void
gdk_font_list_free (gchar **font_list)
{
  g_strfreev (font_list);
}

Tor Lillqvist's avatar
Tor Lillqvist committed
450
/* This table classifies Unicode characters according to the Microsoft
451
 * Unicode subset numbering. This is based on the table in "Developing
Tor Lillqvist's avatar
Tor Lillqvist committed
452 453 454 455
 * International Software for Windows 95 and Windows NT". This is almost,
 * but not quite, the same as the official Unicode block table in
 * Blocks.txt from ftp.unicode.org. The bit number field is the bitfield
 * number as in the FONTSIGNATURE struct's fsUsb field.
456
 * There are some grave bugs in the table in the books. For instance
457
 * it claims there are Hangul at U+3400..U+4DFF while this range in
458 459
 * fact contains CJK Unified Ideographs Extension A. Also, the whole
 * block of Hangul Syllables U+AC00..U+D7A3 is missing from the book.
Tor Lillqvist's avatar
Tor Lillqvist committed
460
 */
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532

typedef enum
{
  U_BASIC_LATIN = 0,
  U_LATIN_1_SUPPLEMENT = 1,
  U_LATIN_EXTENDED_A = 2,
  U_LATIN_EXTENDED_B = 3,
  U_IPA_EXTENSIONS = 4,
  U_SPACING_MODIFIER_LETTERS = 5,
  U_COMBINING_DIACRITICAL_MARKS = 6,
  U_BASIC_GREEK = 7,
  U_GREEK_SYMBOLS_AND_COPTIC = 8,
  U_CYRILLIC = 9,
  U_ARMENIAN = 10,
  U_HEBREW_EXTENDED = 12,
  U_BASIC_HEBREW = 11,
  U_BASIC_ARABIC = 13,
  U_ARABIC_EXTENDED = 14,
  U_DEVANAGARI = 15,
  U_BENGALI = 16,
  U_GURMUKHI = 17,
  U_GUJARATI = 18,
  U_ORIYA = 19,
  U_TAMIL = 20,
  U_TELUGU = 21,
  U_KANNADA = 22,
  U_MALAYALAM = 23,
  U_THAI = 24,
  U_LAO = 25,
  U_GEORGIAN_EXTENDED = 27,
  U_BASIC_GEORGIAN = 26,
  U_HANGUL_JAMO = 28,
  U_LATIN_EXTENDED_ADDITIONAL = 29,
  U_GREEK_EXTENDED = 30,
  U_GENERAL_PUNCTUATION = 31,
  U_SUPERSCRIPTS_AND_SUBSCRIPTS = 32,
  U_CURRENCY_SYMBOLS = 33,
  U_COMBINING_DIACRITICAL_MARKS_FOR_SYMBOLS = 34,
  U_LETTERLIKE_SYMBOLS = 35,
  U_NUMBER_FORMS = 36,
  U_ARROWS = 37,
  U_MATHEMATICAL_OPERATORS = 38,
  U_MISCELLANEOUS_TECHNICAL = 39,
  U_CONTROL_PICTURES = 40,
  U_OPTICAL_CHARACTER_RECOGNITION = 41,
  U_ENCLOSED_ALPHANUMERICS = 42,
  U_BOX_DRAWING = 43,
  U_BLOCK_ELEMENTS = 44,
  U_GEOMETRIC_SHAPES = 45,
  U_MISCELLANEOUS_SYMBOLS = 46,
  U_DINGBATS = 47,
  U_CJK_SYMBOLS_AND_PUNCTUATION = 48,
  U_HIRAGANA = 49,
  U_KATAKANA = 50,
  U_BOPOMOFO = 51,
  U_HANGUL_COMPATIBILITY_JAMO = 52,
  U_CJK_MISCELLANEOUS = 53,
  U_ENCLOSED_CJK = 54,
  U_CJK_COMPATIBILITY = 55,
  U_HANGUL = 56,
  U_HANGUL_SUPPLEMENTARY_A = 57,
  U_HANGUL_SUPPLEMENTARY_B = 58,
  U_CJK_UNIFIED_IDEOGRAPHS = 59,
  U_PRIVATE_USE_AREA = 60,
  U_CJK_COMPATIBILITY_IDEOGRAPHS = 61,
  U_ALPHABETIC_PRESENTATION_FORMS = 62,
  U_ARABIC_PRESENTATION_FORMS_A = 63,
  U_COMBINING_HALF_MARKS = 64,
  U_CJK_COMPATIBILITY_FORMS = 65,
  U_SMALL_FORM_VARIANTS = 66,
  U_ARABIC_PRESENTATION_FORMS_B = 67,
  U_SPECIALS = 69,
533 534
  U_HALFWIDTH_AND_FULLWIDTH_FORMS = 68,
  U_LAST_PLUS_ONE
535 536
} unicode_subset;

Tor Lillqvist's avatar
Tor Lillqvist committed
537 538
static struct {
  wchar_t low, high;
539
  unicode_subset bit; 
Tor Lillqvist's avatar
Tor Lillqvist committed
540 541 542
  gchar *name;
} utab[] =
{
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
  { 0x0000, 0x007E,
    U_BASIC_LATIN, "Basic Latin" },
  { 0x00A0, 0x00FF,
    U_LATIN_1_SUPPLEMENT, "Latin-1 Supplement" },
  { 0x0100, 0x017F,
    U_LATIN_EXTENDED_A, "Latin Extended-A" },
  { 0x0180, 0x024F,
    U_LATIN_EXTENDED_B, "Latin Extended-B" },
  { 0x0250, 0x02AF,
    U_IPA_EXTENSIONS, "IPA Extensions" },
  { 0x02B0, 0x02FF,
    U_SPACING_MODIFIER_LETTERS, "Spacing Modifier Letters" },
  { 0x0300, 0x036F,
    U_COMBINING_DIACRITICAL_MARKS, "Combining Diacritical Marks" },
  { 0x0370, 0x03CF,
    U_BASIC_GREEK, "Basic Greek" },
  { 0x03D0, 0x03FF,
    U_GREEK_SYMBOLS_AND_COPTIC, "Greek Symbols and Coptic" },
  { 0x0400, 0x04FF,
    U_CYRILLIC, "Cyrillic" },
  { 0x0530, 0x058F,
    U_ARMENIAN, "Armenian" },
  { 0x0590, 0x05CF,
    U_HEBREW_EXTENDED, "Hebrew Extended" },
  { 0x05D0, 0x05FF,
    U_BASIC_HEBREW, "Basic Hebrew" },
  { 0x0600, 0x0652,
    U_BASIC_ARABIC, "Basic Arabic" },
  { 0x0653, 0x06FF,
    U_ARABIC_EXTENDED, "Arabic Extended" },
  { 0x0900, 0x097F,
    U_DEVANAGARI, "Devanagari" },
  { 0x0980, 0x09FF,
    U_BENGALI, "Bengali" },
  { 0x0A00, 0x0A7F,
    U_GURMUKHI, "Gurmukhi" },
  { 0x0A80, 0x0AFF,
    U_GUJARATI, "Gujarati" },
  { 0x0B00, 0x0B7F,
    U_ORIYA, "Oriya" },
  { 0x0B80, 0x0BFF,
    U_TAMIL, "Tamil" },
  { 0x0C00, 0x0C7F,
    U_TELUGU, "Telugu" },
  { 0x0C80, 0x0CFF,
    U_KANNADA, "Kannada" },
  { 0x0D00, 0x0D7F,
    U_MALAYALAM, "Malayalam" },
  { 0x0E00, 0x0E7F,
    U_THAI, "Thai" },
  { 0x0E80, 0x0EFF,
    U_LAO, "Lao" },
  { 0x10A0, 0x10CF,
    U_GEORGIAN_EXTENDED, "Georgian Extended" },
  { 0x10D0, 0x10FF,
    U_BASIC_GEORGIAN, "Basic Georgian" },
  { 0x1100, 0x11FF,
    U_HANGUL_JAMO, "Hangul Jamo" },
  { 0x1E00, 0x1EFF,
    U_LATIN_EXTENDED_ADDITIONAL, "Latin Extended Additional" },
  { 0x1F00, 0x1FFF,
    U_GREEK_EXTENDED, "Greek Extended" },
  { 0x2000, 0x206F,
    U_GENERAL_PUNCTUATION, "General Punctuation" },
  { 0x2070, 0x209F,
    U_SUPERSCRIPTS_AND_SUBSCRIPTS, "Superscripts and Subscripts" },
  { 0x20A0, 0x20CF,
    U_CURRENCY_SYMBOLS, "Currency Symbols" },
  { 0x20D0, 0x20FF,
    U_COMBINING_DIACRITICAL_MARKS_FOR_SYMBOLS, "Combining Diacritical Marks for Symbols" },
  { 0x2100, 0x214F,
    U_LETTERLIKE_SYMBOLS, "Letterlike Symbols" },
  { 0x2150, 0x218F,
    U_NUMBER_FORMS, "Number Forms" },
  { 0x2190, 0x21FF,
    U_ARROWS, "Arrows" },
  { 0x2200, 0x22FF,
    U_MATHEMATICAL_OPERATORS, "Mathematical Operators" },
  { 0x2300, 0x23FF,
    U_MISCELLANEOUS_TECHNICAL, "Miscellaneous Technical" },
  { 0x2400, 0x243F,
    U_CONTROL_PICTURES, "Control Pictures" },
  { 0x2440, 0x245F,
    U_OPTICAL_CHARACTER_RECOGNITION, "Optical Character Recognition" },
  { 0x2460, 0x24FF,
    U_ENCLOSED_ALPHANUMERICS, "Enclosed Alphanumerics" },
  { 0x2500, 0x257F,
    U_BOX_DRAWING, "Box Drawing" },
  { 0x2580, 0x259F,
    U_BLOCK_ELEMENTS, "Block Elements" },
  { 0x25A0, 0x25FF,
    U_GEOMETRIC_SHAPES, "Geometric Shapes" },
  { 0x2600, 0x26FF,
    U_MISCELLANEOUS_SYMBOLS, "Miscellaneous Symbols" },
  { 0x2700, 0x27BF,
    U_DINGBATS, "Dingbats" },
  { 0x3000, 0x303F,
    U_CJK_SYMBOLS_AND_PUNCTUATION, "CJK Symbols and Punctuation" },
  { 0x3040, 0x309F,
    U_HIRAGANA, "Hiragana" },
  { 0x30A0, 0x30FF,
    U_KATAKANA, "Katakana" },
  { 0x3100, 0x312F,
    U_BOPOMOFO, "Bopomofo" },
  { 0x3130, 0x318F,
    U_HANGUL_COMPATIBILITY_JAMO, "Hangul Compatibility Jamo" },
  { 0x3190, 0x319F,
    U_CJK_MISCELLANEOUS, "CJK Miscellaneous" },
  { 0x3200, 0x32FF,
    U_ENCLOSED_CJK, "Enclosed CJK" },
  { 0x3300, 0x33FF,
    U_CJK_COMPATIBILITY, "CJK Compatibility" },
655 656 657 658 659 660 661 662 663
  /* The book claims:
   * U+3400..U+3D2D = Hangul
   * U+3D2E..U+44B7 = Hangul Supplementary A
   * U+44B8..U+4DFF = Hangul Supplementary B
   * but actually in Unicode
   * U+3400..U+4DB5 = CJK Unified Ideographs Extension A
   */
  { 0x3400, 0x4DB5,
    U_CJK_UNIFIED_IDEOGRAPHS, "CJK Unified Ideographs Extension A" },
664 665
  { 0x4E00, 0x9FFF,
    U_CJK_UNIFIED_IDEOGRAPHS, "CJK Unified Ideographs" },
666 667 668
  /* This was missing completely from the book's table. */
  { 0xAC00, 0xD7A3,
    U_HANGUL, "Hangul Syllables" },
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
  { 0xE000, 0xF8FF,
    U_PRIVATE_USE_AREA, "Private Use Area" },
  { 0xF900, 0xFAFF,
    U_CJK_COMPATIBILITY_IDEOGRAPHS, "CJK Compatibility Ideographs" },
  { 0xFB00, 0xFB4F,
    U_ALPHABETIC_PRESENTATION_FORMS, "Alphabetic Presentation Forms" },
  { 0xFB50, 0xFDFF,
    U_ARABIC_PRESENTATION_FORMS_A, "Arabic Presentation Forms-A" },
  { 0xFE20, 0xFE2F,
    U_COMBINING_HALF_MARKS, "Combining Half Marks" },
  { 0xFE30, 0xFE4F,
    U_CJK_COMPATIBILITY_FORMS, "CJK Compatibility Forms" },
  { 0xFE50, 0xFE6F,
    U_SMALL_FORM_VARIANTS, "Small Form Variants" },
  { 0xFE70, 0xFEFE,
    U_ARABIC_PRESENTATION_FORMS_B, "Arabic Presentation Forms-B" },
  { 0xFEFF, 0xFEFF,
    U_SPECIALS, "Specials" },
  { 0xFF00, 0xFFEF,
    U_HALFWIDTH_AND_FULLWIDTH_FORMS, "Halfwidth and Fullwidth Forms" },
  { 0xFFF0, 0xFFFD,
    U_SPECIALS, "Specials" }
Tor Lillqvist's avatar
Tor Lillqvist committed
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
};

static void
print_unicode_subranges (FONTSIGNATURE *fsp)
{
  int i;
  gboolean checked[sizeof (utab) / sizeof (utab[0])];
  gboolean need_comma = FALSE;

  memset (checked, 0, sizeof (checked));

  for (i = 0; i < sizeof (utab) / sizeof (utab[0]); i++)
    if (!checked[i]
	&& (fsp->fsUsb[utab[i].bit/32] & (1 << (utab[i].bit % 32))))
      {
	g_print ("%s %s", (need_comma ? "," : ""), utab[i].name);
	need_comma = TRUE;
	checked[i] = TRUE;
      }
710 711
  if (!need_comma)
    g_print (" none!");
Tor Lillqvist's avatar
Tor Lillqvist committed
712 713 714
  g_print ("\n");
}

715 716 717 718
static gboolean
check_unicode_subranges (UINT           charset,
			 FONTSIGNATURE *fsp)
{
719
  gint i;
720 721
  gboolean retval = FALSE;

722 723 724 725
  /* If the fsUsb bit array has at least one of the bits set, trust it */
  for (i = 0; i < U_LAST_PLUS_ONE; i++)
    if (i != U_PRIVATE_USE_AREA && (fsp->fsUsb[i/32] & (1 << (i % 32))))
      return FALSE;
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842

  /* Otherwise, guess what subranges there should be in the font */
  fsp->fsUsb[0] = fsp->fsUsb[1] = fsp->fsUsb[2] = fsp->fsUsb[3] = 0;

#define set_bit(bitno) (fsp->fsUsb[(bitno)/32] |= (1 << ((bitno) % 32)))

  /* Set Unicode subrange bits based on code pages supported.
   * This is mostly just guesswork.
   */

#define check_cp(bit) (fsp->fsCsb[0] & (bit))

  if (check_cp(FS_LATIN1))
    {
      set_bit (U_BASIC_LATIN);
      set_bit (U_LATIN_1_SUPPLEMENT);
      set_bit (U_CURRENCY_SYMBOLS);
      retval = TRUE;
    }
  if (check_cp (FS_LATIN2))
    {
      set_bit (U_BASIC_LATIN);
      set_bit (U_LATIN_1_SUPPLEMENT);
      set_bit (U_LATIN_EXTENDED_A);
      set_bit (U_CURRENCY_SYMBOLS);
      retval = TRUE;
    }
  if (check_cp (FS_CYRILLIC))
    {
      set_bit (U_BASIC_LATIN);
      set_bit (U_CYRILLIC);
      retval = TRUE;
    }
  if (check_cp (FS_GREEK))
    {
      set_bit (U_BASIC_LATIN);
      set_bit (U_BASIC_GREEK);
      retval = TRUE;
    }
  if (check_cp (FS_TURKISH))
    {
      set_bit (U_BASIC_LATIN);
      set_bit (U_LATIN_1_SUPPLEMENT);
      set_bit (U_LATIN_EXTENDED_A);
      set_bit (U_CURRENCY_SYMBOLS);
      retval = TRUE;
    }
  if (check_cp (FS_HEBREW))
    {
      set_bit (U_BASIC_LATIN);
      set_bit (U_CURRENCY_SYMBOLS);
      set_bit (U_BASIC_HEBREW);
      set_bit (U_HEBREW_EXTENDED);
      retval = TRUE;
    }
  if (check_cp (FS_ARABIC))
    {
      set_bit (U_BASIC_LATIN);
      set_bit (U_CURRENCY_SYMBOLS);
      set_bit (U_BASIC_ARABIC);
      set_bit (U_ARABIC_EXTENDED);
      retval = TRUE;
    }
  if (check_cp (FS_BALTIC))
    {
      set_bit (U_BASIC_LATIN);
      set_bit (U_LATIN_1_SUPPLEMENT);
      set_bit (U_CURRENCY_SYMBOLS);
      set_bit (U_LATIN_EXTENDED_A);
      set_bit (U_LATIN_EXTENDED_B);
      retval = TRUE;
    }
  if (check_cp (FS_VIETNAMESE))
    {
      /* ??? */
      set_bit (U_BASIC_LATIN);
      retval = TRUE;
    }
  if (check_cp (FS_THAI))
    {
      set_bit (U_BASIC_LATIN);
      set_bit (U_THAI);
      retval = TRUE;
    }
  if (check_cp (FS_JISJAPAN))
    {
      /* Based on MS Gothic */
      set_bit (U_BASIC_LATIN);
      set_bit (U_CJK_SYMBOLS_AND_PUNCTUATION);
      set_bit (U_HIRAGANA);
      set_bit (U_KATAKANA);
      set_bit (U_CJK_UNIFIED_IDEOGRAPHS);
      set_bit (U_HALFWIDTH_AND_FULLWIDTH_FORMS);
      retval = TRUE;
    }
  if (check_cp (FS_CHINESESIMP))
    {
      /* Based on MS Hei */
      set_bit (U_BASIC_LATIN);
      set_bit (U_HIRAGANA);
      set_bit (U_KATAKANA);
      set_bit (U_BOPOMOFO);
      set_bit (U_CJK_UNIFIED_IDEOGRAPHS);
      retval = TRUE;
    }
  if (check_cp (FS_WANSUNG)
      || check_cp (FS_JOHAB))	/* ??? */
    {
      /* Based on GulimChe. I wonder if all Korean fonts
       * really support this large range of Unicode subranges?
       */
      set_bit (U_BASIC_LATIN);
      set_bit (U_LATIN_1_SUPPLEMENT);
      set_bit (U_LATIN_EXTENDED_A);
      set_bit (U_SPACING_MODIFIER_LETTERS);
      set_bit (U_BASIC_GREEK);
      set_bit (U_CYRILLIC);
843
      set_bit (U_HANGUL_JAMO);
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
      set_bit (U_GENERAL_PUNCTUATION);
      set_bit (U_SUPERSCRIPTS_AND_SUBSCRIPTS);
      set_bit (U_CURRENCY_SYMBOLS);
      set_bit (U_LETTERLIKE_SYMBOLS);
      set_bit (U_NUMBER_FORMS);
      set_bit (U_ARROWS);
      set_bit (U_MATHEMATICAL_OPERATORS);
      set_bit (U_MISCELLANEOUS_TECHNICAL);
      set_bit (U_ENCLOSED_ALPHANUMERICS);
      set_bit (U_BOX_DRAWING);
      set_bit (U_BLOCK_ELEMENTS);
      set_bit (U_GEOMETRIC_SHAPES);
      set_bit (U_MISCELLANEOUS_SYMBOLS);
      set_bit (U_CJK_SYMBOLS_AND_PUNCTUATION);
      set_bit (U_HIRAGANA);
      set_bit (U_KATAKANA);
      set_bit (U_HANGUL_COMPATIBILITY_JAMO);
      set_bit (U_ENCLOSED_CJK);
      set_bit (U_CJK_COMPATIBILITY_FORMS);
      set_bit (U_HANGUL);
      set_bit (U_CJK_UNIFIED_IDEOGRAPHS);
      set_bit (U_CJK_COMPATIBILITY_IDEOGRAPHS);
      set_bit (U_HALFWIDTH_AND_FULLWIDTH_FORMS);
      retval = TRUE;
    }
  if (check_cp (FS_CHINESETRAD))
    {
      /* Based on MingLiU */
      set_bit (U_BASIC_LATIN);
      set_bit (U_GENERAL_PUNCTUATION);
      set_bit (U_BOX_DRAWING);
      set_bit (U_BLOCK_ELEMENTS);
      set_bit (U_CJK_SYMBOLS_AND_PUNCTUATION);
      set_bit (U_BOPOMOFO);
      set_bit (U_CJK_UNIFIED_IDEOGRAPHS);
      set_bit (U_CJK_COMPATIBILITY_IDEOGRAPHS);
      set_bit (U_SMALL_FORM_VARIANTS);
      set_bit (U_HALFWIDTH_AND_FULLWIDTH_FORMS);
      retval = TRUE;
    }
  if (check_cp (FS_SYMBOL) || charset == MAC_CHARSET)
    {
      /* Non-Unicode encoding, I guess. Pretend it covers
       * the single-byte range of values.
       */
      set_bit (U_BASIC_LATIN);
      set_bit (U_LATIN_1_SUPPLEMENT);
      retval = TRUE;
    }
893 894 895 896 897 898 899 900 901 902

  if (retval)
    return TRUE;

  GDK_NOTE (MISC, g_print ("... No code page bits set!\n"));

  /* Sigh. Not even any code page bits were set. Guess based on
   * charset, then. These somewhat optimistic guesses are based on the
   * table in Appendix M in the book "Developing ..."  mentioned
   * above.
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
   */
  switch (charset)
    {
    case ANSI_CHARSET:
      set_bit (U_BASIC_LATIN);
      set_bit (U_LATIN_1_SUPPLEMENT);
      set_bit (U_LATIN_EXTENDED_A);
      set_bit (U_LATIN_EXTENDED_B);
      set_bit (U_SPACING_MODIFIER_LETTERS);
      set_bit (U_COMBINING_DIACRITICAL_MARKS);
      set_bit (U_GENERAL_PUNCTUATION);
      set_bit (U_SUPERSCRIPTS_AND_SUBSCRIPTS);
      set_bit (U_CURRENCY_SYMBOLS);
#if 0 /* I find this too hard to believe... */
      set_bit (U_BASIC_GREEK);
      set_bit (U_CYRILLIC);
      set_bit (U_BASIC_HEBREW);
      set_bit (U_HEBREW_EXTENDED);
      set_bit (U_BASIC_ARABIC);
      set_bit (U_ARABIC_EXTENDED);
      set_bit (U_LETTERLIKE_SYMBOLS);
      set_bit (U_NUMBER_FORMS);
      set_bit (U_ARROWS);
      set_bit (U_MATHEMATICAL_OPERATORS);
      set_bit (U_MISCELLANEOUS_TECHNICAL);
      set_bit (U_ENCLOSED_ALPHANUMERICS);
      set_bit (U_BOX_DRAWING);
      set_bit (U_BLOCK_ELEMENTS);
      set_bit (U_GEOMETRIC_SHAPES);
      set_bit (U_MISCELLANEOUS_SYMBOLS);
      set_bit (U_HIRAGANA);
      set_bit (U_KATAKANA);
      set_bit (U_BOPOMOFO);
      set_bit (U_HANGUL_COMPATIBILITY_JAMO);
      set_bit (U_CJK_MISCELLANEOUS);
      set_bit (U_CJK_COMPATIBILITY);
      set_bit (U_HANGUL);
      set_bit (U_HANGUL_SUPPLEMENTARY_A);
      set_bit (U_CJK_COMPATIBILITY_IDEOGRAPHS);
      set_bit (U_ALPHABETIC_PRESENTATION_FORMS);
      set_bit (U_SMALL_FORM_VARIANTS);
      set_bit (U_ARABIC_PRESENTATION_FORMS_B);
      set_bit (U_HALFWIDTH_AND_FULLWIDTH_FORMS);
      set_bit (U_SPECIALS);
#endif
      retval = TRUE;
      break;
    case SYMBOL_CHARSET:
      /* Unggh */
      set_bit (U_BASIC_LATIN);
      set_bit (U_LATIN_1_SUPPLEMENT);
      retval = TRUE;
      break;
    case SHIFTJIS_CHARSET:
    case HANGEUL_CHARSET:
    case GB2312_CHARSET:
    case CHINESEBIG5_CHARSET:
    case JOHAB_CHARSET:
      /* The table really does claim these "locales" (it doesn't
       * talk about charsets per se) cover the same Unicode
       * subranges
       */
      set_bit (U_BASIC_LATIN);
      set_bit (U_LATIN_1_SUPPLEMENT);
      set_bit (U_LATIN_EXTENDED_A);
      set_bit (U_LATIN_EXTENDED_B);
      set_bit (U_SPACING_MODIFIER_LETTERS);
      set_bit (U_COMBINING_DIACRITICAL_MARKS_FOR_SYMBOLS);
      set_bit (U_BASIC_GREEK);
      set_bit (U_CYRILLIC);
973
      set_bit (U_HANGUL_JAMO);
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
      set_bit (U_GENERAL_PUNCTUATION);
      set_bit (U_SUPERSCRIPTS_AND_SUBSCRIPTS);
      set_bit (U_CURRENCY_SYMBOLS);
      set_bit (U_LETTERLIKE_SYMBOLS);
      set_bit (U_NUMBER_FORMS);
      set_bit (U_ARROWS);
      set_bit (U_MATHEMATICAL_OPERATORS);
      set_bit (U_MISCELLANEOUS_TECHNICAL);
      set_bit (U_ENCLOSED_ALPHANUMERICS);
      set_bit (U_BOX_DRAWING);
      set_bit (U_BLOCK_ELEMENTS);
      set_bit (U_GEOMETRIC_SHAPES);
      set_bit (U_MISCELLANEOUS_SYMBOLS);
      set_bit (U_CJK_SYMBOLS_AND_PUNCTUATION);
      set_bit (U_HIRAGANA);
      set_bit (U_KATAKANA);
      set_bit (U_BOPOMOFO);
      set_bit (U_HANGUL_COMPATIBILITY_JAMO);
      set_bit (U_CJK_MISCELLANEOUS);
      set_bit (U_CJK_COMPATIBILITY);
      set_bit (U_HANGUL);
      set_bit (U_HANGUL_SUPPLEMENTARY_A);
      set_bit (U_CJK_UNIFIED_IDEOGRAPHS);
      set_bit (U_CJK_COMPATIBILITY_IDEOGRAPHS);
      set_bit (U_ALPHABETIC_PRESENTATION_FORMS);
      set_bit (U_SMALL_FORM_VARIANTS);
      set_bit (U_ARABIC_PRESENTATION_FORMS_B);
      set_bit (U_SPECIALS);
      retval = TRUE;
      break;
    case HEBREW_CHARSET:
      set_bit (U_BASIC_LATIN);
      set_bit (U_LATIN_1_SUPPLEMENT);
      set_bit (U_LATIN_EXTENDED_B);
      set_bit (U_SPACING_MODIFIER_LETTERS);
      set_bit (U_BASIC_HEBREW);
      set_bit (U_HEBREW_EXTENDED);
      set_bit (U_GENERAL_PUNCTUATION);
      set_bit (U_LETTERLIKE_SYMBOLS);
      retval = TRUE; 
      break;
    case ARABIC_CHARSET:
      set_bit (U_BASIC_LATIN);
      set_bit (U_LATIN_1_SUPPLEMENT);
      set_bit (U_LATIN_EXTENDED_A);
      set_bit (U_LATIN_EXTENDED_B);
      set_bit (U_SPACING_MODIFIER_LETTERS);
      set_bit (U_BASIC_GREEK);
      set_bit (U_BASIC_ARABIC);
      set_bit (U_ARABIC_EXTENDED);
      set_bit (U_GENERAL_PUNCTUATION);
      set_bit (U_LETTERLIKE_SYMBOLS);
      set_bit (U_ARROWS);
      set_bit (U_MATHEMATICAL_OPERATORS);
      set_bit (U_MISCELLANEOUS_TECHNICAL);
      set_bit (U_BOX_DRAWING);
      set_bit (U_BLOCK_ELEMENTS);
      set_bit (U_GEOMETRIC_SHAPES);
      set_bit (U_MISCELLANEOUS_SYMBOLS);
      set_bit (U_HALFWIDTH_AND_FULLWIDTH_FORMS);
      retval = TRUE;
      break;
    case GREEK_CHARSET:
      set_bit (U_BASIC_LATIN);
      set_bit (U_LATIN_1_SUPPLEMENT);
      set_bit (U_LATIN_EXTENDED_B);
      set_bit (U_BASIC_GREEK);
      set_bit (U_GENERAL_PUNCTUATION);
      set_bit (U_SUPERSCRIPTS_AND_SUBSCRIPTS);
      set_bit (U_LETTERLIKE_SYMBOLS);
      set_bit (U_ARROWS);
      set_bit (U_MATHEMATICAL_OPERATORS);
      set_bit (U_MISCELLANEOUS_TECHNICAL);
      set_bit (U_BOX_DRAWING);
      set_bit (U_BLOCK_ELEMENTS);
      set_bit (U_GEOMETRIC_SHAPES);
      set_bit (U_MISCELLANEOUS_SYMBOLS);
      retval = TRUE;
      break;
    case TURKISH_CHARSET:
      set_bit (U_BASIC_LATIN);
      set_bit (U_LATIN_1_SUPPLEMENT);
      set_bit (U_LATIN_EXTENDED_A);
      set_bit (U_LATIN_EXTENDED_B);
      set_bit (U_SPACING_MODIFIER_LETTERS);
      set_bit (U_BASIC_GREEK);
      set_bit (U_GENERAL_PUNCTUATION);
      set_bit (U_SUPERSCRIPTS_AND_SUBSCRIPTS);
      set_bit (U_CURRENCY_SYMBOLS);
      set_bit (U_LETTERLIKE_SYMBOLS);
      set_bit (U_ARROWS);
      set_bit (U_MATHEMATICAL_OPERATORS);
      set_bit (U_MISCELLANEOUS_TECHNICAL);
      set_bit (U_BOX_DRAWING);
      set_bit (U_BLOCK_ELEMENTS);
      set_bit (U_GEOMETRIC_SHAPES);
      set_bit (U_MISCELLANEOUS_SYMBOLS);
      retval = TRUE;
      break;
    case VIETNAMESE_CHARSET:
    case THAI_CHARSET:
      /* These are not in the table, so I have no idea */
      break;
    case BALTIC_CHARSET:
      set_bit (U_BASIC_LATIN);
      set_bit (U_LATIN_1_SUPPLEMENT);
      set_bit (U_LATIN_EXTENDED_A);
      set_bit (U_LATIN_EXTENDED_B);
      set_bit (U_SPACING_MODIFIER_LETTERS);
      set_bit (U_BASIC_GREEK);
      set_bit (U_GENERAL_PUNCTUATION);
      set_bit (U_SUPERSCRIPTS_AND_SUBSCRIPTS);
      set_bit (U_CURRENCY_SYMBOLS);
      set_bit (U_LETTERLIKE_SYMBOLS);
      set_bit (U_ARROWS);
      set_bit (U_MATHEMATICAL_OPERATORS);
      set_bit (U_MISCELLANEOUS_TECHNICAL);
      set_bit (U_BOX_DRAWING);
      set_bit (U_BLOCK_ELEMENTS);
      set_bit (U_GEOMETRIC_SHAPES);
      set_bit (U_MISCELLANEOUS_SYMBOLS);
      retval = TRUE;
      break;
    case EASTEUROPE_CHARSET:
      set_bit (U_BASIC_LATIN);
      set_bit (U_LATIN_1_SUPPLEMENT);
      set_bit (U_LATIN_EXTENDED_A);
      set_bit (U_LATIN_EXTENDED_B);
      set_bit (U_SPACING_MODIFIER_LETTERS);
      set_bit (U_BASIC_GREEK);
      set_bit (U_GENERAL_PUNCTUATION);
      set_bit (U_SUPERSCRIPTS_AND_SUBSCRIPTS);
      set_bit (U_CURRENCY_SYMBOLS);
      set_bit (U_LETTERLIKE_SYMBOLS);
      set_bit (U_ARROWS);
      set_bit (U_MATHEMATICAL_OPERATORS);
      set_bit (U_MISCELLANEOUS_TECHNICAL);
      set_bit (U_BOX_DRAWING);
      set_bit (U_BLOCK_ELEMENTS);
      set_bit (U_GEOMETRIC_SHAPES);
      set_bit (U_MISCELLANEOUS_SYMBOLS);
      retval = TRUE;
      break;
    case RUSSIAN_CHARSET:
      set_bit (U_BASIC_LATIN);
      set_bit (U_LATIN_1_SUPPLEMENT);
      set_bit (U_CYRILLIC);
      set_bit (U_GENERAL_PUNCTUATION);
      set_bit (U_LETTERLIKE_SYMBOLS);
      set_bit (U_ARROWS);
      set_bit (U_MATHEMATICAL_OPERATORS);
      set_bit (U_MISCELLANEOUS_TECHNICAL);
      set_bit (U_BOX_DRAWING);
      set_bit (U_BLOCK_ELEMENTS);
      set_bit (U_GEOMETRIC_SHAPES);
      set_bit (U_MISCELLANEOUS_SYMBOLS);
      retval = TRUE;
      break;
    }

#undef set_bit
  return retval;
}

Tor Lillqvist's avatar
Tor Lillqvist committed
1138 1139
static GdkWin32SingleFont *
gdk_font_load_logfont (LOGFONT *lfp)
1140 1141 1142 1143 1144
{
  GdkWin32SingleFont *singlefont;
  HFONT hfont;
  LOGFONT logfont;
  CHARSETINFO csi;
Tor Lillqvist's avatar
Tor Lillqvist committed
1145
  HGDIOBJ oldfont;
Tor Lillqvist's avatar
Tor Lillqvist committed
1146
  int tries;
1147
  gchar face[100];
1148

Tor Lillqvist's avatar
Tor Lillqvist committed
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
  for (tries = 0; ; tries++)
    {
      GDK_NOTE (MISC, g_print ("... trying %d,%d,%d,%d,"
			       "%d,%d,%d,%d,"
			       "%d,%d,%d,"
			       "%d,%#.02x,\"%s\"\n",
			       lfp->lfHeight, lfp->lfWidth,
			       lfp->lfEscapement, lfp->lfOrientation,
			       lfp->lfWeight, lfp->lfItalic,
			       lfp->lfUnderline, lfp->lfStrikeOut,
			       lfp->lfCharSet,
			       lfp->lfOutPrecision, lfp->lfClipPrecision,
			       lfp->lfQuality, lfp->lfPitchAndFamily,
			       lfp->lfFaceName));
      hfont = CreateFontIndirect (lfp);

      if (hfont != NULL)
	break;
      
      /* If we fail, try some similar fonts often found on Windows. */
      if (tries == 0)
	{
	  if (g_strcasecmp (lfp->lfFaceName, "helvetica") == 0)
	    strcpy (lfp->lfFaceName, "arial");
	  else if (g_strcasecmp (lfp->lfFaceName, "new century schoolbook") == 0)
	    strcpy (lfp->lfFaceName, "century schoolbook");
	  else if (g_strcasecmp (lfp->lfFaceName, "courier") == 0)
	    strcpy (lfp->lfFaceName, "courier new");
	  else if (g_strcasecmp (lfp->lfFaceName, "lucida") == 0)
	    strcpy (lfp->lfFaceName, "lucida sans unicode");
	  else if (g_strcasecmp (lfp->lfFaceName, "lucidatypewriter") == 0)
	    strcpy (lfp->lfFaceName, "lucida console");
	  else if (g_strcasecmp (lfp->lfFaceName, "times") == 0)
	    strcpy (lfp->lfFaceName, "times new roman");
	}
      else if (tries == 1)
	{
	  if (g_strcasecmp (lfp->lfFaceName, "courier") == 0)
	    {
	      strcpy (lfp->lfFaceName, "");
	      lfp->lfPitchAndFamily |= FF_MODERN;
	    }
	  else if (g_strcasecmp (lfp->lfFaceName, "times new roman") == 0)
	    {
	      strcpy (lfp->lfFaceName, "");
	      lfp->lfPitchAndFamily |= FF_ROMAN;
	    }
	  else if (g_strcasecmp (lfp->lfFaceName, "helvetica") == 0
		   || g_strcasecmp (lfp->lfFaceName, "lucida") == 0)
	    {
	      strcpy (lfp->lfFaceName, "");
	      lfp->lfPitchAndFamily |= FF_SWISS;
	    }
	  else
	    {
	      strcpy (lfp->lfFaceName, "");
	      lfp->lfPitchAndFamily = (lfp->lfPitchAndFamily & 0x0F) | FF_DONTCARE;
	    }
	}
      else
	break;
      tries++;
    }

  if (!hfont)
    return NULL;
      
  singlefont = g_new (GdkWin32SingleFont, 1);
  singlefont->hfont = hfont;
  GetObject (singlefont->hfont, sizeof (logfont), &logfont);
  oldfont = SelectObject (gdk_display_hdc, singlefont->hfont);
  memset (&singlefont->fs, 0, sizeof (singlefont->fs));
  singlefont->charset = GetTextCharsetInfo (gdk_display_hdc, &singlefont->fs, 0);
  GetTextFace (gdk_display_hdc, sizeof (face), face);
  SelectObject (gdk_display_hdc, oldfont);
  if (TranslateCharsetInfo ((DWORD *) singlefont->charset, &csi,
			    TCI_SRCCHARSET)
      && singlefont->charset != MAC_CHARSET)
    singlefont->codepage = csi.ciACP;
  else
    singlefont->codepage = 0;

  GDK_NOTE (MISC, (g_print ("... = %#x %s cs %s cp%d\n",
			    singlefont->hfont, face,
			    charset_name (singlefont->charset),
			    singlefont->codepage),
		   g_print ("... Unicode subranges:"),
		   print_unicode_subranges (&singlefont->fs)));
  if (check_unicode_subranges (singlefont->charset, &singlefont->fs))
    GDK_NOTE (MISC, (g_print ("... Guesstimated Unicode subranges:"),
		     print_unicode_subranges (&singlefont->fs)));

  return singlefont;
}

static GdkWin32SingleFont *
gdk_font_load_internal (const gchar *font_name)
{
  GdkWin32SingleFont *singlefont;

  LOGFONT logfont;

  char *fn;
  int numfields, n1, n2;
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
  char foundry[32], family[100], weight[32], slant[32], set_width[32],
    spacing[32], registry[32], encoding[32];
  char pixel_size[10], point_size[10], res_x[10], res_y[10], avg_width[10];
  int c;
  char *p;
  int logpixelsy;

  g_return_val_if_fail (font_name != NULL, NULL);

  GDK_NOTE (MISC, g_print ("gdk_font_load_internal: %s\n", font_name));

  numfields = sscanf (font_name,
		      "-%30[^-]-%100[^-]-%30[^-]-%30[^-]-%30[^-]-%n",
		      foundry,
		      family,
		      weight,
		      slant,
		      set_width,
		      &n1);
  if (numfields == 0)
    {
      /* Probably a plain Windows font name */
Tor Lillqvist's avatar
Tor Lillqvist committed
1275 1276 1277 1278 1279 1280 1281 1282 1283
      logfont.lfHeight = 0;
      logfont.lfWidth = 0;
      logfont.lfEscapement = 0;
      logfont.lfOrientation = 0;
      logfont.lfWeight = FW_DONTCARE;
      logfont.lfItalic = FALSE;
      logfont.lfUnderline = FALSE;
      logfont.lfStrikeOut = FALSE;
      logfont.lfCharSet = ANSI_CHARSET;
1284
      logfont.lfOutPrecision = OUT_TT_ONLY_PRECIS;
Tor Lillqvist's avatar
Tor Lillqvist committed
1285 1286 1287 1288 1289 1290
      logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
      logfont.lfQuality = PROOF_QUALITY;
      logfont.lfPitchAndFamily = DEFAULT_PITCH;
      fn = g_filename_from_utf8 (font_name);
      strcpy (logfont.lfFaceName, fn);
      g_free (fn);
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
    }
  else if (numfields != 5)
    {
      g_warning ("gdk_font_load: font name %s illegal", font_name);
      return NULL;
    }
  else
    {
      /* It must be a XLFD name */

      /* Check for hex escapes in the font family,
1302
       * put in there by logfont_to_xlfd. Convert them in-place.
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
       */
      p = family;
      while (*p)
	{
	  if (*p == '%' && isxdigit (p[1]) && isxdigit (p[2]))
	    {
	      sscanf (p+1, "%2x", &c);
	      *p = c;
	      strcpy (p+1, p+3);
	    }
	  p++;
	}

      /* Skip add_style which often is empty in the requested font name */
      while (font_name[n1] && font_name[n1] != '-')
	n1++;
      numfields++;

      numfields += sscanf (font_name + n1,
			   "-%8[^-]-%8[^-]-%8[^-]-%8[^-]-%30[^-]-%8[^-]-%30[^-]-%30[^-]%n",
			   pixel_size,
			   point_size,
			   res_x,
			   res_y,
			   spacing,
			   avg_width,
			   registry,
			   encoding,
			   &n2);

      if (numfields != 14 || font_name[n1 + n2] != '\0')
	{
	  g_warning ("gdk_font_load: font name %s illegal", font_name);
	  return NULL;
	}

1339
      logpixelsy = GetDeviceCaps (gdk_display_hdc, LOGPIXELSY);
1340 1341 1342

      if (strcmp (pixel_size, "*") == 0)
	if (strcmp (point_size, "*") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1343
	  logfont.lfHeight = 0;
1344
	else
Tor Lillqvist's avatar
Tor Lillqvist committed
1345
	  logfont.lfHeight = (int) (((double) atoi (point_size))/720.*logpixelsy);
1346
      else
Tor Lillqvist's avatar
Tor Lillqvist committed
1347
	logfont.lfHeight = atoi (pixel_size);
1348

Tor Lillqvist's avatar
Tor Lillqvist committed
1349 1350 1351
      logfont.lfWidth = 0;
      logfont.lfEscapement = 0;
      logfont.lfOrientation = 0;
1352 1353

      if (g_strcasecmp (weight, "thin") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1354
	logfont.lfWeight = FW_THIN;
1355
      else if (g_strcasecmp (weight, "extralight") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1356
	logfont.lfWeight = FW_EXTRALIGHT;
1357 1358
      else if (g_strcasecmp (weight, "ultralight") == 0)
#ifdef FW_ULTRALIGHT
Tor Lillqvist's avatar
Tor Lillqvist committed
1359
	logfont.lfWeight = FW_ULTRALIGHT;
1360
#else
Tor Lillqvist's avatar
Tor Lillqvist committed
1361 1362 1363
	logfont.lfWeight = FW_EXTRALIGHT; /* In fact, FW_ULTRALIGHT really is 
					   * defined as FW_EXTRALIGHT anyway.
					   */
1364 1365
#endif
      else if (g_strcasecmp (weight, "light") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1366
	logfont.lfWeight = FW_LIGHT;
1367
      else if (g_strcasecmp (weight, "normal") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1368
	logfont.lfWeight = FW_NORMAL;
1369
      else if (g_strcasecmp (weight, "regular") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1370
	logfont.lfWeight = FW_REGULAR;
1371
      else if (g_strcasecmp (weight, "medium") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1372
	logfont.lfWeight = FW_MEDIUM;
1373
      else if (g_strcasecmp (weight, "semibold") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1374
	logfont.lfWeight = FW_SEMIBOLD;
1375 1376
      else if (g_strcasecmp (weight, "demibold") == 0)
#ifdef FW_DEMIBOLD
Tor Lillqvist's avatar
Tor Lillqvist committed
1377
	logfont.lfWeight = FW_DEMIBOLD;
1378
#else
Tor Lillqvist's avatar
Tor Lillqvist committed
1379
	logfont.lfWeight = FW_SEMIBOLD;	/* As above */
1380 1381
#endif
      else if (g_strcasecmp (weight, "bold") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1382
	logfont.lfWeight = FW_BOLD;
1383
      else if (g_strcasecmp (weight, "extrabold") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1384
	logfont.lfWeight = FW_EXTRABOLD;
1385 1386
      else if (g_strcasecmp (weight, "ultrabold") == 0)
#ifdef FW_ULTRABOLD
Tor Lillqvist's avatar
Tor Lillqvist committed
1387
	logfont.lfWeight = FW_ULTRABOLD;
1388
#else
Tor Lillqvist's avatar
Tor Lillqvist committed
1389
	logfont.lfWeight = FW_EXTRABOLD; /* As above */
1390 1391
#endif
      else if (g_strcasecmp (weight, "heavy") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1392
	logfont.lfWeight = FW_HEAVY;
1393 1394
      else if (g_strcasecmp (weight, "black") == 0)
#ifdef FW_BLACK
Tor Lillqvist's avatar
Tor Lillqvist committed
1395
	logfont.lfWeight = FW_BLACK;
1396
#else
Tor Lillqvist's avatar
Tor Lillqvist committed
1397
	logfont.lfWeight = FW_HEAVY;	/* As above */
1398 1399
#endif
      else
Tor Lillqvist's avatar
Tor Lillqvist committed
1400
	logfont.lfWeight = FW_DONTCARE;
1401 1402 1403 1404 1405

      if (g_strcasecmp (slant, "italic") == 0
	  || g_strcasecmp (slant, "oblique") == 0
	  || g_strcasecmp (slant, "i") == 0
	  || g_strcasecmp (slant, "o") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1406
	logfont.lfItalic = TRUE;
1407
      else
Tor Lillqvist's avatar
Tor Lillqvist committed
1408 1409 1410
	logfont.lfItalic = FALSE;
      logfont.lfUnderline = FALSE;
      logfont.lfStrikeOut = FALSE;
1411 1412
      if (g_strcasecmp (registry, "iso8859") == 0)
	if (strcmp (encoding, "1") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1413
	  logfont.lfCharSet = ANSI_CHARSET;
1414
	else if (strcmp (encoding, "2") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1415
	  logfont.lfCharSet = EASTEUROPE_CHARSET;
1416
	else if (strcmp (encoding, "7") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1417
	  logfont.lfCharSet = GREEK_CHARSET;
1418
	else if (strcmp (encoding, "8") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1419
	  logfont.lfCharSet = HEBREW_CHARSET;
1420
	else if (strcmp (encoding, "9") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1421
	  logfont.lfCharSet = TURKISH_CHARSET;
1422
	else
Tor Lillqvist's avatar
Tor Lillqvist committed
1423
	  logfont.lfCharSet = ANSI_CHARSET; /* XXX ??? */
1424
      else if (g_strcasecmp (registry, "jisx0208.1983") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1425
	logfont.lfCharSet = SHIFTJIS_CHARSET;
1426
      else if (g_strcasecmp (registry, "ksc5601.1987") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1427
	logfont.lfCharSet = HANGEUL_CHARSET;
1428
      else if (g_strcasecmp (registry, "gb2312.1980") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1429
	logfont.lfCharSet = GB2312_CHARSET;
1430
      else if (g_strcasecmp (registry, "big5") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1431
	logfont.lfCharSet = CHINESEBIG5_CHARSET;
1432 1433 1434
      else if (g_strcasecmp (registry, "windows") == 0
	       || g_strcasecmp (registry, "microsoft") == 0)
	if (g_strcasecmp (encoding, "symbol") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1435
	  logfont.lfCharSet = SYMBOL_CHARSET;
1436
	else if (g_strcasecmp (encoding, "shiftjis") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1437
	  logfont.lfCharSet = SHIFTJIS_CHARSET;
1438
	else if (g_strcasecmp (encoding, "gb2312") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1439
	  logfont.lfCharSet = GB2312_CHARSET;
1440
	else if (g_strcasecmp (encoding, "hangeul") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1441
	  logfont.lfCharSet = HANGEUL_CHARSET;
1442
	else if (g_strcasecmp (encoding, "big5") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1443
	  logfont.lfCharSet = CHINESEBIG5_CHARSET;
1444
	else if (g_strcasecmp (encoding, "johab") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1445
	  logfont.lfCharSet = JOHAB_CHARSET;
1446
	else if (g_strcasecmp (encoding, "hebrew") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1447
	  logfont.lfCharSet = HEBREW_CHARSET;
1448
	else if (g_strcasecmp (encoding, "arabic") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1449
	  logfont.lfCharSet = ARABIC_CHARSET;
1450
	else if (g_strcasecmp (encoding, "greek") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1451
	  logfont.lfCharSet = GREEK_CHARSET;
1452
	else if (g_strcasecmp (encoding, "turkish") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1453
	  logfont.lfCharSet = TURKISH_CHARSET;
1454
	else if (g_strcasecmp (encoding, "easteurope") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1455
	  logfont.lfCharSet = EASTEUROPE_CHARSET;
1456
	else if (g_strcasecmp (encoding, "russian") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1457
	  logfont.lfCharSet = RUSSIAN_CHARSET;
1458
	else if (g_strcasecmp (encoding, "mac") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1459
	  logfont.lfCharSet = MAC_CHARSET;
1460
	else if (g_strcasecmp (encoding, "baltic") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1461
	  logfont.lfCharSet = BALTIC_CHARSET;
1462
	else if (g_strcasecmp (encoding, "cp1251") == 0)
Tor Lillqvist's avatar
Tor Lillqvist committed
1463
	  logfont.lfCharSet = RUSSIAN_CHARSET;
1464
	else
Tor Lillqvist's avatar
Tor Lillqvist committed
1465
	  logfont.lfCharSet = ANSI_CHARSET; /* XXX ??? */
1466
      else
Tor Lillqvist's avatar
Tor Lillqvist committed
1467 1468 1469 1470
	logfont.lfCharSet = ANSI_CHARSET; /* XXX ??? */
      logfont.lfOutPrecision = OUT_TT_PRECIS;
      logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
      logfont.lfQuality = PROOF_QUALITY;
1471
      if (g_strcasecmp (spacing, "m") == 0)