~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/intl/ctl/src/nsUnicodeToThaiTTF.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
/* vim:expandtab:shiftwidth=2:tabstop=2:
 
3
 */
 
4
/* ***** BEGIN LICENSE BLOCK *****
 
5
 ** Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
6
 **
 
7
 ** The contents of this file are subject to the Mozilla Public License Version
 
8
 ** 1.1 (the "License"); you may not use this file except in compliance with
 
9
 ** the License. You may obtain a copy of the License at
 
10
 ** http://www.mozilla.org/MPL/
 
11
 **
 
12
 ** Software distributed under the License is distributed on an "AS IS" basis,
 
13
 ** WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
14
 ** for the specific language governing rights and limitations under the
 
15
 ** License.
 
16
 **
 
17
 ** The Original Code is Mozilla Communicator client code.
 
18
 **
 
19
 ** The Initial Developer of the Original Code is
 
20
 ** Netscape Communications Corp.
 
21
 ** Portions created by the Initial Developer are Copyright (C) 2003
 
22
 ** the Initial Developer. All Rights Reserved.
 
23
 **
 
24
 ** XXX : for the table included below. check how to credit...
 
25
 ** Copyright (C) 1999 Red Hat Software
 
26
 ** Author: Owen Taylor <otaylor@redhat.com>
 
27
 ** 
 
28
 ** Copyright (C) 2002 NECTEC
 
29
 ** Author: Theppitak Karoonboonyanan <thep@links.nectec.or.th>
 
30
 **
 
31
 ** Copyright (c) 1996-2000 by Sun Microsystems, Inc.
 
32
 ** Author: Chookij Vanatham <Chookij.Vanatham@Eng.Sun.COM>
 
33
 **
 
34
 ** Contributor(s): 
 
35
 **   Jungshik Shin <jshin@mailaps.org> 
 
36
 **
 
37
 ** Alternatively, the contents of this file may be used under the terms of
 
38
 ** either the GNU General Public License Version 2 or later (the "GPL"), or
 
39
 ** the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
40
 ** in which case the provisions of the GPL or the LGPL are applicable instead
 
41
 ** of those above. If you wish to allow use of your version of this file only
 
42
 ** under the terms of either the GPL or the LGPL, and not to allow others to
 
43
 ** use your version of this file under the terms of the MPL, indicate your
 
44
 ** decision by deleting the provisions above and replace them with the notice
 
45
 ** and other provisions required by the GPL or the LGPL. If you do not delete
 
46
 ** the provisions above, a recipient may use your version of this file under
 
47
 ** the terms of any one of the MPL, the GPL or the LGPL.
 
48
 **
 
49
 ** ***** END LICENSE BLOCK ***** */
 
50
 
 
51
#include "nsMemory.h"
 
52
#include "nsUnicodeToThaiTTF.h"
 
53
 
 
54
// tis620-2 to Thai TTF 'custom' font encoding. Actually, most positions
 
55
// are regular code points for Thai and general punctuation marks around
 
56
// U+2000. Only about one and half dozens of PUA code points  are used.
 
57
// Copied from Pango Thai shaper (thai-xft.c)
 
58
  
 
59
const static PRUnichar gTIS620ToUnicode[] = {
 
60
    0xf700, 0xf701, 0xf702, 0xf703, 0xf704, 0x2026, 0xf705, 0xf706,
 
61
    0xf707, 0xf708, 0xf709, 0xf70a, 0xf70b, 0xf70c, 0xf70d, 0xf70e,
 
62
    0xf70f, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014,
 
63
    0xf710, 0xf711, 0xf712, 0xf713, 0xf714, 0xf715, 0xf716, 0xf717,
 
64
    0x00a0, 0x0e01, 0x0e02, 0x0e03, 0x0e04, 0x0e05, 0x0e06, 0x0e07,
 
65
    0x0e08, 0x0e09, 0x0e0a, 0x0e0b, 0x0e0c, 0x0e0d, 0x0e0e, 0x0e0f,
 
66
    0x0e10, 0x0e11, 0x0e12, 0x0e13, 0x0e14, 0x0e15, 0x0e16, 0x0e17,
 
67
    0x0e18, 0x0e19, 0x0e1a, 0x0e1b, 0x0e1c, 0x0e1d, 0x0e1e, 0x0e1f,
 
68
    0x0e20, 0x0e21, 0x0e22, 0x0e23, 0x0e24, 0x0e25, 0x0e26, 0x0e27,
 
69
    0x0e28, 0x0e29, 0x0e2a, 0x0e2b, 0x0e2c, 0x0e2d, 0x0e2e, 0x0e2f,
 
70
    0x0e30, 0x0e31, 0x0e32, 0x0e33, 0x0e34, 0x0e35, 0x0e36, 0x0e37,
 
71
    0x0e38, 0x0e39, 0x0e3a,      0,      0,      0,      0, 0x0e3f,
 
72
    0x0e40, 0x0e41, 0x0e42, 0x0e43, 0x0e44, 0x0e45, 0x0e46, 0x0e47,
 
73
    0x0e48, 0x0e49, 0x0e4a, 0x0e4b, 0x0e4c, 0x0e4d, 0x0e4e, 0x0e4f,
 
74
    0x0e50, 0x0e51, 0x0e52, 0x0e53, 0x0e54, 0x0e55, 0x0e56, 0x0e57,
 
75
    0x0e58, 0x0e59, 0x0e5a, 0x0e5b, 0xf718, 0xf719, 0xf71a,      0
 
76
};
 
