home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / cuvfs.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  6.8 KB  |  184 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. //    Author: Frank Tang ftang@netscape.com x2913
  21. //
  22. //    Text Handlering Routien for Unicode rendering 
  23. //--------------------------------------------------------------------------------------------------------
  24. #include "stdafx.h"
  25. #include "cuvfs.h"
  26. #include "cuvfm.h"
  27. #include "libi18n.h"
  28.  
  29. //------------------------------------------------------------------------------------------------
  30. //    INTL_CompoundStrFromUTF8
  31. //------------------------------------------------------------------------------------------------
  32. static INTL_CompoundStr* INTL_CompoundStrFromUTF8(LPCSTR pString, int length)
  33. {
  34.     WCHAR ucs2[512];
  35.     int ulen;
  36.     INTL_CompoundStr *This = NULL;
  37.     ulen = CASTINT(INTL_TextToUnicode(CS_UTF8, (unsigned char*)pString, length, ucs2, 512));
  38.     This = INTL_CompoundStrFromUnicode(ucs2, ulen);
  39.     return This;
  40. }
  41.  
  42. //------------------------------------------------------------------------------------------------
  43. //
  44. //    CIntlUnicodeVirtualFontStrategy::GetTextExtentPoint
  45. //
  46. //------------------------------------------------------------------------------------------------
  47. BOOL CIntlUnicodeVirtualFontStrategy::GetTextExtentPoint(HDC hDC, LPCSTR pString, int iLength, LPSIZE pSize)
  48. {
  49.     pSize->cx = 0;
  50.     pSize->cy = 0;
  51.  
  52.     CUnicodeVirtualFontMgr ufm(hDC);
  53.     INTL_CompoundStr *str = INTL_CompoundStrFromUTF8(pString, iLength);
  54.     if(str)
  55.     {    
  56.         CDC * pDC = CDC::FromHandle(hDC);
  57.         INTL_Encoding_ID encoding;
  58.         unsigned char *text;
  59.         INTL_CompoundStrIterator iter;
  60.         for(iter = INTL_CompoundStrFirstStr((INTL_CompoundStrIterator) str, &encoding, &text);
  61.                 iter != NULL;
  62.                     iter = INTL_CompoundStrNextStr(iter, &encoding, &text))
  63.         {
  64.             SIZE textsize;
  65.             pDC->SelectObject(ufm.GetCsidFont(encoding));
  66.             int textlength = XP_STRLEN((char*)text);
  67.             VERIFY(CIntlWin::GetTextExtentPoint(encoding, hDC, (char*)text, textlength, &textsize));
  68.             pSize->cx += textsize.cx;
  69.             if(textsize.cy > pSize->cy)
  70.                 pSize->cy = textsize.cy;
  71.         }
  72.         INTL_CompoundStrDestroy(str);
  73.     }
  74.     // The destructor of CUnicodeVirtualFontMgr will reset the font.
  75.     return TRUE;
  76. }
  77. //------------------------------------------------------------------------------------------------
  78. //
  79. //    CIntlUnicodeVirtualFontStrategy::TextOut
  80. //
  81. //------------------------------------------------------------------------------------------------
  82. BOOL CIntlUnicodeVirtualFontStrategy::TextOut(HDC hDC, int nXStart, int nYStart, LPCSTR pString, int iLength)
  83. {
  84.     // Save the current font on stack
  85.     CUnicodeVirtualFontMgr ufm(hDC);
  86.     INTL_CompoundStr *str = INTL_CompoundStrFromUTF8(pString, iLength);
  87.     if(str)
  88.     {    
  89.         CDC * pDC = CDC::FromHandle(hDC);
  90.         INTL_Encoding_ID encoding;
  91.         unsigned char *text;
  92.         INTL_CompoundStrIterator iter;
  93.         for(iter = INTL_CompoundStrFirstStr((INTL_CompoundStrIterator) str, &encoding, &text);
  94.                 iter != NULL;
  95.                     iter = INTL_CompoundStrNextStr(iter, &encoding, &text))
  96.         {
  97.             SIZE textsize;
  98.             pDC->SelectObject(ufm.GetCsidFont(encoding));
  99.             int textlength = XP_STRLEN((char*)text);
  100.             VERIFY(CIntlWin::TextOut(encoding, hDC, nXStart, nYStart + ufm.CacularAscentDelta(hDC), (char*)text, textlength));
  101.             VERIFY(CIntlWin::GetTextExtentPoint(encoding, hDC, (char*)text, textlength, &textsize));
  102.             nXStart += textsize.cx;
  103.         }
  104.         INTL_CompoundStrDestroy(str);
  105.     }
  106.     // The destructor of CUnicodeVirtualFontMgr will reset the font.
  107.     return TRUE;
  108. }
  109.  
  110. BOOL CIntlUnicodeVirtualFontStrategy::GetTextExtentPointWithCyaFont(CyaFont *theNSFont, HDC hDC, LPCSTR pString, int iLength, LPSIZE pSize)
  111. {
  112.  
  113.     pSize->cx = 0;
  114.     pSize->cy = 0;
  115.  
  116.     CUnicodeVirtualFontMgr ufm(hDC);    // Change this to get attribute from CyaFont instead of hDC 
  117.     INTL_CompoundStr *str = INTL_CompoundStrFromUTF8(pString, iLength);
  118.     if(str)
  119.     {    
  120.         CDC * pDC = CDC::FromHandle(hDC);
  121.         INTL_Encoding_ID encoding;
  122.         unsigned char *text;
  123.         INTL_CompoundStrIterator iter;
  124.         for(iter = INTL_CompoundStrFirstStr((INTL_CompoundStrIterator) str, &encoding, &text);
  125.                 iter != NULL;
  126.                     iter = INTL_CompoundStrNextStr(iter, &encoding, &text))
  127.         {
  128.             SIZE textsize;
  129.             CyaFont *encodingFont = ufm.GetCsidCyaFont(hDC, encoding);
  130.  
  131.             encodingFont->PrepareDrawText( hDC );    // Select the Font in the DC
  132.  
  133.             int textlength = XP_STRLEN((char*)text);
  134.             VERIFY(CIntlWin::GetTextExtentPointWithCyaFont(encodingFont ,encoding, hDC, (char*)text, textlength, &textsize));
  135.  
  136.             encodingFont->EndDrawText( hDC );        // Restore the old  Font in the DC
  137.             
  138.             pSize->cx += textsize.cx;
  139.             if(textsize.cy > pSize->cy)
  140.                 pSize->cy = textsize.cy;
  141.         }
  142.         INTL_CompoundStrDestroy(str);
  143.     }
  144.  
  145.     // The destructor of CUnicodeVirtualFontMgr will reset the font.
  146.     return TRUE;
  147.     
  148. }    // GetTextExtentPointWithCyaFont
  149.  
  150. BOOL CIntlUnicodeVirtualFontStrategy::TextOutWithCyaFont(CyaFont *theNSFont, HDC hDC, int nXStart, int nYStart, LPCSTR lpString, int iLength)
  151. {
  152.     // Save the current font on stack
  153.     CUnicodeVirtualFontMgr ufm(hDC);    /* May be we should pass theNSFont instead */
  154.     INTL_CompoundStr *str = INTL_CompoundStrFromUTF8(lpString, iLength);
  155.     if(str)
  156.     {    
  157.         CDC * pDC = CDC::FromHandle(hDC);
  158.         INTL_Encoding_ID encoding;
  159.         unsigned char *text;
  160.         INTL_CompoundStrIterator iter;
  161.         for(iter = INTL_CompoundStrFirstStr((INTL_CompoundStrIterator) str, &encoding, &text);
  162.                 iter != NULL;
  163.                     iter = INTL_CompoundStrNextStr(iter, &encoding, &text))
  164.         {
  165.             CyaFont *encodingFont = ufm.GetCsidCyaFont(hDC, encoding);
  166.             SIZE textsize;
  167.             int textlength = XP_STRLEN((char*)text);
  168.  
  169.             encodingFont->PrepareDrawText( hDC );    // Select the Font in the DC
  170.             
  171.             VERIFY(CIntlWin::TextOutWithCyaFont(encodingFont,encoding, hDC, nXStart, nYStart + ufm.CacularAscentDelta(hDC), (char*)text, textlength));
  172.             VERIFY(CIntlWin::GetTextExtentPoint(encoding, hDC, (char*)text, textlength, &textsize));
  173.  
  174.             encodingFont->EndDrawText( hDC );        // Restore the old  Font in the DC
  175.             
  176.             nXStart += textsize.cx;
  177.         }
  178.         INTL_CompoundStrDestroy(str);
  179.     }
  180.     // The destructor of CUnicodeVirtualFontMgr will reset the font.
  181.     return TRUE;
  182. }    // TextOutWithCyaFont
  183.  
  184.