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) 1998-2015, International Business Machines Corporation and
      6  * others. All Rights Reserved.
      7  *******************************************************************************
      8  */
      9 package com.ibm.icu.dev.tool.layout;
     10 
     11 import java.util.Date;
     12 
     13 import com.ibm.icu.lang.UScript;
     14 import com.ibm.icu.text.MessageFormat;
     15 import com.ibm.icu.util.VersionInfo;
     16 
     17 public class ScriptIDModuleWriter extends ScriptModuleWriter
     18 {
     19     private static final boolean WITH_STABLE_TAGS = false;
     20 
     21     public ScriptIDModuleWriter(ScriptData scriptData, LanguageData languageData)
     22     {
     23         super(scriptData, languageData);
     24     }
     25 
     26     public void writeScriptHeader(String fileName)
     27     {
     28         int minScript = scriptData.getMinValue();
     29         int maxScript = scriptData.getMaxValue();
     30 
     31         openFile(fileName);
     32         writeHeader("__LESCRIPTS_H", includeFiles, scriptBrief);
     33 
     34         totalScript = maxScript;
     35         what = "script";
     36         readFile(ScriptAndLanguages, what);
     37         String checkICUVersion, previousVersion;
     38         int arrayListIndex=0, numberOfScripts;
     39         boolean initialheader = false;
     40         boolean newScripts = false;
     41 
     42         if(totalScript>previousTotalScripts){
     43             newScripts = true;
     44         }
     45         //Processing old scripts
     46         MessageFormat format = new MessageFormat(scriptPreamble);
     47         for(int script=minScript;script<=previousTotalScripts;){
     48              checkICUVersion = (String)scriptVersionNumber.get(arrayListIndex);
     49              checkICUVersion = checkICUVersion.substring(checkICUVersion.indexOf("_")+1);
     50              previousVersion = checkICUVersion.substring(0, checkICUVersion.indexOf("="));
     51              numberOfScripts = Integer.parseInt(checkICUVersion.substring(checkICUVersion.indexOf("=")+1));
     52 
     53              Object args[] = { previousVersion };
     54              //Check for the initial header. It should be written only one time
     55              if(!initialheader){
     56                  output.println(format.format(args));
     57                  initialheader = true;
     58              }else if(WITH_STABLE_TAGS) {
     59                  // Unicode API constants are "born stable".
     60                  format = new MessageFormat(scriptPreambleStable);
     61                  output.println(format.format(args));
     62              }
     63 
     64              for(int i=0;i<numberOfScripts;i++){
     65                  output.print("    ");
     66                  output.print(scriptData.getTagLabel(script));
     67                  output.print("ScriptCode = ");
     68 
     69                  if (script < 10) {
     70                      output.print(" ");
     71                  }
     72 
     73                  output.print(script);
     74                  output.println(",");
     75                  if(script == UScript.INHERITED) {
     76                      output.println("    qaaiScriptCode = zinhScriptCode,  /* manually added alias, for API stability */");
     77                  }
     78                  script++;
     79              }
     80              arrayListIndex++;
     81         }
     82 
     83         if(newScripts){//Processing newly added scripts
     84             if(WITH_STABLE_TAGS) {
     85                 format = new MessageFormat(scriptPreambleStable);
     86                 Object args[] = { VersionInfo.ICU_VERSION };
     87                 output.println(format.format(args));
     88             }
     89 
     90             for (int script = previousTotalScripts+1; script <= totalScript; script += 1) {
     91                 output.print("    ");
     92                 output.print(scriptData.getTagLabel(script));
     93                 output.print("ScriptCode = ");
     94 
     95                 if (script < 10) {
     96                     output.print(" ");
     97                 }
     98 
     99                 output.print(script);
    100                 output.println(",");
    101             }
    102         }
    103         output.println();
    104         output.print("    scriptCodeCount = ");
    105         output.println(maxScript - minScript + 1);
    106         output.println(postamble);
    107 
    108         writeTrailer();
    109         closeFile();
    110 
    111     }
    112 
    113     public void writeLanguageHeader(String fileName)
    114     {
    115         int minLanguage = languageData.getMinValue();
    116         int maxLanguage = languageData.getMaxValue();
    117 
    118         openFile(fileName);
    119         writeHeader("__LELANGUAGES_H", includeFiles, languageBrief);
    120 
    121         totalLanguage = maxLanguage;
    122         what = "languages";
    123         readFile(ScriptAndLanguages, what);
    124         String checkICUVersion, previousVersion;
    125         int arrayListIndex=0, numberOfLanguages;
    126         boolean initialheader = false;
    127         boolean newLanguage = false;
    128 
    129         if(totalLanguage>previousTotalLanguages){
    130             newLanguage = true;
    131         }
    132         //Processing old languages
    133         MessageFormat format = new MessageFormat(languagePreamble);
    134         for(int language=minLanguage;language<=previousTotalLanguages;){
    135              checkICUVersion = (String)languageVersionNumber.get(arrayListIndex);
    136              checkICUVersion = checkICUVersion.substring(checkICUVersion.indexOf("_")+1);
    137              previousVersion = checkICUVersion.substring(0, checkICUVersion.indexOf("="));
    138              numberOfLanguages = Integer.parseInt(checkICUVersion.substring(checkICUVersion.indexOf("=")+1));
    139 
    140              Object args[] = { previousVersion };
    141 
    142              //Check for the initial header. It should be written only one time
    143              if(!initialheader){
    144                  output.println(format.format(args));
    145                  initialheader = true;
    146              } else if(WITH_STABLE_TAGS) {
    147                  // Unicode API constants are "born stable".
    148                  format = new MessageFormat(languagePreambleStable);
    149                  output.println(format.format(args));
    150              }
    151 
    152              for(int i=0;i<numberOfLanguages;i++){
    153                  output.print("    ");
    154                  output.print(languageData.getTagLabel(language).toLowerCase());
    155                  output.print("LanguageCode = ");
    156 
    157                  if (language < 10) {
    158                      output.print(" ");
    159                  }
    160 
    161                  output.print(language);
    162                  output.println(",");
    163                  language++;
    164              }
    165              arrayListIndex++;
    166         }
    167         if(newLanguage){
    168             //Processing newly added languages
    169             if(WITH_STABLE_TAGS) {
    170                 format = new MessageFormat(languagePreambleStable);
    171                 Object args[] = { VersionInfo.ICU_VERSION };
    172                 output.println(format.format(args));
    173             }
    174 
    175             for (int language = previousTotalLanguages+1; language <= totalLanguage; language += 1) {
    176                 output.print("    ");
    177                 output.print(languageData.getTagLabel(language).toLowerCase());
    178                 output.print("ScriptCode = ");
    179 
    180                 if (language < 10) {
    181                     output.print(" ");
    182                 }
    183 
    184                 output.print(language);
    185                 output.println(",");
    186             }
    187         }
    188         output.println();
    189         output.print("    languageCodeCount = ");
    190         output.println(maxLanguage - minLanguage + 1);
    191 
    192         output.println(postamble);
    193 
    194         writeTrailer();
    195         closeFile();
    196     }
    197 
    198     private static final String[] includeFiles = {"LETypes.h"};
    199 
    200     private static final String scriptPreamble =
    201     "/**\n" +
    202     " * Constants for Unicode script values, generated using\n" +
    203     " * ICU4J''s <code>UScript</code> class.\n" +
    204     " *\n" +
    205     " * @deprecated ICU 54. See '{@link icu::LayoutEngine}'\n" + // was " * @stable ICU {0}\n"
    206     " */\n" +
    207     "\n" +
    208     "enum ScriptCodes '{'";
    209 
    210     private static final String scriptBrief =
    211     "/**\n" +
    212     " * \\file\n" +
    213     " * \\brief C++ API: Constants for Unicode script values\n" +
    214     " */\n" +
    215     "\n";
    216 
    217 
    218     private static final String languagePreamble =
    219     "/**\n" +
    220     " * A provisional list of language codes. For now,\n" +
    221     " * this is just a list of languages which the LayoutEngine\n" +
    222     " * supports.\n" +
    223     " *\n" +
    224     " * @deprecated ICU 54. See '{@link icu::LayoutEngine}'\n" + // was " * @stable ICU {0}\n"
    225     " */\n" +
    226     "\n" +
    227     "enum LanguageCodes '{'";
    228 
    229     private static final String languageBrief =
    230         "/**\n" +
    231         " * \\file\n" +
    232         " * \\brief C++ API: List of language codes for LayoutEngine\n" +
    233         " */\n" +
    234         "\n";
    235 
    236     private static final String postamble =
    237     "};\n";
    238 
    239     private static final String ScriptAndLanguages = "ScriptAndLanguages";
    240     private static String ICUVersion = "ICU Version="+VersionInfo.ICU_VERSION.getMajor()+"."+VersionInfo.ICU_VERSION.getMinor();
    241     private static int totalScript;
    242     private static int totalLanguage;
    243     private static String what;
    244 
    245     public void updateScriptAndLanguages(){
    246         openScriptAndLanguages(ScriptAndLanguages);
    247         MessageFormat format = new MessageFormat(moduleHeader);
    248         Object args[] = {new Date(System.currentTimeMillis())};
    249 
    250         System.out.print("Updating file "+ScriptAndLanguages);
    251 
    252         updateFile.print(format.format(args));
    253         updateFile.println(ICUVersion);
    254         updateFile.println("Total Script="+totalScript);
    255         updateFile.println("Total Language="+totalLanguage);
    256         updateFile.println("Scripts={");
    257         for(int i=0;i<scriptVersionNumber.size();i++){
    258             updateFile.println(scriptVersionNumber.get(i));
    259         }
    260         if(totalScript>previousTotalScripts){
    261             updateFile.println("         ICU_"+VersionInfo.ICU_VERSION.getMajor()+"."+VersionInfo.ICU_VERSION.getMinor()+"="+(totalScript-previousTotalScripts));
    262 
    263         }
    264         updateFile.println("        }");
    265         updateFile.println("Languages={");
    266         for(int i=0;i<languageVersionNumber.size();i++){
    267             updateFile.println(languageVersionNumber.get(i));
    268         }
    269         if(totalLanguage>previousTotalLanguages){
    270             updateFile.println("           ICU_"+VersionInfo.ICU_VERSION.getMajor()+"."+VersionInfo.ICU_VERSION.getMinor()+"="+(totalLanguage-previousTotalLanguages));
    271         }
    272         updateFile.println("          }");
    273         scriptVersionNumber.clear();
    274         languageVersionNumber.clear();
    275         updateFile.close();
    276 
    277         System.out.println("Done");
    278     }
    279 
    280     private static final String scriptPreambleStable =
    281         "/**\n" +
    282         " * @stable ICU {0}\n" +
    283         " */\n";// +
    284 
    285     private static final String languagePreambleStable =
    286         "/**\n" +
    287         " * @stable ICU {0}\n" +
    288         " */\n";// +
    289 }
    290