Back to index

lightning-sunbird  0.9+nobinonly
nsUnicodeToThaiTTF.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* vim:expandtab:shiftwidth=2:tabstop=2:
00003  */
00004 /* ***** BEGIN LICENSE BLOCK *****
00005  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00006  *
00007  * The contents of this file are subject to the Mozilla Public License Version
00008  * 1.1 (the "License"); you may not use this file except in compliance with
00009  * the License. You may obtain a copy of the License at
00010  * http://www.mozilla.org/MPL/
00011  *
00012  * Software distributed under the License is distributed on an "AS IS" basis,
00013  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00014  * for the specific language governing rights and limitations under the
00015  * License.
00016  *
00017  * The Original Code is Mozilla Communicator client code.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corp.
00021  * Portions created by the Initial Developer are Copyright (C) 2003
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * XXX : for the table included below. check how to credit...
00025  * Copyright (C) 1999 Red Hat Software
00026  * Author: Owen Taylor <otaylor@redhat.com>
00027  * 
00028  * Copyright (C) 2002 NECTEC
00029  * Author: Theppitak Karoonboonyanan <thep@links.nectec.or.th>
00030  *
00031  * Copyright (c) 1996-2000 by Sun Microsystems, Inc.
00032  * Author: Chookij Vanatham <Chookij.Vanatham@Eng.Sun.COM>
00033  *
00034  * Contributor(s): 
00035  *   Jungshik Shin <jshin@mailaps.org> 
00036  *
00037  * Alternatively, the contents of this file may be used under the terms of
00038  * either the GNU General Public License Version 2 or later (the "GPL"), or
00039  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00040  * in which case the provisions of the GPL or the LGPL are applicable instead
00041  * of those above. If you wish to allow use of your version of this file only
00042  * under the terms of either the GPL or the LGPL, and not to allow others to
00043  * use your version of this file under the terms of the MPL, indicate your
00044  * decision by deleting the provisions above and replace them with the notice
00045  * and other provisions required by the GPL or the LGPL. If you do not delete
00046  * the provisions above, a recipient may use your version of this file under
00047  * the terms of any one of the MPL, the GPL or the LGPL.
00048  *
00049  * ***** END LICENSE BLOCK ***** */
00050 
00051 #include "nsMemory.h"
00052 #include "nsUnicodeToThaiTTF.h"
00053 
00054 // tis620-2 to Thai TTF 'custom' font encoding. Actually, most positions
00055 // are regular code points for Thai and general punctuation marks around
00056 // U+2000. Only about one and half dozens of PUA code points  are used.
00057 // Copied from Pango Thai shaper (thai-xft.c)
00058   
00059 const static PRUnichar gTIS620ToUnicode[] = {
00060     0xf700, 0xf701, 0xf702, 0xf703, 0xf704, 0x2026, 0xf705, 0xf706,
00061     0xf707, 0xf708, 0xf709, 0xf70a, 0xf70b, 0xf70c, 0xf70d, 0xf70e,
00062     0xf70f, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014,
00063     0xf710, 0xf711, 0xf712, 0xf713, 0xf714, 0xf715, 0xf716, 0xf717,
00064     0x00a0, 0x0e01, 0x0e02, 0x0e03, 0x0e04, 0x0e05, 0x0e06, 0x0e07,
00065     0x0e08, 0x0e09, 0x0e0a, 0x0e0b, 0x0e0c, 0x0e0d, 0x0e0e, 0x0e0f,
00066     0x0e10, 0x0e11, 0x0e12, 0x0e13, 0x0e14, 0x0e15, 0x0e16, 0x0e17,
00067     0x0e18, 0x0e19, 0x0e1a, 0x0e1b, 0x0e1c, 0x0e1d, 0x0e1e, 0x0e1f,
00068     0x0e20, 0x0e21, 0x0e22, 0x0e23, 0x0e24, 0x0e25, 0x0e26, 0x0e27,
00069     0x0e28, 0x0e29, 0x0e2a, 0x0e2b, 0x0e2c, 0x0e2d, 0x0e2e, 0x0e2f,
00070     0x0e30, 0x0e31, 0x0e32, 0x0e33, 0x0e34, 0x0e35, 0x0e36, 0x0e37,
00071     0x0e38, 0x0e39, 0x0e3a,      0,      0,      0,      0, 0x0e3f,
00072     0x0e40, 0x0e41, 0x0e42, 0x0e43, 0x0e44, 0x0e45, 0x0e46, 0x0e47,
00073     0x0e48, 0x0e49, 0x0e4a, 0x0e4b, 0x0e4c, 0x0e4d, 0x0e4e, 0x0e4f,
00074     0x0e50, 0x0e51, 0x0e52, 0x0e53, 0x0e54, 0x0e55, 0x0e56, 0x0e57,
00075     0x0e58, 0x0e59, 0x0e5a, 0x0e5b, 0xf718, 0xf719, 0xf71a,      0
00076 };
00077 
00078 //----------------------------------------------------------------------
00079 // Class nsUnicodeToThaiTTF [implementation]
00080   
00081 NS_IMPL_ISUPPORTS_INHERITED0(nsUnicodeToThaiTTF, nsUnicodeToTIS620)
00082 
00083 
00084 NS_IMETHODIMP 
00085 nsUnicodeToThaiTTF::Convert(const PRUnichar * aSrc, 
00086                             PRInt32 * aSrcLength, char * aDest, 
00087                             PRInt32 * aDestLength)
00088 {
00089 
00090   PRInt32 medLen;
00091   char *med;
00092 
00093   GetMaxLength(aSrc, *aSrcLength, &medLen);
00094   NS_ASSERTION(medLen <= *aDestLength, "Insufficient buffer size");
00095 
00096   //intermediate encoder(TIS620) is a single byte converter taking half
00097   //the space taken by ThaiTTF encoder.
00098   medLen /= 2;
00099   if (medLen > CHAR_BUFFER_SIZE)
00100   {
00101     med = (char *) nsMemory::Alloc(*aDestLength);
00102     if (!med)
00103       return NS_ERROR_OUT_OF_MEMORY;
00104   }
00105   else 
00106     med = mStaticBuffer;
00107 
00108   nsresult rv = nsUnicodeToTIS620::Convert(aSrc, aSrcLength, med, &medLen);
00109 
00110   if (NS_FAILED(rv)) {
00111     if (med != mStaticBuffer)
00112       nsMemory::Free(med);
00113     return rv;
00114   }
00115 
00116   PRInt32 i, j;
00117 
00118   for (i = 0, j = 0; i < medLen; i++)
00119   {
00120     PRUnichar ucs2 = (med[i] & 0x80) ? gTIS620ToUnicode[med[i] & 0x7f] : med[i];
00121     aDest[j++] = PRUint8((ucs2 & 0xff00) >> 8);
00122     aDest[j++] = PRUint8(ucs2 & 0x00ff);
00123   }
00124 
00125   *aDestLength = j;
00126 
00127   if (med != mStaticBuffer)
00128     nsMemory::Free(med);
00129 
00130   return NS_OK;
00131 }
00132 
00133 NS_IMETHODIMP
00134 nsUnicodeToThaiTTF::GetMaxLength(const PRUnichar * aSrc, PRInt32 aSrcLength, PRInt32 * aDestLength)
00135 {
00136   // Each Thai character can generate at most two presentation forms,
00137   // but we're 'extending' them to 16bit shorts, which accounts for 
00138   // additional factor of 2.
00139   *aDestLength = (aSrcLength + 1) *  4; 
00140   
00141   return NS_OK;
00142 }
00143 
00144 NS_IMETHODIMP 
00145 nsUnicodeToThaiTTF::SetOutputErrorBehavior(PRInt32 aBehavior, 
00146                                            nsIUnicharEncoder *aEncoder, 
00147                                            PRUnichar aChar)
00148 {
00149   if (aBehavior == kOnError_CallBack && aEncoder == nsnull)
00150     return NS_ERROR_NULL_POINTER;
00151   mErrEncoder = aEncoder;
00152   mErrBehavior = aBehavior;
00153   mErrChar = aChar;
00154   return NS_OK;
00155 }