1 2 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 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 138 139 140 141 142 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 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 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 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 533 534 535 536 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 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 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 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 843 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 893 894 895 896 897 898 899 900 901 902 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 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 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 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 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 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 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 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669
|
/* Pango
* pangocoretext-fontmap.c
*
* Copyright (C) 2000-2003 Red Hat, Inc.
* Copyright (C) 2005-2007 Imendio AB
* Copyright (C) 2010 Kristian Rietveld <kris@gtk.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include "pango-fontmap.h"
#include "pangocoretext-private.h"
#include "pango-impl-utils.h"
#include "modules.h"
#include <Carbon/Carbon.h>
typedef struct _FontHashKey FontHashKey;
typedef struct _PangoCoreTextFontset PangoCoreTextFontset;
#define PANGO_TYPE_CORE_TEXT_FAMILY (pango_core_text_family_get_type ())
#define PANGO_CORE_TEXT_FAMILY(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), PANGO_TYPE_CORE_TEXT_FAMILY, PangoCoreTextFamily))
#define PANGO_IS_CORE_TEXT_FAMILY(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), PANGO_TYPE_CORE_TEXT_FAMILY))
#define PANGO_CORE_TEXT_FAMILY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PANGO_CORE_TEXT_FAMILY, PangoCoreTextFamilyClass))
#define PANGO_IS_CORE_TEXT_FAMILY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PANGO_CORE_TEXT_FAMILY))
#define PANGO_CORE_TEXT_FAMILY_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS((object), PANGO_CORE_TEXT_FAMILY, PangoCoreTextFamilyClass))
#define PANGO_TYPE_CORE_TEXT_FONTSET (pango_core_text_fontset_get_type ())
#define PANGO_CORE_TEXT_FONTSET(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), PANGO_TYPE_CORE_TEXT_FONTSET, PangoCoreTextFontset))
#define PANGO_IS_CORE_TEXT_FONTSET(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), PANGO_TYPE_CORE_TEXT_FONTSET))
struct _PangoCoreTextFamily
{
PangoFontFamily parent_instance;
char *family_name;
guint is_monospace : 1;
PangoFontFace **faces;
gint n_faces;
};
struct _PangoCoreTextFamilyClass
{
PangoFontFamilyClass parent_class;
};
typedef struct _PangoCoreTextFamilyClass PangoCoreTextFamilyClass;
#define PANGO_TYPE_CORE_TEXT_FACE (pango_core_text_face_get_type ())
#define PANGO_CORE_TEXT_FACE(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), PANGO_TYPE_CORE_TEXT_FACE, PangoCoreTextFace))
#define PANGO_IS_CORE_TEXT_FACE(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), PANGO_TYPE_CORE_TEXT_FACE))
#define PANGO_CORE_TEXT_FACE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PANGO_CORE_TEXT_FACE, PangoCoreTextFaceClass))
#define PANGO_IS_CORE_TEXT_FACE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PANGO_CORE_TEXT_FACE))
#define PANGO_CORE_TEXT_FACE_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS((object), PANGO_CORE_TEXT_FACE, PangoCoreTextFaceClass))
struct _PangoCoreTextFace
{
PangoFontFace parent_instance;
PangoCoreTextFamily *family;
CTFontDescriptorRef ctfontdescriptor;
char *style_name;
PangoWeight weight;
CTFontSymbolicTraits traits;
guint synthetic_italic : 1;
};
struct _PangoCoreTextFaceClass
{
PangoFontFaceClass parent_class;
};
typedef struct _PangoCoreTextFaceClass PangoCoreTextFaceClass;
static GType pango_core_text_family_get_type (void);
static GType pango_core_text_face_get_type (void);
static GType pango_core_text_fontset_get_type (void);
static PangoCoreTextFontset *pango_core_text_fontset_new (PangoCoreTextFontsetKey *key,
const PangoFontDescription *description);
static PangoCoreTextFontsetKey *pango_core_text_fontset_get_key (PangoCoreTextFontset *fontset);
/*
* Helper functions to translate CoreText data to Pango
*/
typedef struct
{
float bound;
PangoWeight weight;
} PangoCTWeight;
static const float ct_weight_min = -1.00f;
static const float ct_weight_max = 1.00f;
static const PangoCTWeight ct_weight_limits[] = {
{ -0.70, PANGO_WEIGHT_THIN},
{ -0.50, PANGO_WEIGHT_ULTRALIGHT },
{ -0.35, PANGO_WEIGHT_LIGHT },
{ -0.10, PANGO_WEIGHT_BOOK },
{ 0.10, PANGO_WEIGHT_NORMAL },
{ 0.24, PANGO_WEIGHT_MEDIUM },
{ 0.36, PANGO_WEIGHT_SEMIBOLD },
{ 0.50, PANGO_WEIGHT_BOLD },
{ 0.75, PANGO_WEIGHT_ULTRABOLD },
{ 1.00, PANGO_WEIGHT_HEAVY }
};
static const char *
get_real_family (const char *family_name)
{
switch (family_name[0])
{
case 'm':
case 'M':
if (g_ascii_strcasecmp (family_name, "monospace") == 0)
return "Courier";
break;
case 's':
case 'S':
if (g_ascii_strcasecmp (family_name, "sans") == 0)
return "Helvetica";
else if (g_ascii_strcasecmp (family_name, "serif") == 0)
return "Times";
break;
}
return family_name;
}
static gchar *
gchar_from_cf_string (CFStringRef str)
{
CFIndex len;
gchar *buffer;
/* GetLength returns the number of UTF-16 pairs, so this number
* times 2 should definitely gives us enough space for UTF8.
* We add one for the terminating zero.
*/
len = CFStringGetLength (str) * 2 + 1;
buffer = g_new0 (char, len);
CFStringGetCString (str, buffer, len, kCFStringEncodingUTF8);
return buffer;
}
static char *
ct_font_descriptor_get_family_name (CTFontDescriptorRef desc)
{
CFStringRef cf_str;
char *buffer;
cf_str = CTFontDescriptorCopyAttribute (desc, kCTFontFamilyNameAttribute);
if (!cf_str)
{
int i;
/* No font family name is set, try to retrieve font name and deduce
* the family name from that instead.
*/
cf_str = CTFontDescriptorCopyAttribute (desc, kCTFontNameAttribute);
if (!cf_str)
{
/* This font is likely broken, return a default family name ... */
return g_strdup ("Sans");
}
buffer = gchar_from_cf_string (cf_str);
CFRelease (cf_str);
for (i = 0; i < strlen (buffer); i++)
if (buffer[i] == '-')
break;
if (i < strlen (buffer))
{
char *ret;
ret = g_strndup (buffer, i);
g_free (buffer);
return ret;
}
else
return buffer;
}
/* else */
buffer = gchar_from_cf_string (cf_str);
CFRelease (cf_str);
return buffer;
}
static char *
ct_font_descriptor_get_style_name (CTFontDescriptorRef desc)
{
CFStringRef cf_str;
char *buffer;
cf_str = CTFontDescriptorCopyAttribute (desc, kCTFontStyleNameAttribute);
if (!cf_str)
return NULL;
buffer = gchar_from_cf_string (cf_str);
CFRelease (cf_str);
return buffer;
}
static CTFontSymbolicTraits
ct_font_descriptor_get_traits (CTFontDescriptorRef desc)
{
CFDictionaryRef dict;
CFNumberRef cf_number;
SInt64 traits;
/* This is interesting, the value stored is a CTFontSymbolicTraits which
* is defined as uint32_t. CFNumber does not have an obvious type which
* deals with unsigned values. Upon inspection with CFNumberGetType,
* it turns out this value is stored as SInt64, so we use that to
* obtain the value from the CFNumber.
*/
dict = CTFontDescriptorCopyAttribute (desc, kCTFontTraitsAttribute);
cf_number = (CFNumberRef)CFDictionaryGetValue (dict, kCTFontSymbolicTrait);
if (!CFNumberGetValue (cf_number, kCFNumberSInt64Type, &traits))
traits = 0;
CFRelease (dict);
return (CTFontSymbolicTraits)traits;
}
static CTFontDescriptorRef
cf_font_descriptor_copy_with_traits (CTFontDescriptorRef desc,
const CTFontSymbolicTraits traits)
{
CFMutableDictionaryRef dict, traits_dict;
CFDictionaryRef tmp;
CTFontDescriptorRef new_desc;
SInt64 tmp_traits;
tmp = CTFontDescriptorCopyAttributes (desc);
dict = CFDictionaryCreateMutableCopy (kCFAllocatorDefault, 0, tmp);
CFRelease (tmp);
tmp = CTFontDescriptorCopyAttribute (desc, kCTFontTraitsAttribute);
traits_dict = CFDictionaryCreateMutableCopy (kCFAllocatorDefault, 0, tmp);
CFRelease (tmp);
tmp_traits = traits;
CFDictionarySetValue (traits_dict, (CFTypeRef) kCTFontSymbolicTrait,
CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt64Type, &tmp_traits));
CFDictionarySetValue (dict, (CFTypeRef)kCTFontTraitsAttribute, traits_dict);
new_desc = CTFontDescriptorCreateCopyWithAttributes (desc, dict);
CFRelease (dict);
return new_desc;
}
static PangoWeight
ct_font_descriptor_get_weight (CTFontDescriptorRef desc)
{
CFDictionaryRef dict;
CFNumberRef cf_number;
CGFloat value;
PangoWeight weight;
dict = CTFontDescriptorCopyAttribute (desc, kCTFontTraitsAttribute);
cf_number = (CFNumberRef)CFDictionaryGetValue (dict,
kCTFontWeightTrait);
if (CFNumberGetValue (cf_number, kCFNumberCGFloatType, &value))
{
if (value < ct_weight_min || value > ct_weight_max)
{
/* This is really an error */
weight = PANGO_WEIGHT_NORMAL;
}
else
{
guint i;
for (i = 0; i < G_N_ELEMENTS(ct_weight_limits); i++)
if (value < ct_weight_limits[i].bound)
{
weight = ct_weight_limits[i].weight;
break;
}
}
}
else
weight = PANGO_WEIGHT_NORMAL;
CFRelease (dict);
return weight;
}
static inline gboolean
pango_core_text_style_name_is_oblique (const char *style_name)
{
if (!style_name)
return FALSE;
return g_strrstr (style_name, "Oblique") != NULL;
}
PangoFontDescription *
_pango_core_text_font_description_from_ct_font_descriptor (CTFontDescriptorRef desc)
{
SInt64 font_traits;
char *family_name;
char *style_name;
PangoFontDescription *font_desc;
font_desc = pango_font_description_new ();
/* Family name */
/* FIXME: Should we actually retrieve the family name from the list of
* families in a font map?
*/
family_name = ct_font_descriptor_get_family_name (desc);
pango_font_description_set_family (font_desc, family_name);
g_free (family_name);
/* Weight */
pango_font_description_set_weight (font_desc,
ct_font_descriptor_get_weight (desc));
/* Font traits, style name; from this we deduce style and variant */
font_traits = ct_font_descriptor_get_traits (desc);
style_name = ct_font_descriptor_get_style_name (desc);
if ((font_traits & kCTFontItalicTrait) == kCTFontItalicTrait)
pango_font_description_set_style (font_desc, PANGO_STYLE_ITALIC);
else if (pango_core_text_style_name_is_oblique (style_name))
pango_font_description_set_style (font_desc, PANGO_STYLE_OBLIQUE);
else
pango_font_description_set_style (font_desc, PANGO_STYLE_NORMAL);
/* FIXME: How can this be figured using CoreText? */
#if 0
if (font_traits & NSSmallCapsFontMask)
pango_font_description_set_variant (font_desc, PANGO_VARIANT_SMALL_CAPS);
else
#endif
pango_font_description_set_variant (font_desc, PANGO_VARIANT_NORMAL);
g_free (style_name);
return font_desc;
}
/*
* PangoCoreTextFace
*/
static inline gboolean
pango_core_text_face_is_oblique (PangoCoreTextFace *face)
{
return pango_core_text_style_name_is_oblique (face->style_name);
}
static void
pango_core_text_face_make_italic (PangoCoreTextFace *ctface,
gboolean synthetic_italic)
{
CTFontDescriptorRef new_desc;
ctface->traits |= kCTFontItalicTrait;
if (synthetic_italic)
ctface->synthetic_italic = TRUE;
/* Update the font descriptor */
new_desc = cf_font_descriptor_copy_with_traits (ctface->ctfontdescriptor,
ctface->traits);
CFRelease (ctface->ctfontdescriptor);
ctface->ctfontdescriptor = new_desc;
}
static inline PangoCoreTextFace *
pango_core_text_face_copy (const PangoCoreTextFace *old)
{
PangoCoreTextFace *face;
face = g_object_new (PANGO_TYPE_CORE_TEXT_FACE, NULL);
face->family = old->family;
face->ctfontdescriptor = CFRetain (old->ctfontdescriptor);
face->style_name = g_strdup (old->style_name);
face->weight = old->weight;
face->traits = old->traits;
face->synthetic_italic = old->synthetic_italic;
return face;
}
static inline PangoCoreTextFace *
pango_core_text_face_from_ct_font_descriptor (CTFontDescriptorRef desc)
{
PangoCoreTextFace *face = g_object_new (PANGO_TYPE_CORE_TEXT_FACE,
NULL);
face->synthetic_italic = FALSE;
face->ctfontdescriptor = CFRetain (desc);
face->style_name = ct_font_descriptor_get_style_name (desc);
face->traits = ct_font_descriptor_get_traits (desc);
face->weight = ct_font_descriptor_get_weight (desc);
return face;
}
static PangoFontDescription *
pango_core_text_face_describe (PangoFontFace *face)
{
PangoCoreTextFace *ctface = PANGO_CORE_TEXT_FACE (face);
return _pango_core_text_font_description_from_ct_font_descriptor (ctface->ctfontdescriptor);
}
static const char *
pango_core_text_face_get_face_name (PangoFontFace *face)
{
PangoCoreTextFace *ctface = PANGO_CORE_TEXT_FACE (face);
return ctface->style_name;
}
static void
pango_core_text_face_list_sizes (PangoFontFace *face,
int **sizes,
int *n_sizes)
{
*n_sizes = 0;
*sizes = NULL;
}
G_DEFINE_TYPE (PangoCoreTextFace, pango_core_text_face, PANGO_TYPE_FONT_FACE);
static void
pango_core_text_face_init (PangoCoreTextFace *face)
{
face->family = NULL;
}
static void
pango_core_text_face_finalize (GObject *object)
{
PangoCoreTextFace *ctface = PANGO_CORE_TEXT_FACE (object);
g_free (ctface->style_name);
CFRelease (ctface->ctfontdescriptor);
G_OBJECT_CLASS (pango_core_text_face_parent_class)->finalize (object);
}
static gboolean
pango_core_text_face_is_synthesized (PangoFontFace *face)
{
PangoCoreTextFace *cface = PANGO_CORE_TEXT_FACE (face);
return cface->synthetic_italic;
}
static void
pango_core_text_face_class_init (PangoCoreTextFaceClass *klass)
{
GObjectClass *object_class = (GObjectClass *)klass;
PangoFontFaceClass *pfclass = PANGO_FONT_FACE_CLASS(klass);
object_class->finalize = pango_core_text_face_finalize;
pfclass->describe = pango_core_text_face_describe;
pfclass->get_face_name = pango_core_text_face_get_face_name;
pfclass->list_sizes = pango_core_text_face_list_sizes;
pfclass->is_synthesized = pango_core_text_face_is_synthesized;
}
/*
* PangoCoreTextFamily
*/
static void
pango_core_text_family_list_faces (PangoFontFamily *family,
PangoFontFace ***faces,
int *n_faces)
{
PangoCoreTextFamily *ctfamily = PANGO_CORE_TEXT_FAMILY (family);
if (ctfamily->n_faces < 0)
{
GList *l;
GList *faces = NULL;
GList *synthetic_faces = NULL;
GHashTable *italic_faces;
const char *real_family = get_real_family (ctfamily->family_name);
CTFontCollectionRef collection;
CFArrayRef ctfaces;
CFArrayRef font_descriptors;
CFDictionaryRef attributes;
CFIndex i, count;
CFTypeRef keys[] = {
(CFTypeRef) kCTFontFamilyNameAttribute
};
CFStringRef values[] = {
CFStringCreateWithCString (kCFAllocatorDefault,
real_family,
kCFStringEncodingUTF8)
};
CTFontDescriptorRef descriptors[1];
attributes = CFDictionaryCreate (kCFAllocatorDefault,
(const void **)keys,
(const void **)values,
1,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
descriptors[0] = CTFontDescriptorCreateWithAttributes (attributes);
font_descriptors = CFArrayCreate (kCFAllocatorDefault,
(const void **)descriptors,
1,
&kCFTypeArrayCallBacks);
collection = CTFontCollectionCreateWithFontDescriptors (font_descriptors,
NULL);
ctfaces = CTFontCollectionCreateMatchingFontDescriptors (collection);
italic_faces = g_hash_table_new (g_direct_hash, g_direct_equal);
count = CFArrayGetCount (ctfaces);
for (i = 0; i < count; i++)
{
PangoCoreTextFace *face;
CTFontDescriptorRef desc = CFArrayGetValueAtIndex (ctfaces, i);
face = pango_core_text_face_from_ct_font_descriptor (desc);
face->family = ctfamily;
faces = g_list_prepend (faces, face);
if ((face->traits & kCTFontItalicTrait) == kCTFontItalicTrait ||
pango_core_text_face_is_oblique (face))
g_hash_table_insert (italic_faces,
GINT_TO_POINTER ((gint)face->weight),
face);
}
CFRelease (font_descriptors);
CFRelease (attributes);
CFRelease (ctfaces);
/* For all fonts for which a non-synthetic italic variant does
* not exist on the system, we create synthesized versions here.
*/
for (l = faces; l; l = l->next)
{
PangoCoreTextFace *face = l->data;
if (!g_hash_table_lookup (italic_faces,
GINT_TO_POINTER ((gint)face->weight)))
{
PangoCoreTextFace *italic_face;
italic_face = pango_core_text_face_copy (face);
italic_face->family = ctfamily;
pango_core_text_face_make_italic (italic_face, TRUE);
/* Try to create a sensible face name. */
g_free (italic_face->style_name);
if (strcasecmp (face->style_name, "regular") == 0)
italic_face->style_name = g_strdup ("Oblique");
else
italic_face->style_name = g_strdup_printf ("%s Oblique",
face->style_name);
synthetic_faces = g_list_prepend (synthetic_faces, italic_face);
}
}
faces = g_list_concat (faces, synthetic_faces);
ctfamily->n_faces = g_list_length (faces);
ctfamily->faces = g_new (PangoFontFace *, ctfamily->n_faces);
for (l = faces, i = 0; l; l = l->next, i++)
ctfamily->faces[i] = l->data;
g_list_free (faces);
g_hash_table_destroy (italic_faces);
}
if (n_faces)
*n_faces = ctfamily->n_faces;
if (faces)
*faces = g_memdup (ctfamily->faces, ctfamily->n_faces * sizeof (PangoFontFace *));
}
static const char *
pango_core_text_family_get_name (PangoFontFamily *family)
{
PangoCoreTextFamily *ctfamily = PANGO_CORE_TEXT_FAMILY (family);
return ctfamily->family_name;
}
static gboolean
pango_core_text_family_is_monospace (PangoFontFamily *family)
{
PangoCoreTextFamily *ctfamily = PANGO_CORE_TEXT_FAMILY (family);
return ctfamily->is_monospace;
}
G_DEFINE_TYPE (PangoCoreTextFamily, pango_core_text_family, PANGO_TYPE_FONT_FAMILY);
static void
pango_core_text_family_finalize (GObject *object)
{
PangoCoreTextFamily *family = PANGO_CORE_TEXT_FAMILY (object);
int i;
g_free (family->family_name);
if (family->n_faces != -1)
{
for (i = 0; i < family->n_faces; i++)
g_object_unref (family->faces[i]);
g_free (family->faces);
}
G_OBJECT_CLASS (pango_core_text_family_parent_class)->finalize (object);
}
static void
pango_core_text_family_class_init (PangoCoreTextFamilyClass *klass)
{
GObjectClass *object_class = (GObjectClass *)klass;
int i;
PangoFontFamilyClass *pfclass = PANGO_FONT_FAMILY_CLASS(klass);
object_class->finalize = pango_core_text_family_finalize;
pfclass->list_faces = pango_core_text_family_list_faces;
pfclass->get_name = pango_core_text_family_get_name;
pfclass->is_monospace = pango_core_text_family_is_monospace;
for (i = 0; _pango_included_core_text_modules[i].list; i++)
pango_module_register (&_pango_included_core_text_modules[i]);
}
static void
pango_core_text_family_init (PangoCoreTextFamily *family)
{
family->n_faces = -1;
}
static void pango_core_text_font_map_class_init (PangoCoreTextFontMapClass *class);
static void pango_core_text_font_map_init (PangoCoreTextFontMap *ctfontmap);
G_DEFINE_TYPE (PangoCoreTextFontMap, pango_core_text_font_map, PANGO_TYPE_FONT_MAP);
static void
pango_core_text_font_map_finalize (GObject *object)
{
PangoCoreTextFontMap *fontmap = PANGO_CORE_TEXT_FONT_MAP (object);
g_hash_table_destroy (fontmap->fontset_hash);
g_hash_table_destroy (fontmap->font_hash);
g_hash_table_destroy (fontmap->families);
G_OBJECT_CLASS (pango_core_text_font_map_parent_class)->finalize (object);
}
/* Fowler / Noll / Vo (FNV) Hash (http://www.isthe.com/chongo/tech/comp/fnv/)
*
* Not necessarily better than a lot of other hashes, but should be OK, and
* well tested with binary data.
*/
#define FNV_32_PRIME ((guint32)0x01000193)
#define FNV1_32_INIT ((guint32)0x811c9dc5)
static guint32
hash_bytes_fnv (unsigned char *buffer,
int len,
guint32 hval)
{
while (len--)
{
hval *= FNV_32_PRIME;
hval ^= *buffer++;
}
return hval;
}
static void
get_context_matrix (PangoContext *context,
PangoMatrix *matrix)
{
const PangoMatrix *set_matrix;
static const PangoMatrix identity = PANGO_MATRIX_INIT;
if (context)
set_matrix = pango_context_get_matrix (context);
else
set_matrix = NULL;
if (set_matrix)
*matrix = *set_matrix;
else
*matrix = identity;
}
/*
* Helper functions for PangoCoreTextFontsetKey
*/
static double
pango_core_text_font_map_get_resolution (PangoCoreTextFontMap *fontmap,
PangoContext *context)
{
if (PANGO_CORE_TEXT_FONT_MAP_GET_CLASS (fontmap)->get_resolution)
return PANGO_CORE_TEXT_FONT_MAP_GET_CLASS (fontmap)->get_resolution (fontmap, context);
/* FIXME: acquire DPI from CoreText using some deafault font */
g_warning ("FIXME: returning default DPI");
return 72.0;
}
static int
get_scaled_size (PangoCoreTextFontMap *fontmap,
PangoContext *context,
const PangoFontDescription *desc)
{
double size = pango_font_description_get_size (desc);
if (!pango_font_description_get_size_is_absolute (desc))
{
double dpi = pango_core_text_font_map_get_resolution (fontmap, context);
size = size * dpi / 72.;
}
return .5 + pango_matrix_get_font_scale_factor (pango_context_get_matrix (context)) * size;
}
/*
* PangoCoreTextFontsetKey
*/
struct _PangoCoreTextFontsetKey
{
PangoCoreTextFontMap *fontmap;
PangoLanguage *language;
PangoFontDescription *desc;
PangoMatrix matrix;
int pixelsize;
double resolution;
PangoGravity gravity;
gpointer context_key;
};
static void
pango_core_text_fontset_key_init (PangoCoreTextFontsetKey *key,
PangoCoreTextFontMap *fontmap,
PangoContext *context,
const PangoFontDescription *desc,
PangoLanguage *language)
{
if (!language && context)
language = pango_context_get_language (context);
key->fontmap = fontmap;
get_context_matrix (context, &key->matrix);
key->language = language;
key->pixelsize = get_scaled_size (fontmap, context, desc);
key->resolution = pango_core_text_font_map_get_resolution (fontmap, context);
key->gravity = pango_context_get_gravity (context);
key->desc = pango_font_description_copy_static (desc);
pango_font_description_unset_fields (key->desc, PANGO_FONT_MASK_SIZE);
if (context && PANGO_CORE_TEXT_FONT_MAP_GET_CLASS (fontmap)->context_key_get)
key->context_key = (gpointer)PANGO_CORE_TEXT_FONT_MAP_GET_CLASS (fontmap)->context_key_get (fontmap, context);
else
key->context_key = NULL;
}
static PangoCoreTextFontsetKey *
pango_core_text_fontset_key_copy (const PangoCoreTextFontsetKey *old)
{
PangoCoreTextFontsetKey *key = g_slice_new (PangoCoreTextFontsetKey);
key->fontmap = old->fontmap;
key->matrix = old->matrix;
key->language = old->language;
key->pixelsize = old->pixelsize;
key->resolution = old->resolution;
key->gravity = old->gravity;
key->desc = pango_font_description_copy (old->desc);
if (old->context_key)
key->context_key = PANGO_CORE_TEXT_FONT_MAP_GET_CLASS (key->fontmap)->context_key_copy (key->fontmap, old->context_key);
else
key->context_key = NULL;
return key;
}
static void
pango_core_text_fontset_key_free (PangoCoreTextFontsetKey *key)
{
pango_font_description_free (key->desc);
if (key->context_key)
PANGO_CORE_TEXT_FONT_MAP_GET_CLASS (key->fontmap)->context_key_free (key->fontmap, key->context_key);
g_slice_free (PangoCoreTextFontsetKey, key);
}
static guint
pango_core_text_fontset_key_hash (const PangoCoreTextFontsetKey *key)
{
guint32 hash = FNV1_32_INIT;
hash = hash_bytes_fnv ((unsigned char *)(&key->matrix), sizeof (double) * 4, hash);
hash ^= hash_bytes_fnv ((unsigned char *)(&key->resolution), sizeof (double), hash);
if (key->context_key)
hash ^= PANGO_CORE_TEXT_FONT_MAP_GET_CLASS (key->fontmap)->context_key_hash (key->fontmap, key->context_key);
return (hash ^
GPOINTER_TO_UINT (key->language) ^
pango_font_description_hash (key->desc));
}
static gboolean
pango_core_text_fontset_key_equal (const PangoCoreTextFontsetKey *key_a,
const PangoCoreTextFontsetKey *key_b)
{
if (key_a->language == key_b->language &&
key_a->pixelsize == key_b->pixelsize &&
key_a->resolution == key_b->resolution &&
key_a->gravity == key_b->gravity &&
pango_font_description_equal (key_a->desc, key_b->desc) &&
memcmp ((void *)&key_a->matrix, (void *)&key_b->matrix, 4 * sizeof (double)) == 0)
{
if (key_a->context_key)
return PANGO_CORE_TEXT_FONT_MAP_GET_CLASS (key_a->fontmap)->context_key_equal (key_a->fontmap,
key_a->context_key,
key_b->context_key);
else
return key_a->context_key == key_b->context_key;
}
/* else */
return FALSE;
}
static PangoLanguage *
pango_core_text_fontset_key_get_language (const PangoCoreTextFontsetKey *key)
{
return key->language;
}
static const PangoMatrix *
pango_core_text_fontset_key_get_matrix (const PangoCoreTextFontsetKey *key)
{
return &key->matrix;
}
static PangoGravity
pango_core_text_fontset_key_get_gravity (const PangoCoreTextFontsetKey *key)
{
return key->gravity;
}
static gpointer
pango_core_text_fontset_key_get_context_key (const PangoCoreTextFontsetKey *key)
{
return key->context_key;
}
/*
* PangoCoreTextFontKey
*/
struct _PangoCoreTextFontKey
{
PangoCoreTextFontMap *fontmap;
CTFontDescriptorRef ctfontdescriptor;
PangoMatrix matrix;
PangoGravity gravity;
int pixelsize;
double resolution;
gboolean synthetic_italic;
gpointer context_key;
};
static void
pango_core_text_font_key_init (PangoCoreTextFontKey *key,
PangoCoreTextFontMap *ctfontmap,
PangoCoreTextFontsetKey *fontset_key,
CTFontDescriptorRef ctdescriptor,
gboolean synthetic_italic)
{
key->fontmap = ctfontmap;
key->ctfontdescriptor = ctdescriptor;
key->matrix = *pango_core_text_fontset_key_get_matrix (fontset_key);
key->pixelsize = fontset_key->pixelsize;
key->resolution = fontset_key->resolution;
key->synthetic_italic = synthetic_italic;
key->gravity = pango_core_text_fontset_key_get_gravity (fontset_key);
key->context_key = pango_core_text_fontset_key_get_context_key (fontset_key);
}
static PangoCoreTextFontKey *
pango_core_text_font_key_copy (const PangoCoreTextFontKey *old)
{
PangoCoreTextFontKey *key = g_slice_new (PangoCoreTextFontKey);
key->fontmap = old->fontmap;
key->ctfontdescriptor = old->ctfontdescriptor;
CFRetain (key->ctfontdescriptor);
key->matrix = old->matrix;
key->pixelsize = old->pixelsize;
key->resolution = old->resolution;
key->synthetic_italic = old->synthetic_italic;
key->gravity = old->gravity;
if (old->context_key)
key->context_key = PANGO_CORE_TEXT_FONT_MAP_GET_CLASS (key->fontmap)->context_key_copy (key->fontmap, old->context_key);
else
key->context_key = NULL;
return key;
}
static void
pango_core_text_font_key_free (PangoCoreTextFontKey *key)
{
if (key->ctfontdescriptor)
CFRelease (key->ctfontdescriptor);
if (key->context_key)
PANGO_CORE_TEXT_FONT_MAP_GET_CLASS (key->fontmap)->context_key_free (key->fontmap, key->context_key);
g_slice_free (PangoCoreTextFontKey, key);
}
static guint
pango_core_text_font_key_hash (const PangoCoreTextFontKey *key)
{
guint32 hash = FNV1_32_INIT;
/* Not everything is included here, probably good enough for a hash */
hash = hash_bytes_fnv ((unsigned char *)(&key->matrix), sizeof (double) * 4, hash);
if (key->context_key)
hash ^= PANGO_CORE_TEXT_FONT_MAP_GET_CLASS (key->fontmap)->context_key_hash (key->fontmap, key->context_key);
return (hash ^ CFHash (key->ctfontdescriptor));
}
static gboolean
pango_core_text_font_key_equal (const PangoCoreTextFontKey *key_a,
const PangoCoreTextFontKey *key_b)
{
if (CFEqual (key_a->ctfontdescriptor, key_b->ctfontdescriptor) &&
memcmp (&key_a->matrix, &key_b->matrix, 4 * sizeof (double)) == 0 &&
key_a->gravity == key_b->gravity &&
key_a->pixelsize == key_b->pixelsize &&
key_a->resolution == key_b->resolution &&
key_a->synthetic_italic == key_b->synthetic_italic)
{
if (key_a->context_key && key_b->context_key)
return PANGO_CORE_TEXT_FONT_MAP_GET_CLASS (key_a->fontmap)->context_key_equal (key_a->fontmap,
key_a->context_key,
key_b->context_key);
else
return key_a->context_key == key_b->context_key;
}
else
return FALSE;
}
int
pango_core_text_font_key_get_absolute_size (const PangoCoreTextFontKey *key)
{
return key->pixelsize;
}
double
pango_core_text_font_key_get_resolution (const PangoCoreTextFontKey *key)
{
return key->resolution;
}
gboolean
pango_core_text_font_key_get_synthetic_italic (const PangoCoreTextFontKey *key)
{
return key->synthetic_italic;
}
gpointer
pango_core_text_font_key_get_context_key (const PangoCoreTextFontKey *key)
{
return key->context_key;
}
const PangoMatrix *
pango_core_text_font_key_get_matrix (const PangoCoreTextFontKey *key)
{
return &key->matrix;
}
PangoGravity
pango_core_text_font_key_get_gravity (const PangoCoreTextFontKey *key)
{
return key->gravity;
}
CTFontDescriptorRef
pango_core_text_font_key_get_ctfontdescriptor (const PangoCoreTextFontKey *key)
{
return key->ctfontdescriptor;
}
static void
pango_core_text_font_map_add (PangoCoreTextFontMap *ctfontmap,
PangoCoreTextFontKey *key,
PangoCoreTextFont *ctfont)
{
PangoCoreTextFontKey *key_copy;
_pango_core_text_font_set_font_map (ctfont, ctfontmap);
key_copy = pango_core_text_font_key_copy (key);
_pango_core_text_font_set_font_key (ctfont, key_copy);
g_hash_table_insert (ctfontmap->font_hash, key_copy, ctfont);
}
static PangoCoreTextFont *
pango_core_text_font_map_new_font (PangoCoreTextFontMap *fontmap,
PangoCoreTextFontsetKey *fontset_key,
CTFontDescriptorRef ctfontdescriptor,
gboolean synthetic_italic)
{
PangoCoreTextFontMapClass *klass;
PangoCoreTextFont *font;
PangoCoreTextFontKey key;
pango_core_text_font_key_init (&key, fontmap, fontset_key, ctfontdescriptor,
synthetic_italic);
font = g_hash_table_lookup (fontmap->font_hash, &key);
if (font)
return g_object_ref (font);
/* Call create_font */
klass = PANGO_CORE_TEXT_FONT_MAP_GET_CLASS (fontmap);
font = klass->create_font (fontmap, &key);
if (!font)
return NULL;
pango_core_text_font_map_add (fontmap, &key, font);
return font;
}
static gboolean
find_best_match (PangoCoreTextFamily *font_family,
const PangoFontDescription *description,
PangoFontDescription **best_description,
PangoCoreTextFace **best_face)
{
PangoFontDescription *new_desc;
int i;
*best_description = NULL;
*best_face = NULL;
for (i = 0; i < font_family->n_faces; i++)
{
new_desc = pango_font_face_describe (font_family->faces[i]);
if (pango_font_description_better_match (description, *best_description, new_desc))
{
pango_font_description_free (*best_description);
*best_description = new_desc;
*best_face = (PangoCoreTextFace *)font_family->faces[i];
}
else
pango_font_description_free (new_desc);
}
if (*best_description)
return TRUE;
return FALSE;
}
static gboolean
get_first_font (PangoFontset *fontset G_GNUC_UNUSED,
PangoFont *font,
gpointer data)
{
*(PangoFont **)data = font;
return TRUE;
}
static PangoFont *
pango_core_text_font_map_load_font (PangoFontMap *fontmap,
PangoContext *context,
const PangoFontDescription *description)
{
PangoLanguage *language;
PangoFontset *fontset;
PangoFont *font = NULL;
if (context)
language = pango_context_get_language (context);
else
language = NULL;
fontset = pango_font_map_load_fontset (fontmap, context,
description, language);
if (fontset)
{
pango_fontset_foreach (fontset, get_first_font, &font);
if (font)
g_object_ref (font);
g_object_unref (fontset);
}
return font;
}
static void
list_families_foreach (gpointer key,
gpointer value,
gpointer user_data)
{
GSList **list = user_data;
*list = g_slist_prepend (*list, value);
}
static void
pango_core_text_font_map_list_families (PangoFontMap *fontmap,
PangoFontFamily ***families,
int *n_families)
{
GSList *family_list = NULL;
GSList *tmp_list;
PangoCoreTextFontMap *ctfontmap = (PangoCoreTextFontMap *)fontmap;
if (!n_families)
return;
g_hash_table_foreach (ctfontmap->families,
list_families_foreach, &family_list);
*n_families = g_slist_length (family_list);
if (families)
{
int i = 0;
*families = g_new (PangoFontFamily *, *n_families);
tmp_list = family_list;
while (tmp_list)
{
(*families)[i] = tmp_list->data;
i++;
tmp_list = tmp_list->next;
}
}
g_slist_free (family_list);
}
static PangoFontset *
pango_core_text_font_map_load_fontset (PangoFontMap *fontmap,
PangoContext *context,
const PangoFontDescription *desc,
PangoLanguage *language)
{
PangoCoreTextFontset *fontset;
PangoCoreTextFontsetKey key;
PangoCoreTextFontMap *ctfontmap = PANGO_CORE_TEXT_FONT_MAP (fontmap);
pango_core_text_fontset_key_init (&key, ctfontmap,
context, desc, language);
fontset = g_hash_table_lookup (ctfontmap->fontset_hash, &key);
if (G_UNLIKELY (!fontset))
{
fontset = pango_core_text_fontset_new (&key, desc);
if (G_LIKELY (fontset))
g_hash_table_insert (ctfontmap->fontset_hash,
pango_core_text_fontset_get_key (fontset),
fontset);
else
{
/* If no font(set) could be loaded, we fallback to "Sans",
* which should always work on Mac.
*/
PangoFontDescription *tmp_desc;
/* Cannot use pango_core_text_fontset_key_free() here */
pango_font_description_free (key.desc);
tmp_desc = pango_font_description_copy_static (desc);
pango_font_description_set_family_static (tmp_desc, "Sans");
pango_core_text_fontset_key_init (&key, ctfontmap, context, tmp_desc,
language);
fontset = g_hash_table_lookup (ctfontmap->fontset_hash, &key);
if (G_UNLIKELY (!fontset))
{
fontset = pango_core_text_fontset_new (&key, tmp_desc);
g_hash_table_insert (ctfontmap->fontset_hash,
pango_core_text_fontset_get_key (fontset),
fontset);
}
}
}
/* Cannot use pango_core_text_fontset_key_free() here */
pango_font_description_free (key.desc);
return g_object_ref (fontset);
}
static void
pango_core_text_font_map_init (PangoCoreTextFontMap *ctfontmap)
{
PangoCoreTextFamily *family;
CTFontCollectionRef collection;
CFArrayRef ctfaces;
CFIndex i, count;
ctfontmap->families = g_hash_table_new_full (g_str_hash, g_str_equal,
g_free, g_object_unref);
ctfontmap->font_hash = g_hash_table_new_full ((GHashFunc)pango_core_text_font_key_hash,
(GEqualFunc)pango_core_text_font_key_equal,
(GDestroyNotify)pango_core_text_font_key_free,
NULL);
ctfontmap->fontset_hash = g_hash_table_new_full ((GHashFunc)pango_core_text_fontset_key_hash,
(GEqualFunc)pango_core_text_fontset_key_equal,
NULL,
(GDestroyNotify)g_object_unref);
collection = CTFontCollectionCreateFromAvailableFonts (0);
ctfaces = CTFontCollectionCreateMatchingFontDescriptors (collection);
count = CFArrayGetCount (ctfaces);
for (i = 0; i < count; i++)
{
SInt64 font_traits;
char *buffer;
char *family_name;
CFStringRef str;
CFNumberRef number;
CFDictionaryRef dict;
CTFontDescriptorRef desc = CFArrayGetValueAtIndex (ctfaces, i);
str = CTFontDescriptorCopyAttribute (desc, kCTFontFamilyNameAttribute);
buffer = gchar_from_cf_string (str);
family_name = g_utf8_casefold (buffer, -1);
family = g_hash_table_lookup (ctfontmap->families, family_name);
if (!family)
{
family = g_object_new (PANGO_TYPE_CORE_TEXT_FAMILY, NULL);
g_hash_table_insert (ctfontmap->families, g_strdup (family_name),
family);
family->family_name = g_strdup (buffer);
}
CFRelease (str);
g_free (buffer);
g_free (family_name);
/* We assume that all faces in the family are monospaced or none. */
dict = CTFontDescriptorCopyAttribute (desc, kCTFontTraitsAttribute);
number = (CFNumberRef)CFDictionaryGetValue (dict,
kCTFontSymbolicTrait);
if (CFNumberGetValue (number, kCFNumberSInt64Type, &font_traits))
{
if ((font_traits & kCTFontMonoSpaceTrait) == kCTFontMonoSpaceTrait)
family->is_monospace = TRUE;
}
CFRelease (dict);
}
/* Insert aliases */
family = g_object_new (PANGO_TYPE_CORE_TEXT_FAMILY, NULL);
family->family_name = g_strdup ("Sans");
g_hash_table_insert (ctfontmap->families,
g_utf8_casefold (family->family_name, -1), family);
family = g_object_new (PANGO_TYPE_CORE_TEXT_FAMILY, NULL);
family->family_name = g_strdup ("Serif");
g_hash_table_insert (ctfontmap->families,
g_utf8_casefold (family->family_name, -1), family);
family = g_object_new (PANGO_TYPE_CORE_TEXT_FAMILY, NULL);
family->family_name = g_strdup ("Monospace");
family->is_monospace = TRUE;
g_hash_table_insert (ctfontmap->families,
g_utf8_casefold (family->family_name, -1), family);
}
static void
pango_core_text_font_map_class_init (PangoCoreTextFontMapClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
PangoFontMapClass *fontmap_class = PANGO_FONT_MAP_CLASS (class);
object_class->finalize = pango_core_text_font_map_finalize;
fontmap_class->load_font = pango_core_text_font_map_load_font;
fontmap_class->list_families = pango_core_text_font_map_list_families;
fontmap_class->load_fontset = pango_core_text_font_map_load_fontset;
fontmap_class->shape_engine_type = PANGO_RENDER_TYPE_CORE_TEXT;
}
/*
* PangoCoreTextFontSet
*/
static void pango_core_text_fontset_finalize (GObject *object);
static void pango_core_text_fontset_init (PangoCoreTextFontset *fontset);
static PangoLanguage * pango_core_text_fontset_get_language (PangoFontset *fontset);
static PangoFont * pango_core_text_fontset_get_font (PangoFontset *fontset,
guint wc);
static void pango_core_text_fontset_foreach (PangoFontset *fontset,
PangoFontsetForeachFunc func,
gpointer data);
struct _PangoCoreTextFontset
{
PangoFontset parent_instance;
const gchar *orig_family;
PangoFontDescription *orig_description;
PangoCoreTextFontsetKey *key;
CFArrayRef cascade_list;
GPtrArray *fonts;
GPtrArray *coverages;
};
typedef PangoFontsetClass PangoCoreTextFontsetClass;
static PangoFontsetClass *core_text_fontset_parent_class;
/* This symbol does exist in the CoreText library shipped with Snow
* Leopard and Lion, however, it is not found in the public header files.
*/
CFArrayRef CTFontCopyDefaultCascadeList (CTFontRef font_ref);
static PangoCoreTextFontset *
pango_core_text_fontset_new (PangoCoreTextFontsetKey *key,
const PangoFontDescription *description)
{
PangoCoreTextFamily *font_family;
PangoCoreTextFontset *fontset;
PangoCoreTextFont *best_font = NULL;
const gchar *family;
gchar *name;
family = pango_font_description_get_family (description);
family = family ? family : "";
name = g_utf8_casefold (family, -1);
font_family = g_hash_table_lookup (key->fontmap->families, name);
g_free (name);
if (font_family)
{
PangoFontDescription *best_description;
PangoCoreTextFace *best_face;
gint size;
gboolean is_absolute;
/* Force a listing of the available faces */
pango_font_family_list_faces ((PangoFontFamily *)font_family, NULL, NULL);
if (!find_best_match (font_family, description, &best_description, &best_face))
return NULL;
size = pango_font_description_get_size (description);
if (size < 0)
return NULL;
is_absolute = pango_font_description_get_size_is_absolute (description);
if (is_absolute)
pango_font_description_set_absolute_size (best_description, size);
else
pango_font_description_set_size (best_description, size);
best_font = pango_core_text_font_map_new_font (key->fontmap,
key,
best_face->ctfontdescriptor,
best_face->synthetic_italic);
pango_font_description_free (best_description);
}
else
return NULL;
if (!best_font)
return NULL;
/* Create a font set with best font */
fontset = g_object_new (PANGO_TYPE_CORE_TEXT_FONTSET, NULL);
fontset->key = pango_core_text_fontset_key_copy (key);
fontset->orig_description = pango_font_description_copy (description);
fontset->fonts = g_ptr_array_new ();
g_ptr_array_add (fontset->fonts, best_font);
fontset->coverages = g_ptr_array_new ();
/* Add the cascade list for this language */
fontset->cascade_list = CTFontCopyDefaultCascadeList (pango_core_text_font_get_ctfont (best_font));
/* length of cascade list + 1 for the "real" font at the front */
g_ptr_array_set_size (fontset->fonts, CFArrayGetCount (fontset->cascade_list) + 1);
g_ptr_array_set_size (fontset->coverages, CFArrayGetCount (fontset->cascade_list) + 1);
return fontset;
}
static PangoFont *
pango_core_text_fontset_load_font (PangoCoreTextFontset *ctfontset,
CTFontDescriptorRef ctdescriptor)
{
PangoCoreTextFontsetKey *key;
PangoCoreTextFont *font;
key = pango_core_text_fontset_get_key (ctfontset);
/* For now, we will default the fallbacks to not have synthetic italic,
* in the future this may be improved.
*/
font = pango_core_text_font_map_new_font (ctfontset->key->fontmap,
ctfontset->key,
ctdescriptor,
FALSE);
return PANGO_FONT (font);
}
static PangoFont *
pango_core_text_fontset_get_font_at (PangoCoreTextFontset *ctfontset,
unsigned int i)
{
/* The first font is loaded as soon as the fontset is created */
if (i == 0)
return g_ptr_array_index (ctfontset->fonts, i);
if (i >= ctfontset->fonts->len)
return NULL;
if (g_ptr_array_index (ctfontset->fonts, i) == NULL)
{
CTFontDescriptorRef ctdescriptor = CFArrayGetValueAtIndex (ctfontset->cascade_list, i - 1);
PangoFont *font = pango_core_text_fontset_load_font (ctfontset, ctdescriptor);
g_ptr_array_index (ctfontset->fonts, i) = font;
g_ptr_array_index (ctfontset->coverages, i) = NULL;
}
return g_ptr_array_index (ctfontset->fonts, i);
}
static void
pango_core_text_fontset_class_init (PangoCoreTextFontsetClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
PangoFontsetClass *fontset_class = PANGO_FONTSET_CLASS (klass);
core_text_fontset_parent_class = g_type_class_peek_parent (klass);
object_class->finalize = pango_core_text_fontset_finalize;
fontset_class->get_font = pango_core_text_fontset_get_font;
fontset_class->get_language = pango_core_text_fontset_get_language;
fontset_class->foreach = pango_core_text_fontset_foreach;
}
static void
pango_core_text_fontset_init (PangoCoreTextFontset *ctfontset)
{
ctfontset->key = NULL;
ctfontset->cascade_list = NULL;
ctfontset->fonts = g_ptr_array_new ();
ctfontset->coverages = g_ptr_array_new ();
}
static void
pango_core_text_fontset_finalize (GObject *object)
{
PangoCoreTextFontset *ctfontset = PANGO_CORE_TEXT_FONTSET (object);
unsigned int i;
for (i = 0; i < ctfontset->fonts->len; i++)
{
PangoFont *font = g_ptr_array_index (ctfontset->fonts, i);
if (font)
g_object_unref (font);
}
g_ptr_array_free (ctfontset->fonts, TRUE);
for (i = 0; i < ctfontset->coverages->len; i++)
{
PangoCoverage *coverage = g_ptr_array_index (ctfontset->coverages, i);
if (coverage)
pango_coverage_unref (coverage);
}
g_ptr_array_free (ctfontset->coverages, TRUE);
CFRelease (ctfontset->cascade_list);
pango_font_description_free (ctfontset->orig_description);
if (ctfontset->key)
pango_core_text_fontset_key_free (ctfontset->key);
G_OBJECT_CLASS (core_text_fontset_parent_class)->finalize (object);
}
static PangoCoreTextFontsetKey *
pango_core_text_fontset_get_key (PangoCoreTextFontset *fontset)
{
return fontset->key;
}
static PangoLanguage *
pango_core_text_fontset_get_language (PangoFontset *fontset)
{
PangoCoreTextFontset *ctfontset = PANGO_CORE_TEXT_FONTSET (fontset);
return pango_core_text_fontset_key_get_language (pango_core_text_fontset_get_key (ctfontset));
}
static PangoFont *
pango_core_text_fontset_get_font (PangoFontset *fontset,
guint wc)
{
PangoCoreTextFontset *ctfontset = PANGO_CORE_TEXT_FONTSET (fontset);
PangoCoverageLevel best_level = PANGO_COVERAGE_NONE;
PangoCoverageLevel level;
PangoFont *font;
PangoCoverage *coverage;
int result = -1;
unsigned int i;
for (i = 0; i < ctfontset->fonts->len; i++)
{
PangoFont *font = pango_core_text_fontset_get_font_at (ctfontset, i);
if (!font)
continue;
coverage = g_ptr_array_index (ctfontset->coverages, i);
if (coverage == NULL)
{
font = g_ptr_array_index (ctfontset->fonts, i);
coverage = pango_font_get_coverage (font, ctfontset->key->language);
g_ptr_array_index (ctfontset->coverages, i) = coverage;
}
level = pango_coverage_get (coverage, wc);
if (result == -1 || level > best_level)
{
result = i;
best_level = level;
if (level == PANGO_COVERAGE_EXACT)
break;
}
}
if (G_UNLIKELY (result == -1))
return NULL;
font = g_ptr_array_index (ctfontset->fonts, result);
return g_object_ref (font);
}
static void
pango_core_text_fontset_foreach (PangoFontset *fontset,
PangoFontsetForeachFunc func,
gpointer data)
{
PangoCoreTextFontset *ctfontset = PANGO_CORE_TEXT_FONTSET (fontset);
unsigned int i;
for (i = 0; i < ctfontset->fonts->len; i++)
{
PangoFont *font = pango_core_text_fontset_get_font_at (ctfontset, i);
if (!font)
continue;
if ((* func) (fontset, font, data))
return;
}
}
G_DEFINE_TYPE (PangoCoreTextFontset,
pango_core_text_fontset,
PANGO_TYPE_FONTSET);
|