Example #1
0
        /** Hyphenates a word and returns the first part of it. To get
         * the second part of the hyphenated word call <CODE>getHyphenatedWordPost()</CODE>.
         * @param word the word to hyphenate
         * @param font the font used by this word
         * @param fontSize the font size used by this word
         * @param remainingWidth the width available to fit this word in
         * @return the first part of the hyphenated word including
         * the hyphen symbol, if any
         */
        virtual public string GetHyphenatedWordPre(string word, BaseFont font, float fontSize, float remainingWidth)
        {
            post = word;
            string hyphen      = this.HyphenSymbol;
            float  hyphenWidth = font.GetWidthPoint(hyphen, fontSize);

            if (hyphenWidth > remainingWidth)
            {
                return("");
            }
            Hyphenation hyphenation = hyphenator.Hyphenate(word);

            if (hyphenation == null)
            {
                return("");
            }
            int len = hyphenation.Length;
            int k;

            for (k = 0; k < len; ++k)
            {
                if (font.GetWidthPoint(hyphenation.GetPreHyphenText(k), fontSize) + hyphenWidth > remainingWidth)
                {
                    break;
                }
            }
            --k;
            if (k < 0)
            {
                return("");
            }
            post = hyphenation.GetPostHyphenText(k);
            return(hyphenation.GetPreHyphenText(k) + hyphen);
        }
Example #2
0
        public void Dictionary()
        {
            //ExStart
            //ExFor:Hyphenation.IsDictionaryRegistered(String)
            //ExFor:Hyphenation.RegisterDictionary(String, String)
            //ExFor:Hyphenation.UnregisterDictionary(String)
            //ExSummary:Shows how to perform and verify hyphenation dictionary registration.
            // Register a dictionary file from the local file system to the "de-CH" locale
            Hyphenation.RegisterDictionary("de-CH", MyDir + "hyph_de_CH.dic");

            // This method can be used to verify that a language has a matching registered hyphenation dictionary
            Assert.True(Hyphenation.IsDictionaryRegistered("de-CH"));

            // The dictionary file contains a long list of words in a specified language, and in this case it is German
            // These words define a set of rules for hyphenating text (splitting words across lines)
            // If we open a document with text of a language matching that of a registered dictionary,
            // that dictionary's hyphenation rules will be applied and visible upon saving
            Document doc = new Document(MyDir + "German text.docx");

            doc.Save(ArtifactsDir + "Hyphenation.Dictionary.Registered.pdf");

            // We can also un-register a dictionary to disable these effects on any documents opened after the operation
            Hyphenation.UnregisterDictionary("de-CH");

            Assert.False(Hyphenation.IsDictionaryRegistered("de-CH"));

            doc = new Document(MyDir + "German text.docx");
            doc.Save(ArtifactsDir + "Hyphenation.Dictionary.Unregistered.pdf");
            //ExEnd
        }
Example #3
0
        [Test] //ExSkip
        public void RegisterDictionary()
        {
            // Set up a callback that tracks warnings that occur during hyphenation dictionary registration
            WarningInfoCollection warningInfoCollection = new WarningInfoCollection();

            Hyphenation.WarningCallback = warningInfoCollection;

            // Register an English (US) hyphenation dictionary by stream
            Stream dictionaryStream = new FileStream(MyDir + "hyph_en_US.dic", FileMode.Open, FileAccess.Read);

            Hyphenation.RegisterDictionary("en-US", dictionaryStream);

            // No warnings detected
            Assert.AreEqual(0, warningInfoCollection.Count);

            // Open a document with a German locale that might not get automatically hyphenated by Microsoft Word an english machine
            Document doc = new Document(MyDir + "German text.docx");

            // To hyphenate that document upon saving, we need a hyphenation dictionary for the "de-CH" language code
            // This callback will handle the automatic request for that dictionary
            Hyphenation.Callback = new CustomHyphenationDictionaryRegister();

            // When we save the document, it will be hyphenated according to rules defined by the dictionary known by our callback
            doc.Save(ArtifactsDir + "Hyphenation.RegisterDictionary.pdf");

            // This dictionary contains two identical patterns, which will trigger a warning
            Assert.AreEqual(1, warningInfoCollection.Count);
            Assert.AreEqual(WarningType.MinorFormattingLoss, warningInfoCollection[0].WarningType);
            Assert.AreEqual(WarningSource.Layout, warningInfoCollection[0].Source);
            Assert.AreEqual("Hyphenation dictionary contains duplicate patterns. The only first found pattern will be used. " +
                            "Content can be wrapped differently.", warningInfoCollection[0].Description);
        }
