home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / utility / UUTF8TextHandler.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  3.3 KB  |  120 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. //    UUTF8TextHandler.cp
  21. // ===========================================================================
  22. //
  23. //    Authror: Frank Tang ftang@netscape.com
  24. #include "UUTF8TextHandler.h"
  25. #include "UUnicodeTextHandler.h"
  26.  
  27. UUTF8TextHandler* UUTF8TextHandler::fTheOnlyInstance = NULL;
  28. UUTF8TextHandler* UUTF8TextHandler::Instance()
  29. {
  30.     if(fTheOnlyInstance == NULL)
  31.         fTheOnlyInstance = new UUTF8TextHandler();
  32.     return fTheOnlyInstance;
  33. }
  34.  
  35. #define MXS(c,x,s)            (((c) - (x)) <<  (s))
  36. #define M80S(c,s)            MXS(c,0x80,s)    
  37. #define UTF8_1_to_UCS2(in)    ((uint16) (in)[0])
  38. #define UTF8_2_to_UCS2(in)    ((uint16) (MXS((in)[0],0xC0, 6) | M80S((in)[1], 0)))
  39. #define UTF8_3_to_UCS2(in)    ((uint16) (MXS((in)[0],0xE0,12) | M80S((in)[1], 6) | M80S((in)[2], 0) ))
  40. uint32 UUTF8TextHandler::UTF8ToUCS2(unsigned char* text, int length, INTL_Unicode *ucs2, uint32 ubuflen)
  41. {
  42.     unsigned char* p = text;
  43.     unsigned char* last = text + length;
  44.     uint32 ul;
  45.     for(p = text, ul = 0; (p < last) && (ul < ubuflen) ; )
  46.     {
  47.         if( (*p) < 0x80)
  48.         {    
  49.             ucs2[ul++] = UTF8_1_to_UCS2(p);
  50.             p += 1;
  51.         }
  52.         else if( (*p) < 0xc0 )
  53.         {    
  54.             Assert_( (*p) );    // Invalid UTF8 First Byte
  55.             ucs2[ul++] = '?';
  56.             p += 1;
  57.         }
  58.         else if( (*p) < 0xe0)
  59.         {
  60.             ucs2[ul++] = UTF8_2_to_UCS2(p);
  61.             p += 2;
  62.         }
  63.         else if( (*p) < 0xf0)
  64.         {
  65.             ucs2[ul++] = UTF8_3_to_UCS2(p);
  66.             p += 3;
  67.         }
  68.         else if( (*p) < 0xf8)
  69.         {
  70.             ucs2[ul++] = '?';
  71.             p += 4;
  72.         }
  73.         else if( (*p) < 0xfc)
  74.         {
  75.             ucs2[ul++] = '?';
  76.             p += 5;
  77.         }
  78.         else if( (*p) < 0xfe)
  79.         {
  80.             ucs2[ul++] = '?';
  81.             p += 6;
  82.         }
  83.         else
  84.         {
  85.             Assert_( (*p) );    // Invalid UTF8 First Byte
  86.             ucs2[ul++] = '?';
  87.             p += 1;
  88.         }
  89.     }
  90.     return ul;
  91. }
  92. void UUTF8TextHandler::DrawText(UFontSwitcher* fs, char* text, int len)
  93. {
  94.     if(text[0] != 0)
  95.     {
  96.         uint32 ucs2len;
  97.         INTL_Unicode ucs2buf[512];
  98.         ucs2len = UTF8ToUCS2((unsigned char*)text, len, ucs2buf, 512);
  99.         // redirect to the UUnicodeTextHandler
  100.         UUnicodeTextHandler::Instance()->DrawUnicode(fs, ucs2buf, ucs2len);
  101.     }
  102. }
  103. short UUTF8TextHandler::TextWidth(UFontSwitcher* fs, char* text, int len)
  104. {
  105.     if(text[0] != 0)
  106.     {
  107.         uint32 ucs2len;
  108.         INTL_Unicode ucs2buf[512];
  109.         // redirect to the UUnicodeTextHandler
  110.         ucs2len  = UTF8ToUCS2((unsigned char*)text, len, ucs2buf, 512 );
  111.         return UUnicodeTextHandler::Instance()->UnicodeWidth(fs, ucs2buf, ucs2len);
  112.     }
  113.     return 0;
  114. }
  115. void UUTF8TextHandler::GetFontInfo    (UFontSwitcher* fs, FontInfo* fi)
  116. {
  117.     // redirect to the UUnicodeTextHandler
  118.     UUnicodeTextHandler::Instance()->GetFontInfo(fs, fi);
  119. }
  120.