Add copy of .ttf font with .eot extension for testing
[wine-gecko.git] / intl / lwbrk / src / nsSampleWordBreaker.cpp
blobf4b6682c47ea0e160306b8303b86f58813dfeac9
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 #include "nsSampleWordBreaker.h"
41 #include "pratom.h"
42 #include "nsLWBRKDll.h"
43 nsSampleWordBreaker::nsSampleWordBreaker()
46 nsSampleWordBreaker::~nsSampleWordBreaker()
50 NS_IMPL_ISUPPORTS1(nsSampleWordBreaker, nsIWordBreaker)
52 PRBool nsSampleWordBreaker::BreakInBetween(
53 const PRUnichar* aText1 , PRUint32 aTextLen1,
54 const PRUnichar* aText2 , PRUint32 aTextLen2)
56 NS_PRECONDITION( nsnull != aText1, "null ptr");
57 NS_PRECONDITION( nsnull != aText2, "null ptr");
59 if(!aText1 || !aText2 || (0 == aTextLen1) || (0 == aTextLen2))
60 return PR_FALSE;
62 return (this->GetClass(aText1[aTextLen1-1]) != this->GetClass(aText2[0]));
66 #define IS_ASCII(c) (0 == ( 0xFF80 & (c)))
67 #define ASCII_IS_ALPHA(c) ((( 'a' <= (c)) && ((c) <= 'z')) || (( 'A' <= (c)) && ((c) <= 'Z')))
68 #define ASCII_IS_DIGIT(c) (( '0' <= (c)) && ((c) <= '9'))
69 #define ASCII_IS_SPACE(c) (( ' ' == (c)) || ( '\t' == (c)) || ( '\r' == (c)) || ( '\n' == (c)))
70 #define IS_ALPHABETICAL_SCRIPT(c) ((c) < 0x2E80)
72 // we change the beginning of IS_HAN from 0x4e00 to 0x3400 to relfect Unicode 3.0
73 #define IS_HAN(c) (( 0x3400 <= (c)) && ((c) <= 0x9fff))||(( 0xf900 <= (c)) && ((c) <= 0xfaff))
74 #define IS_KATAKANA(c) (( 0x30A0 <= (c)) && ((c) <= 0x30FF))
75 #define IS_HIRAGANA(c) (( 0x3040 <= (c)) && ((c) <= 0x309F))
76 #define IS_HALFWIDTHKATAKANA(c) (( 0xFF60 <= (c)) && ((c) <= 0xFF9F))
77 #define IS_THAI(c) (0x0E00 == (0xFF80 & (c) )) // Look at the higest 9 bits
79 PRUint8 nsSampleWordBreaker::GetClass(PRUnichar c)
81 // begin of the hack
83 if (IS_ALPHABETICAL_SCRIPT(c)) {
84 if(IS_ASCII(c)) {
85 if(ASCII_IS_SPACE(c)) {
86 return kWbClassSpace;
87 } else if(ASCII_IS_ALPHA(c) || ASCII_IS_DIGIT(c)) {
88 return kWbClassAlphaLetter;
89 } else {
90 return kWbClassPunct;
92 } else if(IS_THAI(c)) {
93 return kWbClassThaiLetter;
94 } else if (c == 0x00A0/*NBSP*/) {
95 return kWbClassSpace;
96 } else {
97 return kWbClassAlphaLetter;
99 } else {
100 if(IS_HAN(c)) {
101 return kWbClassHanLetter;
102 } else if(IS_KATAKANA(c)) {
103 return kWbClassKatakanaLetter;
104 } else if(IS_HIRAGANA(c)) {
105 return kWbClassHiraganaLetter;
106 } else if(IS_HALFWIDTHKATAKANA(c)) {
107 return kWbClassHWKatakanaLetter;
108 } else {
109 return kWbClassAlphaLetter;
112 return 0;
115 nsWordRange nsSampleWordBreaker::FindWord(
116 const PRUnichar* aText , PRUint32 aTextLen,
117 PRUint32 aOffset)
119 nsWordRange range;
120 NS_PRECONDITION( nsnull != aText, "null ptr");
121 NS_PRECONDITION( 0 != aTextLen, "len = 0");
122 NS_PRECONDITION( aOffset <= aTextLen, "aOffset > aTextLen");
124 range.mBegin = aTextLen + 1;
125 range.mEnd = aTextLen + 1;
127 if(!aText || aOffset > aTextLen)
128 return range;
130 PRUint8 c = this->GetClass(aText[aOffset]);
131 PRUint32 i;
132 // Scan forward
133 range.mEnd--;
134 for(i = aOffset +1;i <= aTextLen; i++)
136 if( c != this->GetClass(aText[i]))
138 range.mEnd = i;
139 break;
143 // Scan backward
144 range.mBegin = 0;
145 for(i = aOffset ;i > 0; i--)
147 if( c != this->GetClass(aText[i-1]))
149 range.mBegin = i;
150 break;
153 if(kWbClassThaiLetter == c)
155 // need to call Thai word breaker from here
156 // we should pass the whole Thai segment to the thai word breaker to find a shorter answer
158 return range;
161 PRInt32 nsSampleWordBreaker::NextWord(
162 const PRUnichar* aText, PRUint32 aLen, PRUint32 aPos)
164 PRInt8 c1, c2;
165 PRUint32 cur = aPos;
166 if (cur == aLen)
167 return NS_WORDBREAKER_NEED_MORE_TEXT;
168 c1 = this->GetClass(aText[cur]);
170 for(cur++; cur <aLen; cur++)
172 c2 = this->GetClass(aText[cur]);
173 if(c2 != c1)
174 break;
176 if(kWbClassThaiLetter == c1)
178 // need to call Thai word breaker from here
179 // we should pass the whole Thai segment to the thai word breaker to find a shorter answer
181 if (cur == aLen)
182 return NS_WORDBREAKER_NEED_MORE_TEXT;
183 return cur;
186 PRInt32 nsSampleWordBreaker::PrevWord(
187 const PRUnichar* aText, PRUint32 aLen, PRUint32 aPos)
189 PRInt8 c1, c2;
190 PRUint32 cur = aPos;
191 if (cur == aLen) {
192 if (cur == 0)
193 return NS_WORDBREAKER_NEED_MORE_TEXT;
194 --cur;
196 c1 = this->GetClass(aText[cur]);
198 for(; cur > 0; cur--)
200 c2 = this->GetClass(aText[cur-1]);
201 if(c2 != c1)
202 break;
204 if(kWbClassThaiLetter == c1)
206 // need to call Thai word breaker from here
207 // we should pass the whole Thai segment to the thai word breaker to find a shorter answer
209 if (!cur)
210 return NS_WORDBREAKER_NEED_MORE_TEXT;
211 return cur;