| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: font.cpp |
| 3 | // Author: Vaclav Slavik |
| 4 | // Id: $Id$ |
| 5 | // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com) |
| 6 | // Licence: wxWindows licence |
| 7 | ///////////////////////////////////////////////////////////////////////////// |
| 8 | |
| 9 | // ============================================================================ |
| 10 | // declarations |
| 11 | // ============================================================================ |
| 12 | |
| 13 | // ---------------------------------------------------------------------------- |
| 14 | // headers |
| 15 | // ---------------------------------------------------------------------------- |
| 16 | |
| 17 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
| 18 | #pragma implementation "font.h" |
| 19 | #endif |
| 20 | |
| 21 | // For compilers that support precompilation, includes "wx.h". |
| 22 | #include "wx/wxprec.h" |
| 23 | |
| 24 | #ifdef __BORLANDC__ |
| 25 | #pragma hdrstop |
| 26 | #endif |
| 27 | |
| 28 | #include "wx/font.h" |
| 29 | #include "wx/fontutil.h" |
| 30 | #include "wx/cmndata.h" |
| 31 | #include "wx/utils.h" |
| 32 | #include "wx/log.h" |
| 33 | #include "wx/gdicmn.h" |
| 34 | #include "wx/tokenzr.h" |
| 35 | #include "wx/settings.h" |
| 36 | #include "wx/mgl/private.h" |
| 37 | |
| 38 | // ---------------------------------------------------------------------------- |
| 39 | // wxFontRefData |
| 40 | // ---------------------------------------------------------------------------- |
| 41 | |
| 42 | class wxFontRefData : public wxObjectRefData |
| 43 | { |
| 44 | public: |
| 45 | wxFontRefData(int size = wxDEFAULT, |
| 46 | int family = wxDEFAULT, |
| 47 | int style = wxDEFAULT, |
| 48 | int weight = wxDEFAULT, |
| 49 | bool underlined = FALSE, |
| 50 | const wxString& faceName = wxEmptyString, |
| 51 | wxFontEncoding encoding = wxFONTENCODING_DEFAULT); |
| 52 | wxFontRefData(const wxFontRefData& data); |
| 53 | virtual ~wxFontRefData(); |
| 54 | |
| 55 | protected: |
| 56 | // common part of all ctors |
| 57 | void Init(int pointSize, |
| 58 | int family, |
| 59 | int style, |
| 60 | int weight, |
| 61 | bool underlined, |
| 62 | const wxString& faceName, |
| 63 | wxFontEncoding encoding); |
| 64 | |
| 65 | private: |
| 66 | int m_pointSize; |
| 67 | int m_family, |
| 68 | m_style, |
| 69 | m_weight; |
| 70 | bool m_underlined; |
| 71 | wxString m_faceName; |
| 72 | wxFontEncoding m_encoding; |
| 73 | |
| 74 | wxMGLFontLibrary *m_library; |
| 75 | bool m_valid; |
| 76 | |
| 77 | wxNativeFontInfo m_info; |
| 78 | |
| 79 | friend class wxFont; |
| 80 | }; |
| 81 | |
| 82 | // ============================================================================ |
| 83 | // implementation |
| 84 | // ============================================================================ |
| 85 | |
| 86 | // ---------------------------------------------------------------------------- |
| 87 | // wxFontRefData |
| 88 | // ---------------------------------------------------------------------------- |
| 89 | |
| 90 | void wxFontRefData::Init(int pointSize, |
| 91 | int family, |
| 92 | int style, |
| 93 | int weight, |
| 94 | bool underlined, |
| 95 | const wxString& faceName, |
| 96 | wxFontEncoding encoding) |
| 97 | { |
| 98 | if ( family == wxDEFAULT ) |
| 99 | m_family = wxSWISS; |
| 100 | else |
| 101 | m_family = family; |
| 102 | |
| 103 | m_faceName = faceName; |
| 104 | |
| 105 | if ( style == wxDEFAULT ) |
| 106 | m_style = wxNORMAL; |
| 107 | else |
| 108 | m_style = style; |
| 109 | |
| 110 | if ( weight == wxDEFAULT ) |
| 111 | m_weight = wxNORMAL; |
| 112 | else |
| 113 | m_weight = weight; |
| 114 | |
| 115 | if ( pointSize == wxDEFAULT ) |
| 116 | m_pointSize = 12; |
| 117 | else |
| 118 | m_pointSize = pointSize; |
| 119 | |
| 120 | m_underlined = underlined; |
| 121 | m_encoding = encoding; |
| 122 | |
| 123 | m_library = NULL; |
| 124 | m_valid = FALSE; |
| 125 | } |
| 126 | |
| 127 | wxFontRefData::wxFontRefData(const wxFontRefData& data) |
| 128 | { |
| 129 | Init(data.m_pointSize, data.m_family, data.m_style, data.m_weight, |
| 130 | data.m_underlined, data.m_faceName, data.m_encoding); |
| 131 | |
| 132 | m_library = data.m_library; |
| 133 | m_valid = data.m_valid; |
| 134 | if ( m_library ) |
| 135 | m_library->IncRef(); |
| 136 | wxLogTrace("mgl_font", "created fntrefdata %p, library is %p", this, m_library); |
| 137 | } |
| 138 | |
| 139 | wxFontRefData::wxFontRefData(int size, int family, int style, |
| 140 | int weight, bool underlined, |
| 141 | const wxString& faceName, |
| 142 | wxFontEncoding encoding) |
| 143 | { |
| 144 | Init(size, family, style, weight, underlined, faceName, encoding); |
| 145 | wxLogTrace("mgl_font", "created fntrefdata %p, library is %p", this, m_library); |
| 146 | } |
| 147 | |
| 148 | wxFontRefData::~wxFontRefData() |
| 149 | { |
| 150 | wxLogTrace("mgl_font", "destructing fntrefdata %p, library is %p", this, m_library); |
| 151 | if ( m_library ) |
| 152 | m_library->DecRef(); |
| 153 | } |
| 154 | |
| 155 | // ---------------------------------------------------------------------------- |
| 156 | // wxFont |
| 157 | // ---------------------------------------------------------------------------- |
| 158 | |
| 159 | IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject) |
| 160 | |
| 161 | bool wxFont::Create(const wxNativeFontInfo& info) |
| 162 | { |
| 163 | return Create(info.pointSize, info.family, info.style, info.weight, |
| 164 | info.underlined, info.faceName, info.encoding); |
| 165 | } |
| 166 | |
| 167 | bool wxFont::Create(int pointSize, |
| 168 | int family, |
| 169 | int style, |
| 170 | int weight, |
| 171 | bool underlined, |
| 172 | const wxString& face, |
| 173 | wxFontEncoding encoding) |
| 174 | { |
| 175 | m_refData = new wxFontRefData(pointSize, family, style, weight, |
| 176 | underlined, face, encoding); |
| 177 | return TRUE; |
| 178 | } |
| 179 | |
| 180 | struct font_t *wxFont::GetMGLfont_t(float scale, bool antialiased) |
| 181 | { |
| 182 | if ( !M_FONTDATA->m_valid ) |
| 183 | { |
| 184 | wxMGLFontLibrary *old = M_FONTDATA->m_library; |
| 185 | M_FONTDATA->m_library = wxTheFontsManager->GetFontLibrary(this); |
| 186 | M_FONTDATA->m_library->IncRef(); |
| 187 | if ( old ) |
| 188 | old->DecRef(); |
| 189 | } |
| 190 | |
| 191 | wxMGLFontInstance *instance = |
| 192 | M_FONTDATA->m_library->GetFontInstance(this, scale, antialiased); |
| 193 | |
| 194 | return instance->GetMGLfont_t(); |
| 195 | } |
| 196 | |
| 197 | wxObjectRefData *wxFont::CreateRefData() const |
| 198 | { |
| 199 | return new wxFontRefData; |
| 200 | } |
| 201 | |
| 202 | wxObjectRefData *wxFont::CloneRefData(const wxObjectRefData *data) const |
| 203 | { |
| 204 | return new wxFontRefData(*(wxFontRefData *)data); |
| 205 | } |
| 206 | |
| 207 | |
| 208 | // ---------------------------------------------------------------------------- |
| 209 | // accessors |
| 210 | // ---------------------------------------------------------------------------- |
| 211 | |
| 212 | int wxFont::GetPointSize() const |
| 213 | { |
| 214 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
| 215 | |
| 216 | return M_FONTDATA->m_pointSize; |
| 217 | } |
| 218 | |
| 219 | wxString wxFont::GetFaceName() const |
| 220 | { |
| 221 | wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") ); |
| 222 | |
| 223 | return M_FONTDATA->m_faceName; |
| 224 | } |
| 225 | |
| 226 | int wxFont::GetFamily() const |
| 227 | { |
| 228 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
| 229 | |
| 230 | return M_FONTDATA->m_family; |
| 231 | } |
| 232 | |
| 233 | int wxFont::GetStyle() const |
| 234 | { |
| 235 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
| 236 | |
| 237 | return M_FONTDATA->m_style; |
| 238 | } |
| 239 | |
| 240 | int wxFont::GetWeight() const |
| 241 | { |
| 242 | wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); |
| 243 | |
| 244 | return M_FONTDATA->m_weight; |
| 245 | } |
| 246 | |
| 247 | bool wxFont::GetUnderlined() const |
| 248 | { |
| 249 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") ); |
| 250 | |
| 251 | return M_FONTDATA->m_underlined; |
| 252 | } |
| 253 | |
| 254 | |
| 255 | wxFontEncoding wxFont::GetEncoding() const |
| 256 | { |
| 257 | wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") ); |
| 258 | |
| 259 | return M_FONTDATA->m_encoding; |
| 260 | } |
| 261 | |
| 262 | bool wxFont::IsFixedWidth() const |
| 263 | { |
| 264 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") ); |
| 265 | |
| 266 | return (bool)(M_FONTDATA->m_library->GetFamily()->GetInfo()->isFixed); |
| 267 | } |
| 268 | |
| 269 | const wxNativeFontInfo *wxFont::GetNativeFontInfo() const |
| 270 | { |
| 271 | wxCHECK_MSG( Ok(), NULL, wxT("invalid font") ); |
| 272 | |
| 273 | M_FONTDATA->m_info.InitFromFont(*this); |
| 274 | |
| 275 | return &(M_FONTDATA->m_info); |
| 276 | } |
| 277 | |
| 278 | // ---------------------------------------------------------------------------- |
| 279 | // change font attributes |
| 280 | // ---------------------------------------------------------------------------- |
| 281 | |
| 282 | void wxFont::SetPointSize(int pointSize) |
| 283 | { |
| 284 | AllocExclusive(); |
| 285 | |
| 286 | M_FONTDATA->m_pointSize = pointSize; |
| 287 | M_FONTDATA->m_valid = FALSE; |
| 288 | } |
| 289 | |
| 290 | void wxFont::SetFamily(int family) |
| 291 | { |
| 292 | AllocExclusive(); |
| 293 | |
| 294 | M_FONTDATA->m_family = family; |
| 295 | M_FONTDATA->m_valid = FALSE; |
| 296 | } |
| 297 | |
| 298 | void wxFont::SetStyle(int style) |
| 299 | { |
| 300 | AllocExclusive(); |
| 301 | |
| 302 | M_FONTDATA->m_style = style; |
| 303 | M_FONTDATA->m_valid = FALSE; |
| 304 | } |
| 305 | |
| 306 | void wxFont::SetWeight(int weight) |
| 307 | { |
| 308 | AllocExclusive(); |
| 309 | |
| 310 | M_FONTDATA->m_weight = weight; |
| 311 | M_FONTDATA->m_valid = FALSE; |
| 312 | } |
| 313 | |
| 314 | void wxFont::SetFaceName(const wxString& faceName) |
| 315 | { |
| 316 | AllocExclusive(); |
| 317 | |
| 318 | M_FONTDATA->m_faceName = faceName; |
| 319 | M_FONTDATA->m_valid = FALSE; |
| 320 | } |
| 321 | |
| 322 | void wxFont::SetUnderlined(bool underlined) |
| 323 | { |
| 324 | AllocExclusive(); |
| 325 | |
| 326 | M_FONTDATA->m_underlined = underlined; |
| 327 | } |
| 328 | |
| 329 | void wxFont::SetEncoding(wxFontEncoding encoding) |
| 330 | { |
| 331 | AllocExclusive(); |
| 332 | |
| 333 | M_FONTDATA->m_encoding = encoding; |
| 334 | M_FONTDATA->m_valid = FALSE; |
| 335 | } |