Home | History | Annotate | Download | only in layout
      1 //  2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html#License
      3 /*
      4  *******************************************************************************
      5  * Copyright (C) 1996-2005, International Business Machines Corporation and    *
      6  * others. All Rights Reserved.                                                *
      7  *******************************************************************************
      8  *
      9  */
     10 
     11 package com.ibm.icu.dev.tool.layout;
     12 
     13 import com.ibm.icu.impl.Utility;
     14 import com.ibm.icu.lang.UCharacter;
     15 import com.ibm.icu.text.UnicodeSet;
     16 
     17 /**
     18  * @author emader
     19  *
     20  * TODO To change the template for this generated type comment go to
     21  * Window - Preferences - Java - Code Style - Code Templates
     22  */
     23 public class BuildMirroringTables extends ModuleWriter
     24 {
     25     public BuildMirroringTables()
     26     {
     27         super();
     28     }
     29 
     30     public void dump(String name, int[] array, int length, int valuesPerLine)
     31     {
     32         StringBuffer line = new StringBuffer("    ");
     33 
     34         output.println("const LEUnicode32 DefaultCharMapper::" + name + "[] = {");
     35 
     36         for (int i = 0; i < length; i += 1) {
     37 
     38             if (i > 0 && i % valuesPerLine == 0) {
     39                 output.println(line.toString());
     40                 line.setLength(4);
     41             }
     42 
     43             line.append("0x" + Utility.hex(array[i], 4));
     44             line.append(", ");
     45         }
     46 
     47         line.setLength(line.length() - 2);
     48 
     49         output.println(line.toString());
     50         output.println("};\n");
     51     }
     52 
     53     public void writeMirroredDataFile(String filename)
     54     {
     55         UnicodeSet mirrored = new UnicodeSet("[\\p{Bidi_Mirrored}]");
     56         int count = mirrored.size();
     57         int[] chars   = new int[count];
     58         int[] mirrors = new int[count];
     59         int total = 0;
     60 
     61         System.out.println("There are " + count + " mirrored characters.");
     62 
     63         for(int i = 0; i < count; i += 1) {
     64             int ch = mirrored.charAt(i);
     65             int m  = UCharacter.getMirror(ch);
     66 
     67             if (ch != m) {
     68                 chars[total] = ch & 0xFFFF;
     69                 mirrors[total++] = m & 0xFFFF;
     70             }
     71         }
     72 
     73         System.out.println("There are " + total + " characters with a different mirror.\n");
     74 
     75         openFile(filename);
     76         writeHeader(null, includeFiles);
     77 
     78 
     79         dump("mirroredChars", chars, total, 8);
     80 
     81         System.out.println();
     82 
     83         dump("srahCderorrim", mirrors, total, 8);
     84 
     85         output.println("const le_int32 DefaultCharMapper::mirroredCharsCount = " + total + ";\n");
     86 
     87         writeTrailer();
     88         closeFile();
     89     }
     90 
     91     private static String includeFiles[] = {"LETypes.h", "DefaultCharMapper.h"};
     92 
     93     public static void main(String[] args)
     94     {
     95         BuildMirroringTables bmt = new BuildMirroringTables();
     96 
     97         bmt.writeMirroredDataFile("MirroredCharData.cpp");
     98     }
     99 }
    100