From c01f6ce7f6c70b72ceef439996c146f7b4744f98 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Thu, 29 Apr 2010 00:20:54 +0200 Subject: More proper handling of CJK and Hangul. --- .../madore/android/unicodeMap/UnicodeDatabase.java | 88 +++++++++++++++++----- 1 file changed, 71 insertions(+), 17 deletions(-) (limited to 'src/org/madore/android/unicodeMap/UnicodeDatabase.java') 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 { + 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 iterator() { + return (new Iterator() { + 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 getRange(int from, int to) { + List annoyance = null; + boolean muchAnnoyance = false; for ( UnicodeCharacter.SpecialRange rng : UnicodeCharacter.SpecialRange.values() ) { if ( rng.inside(from, to) ) { - List list = new ArrayList(to-from); - for ( int i=from ; i 0 ) { + if ( annoyance == null ) + annoyance + = new ArrayList(rng.interCount(from, to)); + else + muchAnnoyance = true; + int from0 = (fromrng.getTo())?rng.getTo():to; + Iterable 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 cursorIt + = new CursorIterable(c); + for ( UnicodeCharacter ch : cursorIt ) { + annoyance.add(ch); + muchAnnoyance = true; + } + if ( muchAnnoyance ) + Collections.sort(annoyance, new Comparator() { + public int compare(UnicodeCharacter ch1, + UnicodeCharacter ch2) { + int cp1 = ch1.getCodePoint(); + int cp2 = ch2.getCodePoint(); + return (cp1cp2)?1:0; + } + }); + return annoyance; + } return new CursorIterable(c); } -- cgit v1.2.3