fcfreetype.c 12.6 KB
Newer Older
Keith Packard's avatar
Keith Packard committed
1
/*
2
 * $XFree86: xc/lib/fontconfig/src/fcfreetype.c,v 1.9 2002/07/13 05:43:25 keithp Exp $
Keith Packard's avatar
Keith Packard committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 * Copyright  2001 Keith Packard, member of The XFree86 Project, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Keith Packard not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Keith Packard makes no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "fcint.h"
#include <freetype/freetype.h>
#include <freetype/internal/ftobjs.h>
#include <freetype/tttables.h>
32 33
#include <freetype/ftsnames.h>
#include <freetype/ttnameid.h>
Keith Packard's avatar
Keith Packard committed
34

35
/*
36 37
 * Keep Han languages separated by eliminating languages
 * that the codePageRange bits says aren't supported
38
 */
39

Keith Packard's avatar
Keith Packard committed
40
static const struct {
41
    int		    bit;
42
    const FcChar8   *lang;
Keith Packard's avatar
Keith Packard committed
43
} FcCodePageRange[] = {
44
    { 17,	(const FcChar8 *) "ja" },
45
    { 18,	(const FcChar8 *) "zh-cn" },
46
    { 19,	(const FcChar8 *) "ko" },
47
    { 20,	(const FcChar8 *) "zh-tw" },
Keith Packard's avatar
Keith Packard committed
48 49 50 51
};

#define NUM_CODE_PAGE_RANGE (sizeof FcCodePageRange / sizeof FcCodePageRange[0])

52
FcBool
53
FcFreeTypeIsExclusiveLang (const FcChar8  *lang)
54
{
55
    int	    i;
56

57 58 59
    for (i = 0; i < NUM_CODE_PAGE_RANGE; i++)
    {
	if (FcLangCompare (lang, FcCodePageRange[i].lang) != FcLangDifferentLang)
60
	    return FcTrue;
61
    }
62 63
    return FcFalse;
}
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
#define FC_NAME_PRIO_LANG	    0x0f00
#define FC_NAME_PRIO_LANG_ENGLISH   0x0200
#define FC_NAME_PRIO_LANG_LATIN	    0x0100
#define FC_NAME_PRIO_LANG_NONE	    0x0000

#define FC_NAME_PRIO_ENC	    0x00f0
#define FC_NAME_PRIO_ENC_UNICODE    0x0010
#define FC_NAME_PRIO_ENC_NONE	    0x0000

#define FC_NAME_PRIO_NAME	    0x000f
#define FC_NAME_PRIO_NAME_FAMILY    0x0002
#define FC_NAME_PRIO_NAME_PS	    0x0001
#define FC_NAME_PRIO_NAME_NONE	    0x0000

static FcBool
FcUcs4IsLatin (FcChar32 ucs4)
{
    FcChar32	page = ucs4 >> 8;
    
    if (page <= 2)
	return FcTrue;
    if (page == 0x1e)
	return FcTrue;
    if (0x20 <= page && page <= 0x23)
	return FcTrue;
    if (page == 0xfb)
	return FcTrue;
    if (page == 0xff)
	return FcTrue;
    return FcFalse;
}

static FcBool
FcUtf8IsLatin (FcChar8 *str, int len)
{
    while (len)
    {
	FcChar32    ucs4;
	int	    clen = FcUtf8ToUcs4 (str, &ucs4, len);
	if (clen <= 0)
	    return FcFalse;
	if (!FcUcs4IsLatin (ucs4))
	    return FcFalse;
	len -= clen;
	str += clen;
    }
    return FcTrue;
}

Keith Packard's avatar
Keith Packard committed
114
FcPattern *
115 116 117 118
FcFreeTypeQuery (const FcChar8	*file,
		 int		id,
		 FcBlanks	*blanks,
		 int		*count)