Example #4
0
        public void Dictionary()
        {
            //ExStart
            //ExFor:Hyphenation.IsDictionaryRegistered(String)
            //ExFor:Hyphenation.RegisterDictionary(String, String)
            //ExFor:Hyphenation.UnregisterDictionary(String)
            //ExSummary:Shows how to register a hyphenation dictionary.
            // A hyphenation dictionary contains a list of strings that define hyphenation rules for the dictionary's language.
            // When a document contains lines of text in which a word could be split up and continued on the next line,
            // hyphenation will look through the dictionary's list of strings for that word's substrings.
            // If the dictionary contains a substring, then hyphenation will split the word across two lines
            // by the substring, and add a hyphen to the first half.
            // Register a dictionary file from the local file system to the "de-CH" locale.
            Hyphenation.RegisterDictionary("de-CH", MyDir + "hyph_de_CH.dic");

            Assert.True(Hyphenation.IsDictionaryRegistered("de-CH"));

            // Open a document containing text with a locale matching that of our dictionary,
            // and save it to a fixed-page save format. The text in that document will be hyphenated.
            Document doc = new Document(MyDir + "German text.docx");

            Assert.True(doc.FirstSection.Body.FirstParagraph.Runs.OfType <Run>().All(
                            r => r.Font.LocaleId == new CultureInfo("de-CH").LCID));

            doc.Save(ArtifactsDir + "Hyphenation.Dictionary.Registered.pdf");

            // Re-load the document after un-registering the dictionary,
            // and save it to another PDF, which will not have hyphenated text.
            Hyphenation.UnregisterDictionary("de-CH");

            Assert.False(Hyphenation.IsDictionaryRegistered("de-CH"));

            doc = new Document(MyDir + "German text.docx");
            doc.Save(ArtifactsDir + "Hyphenation.Dictionary.Unregistered.pdf");
            //ExEnd

#if NET462 || NETCOREAPP2_1 || JAVA
            Aspose.Pdf.Document pdfDoc       = new Aspose.Pdf.Document(ArtifactsDir + "Hyphenation.Dictionary.Registered.pdf");
            TextAbsorber        textAbsorber = new TextAbsorber();
            textAbsorber.Visit(pdfDoc);

            Assert.True(textAbsorber.Text.Contains("La ob storen an deinen am sachen. Dop-\r\n" +
                                                   "pelte  um  da  am  spateren  verlogen  ge-\r\n" +
                                                   "kommen  achtzehn  blaulich."));

            pdfDoc       = new Aspose.Pdf.Document(ArtifactsDir + "Hyphenation.Dictionary.Unregistered.pdf");
            textAbsorber = new TextAbsorber();
            textAbsorber.Visit(pdfDoc);

            Assert.True(textAbsorber.Text.Contains("La  ob  storen  an  deinen  am  sachen. \r\n" +
                                                   "Doppelte  um  da  am  spateren  verlogen \r\n" +
                                                   "gekommen  achtzehn  blaulich."));
#endif
        }
Example #5
0
        public void HyphenateWordsOfLanguages()
        {
            //ExStart:HyphenateWordsOfLanguages
            Document doc = new Document(MyDir + "German text.docx");

            Hyphenation.RegisterDictionary("en-US", MyDir + "hyph_en_US.dic");
            Hyphenation.RegisterDictionary("de-CH", MyDir + "hyph_de_CH.dic");

            doc.Save(ArtifactsDir + "WorkingWithHyphenation.HyphenateWordsOfLanguages.pdf");
            //ExEnd:HyphenateWordsOfLanguages
        }
        public void IsDictionaryRegisteredEx()
        {
            //ExStart
            //ExFor:Hyphenation.IsDictionaryRegistered(String)
            //ExSummary:Shows how to open check if some dictionary is registered.
            Document doc = new Document(MyDir + "Document.doc");

            Hyphenation.RegisterDictionary("en-US", MyDir + "hyph_en_US.dic");

            Assert.AreEqual(true, Hyphenation.IsDictionaryRegistered("en-US"));
            //ExEnd
        }
        public void IsDictionaryRegisteredEx()
        {
            //ExStart
            //ExFor:Hyphenation.IsDictionaryRegistered(string)
            //ExSummary:Shows how to open check if some dictionary is registered.
            Document doc = new Document(MyDir + "Document.doc");

            Hyphenation.RegisterDictionary("en-US", MyDir + "hyph_en_US.dic");

            Console.WriteLine(Hyphenation.IsDictionaryRegistered("en-US")); // True
            //ExEnd
        }
