1 package org.unicode.cldr.util; 2 3 import com.ibm.icu.dev.util.UnicodeMap; 4 5 public class ApproximateWidth { 6 static UnicodeMap<Integer> data = new UnicodeMap<>(); 7 static Integer defaultWidth; 8 getWidth(int cp)9 public static Integer getWidth(int cp) { 10 Integer result = data.get(cp); 11 return result == null ? defaultWidth : result; 12 } 13 14 /** Return # of ems * 10 */ getWidth(CharSequence s)15 public static int getWidth(CharSequence s) { 16 int result = 0; 17 int cp; 18 for (int i = 0; i < s.length(); i += Character.charCount(cp)) { 19 cp = Character.codePointAt(s, i); 20 result += getWidth(cp); 21 } 22 return result; 23 } 24 25 static { 26 SemiFileReader MyFileHander = 27 new SemiFileReader() { 28 @Override 29 public void handleComment(String line, int commentCharPosition) { 30 if (line.contains("@missing")) { 31 String[] items = SPLIT.split(line); 32 defaultWidth = Integer.parseInt(items[1]); 33 } 34 } 35 36 @Override 37 protected boolean handleLine( 38 int lineCount, int start, int end, String[] items) { 39 data.putAll(start, end, Integer.parseInt(items[1])); 40 return true; 41 } 42 }; 43 MyFileHander.process(ApproximateWidth.class, "data/ApproximateWidth.txt")44 MyFileHander.process(ApproximateWidth.class, "data/ApproximateWidth.txt"); 45 } 46 main(String[] args)47 public static void main(String[] args) { 48 for (String arg : args) { 49 System.out.println(arg + ":\t" + getWidth(arg)); 50 } 51 } 52 } 53