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 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
|
/****************************************************************************
** $Id: qt/src/kernel/qfont.cpp 2.3.1 edited 2001-04-19 $
**
** Implementation of QFont, QFontMetrics and QFontInfo classes
**
** Created : 941207
**
** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
**
** This file is part of the kernel module of the Qt GUI Toolkit.
**
** This file may be distributed under the terms of the Q Public License
** as defined by Trolltech AS of Norway and appearing in the file
** LICENSE.QPL included in the packaging of this file.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
** licenses may use this file in accordance with the Qt Commercial License
** Agreement provided with the Software.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
** information about Qt Commercial License Agreements.
** See http://www.trolltech.com/qpl/ for QPL licensing information.
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
#include "qfont.h"
#include "qfontdata_p.h"
#include "qfontmetrics.h"
#include "qfontinfo.h"
#include "qwidget.h"
#include "qpainter.h"
#include "qpainter_p.h"
#include "qmap.h"
#include "qstrlist.h"
#include "qdatastream.h"
#include "qapplication.h"
#include <ctype.h>
#include <limits.h>
// REVISED: arnt
/*!
\class QFont qfont.h
\brief The QFont class specifies a font used for drawing text.
\ingroup drawing
\ingroup appearance
\ingroup shared
QFont, more precisely, is a collection of attributes of a font.
When Qt needs to draw text, it will look up and load the closest
matching installed font and draw using that.
The most important attributes of a QFont are its family(),
pointSize(), weight() and whether it is italic() or not. There are
QFont constructors that take these attributes as arguments, as shown
in this example:
\code
void MyWidget::paintEvent( QPaintEvent * )
{
QPainter p( this );
// times, 12pt, normal
p.setFont( QFont( "times" ) );
p.drawText( 10, 20, "Text1" );
// helvetica, 18pt, normal
p.setFont( QFont( "helvetica", 18 ) );
p.drawText( 10, 120, "Text2" );
// courier, 24pt, bold
p.setFont( QFont( "courier", 24, QFont::Bold ) );
p.drawText( 10, 220, "Text3" );
// lucida, 36pt, bold, italic
p.setFont( QFont( "lucida", 36, QFont::Bold, TRUE ) );
p.drawText( 10, 320, "Text4" );
}
\endcode
The default QFont constructor makes a copy of application's default
font, QApplication::font().
You can also change these attributes of an existing QFont object
using functions such as setFamily(), setPointSize(), setWeight() and
setItalic().
There are also some less-used attributes. setUnderline() decides
whether the font is underlined or not; setStrikeOut() can be used to get
overstrike (a horizontal line through the middle of the characters);
setFixedPitch() determines whether Qt should give preference to
fixed-pitch (also known as fixed-width) or variable-pitch fonts when it
needs to choose an installed font; setStyleHint() can be used to offer
more general help to the font matching algorithm, and on X11
setRawName() can be used to bypass the entire font matching and use an
X11 XLFD.
Of course there is also a reader function for each of these set*()
functions. Note that the reader functions return the values
previously set, \e not the attributes of the actual window system
font that will be used for drawing. You can get information about
the font that will be used for drawing by using QFontInfo, but be
aware that QFontInfo may be slow and that its results depend on what
fonts are installed.
In general font handling and loading are costly operations,
especially on X11. The QFont class contains extensive optimizations
to make copying of QFont objects fast, and to cache the results of
the slow window system functions it uses.
QFont also offers a few static functions, mostly to tune the font
matching algorithm: You can control what happens if a font's family
isn't installed using insertSubstitution() and removeSubstitution(),
ask what happens for a single family using substitute() and you can
get a complete list of the fallback families using substitutions().
cacheStatistics() offers cache effectiveness information; this is
useful mostly for debugging.
Finally, QApplication::setFont() allows you to set the default font.
The default default font is chosen at application startup from a set
of common installed fonts that support the correct character set for
the current locale. Of course, the initialization algorithm has a
default, too: The default default default font!
The font matching algorithm works as follows:
First an available font family is found. If the requested is not
available the styleHint() is used to select a replacement family. If
the style hint has not been set, "helvetica" will be used.
If even the replacement family is not found, "helvetica" is searched
for, if that too is not found Qt will search for a last resort font,
i.e. a specific font to match to, ignoring the attribute
settings. Qt searches through a built-in list of very common
fonts. If none of these are available, Qt gives you an error message
and aborts (of course this only happens if you are using fonts and
Qt \e has to load a font). We have not been able to find a case
where this happens. Please <a href="bughowto.html">report it as a
bug</a> if it does, preferably with a list of the fonts you have
installed.
The following attributes are then matched, in order of priority: <ol>
<li> charSet()
<li> fixedPitch()
<li> pointSize() (see below)
<li> weight()
<li> italic()
</ol>
If, for example, a font with the correct character set is found, but
with all other attributes in the list unmatched, it will be chosen
before a font with the wrong character set but with all other
attributes correct.
The point size is defined to match if it is within 20% of the
requested point size. Of course, when several fonts match and only
point size differs the closest point size to the one requested will
be chosen.
For more general information on fonts, see the
<a href="http://www.nwalsh.com/comp.fonts/FAQ/">comp.fonts FAQ</a>
and for more general information on encodings, see
<a href="http://czyborra.com/charsets/">Roman Czyborra's page</a>
about that.
\sa QFontMetrics QFontInfo QApplication::setFont()
QWidget::setFont() QPainter::setFont() QFont::StyleHint
QFont::CharSet QFont::Weight
*/
/*! \enum QFont::CharSet
The following character set encodings are available: <ul>
<li> \c QFont::ISO_8859_1 - Latin1 , common in much of Europe
<li> \c QFont::ISO_8859_2 - Latin2, Central and Eastern European character set
<li> \c QFont::ISO_8859_3 - Latin3, less common European character set
<li> \c QFont::ISO_8859_4 - Latin4, less common European character set
<li> \c QFont::ISO_8859_5, Cyrillic
<li> \c QFont::ISO_8859_6, Arabic
<li> \c QFont::ISO_8859_7, Greek
<li> \c QFont::ISO_8859_8, Hebrew
<li> \c QFont::ISO_8859_9, Turkish
<li> \c QFont::ISO_8859_10..15, other ISO 8859 characters sets
<li> \c QFont::KOI8R - KOI8-R, Cyrillic, defined in
<a href="ftp://ftp.nordu.net/rfc/rfc1489.txt">RFC 1489.</a>
<li> \c QFont::KOI8U - KOI8-U, Cyrillic/Ukrainian, defined in
<a href="ftp://ftp.nordu.net/rfc/rfc2319.txt">RFC 2319.</a>
<li> \c QFont::CP1251, Microsoft Cyrillic encoding
<li> \c QFont::PT154, Paratype Asian Cyrillic encoding
<li> \c QFont::AnyCharSet - whatever is handiest.
<li> \c QFont::Set_Ja, Japanese
<li> \c QFont::Set_Ko, Korean
<li> \c QFont::Set_Th_TH
<li> \c QFont::Set_Zh
<li> \c QFont::Set_Zh_TW
<li> \c QFont::Unicode, Unicode character set
<li> \c QFont::Set_GBK
<li> \c QFont::Set_Big5
</ul>
*/
/*!
\internal
Initializes the internal QFontData structure.
*/
void QFont::init()
{
d = new QFontData;
CHECK_PTR( d );
d->req.pointSize = 0;
d->req.styleHint = AnyStyle;
d->req.styleStrategy = PreferDefault;
d->req.charSet = defaultCharSet;
d->req.weight = 0;
d->req.italic = FALSE;
d->req.underline = FALSE;
d->req.strikeOut = FALSE;
d->req.fixedPitch = FALSE;
d->req.hintSetByUser = FALSE;
d->req.rawMode = FALSE;
d->req.dirty = TRUE;
d->req.lbearing = SHRT_MIN;
d->req.rbearing = SHRT_MIN;
d->exactMatch = FALSE;
}
/*!
\internal
Constructs a font that gets a
\link shclass.html deep copy \endlink of \e data.
*/
QFont::QFont( QFontData *data )
{
d = new QFontData( *data );
CHECK_PTR( d );
d->count = 1; // now a single reference
}
/*!
\internal
Detaches the font object from common font data.
*/
void QFont::detach()
{
if ( d->count != 1 )
*this = QFont( d );
}
/*!
Constructs a font object that refers to the default font.
\sa QApplication::setFont(), QApplication::font()
*/
QFont::QFont()
{
const QFont tmp = QApplication::font();
d = tmp.d;
d->ref();
}
/*!
Constructs a font object with the specified \e family, \e pointSize,
\e weight and \e italic settings. The charSet() is copied from the
\link QApplication::font() default font \endlink and the rest of the
settings are set reasonably.
If \e pointSize is less than or equal to 0 it is set to 1.
\sa setFamily(), setPointSize(), setWeight(), setItalic()
*/
QFont::QFont( const QString &family, int pointSize, int weight, bool italic )
{
init();
d->req.family = family;
if ( pointSize <= 0 )
pointSize = 1;
d->req.pointSize = pointSize * 10;
d->req.weight = weight;
d->req.italic = italic;
}
/*!
Constructs a font object with the specified \e family, \e pointSize,
\e weight, \e italic and \a charSet settings. If \e pointSize is
less than or equal to 0 it is set to 1.
\sa setFamily(), setPointSize(), setWeight(), setItalic()
*/
QFont::QFont( const QString &family, int pointSize, int weight, bool italic,
CharSet charSet)
{
init();
d->req.family = family;
if ( pointSize <= 0 )
pointSize = 1;
d->req.pointSize = pointSize * 10;
d->req.weight = weight;
d->req.italic = italic;
d->req.charSet = charSet;
}
/*!
Constructs a font that is a copy of \e font.
*/
QFont::QFont( const QFont &font )
{
d = font.d;
d->ref();
}
/*!
Destructs the font object.
*/
QFont::~QFont()
{
if ( d->deref() ) {
delete d;
}
}
/*!
Assigns \e font to this font and returns a reference to this font.
*/
QFont &QFont::operator=( const QFont &font )
{
font.d->ref();
if ( d->deref() )
delete d;
d = font.d;
return *this;
}
/*!
Returns the family name set by setFamily().
Use QFontInfo to find the family name of the window system font that
is actually used for drawing.
Example:
\code
QFont font( "Nairobi" );
QFontInfo info( font );
qDebug( "Font family requested is : \"%s\"", font.family() );
qDebug( "Font family actually used is: \"%s\"", info.family() );
\endcode
\sa setFamily(), substitute()
*/
QString QFont::family() const
{
return d->req.family;
}
/*!
Sets the family name of the font (e.g. "Helvetica" or "times").
The family name is case insensitive.
If the family is not available a default family is used.
\sa family(), setStyleHint(), QFontInfo
*/
void QFont::setFamily( const QString &family )
{
if ( d->req.family != family ) {
detach();
d->req.family = family;
d->req.dirty = TRUE;
}
}
/*!
Returns the point size in 1/10ths of a point.
\sa pointSize()
*/
int QFont::deciPointSize() const
{
return d->req.pointSize;
}
/*!
Returns the point size set by setPointSize().
Use QFontInfo to find the point size of the window system font
actually used.
Example of use:
\code
QFont font( "helvetica" );
QFontInfo info( font );
font.setPointSize( 53 );
qDebug( "Font size requested is : %d", font.pointSize() );
qDebug( "Font size actually used is: %d", info.pointSize() );
\endcode
\sa setPointSize() deciPointSize()
*/
int QFont::pointSize() const
{
return d->req.pointSize / 10;
}
/*!
Sets the point size to \a pointSize. The point size must be greater
than zero.
Example:
\code
QFont font( "courier" );
font.setPointSize( 18 );
\endcode
\sa pointSize(), QFontInfo
*/
void QFont::setPointSize( int pointSize )
{
if ( pointSize <= 0 ) {
#if defined(CHECK_RANGE)
qWarning( "QFont::setPointSize: Point size <= 0 (%d)", pointSize );
#endif
return;
}
pointSize *= 10;
if ( d->req.pointSize != pointSize ) {
detach();
d->req.pointSize = (short)pointSize;
d->req.dirty = TRUE;
}
}
/*!
Sets the point size to \a pointSize. The point size must be greater
than zero. The requested precision may not be achieved on all platforms.
*/
void QFont::setPointSizeFloat( float pointSize )
{
if ( pointSize <= 0 ) {
#if defined(CHECK_RANGE)
qWarning( "QFont::setPointSize: Point size <= 0 (%f)", pointSize );
#endif
return;
}
int ps = int(pointSize * 10.0 + 0.5);
if ( d->req.pointSize != ps ) {
detach();
d->req.pointSize = (short)ps;
d->req.dirty = TRUE;
}
}
/*!
Returns the height of characters in the font in points (1/72 inch).
\sa pointSize()
*/
float QFont::pointSizeFloat() const
{
return float(d->req.pointSize)/10.0;
}
/*!
Sets the logical height of characters in the font if shown on
the screen.
*/
void QFont::setPixelSize( int pixelSize )
{
setPixelSizeFloat( float(pixelSize) );
}
/*!
Returns the value set by setItalic().
Use QFontInfo to find the italic value of the window system font actually
used.
\sa setItalic()
*/
bool QFont::italic() const
{
return d->req.italic;
}
/*!
Sets italic on or off.
\sa italic(), QFontInfo
*/
void QFont::setItalic( bool enable )
{
if ( (bool)d->req.italic != enable ) {
detach();
d->req.italic = enable;
d->req.dirty = TRUE;
}
}
/*!
Returns the weight set by setWeight().
Use QFontInfo to find the weight of the window system font actually used.
\sa setWeight(), QFontInfo
*/
int QFont::weight() const
{
return d->req.weight;
}
/*!
\enum QFont::Weight
Contains the predefined font weights:<ul>
<li> \c QFont::Light (25)
<li> \c QFont::Normal (50)
<li> \c QFont::DemiBold (63)
<li> \c QFont::Bold (75)
<li> \c QFont::Black (87)
</ul>
*/
/*!
Sets the weight (or boldness), which should be a value
from the QFont::Weight enumeration.
Example:
\code
QFont font( "courier" );
font.setWeight( QFont::Bold );
\endcode
Strictly speaking you can use all values in the range [0,99] (where
0 is ultralight and 99 is extremely black), but there is perhaps
asking too much of the underlying window system.
If the specified weight is not available the closest available will
be used. Use QFontInfo to check the actual weight.
\sa weight(), QFontInfo
*/
void QFont::setWeight( int weight )
{
if ( weight < 0 || weight > 99 ) {
#if defined(CHECK_RANGE)
qWarning( "QFont::setWeight: Value out of range (%d)", weight );
#endif
return;
}
if ( (int)d->req.weight != weight ) {
detach();
d->req.weight = weight;
d->req.dirty = TRUE;
}
}
/*!
\fn bool QFont::bold() const
Returns TRUE if weight() is a value greater than \c QFont::Normal,
otherwise FALSE.
\sa weight(), setBold(), QFontInfo::bold()
*/
/*!
\fn void QFont::setBold( bool enable )
Sets the weight to \c QFont::Bold if \e enable is TRUE, or to
\c QFont::Normal if \e enable is FALSE.
Use setWeight() to set the weight to other values.
\sa bold(), setWeight()
*/
/*!
Returns the value set by setUnderline().
Use QFontInfo to find the underline value of the window system font
actually used for drawing.
\sa setUnderline(), QFontInfo::underline()
*/
bool QFont::underline() const
{
return d->req.underline;
}
/*!
Sets underline on or off.
\sa underline(), QFontInfo
*/
void QFont::setUnderline( bool enable )
{
if ( (bool)d->req.underline != enable ) {
detach();
d->req.underline = enable;
d->req.dirty = TRUE;
}
}
/*!
Returns the value set by setStrikeOut().
Use QFontInfo to find the strike out value of the window system font
actually used.
\sa setStrikeOut(), QFontInfo::strikeOut().
*/
bool QFont::strikeOut() const
{
return d->req.strikeOut;
}
/*!
Sets strike out on or off.
\sa strikeOut(), QFontInfo
*/
void QFont::setStrikeOut( bool enable )
{
if ( (bool)d->req.strikeOut != enable ) {
detach();
d->req.strikeOut = enable;
d->req.dirty = TRUE;
}
}
/*!
Returns the value set by setFixedPitch().
Use QFontInfo to find the fixed pitch value of the window system font
actually used.
\sa setFixedPitch(), QFontInfo::fixedPitch()
*/
bool QFont::fixedPitch() const
{
return d->req.fixedPitch;
}
/*!
Sets fixed pitch on or off.
A fixed pitch font is a font where all characters have the same width.
\sa fixedPitch(), QFontInfo
*/
void QFont::setFixedPitch( bool enable )
{
if ( (bool)d->req.fixedPitch != enable ) {
detach();
d->req.fixedPitch = enable;
d->req.dirty = TRUE;
}
}
/*!
Returns the StyleStratgie set by setStyleHint()
\sa setStyleHint()
*/
QFont::StyleStrategy QFont::styleStrategy() const
{
return (StyleStrategy)d->req.styleStrategy;
}
/*!
Returns the StyleHint set by setStyleHint().
\sa setStyleHint(), QFontInfo::styleHint()
*/
QFont::StyleHint QFont::styleHint() const
{
return (StyleHint)d->req.styleHint;
}
/*!
\enum QFont::StyleHint
Style hints are used by the font matching algorithm when a selected
font family cannot be found and is used to find an appropriate
default family.
The style hint value of \c AnyStyle leaves the task of finding a
good default family to the font matching algorithm.
The other available style hints are \c QFont::SansSerif, \c
QFont::TypeWriter, \c QFont::OldEnglish, \c QFont::System
*/
/*!
\enum QFont::StyleStrategy
The style strategy tells the font matching algorithm what type
of fonts should be used to find an appropriate default family.
The algorithm won't prefer any type of font if \c NoStratgie is
provided.
The other available strategys are \c QFont::PreferBitmap, \c
QFont::PreferDevice, \c QFont::PreferOutline, \c QFont::ForceOutline
Any of these may be ORed with a indicator whether exact matching or
good quality should be preferred.
\c QFont::PreferMatch, \c QFont::PreferQuality
*/
/*!
Sets the style hint and strategy.
The style hint has a default value of \c AnyStyle which leaves the
task of finding a good default family to the font matching
algorithm.
The style strategy has a default value of \c PreferDefault which tells
the algorithm not to prefer any type of font.
Right now, the X version only supports bitmap fonts.
In the example below the push button will
display its text label with the Bavaria font family if this family
is available, if not it will display its text label with another
serif font:
\code
#include <qapplication.h>
#include <qpushbutton.h>
#include <qfont.h>
int main( int argc, char **argv )
{
QApplication app( argc, argv );
QPushButton push("Push me");
QFont font( "Bavaria", 18 ); // preferred family is Bavaria
font.setStyleHint( QFont::Serif ) // can also use any serif font
push.setFont( font );
return app.exec( &push );
}
\endcode
\sa QFont::StyleHint, styleHint(), QFont::StyleStrategy, styleStrategy(), QFontInfo
*/
void QFont::setStyleHint( StyleHint hint, StyleStrategy strategy )
{
if ( (StyleHint)d->req.styleHint != hint ) {
detach();
d->req.styleHint = hint;
d->req.styleStrategy = strategy;
d->req.hintSetByUser = TRUE;
d->req.dirty = TRUE;
}
}
/*!
Sets the style hint.
\sa setStyleHint()
*/
void QFont::setStyleHint( StyleHint hint ) //#### merge with above in 3.0
{
setStyleHint( hint, PreferDefault );
}
/*!
Returns the character set by setCharSet().
Use QFontInfo to find the CharSet of the window system font actually used.
\sa setCharSet()
*/
QFont::CharSet QFont::charSet() const
{
return (CharSet) d->req.charSet;
}
/*!
Sets the character set encoding (e.g. \c Latin1).
If the character set encoding is not available another will be used
for drawing. For most non-trivial applications you will probably
not want this to happen since it can totally obscure the text shown
to the user. This is why the font matching algorithm gives high
priority to finding the correct character set.
You can test that the character set is correct using the QFontInfo
class.
\sa charSet(), QFontInfo
*/
void QFont::setCharSet( CharSet charset )
{
if ( (CharSet)d->req.charSet == charset )
return;
detach();
d->req.charSet = charset;
d->req.dirty = TRUE;
}
/*! Returns a guess at the character set most likely to be
appropriate for the locale the application is running in. This is
the character sets fonts will use by default.
*/
QFont::CharSet QFont::charSetForLocale()
{
return defaultCharSet;
}
/*!
Returns the value set by setRawMode().
\sa setRawMode()
*/
bool QFont::rawMode() const
{
return d->req.rawMode;
}
/*! Turns raw mode on if \a enable is TRUE, or turns it off if \a
enable is FALSE.
Calling this function only has effect under X windows. If raw mode
is enabled, Qt will search for an X font with a complete font name
matching the family name, ignoring all other values set for the
QFont. If the font name matches several fonts, Qt will use the
first font returned by X. QFontInfo \e cannot be used to fetch
information about a QFont using raw mode (it will return the values
set in the QFont for all parameters, including the family name).
\warning Do not use raw mode unless you really, really need it! In
most (if not all) cases, setRawName() is a much better choise.
\sa rawMode(), setRawName()
*/
void QFont::setRawMode( bool enable )
{
if ( (bool)d->req.rawMode != enable ) {
detach();
d->req.rawMode = enable;
d->req.dirty = TRUE;
}
}
/*!
Returns TRUE if a window system font exactly matching the settings
of this font is available.
\sa QFontInfo
*/
bool QFont::exactMatch() const
{
if ( d->req.dirty )
load();
return d->exactMatch;
}
/*!
Returns TRUE if the this font is equal to \e f, or FALSE if they are
different.
Two QFonts are equal if their font attributes are equal. If
rawMode() is enabled for both fonts, then only the family fields are
compared.
\sa operator!=()
*/
bool QFont::operator==( const QFont &f ) const
{
return f.d == d || f.key() == key();
}
/*!
Returns TRUE if the this font is different from \e f, or FALSE if they are
equal.
Two QFonts are different if their font attributes are different. If
rawMode() is enabled for both fonts, then only the family fields are
compared.
\sa operator==()
*/
bool QFont::operator!=( const QFont &f ) const
{
return !(operator==( f ));
}
/*!
Returns TRUE if this font and \a f are copies of each other,
i.e. one of them was created as a copy of the other and neither was
subsequently modified. This is much stricter than equality.
\sa operator=, operator==
*/
bool QFont::isCopyOf( const QFont & f ) const
{
return d && d == f.d;
}
/*!
Returns the encoding name of a character set, e.g. QFont::ISO_8859_1
returns "iso8859-1" and QFont::Unicode returns "iso10646".
*/
QString QFont::encodingName( CharSet cs )
{
QString result;
switch( cs ) {
case QFont::ISO_8859_1:
result = "iso8859-1";
break;
case QFont::ISO_8859_2:
result = "iso8859-2";
break;
case QFont::ISO_8859_3:
result = "iso8859-3";
break;
case QFont::ISO_8859_4:
result = "iso8859-4";
break;
case QFont::ISO_8859_5:
result = "iso8859-5";
break;
case QFont::ISO_8859_6:
result = "iso8859-6";
break;
case QFont::ISO_8859_7:
result = "iso8859-7";
break;
case QFont::ISO_8859_8:
result = "iso8859-8";
break;
case QFont::ISO_8859_9:
result = "iso8859-9";
break;
case QFont::ISO_8859_10:
result = "iso8859-10";
break;
case QFont::ISO_8859_11:
result = "iso8859-11";
break;
case QFont::ISO_8859_12:
result = "iso8859-12";
break;
case QFont::ISO_8859_13:
result = "iso8859-13";
break;
case QFont::ISO_8859_14:
result = "iso8859-14";
break;
case QFont::ISO_8859_15:
result = "iso8859-15";
break;
case QFont::KOI8R:
result = "koi8-r";
break;
case QFont::KOI8U:
result = "koi8-u";
break;
case QFont::Set_Ja:
result = "Set_Ja";
break;
case QFont::Set_Ko:
result = "Set_Ko";
break;
case QFont::Set_Th_TH:
result = "Set_Th_TH";
break;
case QFont::Set_Zh:
result = "Set_Zh";
break;
case QFont::Set_Zh_TW:
result = "Set_Zh_TW";
break;
case QFont::Set_GBK:
result = "Set_GBK";
break;
case QFont::Set_Big5:
result = "Set_Big5";
break;
case QFont::AnyCharSet:
result = "AnyCharSet";
break;
case QFont::Unicode:
result = "iso10646";
break;
case JIS_X_0201:
result = "jisx0201";
break;
case JIS_X_0208:
result = "jisx0208";
break;
case KSC_5601:
result = "ksc5601";
break;
case GB_2312:
result = "gb2312";
break;
case Big5:
result = "big5";
break;
case QFont::TSCII:
result = "TSCII";
break;
case QFont::CP1251:
result = "cp1251";
case QFont::PT154:
result = "pt154";
break;
}
return result;
}
/*!
Returns the QApplication default font.
This function will be removed in a future version of Qt. Please use
QApplication::font() instead.
*/
QFont QFont::defaultFont()
{
return QApplication::font();
}
/*!
\obsolete
Please use QApplication::setFont() instead.
*/
void QFont::setDefaultFont( const QFont &f )
{
QApplication::setFont( f );
}
/*****************************************************************************
QFont substitution management
*****************************************************************************/
// Just case insensitive
class QCIString : public QString {
public:
QCIString() { }
QCIString( const QString& s ) : QString(s) { }
};
bool operator<( const QCIString &s1, const QCIString &s2 )
{ return s1.lower() < s2.lower(); }
bool operator==( const QCIString &s1, const QCIString &s2 )
{ return s1.lower() == s2.lower(); }
bool operator!=( const QCIString &s1, const QCIString &s2 )
{ return s1.lower() != s2.lower(); }
// .... That's all QMap needs
typedef QMap<QCIString,QString> QFontSubst;
static QFontSubst *fontSubst = 0;
static void cleanupFontSubst()
{
delete fontSubst;
fontSubst = 0;
}
static void initFontSubst() // create substitution dict
{
static const char *initTbl[] = { // default substitutions
#if defined(_WS_X11_)
"arial", "helvetica",
"helv", "helvetica",
"tms rmn", "times",
#elif defined(_WS_WIN_)
"times", "Times New Roman",
"courier", "Courier New",
"helvetica", "Arial",
#endif
0, 0
};
if ( fontSubst )
return;
fontSubst = new QFontSubst();
CHECK_PTR( fontSubst );
for ( int i=0; initTbl[i] != 0; i += 2 )
fontSubst->insert(
QString::fromLatin1(initTbl[i]),
QString::fromLatin1(initTbl[i+1]) );
qAddPostRoutine( cleanupFontSubst );
}
/*!
Returns the font family name to be used whenever \e familyName is
specified. The lookup is case insensitive.
If there is no substitution for \e familyName, then \e familyName is
returned.
Example:
\code
QFont::insertSubstitution( "NewYork", "London" );
QFont::insertSubstitution( "Paris", "Texas" );
QFont::substitute( "NewYork" ); // returns "London"
QFont::substitute( "PARIS" ); // returns "Texas"
QFont::substitute( "Rome" ); // returns "Rome"
QFont::removeSubstitution( "newyork" );
QFont::substitute( "NewYork" ); // returns "NewYork"
\endcode
\sa setFamily(), insertSubstitution(), removeSubstitution()
*/
QString QFont::substitute( const QString &familyName )
{
initFontSubst();
QFontSubst::Iterator i = fontSubst->find( familyName );
if ( i != fontSubst->end() )
return *i;
return familyName;
}
/*!
Inserts a new font family name substitution in the family substitution
table.
If \e familyName already exists in the substitution table, it will
be replaced with this new substitution.
\sa removeSubstitution(), substitutions(), substitute()
*/
void QFont::insertSubstitution( const QString &familyName,
const QString &replacementName )
{
initFontSubst();
fontSubst->replace( familyName, replacementName );
}
/*!
Removes a font family name substitution from the family substitution
table.
\sa insertSubstitution(), substitutions(), substitute()
*/
void QFont::removeSubstitution( const QString &familyName )
{
initFontSubst();
if ( fontSubst )
fontSubst->remove( familyName );
}
#ifndef QT_NO_STRINGLIST
/*!
Returns a sorted list of substituted family names.
\sa insertSubstitution(), removeSubstitution(), substitute()
*/
QStringList QFont::substitutions()
{
QStringList list;
initFontSubst();
QFontSubst::Iterator it = fontSubst->begin();
while ( it != fontSubst->end() ) {
list.append(*it);
++it;
}
return list;
}
#endif // QT_NO_STRINGLIST
/*
Internal function. Converts boolean font settings (except dirty)
to an unsigned 8-bit number. Used for serialization etc.
*/
static Q_UINT8 get_font_bits( const QFontDef &f )
{
Q_UINT8 bits = 0;
if ( f.italic )
bits |= 0x01;
if ( f.underline )
bits |= 0x02;
if ( f.strikeOut )
bits |= 0x04;
if ( f.fixedPitch )
bits |= 0x08;
if ( f.hintSetByUser )
bits |= 0x10;
if ( f.rawMode )
bits |= 0x20;
return bits;
}
#ifndef QT_NO_DATASTREAM
/*
Internal function. Sets boolean font settings (except dirty)
from an unsigned 8-bit number. Used for serialization etc.
*/
static void set_font_bits( Q_UINT8 bits, QFontDef *f )
{
f->italic = (bits & 0x01) != 0;
f->underline = (bits & 0x02) != 0;
f->strikeOut = (bits & 0x04) != 0;
f->fixedPitch = (bits & 0x08) != 0;
f->hintSetByUser = (bits & 0x10) != 0;
f->rawMode = (bits & 0x20) != 0;
}
#endif
/* NOT USED
static void hex2( uchar n, char *s )
{
uchar b = (n >> 4) & 0x0f;
*s++ = b + (b < 10 ? '0' : ('a'-10));
b = n & 0x0f;
*s++ = b + (b < 10 ? '0' : ('a'-10));
*s = '\0';
}
static void hex4( ushort n, char *s )
{
hex2( (n >> 8) & 0xff, s );
hex2( n & 0xff, s+2 );
}
*/
/*!
Returns the font's key, which is a textual representation of the font
settings. It is typically used to insert and find fonts in a
dictionary or a cache.
\sa QMap
*/
QString QFont::key() const
{
if ( d->req.rawMode )
return d->req.family.lower();
QString family = d->req.family.lower();
QString addStyle = d->req.addStyle.lower();
int len = family.length() * 2 +
addStyle.length() * 2 +
2 + // point size
1 + // font bits
1 + // weight
1 + // hint
1; // char set
QByteArray buf(len);
uchar *p = (uchar *)buf.data();
memcpy( (char *)p, (char *)family.unicode(), family.length()*2 );
p += family.length()*2;
if (addStyle.length() > 0) {
memcpy( (char *)p, (char *)addStyle.unicode(), addStyle.length() * 2);
p += addStyle.length() * 2;
}
*((Q_UINT16*)p) = d->req.pointSize;
p += 2;
*p++ = get_font_bits( d->req );
*p++ = d->req.weight;
*p++ = d->req.hintSetByUser ? (int)d->req.styleHint : (int)QFont::AnyStyle;
*p = d->req.charSet;
return QString( (QChar*)buf.data(), buf.size()/2 );
}
/*****************************************************************************
QFont stream functions
*****************************************************************************/
#ifndef QT_NO_DATASTREAM
/*!
\relates QFont
Writes a font to the stream.
\sa \link datastreamformat.html Format of the QDataStream operators \endlink
*/
QDataStream &operator<<( QDataStream &s, const QFont &f )
{
if ( s.version() == 1 ) {
QCString fam( f.d->req.family.latin1() );
s << fam;
}
else {
s << f.d->req.family;
}
return s << (Q_INT16)f.d->req.pointSize
<< (Q_UINT8)f.d->req.styleHint
<< (Q_UINT8)f.d->req.charSet
<< (Q_UINT8)f.d->req.weight
<< get_font_bits(f.d->req);
}
/*!
\relates QFont
Reads a font from the stream.
\sa \link datastreamformat.html Format of the QDataStream operators \endlink
*/
QDataStream &operator>>( QDataStream &s, QFont &f )
{
if ( f.d->deref() )
delete f.d;
f.init();
Q_INT16 pointSize;
Q_UINT8 styleHint, charSet, weight, bits;
if ( s.version() == 1 ) {
QCString fam;
s >> fam;
f.d->req.family = QString( fam );
}
else {
s >> f.d->req.family;
}
s >> pointSize;
s >> styleHint >> charSet >> weight >> bits;
f.d->req.pointSize = pointSize;
f.d->req.styleHint = styleHint;
f.d->req.charSet = charSet;
f.d->req.weight = weight;
f.d->req.dirty = TRUE;
set_font_bits( bits, &(f.d->req) );
return s;
}
#endif //QT_NO_DATASTREAM
/*****************************************************************************
QFontMetrics member functions
*****************************************************************************/
// invariant: this list contains pointers to ALL QFontMetrics objects
// with non-null painter pointers, and no other objects. Callers of
// these functions must maintain this invariant.
typedef QList<QFontMetrics> QFontMetricsList;
static QFontMetricsList *fm_list = 0;
static void cleanupFontMetricsList()
{
delete fm_list;
fm_list = 0;
}
static void insertFontMetrics( QFontMetrics *fm ) {
if ( !fm_list ) {
fm_list = new QFontMetricsList;
CHECK_PTR( fm_list );
qAddPostRoutine( cleanupFontMetricsList );
}
fm_list->append( fm );
}
static void removeFontMetrics( QFontMetrics *fm )
{
if ( !fm_list ) {
#if defined(CHECK_NULL)
qWarning( "QFontMetrics::~QFontMetrics: Internal error" );
#endif
return;
}
fm_list->removeRef( fm );
}
/*!
Resets all pointers to \a painter in all font metrics objects in the
application.
*/
void QFontMetrics::reset( const QPainter *painter )
{
if ( fm_list ) {
QListIterator<QFontMetrics> it( *fm_list );
QFontMetrics * fm;
while( (fm=it.current()) != 0 ) {
++it;
if ( fm->painter == painter ) {
fm->painter = 0; // detach from painter
removeFontMetrics( fm );
}
}
}
}
/*!
\class QFontMetrics qfontmetrics.h
\brief The QFontMetrics class provides font metrics information about fonts.
\ingroup fonts
\ingroup shared
QFontMetrics functions calculate size of characters and strings for
a given font. There are three ways you can create a QFontMetrics object:
The QFontMetrics constructor with a QFont creates a font metrics
object for a screen-compatible font, i.e. the font can not be a
printer font.
QWidget::fontMetrics() returns the font metrics for a widget's font.
This is equivalent to QFontMetrics(widget->font()). Setting a new
font for the widget later does not affect the font metrics object.
QPainter::fontMetrics() returns the font metrics for a painter's
current font. The font metrics object is automatically updated if
somebody sets a new painter font (unlike the two above cases, which
take a "snapshot" of a font).
Once created, the object provides functions to access the individual
metrics of the font, its characters, and for strings rendered in
this font.
There are several functions that operate on the font: ascent(),
descent(), height(), leading() and lineSpacing() return the basic
size properties of the font, and underlinePos(), strikeOutPos() and
lineWidth() return properties of the line that underlines or strikes
out the characters. These functions are all fast.
There are also some functions that operate on the set of glyphs in
the font: minLeftBearing(), minRightBearing() and maxWidth(). These
are by necessity slow, and we recommend avoiding them if possible.
For each character, you can get its width(), leftBearing() and
rightBearing() and find out whether it is in the font using
inFont(). You can also treat the character as a string, and use the
string functions on it.
The string functions include width(), to return the width of a
string in pixels (or points, for a printer), boundingRect(), to
return the rectangle necessary to render a string, and size(), to
return the size of that rectangle.
Example:
\code
QFont font("times",24);
QFontMetrics fm(font);
int w = fm.width("What's the width of this text");
int h = fm.height();
\endcode
\sa QFont QFontInfo
*/
/*!
Constructs a font metrics object for \a font.
The font must be screen-compatible, i.e. a font you use when drawing
text in QWidget or QPixmap objects, not QPicture or QPrinter.
If \a font is a printer font, you'll probably get wrong results.
Use QPainter::fontMetrics() to get the font metrics when painting.
This is a little slower than using this constructor, but it always
gives correct results.
*/
QFontMetrics::QFontMetrics( const QFont &font )
{
font.handle();
fin = font.d->fin;
painter = 0;
flags = 0;
if ( font.underline() )
setUnderlineFlag();
if ( font.strikeOut() )
setStrikeOutFlag();
}
/*!
\internal
Constructs a font metrics object for a painter.
*/
QFontMetrics::QFontMetrics( const QPainter *p )
{
painter = (QPainter *)p;
#if defined(CHECK_STATE)
if ( !painter->isActive() )
qWarning( "QFontMetrics: Get font metrics between QPainter::begin() "
"and QPainter::end()" );
#endif
// ######### that is not necessary, or is it?????? (ME)
//if ( painter->testf(DirtyFont) )
// painter->updateFont();
painter->setf( QPainter::FontMet );
fin = painter->cfont.d->fin;
flags = 0;
insertFontMetrics( this );
}
/*!
Constructs a copy of \e fm.
*/
QFontMetrics::QFontMetrics( const QFontMetrics &fm )
: fin(fm.fin), painter(fm.painter), flags(fm.flags)
{
if ( painter )
insertFontMetrics( this );
}
/*!
Destructs the font metrics object.
*/
QFontMetrics::~QFontMetrics()
{
if ( painter )
removeFontMetrics( this );
}
/*!
Font metrics assignment.
*/
QFontMetrics &QFontMetrics::operator=( const QFontMetrics &fm )
{
if ( painter )
removeFontMetrics( this );
fin = fm.fin;
painter = fm.painter;
flags = fm.flags;
if ( painter )
insertFontMetrics( this );
return *this;
}
/*!
Returns the bounding rectangle of \e ch relative to the leftmost
point on the base line.
Note that the bounding rectangle may extend to the left of (0,0),
e.g. for italicized fonts, and that the text output may cover \e all
pixels in the bounding rectangle.
Note that the rectangle usually extends both above and below the
base line.
\sa width()
*/
QRect QFontMetrics::boundingRect( QChar ch ) const
{
QString str;
str += ch;
return boundingRect( str, 1 );
}
/*!
Returns the bounding rectangle of the first \e len characters of \e str,
which is the set of pixels the text would cover if drawn at (0,0). The
drawing, and hence the bounding rectangle, is constrained to the rectangle
\a (x,y,w,h).
If \a len is negative (default value), the whole string is used.
The \a flgs argument is
the bitwise OR of the following flags: <ul>
<li> \c AlignLeft aligns to the left border.
<li> \c AlignRight aligns to the right border.
<li> \c AlignHCenter aligns horizontally centered.
<li> \c AlignTop aligns to the top border.
<li> \c AlignBottom aligns to the bottom border.
<li> \c AlignVCenter aligns vertically centered
<li> \c AlignCenter (= \c AlignHCenter | AlignVCenter)
<li> \c SingleLine ignores newline characters in the text.
<li> \c ExpandTabs expands tabulators.
<li> \c ShowPrefix interprets "&x" as "x" underlined.
<li> \c WordBreak breaks the text to fit the rectangle.
</ul>
Horizontal alignment defaults to AlignLeft and vertical alignment
defaults to AlignTop.
If several of the horizontal or several of the vertical alignment flags
are set, the resulting alignment is undefined.
These flags are defined in qnamespace.h.
If \c ExpandTabs is set in \a flgs, then:
if \a tabarray is non.zero, it specifies a 0-terminated sequence
of pixel-positions for tabs; otherwise
if \a tabstops is non-zero, it is used as the tab spacing (in pixels).
Note that the bounding rectangle may extend to the left of (0,0),
e.g. for italicized fonts, and that the text output may cover \e all
pixels in the bounding rectangle.
Newline characters are processed as linebreaks.
Despite the different actual character heights, the heights of the
bounding rectangles of "Yes" and "yes" are the same.
The bounding rectangle given by this function is somewhat larger
than that calculated by the simpler boundingRect() function. This
function uses the \link minLeftBearing() maximum left \endlink and
\link minRightBearing() right \endlink font bearings as is necessary
for multi-line text to align correctly. Also, fontHeight() and
lineSpacing() are used to calculate the height, rather than
individual character heights.
The \a internal argument is for internal purposes.
\sa width(), QPainter::boundingRect(), Qt::AlignmentFlags
*/
QRect QFontMetrics::boundingRect( int x, int y, int w, int h, int flgs,
const QString& str, int len, int tabstops,
int *tabarray, char **intern ) const
{
if ( len < 0 )
len = str.length();
int tabarraylen=0;
if (tabarray)
while (tabarray[tabarraylen])
tabarraylen++;
QRect r;
qt_format_text( *this, x, y, w, h, flgs, str, len, &r,
tabstops, tabarray, tabarraylen, intern, 0 );
return r;
}
/*!
Returns the size in pixels of the first \e len characters of \e str.
If \a len is negative (default value), the whole string is used.
The \a flgs argument is
the bitwise OR of the following flags: <ul>
<li> \c SingleLine ignores newline characters in the text.
<li> \c ExpandTabs expands tabulators.
<li> \c ShowPrefix interprets "&x" as "x" underlined.
<li> \c WordBreak breaks the text to fit the rectangle.
</ul>
These flags are defined in qnamespace.h.
If \c ExpandTabs is set in \a flgs, then:
if \a tabarray is non.zero, it specifies a 0-terminated sequence
of pixel-positions for tabs; otherwise
if \a tabstops is non-zero, it is used as the tab spacing (in pixels).
Newline characters are processed as linebreaks.
Despite the different actual character heights, the heights of the
bounding rectangles of "Yes" and "yes" are the same.
The \a internal argument is for internal purposes.
\sa boundingRect()
*/
QSize QFontMetrics::size( int flgs, const QString &str, int len, int tabstops,
int *tabarray, char **intern ) const
{
return boundingRect(0,0,1,1,flgs,str,len,tabstops,tabarray,intern).size();
}
/*****************************************************************************
QFontInfo member functions
*****************************************************************************/
// invariant: this list contains pointers to ALL QFontInfo objects
// with non-null painter pointers, and no other objects. Callers of
// these functions must maintain this invariant.
typedef QList<QFontInfo> QFontInfoList;
static QFontInfoList *fi_list = 0;
static void cleanupFontInfoList()
{
delete fi_list;
fi_list = 0;
}
static void insertFontInfo( QFontInfo *fi )
{
if ( !fi_list ) {
fi_list = new QFontInfoList;
CHECK_PTR( fi_list );
qAddPostRoutine( cleanupFontInfoList );
}
fi_list->append( fi );
}
static void removeFontInfo( QFontInfo *fi )
{
if ( !fi_list ) {
#if defined(CHECK_NULL)
qWarning( "QFontInfo::~QFontInfo: Internal error" );
#endif
return;
}
fi_list->removeRef( fi );
}
/*!
Resets all pointers to \a painter in all font metrics objects in the
application.
*/
void QFontInfo::reset( const QPainter *painter )
{
if ( fi_list ) {
QListIterator<QFontInfo> it( *fi_list );
QFontInfo * fi;
while( (fi=it.current()) != 0 ) {
++it;
if ( fi->painter == painter ) {
fi->painter = 0; // detach from painter
removeFontInfo( fi );
}
}
}
}
/*!
\class QFontInfo qfontinfo.h
\brief The QFontInfo class provides general information about fonts.
\ingroup fonts
\ingroup shared
The QFontInfo class mirrors QFont exactly, but where QFont access
functions return set values, QFontInfo returns the values that
apply to the font in use.
For example, when the program asks for a 25pt Courier font on a
machine that has a 24pt Courier font but not a scalable one, QFont
will (normally) use the 24pt Courier for rendering. In this case,
QFont::pointSize() returns 25 and QFontInfo::pointSize() 24.
The access functions in QFontInfo mirror QFont exactly, except for
this difference.
There are three ways to create a QFontInfo object.
The QFontInfo constructor with a QFont creates a font info object
for a screen-compatible font, i.e. the font can not be a printer
font.
QWidget::fontInfo() returns the font info for a widget's font. This
is equivalent to QFontInfo(widget->font()). Setting a new font for
the widget later does not affect the font info object.
QPainter::fontInfo() returns the font info for a painter's current
font. The font info object is automatically updated if somebody sets
a new painter font, unlike the two above cases, which take a
"snapshot" of a font.
\sa QFont QFontMetrics
*/
/*!
Constructs a font info object for \a font.
The font must be screen-compatible, i.e. a font you use when drawing
text in \link QWidget widgets\endlink or \link QPixmap pixmaps\endlink.
If \a font is a printer font, you'll probably get wrong results.
Use the QPainter::fontInfo() to get the font info when painting.
This is a little slower than using this constructor, but it always
gives correct results.
*/
QFontInfo::QFontInfo( const QFont &font )
{
font.handle();
fin = font.d->fin;
painter = 0;
flags = 0;
if ( font.underline() )
setUnderlineFlag();
if ( font.strikeOut() )
setStrikeOutFlag();
if ( font.exactMatch() )
setExactMatchFlag();
}
/*!
\internal
Constructs a font info object for a painter.
*/
QFontInfo::QFontInfo( const QPainter *p )
{
painter = (QPainter *)p;
#if defined(CHECK_STATE)
if ( !painter->isActive() )
qWarning( "QFontInfo: Get font info between QPainter::begin() "
"and QPainter::end()" );
#endif
painter->setf( QPainter::FontInf );
fin = painter->cfont.d->fin;
flags = 0;
insertFontInfo( this );
}
/*!
Constructs a copy of \e fi.
*/
QFontInfo::QFontInfo( const QFontInfo &fi )
: fin(fi.fin), painter(fi.painter), flags(fi.flags)
{
if ( painter )
insertFontInfo( this );
}
/*!
Destructs the font info object.
*/
QFontInfo::~QFontInfo()
{
if ( painter )
removeFontInfo( this );
}
/*!
Font info assignment.
*/
QFontInfo &QFontInfo::operator=( const QFontInfo &fi )
{
if ( painter )
removeFontInfo( this );
fin = fi.fin;
painter = fi.painter;
flags = fi.flags;
if ( painter )
insertFontInfo( this );
return *this;
}
/*!
Returns the family name of the matched window system font.
\sa QFont::family()
*/
QString QFontInfo::family() const
{
return spec()->family;
}
/*!
Returns the point size of the matched window system font.
\sa QFont::pointSize()
*/
int QFontInfo::pointSize() const
{
return spec()->pointSize / 10;
}
/*!
Returns the italic value of the matched window system font.
\sa QFont::italic()
*/
bool QFontInfo::italic() const
{
return spec()->italic;
}
/*!
Returns the weight of the matched window system font.
\sa QFont::weight(), bold()
*/
int QFontInfo::weight() const
{
return (int)spec()->weight;
}
/*!
\fn bool QFontInfo::bold() const
Returns TRUE if weight() would return a greater than
\c QFont::Normal, and FALSE otherwise.
\sa weight(), QFont::bold()
*/
/*!
Returns the underline value of the matched window system font.
\sa QFont::underline()
\internal
Here we read the underline flag directly from the QFont.
This is OK for X11 and for Windows because we always get what we want.
*/
bool QFontInfo::underline() const
{
return painter ? painter->font().underline() : underlineFlag();
}
/*!
Returns the strike out value of the matched window system font.
\sa QFont::strikeOut()
\internal Here we read the strikeOut flag directly from the QFont.
This is OK for X11 and for Windows because we always get what we want.
*/
bool QFontInfo::strikeOut() const
{
return painter ? painter->font().strikeOut() : strikeOutFlag();
}
/*!
Returns the fixed pitch value of the matched window system font.
A fixed pitch font is a font that has constant character pixel width.
\sa QFont::fixedPitch()
*/
bool QFontInfo::fixedPitch() const
{
return spec()->fixedPitch;
}
/*!
Returns the style of the matched window system font.
Currently only returns the hint set in QFont.
\sa QFont::styleHint()
*/
QFont::StyleHint QFontInfo::styleHint() const
{
return (QFont::StyleHint)spec()->styleHint;
}
/*!
Returns the character set of the matched window system font.
\sa QFont::charSet()
*/
QFont::CharSet QFontInfo::charSet() const
{
return (QFont::CharSet)spec()->charSet;
}
/*!
Returns TRUE if the font is a raw mode font.
If it is a raw mode font, all other functions in QFontInfo will return the
same values set in the QFont, regardless of the font actually used.
\sa QFont::rawMode()
*/
bool QFontInfo::rawMode() const
{
return spec()->rawMode;
}
/*!
Returns TRUE if the matched window system font is exactly the one specified
by the font.
\sa QFont::exactMatch()
*/
bool QFontInfo::exactMatch() const
{
return painter ? painter->font().exactMatch() : exactMatchFlag();
}
|