Example #8
0
        public void LoadHyphenationDictionaryForLanguage()
        {
            //ExStart:LoadHyphenationDictionaryForLanguage
            Document doc = new Document(MyDir + "German text.docx");

            Stream stream = File.OpenRead(MyDir + "hyph_de_CH.dic");

            Hyphenation.RegisterDictionary("de-CH", stream);

            doc.Save(ArtifactsDir + "WorkingWithHyphenation.LoadHyphenationDictionaryForLanguage.pdf");
            //ExEnd:LoadHyphenationDictionaryForLanguage
        }
        public void UnregisteredDictionaryEx()
        {
            //ExStart
            //ExFor:Hyphenation.UnregisterDictionary(String)
            //ExSummary:Shows how to un-register a dictionary.
            Document doc = new Document(MyDir + "Document.doc");

            Hyphenation.RegisterDictionary("en-US", MyDir + "hyph_en_US.dic");

            Hyphenation.UnregisterDictionary("en-US");

            Assert.AreEqual(false, Hyphenation.IsDictionaryRegistered("en-US"));
            //ExEnd
        }
        public void UnregisterDictionaryEx()
        {
            //ExStart
            //ExFor:Hyphenation.UnregisterDictionary(string)
            //ExSummary:Shows how to un-register a dictionary
            Document doc = new Document(MyDir + "Document.doc");

            Hyphenation.RegisterDictionary("en-US", MyDir + "hyph_en_US.dic");

            Hyphenation.UnregisterDictionary("en-US");

            Console.WriteLine(Hyphenation.IsDictionaryRegistered("en-US")); // False
            //ExEnd
        }
        public static void Run()
        {
            //ExStart:LoadHyphenationDictionaryForLanguage
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_RenderingAndPrinting();

            // Load the documents which store the shapes we want to render.
            Document doc    = new Document(dataDir + "TestFile RenderShape.doc");
            Stream   stream = File.OpenRead(dataDir + @"hyph_de_CH.dic");

            Hyphenation.RegisterDictionary("de-CH", stream);

            dataDir = dataDir + "LoadHyphenationDictionaryForLanguage_out_.pdf";
            doc.Save(dataDir);
            //ExEnd:LoadHyphenationDictionaryForLanguage
            Console.WriteLine("\nHyphenation dictionary for special language loaded successfully.\nFile saved at " + dataDir);
        }
Example #12
0
        public static void Run()
        {
            //ExStart:HyphenateWordsOfLanguages
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_RenderingAndPrinting();

            // Load the documents which store the shapes we want to render.
            Document doc = new Document(dataDir + "TestFile RenderShape.doc");

            Hyphenation.RegisterDictionary("en-US", dataDir + @"hyph_en_US.dic");
            Hyphenation.RegisterDictionary("de-CH", dataDir + @"hyph_de_CH.dic");

            dataDir = dataDir + "HyphenateWordsOfLanguages_out_.pdf";
            doc.Save(dataDir);
            //ExEnd:HyphenateWordsOfLanguages
            Console.WriteLine("\nWords of special languages hyphenate successfully.\nFile saved at " + dataDir);
        }
Example #13
0
        public void RegisterDictionaryEx()
        {
            //ExStart
            //ExFor:Hyphenation.RegisterDictionary(String, Stream)
            //ExFor:Hyphenation.RegisterDictionary(String, String)
            //ExSummary:Shows how to open and register a dictionary from a file.
            Document doc = new Document(MyDir + "Document.doc");

            // Register by String
            Hyphenation.RegisterDictionary("en-US", MyDir + "hyph_en_US.dic");

            // Register by stream
            Stream dictionaryStream = new FileStream(MyDir + "hyph_de_CH.dic", FileMode.Open);

            Hyphenation.RegisterDictionary("de-CH", dictionaryStream);
            //ExEnd
        }
