/// <summary>Load tree from the stream.</summary> /// <param name="in">the input stream to load the tree from</param> /// <param name="name">unique key representing country-language combination</param> /// <returns>the requested HyphenationTree or null if it is not available</returns> public static HyphenationTree GetHyphenationTree(Stream @in, String name) { if (@in == null) { return(null); } HyphenationTree hTree; try { hTree = new HyphenationTree(); hTree.LoadPatterns(@in, name); } catch (HyphenationException ex) { log.Error("Can't load user patterns from XML file " + name + ": " + ex.Message); return(null); } finally { try { @in.Dispose(); } catch (Exception) { } } return(hTree); }
/// <summary> /// Returns a hyphenation tree for a given language and country, /// with fallback from (lang,country) to (lang). /// </summary> /// <remarks> /// Returns a hyphenation tree for a given language and country, /// with fallback from (lang,country) to (lang). /// The hyphenation trees are cached. /// </remarks> /// <param name="lang">the language</param> /// <param name="country">the country (may be null or "none")</param> /// <param name="hyphPathNames">the map with user-configured hyphenation pattern file names</param> /// <returns>the hyphenation tree</returns> public static HyphenationTree GetHyphenationTree(String lang, String country, IDictionary <String, String> hyphPathNames) { String llccKey = HyphenationTreeCache.ConstructLlccKey(lang, country); HyphenationTreeCache cache = GetHyphenationTreeCache(); // If this hyphenation tree has been registered as missing, return immediately if (cache.IsMissing(llccKey)) { return(null); } HyphenationTree hTree = GetHyphenationTree2(lang, country, hyphPathNames); // fallback to lang only if (hTree == null && country != null && !country.Equals("none")) { String llKey = HyphenationTreeCache.ConstructLlccKey(lang, null); if (!cache.IsMissing(llKey)) { hTree = GetHyphenationTree2(lang, null, hyphPathNames); if (hTree != null && log.IsDebugEnabled) { log.Debug("Couldn't find hyphenation pattern " + "for lang=\"" + lang + "\",country=\"" + country + "\"." + " Using general language pattern " + "for lang=\"" + lang + "\" instead."); } if (hTree == null) { // no fallback; register as missing cache.NoteMissing(llKey); } else { // also register for (lang,country) cache.Cache(llccKey, hTree); } } } if (hTree == null) { // (lang,country) and (lang) tried; register as missing cache.NoteMissing(llccKey); log.Error("Couldn't find hyphenation pattern " + "for lang=\"" + lang + "\"" + (country != null && !country .Equals("none") ? ",country=\"" + country + "\"" : "") + "."); } return(hTree); }
/// <summary>Hyphenates a word.</summary> /// <param name="lang">the language</param> /// <param name="country">the optional country code (may be null or "none")</param> /// <param name="hyphPathNames">the map with user-configured hyphenation pattern file names</param> /// <param name="word">the word to hyphenate</param> /// <param name="leftMin">the minimum number of characters before the hyphenation point</param> /// <param name="rightMin">the minimum number of characters after the hyphenation point</param> /// <returns>the hyphenation result</returns> public static iText.Layout.Hyphenation.Hyphenation Hyphenate(String lang, String country, IDictionary <String , String> hyphPathNames, String word, int leftMin, int rightMin) { HyphenationTree hTree = GetHyphenationTree(lang, country, hyphPathNames); if (hTree == null) { log.Warn("Soft hyphen unicode symbols will be used as hints for hyphenation"); char softHyphen = '\u00ad'; IList <int> softHyphens = new List <int>(); int lastSoftHyphenIndex = -1; int curSoftHyphenIndex; while ((curSoftHyphenIndex = word.IndexOf(softHyphen, lastSoftHyphenIndex + 1)) > 0) { softHyphens.Add(curSoftHyphenIndex); lastSoftHyphenIndex = curSoftHyphenIndex; } int leftInd = 0; int rightInd = softHyphens.Count - 1; while (leftInd < softHyphens.Count && word.JSubstring(0, softHyphens[leftInd]).Replace(softHyphen.ToString (), "").Length < leftMin) { leftInd++; } while (rightInd >= 0 && word.Substring(softHyphens[rightInd] + 1).Replace(softHyphen.ToString(), "").Length < rightMin) { rightInd--; } if (leftInd <= rightInd) { int[] hyphenationPoints = new int[rightInd - leftInd + 1]; for (int i = leftInd; i <= rightInd; i++) { hyphenationPoints[i - leftInd] = softHyphens[i]; } return(new iText.Layout.Hyphenation.Hyphenation(word, hyphenationPoints)); } else { return(null); } } return(hTree.Hyphenate(word, leftMin, rightMin)); }
/// <summary>Hyphenates a word.</summary> /// <param name="lang">the language</param> /// <param name="country">the optional country code (may be null or "none")</param> /// <param name="hyphPathNames">the map with user-configured hyphenation pattern file names</param> /// <param name="word">the word to hyphenate</param> /// <param name="leftMin">the minimum number of characters before the hyphenation point</param> /// <param name="rightMin">the minimum number of characters after the hyphenation point</param> /// <returns>the hyphenation result</returns> public static iText.Layout.Hyphenation.Hyphenation Hyphenate(String lang, String country, IDictionary <String , String> hyphPathNames, String word, int leftMin, int rightMin) { // If a word contains soft hyphens, then hyphenation based on soft hyphens has higher priority if (WordContainsSoftHyphens(word)) { return(HyphenateBasedOnSoftHyphens(word, leftMin, rightMin)); } else { HyphenationTree hTree = null; if (lang != null) { hTree = GetHyphenationTree(lang, country, hyphPathNames); } return(hTree != null?hTree.Hyphenate(word, leftMin, rightMin) : null); } }
/// <summary>Cache a hyphenation tree under its key.</summary> /// <param name="key">the key (ex. "de_CH" or "en")</param> /// <param name="hTree">the hyphenation tree</param> public virtual void Cache(String key, HyphenationTree hTree) { hyphenTrees[key] = hTree; }
/// <summary>Cache a hyphenation tree under its key.</summary> /// <param name="key">the key (ex. "de_CH" or "en")</param> /// <param name="hTree">the hyphenation tree</param> public virtual void Cache(String key, HyphenationTree hTree) { hyphenTrees.Put(key, hTree); }