Hyphenation.cs
//
// This code is part of Document Solutions for PDF demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Drawing;

namespace DsPdfWeb.Demos.Basics
{
    // This sample shows how to hyphenate text that contains soft hyphen characters (0x00AD).
    // Breaks will be inserted at soft hyphen positions (if they are present in the text)
    // if TextLayout.WrapMode is set to WordWrap. Two properties are provided to control
    // hyphenation:
    // - TextLayout.SoftHyphenReplacementCharCode: specifies the character used as replacement
    //   for soft hyphen when breaking words across lines. By default this property is 0x002D
    //   (the Unicode hyphen-minus character). Setting this property to 0 breaks words without
    //   showing any visible hyphen character. Setting it to -1 prevents breaking words at soft
    //   hyphens).
    // - TextLayout.LinesBetweenConsecutiveHyphens: specifies the minimum number of non-
    //   hyphenated lines between lines ending in a hyphen. By default this property is zero.
    public class Hyphenation
    {
        public int CreatePDF(Stream stream)
        {
            // The online hypho-o tool
            // was used to insert soft hyphens in the sample text from WordCharWrap:
            var str =
                "Lose noth­ing in your doc­u­ments! Grape­City Doc­u­ments for PDF " +
                "in­cludes text and para­graph format­ting, spe­cial char­ac­ters, " +
                "mul­tiple lan­guages, RTL sup­port, ver­tic­al and ro­tated text " +
                "on all sup­por­ted plat­forms.";
            // Replace HTML soft hyphens with Unicode ones:
            str = str.Replace("­", "\u00AD");

            var doc = new GcPdfDocument();
            var page = doc.NewPage();
            var g = page.Graphics;

            var tl = g.CreateTextLayout();
            tl.Append(str);
            tl.DefaultFormat.Font = StandardFonts.Times;
            tl.DefaultFormat.FontSize = 12;
            tl.MaxWidth = 72 * 3;

            // By default 0x002D (hyphen-minus) will be used as the hyphenation character
            // when breaking a line at a soft hyphen (0x00AD):
            tl.PerformLayout(true);

            var dy = tl.Lines[0].Height + 72 / 16;
            var rc = new RectangleF(72, 72 + dy, tl.MaxWidth.Value, 72 * 1.4F);

            g.DrawString("Default hyphenation:", tl.DefaultFormat, new PointF(rc.Left, rc.Top - dy));
            g.DrawTextLayout(tl, rc.Location);
            g.DrawRectangle(rc, Color.CornflowerBlue);

            rc.Offset(0, 72 * 2);
            // This will avoid hyphenating two consecutive lines:
            tl.LinesBetweenConsecutiveHyphens = 1;
            // Changing hyphenation options requires RecalculateGlyphs():
            tl.PerformLayout(true);
            g.DrawString("LinesBetweenConsecutiveHyphens: 1", tl.DefaultFormat, new PointF(rc.Left, rc.Top - dy));
            g.DrawTextLayout(tl, rc.Location);
            g.DrawRectangle(rc, Color.CornflowerBlue);

            rc.Offset(0, 72 * 2);
            // Reset previous setting:
            tl.LinesBetweenConsecutiveHyphens = 0;
            // Prevent hyphenating words at all:
            tl.SoftHyphenReplacementCharCode = -1;
            // Changing hyphenation options requires RecalculateGlyphs():
            tl.PerformLayout(true);
            g.DrawString("SoftHyphenReplacementCharCode: -1", tl.DefaultFormat, new PointF(rc.Left, rc.Top - dy));
            g.DrawTextLayout(tl, rc.Location);
            g.DrawRectangle(rc, Color.CornflowerBlue);

            // Done:
            doc.Save(stream);
            return doc.Pages.Count;
        }
    }
}