Example #14
0
            public void RequestDictionary(string language)
            {
                Console.Write("Hyphenation dictionary requested: " + language);

                if (Hyphenation.IsDictionaryRegistered(language))
                {
                    Console.WriteLine(", is already registered.");
                    return;
                }

                if (mHyphenationDictionaryFiles.ContainsKey(language))
                {
                    Hyphenation.RegisterDictionary(language, mHyphenationDictionaryFiles[language]);
                    Console.WriteLine(", successfully registered.");
                    return;
                }

                Console.WriteLine(", no respective dictionary file known by this Callback.");
            }
Example #15
0
        public void SuppressHyphens(bool suppressAutoHyphens)
        {
            //ExStart
            //ExFor:ParagraphFormat.SuppressAutoHyphens
            //ExSummary:Shows how to suppress hyphenation for a paragraph.
            Hyphenation.RegisterDictionary("de-CH", MyDir + "hyph_de_CH.dic");

            Assert.True(Hyphenation.IsDictionaryRegistered("de-CH"));

            // Open a document containing text with a locale matching that of our dictionary.
            // When we save this document to a fixed page save format, its text will have hyphenation.
            Document doc = new Document(MyDir + "German text.docx");

            // We can set the "SuppressAutoHyphens" property to "true" to disable hyphenation
            // for a specific paragraph while keeping it enabled for the rest of the document.
            // The default value for this property is "false",
            // which means every paragraph by default uses hyphenation if any is available.
            doc.FirstSection.Body.FirstParagraph.ParagraphFormat.SuppressAutoHyphens = suppressAutoHyphens;

            doc.Save(ArtifactsDir + "ParagraphFormat.SuppressHyphens.pdf");
            //ExEnd

#if NET462 || NETCOREAPP2_1 || JAVA
            Aspose.Pdf.Document pdfDoc       = new Aspose.Pdf.Document(ArtifactsDir + "ParagraphFormat.SuppressHyphens.pdf");
            TextAbsorber        textAbsorber = new TextAbsorber();
            textAbsorber.Visit(pdfDoc);

            if (suppressAutoHyphens)
            {
                Assert.True(textAbsorber.Text.Contains("La  ob  storen  an  deinen  am  sachen. \r\n" +
                                                       "Doppelte  um  da  am  spateren  verlogen \r\n" +
                                                       "gekommen  achtzehn  blaulich."));
            }
            else
            {
                Assert.True(textAbsorber.Text.Contains("La ob storen an deinen am sachen. Dop-\r\n" +
                                                       "pelte  um  da  am  spateren  verlogen  ge-\r\n" +
                                                       "kommen  achtzehn  blaulich."));
            }
#endif
        }
            public void RequestDictionary(string language)
            {
                string dictionaryFolder = MyDir;
                string dictionaryFullFileName;

                switch (language)
                {
                case "en-US":
                    dictionaryFullFileName = Path.Combine(dictionaryFolder, "hyph_en_US.dic");
                    break;

                case "de-CH":
                    dictionaryFullFileName = Path.Combine(dictionaryFolder, "hyph_de_CH.dic");
                    break;

                default:
                    throw new Exception($"Missing hyphenation dictionary for {language}.");
                }
                // Register dictionary for requested language.
                Hyphenation.RegisterDictionary(language, dictionaryFullFileName);
            }