Keith Packard's avatar
Keith Packard committed
119
{
120 121 122 123 124 125
    FT_Face	    face;
    FcPattern	    *pat;
    int		    slant;
    int		    weight;
    int		    i;
    FcCharSet	    *cs;
126
    FcLangSet	    *ls;
127
    FT_Library	    ftLibrary;
128 129
    FcChar8	    *family;
    FcChar8	    *style;
130
    TT_OS2	    *os2;
131
    const FcChar8   *exclusiveLang = 0;
132 133 134 135 136 137
    FT_SfntName	    sname;
    FT_UInt    	    snamei, snamec;
    FcBool	    family_allocated = FcFalse;
    FcBool	    style_allocated = FcFalse;
    int		    family_prio = 0;
    int		    style_prio = 0;
Keith Packard's avatar
Keith Packard committed
138 139 140 141

    if (FT_Init_FreeType (&ftLibrary))
	return 0;
    
142
    if (FT_New_Face (ftLibrary, (char *) file, id, &face))
Keith Packard's avatar
Keith Packard committed
143 144 145 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
	goto bail;

    *count = face->num_faces;

    pat = FcPatternCreate ();
    if (!pat)
	goto bail0;

    if (!FcPatternAddBool (pat, FC_OUTLINE,
			   (face->face_flags & FT_FACE_FLAG_SCALABLE) != 0))
	goto bail1;

    if (!FcPatternAddBool (pat, FC_SCALABLE,
			   (face->face_flags & FT_FACE_FLAG_SCALABLE) != 0))
	goto bail1;


    slant = FC_SLANT_ROMAN;
    if (face->style_flags & FT_STYLE_FLAG_ITALIC)
	slant = FC_SLANT_ITALIC;

    if (!FcPatternAddInteger (pat, FC_SLANT, slant))
	goto bail1;

    weight = FC_WEIGHT_MEDIUM;
    if (face->style_flags & FT_STYLE_FLAG_BOLD)
	weight = FC_WEIGHT_BOLD;

    if (!FcPatternAddInteger (pat, FC_WEIGHT, weight))
	goto bail1;

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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 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 363 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 400 401 402 403 404 405 406 407 408 409 410 411 412
    /*
     * Grub through the name table looking for family
     * and style names.  FreeType makes quite a hash
     * of them
     */
    family = 0;
    style = 0;
    snamec = FT_Get_Sfnt_Name_Count (face);
    for (snamei = 0; snamei < snamec; snamei++)
    {
	FcChar8		*utf8;
	int		len;
	int		wchar;
	FcChar8		*src;
	int		src_len;
	FcChar8		*u8;
	FcChar32	ucs4;
	int		ilen, olen;
	int		prio = 0;
	
	const FcCharMap	*map;
	enum {
	    FcNameEncodingUtf16, 
	    FcNameEncodingAppleRoman,
	    FcNameEncodingLatin1 
	} encoding;
	
	
	if (FT_Get_Sfnt_Name (face, snamei, &sname) != 0)
	    break;
	
	/*
	 * Look for Unicode strings
	 */
	switch (sname.platform_id) {
	case TT_PLATFORM_APPLE_UNICODE:
	    /*
	     * All APPLE_UNICODE encodings are Utf16 BE
	     *
	     * Because there's no language id for Unicode,
	     * assume it's English
	     */
	    prio |= FC_NAME_PRIO_LANG_ENGLISH;
	    prio |= FC_NAME_PRIO_ENC_UNICODE;
	    encoding = FcNameEncodingUtf16;
	    break;
	case TT_PLATFORM_MACINTOSH:
	    switch (sname.encoding_id) {
	    case TT_MAC_ID_ROMAN:
		encoding = FcNameEncodingAppleRoman;
		break;
	    default:
		continue;
	    }
	    switch (sname.language_id) {
	    case TT_MAC_LANGID_ENGLISH:
		prio |= FC_NAME_PRIO_LANG_ENGLISH;
		break;
	    default:
		/*
		 * Sometimes Microsoft language ids
		 * end up in the macintosh table.  This
		 * is often accompanied by data in
		 * some mystic encoding.  Ignore these names
		 */
		if (sname.language_id >= 0x100)
		    continue;
		break;
	    }
	    break;
	case TT_PLATFORM_MICROSOFT:
	    switch (sname.encoding_id) {
	    case TT_MS_ID_UNICODE_CS:
		encoding = FcNameEncodingUtf16;
		prio |= FC_NAME_PRIO_ENC_UNICODE;
		break;
	    default:
		continue;
	    }
	    switch (sname.language_id & 0xff) {
	    case 0x09:
		prio |= FC_NAME_PRIO_LANG_ENGLISH;
		break;
	    default:
		break;
	    }
	    break;
	case TT_PLATFORM_ISO:
	    switch (sname.encoding_id) {
	    case TT_ISO_ID_10646:
		encoding = FcNameEncodingUtf16;
		prio |= FC_NAME_PRIO_ENC_UNICODE;
		break;
	    case TT_ISO_ID_7BIT_ASCII:
	    case TT_ISO_ID_8859_1:
		encoding = FcNameEncodingLatin1;
		break;
	    default:
		continue;
	    }
	    break;
	default:
	    continue;
	}
	
	/*
	 * Look for family and style names 
	 */
	switch (sname.name_id) {
	case TT_NAME_ID_FONT_FAMILY:
	    prio |= FC_NAME_PRIO_NAME_FAMILY;
	    break;
	case TT_NAME_ID_PS_NAME:
	    prio |= FC_NAME_PRIO_NAME_PS;
	    break;
	case TT_NAME_ID_FONT_SUBFAMILY:
	    break;
	default:
	    continue;
	}
	    
        src = (FcChar8 *) sname.string;
        src_len = sname.string_len;
	
	switch (encoding) {
	case FcNameEncodingUtf16:
	    /*
	     * Convert Utf16 to Utf8
	     */
	    
	    if (!FcUtf16Len (src, FcEndianBig, src_len, &len, &wchar))
		continue;
    
	    /*
	     * Allocate plenty of space
	     */
	    utf8 = malloc (len * FC_UTF8_MAX_LEN + 1);
	    if (!utf8)
		continue;
		
	    u8 = utf8;
	    
	    while ((ilen = FcUtf16ToUcs4 (src, FcEndianBig, &ucs4, src_len)) > 0)
	    {
		src_len -= ilen;
		src += ilen;
		olen = FcUcs4ToUtf8 (ucs4, u8);
		u8 += olen;
	    }
	    *u8 = '\0';
	    break;
	case FcNameEncodingLatin1:
	    /*
	     * Convert Latin1 to Utf8
	     */
	    utf8 = malloc (src_len * 2 + 1);
	    if (!utf8)
		continue;

	    u8 = utf8;
	    while (src_len > 0)
	    {
		ucs4 = *src++;
		src_len--;
		olen = FcUcs4ToUtf8 (ucs4, u8);
		u8 += olen;
	    }
	    *u8 = '\0';
	    break;
	case FcNameEncodingAppleRoman:
	    /*
	     * Convert AppleRoman to Utf8
	     */
	    map = FcFreeTypeGetPrivateMap (ft_encoding_apple_roman);
	    if (!map)
		continue;

	    utf8 = malloc (src_len * 3 + 1);
	    if (!utf8)
		continue;

	    u8 = utf8;
	    while (src_len > 0)
	    {
		ucs4 = FcFreeTypePrivateToUcs4 (*src++, map);
		src_len--;
		olen = FcUcs4ToUtf8 (ucs4, u8);
		u8 += olen;
	    }
	    *u8 = '\0';
	    break;
	default:
	    continue;
	}
	if ((prio & FC_NAME_PRIO_LANG) == FC_NAME_PRIO_LANG_NONE)
	    if (FcUtf8IsLatin (utf8, strlen ((char *) utf8)))
		prio |= FC_NAME_PRIO_LANG_LATIN;
			       
	if (FcDebug () & FC_DBG_SCANV)
	    printf ("\nfound name (name %d platform %d encoding %d language 0x%x prio 0x%x) %s\n",
		    sname.name_id, sname.platform_id,
		    sname.encoding_id, sname.language_id,
		    prio, utf8);
    
	switch (sname.name_id) {
	case TT_NAME_ID_FONT_FAMILY:
	case TT_NAME_ID_PS_NAME:
	    if (!family || prio > family_prio)
	    {
		if (family)
		    free (family);
		family = utf8;
		utf8 = 0;
		family_allocated = FcTrue;
		family_prio = prio;
	    }
	    break;
	case TT_NAME_ID_FONT_SUBFAMILY:
	    if (!style || prio > style_prio)
	    {
		if (style)
		    free (style);
		style = utf8;
		utf8 = 0;
		style_allocated = FcTrue;
		style_prio = prio;
	    }
	    break;
	}
	if (utf8)
	    free (utf8);
    }
    
    if (!family)
	family = (FcChar8 *) face->family_name;
    
    if (!style)
	style = (FcChar8 *) face->style_name;
    
Keith Packard's avatar
Keith Packard committed
413 414
    if (!family)
    {
415 416 417 418 419
	FcChar8	*start, *end;
	
	start = (FcChar8 *) strrchr ((char *) file, '/');
	if (start)
	    start++;
Keith Packard's avatar
Keith Packard committed
420
	else
421 422 423 424 425 426 427 428
	    start = (FcChar8 *) file;
	end = (FcChar8 *) strrchr ((char *) start, '.');
	if (!end)
	    end = start + strlen ((char *) start);
	family = malloc (end - start + 1);
	strncpy ((char *) family, (char *) start, end - start);
	family[end - start] = '\0';
	family_allocated = FcTrue;
Keith Packard's avatar
Keith Packard committed
429
    }
430 431 432 433

    if (FcDebug() & FC_DBG_SCAN)
	printf ("\"%s\" \"%s\" ", family, style ? style : (FcChar8 *) "<none>");

Keith Packard's avatar
Keith Packard committed
434
    if (!FcPatternAddString (pat, FC_FAMILY, family))
435 436 437 438 439
    {
	if (family_allocated)
	    free (family);
	if (style_allocated)
	    free (style);
Keith Packard's avatar
Keith Packard committed
440
	goto bail1;
441 442 443 444
    }

    if (family_allocated)
	free (family);
Keith Packard's avatar
Keith Packard committed
445

446
    if (style)
Keith Packard's avatar
Keith Packard committed
447
    {
448 449 450 451
	if (!FcPatternAddString (pat, FC_STYLE, style))
	{
	    if (style_allocated)
		free (style);
Keith Packard's avatar
Keith Packard committed
452
	    goto bail1;
453 454 455
	}
	if (style_allocated)
	    free (style);
Keith Packard's avatar
Keith Packard committed
456 457 458 459 460 461 462 463
    }

    if (!FcPatternAddString (pat, FC_FILE, file))
	goto bail1;

    if (!FcPatternAddInteger (pat, FC_INDEX, id))
	goto bail1;

464
    if (!FcPatternAddString (pat, FC_SOURCE, (FcChar8 *) "FreeType"))
Keith Packard's avatar
Keith Packard committed
465 466
	goto bail1;

467
#if 1
Keith Packard's avatar
Keith Packard committed
468 469 470 471 472
    if ((face->face_flags & FT_FACE_FLAG_FIXED_WIDTH) != 0)
	if (!FcPatternAddInteger (pat, FC_SPACING, FC_MONO))
	    goto bail1;
#endif

473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
    /*
     * Get the OS/2 table and poke about
     */
    os2 = (TT_OS2 *) FT_Get_Sfnt_Table (face, ft_sfnt_os2);
    if (os2 && os2->version >= 0x0001 && os2->version != 0xffff)
    {
	for (i = 0; i < NUM_CODE_PAGE_RANGE; i++)
	{
	    FT_ULong	bits;
	    int		bit;
	    if (FcCodePageRange[i].bit < 32)
	    {
		bits = os2->ulCodePageRange1;
		bit = FcCodePageRange[i].bit;
	    }
	    else
	    {
		bits = os2->ulCodePageRange2;
		bit = FcCodePageRange[i].bit - 32;
	    }
	    if (bits & (1 << bit))
	    {
495 496 497 498 499 500 501 502 503 504 505
		/* 
		 * If the font advertises support for multiple
		 * "exclusive" languages, then include support
		 * for any language found to have coverage
		 */
		if (exclusiveLang)
		{
		    exclusiveLang = 0;
		    break;
		}
		exclusiveLang = FcCodePageRange[i].lang;
506 507 508 509 510 511 512
	    }
	}
    }

    /*
     * Compute the unicode coverage for the font
     */
Keith Packard's avatar
Keith Packard committed
513 514 515 516 517 518 519 520
    cs = FcFreeTypeCharSet (face, blanks);
    if (!cs)
	goto bail1;

    /*
     * Skip over PCF fonts that have no encoded characters; they're
     * usually just Unicode fonts transcoded to some legacy encoding
     */
521
    if (FcCharSetCount (cs) == 0)
Keith Packard's avatar
Keith Packard committed
522 523 524 525 526 527 528
    {
	if (!strcmp(FT_MODULE_CLASS(&face->driver->root)->module_name, "pcf"))
	    goto bail2;
    }

    if (!FcPatternAddCharSet (pat, FC_CHARSET, cs))
	goto bail2;
529

530 531 532 533 534 535
    ls = FcFreeTypeLangSet (cs, exclusiveLang);
    if (!ls)
	goto bail2;

    if (!FcPatternAddLangSet (pat, FC_LANG, ls))
	goto bail2;
536

Keith Packard's avatar
Keith Packard committed
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
    /*
     * Drop our reference to the charset
     */
    FcCharSetDestroy (cs);
    
    if (!(face->face_flags & FT_FACE_FLAG_SCALABLE))
    {
	for (i = 0; i < face->num_fixed_sizes; i++)
	    if (!FcPatternAddDouble (pat, FC_PIXEL_SIZE,
				     (double) face->available_sizes[i].height))
		goto bail1;
	if (!FcPatternAddBool (pat, FC_ANTIALIAS, FcFalse))
	    goto bail1;
    }

    FT_Done_Face (face);
    FT_Done_FreeType (ftLibrary);
    return pat;

bail2:
    FcCharSetDestroy (cs);
bail1:
    FcPatternDestroy (pat);
bail0:
    FT_Done_Face (face);
bail:
    FT_Done_FreeType (ftLibrary);
    return 0;
}