home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / cuvfm.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  6.8 KB  |  238 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19.  
  20. //--------------------------------------------------------------------------------------------------------
  21. //    Unicode Virtual Font Manager used for Unicode rendering 
  22. //--------------------------------------------------------------------------------------------------------
  23. #include "stdafx.h"
  24. #include "cuvfm.h"
  25. //------------------------------------------------------------------------------------------------
  26. //
  27. //    CUnicodeVirtualFontMgr
  28. //
  29. //------------------------------------------------------------------------------------------------
  30. #ifdef XP_WIN
  31. #ifndef XP_WIN32
  32. #define GDI_ERROR NULL
  33. #endif
  34. #endif
  35.  
  36. CUnicodeVirtualFontMgr::CUnicodeVirtualFontMgr(HDC in_hdc)
  37. {
  38.     TEXTMETRIC tm;
  39.     ::GetTextMetrics(in_hdc, &tm);
  40.     m_DC = in_hdc;
  41.     m_OrigFont = (HFONT)::SelectObject(in_hdc, ::GetStockObject(SYSTEM_FONT));
  42.     if(m_OrigFont != (HGDIOBJ)GDI_ERROR)
  43.     {
  44.         CFont *pFont = CFont::FromHandle(m_OrigFont);
  45.         if( (pFont) &&
  46. #ifdef XP_WIN32
  47.             (pFont->GetLogFont(& (this->m_lf))))
  48. #else
  49.         (::GetObject(pFont->m_hObject ,sizeof(LOGFONT), &(this->m_lf))))
  50. #endif
  51.         {
  52.             m_bFixed = ( m_lf.lfPitchAndFamily & FIXED_PITCH );
  53.             m_iOrigAscent = tm.tmAscent;
  54.         }
  55.         ::SelectObject(in_hdc, m_OrigFont);
  56.     }
  57.  
  58. }
  59. //------------------------------------------------------------------------------------------------
  60. //
  61. //    ~CUnicodeVirtualFontMgr()
  62. //
  63. //------------------------------------------------------------------------------------------------
  64. CUnicodeVirtualFontMgr::~CUnicodeVirtualFontMgr()
  65. {
  66.     // restore the original font when we destroy.
  67.     if(m_OrigFont != (HGDIOBJ)GDI_ERROR)
  68.     {
  69.         ::SelectObject(m_DC, m_OrigFont);
  70.     }
  71. }
  72. //------------------------------------------------------------------------------------------------
  73. //
  74. //    CacularAscentDelta()
  75. //
  76. //------------------------------------------------------------------------------------------------
  77. int CUnicodeVirtualFontMgr::CacularAscentDelta(HDC hDC)
  78. {
  79.     // restore the original font when we destroy.
  80.     if(m_OrigFont != (HGDIOBJ)GDI_ERROR)
  81.     {
  82.         TEXTMETRIC tm;
  83.         ::GetTextMetrics(hDC, &tm);
  84.         return m_iOrigAscent - tm.tmAscent;
  85.     }
  86.     return 0;
  87. }
  88. //------------------------------------------------------------------------------------------------
  89. //
  90. //    CUnicodeVirtualFontMgr::GetCsidFont
  91. //
  92. //------------------------------------------------------------------------------------------------
  93. CFont*    CUnicodeVirtualFontMgr::GetCsidFont(int16 encoding)
  94. {
  95.     if(m_OrigFont != (HGDIOBJ)GDI_ERROR)
  96.     {
  97.  
  98.         CFont *pFont;
  99.         if(! GetFontFromCache(encoding, pFont))
  100.         {
  101.             VERIFY(pFont = CreateFont(encoding));
  102.             if(pFont != NULL)
  103.             {    
  104.                 if(AddFontToCache(encoding, pFont) == FALSE)
  105.                 {
  106.                     delete pFont;
  107.                     return NULL;
  108.                 }
  109.             }
  110.         }
  111.         return pFont;
  112.     }
  113.     return NULL;
  114. }
  115.  
  116. CyaFont *CUnicodeVirtualFontMgr::GetCsidCyaFont(HDC hdc, int16 encoding)
  117. {
  118.     if(m_OrigFont != (HGDIOBJ)GDI_ERROR)
  119.     {
  120.  
  121.         CyaFont *pFont;
  122.         if(! GetCyaFontFromCache(encoding, pFont))
  123.         {
  124.             VERIFY(pFont = CreateCyaFont(hdc, encoding));
  125.             if(pFont != NULL)
  126.             {    
  127.                 if(AddCyaFontToCache(encoding, pFont) == FALSE)
  128.                 {
  129.                     delete pFont;
  130.                     return NULL;
  131.                 }
  132.             }
  133.         }
  134.         return pFont;
  135.     }
  136.     return NULL;
  137. }    // GetCsidCyaFont
  138.  
  139. //------------------------------------------------------------------------------------------------
  140. //
  141. //    CUnicodeVirtualFontMgr::CreateFont
  142. //
  143. //------------------------------------------------------------------------------------------------
  144. void CUnicodeVirtualFontMgr::UpdateLOGFONTForEncoding(int16 encoding)
  145. {
  146.     m_lf.lfCharSet = DEFAULT_CHARSET;
  147.     m_lf.lfPitchAndFamily = DEFAULT_PITCH;
  148.     switch(encoding)
  149.     {
  150.         case CS_DINGBATS:
  151.             {
  152.                 strcpy(m_lf.lfFaceName, "Wingdings");
  153.                 if( (! theApp.m_bUseUnicodeFont) )
  154.                     m_lf.lfCharSet = SYMBOL_CHARSET;
  155.             }
  156.             break;
  157.         case CS_SYMBOL:
  158.             {
  159.                 strcpy(m_lf.lfFaceName, "Symbol");
  160.                 if( (! theApp.m_bUseUnicodeFont) )
  161.                     m_lf.lfCharSet = SYMBOL_CHARSET;
  162.             }
  163.             break;
  164.         default:
  165.             {
  166.                 EncodingInfo *pEncoding = theApp.m_pIntlFont->GetEncodingInfo(theApp.m_pIntlFont->DocCSIDtoID(encoding & ~CS_AUTO));
  167.                 XP_ASSERT(pEncoding);
  168.                 if(pEncoding)
  169.                 {
  170.                     strcpy(m_lf.lfFaceName, ((m_bFixed) ? pEncoding->szFixName : pEncoding->szPropName ));
  171.                     if(sysInfo.m_bWin4)
  172.                     {
  173.                         if( (! theApp.m_bUseUnicodeFont) )
  174.                         {
  175.                             m_lf.lfCharSet = ((m_bFixed) ? 
  176.                                 pEncoding->iFixCharset : pEncoding->iPropCharset);
  177.                         }
  178.                     }
  179.                 }
  180.             }
  181.             break;
  182.     }
  183. }
  184. CFont* CUnicodeVirtualFontMgr::CreateFont(int16 encoding)
  185. {
  186.     UpdateLOGFONTForEncoding(encoding);
  187.     CFont *pSelectThis = new CFont();
  188.     VERIFY(pSelectThis->CreateFontIndirect(&m_lf));
  189.     return pSelectThis;
  190. }
  191.  
  192. CyaFont* CUnicodeVirtualFontMgr::CreateCyaFont(HDC hdc, int16 encoding)
  193. {
  194.     int   isUnicode;
  195.     if ( CIntlWin::UseUnicodeFontAPI( encoding )) 
  196.     {
  197.         isUnicode = 1;
  198.     }
  199.     else    
  200.     {
  201.         isUnicode = 0;
  202.     }
  203.     UpdateLOGFONTForEncoding(encoding); 
  204.     CyaFont *pSelectThis = new CyaFont();
  205.     // todo: pass in context as first paramer, it will be used
  206.     // to get current URL, for webfont security.
  207.     VERIFY(pSelectThis->CreateNetscapeFontWithLOGFONT(NULL,hdc, &m_lf, isUnicode) == FONTERR_OK );
  208.     return pSelectThis;
  209. }
  210.  
  211. BOOL CUnicodeVirtualFontMgr::GetFontFromCache(int16 encoding, CFont*& pFont)
  212. {
  213.     return CVirtualFontFontCache::Get(encoding, 
  214.         m_lf.lfHeight, m_bFixed, (m_lf.lfWeight == FW_BOLD), 
  215.         m_lf.lfItalic, m_lf.lfUnderline ,pFont);
  216. }
  217. BOOL CUnicodeVirtualFontMgr::AddFontToCache(int16 encoding, CFont* pFont)    
  218.     return CVirtualFontFontCache::Add(encoding, 
  219.         m_lf.lfHeight, m_bFixed, (m_lf.lfWeight == FW_BOLD), 
  220.         m_lf.lfItalic, m_lf.lfUnderline ,pFont);    
  221. }
  222.  
  223. BOOL CUnicodeVirtualFontMgr::GetCyaFontFromCache(int16 encoding, CyaFont*& pFont)
  224. {
  225.     return CVirtualFontFontCache::Get(encoding, 
  226.         m_lf.lfHeight, m_bFixed, (m_lf.lfWeight == FW_BOLD), 
  227.         m_lf.lfItalic, m_lf.lfUnderline ,pFont);
  228. }
  229. BOOL CUnicodeVirtualFontMgr::AddCyaFontToCache(int16 encoding, CyaFont* pFont)    
  230.     return CVirtualFontFontCache::Add(encoding, 
  231.         m_lf.lfHeight, m_bFixed, (m_lf.lfWeight == FW_BOLD), 
  232.         m_lf.lfItalic, m_lf.lfUnderline ,pFont);    
  233. }
  234.  
  235.  
  236.