Example #17
0
        private IEnumerable <string> _GetSentence(string message)
        {
            var    splitSymbol = new string[] { Environment.NewLine };
            var    tempText    = Hyphenation.GetAdjustmentTextList(message, _UI.Sentence);
            var    textList    = tempText.Split(splitSymbol, StringSplitOptions.None);
            var    dirty       = true;
            string newText;
            int    count = 0;

            while (dirty && count < 30)
            {
                count++;
                newText = string.Empty;
                for (int i = 0; i < textList.Length; i++)
                {
                    dirty = false;
                    if (i % 2 == 0 && textList[i].FirstOrDefault() != '「')
                    {
                        textList[i] = "「" + textList[i];
                        dirty       = true;
                    }

                    if (i != 0 && (i + 1) % 2 == 0 && textList[i].LastOrDefault() != '」')
                    {
                        textList[i] += "」";
                        dirty        = true;
                    }
                }
                for (int i = 0; i < textList.Length; i++)
                {
                    newText += textList[i];
                }
                tempText = Hyphenation.GetAdjustmentTextList(newText, _UI.Sentence);
                textList = tempText.Split(splitSymbol, StringSplitOptions.None);
            }
            return(textList);
        }
        private void ManipulatePdf(string dest)
        {
            // See hyphenation example of specified word in console
            // For the correct run of sample, please, add an itext.hyph dependency,
            // which could be found on the following web-page: https://mvnrepository.com/artifact/com.itextpdf/hyph
            Hyphenation s = Hyphenator.Hyphenate("de", "DE", "Leistungsscheinziffer", 2, 2);

            Console.Out.WriteLine(s);

            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document    doc    = new Document(pdfDoc);

            doc.SetMargins(0, 0, 0, 0);

            Table table = new Table(UnitValue.CreatePercentArray(1)).SetFixedLayout();

            table.SetWidth(UnitValue.CreatePercentValue(10));

            Text text = new Text("Leistungsscheinziffer");

            text.SetHyphenation(new HyphenationConfig("de", "DE", 2, 2));
            table.AddCell(new Cell().Add(new Paragraph(text)));

            Paragraph paragraph = new Paragraph();

            paragraph.SetHyphenation(new HyphenationConfig("de", "DE", 2, 2));
            paragraph.Add("Leistungsscheinziffer");
            table.AddCell(new Cell().Add(paragraph));

            // soft hyphens
            table.AddCell(new Cell().Add(new Paragraph("Le\u00adistun\u00ADgssch\u00ADeinziffe\u00ADr").SetHyphenation
                                             (new HyphenationConfig(3, 2))));

            doc.Add(table);

            doc.Close();
        }
        protected internal override void decompose()
        {
            // get the hyphenation points
            Hyphenation hyphens = hyphenator.hyphenate(termAtt.Buffer(), 0, termAtt.Length(), 1, 1);

            // No hyphen points found -> exit
            if (hyphens == null)
            {
                return;
            }

            int[] hyp = hyphens.HyphenationPoints;

            for (int i = 0; i < hyp.Length; ++i)
            {
                int           remaining         = hyp.Length - i;
                int           start             = hyp[i];
                CompoundToken longestMatchToken = null;
                for (int j = 1; j < remaining; j++)
                {
                    int partLength = hyp[i + j] - start;

                    // if the part is longer than maxSubwordSize we
                    // are done with this round
                    if (partLength > this.maxSubwordSize)
                    {
                        break;
                    }

                    // we only put subwords to the token stream
                    // that are longer than minPartSize
                    if (partLength < this.minSubwordSize)
                    {
                        // BOGUS/BROKEN/FUNKY/WACKO: somehow we have negative 'parts' according to the
                        // calculation above, and we rely upon minSubwordSize being >=0 to filter them out...
                        continue;
                    }

                    // check the dictionary
                    if (dictionary == null || dictionary.Contains(termAtt.Buffer(), start, partLength))
                    {
                        if (this.onlyLongestMatch)
                        {
                            if (longestMatchToken != null)
                            {
                                if (longestMatchToken.txt.Length() < partLength)
                                {
                                    longestMatchToken = new CompoundToken(this, start, partLength);
                                }
                            }
                            else
                            {
                                longestMatchToken = new CompoundToken(this, start, partLength);
                            }
                        }
                        else
                        {
                            tokens.AddLast(new CompoundToken(this, start, partLength));
                        }
                    }
                    else if (dictionary.contains(termAtt.buffer(), start, partLength - 1))
                    {
                        // check the dictionary again with a word that is one character
                        // shorter
                        // to avoid problems with genitive 's characters and other binding
                        // characters
                        if (this.onlyLongestMatch)
                        {
                            if (longestMatchToken != null)
                            {
                                if (longestMatchToken.txt.Length() < partLength - 1)
                                {
                                    longestMatchToken = new CompoundToken(this, start, partLength - 1);
                                }
                            }
                            else
                            {
                                longestMatchToken = new CompoundToken(this, start, partLength - 1);
                            }
                        }
                        else
                        {
                            tokens.AddLast(new CompoundToken(this, start, partLength - 1));
                        }
                    }
                }
                if (this.onlyLongestMatch && longestMatchToken != null)
                {
                    tokens.AddLast(longestMatchToken);
                }
            }
        }