home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / utility / UUnicodeTextHandler.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  3.3 KB  |  100 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. //    UUnicodeTextHandler.cp
  21. // ===========================================================================
  22. //
  23. //    Authror: Frank Tang ftang@netscape.com
  24.  
  25. /*-----------------------------------------------------------------------------
  26.     UUnicodeTextHandler
  27.     Switch font to draw Unicode (UCS2)
  28.     It use Singleton (See Design Patterns by Erich Gamma )
  29. -----------------------------------------------------------------------------*/
  30. #include "UUnicodeTextHandler.h"
  31.  
  32. UUnicodeTextHandler* UUnicodeTextHandler::fTheOnlyInstance = NULL;
  33. UUnicodeTextHandler* UUnicodeTextHandler::Instance()
  34. {
  35.     if(fTheOnlyInstance == NULL)
  36.         fTheOnlyInstance = new UUnicodeTextHandler();
  37.     return fTheOnlyInstance;
  38. }
  39. void UUnicodeTextHandler::DrawUnicode(UFontSwitcher* fs, INTL_Unicode* unicode, int len)
  40. {
  41.     INTL_CompoundStr* cs = INTL_CompoundStrFromUnicode(unicode, len);
  42.     if(cs)
  43.     {
  44.         INTL_Encoding_ID encoding;
  45.         unsigned char* outtext;
  46.         INTL_CompoundStrIterator iter;
  47.         for(iter = INTL_CompoundStrFirstStr((INTL_CompoundStrIterator)cs, &encoding , &outtext);
  48.                 iter != NULL;
  49.                     iter = INTL_CompoundStrNextStr(iter, &encoding, &outtext))
  50.         {
  51.             if((outtext) && (*outtext))
  52.             {
  53.                 fs->EncodingTextFont(encoding);
  54.                 ::DrawText(outtext, 0, XP_STRLEN((char*)outtext));
  55.             }
  56.         }
  57.         INTL_CompoundStrDestroy(cs);
  58.     }
  59. }
  60. short UUnicodeTextHandler::UnicodeWidth(UFontSwitcher* fs, INTL_Unicode* unicode, int len)
  61. {
  62.     short width = 0;
  63.     INTL_CompoundStr* cs = INTL_CompoundStrFromUnicode(unicode, len);
  64.     if(cs)
  65.     {
  66.         INTL_Encoding_ID encoding;
  67.         unsigned char* outtext;
  68.         INTL_CompoundStrIterator iter;
  69.         for(iter = INTL_CompoundStrFirstStr((INTL_CompoundStrIterator)cs, &encoding , &outtext);
  70.                 iter != NULL;
  71.                     iter = INTL_CompoundStrNextStr(iter, &encoding, &outtext))
  72.         {
  73.             if((outtext) && (*outtext))
  74.             {
  75.                 fs->EncodingTextFont(encoding);
  76.                 width += ::TextWidth(outtext, 0, XP_STRLEN((char*)outtext));
  77.             }
  78.         }
  79.         INTL_CompoundStrDestroy(cs);
  80.     }
  81.     return width;
  82. }
  83. void UUnicodeTextHandler::GetFontInfo    (UFontSwitcher* fs, FontInfo* fi)
  84. {
  85.     FontInfo cur;
  86.     int16 *list;
  87.     int16 num;
  88.     list = INTL_GetUnicodeCSIDList(&num);
  89.     fi->ascent =    fi->descent =    fi->widMax =    fi->leading = 0;
  90.     for(int i = 0; i < num; i++)
  91.     {
  92.         fs->EncodingTextFont(list[i]);
  93.         ::GetFontInfo(&cur);
  94.         if(fi->ascent  < cur.ascent)    fi->ascent  = cur.ascent;
  95.         if(fi->descent < cur.descent)    fi->descent = cur.descent;
  96.         if(fi->widMax  < cur.widMax)    fi->widMax  = cur.widMax;
  97.         if(fi->leading < cur.leading)    fi->leading = cur.leading;
  98.     }
  99. }
  100.