77
 
 
78
//----------------------------------------------------------------------
 
79
// Class nsUnicodeToThaiTTF [implementation]
 
80
  
 
81
NS_IMPL_ISUPPORTS_INHERITED0(nsUnicodeToThaiTTF, nsUnicodeToTIS620)
 
82
 
 
83
 
 
84
NS_IMETHODIMP 
 
85
nsUnicodeToThaiTTF::Convert(const PRUnichar * aSrc, 
 
86
                            PRInt32 * aSrcLength, char * aDest, 
 
87
                            PRInt32 * aDestLength)
 
88
{
 
89
 
 
90
  PRInt32 medLen;
 
91
  char *med;
 
92
 
 
93
  GetMaxLength(aSrc, *aSrcLength, &medLen);
 
94
  NS_ASSERTION(medLen <= *aDestLength, "Insufficient buffer size");
 
95
 
 
96
  //intermediate encoder(TIS620) is a single byte converter taking half
 
97
  //the space taken by ThaiTTF encoder.
 
98
  medLen /= 2;
 
99
  if (medLen > CHAR_BUFFER_SIZE)
 
100
  {
 
101
    med = (char *) nsMemory::Alloc(*aDestLength);
 
102
    if (!med)
 
103
      return NS_ERROR_OUT_OF_MEMORY;
 
104
  }
 
105
  else 
 
106
    med = mStaticBuffer;
 
107
 
 
108
  nsresult rv = nsUnicodeToTIS620::Convert(aSrc, aSrcLength, med, &medLen);
 
109
 
 
110
  if (NS_FAILED(rv)) {
 
111
    if (med != mStaticBuffer)
 
112
      nsMemory::Free(med);
 
113
    return rv;
 
114
  }
 
115
 
 
116
  PRInt32 i, j;
 
117
 
 
118
  for (i = 0, j = 0; i < medLen; i++)
 
119
  {
 
120
    PRUnichar ucs2 = (med[i] & 0x80) ? gTIS620ToUnicode[med[i] & 0x7f] : med[i];
 
121
    aDest[j++] = PRUint8((ucs2 & 0xff00) >> 8);
 
122
    aDest[j++] = PRUint8(ucs2 & 0x00ff);
 
123
  }
 
124
 
 
125
  *aDestLength = j;
 
126
 
 
127
  if (med != mStaticBuffer)
 
128
    nsMemory::Free(med);
 
129
 
 
130
  return NS_OK;
 
131
}
 
132
 
 
133
NS_IMETHODIMP
 
134
nsUnicodeToThaiTTF::GetMaxLength(const PRUnichar * aSrc, PRInt32 aSrcLength, PRInt32 * aDestLength)
 
135
{
 
136
  // Each Thai character can generate at most two presentation forms,
 
137
  // but we're 'extending' them to 16bit shorts, which accounts for 
 
138
  // additional factor of 2.
 
139
  *aDestLength = (aSrcLength + 1) *  4; 
 
140
  
 
141
  return NS_OK;
 
142
}
 
143
 
 
144
NS_IMETHODIMP 
 
145
nsUnicodeToThaiTTF::SetOutputErrorBehavior(PRInt32 aBehavior, 
 
146
                                           nsIUnicharEncoder *aEncoder, 
 
147
                                           PRUnichar aChar)
 
148
{
 
149
  if (aBehavior == kOnError_CallBack && aEncoder == nsnull)
 
150
    return NS_ERROR_NULL_POINTER;
 
151
  NS_IF_RELEASE(aEncoder);
 
152
  mErrEncoder = aEncoder;
 
153
  NS_IF_ADDREF(aEncoder);
 
154
  
 
155
  mErrBehavior = aBehavior;
 
156
  mErrChar = aChar;
 
157
  return NS_OK;
 
158
}