001/* 002 * Copyright 1999-2004 The Apache Software Foundation. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.itextpdf.text.pdf.hyphenation; 018 019import java.io.File; 020import java.io.FileInputStream; 021import java.io.InputStream; 022import java.util.Hashtable; 023 024import com.itextpdf.text.pdf.BaseFont; 025 026/** 027 * This class is the main entry point to the hyphenation package. 028 * You can use only the static methods or create an instance. 029 * 030 * @author Carlos Villegas <cav@uniscope.co.jp> 031 */ 032public class Hyphenator { 033 034 /** TODO: Don't use statics */ 035 private static Hashtable<String, HyphenationTree> hyphenTrees = new Hashtable<String, HyphenationTree>(); 036 037 private HyphenationTree hyphenTree = null; 038 private int remainCharCount = 2; 039 private int pushCharCount = 2; 040 private static final String defaultHyphLocation = "com/itextpdf/text/pdf/hyphenation/hyph/"; 041 042 /** Holds value of property hyphenDir. */ 043 private static String hyphenDir = ""; 044 045 /** 046 * @param lang 047 * @param country 048 * @param leftMin 049 * @param rightMin 050 */ 051 public Hyphenator(String lang, String country, int leftMin, 052 int rightMin) { 053 hyphenTree = getHyphenationTree(lang, country); 054 remainCharCount = leftMin; 055 pushCharCount = rightMin; 056 } 057 058 /** 059 * @param lang 060 * @param country 061 * @return the hyphenation tree 062 */ 063 public static HyphenationTree getHyphenationTree(String lang, 064 String country) { 065 String key = lang; 066 // check whether the country code has been used 067 if (country != null && !country.equals("none")) { 068 key += "_" + country; 069 } 070 // first try to find it in the cache 071 if (hyphenTrees.containsKey(key)) { 072 return hyphenTrees.get(key); 073 } 074 if (hyphenTrees.containsKey(lang)) { 075 return hyphenTrees.get(lang); 076 } 077 078 HyphenationTree hTree = getResourceHyphenationTree(key); 079 if (hTree == null) 080 hTree = getFileHyphenationTree(key); 081 // put it into the pattern cache 082 if (hTree != null) { 083 hyphenTrees.put(key, hTree); 084 } 085 return hTree; 086 } 087 088 /** 089 * @param key 090 * @return a hyphenation tree 091 */ 092 public static HyphenationTree getResourceHyphenationTree(String key) { 093 try { 094 InputStream stream = BaseFont.getResourceStream(defaultHyphLocation + key + ".xml"); 095 if (stream == null && key.length() > 2) 096 stream = BaseFont.getResourceStream(defaultHyphLocation + key.substring(0, 2) + ".xml"); 097 if (stream == null) 098 return null; 099 HyphenationTree hTree = new HyphenationTree(); 100 hTree.loadSimplePatterns(stream); 101 return hTree; 102 } 103 catch (Exception e) { 104 return null; 105 } 106 } 107 108 /** 109 * @param key 110 * @return a hyphenation tree 111 */ 112 public static HyphenationTree getFileHyphenationTree(String key) { 113 try { 114 if (hyphenDir == null) 115 return null; 116 InputStream stream = null; 117 File hyphenFile = new File(hyphenDir, key + ".xml"); 118 if (hyphenFile.canRead()) 119 stream = new FileInputStream(hyphenFile); 120 if (stream == null && key.length() > 2) { 121 hyphenFile = new File(hyphenDir, key.substring(0, 2) + ".xml"); 122 if (hyphenFile.canRead()) 123 stream = new FileInputStream(hyphenFile); 124 } 125 if (stream == null) 126 return null; 127 HyphenationTree hTree = new HyphenationTree(); 128 hTree.loadSimplePatterns(stream); 129 return hTree; 130 } 131 catch (Exception e) { 132 return null; 133 } 134 } 135 136 /** 137 * @param lang 138 * @param country 139 * @param word 140 * @param leftMin 141 * @param rightMin 142 * @return a hyphenation object 143 */ 144 public static Hyphenation hyphenate(String lang, String country, 145 String word, int leftMin, 146 int rightMin) { 147 HyphenationTree hTree = getHyphenationTree(lang, country); 148 if (hTree == null) { 149 //log.error("Error building hyphenation tree for language " 150 // + lang); 151 return null; 152 } 153 return hTree.hyphenate(word, leftMin, rightMin); 154 } 155 156 /** 157 * @param lang 158 * @param country 159 * @param word 160 * @param offset 161 * @param len 162 * @param leftMin 163 * @param rightMin 164 * @return a hyphenation object 165 */ 166 public static Hyphenation hyphenate(String lang, String country, 167 char[] word, int offset, int len, 168 int leftMin, int rightMin) { 169 HyphenationTree hTree = getHyphenationTree(lang, country); 170 if (hTree == null) { 171 //log.error("Error building hyphenation tree for language " 172 // + lang); 173 return null; 174 } 175 return hTree.hyphenate(word, offset, len, leftMin, rightMin); 176 } 177 178 /** 179 * @param min 180 */ 181 public void setMinRemainCharCount(int min) { 182 remainCharCount = min; 183 } 184 185 /** 186 * @param min 187 */ 188 public void setMinPushCharCount(int min) { 189 pushCharCount = min; 190 } 191 192 /** 193 * @param lang 194 * @param country 195 */ 196 public void setLanguage(String lang, String country) { 197 hyphenTree = getHyphenationTree(lang, country); 198 } 199 200 /** 201 * @param word 202 * @param offset 203 * @param len 204 * @return a hyphenation object 205 */ 206 public Hyphenation hyphenate(char[] word, int offset, int len) { 207 if (hyphenTree == null) { 208 return null; 209 } 210 return hyphenTree.hyphenate(word, offset, len, remainCharCount, 211 pushCharCount); 212 } 213 214 /** 215 * @param word 216 * @return a hyphenation object 217 */ 218 public Hyphenation hyphenate(String word) { 219 if (hyphenTree == null) { 220 return null; 221 } 222 return hyphenTree.hyphenate(word, remainCharCount, pushCharCount); 223 } 224 225 /** Getter for property hyphenDir. 226 * @return Value of property hyphenDir. 227 */ 228 public static String getHyphenDir() { 229 return hyphenDir; 230 } 231 232 /** Setter for property hyphenDir. 233 * @param _hyphenDir New value of property hyphenDir. 234 */ 235 public static void setHyphenDir(String _hyphenDir) { 236 hyphenDir = _hyphenDir; 237 } 238 239}