| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/x11/font.cpp |
| 3 | // Purpose: wxFont class |
| 4 | // Author: Julian Smart |
| 5 | // Modified by: |
| 6 | // Created: 17/09/98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Julian Smart |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // declarations |
| 14 | // ============================================================================ |
| 15 | |
| 16 | // ---------------------------------------------------------------------------- |
| 17 | // headers |
| 18 | // ---------------------------------------------------------------------------- |
| 19 | |
| 20 | #ifdef __VMS |
| 21 | #pragma message disable nosimpint |
| 22 | #include "wx/vms_x_fix.h" |
| 23 | #endif |
| 24 | |
| 25 | #ifdef __VMS |
| 26 | #pragma message enable nosimpint |
| 27 | #endif |
| 28 | |
| 29 | #include "wx/defs.h" |
| 30 | #include "wx/string.h" |
| 31 | #include "wx/font.h" |
| 32 | #include "wx/gdicmn.h" |
| 33 | #include "wx/utils.h" // for wxGetDisplay() |
| 34 | #include "wx/fontutil.h" // for wxNativeFontInfo |
| 35 | #include "wx/tokenzr.h" |
| 36 | #include "wx/settings.h" |
| 37 | |
| 38 | #include "wx/x11/private.h" |
| 39 | |
| 40 | IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject) |
| 41 | |
| 42 | // ---------------------------------------------------------------------------- |
| 43 | // constants |
| 44 | // ---------------------------------------------------------------------------- |
| 45 | |
| 46 | // the default size (in points) for the fonts |
| 47 | static const int wxDEFAULT_FONT_SIZE = 12; |
| 48 | |
| 49 | |
| 50 | #if wxUSE_UNICODE |
| 51 | #else |
| 52 | // ---------------------------------------------------------------------------- |
| 53 | // wxXFont |
| 54 | // ---------------------------------------------------------------------------- |
| 55 | |
| 56 | // For every wxFont, there must be a font for each display and scale requested. |
| 57 | // So these objects are stored in wxFontRefData::m_fonts |
| 58 | class wxXFont : public wxObject |
| 59 | { |
| 60 | public: |
| 61 | wxXFont(); |
| 62 | ~wxXFont(); |
| 63 | |
| 64 | WXFontStructPtr m_fontStruct; // XFontStruct |
| 65 | WXDisplay* m_display; // XDisplay |
| 66 | int m_scale; // Scale * 100 |
| 67 | }; |
| 68 | |
| 69 | wxXFont::wxXFont() |
| 70 | { |
| 71 | m_fontStruct = (WXFontStructPtr) 0; |
| 72 | m_display = (WXDisplay*) 0; |
| 73 | m_scale = 100; |
| 74 | } |
| 75 | |
| 76 | wxXFont::~wxXFont() |
| 77 | { |
| 78 | // Freeing the font used to produce a segv, but |
| 79 | // appears to be OK now (bug fix in X11?) |
| 80 | XFontStruct* fontStruct = (XFontStruct*) m_fontStruct; |
| 81 | XFreeFont((Display*) m_display, fontStruct); |
| 82 | } |
| 83 | #endif |
| 84 | |
| 85 | // ---------------------------------------------------------------------------- |
| 86 | // wxFontRefData |
| 87 | // ---------------------------------------------------------------------------- |
| 88 | |
| 89 | class wxFontRefData: public wxObjectRefData |
| 90 | { |
| 91 | friend class wxFont; |
| 92 | |
| 93 | public: |
| 94 | wxFontRefData(int size = wxDEFAULT, |
| 95 | int family = wxDEFAULT, |
| 96 | int style = wxDEFAULT, |
| 97 | int weight = wxDEFAULT, |
| 98 | bool underlined = FALSE, |
| 99 | const wxString& faceName = wxEmptyString, |
| 100 | wxFontEncoding encoding = wxFONTENCODING_DEFAULT); |
| 101 | |
| 102 | // copy cstr |
| 103 | wxFontRefData(const wxFontRefData& data); |
| 104 | |
| 105 | // from XFLD |
| 106 | wxFontRefData(const wxString& fontname); |
| 107 | |
| 108 | // dstr |
| 109 | virtual ~wxFontRefData(); |
| 110 | |
| 111 | // setters: all of them also take care to modify m_nativeFontInfo if we |
| 112 | // have it so as to not lose the information not carried by our fields |
| 113 | void SetPointSize(int pointSize); |
| 114 | void SetFamily(int family); |
| 115 | void SetStyle(int style); |
| 116 | void SetWeight(int weight); |
| 117 | void SetUnderlined(bool underlined); |
| 118 | void SetFaceName(const wxString& facename); |
| 119 | void SetEncoding(wxFontEncoding encoding); |
| 120 | |
| 121 | void SetNoAntiAliasing( bool no = TRUE ) { m_noAA = no; } |
| 122 | bool GetNoAntiAliasing() const { return m_noAA; } |
| 123 | |
| 124 | // and this one also modifies all the other font data fields |
| 125 | void SetNativeFontInfo(const wxNativeFontInfo& info); |
| 126 | |
| 127 | protected: |
| 128 | // common part of all ctors |
| 129 | void Init(int size, |
| 130 | int family, |
| 131 | int style, |
| 132 | int weight, |
| 133 | bool underlined, |
| 134 | const wxString& faceName, |
| 135 | wxFontEncoding encoding); |
| 136 | |
| 137 | // set all fields from (already initialized and valid) m_nativeFontInfo |
| 138 | void InitFromNative(); |
| 139 | |
| 140 | // font attributes |
| 141 | int m_pointSize; |
| 142 | int m_family; |
| 143 | int m_style; |
| 144 | int m_weight; |
| 145 | bool m_underlined; |
| 146 | wxString m_faceName; |
| 147 | wxFontEncoding m_encoding; // Unused in Unicode mode |
| 148 | bool m_noAA; // No anti-aliasing |
| 149 | |
| 150 | wxNativeFontInfo m_nativeFontInfo; |
| 151 | |
| 152 | void ClearX11Fonts(); |
| 153 | |
| 154 | #if wxUSE_UNICODE |
| 155 | #else |
| 156 | // A list of wxXFonts |
| 157 | wxList m_fonts; |
| 158 | #endif |
| 159 | }; |
| 160 | |
| 161 | // ---------------------------------------------------------------------------- |
| 162 | // wxFontRefData |
| 163 | // ---------------------------------------------------------------------------- |
| 164 | |
| 165 | void wxFontRefData::Init(int pointSize, |
| 166 | int family, |
| 167 | int style, |
| 168 | int weight, |
| 169 | bool underlined, |
| 170 | const wxString& faceName, |
| 171 | wxFontEncoding encoding) |
| 172 | { |
| 173 | m_family = family == wxFONTFAMILY_DEFAULT ? wxFONTFAMILY_SWISS : family; |
| 174 | |
| 175 | m_faceName = faceName; |
| 176 | |
| 177 | // we accept both wxDEFAULT and wxNORMAL here - should we? |
| 178 | m_style = style == wxDEFAULT ? wxFONTSTYLE_NORMAL : style; |
| 179 | m_weight = weight == wxDEFAULT ? wxFONTWEIGHT_NORMAL : weight; |
| 180 | |
| 181 | // and here, do we really want to forbid creation of the font of the size |
| 182 | // 90 (the value of wxDEFAULT)?? |
| 183 | m_pointSize = pointSize == wxDEFAULT || pointSize == -1 |
| 184 | ? wxDEFAULT_FONT_SIZE |
| 185 | : pointSize; |
| 186 | |
| 187 | m_underlined = underlined; |
| 188 | m_encoding = encoding; |
| 189 | |
| 190 | #if wxUSE_UNICODE |
| 191 | // Create native font info |
| 192 | m_nativeFontInfo.description = pango_font_description_new(); |
| 193 | |
| 194 | // And set its values |
| 195 | switch (m_family) |
| 196 | { |
| 197 | case wxFONTFAMILY_MODERN: |
| 198 | case wxFONTFAMILY_TELETYPE: |
| 199 | pango_font_description_set_family( m_nativeFontInfo.description, "monospace" ); |
| 200 | break; |
| 201 | case wxFONTFAMILY_ROMAN: |
| 202 | pango_font_description_set_family( m_nativeFontInfo.description, "serif" ); |
| 203 | break; |
| 204 | default: |
| 205 | pango_font_description_set_family( m_nativeFontInfo.description, "sans" ); |
| 206 | break; |
| 207 | } |
| 208 | SetStyle( m_style ); |
| 209 | SetPointSize( m_pointSize ); |
| 210 | SetWeight( m_weight ); |
| 211 | #endif |
| 212 | } |
| 213 | |
| 214 | void wxFontRefData::InitFromNative() |
| 215 | { |
| 216 | m_noAA = FALSE; |
| 217 | |
| 218 | #if wxUSE_UNICODE |
| 219 | // Get native info |
| 220 | PangoFontDescription *desc = m_nativeFontInfo.description; |
| 221 | |
| 222 | // init fields |
| 223 | m_faceName = wxGTK_CONV_BACK( pango_font_description_get_family( desc ) ); |
| 224 | |
| 225 | m_pointSize = pango_font_description_get_size( desc ) / PANGO_SCALE; |
| 226 | |
| 227 | switch (pango_font_description_get_style( desc )) |
| 228 | { |
| 229 | case PANGO_STYLE_NORMAL: |
| 230 | m_style = wxFONTSTYLE_NORMAL; |
| 231 | break; |
| 232 | case PANGO_STYLE_ITALIC: |
| 233 | m_style = wxFONTSTYLE_ITALIC; |
| 234 | break; |
| 235 | case PANGO_STYLE_OBLIQUE: |
| 236 | m_style = wxFONTSTYLE_SLANT; |
| 237 | break; |
| 238 | } |
| 239 | |
| 240 | switch (pango_font_description_get_weight( desc )) |
| 241 | { |
| 242 | case PANGO_WEIGHT_ULTRALIGHT: |
| 243 | m_weight = wxFONTWEIGHT_LIGHT; |
| 244 | break; |
| 245 | case PANGO_WEIGHT_LIGHT: |
| 246 | m_weight = wxFONTWEIGHT_LIGHT; |
| 247 | break; |
| 248 | case PANGO_WEIGHT_NORMAL: |
| 249 | m_weight = wxFONTWEIGHT_NORMAL; |
| 250 | break; |
| 251 | case PANGO_WEIGHT_BOLD: |
| 252 | m_weight = wxFONTWEIGHT_BOLD; |
| 253 | break; |
| 254 | case PANGO_WEIGHT_ULTRABOLD: |
| 255 | m_weight = wxFONTWEIGHT_BOLD; |
| 256 | break; |
| 257 | case PANGO_WEIGHT_HEAVY: |
| 258 | m_weight = wxFONTWEIGHT_BOLD; |
| 259 | break; |
| 260 | } |
| 261 | |
| 262 | if (m_faceName == wxT("monospace")) |
| 263 | { |
| 264 | m_family = wxFONTFAMILY_TELETYPE; |
| 265 | } |
| 266 | else if (m_faceName == wxT("sans")) |
| 267 | { |
| 268 | m_family = wxFONTFAMILY_SWISS; |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | m_family = wxFONTFAMILY_UNKNOWN; |
| 273 | } |
| 274 | |
| 275 | // Pango description are never underlined (?) |
| 276 | m_underlined = FALSE; |
| 277 | |
| 278 | // Cannot we choose that |
| 279 | m_encoding = wxFONTENCODING_SYSTEM; |
| 280 | #else // X11 |
| 281 | // get the font parameters from the XLFD |
| 282 | // ------------------------------------- |
| 283 | |
| 284 | m_faceName = m_nativeFontInfo.GetXFontComponent(wxXLFD_FAMILY); |
| 285 | |
| 286 | m_weight = wxFONTWEIGHT_NORMAL; |
| 287 | |
| 288 | wxString w = m_nativeFontInfo.GetXFontComponent(wxXLFD_WEIGHT).Upper(); |
| 289 | if ( !w.empty() && w != _T('*') ) |
| 290 | { |
| 291 | // the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD |
| 292 | // and BLACK |
| 293 | if ( ((w[0u] == _T('B') && (!wxStrcmp(w.c_str() + 1, wxT("OLD")) || |
| 294 | !wxStrcmp(w.c_str() + 1, wxT("LACK"))))) || |
| 295 | wxStrstr(w.c_str() + 1, _T("BOLD")) ) |
| 296 | { |
| 297 | m_weight = wxFONTWEIGHT_BOLD; |
| 298 | } |
| 299 | else if ( w == _T("LIGHT") || w == _T("THIN") ) |
| 300 | { |
| 301 | m_weight = wxFONTWEIGHT_LIGHT; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | switch ( wxToupper(*m_nativeFontInfo. |
| 306 | GetXFontComponent(wxXLFD_SLANT).c_str()) ) |
| 307 | { |
| 308 | case _T('I'): // italique |
| 309 | m_style = wxFONTSTYLE_ITALIC; |
| 310 | break; |
| 311 | |
| 312 | case _T('O'): // oblique |
| 313 | m_style = wxFONTSTYLE_SLANT; |
| 314 | break; |
| 315 | |
| 316 | default: |
| 317 | m_style = wxFONTSTYLE_NORMAL; |
| 318 | } |
| 319 | |
| 320 | long ptSize; |
| 321 | if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_POINTSIZE).ToLong(&ptSize) ) |
| 322 | { |
| 323 | // size in XLFD is in 10 point units |
| 324 | m_pointSize = (int)(ptSize / 10); |
| 325 | } |
| 326 | else |
| 327 | { |
| 328 | m_pointSize = wxDEFAULT_FONT_SIZE; |
| 329 | } |
| 330 | |
| 331 | // examine the spacing: if the font is monospaced, assume wxTELETYPE |
| 332 | // family for compatibility with the old code which used it instead of |
| 333 | // IsFixedWidth() |
| 334 | if ( m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING).Upper() == _T('M') ) |
| 335 | { |
| 336 | m_family = wxFONTFAMILY_TELETYPE; |
| 337 | } |
| 338 | else // not monospaceed |
| 339 | { |
| 340 | // don't even try guessing it, it doesn't work for too many fonts |
| 341 | // anyhow |
| 342 | m_family = wxFONTFAMILY_UNKNOWN; |
| 343 | } |
| 344 | |
| 345 | // X fonts are never underlined... |
| 346 | m_underlined = FALSE; |
| 347 | |
| 348 | // deal with font encoding |
| 349 | wxString |
| 350 | registry = m_nativeFontInfo.GetXFontComponent(wxXLFD_REGISTRY).Upper(), |
| 351 | encoding = m_nativeFontInfo.GetXFontComponent(wxXLFD_ENCODING).Upper(); |
| 352 | |
| 353 | if ( registry == _T("ISO8859") ) |
| 354 | { |
| 355 | int cp; |
| 356 | if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 ) |
| 357 | { |
| 358 | m_encoding = (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1); |
| 359 | } |
| 360 | } |
| 361 | else if ( registry == _T("MICROSOFT") ) |
| 362 | { |
| 363 | int cp; |
| 364 | if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 ) |
| 365 | { |
| 366 | m_encoding = (wxFontEncoding)(wxFONTENCODING_CP1250 + cp); |
| 367 | } |
| 368 | } |
| 369 | else if ( registry == _T("KOI8") ) |
| 370 | { |
| 371 | m_encoding = wxFONTENCODING_KOI8; |
| 372 | } |
| 373 | else // unknown encoding |
| 374 | { |
| 375 | // may be give a warning here? or use wxFontMapper? |
| 376 | m_encoding = wxFONTENCODING_SYSTEM; |
| 377 | } |
| 378 | #endif // Pango/X11 |
| 379 | } |
| 380 | |
| 381 | wxFontRefData::wxFontRefData( const wxFontRefData& data ) |
| 382 | : wxObjectRefData() |
| 383 | { |
| 384 | m_pointSize = data.m_pointSize; |
| 385 | m_family = data.m_family; |
| 386 | m_style = data.m_style; |
| 387 | m_weight = data.m_weight; |
| 388 | |
| 389 | m_underlined = data.m_underlined; |
| 390 | |
| 391 | m_faceName = data.m_faceName; |
| 392 | m_encoding = data.m_encoding; |
| 393 | |
| 394 | m_noAA = data.m_noAA; |
| 395 | |
| 396 | m_nativeFontInfo = data.m_nativeFontInfo; |
| 397 | } |
| 398 | |
| 399 | wxFontRefData::wxFontRefData(int size, int family, int style, |
| 400 | int weight, bool underlined, |
| 401 | const wxString& faceName, |
| 402 | wxFontEncoding encoding) |
| 403 | { |
| 404 | Init(size, family, style, weight, underlined, faceName, encoding); |
| 405 | } |
| 406 | |
| 407 | wxFontRefData::wxFontRefData(const wxString& fontname) |
| 408 | { |
| 409 | // VZ: FromString() should really work in both cases, doesn't it? |
| 410 | #if wxUSE_UNICODE |
| 411 | m_nativeFontInfo.FromString( fontname ); |
| 412 | #else |
| 413 | m_nativeFontInfo.SetXFontName(fontname); |
| 414 | #endif |
| 415 | |
| 416 | InitFromNative(); |
| 417 | } |
| 418 | |
| 419 | void wxFontRefData::ClearX11Fonts() |
| 420 | { |
| 421 | #if wxUSE_UNICODE |
| 422 | #else |
| 423 | wxList::compatibility_iterator node = m_fonts.GetFirst(); |
| 424 | while (node) |
| 425 | { |
| 426 | wxXFont* f = (wxXFont*) node->GetData(); |
| 427 | delete f; |
| 428 | node = node->GetNext(); |
| 429 | } |
| 430 | m_fonts.Clear(); |
| 431 | #endif |
| 432 | } |
| 433 | |
| 434 | wxFontRefData::~wxFontRefData() |
| 435 | { |
| 436 | ClearX11Fonts(); |
| 437 | } |
| 438 | |
| 439 | // ---------------------------------------------------------------------------- |
| 440 | // wxFontRefData SetXXX() |
| 441 | // ---------------------------------------------------------------------------- |
| 442 | |
| 443 | void wxFontRefData::SetPointSize(int pointSize) |
| 444 | { |
| 445 | m_pointSize = pointSize; |
| 446 | |
| 447 | #if wxUSE_UNICODE |
| 448 | // Get native info |
| 449 | PangoFontDescription *desc = m_nativeFontInfo.description; |
| 450 | |
| 451 | pango_font_description_set_size( desc, m_pointSize * PANGO_SCALE ); |
| 452 | #endif |
| 453 | } |
| 454 | |
| 455 | void wxFontRefData::SetFamily(int family) |
| 456 | { |
| 457 | m_family = family; |
| 458 | |
| 459 | // TODO: what are we supposed to do with m_nativeFontInfo here? |
| 460 | } |
| 461 | |
| 462 | void wxFontRefData::SetStyle(int style) |
| 463 | { |
| 464 | m_style = style; |
| 465 | |
| 466 | #if wxUSE_UNICODE |
| 467 | // Get native info |
| 468 | PangoFontDescription *desc = m_nativeFontInfo.description; |
| 469 | |
| 470 | switch ( style ) |
| 471 | { |
| 472 | case wxFONTSTYLE_ITALIC: |
| 473 | pango_font_description_set_style( desc, PANGO_STYLE_ITALIC ); |
| 474 | break; |
| 475 | case wxFONTSTYLE_SLANT: |
| 476 | pango_font_description_set_style( desc, PANGO_STYLE_OBLIQUE ); |
| 477 | break; |
| 478 | default: |
| 479 | wxFAIL_MSG( _T("unknown font style") ); |
| 480 | // fall through |
| 481 | case wxFONTSTYLE_NORMAL: |
| 482 | pango_font_description_set_style( desc, PANGO_STYLE_NORMAL ); |
| 483 | break; |
| 484 | } |
| 485 | #endif |
| 486 | } |
| 487 | |
| 488 | void wxFontRefData::SetWeight(int weight) |
| 489 | { |
| 490 | m_weight = weight; |
| 491 | } |
| 492 | |
| 493 | void wxFontRefData::SetUnderlined(bool underlined) |
| 494 | { |
| 495 | m_underlined = underlined; |
| 496 | |
| 497 | // the XLFD doesn't have "underlined" field anyhow |
| 498 | } |
| 499 | |
| 500 | void wxFontRefData::SetFaceName(const wxString& facename) |
| 501 | { |
| 502 | m_faceName = facename; |
| 503 | } |
| 504 | |
| 505 | void wxFontRefData::SetEncoding(wxFontEncoding encoding) |
| 506 | { |
| 507 | m_encoding = encoding; |
| 508 | } |
| 509 | |
| 510 | void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo& info) |
| 511 | { |
| 512 | // previously cached fonts shouldn't be used |
| 513 | ClearX11Fonts(); |
| 514 | |
| 515 | m_nativeFontInfo = info; |
| 516 | |
| 517 | // set all the other font parameters from the native font info |
| 518 | InitFromNative(); |
| 519 | } |
| 520 | |
| 521 | // ---------------------------------------------------------------------------- |
| 522 | // wxFont |
| 523 | // ---------------------------------------------------------------------------- |
| 524 | |
| 525 | wxFont::wxFont(const wxNativeFontInfo& info) |
| 526 | { |
| 527 | #if wxUSE_UNICODE |
| 528 | Create( info.GetPointSize(), |
| 529 | info.GetFamily(), |
| 530 | info.GetStyle(), |
| 531 | info.GetWeight(), |
| 532 | info.GetUnderlined(), |
| 533 | info.GetFaceName(), |
| 534 | info.GetEncoding() ); |
| 535 | #else |
| 536 | (void) Create(info.GetXFontName()); |
| 537 | #endif |
| 538 | } |
| 539 | |
| 540 | bool wxFont::Create(int pointSize, |
| 541 | int family, |
| 542 | int style, |
| 543 | int weight, |
| 544 | bool underlined, |
| 545 | const wxString& faceName, |
| 546 | wxFontEncoding encoding) |
| 547 | { |
| 548 | UnRef(); |
| 549 | |
| 550 | m_refData = new wxFontRefData(pointSize, family, style, weight, |
| 551 | underlined, faceName, encoding); |
| 552 | |
| 553 | return TRUE; |
| 554 | } |
| 555 | |
| 556 | #if !wxUSE_UNICODE |
| 557 | |
| 558 | bool wxFont::Create(const wxString& fontname, wxFontEncoding enc) |
| 559 | { |
| 560 | if( !fontname ) |
| 561 | { |
| 562 | *this = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT); |
| 563 | return TRUE; |
| 564 | } |
| 565 | |
| 566 | m_refData = new wxFontRefData(); |
| 567 | |
| 568 | M_FONTDATA->m_nativeFontInfo.SetXFontName(fontname); // X font name |
| 569 | |
| 570 | wxString tmp; |
| 571 | |
| 572 | wxStringTokenizer tn( fontname, wxT("-") ); |
| 573 | |
| 574 | tn.GetNextToken(); // skip initial empty token |
| 575 | tn.GetNextToken(); // foundry |
| 576 | |
| 577 | |
| 578 | M_FONTDATA->m_faceName = tn.GetNextToken(); // family |
| 579 | |
| 580 | tmp = tn.GetNextToken().MakeUpper(); // weight |
| 581 | if (tmp == wxT("BOLD")) M_FONTDATA->m_weight = wxBOLD; |
| 582 | if (tmp == wxT("BLACK")) M_FONTDATA->m_weight = wxBOLD; |
| 583 | if (tmp == wxT("EXTRABOLD")) M_FONTDATA->m_weight = wxBOLD; |
| 584 | if (tmp == wxT("DEMIBOLD")) M_FONTDATA->m_weight = wxBOLD; |
| 585 | if (tmp == wxT("ULTRABOLD")) M_FONTDATA->m_weight = wxBOLD; |
| 586 | |
| 587 | if (tmp == wxT("LIGHT")) M_FONTDATA->m_weight = wxLIGHT; |
| 588 | if (tmp == wxT("THIN")) M_FONTDATA->m_weight = wxLIGHT; |
| 589 | |
| 590 | tmp = tn.GetNextToken().MakeUpper(); // slant |
| 591 | if (tmp == wxT("I")) M_FONTDATA->m_style = wxITALIC; |
| 592 | if (tmp == wxT("O")) M_FONTDATA->m_style = wxITALIC; |
| 593 | |
| 594 | tn.GetNextToken(); // set width |
| 595 | tn.GetNextToken(); // add. style |
| 596 | tn.GetNextToken(); // pixel size |
| 597 | |
| 598 | tmp = tn.GetNextToken(); // pointsize |
| 599 | if (tmp != wxT("*")) |
| 600 | { |
| 601 | long num = wxStrtol (tmp.c_str(), (wxChar **) NULL, 10); |
| 602 | M_FONTDATA->m_pointSize = (int)(num / 10); |
| 603 | } |
| 604 | |
| 605 | tn.GetNextToken(); // x-res |
| 606 | tn.GetNextToken(); // y-res |
| 607 | |
| 608 | tmp = tn.GetNextToken().MakeUpper(); // spacing |
| 609 | |
| 610 | if (tmp == wxT("M")) |
| 611 | M_FONTDATA->m_family = wxMODERN; |
| 612 | else if (M_FONTDATA->m_faceName == wxT("TIMES")) |
| 613 | M_FONTDATA->m_family = wxROMAN; |
| 614 | else if (M_FONTDATA->m_faceName == wxT("HELVETICA")) |
| 615 | M_FONTDATA->m_family = wxSWISS; |
| 616 | else if (M_FONTDATA->m_faceName == wxT("LUCIDATYPEWRITER")) |
| 617 | M_FONTDATA->m_family = wxTELETYPE; |
| 618 | else if (M_FONTDATA->m_faceName == wxT("LUCIDA")) |
| 619 | M_FONTDATA->m_family = wxDECORATIVE; |
| 620 | else if (M_FONTDATA->m_faceName == wxT("UTOPIA")) |
| 621 | M_FONTDATA->m_family = wxSCRIPT; |
| 622 | |
| 623 | tn.GetNextToken(); // avg width |
| 624 | |
| 625 | // deal with font encoding |
| 626 | M_FONTDATA->m_encoding = enc; |
| 627 | if ( M_FONTDATA->m_encoding == wxFONTENCODING_SYSTEM ) |
| 628 | { |
| 629 | wxString registry = tn.GetNextToken().MakeUpper(), |
| 630 | encoding = tn.GetNextToken().MakeUpper(); |
| 631 | |
| 632 | if ( registry == _T("ISO8859") ) |
| 633 | { |
| 634 | int cp; |
| 635 | if ( wxSscanf(encoding, wxT("%d"), &cp) == 1 ) |
| 636 | { |
| 637 | M_FONTDATA->m_encoding = |
| 638 | (wxFontEncoding)(wxFONTENCODING_ISO8859_1 + cp - 1); |
| 639 | } |
| 640 | } |
| 641 | else if ( registry == _T("MICROSOFT") ) |
| 642 | { |
| 643 | int cp; |
| 644 | if ( wxSscanf(encoding, wxT("cp125%d"), &cp) == 1 ) |
| 645 | { |
| 646 | M_FONTDATA->m_encoding = |
| 647 | (wxFontEncoding)(wxFONTENCODING_CP1250 + cp); |
| 648 | } |
| 649 | } |
| 650 | else if ( registry == _T("KOI8") ) |
| 651 | { |
| 652 | M_FONTDATA->m_encoding = wxFONTENCODING_KOI8; |
| 653 | } |
| 654 | //else: unknown encoding - may be give a warning here? |
| 655 | else |
| 656 | return FALSE; |
| 657 | } |
| 658 | return TRUE; |
| 659 | } |
| 660 | #endif // !wxUSE_UNICODE |
| 661 | |
| 662 | wxFont::~wxFont() |
| 663 | { |
| 664 | } |
| 665 | |
| 666 | // ---------------------------------------------------------------------------- |
| 667 | // change the font attributes |
| 668 | // ---------------------------------------------------------------------------- |
| 669 | |
| 670 | void wxFont::Unshare() |
| 671 | { |
| 672 | // Don't change shared data |
| 673 | if (!m_refData) |
| 674 | { |
| 675 | m_refData = new wxFontRefData(); |
| 676 | } |
| 677 | else |
| 678 | { |
| 679 | wxFontRefData* ref = new wxFontRefData(*(wxFontRefData*)m_refData); |
| 680 | UnRef(); |
| 681 | m_refData = ref; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | // ---------------------------------------------------------------------------- |
| 686 | // accessors |
| 687 | // ---------------------------------------------------------------------------- |
| 688 | |
| 689 | int wxFont::GetPointSize() const |
| 690 | { |
| 691 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
| 692 | |
| 693 | return M_FONTDATA->m_pointSize; |
| 694 | } |
| 695 | |
| 696 | wxString wxFont::GetFaceName() const |
| 697 | { |
| 698 | wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") ); |
| 699 | |
| 700 | return M_FONTDATA->m_faceName; |
| 701 | } |
| 702 | |
| 703 | int wxFont::GetFamily() const |
| 704 | { |
| 705 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
| 706 | |
| 707 | return M_FONTDATA->m_family; |
| 708 | } |
| 709 | |
| 710 | int wxFont::GetStyle() const |
| 711 | { |
| 712 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
| 713 | |
| 714 | return M_FONTDATA->m_style; |
| 715 | } |
| 716 | |
| 717 | int wxFont::GetWeight() const |
| 718 | { |
| 719 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
| 720 | |
| 721 | return M_FONTDATA->m_weight; |
| 722 | } |
| 723 | |
| 724 | bool wxFont::GetUnderlined() const |
| 725 | { |
| 726 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") ); |
| 727 | |
| 728 | return M_FONTDATA->m_underlined; |
| 729 | } |
| 730 | |
| 731 | wxFontEncoding wxFont::GetEncoding() const |
| 732 | { |
| 733 | wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") ); |
| 734 | |
| 735 | return M_FONTDATA->m_encoding; |
| 736 | } |
| 737 | |
| 738 | bool wxFont::GetNoAntiAliasing() const |
| 739 | { |
| 740 | wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") ); |
| 741 | |
| 742 | return M_FONTDATA->m_noAA; |
| 743 | } |
| 744 | |
| 745 | const wxNativeFontInfo *wxFont::GetNativeFontInfo() const |
| 746 | { |
| 747 | wxCHECK_MSG( Ok(), (wxNativeFontInfo *)NULL, wxT("invalid font") ); |
| 748 | |
| 749 | #if wxUSE_UNICODE |
| 750 | #else |
| 751 | if ( M_FONTDATA->m_nativeFontInfo.GetXFontName().empty() ) |
| 752 | GetInternalFont(); |
| 753 | #endif |
| 754 | |
| 755 | return &(M_FONTDATA->m_nativeFontInfo); |
| 756 | } |
| 757 | |
| 758 | bool wxFont::IsFixedWidth() const |
| 759 | { |
| 760 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") ); |
| 761 | |
| 762 | #if wxUSE_UNICODE |
| 763 | return wxFontBase::IsFixedWidth(); |
| 764 | #else |
| 765 | // Robert, is this right? HasNativeFont doesn't exist. |
| 766 | if ( TRUE ) |
| 767 | // if ( M_FONTDATA->HasNativeFont() ) |
| 768 | { |
| 769 | // the monospace fonts are supposed to have "M" in the spacing field |
| 770 | wxString spacing = M_FONTDATA-> |
| 771 | m_nativeFontInfo.GetXFontComponent(wxXLFD_SPACING); |
| 772 | |
| 773 | return spacing.Upper() == _T('M'); |
| 774 | } |
| 775 | // Unreaceable code for now |
| 776 | // return wxFontBase::IsFixedWidth(); |
| 777 | #endif |
| 778 | |
| 779 | } |
| 780 | |
| 781 | // ---------------------------------------------------------------------------- |
| 782 | // change font attributes |
| 783 | // ---------------------------------------------------------------------------- |
| 784 | |
| 785 | void wxFont::SetPointSize(int pointSize) |
| 786 | { |
| 787 | Unshare(); |
| 788 | |
| 789 | M_FONTDATA->SetPointSize(pointSize); |
| 790 | } |
| 791 | |
| 792 | void wxFont::SetFamily(int family) |
| 793 | { |
| 794 | Unshare(); |
| 795 | |
| 796 | M_FONTDATA->SetFamily(family); |
| 797 | } |
| 798 | |
| 799 | void wxFont::SetStyle(int style) |
| 800 | { |
| 801 | Unshare(); |
| 802 | |
| 803 | M_FONTDATA->SetStyle(style); |
| 804 | } |
| 805 | |
| 806 | void wxFont::SetWeight(int weight) |
| 807 | { |
| 808 | Unshare(); |
| 809 | |
| 810 | M_FONTDATA->SetWeight(weight); |
| 811 | } |
| 812 | |
| 813 | void wxFont::SetFaceName(const wxString& faceName) |
| 814 | { |
| 815 | Unshare(); |
| 816 | |
| 817 | M_FONTDATA->SetFaceName(faceName); |
| 818 | } |
| 819 | |
| 820 | void wxFont::SetUnderlined(bool underlined) |
| 821 | { |
| 822 | Unshare(); |
| 823 | |
| 824 | M_FONTDATA->SetUnderlined(underlined); |
| 825 | } |
| 826 | |
| 827 | void wxFont::SetEncoding(wxFontEncoding encoding) |
| 828 | { |
| 829 | Unshare(); |
| 830 | |
| 831 | M_FONTDATA->SetEncoding(encoding); |
| 832 | } |
| 833 | |
| 834 | void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo& info ) |
| 835 | { |
| 836 | Unshare(); |
| 837 | |
| 838 | M_FONTDATA->SetNativeFontInfo( info ); |
| 839 | } |
| 840 | |
| 841 | void wxFont::SetNoAntiAliasing( bool no ) |
| 842 | { |
| 843 | Unshare(); |
| 844 | |
| 845 | M_FONTDATA->SetNoAntiAliasing( no ); |
| 846 | } |
| 847 | |
| 848 | #if wxUSE_UNICODE |
| 849 | #else |
| 850 | |
| 851 | // ---------------------------------------------------------------------------- |
| 852 | // X11 implementation |
| 853 | // ---------------------------------------------------------------------------- |
| 854 | |
| 855 | // Find an existing, or create a new, XFontStruct |
| 856 | // based on this wxFont and the given scale. Append the |
| 857 | // font to list in the private data for future reference. |
| 858 | wxXFont* wxFont::GetInternalFont(double scale, WXDisplay* display) const |
| 859 | { |
| 860 | if ( !Ok() ) |
| 861 | return (wxXFont *)NULL; |
| 862 | |
| 863 | long intScale = long(scale * 100.0 + 0.5); // key for wxXFont |
| 864 | int pointSize = (M_FONTDATA->m_pointSize * 10 * intScale) / 100; |
| 865 | |
| 866 | // search existing fonts first |
| 867 | wxList::compatibility_iterator node = M_FONTDATA->m_fonts.GetFirst(); |
| 868 | while (node) |
| 869 | { |
| 870 | wxXFont* f = (wxXFont*) node->GetData(); |
| 871 | if ((!display || (f->m_display == display)) && (f->m_scale == intScale)) |
| 872 | return f; |
| 873 | node = node->GetNext(); |
| 874 | } |
| 875 | |
| 876 | wxString xFontName = M_FONTDATA->m_nativeFontInfo.GetXFontName(); |
| 877 | if (xFontName == "-*-*-*-*-*--*-*-*-*-*-*-*-*") |
| 878 | // wxFont constructor not called with native font info parameter => take M_FONTDATA values |
| 879 | xFontName.Clear(); |
| 880 | |
| 881 | // not found, create a new one |
| 882 | XFontStruct *font = (XFontStruct *) |
| 883 | wxLoadQueryNearestFont(pointSize, |
| 884 | M_FONTDATA->m_family, |
| 885 | M_FONTDATA->m_style, |
| 886 | M_FONTDATA->m_weight, |
| 887 | M_FONTDATA->m_underlined, |
| 888 | wxT(""), |
| 889 | M_FONTDATA->m_encoding, |
| 890 | & xFontName); |
| 891 | |
| 892 | if ( !font ) |
| 893 | { |
| 894 | wxFAIL_MSG( wxT("Could not allocate even a default font -- something is wrong.") ); |
| 895 | |
| 896 | return (wxXFont*) NULL; |
| 897 | } |
| 898 | |
| 899 | wxXFont* f = new wxXFont; |
| 900 | f->m_fontStruct = (WXFontStructPtr)font; |
| 901 | f->m_display = ( display ? display : wxGetDisplay() ); |
| 902 | f->m_scale = intScale; |
| 903 | M_FONTDATA->m_fonts.Append(f); |
| 904 | |
| 905 | return f; |
| 906 | } |
| 907 | |
| 908 | WXFontStructPtr wxFont::GetFontStruct(double scale, WXDisplay* display) const |
| 909 | { |
| 910 | wxXFont* f = GetInternalFont(scale, display); |
| 911 | |
| 912 | return (f ? f->m_fontStruct : (WXFontStructPtr) 0); |
| 913 | } |
| 914 | |
| 915 | #endif |