TextTrimming.cs
// 
// このコードは、DioDocs for Imaging のサンプルの一部として提供されています。
// © MESCIUS inc. All rights reserved.
// 
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Imaging;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Drawing;
using GCTEXT = GrapeCity.Documents.Text;
using GCDRAW = GrapeCity.Documents.Drawing;

namespace DsImagingWeb.Demos
{
    // このサンプルでは、文字列が割り当てられた領域に収まらない場合に
    // 省略記号(…)を表示する方法を示します。
    public class TextTrimming
    {
        public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
        {
            var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, true, dpi, dpi);
            var Inch = dpi;
            const float fontSize = 12;
            using (var g = bmp.CreateGraphics(Color.White))
            {
                var str = "This is a long line of text which does not fit in the allocated space.";
                var wid = Inch * 4;
                var dy = 0.3f;

                var rc = Common.Util.AddNote(
                    "TextLayout allows displaying ellipsis (or other character) " +
                    "at the end of a text line that did not fit in the allocated space.\n" +
                    "To use trimming, set TrimmingGranularity to Character or Word " +
                    "(the default is None). Trimming will kick in if WrapMode is NoWrap, " +
                    "and the text is too long. Wrapped text may also display trimming if " +
                    "the layout width is too narrow to fit a single word.\n" +
                    "Below are examples of text untrimmed, trimmed to character and to word. " +
                    "The next line demonstrates a different trimming character (tilde in this case). " +
                    "Finally, the last line shows how to trim text (respecting TrimmingGranularity) " +
                    "without adding any trimming character.",
                    g);
                var top = rc.Bottom + 36;

                var ip = new PointF(rc.Left, top);

                var tl = g.CreateTextLayout();
                tl.DefaultFormat.Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSerif-Regular.ttf"));
                tl.DefaultFormat.FontSize = fontSize;
                tl.MaxWidth = wid;
                tl.WrapMode = WrapMode.NoWrap;

                // TrimmingGranularityはデフォルトでNoneに設定されています。
                tl.Append(str);
                tl.PerformLayout(true);
                g.DrawTextLayout(tl, ip);
                ip.Y += tl.ContentHeight + dy;

                // 文字単位でのトリミングです。
                tl.TrimmingGranularity = TrimmingGranularity.Character;
                // PerformLayoutのrecalculateGlyphsBeforeLayoutパラメータは、
                // テキストやフォントが変更されていない場合、初回の呼び出し以降はfalseに設定できることに注意してください。
                tl.PerformLayout(false);
                g.DrawTextLayout(tl, ip);
                ip.Y += tl.ContentHeight + dy;

                // 単語単位でのトリミングです。
                tl.TrimmingGranularity = TrimmingGranularity.Word;
                tl.PerformLayout(false);
                g.DrawTextLayout(tl, ip);
                ip.Y += tl.ContentHeight + dy;

                // tl.EllipsisCharCodeはデフォルトでHorizontalEllipsis (0x2026) です。
                // チルダに変更します。
                tl.EllipsisCharCode = 0x007E;
                tl.PerformLayout(false);
                g.DrawTextLayout(tl, ip);
                ip.Y += tl.ContentHeight + dy;

                // 最後に、tl.EllipsisCharCodeを0に設定することで、
                // トリミング文字をレンダリングせずにテキストをトリミングできます。
                tl.EllipsisCharCode = 0;
                tl.PerformLayout(false);
                g.DrawTextLayout(tl, ip);
                ip.Y += tl.ContentHeight + dy;

                g.DrawRectangle(new RectangleF(rc.Left, top, wid, ip.Y - top), Color.OrangeRed);

                // 画像全体の周囲に境界線を描画します。
                g.DrawRectangle(new RectangleF(0, 0, bmp.Width, bmp.Height), Color.DarkSlateBlue, 4);
            }

            return bmp;
        }
    }
}