| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/msw/font.cpp |
| 3 | // Purpose: wxFont class |
| 4 | // Author: Julian Smart |
| 5 | // Modified by: |
| 6 | // Created: 01/02/97 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) wxWindows team |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // declarations |
| 14 | // ============================================================================ |
| 15 | |
| 16 | // ---------------------------------------------------------------------------- |
| 17 | // headers |
| 18 | // ---------------------------------------------------------------------------- |
| 19 | |
| 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
| 21 | #pragma implementation "font.h" |
| 22 | #endif |
| 23 | |
| 24 | // For compilers that support precompilation, includes "wx.h". |
| 25 | #include "wx/wxprec.h" |
| 26 | |
| 27 | #ifdef __BORLANDC__ |
| 28 | #pragma hdrstop |
| 29 | #endif |
| 30 | |
| 31 | #ifndef WX_PRECOMP |
| 32 | #include "wx/setup.h" |
| 33 | #include "wx/list.h" |
| 34 | #include "wx/utils.h" |
| 35 | #include "wx/app.h" |
| 36 | #include "wx/font.h" |
| 37 | #include "wx/log.h" |
| 38 | #endif // WX_PRECOMP |
| 39 | |
| 40 | #include "wx/msw/private.h" |
| 41 | |
| 42 | #include "wx/fontutil.h" |
| 43 | #include "wx/fontmap.h" |
| 44 | |
| 45 | #include "wx/tokenzr.h" |
| 46 | |
| 47 | #if wxUSE_EXTENDED_RTTI |
| 48 | |
| 49 | wxBEGIN_ENUM( wxFontFamily ) |
| 50 | wxENUM_MEMBER( wxDEFAULT ) |
| 51 | wxENUM_MEMBER( wxDECORATIVE ) |
| 52 | wxENUM_MEMBER( wxROMAN ) |
| 53 | wxENUM_MEMBER( wxSCRIPT ) |
| 54 | wxENUM_MEMBER( wxSWISS ) |
| 55 | wxENUM_MEMBER( wxMODERN ) |
| 56 | wxENUM_MEMBER( wxTELETYPE ) |
| 57 | wxEND_ENUM( wxFontFamily ) |
| 58 | |
| 59 | wxBEGIN_ENUM( wxFontStyle ) |
| 60 | wxENUM_MEMBER( wxNORMAL ) |
| 61 | wxENUM_MEMBER( wxITALIC ) |
| 62 | wxENUM_MEMBER( wxSLANT ) |
| 63 | wxEND_ENUM( wxFontStyle ) |
| 64 | |
| 65 | wxBEGIN_ENUM( wxFontWeight ) |
| 66 | wxENUM_MEMBER( wxNORMAL ) |
| 67 | wxENUM_MEMBER( wxLIGHT ) |
| 68 | wxENUM_MEMBER( wxBOLD ) |
| 69 | wxEND_ENUM( wxFontWeight ) |
| 70 | |
| 71 | IMPLEMENT_DYNAMIC_CLASS_WITH_COPY_XTI(wxFont, wxGDIObject,"wx/font.h") |
| 72 | |
| 73 | wxBEGIN_PROPERTIES_TABLE(wxFont) |
| 74 | wxPROPERTY( Size,int, SetPointSize, GetPointSize, 12 , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) |
| 75 | wxPROPERTY( Family, int , SetFamily, GetFamily, (int)wxDEFAULT , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // wxFontFamily |
| 76 | wxPROPERTY( Style, int , SetStyle, GetStyle, (int)wxNORMAL , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // wxFontStyle |
| 77 | wxPROPERTY( Weight, int , SetWeight, GetWeight, (int)wxNORMAL , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // wxFontWeight |
| 78 | wxPROPERTY( Underlined, bool , SetUnderlined, GetUnderlined, false , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) |
| 79 | wxPROPERTY( Face, wxString , SetFaceName, GetFaceName, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) |
| 80 | wxPROPERTY( Encoding, wxFontEncoding , SetEncoding, GetEncoding, wxFONTENCODING_DEFAULT , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) |
| 81 | wxEND_PROPERTIES_TABLE() |
| 82 | |
| 83 | wxCONSTRUCTOR_6( wxFont , int , Size , int , Family , int , Style , int , Weight , bool , Underlined , wxString , Face ) |
| 84 | |
| 85 | wxBEGIN_HANDLERS_TABLE(wxFont) |
| 86 | wxEND_HANDLERS_TABLE() |
| 87 | |
| 88 | #else |
| 89 | IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject) |
| 90 | #endif |
| 91 | |
| 92 | |
| 93 | // ---------------------------------------------------------------------------- |
| 94 | // constants |
| 95 | // ---------------------------------------------------------------------------- |
| 96 | |
| 97 | // the mask used to extract the pitch from LOGFONT::lfPitchAndFamily field |
| 98 | static const int PITCH_MASK = FIXED_PITCH | VARIABLE_PITCH; |
| 99 | |
| 100 | // ---------------------------------------------------------------------------- |
| 101 | // wxFontRefData - the internal description of the font |
| 102 | // ---------------------------------------------------------------------------- |
| 103 | |
| 104 | class WXDLLEXPORT wxFontRefData: public wxGDIRefData |
| 105 | { |
| 106 | public: |
| 107 | // constructors |
| 108 | wxFontRefData() |
| 109 | { |
| 110 | Init(-1, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, |
| 111 | FALSE, wxEmptyString, wxFONTENCODING_DEFAULT); |
| 112 | } |
| 113 | |
| 114 | wxFontRefData(int size, |
| 115 | int family, |
| 116 | int style, |
| 117 | int weight, |
| 118 | bool underlined, |
| 119 | const wxString& faceName, |
| 120 | wxFontEncoding encoding) |
| 121 | { |
| 122 | Init(size, family, style, weight, underlined, faceName, encoding); |
| 123 | } |
| 124 | |
| 125 | wxFontRefData(const wxNativeFontInfo& info, WXHFONT hFont = 0) |
| 126 | { |
| 127 | Init(info, hFont); |
| 128 | } |
| 129 | |
| 130 | wxFontRefData(const wxFontRefData& data) |
| 131 | { |
| 132 | if ( data.m_nativeFontInfoOk ) |
| 133 | { |
| 134 | Init(data.m_nativeFontInfo); |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight, |
| 139 | data.m_underlined, data.m_faceName, data.m_encoding); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | virtual ~wxFontRefData(); |
| 144 | |
| 145 | // operations |
| 146 | bool Alloc(wxFont *font); |
| 147 | |
| 148 | void Free(); |
| 149 | |
| 150 | // all wxFont accessors |
| 151 | int GetPointSize() const |
| 152 | { |
| 153 | return m_nativeFontInfoOk ? m_nativeFontInfo.GetPointSize() |
| 154 | : m_pointSize; |
| 155 | } |
| 156 | |
| 157 | int GetFamily() const |
| 158 | { |
| 159 | return m_family; |
| 160 | } |
| 161 | |
| 162 | int GetStyle() const |
| 163 | { |
| 164 | return m_nativeFontInfoOk ? m_nativeFontInfo.GetStyle() |
| 165 | : m_style; |
| 166 | } |
| 167 | |
| 168 | int GetWeight() const |
| 169 | { |
| 170 | return m_nativeFontInfoOk ? m_nativeFontInfo.GetWeight() |
| 171 | : m_weight; |
| 172 | } |
| 173 | |
| 174 | bool GetUnderlined() const |
| 175 | { |
| 176 | return m_nativeFontInfoOk ? m_nativeFontInfo.GetUnderlined() |
| 177 | : m_underlined; |
| 178 | } |
| 179 | |
| 180 | wxString GetFaceName() const |
| 181 | { |
| 182 | wxString s; |
| 183 | if ( m_nativeFontInfoOk ) |
| 184 | s = m_nativeFontInfo.GetFaceName(); |
| 185 | else |
| 186 | s = m_faceName; |
| 187 | |
| 188 | return s; |
| 189 | } |
| 190 | |
| 191 | wxFontEncoding GetEncoding() const |
| 192 | { |
| 193 | return m_nativeFontInfoOk ? m_nativeFontInfo.GetEncoding() |
| 194 | : m_encoding; |
| 195 | } |
| 196 | |
| 197 | WXHFONT GetHFONT() const { return m_hFont; } |
| 198 | |
| 199 | // ... and setters |
| 200 | void SetPointSize(int pointSize) |
| 201 | { |
| 202 | if ( m_nativeFontInfoOk ) |
| 203 | m_nativeFontInfo.SetPointSize(pointSize); |
| 204 | else |
| 205 | m_pointSize = pointSize; |
| 206 | } |
| 207 | |
| 208 | void SetFamily(int family) |
| 209 | { |
| 210 | m_family = family; |
| 211 | } |
| 212 | |
| 213 | void SetStyle(int style) |
| 214 | { |
| 215 | if ( m_nativeFontInfoOk ) |
| 216 | m_nativeFontInfo.SetStyle((wxFontStyle)style); |
| 217 | else |
| 218 | m_style = style; |
| 219 | } |
| 220 | |
| 221 | void SetWeight(int weight) |
| 222 | { |
| 223 | if ( m_nativeFontInfoOk ) |
| 224 | m_nativeFontInfo.SetWeight((wxFontWeight)weight); |
| 225 | else |
| 226 | m_weight = weight; |
| 227 | } |
| 228 | |
| 229 | void SetFaceName(const wxString& faceName) |
| 230 | { |
| 231 | if ( m_nativeFontInfoOk ) |
| 232 | m_nativeFontInfo.SetFaceName(faceName); |
| 233 | else |
| 234 | m_faceName = faceName; |
| 235 | } |
| 236 | |
| 237 | void SetUnderlined(bool underlined) |
| 238 | { |
| 239 | if ( m_nativeFontInfoOk ) |
| 240 | m_nativeFontInfo.SetUnderlined(underlined); |
| 241 | else |
| 242 | m_underlined = underlined; |
| 243 | } |
| 244 | |
| 245 | void SetEncoding(wxFontEncoding encoding) |
| 246 | { |
| 247 | if ( m_nativeFontInfoOk ) |
| 248 | m_nativeFontInfo.SetEncoding(encoding); |
| 249 | else |
| 250 | m_encoding = encoding; |
| 251 | } |
| 252 | |
| 253 | // native font info tests |
| 254 | bool HasNativeFontInfo() const { return m_nativeFontInfoOk; } |
| 255 | |
| 256 | const wxNativeFontInfo& GetNativeFontInfo() const |
| 257 | { return m_nativeFontInfo; } |
| 258 | |
| 259 | protected: |
| 260 | // common part of all ctors |
| 261 | void Init(int size, |
| 262 | int family, |
| 263 | int style, |
| 264 | int weight, |
| 265 | bool underlined, |
| 266 | const wxString& faceName, |
| 267 | wxFontEncoding encoding); |
| 268 | |
| 269 | void Init(const wxNativeFontInfo& info, WXHFONT hFont = 0); |
| 270 | |
| 271 | // font characterstics |
| 272 | int m_pointSize; |
| 273 | int m_family; |
| 274 | int m_style; |
| 275 | int m_weight; |
| 276 | bool m_underlined; |
| 277 | wxString m_faceName; |
| 278 | wxFontEncoding m_encoding; |
| 279 | |
| 280 | // Windows font handle |
| 281 | WXHFONT m_hFont; |
| 282 | |
| 283 | // Native font info |
| 284 | wxNativeFontInfo m_nativeFontInfo; |
| 285 | bool m_nativeFontInfoOk; |
| 286 | }; |
| 287 | |
| 288 | // ============================================================================ |
| 289 | // implementation |
| 290 | // ============================================================================ |
| 291 | |
| 292 | // ---------------------------------------------------------------------------- |
| 293 | // wxFontRefData |
| 294 | // ---------------------------------------------------------------------------- |
| 295 | |
| 296 | void wxFontRefData::Init(int pointSize, |
| 297 | int family, |
| 298 | int style, |
| 299 | int weight, |
| 300 | bool underlined, |
| 301 | const wxString& faceName, |
| 302 | wxFontEncoding encoding) |
| 303 | { |
| 304 | m_style = style; |
| 305 | m_pointSize = pointSize == -1 ? wxNORMAL_FONT->GetPointSize() : pointSize; |
| 306 | m_family = family; |
| 307 | m_style = style; |
| 308 | m_weight = weight; |
| 309 | m_underlined = underlined; |
| 310 | m_faceName = faceName; |
| 311 | m_encoding = encoding; |
| 312 | |
| 313 | m_hFont = 0; |
| 314 | |
| 315 | m_nativeFontInfoOk = FALSE; |
| 316 | } |
| 317 | |
| 318 | void wxFontRefData::Init(const wxNativeFontInfo& info, WXHFONT hFont) |
| 319 | { |
| 320 | // hFont may be zero, or it be passed in case we really want to |
| 321 | // use the exact font created in the underlying system |
| 322 | // (for example where we can't guarantee conversion from HFONT |
| 323 | // to LOGFONT back to HFONT) |
| 324 | m_hFont = hFont; |
| 325 | |
| 326 | m_nativeFontInfoOk = TRUE; |
| 327 | m_nativeFontInfo = info; |
| 328 | // This is the best we can do since we don't have the |
| 329 | // correct information at this point. |
| 330 | m_family = wxSWISS; |
| 331 | } |
| 332 | |
| 333 | wxFontRefData::~wxFontRefData() |
| 334 | { |
| 335 | Free(); |
| 336 | } |
| 337 | |
| 338 | bool wxFontRefData::Alloc(wxFont *font) |
| 339 | { |
| 340 | if ( !m_nativeFontInfoOk ) |
| 341 | { |
| 342 | wxFillLogFont(&m_nativeFontInfo.lf, font); |
| 343 | m_nativeFontInfoOk = TRUE; |
| 344 | } |
| 345 | |
| 346 | HFONT hfont = ::CreateFontIndirect(&m_nativeFontInfo.lf); |
| 347 | if ( !hfont ) |
| 348 | { |
| 349 | wxLogLastError(wxT("CreateFont")); |
| 350 | |
| 351 | return FALSE; |
| 352 | } |
| 353 | |
| 354 | m_hFont = (WXHFONT)hfont; |
| 355 | |
| 356 | return TRUE; |
| 357 | } |
| 358 | |
| 359 | void wxFontRefData::Free() |
| 360 | { |
| 361 | if ( m_hFont ) |
| 362 | { |
| 363 | if ( !::DeleteObject((HFONT) m_hFont) ) |
| 364 | { |
| 365 | wxLogLastError(wxT("DeleteObject(font)")); |
| 366 | } |
| 367 | |
| 368 | m_hFont = 0; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | // ---------------------------------------------------------------------------- |
| 373 | // wxNativeFontInfo |
| 374 | // ---------------------------------------------------------------------------- |
| 375 | |
| 376 | void wxNativeFontInfo::Init() |
| 377 | { |
| 378 | wxZeroMemory(lf); |
| 379 | } |
| 380 | |
| 381 | int wxNativeFontInfo::GetPointSize() const |
| 382 | { |
| 383 | // FIXME: using the screen here results in incorrect font size calculation |
| 384 | // for printing! |
| 385 | const int ppInch = ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY); |
| 386 | |
| 387 | return (int) (((72.0*(double)abs(lf.lfHeight)) / (double) ppInch) + 0.5); |
| 388 | } |
| 389 | |
| 390 | wxFontStyle wxNativeFontInfo::GetStyle() const |
| 391 | { |
| 392 | return lf.lfItalic ? wxFONTSTYLE_ITALIC : wxFONTSTYLE_NORMAL; |
| 393 | } |
| 394 | |
| 395 | wxFontWeight wxNativeFontInfo::GetWeight() const |
| 396 | { |
| 397 | if ( lf.lfWeight <= 300 ) |
| 398 | return wxFONTWEIGHT_LIGHT; |
| 399 | |
| 400 | if ( lf.lfWeight >= 600 ) |
| 401 | return wxFONTWEIGHT_BOLD; |
| 402 | |
| 403 | return wxFONTWEIGHT_NORMAL; |
| 404 | } |
| 405 | |
| 406 | bool wxNativeFontInfo::GetUnderlined() const |
| 407 | { |
| 408 | return lf.lfUnderline != 0; |
| 409 | } |
| 410 | |
| 411 | wxString wxNativeFontInfo::GetFaceName() const |
| 412 | { |
| 413 | return lf.lfFaceName; |
| 414 | } |
| 415 | |
| 416 | wxFontFamily wxNativeFontInfo::GetFamily() const |
| 417 | { |
| 418 | wxFontFamily family; |
| 419 | |
| 420 | // extract family from pitch-and-family |
| 421 | switch ( lf.lfPitchAndFamily & ~PITCH_MASK ) |
| 422 | { |
| 423 | case FF_ROMAN: |
| 424 | family = wxFONTFAMILY_ROMAN; |
| 425 | break; |
| 426 | |
| 427 | default: |
| 428 | wxFAIL_MSG( _T("unknown LOGFONT::lfFamily value") ); |
| 429 | // fall through |
| 430 | |
| 431 | case FF_SWISS: |
| 432 | family = wxFONTFAMILY_SWISS; |
| 433 | break; |
| 434 | |
| 435 | case FF_SCRIPT: |
| 436 | family = wxFONTFAMILY_SCRIPT; |
| 437 | break; |
| 438 | |
| 439 | case FF_MODERN: |
| 440 | family = wxFONTFAMILY_MODERN; |
| 441 | break; |
| 442 | |
| 443 | case FF_DECORATIVE: |
| 444 | family = wxFONTFAMILY_DECORATIVE; |
| 445 | break; |
| 446 | } |
| 447 | |
| 448 | return family; |
| 449 | } |
| 450 | |
| 451 | wxFontEncoding wxNativeFontInfo::GetEncoding() const |
| 452 | { |
| 453 | return wxGetFontEncFromCharSet(lf.lfCharSet); |
| 454 | } |
| 455 | |
| 456 | void wxNativeFontInfo::SetPointSize(int pointsize) |
| 457 | { |
| 458 | #if wxFONT_SIZE_COMPATIBILITY |
| 459 | // Incorrect, but compatible with old wxWindows behaviour |
| 460 | lf.lfHeight = (pointSize*ppInch)/72; |
| 461 | #else // wxFONT_SIZE_COMPATIBILITY |
| 462 | // FIXME: using the screen here results in incorrect font size calculation |
| 463 | // for printing! |
| 464 | const int ppInch = ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY); |
| 465 | |
| 466 | lf.lfHeight = -(int)((pointsize*((double)ppInch)/72.0) + 0.5); |
| 467 | #endif // wxFONT_SIZE_COMPATIBILITY/!wxFONT_SIZE_COMPATIBILITY |
| 468 | } |
| 469 | |
| 470 | void wxNativeFontInfo::SetStyle(wxFontStyle style) |
| 471 | { |
| 472 | switch ( style ) |
| 473 | { |
| 474 | default: |
| 475 | wxFAIL_MSG( _T("unknown font style") ); |
| 476 | // fall through |
| 477 | |
| 478 | case wxFONTSTYLE_NORMAL: |
| 479 | lf.lfItalic = FALSE; |
| 480 | break; |
| 481 | |
| 482 | case wxFONTSTYLE_ITALIC: |
| 483 | case wxFONTSTYLE_SLANT: |
| 484 | lf.lfItalic = TRUE; |
| 485 | break; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | void wxNativeFontInfo::SetWeight(wxFontWeight weight) |
| 490 | { |
| 491 | switch ( weight ) |
| 492 | { |
| 493 | default: |
| 494 | wxFAIL_MSG( _T("unknown font weight") ); |
| 495 | // fall through |
| 496 | |
| 497 | case wxFONTWEIGHT_NORMAL: |
| 498 | lf.lfWeight = FW_NORMAL; |
| 499 | break; |
| 500 | |
| 501 | case wxFONTWEIGHT_LIGHT: |
| 502 | lf.lfWeight = FW_LIGHT; |
| 503 | break; |
| 504 | |
| 505 | case wxFONTWEIGHT_BOLD: |
| 506 | lf.lfWeight = FW_BOLD; |
| 507 | break; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | void wxNativeFontInfo::SetUnderlined(bool underlined) |
| 512 | { |
| 513 | lf.lfUnderline = underlined; |
| 514 | } |
| 515 | |
| 516 | void wxNativeFontInfo::SetFaceName(wxString facename) |
| 517 | { |
| 518 | wxStrncpy(lf.lfFaceName, facename, WXSIZEOF(lf.lfFaceName)); |
| 519 | } |
| 520 | |
| 521 | void wxNativeFontInfo::SetFamily(wxFontFamily family) |
| 522 | { |
| 523 | int ff_family; |
| 524 | wxString facename; |
| 525 | |
| 526 | switch ( family ) |
| 527 | { |
| 528 | case wxSCRIPT: |
| 529 | ff_family = FF_SCRIPT; |
| 530 | facename = _T("Script"); |
| 531 | break; |
| 532 | |
| 533 | case wxDECORATIVE: |
| 534 | ff_family = FF_DECORATIVE; |
| 535 | facename = _T("Old English Text MT"); |
| 536 | break; |
| 537 | |
| 538 | case wxROMAN: |
| 539 | ff_family = FF_ROMAN; |
| 540 | facename = _T("Times New Roman"); |
| 541 | break; |
| 542 | |
| 543 | case wxTELETYPE: |
| 544 | case wxMODERN: |
| 545 | ff_family = FF_MODERN; |
| 546 | facename = _T("Courier New"); |
| 547 | break; |
| 548 | |
| 549 | case wxSWISS: |
| 550 | ff_family = FF_SWISS; |
| 551 | facename = _T("Arial"); |
| 552 | break; |
| 553 | |
| 554 | case wxDEFAULT: |
| 555 | default: |
| 556 | ff_family = FF_SWISS; |
| 557 | facename = _T("MS Sans Serif"); |
| 558 | } |
| 559 | |
| 560 | lf.lfPitchAndFamily = DEFAULT_PITCH | ff_family; |
| 561 | |
| 562 | if ( !wxStrlen(lf.lfFaceName) ) |
| 563 | { |
| 564 | SetFaceName(facename); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | void wxNativeFontInfo::SetEncoding(wxFontEncoding encoding) |
| 569 | { |
| 570 | wxNativeEncodingInfo info; |
| 571 | if ( !wxGetNativeFontEncoding(encoding, &info) ) |
| 572 | { |
| 573 | #if wxUSE_FONTMAP |
| 574 | if ( wxFontMapper::Get()->GetAltForEncoding(encoding, &info) ) |
| 575 | { |
| 576 | if ( !info.facename.empty() ) |
| 577 | { |
| 578 | // if we have this encoding only in some particular facename, use |
| 579 | // the facename - it is better to show the correct characters in a |
| 580 | // wrong facename than unreadable text in a correct one |
| 581 | SetFaceName(info.facename); |
| 582 | } |
| 583 | } |
| 584 | else |
| 585 | #endif // wxUSE_FONTMAP |
| 586 | { |
| 587 | // unsupported encoding, replace with the default |
| 588 | info.charset = DEFAULT_CHARSET; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | lf.lfCharSet = info.charset; |
| 593 | } |
| 594 | |
| 595 | bool wxNativeFontInfo::FromString(const wxString& s) |
| 596 | { |
| 597 | long l; |
| 598 | |
| 599 | wxStringTokenizer tokenizer(s, _T(";")); |
| 600 | |
| 601 | // first the version |
| 602 | wxString token = tokenizer.GetNextToken(); |
| 603 | if ( token != _T('0') ) |
| 604 | return FALSE; |
| 605 | |
| 606 | token = tokenizer.GetNextToken(); |
| 607 | if ( !token.ToLong(&l) ) |
| 608 | return FALSE; |
| 609 | lf.lfHeight = l; |
| 610 | |
| 611 | token = tokenizer.GetNextToken(); |
| 612 | if ( !token.ToLong(&l) ) |
| 613 | return FALSE; |
| 614 | lf.lfWidth = l; |
| 615 | |
| 616 | token = tokenizer.GetNextToken(); |
| 617 | if ( !token.ToLong(&l) ) |
| 618 | return FALSE; |
| 619 | lf.lfEscapement = l; |
| 620 | |
| 621 | token = tokenizer.GetNextToken(); |
| 622 | if ( !token.ToLong(&l) ) |
| 623 | return FALSE; |
| 624 | lf.lfOrientation = l; |
| 625 | |
| 626 | token = tokenizer.GetNextToken(); |
| 627 | if ( !token.ToLong(&l) ) |
| 628 | return FALSE; |
| 629 | lf.lfWeight = l; |
| 630 | |
| 631 | token = tokenizer.GetNextToken(); |
| 632 | if ( !token.ToLong(&l) ) |
| 633 | return FALSE; |
| 634 | lf.lfItalic = (BYTE)l; |
| 635 | |
| 636 | token = tokenizer.GetNextToken(); |
| 637 | if ( !token.ToLong(&l) ) |
| 638 | return FALSE; |
| 639 | lf.lfUnderline = (BYTE)l; |
| 640 | |
| 641 | token = tokenizer.GetNextToken(); |
| 642 | if ( !token.ToLong(&l) ) |
| 643 | return FALSE; |
| 644 | lf.lfStrikeOut = (BYTE)l; |
| 645 | |
| 646 | token = tokenizer.GetNextToken(); |
| 647 | if ( !token.ToLong(&l) ) |
| 648 | return FALSE; |
| 649 | lf.lfCharSet = (BYTE)l; |
| 650 | |
| 651 | token = tokenizer.GetNextToken(); |
| 652 | if ( !token.ToLong(&l) ) |
| 653 | return FALSE; |
| 654 | lf.lfOutPrecision = (BYTE)l; |
| 655 | |
| 656 | token = tokenizer.GetNextToken(); |
| 657 | if ( !token.ToLong(&l) ) |
| 658 | return FALSE; |
| 659 | lf.lfClipPrecision = (BYTE)l; |
| 660 | |
| 661 | token = tokenizer.GetNextToken(); |
| 662 | if ( !token.ToLong(&l) ) |
| 663 | return FALSE; |
| 664 | lf.lfQuality = (BYTE)l; |
| 665 | |
| 666 | token = tokenizer.GetNextToken(); |
| 667 | if ( !token.ToLong(&l) ) |
| 668 | return FALSE; |
| 669 | lf.lfPitchAndFamily = (BYTE)l; |
| 670 | |
| 671 | token = tokenizer.GetNextToken(); |
| 672 | if(!token) |
| 673 | return FALSE; |
| 674 | wxStrcpy(lf.lfFaceName, token.c_str()); |
| 675 | |
| 676 | return TRUE; |
| 677 | } |
| 678 | |
| 679 | wxString wxNativeFontInfo::ToString() const |
| 680 | { |
| 681 | wxString s; |
| 682 | |
| 683 | s.Printf(_T("%d;%ld;%ld;%ld;%ld;%ld;%d;%d;%d;%d;%d;%d;%d;%d;%s"), |
| 684 | 0, // version, in case we want to change the format later |
| 685 | lf.lfHeight, |
| 686 | lf.lfWidth, |
| 687 | lf.lfEscapement, |
| 688 | lf.lfOrientation, |
| 689 | lf.lfWeight, |
| 690 | lf.lfItalic, |
| 691 | lf.lfUnderline, |
| 692 | lf.lfStrikeOut, |
| 693 | lf.lfCharSet, |
| 694 | lf.lfOutPrecision, |
| 695 | lf.lfClipPrecision, |
| 696 | lf.lfQuality, |
| 697 | lf.lfPitchAndFamily, |
| 698 | lf.lfFaceName); |
| 699 | |
| 700 | return s; |
| 701 | } |
| 702 | |
| 703 | // ---------------------------------------------------------------------------- |
| 704 | // wxFont |
| 705 | // ---------------------------------------------------------------------------- |
| 706 | |
| 707 | void wxFont::Init() |
| 708 | { |
| 709 | } |
| 710 | |
| 711 | bool wxFont::Create(const wxNativeFontInfo& info, WXHFONT hFont) |
| 712 | { |
| 713 | UnRef(); |
| 714 | |
| 715 | m_refData = new wxFontRefData(info, hFont); |
| 716 | |
| 717 | RealizeResource(); |
| 718 | |
| 719 | return TRUE; |
| 720 | } |
| 721 | |
| 722 | wxFont::wxFont(const wxString& fontdesc) |
| 723 | { |
| 724 | wxNativeFontInfo info; |
| 725 | if ( info.FromString(fontdesc) ) |
| 726 | (void)Create(info); |
| 727 | } |
| 728 | |
| 729 | /* Constructor for a font. Note that the real construction is done |
| 730 | * in wxDC::SetFont, when information is available about scaling etc. |
| 731 | */ |
| 732 | bool wxFont::Create(int pointSize, |
| 733 | int family, |
| 734 | int style, |
| 735 | int weight, |
| 736 | bool underlined, |
| 737 | const wxString& faceName, |
| 738 | wxFontEncoding encoding) |
| 739 | { |
| 740 | UnRef(); |
| 741 | |
| 742 | // wxDEFAULT is a valid value for the font size too so we must treat it |
| 743 | // specially here (otherwise the size would be 70 == wxDEFAULT value) |
| 744 | if ( pointSize == wxDEFAULT ) |
| 745 | { |
| 746 | pointSize = wxNORMAL_FONT->GetPointSize(); |
| 747 | } |
| 748 | |
| 749 | m_refData = new wxFontRefData(pointSize, family, style, weight, |
| 750 | underlined, faceName, encoding); |
| 751 | |
| 752 | RealizeResource(); |
| 753 | |
| 754 | return TRUE; |
| 755 | } |
| 756 | |
| 757 | wxFont::~wxFont() |
| 758 | { |
| 759 | } |
| 760 | |
| 761 | // ---------------------------------------------------------------------------- |
| 762 | // real implementation |
| 763 | // ---------------------------------------------------------------------------- |
| 764 | |
| 765 | bool wxFont::RealizeResource() |
| 766 | { |
| 767 | if ( GetResourceHandle() ) |
| 768 | { |
| 769 | // VZ: the old code returned FALSE in this case, but it doesn't seem |
| 770 | // to make sense because the font _was_ created |
| 771 | return TRUE; |
| 772 | } |
| 773 | |
| 774 | return M_FONTDATA->Alloc(this); |
| 775 | } |
| 776 | |
| 777 | bool wxFont::FreeResource(bool WXUNUSED(force)) |
| 778 | { |
| 779 | if ( GetResourceHandle() ) |
| 780 | { |
| 781 | M_FONTDATA->Free(); |
| 782 | |
| 783 | return TRUE; |
| 784 | } |
| 785 | |
| 786 | return FALSE; |
| 787 | } |
| 788 | |
| 789 | WXHANDLE wxFont::GetResourceHandle() const |
| 790 | { |
| 791 | return (WXHANDLE)GetHFONT(); |
| 792 | } |
| 793 | |
| 794 | WXHFONT wxFont::GetHFONT() const |
| 795 | { |
| 796 | return M_FONTDATA ? M_FONTDATA->GetHFONT() : 0; |
| 797 | } |
| 798 | |
| 799 | bool wxFont::IsFree() const |
| 800 | { |
| 801 | return M_FONTDATA && (M_FONTDATA->GetHFONT() == 0); |
| 802 | } |
| 803 | |
| 804 | void wxFont::Unshare() |
| 805 | { |
| 806 | // Don't change shared data |
| 807 | if ( !m_refData ) |
| 808 | { |
| 809 | m_refData = new wxFontRefData(); |
| 810 | } |
| 811 | else |
| 812 | { |
| 813 | wxFontRefData* ref = new wxFontRefData(*M_FONTDATA); |
| 814 | UnRef(); |
| 815 | m_refData = ref; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | // ---------------------------------------------------------------------------- |
| 820 | // change font attribute: we recreate font when doing it |
| 821 | // ---------------------------------------------------------------------------- |
| 822 | |
| 823 | void wxFont::SetPointSize(int pointSize) |
| 824 | { |
| 825 | Unshare(); |
| 826 | |
| 827 | M_FONTDATA->SetPointSize(pointSize); |
| 828 | |
| 829 | RealizeResource(); |
| 830 | } |
| 831 | |
| 832 | void wxFont::SetFamily(int family) |
| 833 | { |
| 834 | Unshare(); |
| 835 | |
| 836 | M_FONTDATA->SetFamily(family); |
| 837 | |
| 838 | RealizeResource(); |
| 839 | } |
| 840 | |
| 841 | void wxFont::SetStyle(int style) |
| 842 | { |
| 843 | Unshare(); |
| 844 | |
| 845 | M_FONTDATA->SetStyle(style); |
| 846 | |
| 847 | RealizeResource(); |
| 848 | } |
| 849 | |
| 850 | void wxFont::SetWeight(int weight) |
| 851 | { |
| 852 | Unshare(); |
| 853 | |
| 854 | M_FONTDATA->SetWeight(weight); |
| 855 | |
| 856 | RealizeResource(); |
| 857 | } |
| 858 | |
| 859 | void wxFont::SetFaceName(const wxString& faceName) |
| 860 | { |
| 861 | Unshare(); |
| 862 | |
| 863 | M_FONTDATA->SetFaceName(faceName); |
| 864 | |
| 865 | RealizeResource(); |
| 866 | } |
| 867 | |
| 868 | void wxFont::SetUnderlined(bool underlined) |
| 869 | { |
| 870 | Unshare(); |
| 871 | |
| 872 | M_FONTDATA->SetUnderlined(underlined); |
| 873 | |
| 874 | RealizeResource(); |
| 875 | } |
| 876 | |
| 877 | void wxFont::SetEncoding(wxFontEncoding encoding) |
| 878 | { |
| 879 | Unshare(); |
| 880 | |
| 881 | M_FONTDATA->SetEncoding(encoding); |
| 882 | |
| 883 | RealizeResource(); |
| 884 | } |
| 885 | |
| 886 | void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo& info) |
| 887 | { |
| 888 | Unshare(); |
| 889 | |
| 890 | FreeResource(); |
| 891 | |
| 892 | *M_FONTDATA = wxFontRefData(info); |
| 893 | |
| 894 | RealizeResource(); |
| 895 | } |
| 896 | |
| 897 | // ---------------------------------------------------------------------------- |
| 898 | // accessors |
| 899 | // ---------------------------------------------------------------------------- |
| 900 | |
| 901 | int wxFont::GetPointSize() const |
| 902 | { |
| 903 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
| 904 | |
| 905 | return M_FONTDATA->GetPointSize(); |
| 906 | } |
| 907 | |
| 908 | int wxFont::GetFamily() const |
| 909 | { |
| 910 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
| 911 | |
| 912 | return M_FONTDATA->GetFamily(); |
| 913 | } |
| 914 | |
| 915 | int wxFont::GetStyle() const |
| 916 | { |
| 917 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
| 918 | |
| 919 | return M_FONTDATA->GetStyle(); |
| 920 | } |
| 921 | |
| 922 | int wxFont::GetWeight() const |
| 923 | { |
| 924 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
| 925 | |
| 926 | return M_FONTDATA->GetWeight(); |
| 927 | } |
| 928 | |
| 929 | bool wxFont::GetUnderlined() const |
| 930 | { |
| 931 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") ); |
| 932 | |
| 933 | return M_FONTDATA->GetUnderlined(); |
| 934 | } |
| 935 | |
| 936 | wxString wxFont::GetFaceName() const |
| 937 | { |
| 938 | wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid font") ); |
| 939 | |
| 940 | return M_FONTDATA->GetFaceName(); |
| 941 | } |
| 942 | |
| 943 | wxFontEncoding wxFont::GetEncoding() const |
| 944 | { |
| 945 | wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") ); |
| 946 | |
| 947 | return M_FONTDATA->GetEncoding(); |
| 948 | } |
| 949 | |
| 950 | wxNativeFontInfo *wxFont::GetNativeFontInfo() const |
| 951 | { |
| 952 | if ( M_FONTDATA->HasNativeFontInfo() ) |
| 953 | return new wxNativeFontInfo(M_FONTDATA->GetNativeFontInfo()); |
| 954 | |
| 955 | return 0; |
| 956 | } |
| 957 | |
| 958 | bool wxFont::IsFixedWidth() const |
| 959 | { |
| 960 | if ( M_FONTDATA->HasNativeFontInfo() ) |
| 961 | { |
| 962 | // the two low-order bits specify the pitch of the font, the rest is |
| 963 | // family |
| 964 | BYTE pitch = M_FONTDATA->GetNativeFontInfo(). |
| 965 | lf.lfPitchAndFamily & PITCH_MASK; |
| 966 | |
| 967 | return pitch == FIXED_PITCH; |
| 968 | } |
| 969 | |
| 970 | return wxFontBase::IsFixedWidth(); |
| 971 | } |
| 972 | |