diff options
author | David A. Madore <david+git@madore.org> | 2010-04-29 00:20:54 +0200 |
---|---|---|
committer | David A. Madore <david+git@madore.org> | 2010-04-29 00:20:54 +0200 |
commit | c01f6ce7f6c70b72ceef439996c146f7b4744f98 (patch) | |
tree | fd41251a702ec8d4db0efcb01844a8c47caccdf0 /src/org/madore/android/unicodeMap/UnicodeDatabase.java | |
parent | 7108dafb73f4c06261a56b819f4530ffd17bf6e7 (diff) | |
download | UnicodeMap-c01f6ce7f6c70b72ceef439996c146f7b4744f98.tar.gz UnicodeMap-c01f6ce7f6c70b72ceef439996c146f7b4744f98.tar.bz2 UnicodeMap-c01f6ce7f6c70b72ceef439996c146f7b4744f98.zip |
More proper handling of CJK and Hangul.
Diffstat (limited to 'src/org/madore/android/unicodeMap/UnicodeDatabase.java')
-rw-r--r-- | src/org/madore/android/unicodeMap/UnicodeDatabase.java | 88 |
1 files changed, 71 insertions, 17 deletions
diff --git a/src/org/madore/android/unicodeMap/UnicodeDatabase.java b/src/org/madore/android/unicodeMap/UnicodeDatabase.java index df91010..efdf5eb 100644 --- a/src/org/madore/android/unicodeMap/UnicodeDatabase.java +++ b/src/org/madore/android/unicodeMap/UnicodeDatabase.java @@ -1,6 +1,8 @@ package org.madore.android.unicodeMap; +import java.util.Collections; import java.util.Iterator; +import java.util.Comparator; import java.util.List; import java.util.ArrayList; import java.util.NoSuchElementException; @@ -145,27 +147,23 @@ public class UnicodeDatabase { protected final static String[] queryColumns = { "id", "name", "category" }; - public static UnicodeCharacter getSingleSpecial(int codePoint) { - if ( UnicodeCharacter.SpecialRange.isCjkUnifiedIdeograph(codePoint) ) - return new UnicodeCharacter(codePoint, - UnicodeCharacter.SpecialRange.cjkUnifiedIdeographName(codePoint), - UnicodeCharacter.Category.OTHER_LETTER); - if ( UnicodeCharacter.SpecialRange.isHangulSyllable(codePoint) ) - return new UnicodeCharacter(codePoint, - UnicodeCharacter.SpecialRange.hangulSyllableName(codePoint), - UnicodeCharacter.Category.OTHER_LETTER); - return null; + public static UnicodeCharacter getSingleSpecial(UnicodeCharacter.SpecialRange rng, + int codePoint) { + return new UnicodeCharacter(codePoint, + rng.getName(codePoint), + rng.getCategory()); } public UnicodeCharacter getSingle(int codePoint) { - UnicodeCharacter ch = getSingleSpecial(codePoint); - if ( ch != null ) - return ch; + for ( UnicodeCharacter.SpecialRange rng : UnicodeCharacter.SpecialRange.values() ) + if ( rng.belongs(codePoint) ) + return getSingleSpecial(rng, codePoint); final Cursor c = db.query(UNICODE_TABLE_NAME, queryColumns, "id=?", new String[] { Integer.toString(codePoint) }, null, null, null, null); + UnicodeCharacter ch; if ( c.getCount() > 0 ) { c.moveToFirst(); ch = new UnicodeCharacter(c.getInt(0), @@ -230,13 +228,51 @@ public class UnicodeDatabase { } } + private static class SpecialRangeIterable + implements Iterable<UnicodeCharacter> { + protected final UnicodeCharacter.SpecialRange rng; + protected final int from; protected final int to; + public SpecialRangeIterable(UnicodeCharacter.SpecialRange rng, + int from, int to) { + this.rng = rng; + this.from = from; + this.to = to; + } + public Iterator<UnicodeCharacter> iterator() { + return (new Iterator<UnicodeCharacter>() { + int i = from; + public boolean hasNext() { return i < to; } + public UnicodeCharacter next() { + if ( i < to ) + return getSingleSpecial(rng, i++); + else + throw new NoSuchElementException(); + } + public void remove() { + throw new UnsupportedOperationException(); + } + }); + } + } + public Iterable<UnicodeCharacter> getRange(int from, int to) { + List<UnicodeCharacter> annoyance = null; + boolean muchAnnoyance = false; for ( UnicodeCharacter.SpecialRange rng : UnicodeCharacter.SpecialRange.values() ) { if ( rng.inside(from, to) ) { - List<UnicodeCharacter> list = new ArrayList<UnicodeCharacter>(to-from); - for ( int i=from ; i<to ; i++ ) - list.add(getSingleSpecial(i)); - return list; + return new SpecialRangeIterable(rng, from, to); + } else if ( rng.interCount(from, to) > 0 ) { + if ( annoyance == null ) + annoyance + = new ArrayList<UnicodeCharacter>(rng.interCount(from, to)); + else + muchAnnoyance = true; + int from0 = (from<rng.getFrom())?rng.getFrom():from; + int to0 = (to>rng.getTo())?rng.getTo():to; + Iterable<UnicodeCharacter> rangeIt + = new SpecialRangeIterable(rng, from0, to0); + for ( UnicodeCharacter ch : rangeIt ) + annoyance.add(ch); } } final Cursor c @@ -245,6 +281,24 @@ public class UnicodeDatabase { new String[] { Integer.toString(from), Integer.toString(to) }, null, null, "id", null); + if ( annoyance != null ) { + Iterable<UnicodeCharacter> cursorIt + = new CursorIterable(c); + for ( UnicodeCharacter ch : cursorIt ) { + annoyance.add(ch); + muchAnnoyance = true; + } + if ( muchAnnoyance ) + Collections.sort(annoyance, new Comparator<UnicodeCharacter>() { + public int compare(UnicodeCharacter ch1, + UnicodeCharacter ch2) { + int cp1 = ch1.getCodePoint(); + int cp2 = ch2.getCodePoint(); + return (cp1<cp2)?-1:(cp1>cp2)?1:0; + } + }); + return annoyance; + } return new CursorIterable(c); } |