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

'' This example renders samples of all barcode symbologies supported by the Document Solutions Barcode library.
Public Class SupportedBarcodes
    Public Function GenerateImageStream(targetMime As String, pixelSize As Size, dpi As Single, opaque As Boolean, Optional sampleParams As String() = Nothing) As Stream
        Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi)
        Dim g = bmp.CreateGraphics(Color.White)
        Dim ms = New MemoryStream()
        Dim tw As GcTiffWriter = Nothing
        If targetMime = Util.MimeTypes.TIFF Then
            tw = New GcTiffWriter(ms)
        End If

        Dim marginx As Single = dpi, marginy As Single = dpi / 2
        Const padx As Single = 24, pady As Single = 4
        Const gap As Single = 10
        Dim ip = New PointF(marginx, marginy)

        Dim tfCaption = New TextFormat() With {
            .Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSans-Regular.ttf")),
            .FontSize = 14
        }
        Dim tfBarcode = New TextFormat() With {
            .Font = tfCaption.Font,
            .FontSize = 6
        }
        Dim barcode As New GcBarcode() With {
            .TextFormat = tfBarcode,
            .ScaleFactor = 2
        }
        barcode.Options.CaptionPosition = BarCodeCaptionPosition.Below
        barcode.Options.SizeOptions.NarrowWideRatio = 0

        Dim drawBarcode As Action(Of CodeType, String, String) =
            Sub(ct_, txt_, txt2_)
                Dim caption = $"{ct_}:{vbCrLf}{txt_}"
                If String.IsNullOrEmpty(txt2_) Then
                    barcode.Options.GS1Composite.Type = GS1CompositeType.None
                Else
                    caption &= $"{vbCrLf}Dependent CCA: {txt2_}"
                    barcode.Options.GS1Composite.Type = GS1CompositeType.CCA
                    barcode.Options.GS1Composite.Value = txt2_
                End If
                barcode.Options.CheckSumEnabled = (ct_ <> CodeType.Code25intlv AndAlso ct_ <> CodeType.Code_2_of_5 AndAlso ct_ <> CodeType.Matrix_2_of_5)
                Dim csize = g.MeasureString(caption, tfCaption)
                barcode.CodeType = ct_
                barcode.Text = txt_
                Dim size = g.MeasureBarcode(barcode)
                size.Height = Math.Max(size.Height, csize.Height)
                Dim border = New RectangleF(ip, New SizeF(pixelSize.Width - marginx * 2, size.Height + pady * 2))
                If ip.Y + border.Height > pixelSize.Height - marginy Then
                    If tw Is Nothing Then Exit Sub
                    tw.AppendFrame(bmp)
                    bmp.Clear(Color.White)
                    ip.Y = marginy
                    ip = New PointF(marginx, marginy)
                    border = New RectangleF(ip, border.Size)
                End If
                g.DrawRectangle(border, Color.Gray)
                g.DrawString(caption, tfCaption, New PointF(border.Left + padx, border.Top + pady))
                g.DrawBarcode(barcode, New RectangleF(border.Right - size.Width - padx, border.Top + pady, size.Width, size.Height))
                ip.Y = border.Bottom + gap
            End Sub

        '' Barcode samples
        drawBarcode(CodeType.Ansi39, "*DSBARCODE*", Nothing)
        drawBarcode(CodeType.Ansi39x, "*DsImaging*", Nothing)
        drawBarcode(CodeType.Codabar, "A12041961D", Nothing)
        drawBarcode(CodeType.Code25intlv, "1234567890", Nothing) ''#2 Interleaved 2 of 5 (ITF)
        drawBarcode(CodeType.Code39, "*DSBARCODE*", Nothing)
        drawBarcode(CodeType.Code39x, "*DsImaging*", Nothing)
        drawBarcode(CodeType.Code49, "DsBarcode+DsImaging", Nothing)
        drawBarcode(CodeType.Code93x, "DsBarcode+DsImaging", Nothing)
        drawBarcode(CodeType.Code_93, "DSBARCODE", Nothing)
        drawBarcode(CodeType.Code_128_A, "DSIMAGING-2017", Nothing)
        drawBarcode(CodeType.Code_128_B, "DSIMAGING-2017", Nothing)
        drawBarcode(CodeType.Code_128_C, "1234567890", Nothing)
        drawBarcode(CodeType.Code_128auto, "DsImaging-2023", Nothing)
        drawBarcode(CodeType.Code_2_of_5, "1234567890", Nothing)
        drawBarcode(CodeType.DataMatrix, "DsBarcode+DsImaging", Nothing)
        drawBarcode(CodeType.QRCode, "DsBarcode+DsImaging", Nothing)
        drawBarcode(CodeType.EAN_8, "1234567", Nothing)
        drawBarcode(CodeType.EAN_13, "469" & "87654" & "3210", Nothing)
        drawBarcode(CodeType.EAN128FNC1, "DsBarcode" & vbLf & "DsImaging", Nothing)
        drawBarcode(CodeType.IntelligentMail, "00300999999000000001", Nothing)
        drawBarcode(CodeType.JapanesePostal, "TOKYO-10CC-09-1978", Nothing)
        drawBarcode(CodeType.PostNet, "152063949", Nothing)
        drawBarcode(CodeType.RM4SCC, "SE17PB9Z", Nothing)
        drawBarcode(CodeType.Matrix_2_of_5, "1234567890", Nothing)
        drawBarcode(CodeType.MSI, "1234567890", Nothing)
        drawBarcode(CodeType.MicroPDF417, "DsImaging", Nothing)
        drawBarcode(CodeType.Pdf417, "DsImaging", Nothing)
        drawBarcode(CodeType.RSS14, "1234567890", Nothing)
        drawBarcode(CodeType.RSS14Stacked, "1234567890", Nothing)
        drawBarcode(CodeType.RSS14Stacked, "1234567890", "12345")
        drawBarcode(CodeType.RSS14StackedOmnidirectional, "1234567890", Nothing)
        drawBarcode(CodeType.RSS14Truncated, "1234567890", Nothing)
        drawBarcode(CodeType.RSSExpanded, "12345678901234", Nothing)
        drawBarcode(CodeType.RSSExpandedStacked, "12345678901234", Nothing)
        drawBarcode(CodeType.RSSLimited, "1234567890", Nothing)
        drawBarcode(CodeType.RSSLimited, "1234567890", "12345")
        drawBarcode(CodeType.UCCEAN128, "DsBarcode+DsImaging", Nothing)
        drawBarcode(CodeType.UPC_A, "123456789012", Nothing)
        drawBarcode(CodeType.UPC_E0, "123456789012", Nothing)
        drawBarcode(CodeType.UPC_E1, "123456789012", Nothing)

        Select Case targetMime
            Case Util.MimeTypes.TIFF
                tw.AppendFrame(bmp)
                tw.Dispose()
            Case Util.MimeTypes.JPEG
                bmp.SaveAsJpeg(ms)
            Case Util.MimeTypes.PNG
                bmp.SaveAsPng(ms)
            Case Util.MimeTypes.BMP
                bmp.SaveAsBmp(ms)
            Case Util.MimeTypes.GIF
                bmp.SaveAsGif(ms)
            Case Util.MimeTypes.WEBP
                bmp.SaveAsWebp(ms)
            Case Util.MimeTypes.ICO
                bmp.SaveAsIco(ms, Nothing, IcoFrameEncoding.Png)
            Case Else
                Throw New Exception($"Encoding {targetMime} is not supported.")
        End Select

        bmp.Dispose()
        g.Dispose()
        Return ms
    End Function
End Class