PageRenderTime 33ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/ExtLibs/wxWidgets/src/os2/font.cpp

https://bitbucket.org/lennonchan/cafu
C++ | 1146 lines | 842 code | 181 blank | 123 comment | 140 complexity | 55d59b85f9b85b6e41b2484278e015f6 MD5 | raw file
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: src/os2/font.cpp
  3. // Purpose: wxFont class
  4. // Author: David Webster
  5. // Modified by:
  6. // Created: 10/06/99
  7. // RCS-ID: $Id$
  8. // Copyright: (c) David Webster
  9. // Licence: wxWindows licence
  10. /////////////////////////////////////////////////////////////////////////////
  11. // For compilers that support precompilation, includes "wx.h".
  12. #include "wx/wxprec.h"
  13. // ============================================================================
  14. // declarations
  15. // ============================================================================
  16. // ----------------------------------------------------------------------------
  17. // headers
  18. // ----------------------------------------------------------------------------
  19. #include "wx/font.h"
  20. #ifndef WX_PRECOMP
  21. #include <stdio.h>
  22. #include "wx/list.h"
  23. #include "wx/utils.h"
  24. #include "wx/app.h"
  25. #include "wx/log.h"
  26. #endif // WX_PRECOMP
  27. #include "wx/os2/private.h"
  28. #include "wx/fontutil.h"
  29. #include "wx/fontmap.h"
  30. #include "wx/encinfo.h"
  31. #include "wx/tokenzr.h"
  32. #include <malloc.h>
  33. // ----------------------------------------------------------------------------
  34. // wxFontRefData - the internal description of the font
  35. // ----------------------------------------------------------------------------
  36. class WXDLLEXPORT wxFontRefData: public wxGDIRefData
  37. {
  38. public:
  39. wxFontRefData()
  40. {
  41. Init(-1, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false,
  42. wxEmptyString, wxFONTENCODING_DEFAULT);
  43. }
  44. wxFontRefData( int nSize
  45. ,wxFontFamily nFamily
  46. ,wxFontStyle nStyle
  47. ,wxFontWeight nWeight
  48. ,bool bUnderlined
  49. ,const wxString& sFaceName
  50. ,wxFontEncoding vEncoding
  51. )
  52. {
  53. Init( nSize
  54. ,nFamily
  55. ,nStyle
  56. ,nWeight
  57. ,bUnderlined
  58. ,sFaceName
  59. ,vEncoding
  60. );
  61. }
  62. wxFontRefData( const wxNativeFontInfo& rInfo
  63. ,WXHFONT hFont = 0
  64. ,WXHANDLE hPS = 0
  65. )
  66. {
  67. Init( rInfo
  68. ,hFont
  69. ,hPS
  70. );
  71. }
  72. wxFontRefData(const wxFontRefData& rData)
  73. {
  74. Init( rData.m_nPointSize
  75. ,rData.m_nFamily
  76. ,rData.m_nStyle
  77. ,rData.m_nWeight
  78. ,rData.m_bUnderlined
  79. ,rData.m_sFaceName
  80. ,rData.m_vEncoding
  81. );
  82. m_nFontId = rData.m_nFontId;
  83. }
  84. virtual ~wxFontRefData();
  85. //
  86. // Operations
  87. //
  88. bool Alloc(wxFont* pFont);
  89. void Free(void);
  90. //
  91. // All wxFont accessors
  92. //
  93. inline int GetPointSize(void) const
  94. {
  95. //
  96. // We don't use the actual native font point size since it is
  97. // the chosen physical font, which is usually only and approximation
  98. // of the desired outline font. The actual displayable point size
  99. // is the one stored in the refData
  100. //
  101. return m_nPointSize;
  102. }
  103. inline wxFontFamily GetFamily(void) const
  104. {
  105. return m_nFamily;
  106. }
  107. inline wxFontStyle GetStyle(void) const
  108. {
  109. return m_bNativeFontInfoOk ? m_vNativeFontInfo.GetStyle()
  110. : m_nStyle;
  111. }
  112. inline wxFontWeight GetWeight(void) const
  113. {
  114. return m_bNativeFontInfoOk ? m_vNativeFontInfo.GetWeight()
  115. : m_nWeight;
  116. }
  117. inline bool GetUnderlined(void) const
  118. {
  119. return m_bNativeFontInfoOk ? m_vNativeFontInfo.GetUnderlined()
  120. : m_bUnderlined;
  121. }
  122. inline wxString GetFaceName(void) const
  123. {
  124. wxString sFaceName;
  125. if (m_bNativeFontInfoOk)
  126. sFaceName = m_vNativeFontInfo.GetFaceName();
  127. else
  128. sFaceName = m_sFaceName;
  129. return sFaceName;
  130. }
  131. inline wxFontEncoding GetEncoding(void) const
  132. {
  133. return m_bNativeFontInfoOk ? m_vNativeFontInfo.GetEncoding()
  134. : m_vEncoding;
  135. }
  136. inline WXHFONT GetHFONT(void) const { return m_hFont; }
  137. inline HPS GetPS(void) const { return m_hPS; }
  138. inline PFONTMETRICS GetFM(void) const { return m_pFM; }
  139. inline int GetNumFonts(void) const { return m_nNumFonts; }
  140. // ... and setters
  141. inline void SetPointSize(int nPointSize)
  142. {
  143. if (m_bNativeFontInfoOk)
  144. m_vNativeFontInfo.SetPointSize(nPointSize);
  145. else
  146. m_nPointSize = nPointSize;
  147. }
  148. inline void SetFamily(wxFontFamily nFamily)
  149. {
  150. m_nFamily = nFamily;
  151. }
  152. inline void SetStyle(wxFontStyle nStyle)
  153. {
  154. if (m_bNativeFontInfoOk)
  155. m_vNativeFontInfo.SetStyle(nStyle);
  156. else
  157. m_nStyle = nStyle;
  158. }
  159. inline void SetWeight(wxFontWeight nWeight)
  160. {
  161. if (m_bNativeFontInfoOk)
  162. m_vNativeFontInfo.SetWeight(nWeight);
  163. else
  164. m_nWeight = nWeight;
  165. }
  166. inline bool SetFaceName(const wxString& sFaceName)
  167. {
  168. if (m_bNativeFontInfoOk)
  169. return m_vNativeFontInfo.SetFaceName(sFaceName);
  170. else
  171. m_sFaceName = sFaceName;
  172. return true;
  173. }
  174. inline void SetUnderlined(bool bUnderlined)
  175. {
  176. if (m_bNativeFontInfoOk)
  177. m_vNativeFontInfo.SetUnderlined(bUnderlined);
  178. else
  179. m_bUnderlined = bUnderlined;
  180. }
  181. inline void SetEncoding(wxFontEncoding vEncoding)
  182. {
  183. if (m_bNativeFontInfoOk)
  184. m_vNativeFontInfo.SetEncoding(vEncoding);
  185. else
  186. m_vEncoding = vEncoding;
  187. }
  188. inline void SetPS(HPS hPS)
  189. {
  190. m_hPS = hPS;
  191. }
  192. inline void SetFM(PFONTMETRICS pFM)
  193. {
  194. m_pFM = pFM;
  195. }
  196. inline void SetNumFonts(int nNumFonts)
  197. {
  198. m_nNumFonts = nNumFonts;
  199. }
  200. //
  201. // Native font info tests
  202. //
  203. bool HasNativeFontInfo() const { return m_bNativeFontInfoOk; }
  204. const wxNativeFontInfo& GetNativeFontInfo() const
  205. { return m_vNativeFontInfo; }
  206. protected:
  207. //
  208. // Common part of all ctors
  209. //
  210. void Init( int nSize
  211. ,wxFontFamily nFamily
  212. ,wxFontStyle nStyle
  213. ,wxFontWeight nWeight
  214. ,bool bUnderlined
  215. ,const wxString& rsFaceName
  216. ,wxFontEncoding vEncoding
  217. );
  218. void Init( const wxNativeFontInfo& rInfo
  219. ,WXHFONT hFont = 0
  220. ,WXHANDLE hPS = 0
  221. );
  222. //
  223. // If true, the pointer to the actual font is temporary and SHOULD NOT BE
  224. // DELETED by destructor
  225. //
  226. bool m_bTemporary;
  227. int m_nFontId;
  228. //
  229. // Font characterstics
  230. //
  231. int m_nPointSize;
  232. wxFontFamily m_nFamily;
  233. wxFontStyle m_nStyle;
  234. wxFontWeight m_nWeight;
  235. bool m_bUnderlined;
  236. wxString m_sFaceName;
  237. wxFontEncoding m_vEncoding;
  238. WXHFONT m_hFont;
  239. //
  240. // Native font info
  241. //
  242. wxNativeFontInfo m_vNativeFontInfo;
  243. bool m_bNativeFontInfoOk;
  244. //
  245. // Some PM specific stuff
  246. //
  247. PFONTMETRICS m_pFM; // array of FONTMETRICS structs
  248. int m_nNumFonts; // number of fonts in array
  249. HPS m_hPS; // PS handle this font belongs to
  250. FATTRS m_vFattrs; // Current fattrs struct
  251. FACENAMEDESC m_vFname; // Current facename struct
  252. bool m_bInternalPS; // Internally generated PS?
  253. }; // end of CLASS wxFontRefData
  254. #define M_FONTDATA ((wxFontRefData*)m_refData)
  255. // ============================================================================
  256. // implementation
  257. // ============================================================================
  258. // ----------------------------------------------------------------------------
  259. // wxFontRefData
  260. // ----------------------------------------------------------------------------
  261. void wxFontRefData::Init(
  262. int nPointSize
  263. , wxFontFamily nFamily
  264. , wxFontStyle nStyle
  265. , wxFontWeight nWeight
  266. , bool bUnderlined
  267. , const wxString& rsFaceName
  268. , wxFontEncoding vEncoding
  269. )
  270. {
  271. m_nStyle = nStyle;
  272. m_nPointSize = nPointSize;
  273. m_nFamily = nFamily;
  274. m_nStyle = nStyle;
  275. m_nWeight = nWeight;
  276. m_bUnderlined = bUnderlined;
  277. m_sFaceName = rsFaceName;
  278. m_vEncoding = vEncoding;
  279. m_hFont = 0;
  280. m_bNativeFontInfoOk = false;
  281. m_nFontId = 0;
  282. m_bTemporary = false;
  283. m_pFM = (PFONTMETRICS)NULL;
  284. m_hPS = NULLHANDLE;
  285. m_nNumFonts = 0;
  286. } // end of wxFontRefData::Init
  287. void wxFontRefData::Init(
  288. const wxNativeFontInfo& rInfo
  289. , WXHFONT hFont //this is the FontId -- functions as the hFont for OS/2
  290. , WXHANDLE hPS // Presentation Space we are using
  291. )
  292. {
  293. //
  294. // hFont may be zero, or it be passed in case we really want to
  295. // use the exact font created in the underlying system
  296. // (for example where we can't guarantee conversion from HFONT
  297. // to LOGFONT back to HFONT)
  298. //
  299. m_hFont = hFont;
  300. m_nFontId = (int)hFont;
  301. m_bNativeFontInfoOk = true;
  302. m_vNativeFontInfo = rInfo;
  303. if (hPS == NULLHANDLE)
  304. {
  305. m_hPS = ::WinGetPS(HWND_DESKTOP);
  306. m_bInternalPS = true;
  307. }
  308. else
  309. m_hPS = (HPS)hPS;
  310. m_nFontId = 0;
  311. m_bTemporary = false;
  312. m_pFM = (PFONTMETRICS)NULL;
  313. m_nNumFonts = 0;
  314. } // end of wxFontRefData::Init
  315. wxFontRefData::~wxFontRefData()
  316. {
  317. Free();
  318. }
  319. bool wxFontRefData::Alloc( wxFont* pFont )
  320. {
  321. wxString sFaceName;
  322. long flId = m_hFont;
  323. long lRc;
  324. ERRORID vError;
  325. wxString sError;
  326. if (!m_bNativeFontInfoOk)
  327. {
  328. wxFillLogFont( &m_vNativeFontInfo.fa
  329. ,&m_vNativeFontInfo.fn
  330. ,&m_hPS
  331. ,&m_bInternalPS
  332. ,&flId
  333. ,sFaceName
  334. ,pFont
  335. );
  336. m_bNativeFontInfoOk = true;
  337. }
  338. else
  339. {
  340. if (flId == 0L)
  341. flId = 1L;
  342. else
  343. flId++;
  344. if (flId > 254)
  345. flId = 1L;
  346. }
  347. if((lRc = ::GpiCreateLogFont( m_hPS
  348. ,NULL
  349. ,flId
  350. ,&m_vNativeFontInfo.fa
  351. )) != GPI_ERROR)
  352. {
  353. m_hFont = (WXHFONT)flId;
  354. m_nFontId = flId;
  355. }
  356. if (!m_hFont)
  357. {
  358. vError = ::WinGetLastError(vHabmain);
  359. sError = wxPMErrorToStr(vError);
  360. wxLogLastError(wxT("CreateFont"));
  361. }
  362. ::GpiSetCharSet(m_hPS, flId); // sets font for presentation space
  363. ::GpiQueryFontMetrics(m_hPS, sizeof(FONTMETRICS), &m_vNativeFontInfo.fm);
  364. //
  365. // Set refData members with the results
  366. //
  367. memcpy(&m_vFattrs, &m_vNativeFontInfo.fa, sizeof(m_vFattrs));
  368. memcpy(&m_vFname, &m_vNativeFontInfo.fn, sizeof(m_vFname));
  369. //
  370. // Going to leave the point size alone. Mostly we use outline fonts
  371. // that can be set to any point size inside of Presentation Parameters,
  372. // regardless of whether or not the actual font is registered in the system.
  373. // The GpiCreateLogFont will do enough by selecting the right family,
  374. // and face name.
  375. //
  376. if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "Times New Roman") == 0)
  377. m_nFamily = wxFONTFAMILY_ROMAN;
  378. else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "Times New Roman MT 30") == 0)
  379. m_nFamily = wxFONTFAMILY_ROMAN;
  380. else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "@Times New Roman MT 30") == 0)
  381. m_nFamily = wxFONTFAMILY_ROMAN;
  382. else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "Tms Rmn") == 0)
  383. m_nFamily = wxFONTFAMILY_ROMAN;
  384. else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "WarpSans") == 0)
  385. m_nFamily = wxFONTFAMILY_DECORATIVE;
  386. else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "Helvetica") == 0)
  387. m_nFamily = wxFONTFAMILY_SWISS;
  388. else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "Helv") == 0)
  389. m_nFamily = wxFONTFAMILY_SWISS;
  390. else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "Script") == 0)
  391. m_nFamily = wxFONTFAMILY_SCRIPT;
  392. else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "Courier New") == 0)
  393. m_nFamily = wxFONTFAMILY_TELETYPE;
  394. else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "Courier") == 0)
  395. m_nFamily = wxFONTFAMILY_TELETYPE;
  396. else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "System Monospaced") == 0)
  397. m_nFamily = wxFONTFAMILY_TELETYPE;
  398. else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "System VIO") == 0)
  399. m_nFamily = wxFONTFAMILY_MODERN;
  400. else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "System Proportional") == 0)
  401. m_nFamily = wxFONTFAMILY_MODERN;
  402. else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "Arial") == 0)
  403. m_nFamily = wxFONTFAMILY_SWISS;
  404. else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "Swiss") == 0)
  405. m_nFamily = wxFONTFAMILY_SWISS;
  406. else
  407. m_nFamily = wxFONTFAMILY_SWISS;
  408. if (m_vNativeFontInfo.fa.fsSelection & FATTR_SEL_ITALIC)
  409. m_nStyle = wxFONTSTYLE_ITALIC;
  410. else
  411. m_nStyle = wxFONTSTYLE_NORMAL;
  412. switch(m_vNativeFontInfo.fn.usWeightClass)
  413. {
  414. case FWEIGHT_DONT_CARE:
  415. m_nWeight = wxFONTWEIGHT_NORMAL;
  416. break;
  417. case FWEIGHT_NORMAL:
  418. m_nWeight = wxFONTWEIGHT_NORMAL;
  419. break;
  420. case FWEIGHT_LIGHT:
  421. m_nWeight = wxFONTWEIGHT_LIGHT;
  422. break;
  423. case FWEIGHT_BOLD:
  424. m_nWeight = wxFONTWEIGHT_BOLD;
  425. break;
  426. case FWEIGHT_ULTRA_BOLD:
  427. m_nWeight = wxFONTWEIGHT_MAX;
  428. break;
  429. default:
  430. m_nWeight = wxFONTWEIGHT_NORMAL;
  431. }
  432. m_bUnderlined = ((m_vNativeFontInfo.fa.fsSelection & FATTR_SEL_UNDERSCORE) != 0);
  433. m_sFaceName = (wxChar*)m_vNativeFontInfo.fa.szFacename;
  434. m_vEncoding = wxGetFontEncFromCharSet(m_vNativeFontInfo.fa.usCodePage);
  435. //
  436. // We don't actuall keep the font around if using a temporary PS
  437. //
  438. if (m_bInternalPS)
  439. {
  440. if(m_hFont)
  441. ::GpiDeleteSetId( m_hPS
  442. ,flId
  443. );
  444. ::WinReleasePS(m_hPS);
  445. }
  446. else
  447. //
  448. // Select the font into the Presentation space
  449. //
  450. ::GpiSetCharSet(m_hPS, flId); // sets font for presentation space
  451. return true;
  452. } // end of wxFontRefData::Alloc
  453. void wxFontRefData::Free()
  454. {
  455. if (m_pFM)
  456. delete [] m_pFM;
  457. m_pFM = (PFONTMETRICS)NULL;
  458. if ( m_hFont )
  459. {
  460. ::GpiDeleteSetId(m_hPS, 1L); /* delete the logical font */
  461. m_nFontId = 0;
  462. m_hFont = 0;
  463. }
  464. if (m_bInternalPS)
  465. ::WinReleasePS(m_hPS);
  466. m_hPS = NULLHANDLE;
  467. } // end of wxFontRefData::Free
  468. // ----------------------------------------------------------------------------
  469. // wxNativeFontInfo
  470. // ----------------------------------------------------------------------------
  471. void wxNativeFontInfo::Init()
  472. {
  473. memset(&fa, '\0', sizeof(FATTRS));
  474. } // end of wxNativeFontInfo::Init
  475. int wxNativeFontInfo::GetPointSize() const
  476. {
  477. return fm.lEmHeight;
  478. } // end of wxNativeFontInfo::GetPointSize
  479. wxFontStyle wxNativeFontInfo::GetStyle() const
  480. {
  481. return fa.fsSelection & FATTR_SEL_ITALIC ? wxFONTSTYLE_ITALIC : wxFONTSTYLE_NORMAL;
  482. } // end of wxNativeFontInfo::GetStyle
  483. wxFontWeight wxNativeFontInfo::GetWeight() const
  484. {
  485. switch(fn.usWeightClass)
  486. {
  487. case FWEIGHT_DONT_CARE:
  488. return wxFONTWEIGHT_NORMAL;
  489. case FWEIGHT_NORMAL:
  490. return wxFONTWEIGHT_NORMAL;
  491. case FWEIGHT_LIGHT:
  492. return wxFONTWEIGHT_LIGHT;
  493. case FWEIGHT_BOLD:
  494. return wxFONTWEIGHT_BOLD;
  495. case FWEIGHT_ULTRA_BOLD:
  496. return wxFONTWEIGHT_MAX;
  497. }
  498. return wxFONTWEIGHT_NORMAL;
  499. } // end of wxNativeFontInfo::GetWeight
  500. bool wxNativeFontInfo::GetUnderlined() const
  501. {
  502. return ((fa.fsSelection & FATTR_SEL_UNDERSCORE) != 0);
  503. } // end of wxNativeFontInfo::GetUnderlined
  504. wxString wxNativeFontInfo::GetFaceName() const
  505. {
  506. return (wxChar*)fm.szFacename;
  507. } // end of wxNativeFontInfo::GetFaceName
  508. wxFontFamily wxNativeFontInfo::GetFamily() const
  509. {
  510. int nFamily;
  511. //
  512. // Extract family from facename
  513. //
  514. if (strcmp(fm.szFamilyname, "Times New Roman") == 0)
  515. nFamily = wxFONTFAMILY_ROMAN;
  516. else if (strcmp(fm.szFamilyname, "Times New Roman MT 30") == 0)
  517. nFamily = wxFONTFAMILY_ROMAN;
  518. else if (strcmp(fm.szFamilyname, "@Times New Roman MT 30") == 0)
  519. nFamily = wxFONTFAMILY_ROMAN;
  520. else if (strcmp(fm.szFamilyname, "Tms Rmn") == 0)
  521. nFamily = wxFONTFAMILY_ROMAN;
  522. else if (strcmp(fm.szFamilyname, "WarpSans") == 0)
  523. nFamily = wxFONTFAMILY_DECORATIVE;
  524. else if (strcmp(fm.szFamilyname, "Helvetica") == 0)
  525. nFamily = wxFONTFAMILY_SWISS;
  526. else if (strcmp(fm.szFamilyname, "Helv") == 0)
  527. nFamily = wxFONTFAMILY_SWISS;
  528. else if (strcmp(fm.szFamilyname, "Script") == 0)
  529. nFamily = wxFONTFAMILY_SCRIPT;
  530. else if (strcmp(fm.szFamilyname, "Courier New") == 0)
  531. nFamily = wxFONTFAMILY_TELETYPE;
  532. else if (strcmp(fm.szFamilyname, "Courier") == 0)
  533. nFamily = wxFONTFAMILY_TELETYPE;
  534. else if (strcmp(fm.szFamilyname, "System Monospaced") == 0)
  535. nFamily = wxFONTFAMILY_TELETYPE;
  536. else if (strcmp(fm.szFamilyname, "System VIO") == 0)
  537. nFamily = wxFONTFAMILY_MODERN;
  538. else if (strcmp(fm.szFamilyname, "System Proportional") == 0)
  539. nFamily = wxFONTFAMILY_MODERN;
  540. else if (strcmp(fm.szFamilyname, "Arial") == 0)
  541. nFamily = wxFONTFAMILY_SWISS;
  542. else if (strcmp(fm.szFamilyname, "Swiss") == 0)
  543. nFamily = wxFONTFAMILY_SWISS;
  544. else
  545. nFamily = wxFONTFAMILY_SWISS;
  546. return (wxFontFamily)nFamily;
  547. } // end of wxNativeFontInfo::GetFamily
  548. wxFontEncoding wxNativeFontInfo::GetEncoding() const
  549. {
  550. return wxGetFontEncFromCharSet(fa.usCodePage);
  551. } // end of wxNativeFontInfo::GetEncoding
  552. void wxNativeFontInfo::SetPointSize(
  553. int nPointsize
  554. )
  555. {
  556. fm.lEmHeight = (LONG)nPointsize;
  557. } // end of wxNativeFontInfo::SetPointSize
  558. void wxNativeFontInfo::SetStyle(
  559. wxFontStyle eStyle
  560. )
  561. {
  562. switch (eStyle)
  563. {
  564. default:
  565. wxFAIL_MSG( wxT("unknown font style") );
  566. // fall through
  567. case wxFONTSTYLE_NORMAL:
  568. break;
  569. case wxFONTSTYLE_ITALIC:
  570. case wxFONTSTYLE_SLANT:
  571. fa.fsSelection |= FATTR_SEL_ITALIC;
  572. break;
  573. }
  574. } // end of wxNativeFontInfo::SetStyle
  575. void wxNativeFontInfo::SetWeight(
  576. wxFontWeight eWeight
  577. )
  578. {
  579. switch (eWeight)
  580. {
  581. default:
  582. wxFAIL_MSG( wxT("unknown font weight") );
  583. // fall through
  584. case wxFONTWEIGHT_NORMAL:
  585. fn.usWeightClass = FWEIGHT_NORMAL;
  586. break;
  587. case wxFONTWEIGHT_LIGHT:
  588. fn.usWeightClass = FWEIGHT_LIGHT;
  589. break;
  590. case wxFONTWEIGHT_BOLD:
  591. fn.usWeightClass = FWEIGHT_BOLD;
  592. break;
  593. }
  594. } // end of wxNativeFontInfo::SetWeight
  595. void wxNativeFontInfo::SetUnderlined(
  596. bool bUnderlined
  597. )
  598. {
  599. if(bUnderlined)
  600. fa.fsSelection |= FATTR_SEL_UNDERSCORE;
  601. } // end of wxNativeFontInfo::SetUnderlined
  602. bool wxNativeFontInfo::SetFaceName(
  603. const wxString& sFacename
  604. )
  605. {
  606. wxStrlcpy((wxChar*)fa.szFacename, sFacename, WXSIZEOF(fa.szFacename));
  607. return true;
  608. } // end of wxNativeFontInfo::SetFaceName
  609. void wxNativeFontInfo::SetFamily(
  610. wxFontFamily eFamily
  611. )
  612. {
  613. wxString sFacename;
  614. switch (eFamily)
  615. {
  616. case wxFONTFAMILY_SCRIPT:
  617. sFacename = wxT("Tms Rmn");
  618. break;
  619. case wxFONTFAMILY_DECORATIVE:
  620. sFacename = wxT("WarpSans");
  621. break;
  622. case wxFONTFAMILY_ROMAN:
  623. sFacename = wxT("Tms Rmn");
  624. break;
  625. case wxFONTFAMILY_TELETYPE:
  626. sFacename = wxT("Courier") ;
  627. break;
  628. case wxFONTFAMILY_MODERN:
  629. sFacename = wxT("System VIO") ;
  630. break;
  631. case wxFONTFAMILY_SWISS:
  632. sFacename = wxT("Helv") ;
  633. break;
  634. case wxFONTFAMILY_DEFAULT:
  635. default:
  636. sFacename = wxT("System VIO") ;
  637. }
  638. if (!wxStrlen((wxChar*)fa.szFacename) )
  639. {
  640. SetFaceName(sFacename);
  641. }
  642. } // end of wxNativeFontInfo::SetFamily
  643. void wxNativeFontInfo::SetEncoding( wxFontEncoding eEncoding )
  644. {
  645. wxNativeEncodingInfo vInfo;
  646. if ( !wxGetNativeFontEncoding( eEncoding
  647. ,&vInfo
  648. ))
  649. {
  650. if (wxFontMapper::Get()->GetAltForEncoding( eEncoding
  651. ,&vInfo
  652. ))
  653. {
  654. if (!vInfo.facename.empty())
  655. {
  656. //
  657. // If we have this encoding only in some particular facename, use
  658. // the facename - it is better to show the correct characters in a
  659. // wrong facename than unreadable text in a correct one
  660. //
  661. SetFaceName(vInfo.facename);
  662. }
  663. }
  664. else
  665. {
  666. // unsupported encoding, replace with the default
  667. vInfo.charset = 850;
  668. }
  669. }
  670. fa.usCodePage = (USHORT)vInfo.charset;
  671. } // end of wxNativeFontInfo::SetFaceName
  672. bool wxNativeFontInfo::FromString( const wxString& rsStr )
  673. {
  674. long lVal;
  675. wxStringTokenizer vTokenizer(rsStr, wxT(";"));
  676. //
  677. // First the version
  678. //
  679. wxString sToken = vTokenizer.GetNextToken();
  680. if (sToken != wxT('0'))
  681. return false;
  682. sToken = vTokenizer.GetNextToken();
  683. if (!sToken.ToLong(&lVal))
  684. return false;
  685. fm.lEmHeight = lVal;
  686. sToken = vTokenizer.GetNextToken();
  687. if (!sToken.ToLong(&lVal))
  688. return false;
  689. fa.lAveCharWidth = lVal;
  690. sToken = vTokenizer.GetNextToken();
  691. if (!sToken.ToLong(&lVal))
  692. return false;
  693. fa.fsSelection = (USHORT)lVal;
  694. sToken = vTokenizer.GetNextToken();
  695. if (!sToken.ToLong(&lVal))
  696. return false;
  697. fa.fsType = (USHORT)lVal;
  698. sToken = vTokenizer.GetNextToken();
  699. if (!sToken.ToLong(&lVal))
  700. return false;
  701. fa.fsFontUse = (USHORT)lVal;
  702. sToken = vTokenizer.GetNextToken();
  703. if (!sToken.ToLong(&lVal))
  704. return false;
  705. fa.idRegistry = (USHORT)lVal;
  706. sToken = vTokenizer.GetNextToken();
  707. if (!sToken.ToLong(&lVal))
  708. return false;
  709. fa.usCodePage = (USHORT)lVal;
  710. sToken = vTokenizer.GetNextToken();
  711. if (!sToken.ToLong(&lVal))
  712. return false;
  713. fa.lMatch = lVal;
  714. sToken = vTokenizer.GetNextToken();
  715. if (!sToken.ToLong(&lVal))
  716. return false;
  717. fn.usWeightClass = (USHORT)lVal;
  718. sToken = vTokenizer.GetNextToken();
  719. if(!sToken)
  720. return false;
  721. wxStrcpy((wxChar*)fa.szFacename, sToken.c_str());
  722. return true;
  723. } // end of wxNativeFontInfo::FromString
  724. wxString wxNativeFontInfo::ToString() const
  725. {
  726. wxString sStr;
  727. sStr.Printf(wxT("%d;%ld;%ld;%ld;%d;%d;%d;%d;%d;%ld;%d;%s"),
  728. 0, // version, in case we want to change the format later
  729. fm.lEmHeight,
  730. fa.lAveCharWidth,
  731. fa.lMaxBaselineExt,
  732. fa.fsSelection,
  733. fa.fsType,
  734. fa.fsFontUse,
  735. fa.idRegistry,
  736. fa.usCodePage,
  737. fa.lMatch,
  738. fn.usWeightClass,
  739. (char *)fa.szFacename);
  740. return sStr;
  741. } // end of wxNativeFontInfo::ToString
  742. // ----------------------------------------------------------------------------
  743. // wxFont
  744. // ----------------------------------------------------------------------------
  745. bool wxFont::Create( const wxNativeFontInfo& rInfo,
  746. WXHFONT hFont )
  747. {
  748. UnRef();
  749. m_refData = new wxFontRefData( rInfo
  750. ,hFont
  751. );
  752. RealizeResource();
  753. return true;
  754. } // end of wxFont::Create
  755. wxFont::wxFont(
  756. const wxString& rsFontdesc
  757. )
  758. {
  759. wxNativeFontInfo vInfo;
  760. if (vInfo.FromString(rsFontdesc))
  761. (void)Create(vInfo);
  762. } // end of wxFont::wxFont
  763. // ----------------------------------------------------------------------------
  764. // Constructor for a font. Note that the real construction is done
  765. // in wxDC::SetFont, when information is available about scaling etc.
  766. // ----------------------------------------------------------------------------
  767. bool wxFont::Create( int nPointSize,
  768. wxFontFamily nFamily,
  769. wxFontStyle nStyle,
  770. wxFontWeight nWeight,
  771. bool bUnderlined,
  772. const wxString& rsFaceName,
  773. wxFontEncoding vEncoding )
  774. {
  775. UnRef();
  776. //
  777. // wxDEFAULT is a valid value for the font size too so we must treat it
  778. // specially here (otherwise the size would be 70 == wxDEFAULT value)
  779. //
  780. if (nPointSize == wxDEFAULT)
  781. {
  782. nPointSize = wxNORMAL_FONT->GetPointSize();
  783. }
  784. m_refData = new wxFontRefData( nPointSize
  785. ,nFamily
  786. ,nStyle
  787. ,nWeight
  788. ,bUnderlined
  789. ,rsFaceName
  790. ,vEncoding
  791. );
  792. RealizeResource();
  793. return true;
  794. } // end of wxFont::Create
  795. wxFont::~wxFont()
  796. {
  797. } // end of wxFont::~wxFont
  798. // ----------------------------------------------------------------------------
  799. // real implementation
  800. // Boris' Kovalenko comments:
  801. // Because OS/2 fonts are associated with PS we cannot create the font
  802. // here, but we may check that font definition is true
  803. // ----------------------------------------------------------------------------
  804. wxGDIRefData *wxFont::CreateGDIRefData() const
  805. {
  806. return new wxFontRefData();
  807. }
  808. wxGDIRefData *wxFont::CloneGDIRefData(const wxGDIRefData *data) const
  809. {
  810. return new wxFontRefData(*static_cast<const wxFontRefData *>(data));
  811. }
  812. bool wxFont::RealizeResource()
  813. {
  814. if ( GetResourceHandle() )
  815. {
  816. return true;
  817. }
  818. return M_FONTDATA->Alloc(this);
  819. } // end of wxFont::RealizeResource
  820. bool wxFont::FreeResource( bool WXUNUSED(bForce) )
  821. {
  822. if (GetResourceHandle())
  823. {
  824. M_FONTDATA->Free();
  825. return true;
  826. }
  827. return false;
  828. } // end of wxFont::FreeResource
  829. WXHANDLE wxFont::GetResourceHandle() const
  830. {
  831. return GetHFONT();
  832. } // end of wxFont::GetResourceHandle
  833. WXHFONT wxFont::GetHFONT() const
  834. {
  835. return M_FONTDATA ? M_FONTDATA->GetHFONT() : 0;
  836. } // end of wxFont::GetHFONT
  837. bool wxFont::IsFree() const
  838. {
  839. return M_FONTDATA && (M_FONTDATA->GetHFONT() == 0);
  840. } // end of wxFont::IsFree
  841. // ----------------------------------------------------------------------------
  842. // change font attribute: we recreate font when doing it
  843. // ----------------------------------------------------------------------------
  844. void wxFont::SetPointSize(
  845. int nPointSize
  846. )
  847. {
  848. AllocExclusive();
  849. M_FONTDATA->SetPointSize(nPointSize);
  850. RealizeResource();
  851. } // end of wxFont::SetPointSize
  852. void wxFont::SetFamily(
  853. wxFontFamily nFamily
  854. )
  855. {
  856. AllocExclusive();
  857. M_FONTDATA->SetFamily(nFamily);
  858. RealizeResource();
  859. } // end of wxFont::SetFamily
  860. void wxFont::SetStyle(
  861. wxFontStyle nStyle
  862. )
  863. {
  864. AllocExclusive();
  865. M_FONTDATA->SetStyle(nStyle);
  866. RealizeResource();
  867. } // end of wxFont::SetStyle
  868. void wxFont::SetWeight(
  869. wxFontWeight nWeight
  870. )
  871. {
  872. AllocExclusive();
  873. M_FONTDATA->SetWeight(nWeight);
  874. RealizeResource();
  875. } // end of wxFont::SetWeight
  876. bool wxFont::SetFaceName(
  877. const wxString& rsFaceName
  878. )
  879. {
  880. AllocExclusive();
  881. bool refdataok = M_FONTDATA->SetFaceName(rsFaceName);
  882. RealizeResource();
  883. return refdataok && wxFontBase::SetFaceName(rsFaceName);
  884. } // end of wxFont::SetFaceName
  885. void wxFont::SetUnderlined(
  886. bool bUnderlined
  887. )
  888. {
  889. AllocExclusive();
  890. M_FONTDATA->SetUnderlined(bUnderlined);
  891. RealizeResource();
  892. } // end of wxFont::SetUnderlined
  893. void wxFont::SetEncoding(
  894. wxFontEncoding vEncoding
  895. )
  896. {
  897. AllocExclusive();
  898. M_FONTDATA->SetEncoding(vEncoding);
  899. RealizeResource();
  900. } // end of wxFont::SetEncoding
  901. void wxFont::DoSetNativeFontInfo(
  902. const wxNativeFontInfo& rInfo
  903. )
  904. {
  905. AllocExclusive();
  906. FreeResource();
  907. *M_FONTDATA = wxFontRefData(rInfo);
  908. RealizeResource();
  909. }
  910. // ----------------------------------------------------------------------------
  911. // accessors
  912. // ----------------------------------------------------------------------------
  913. int wxFont::GetPointSize() const
  914. {
  915. wxCHECK_MSG( IsOk(), 0, wxT("invalid font") );
  916. return M_FONTDATA->GetPointSize();
  917. } // end of wxFont::GetPointSize
  918. wxFontFamily wxFont::DoGetFamily() const
  919. {
  920. return M_FONTDATA->GetFamily();
  921. } // end of wxFont::DoGetFamily
  922. wxFontStyle wxFont::GetStyle() const
  923. {
  924. wxCHECK_MSG( IsOk(), wxFONTSTYLE_MAX, wxT("invalid font") );
  925. return M_FONTDATA->GetStyle();
  926. } // end of wxFont::GetStyle
  927. wxFontWeight wxFont::GetWeight() const
  928. {
  929. wxCHECK_MSG( IsOk(), wxFONTWEIGHT_MAX, wxT("invalid font") );
  930. return M_FONTDATA->GetWeight();
  931. }
  932. bool wxFont::GetUnderlined() const
  933. {
  934. wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
  935. return M_FONTDATA->GetUnderlined();
  936. } // end of wxFont::GetUnderlined
  937. wxString wxFont::GetFaceName() const
  938. {
  939. wxCHECK_MSG( IsOk(), wxEmptyString, wxT("invalid font") );
  940. return M_FONTDATA->GetFaceName();
  941. } // end of wxFont::GetFaceName
  942. wxFontEncoding wxFont::GetEncoding() const
  943. {
  944. wxCHECK_MSG( IsOk(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
  945. return M_FONTDATA->GetEncoding();
  946. } // end of wxFont::GetEncoding
  947. const wxNativeFontInfo* wxFont::GetNativeFontInfo() const
  948. {
  949. return M_FONTDATA->HasNativeFontInfo() ? &(M_FONTDATA->GetNativeFontInfo())
  950. : NULL;
  951. } // end of wxFont::GetNativeFontInfo
  952. //
  953. // Internal use only method to set the FONTMETRICS array
  954. //
  955. void wxFont::SetFM( PFONTMETRICS pFM, int nNumFonts )
  956. {
  957. M_FONTDATA->SetFM(pFM);
  958. M_FONTDATA->SetNumFonts(nNumFonts);
  959. } // end of wxFont::SetFM
  960. void wxFont::SetPS( HPS hPS )
  961. {
  962. AllocExclusive();
  963. M_FONTDATA->SetPS(hPS);
  964. RealizeResource();
  965. } // end of wxFont::SetPS