1 /* LineBreakIterator.java - Default word BreakIterator.
2 Copyright (C) 1999, 2001, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.java.text;
41 import java.text.CharacterIterator;
44 * @author Tom Tromey <tromey@cygnus.com>
45 * @date March 22, 1999
46 * Written using The Unicode Standard, Version 2.0.
49 public class LineBreakIterator extends BaseBreakIterator
51 public Object clone ()
53 return new LineBreakIterator (this);
56 public LineBreakIterator ()
60 private LineBreakIterator (LineBreakIterator other)
62 iter = (CharacterIterator) other.iter.clone();
65 // Some methods to tell us different properties of characters.
66 private final boolean isNb (char c)
68 return (c == 0x00a0 // NO-BREAK SPACE
69 || c == 0x2011 // NON-BREAKING HYPHEN
70 || c == 0xfeff); // ZERO WITH NO-BREAK SPACE
72 private final boolean isClose (int type)
74 return (type == Character.END_PUNCTUATION
75 // Unicode book says "comma, period, ...", which I take to
77 || type == Character.OTHER_PUNCTUATION);
79 private final boolean isIdeo (char c)
81 return (c >= 0x3040 && c <= 0x309f // Hiragana
82 || c >= 0x30a0 && c <= 0x30ff // Katakana
83 || c >= 0x4e00 && c <= 0x9fff // Han
84 || c >= 0x3100 && c <= 0x312f); // Bopomofo
89 int end = iter.getEndIndex();
90 if (iter.getIndex() == end)
93 while (iter.getIndex() < end)
95 char c = iter.current();
96 int type = Character.getType(c);
100 if (n == CharacterIterator.DONE
101 || type == Character.PARAGRAPH_SEPARATOR
102 || type == Character.LINE_SEPARATOR)
105 // Handle two cases where we must scan for non-spacing marks.
106 int start = iter.getIndex();
107 if (type == Character.SPACE_SEPARATOR
108 || type == Character.START_PUNCTUATION
111 while (n != CharacterIterator.DONE
112 && Character.getType(n) == Character.NON_SPACING_MARK)
114 if (n == CharacterIterator.DONE)
117 if (type == Character.SPACE_SEPARATOR)
119 int nt = Character.getType(n);
120 if (nt != Character.NON_SPACING_MARK
121 && nt != Character.SPACE_SEPARATOR
125 else if (type == Character.START_PUNCTUATION)
129 // Open punctuation followed by non spacing marks
130 // and then ideograph does not have a break in
131 // it. So skip all this.
132 start = iter.getIndex();
137 // Ideograph preceded this character.
138 if (isClose (Character.getType(n)))
142 iter.setIndex(start);
145 return iter.getIndex();
148 public int previous ()
150 int start = iter.getBeginIndex();
151 if (iter.getIndex() == start)
154 while (iter.getIndex() >= start)
156 char c = iter.previous();
157 if (c == CharacterIterator.DONE)
159 int type = Character.getType(c);
161 char n = iter.previous();
162 if (n == CharacterIterator.DONE)
166 int nt = Character.getType(n);
167 // Break after paragraph separators.
168 if (nt == Character.PARAGRAPH_SEPARATOR
169 || nt == Character.LINE_SEPARATOR)
172 // Skip non-spacing marks.
173 int init = iter.getIndex();
174 while (n != CharacterIterator.DONE && nt == Character.NON_SPACING_MARK)
177 nt = Character.getType(n);
180 if (nt == Character.SPACE_SEPARATOR
181 && type != Character.SPACE_SEPARATOR
182 && type != Character.NON_SPACING_MARK
185 if (! isClose (type) && isIdeo (n))
187 if (isIdeo (c) && nt != Character.START_PUNCTUATION)
192 return iter.getIndex();