TextJustifyRules.vb
''
'' This code is part of Document Solutions for Imaging demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.Drawing
Imports System.IO
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Drawing
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Imaging

'' This sample demonstrates how LineBreakingRules and TextExtensionStrategy affect automatic
'' line wrapping and distributed justification.
'' LineBreakingRules.Unicode and .Simplified may break inside a run of non-whitespace
'' characters (e.g. between a Latin/digit run and an adjacent CJK run) to pack a line tighter;
'' LineBreakingRules.WhiteSpace only wraps at whitespace, keeping such runs (hyphenated
'' identifiers, codes, product names, file names, URLs) intact on one line instead.
Public Class TextJustifyRules
    Public Function GenerateImage(
        ByVal pixelSize As Size,
        ByVal dpi As Single,
        ByVal opaque As Boolean,
        Optional ByVal sampleParams As String() = Nothing) As GcBitmap

        Dim regular = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSansJP-Regular.ttf"))
        Dim bold = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSerif-Bold.ttf"))

        Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi)

        Using g = bmp.CreateGraphics(Color.White)

            Dim tl = g.CreateTextLayout()
            tl.TextAlignment = TextAlignment.Distributed
            tl.JustifiedSpaceExtension = 0F
            tl.JustifiedTextExtension = 20F

            Dim tf = New TextFormat() With {.FontSize = 14F, .Font = regular}
            Dim tfInfo = New TextFormat() With {.FontSize = 9F, .Font = bold}

            Dim marginx As Single = 372, marginy As Single = 28
            tl.MaxWidth = pixelSize.Width - marginx * 2
            Dim text = "1234567890-890-9876-54321-123986321 10abc9876 56A-345本列島で使われ456"

            Dim y As Single = marginy
            y = DrawText(g, tl, y, marginx, tf, tfInfo, text)
            tl.TextExtensionStrategy = TextExtensionStrategy.EastAsianExcel
            y = DrawText(g, tl, y, marginx, tf, tfInfo, text)
            tl.TextExtensionStrategy = TextExtensionStrategy.Excel
            y = DrawText(g, tl, y, marginx, tf, tfInfo, text)
            tl.LineBreakingRules = LineBreakingRules.Simplified
            tl.WordBoundaryRules = WordBoundaryRules.Simplified
            tl.TextExtensionStrategy = TextExtensionStrategy.Default
            y = DrawText(g, tl, y, marginx, tf, tfInfo, text)
            tl.TextExtensionStrategy = TextExtensionStrategy.EastAsianExcel
            y = DrawText(g, tl, y, marginx, tf, tfInfo, text)
            tl.TextExtensionStrategy = TextExtensionStrategy.Excel
            y = DrawText(g, tl, y, marginx, tf, tfInfo, text)
            tl.LineBreakingRules = LineBreakingRules.WhiteSpace
            tl.WordBoundaryRules = WordBoundaryRules.Unicode '' WordBoundaryRules has no WhiteSpace value - it only affects word-boundary lookups, not wrapping
            tl.TextExtensionStrategy = TextExtensionStrategy.Default
            y = DrawText(g, tl, y, marginx, tf, tfInfo, text)
            tl.TextExtensionStrategy = TextExtensionStrategy.EastAsianExcel
            y = DrawText(g, tl, y, marginx, tf, tfInfo, text)
            tl.TextExtensionStrategy = TextExtensionStrategy.Excel
            y = DrawText(g, tl, y, marginx, tf, tfInfo, text)
        End Using
        Return bmp
    End Function

    Private Shared Function DrawText(g As GcGraphics, tl As TextLayout, y As Single, marginx As Single,
        tf As TextFormat, tfInfo As TextFormat, text As String) As Single

        Dim pt = New PointF(marginx, y + 12)
        tl.Append(text, tf)
        tl.PerformLayout(True)
        Dim rc = New RectangleF(pt, New SizeF(tl.ContentWidth, tl.ContentHeight))
        g.FillRectangle(rc, Color.PaleGoldenrod)
        g.DrawString($"LineBreakingRules.{tl.LineBreakingRules}, TextExtensionStrategy.{tl.TextExtensionStrategy}:",
            tfInfo, New PointF(marginx / 2.0F, y))
        g.DrawTextLayout(tl, pt)
        tl.Clear()
        Return rc.Bottom + 8
    End Function
End Class