SupportedBarcodes.cs
//
// This code is part of Document Solutions for Imaging demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using GrapeCity.Documents.Imaging;
using GrapeCity.Documents.Drawing;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Barcode;
using GCTEXT = GrapeCity.Documents.Text;
using GCDRAW = GrapeCity.Documents.Drawing;
using DsImagingWeb.Demos.Common;

namespace DsImagingWeb.Demos
{
    // This example renders samples of all barcode symbologies supported by the Document Solutions Barcode library.
    public class SupportedBarcodes
    {
        public Stream GenerateImageStream(string targetMime, Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
        {
            var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
            var g = bmp.CreateGraphics(Color.White);
            var ms = new MemoryStream();
            GcTiffWriter tw = null;
            if (targetMime == Util.MimeTypes.TIFF)
                tw = new GcTiffWriter(ms);

            float marginx = dpi, marginy = dpi / 2;
            const float padx = 24, pady = 4;
            const float gap = 10;
            var ip = new PointF(marginx, marginy);

            var tfCaption = new TextFormat()
            {
                Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeui.ttf")),
                FontSize = 14,
            };
            var tfBarcode = new TextFormat()
            {
                Font = tfCaption.Font,
                FontSize = 6,
            };
            GcBarcode barcode = new GcBarcode()
            {
                TextFormat = tfBarcode,
                ScaleFactor = 2,
            };
            barcode.Options.CaptionPosition = BarCodeCaptionPosition.Below;
            barcode.Options.SizeOptions.NarrowWideRatio = 0;

            Action<CodeType, string, string> drawBarcode = (ct_, txt_, txt2_) =>
            {
                var caption = $"{ct_}:\r\n{txt_}";
                if (string.IsNullOrEmpty(txt2_))
                    barcode.Options.GS1Composite.Type = GS1CompositeType.None;
                else
                {
                    caption += $"\r\nDependent CCA: {txt2_}";
                    barcode.Options.GS1Composite.Type = GS1CompositeType.CCA;
                    barcode.Options.GS1Composite.Value = txt2_;
                }
                barcode.Options.CheckSumEnabled = ct_ != CodeType.Code25intlv && ct_ != CodeType.Code_2_of_5 && ct_ != CodeType.Matrix_2_of_5;
                var csize = g.MeasureString(caption, tfCaption);
                barcode.CodeType = ct_;
                barcode.Text = txt_;
                var size = g.MeasureBarcode(barcode);
                size.Height = Math.Max(size.Height, csize.Height);
                var border = new RectangleF(ip, new SizeF(pixelSize.Width - marginx * 2, size.Height + pady * 2));
                if (ip.Y + border.Height > pixelSize.Height - marginy)
                {
                    if (tw == null)
                        return;
                    tw.AppendFrame(bmp);
                    bmp.Clear(Color.White);
                    ip.Y = marginy;
                    ip = new PointF(marginx, marginy);
                    border = new RectangleF(ip, border.Size);
                }
                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;
            };
            //
            drawBarcode(CodeType.Ansi39, "*DSBARCODE*", null);
            drawBarcode(CodeType.Ansi39x, "*DsImaging*", null);
            drawBarcode(CodeType.Codabar, "A12041961D", null);
            drawBarcode(CodeType.Code25intlv, "1234567890", null); //#2 Interleaved 2 of 5 (ITF)
            drawBarcode(CodeType.Code39, "*DSBARCODE*", null);
            drawBarcode(CodeType.Code39x, "*DsImaging*", null);
            drawBarcode(CodeType.Code49, "DsBarcode+DsImaging", null);
            drawBarcode(CodeType.Code93x, "DsBarcode+DsImaging", null);
            drawBarcode(CodeType.Code_93, "DSBARCODE", null);
            drawBarcode(CodeType.Code_128_A, "DSIMAGING-2017", null);
            drawBarcode(CodeType.Code_128_B, "DSIMAGING-2017", null);
            drawBarcode(CodeType.Code_128_C, "1234567890", null);
            drawBarcode(CodeType.Code_128auto, "DsImaging-2023", null);
            drawBarcode(CodeType.Code_2_of_5, "1234567890", null);
            drawBarcode(CodeType.DataMatrix, "DsBarcode+DsImaging", null);
            drawBarcode(CodeType.QRCode, "DsBarcode+DsImaging", null);
            drawBarcode(CodeType.EAN_8, "1234567", null);
            drawBarcode(CodeType.EAN_13, "469" + "87654" + "3210", null);
            drawBarcode(CodeType.EAN128FNC1, "DsBarcode\nDsImaging", null);
            drawBarcode(CodeType.IntelligentMail, "00300999999000000001", null);
            drawBarcode(CodeType.JapanesePostal, "TOKYO-10CC-09-1978", null);
            drawBarcode(CodeType.PostNet, "152063949", null);
            drawBarcode(CodeType.RM4SCC, "SE17PB9Z", null);
            drawBarcode(CodeType.Matrix_2_of_5, "1234567890", null);
            drawBarcode(CodeType.MSI, "1234567890", null);
            drawBarcode(CodeType.MicroPDF417, "DsImaging", null);
            drawBarcode(CodeType.Pdf417, "DsImaging", null);
            drawBarcode(CodeType.RSS14, "1234567890", null);
            drawBarcode(CodeType.RSS14Stacked, "1234567890", null);
            drawBarcode(CodeType.RSS14Stacked, "1234567890", "12345");
            drawBarcode(CodeType.RSS14StackedOmnidirectional, "1234567890", null);
            drawBarcode(CodeType.RSS14Truncated, "1234567890", null);
            drawBarcode(CodeType.RSSExpanded, "12345678901234", null);
            drawBarcode(CodeType.RSSExpandedStacked, "12345678901234", null);
            drawBarcode(CodeType.RSSLimited, "1234567890", null);
            drawBarcode(CodeType.RSSLimited, "1234567890", "12345");
            drawBarcode(CodeType.UCCEAN128, "DsBarcode+DsImaging", null);
            drawBarcode(CodeType.UPC_A, "123456789012", null);
            drawBarcode(CodeType.UPC_E0, "123456789012", null);
            drawBarcode(CodeType.UPC_E1, "123456789012", null);

            switch (targetMime)
            {
                case Common.Util.MimeTypes.TIFF:
                    tw.AppendFrame(bmp);
                    tw.Dispose();
                    break;
                case Common.Util.MimeTypes.JPEG:
                    bmp.SaveAsJpeg(ms);
                    break;
                case Common.Util.MimeTypes.PNG:
                    bmp.SaveAsPng(ms);
                    break;
                case Common.Util.MimeTypes.BMP:
                    bmp.SaveAsBmp(ms);
                    break;
                case Common.Util.MimeTypes.GIF:
                    bmp.SaveAsGif(ms);
                    break;
                case Common.Util.MimeTypes.WEBP:
                    bmp.SaveAsWebp(ms);
                    break;
                case Common.Util.MimeTypes.ICO:
                    bmp.SaveAsIco(ms, null, IcoFrameEncoding.Png);
                    break;
                default:
                    throw new Exception($"Encoding {targetMime} is not supported.");
            }
            bmp.Dispose();
            g.Dispose();
            return ms;
        }
    }
}