TextTrimming.vb
''
'' This code is part of GrapeCity Documents for Imaging samples.
'' Copyright (c) GrapeCity, Inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports System.Numerics
Imports GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Imaging

'' This sample demonstrates how to display ellipsis
'' if a string does Not fit in the allocated space.
Public Class TextTrimming
    Function GenerateImage(
            Optional pixelWidth As Integer = 1024,
            Optional pixelHeight As Integer = 1024,
            Optional opaque As Boolean = True,
            Optional dpiX As Single = 96,
            Optional dpiY As Single = 96) As GcBitmap
        Dim bmp = New GcBitmap(pixelWidth, pixelHeight, True, dpiX, dpiY)
        Dim Inch = dpiX
        Const fontSize = 12
        Using g = bmp.CreateGraphics(Color.White)
            Dim str = "This is a long line of text which does not fit in the allocated space."
            Dim wid = Inch * 4
            Dim dy = 0.3F

            Dim rc = Util.AddNote(
                "TextLayout allows to display ellipsis (or other character) " +
                "at the end of a text line that did not fit in the allocated space." + vbCrLf +
                "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 may also display trimming if " +
                "the layout width is too narrow to fit a single word." + vbCrLf +
                "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)
            Dim top = rc.Bottom + 36

            Dim ip = New PointF(rc.Left, top)

            Dim tl = g.CreateTextLayout()
            tl.DefaultFormat.Font = Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf"))
            tl.DefaultFormat.FontSize = fontSize
            tl.MaxWidth = wid
            tl.WrapMode = WrapMode.NoWrap

            '' TrimmingGranularity Is None by default
            tl.Append(str)
            tl.PerformLayout(True)
            g.DrawTextLayout(tl, ip)
            ip.Y += tl.ContentHeight + dy

            '' Character trimming
            tl.TrimmingGranularity = TrimmingGranularity.Character
            '' NOTE that the recalculateGlyphsBeforeLayout parameter to PerformLayout
            '' may be false after the first call, as the text/font has Not changed:
            tl.PerformLayout(False)
            g.DrawTextLayout(tl, ip)
            ip.Y += tl.ContentHeight + dy

            '' Word trimming
            tl.TrimmingGranularity = TrimmingGranularity.Word
            tl.PerformLayout(False)
            g.DrawTextLayout(tl, ip)
            ip.Y += tl.ContentHeight + dy

            '' tl.EllipsisCharCode Is HorizontalEllipsis (0x2026) by default.
            '' Change it to a tilde:
            tl.EllipsisCharCode = &H7E
            tl.PerformLayout(False)
            g.DrawTextLayout(tl, ip)
            ip.Y += tl.ContentHeight + dy

            '' Finally, we may set tl.EllipsisCharCode to 0 to trim text
            '' without rendering any trimming character:
            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)

            '' Draw border around the whole image
            g.DrawRectangle(New RectangleF(0, 0, bmp.Width, bmp.Height), Color.DarkSlateBlue, 4)
        End Using
        '' Done
        Return bmp
    End Function
End Class