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