RotatedTableText.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.Numerics;
using GrapeCity.Documents.Drawing;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Imaging;
using GrapeCity.Documents.Layout;
using GCTEXT = GrapeCity.Documents.Text;
using GCDRAW = GrapeCity.Documents.Drawing;

namespace DsImagingWeb.Demos
{
    // This example shows how to draw a table with cells containing rotated texts,
    // using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
    public class RotatedTableText
    {
        public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
        {
            var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
            using var g = bmp.CreateGraphics(Color.White);
            DrawTable(g, pixelSize.Width, pixelSize.Height);
            return bmp;
        }

        static void DrawTable(GcGraphics g, float pageWidth, float pageHeight)
        {
            var host = new LayoutHost();
            var view = host.CreateView(pageWidth, pageHeight);

            var rt = view.CreateRect();
            rt.AnchorTopLeftRight(null, 30, 20, 20);

            var ta = new TableRenderer(g,
                rt, FixedTableSides.TopLeftRight,
                rowCount: 9, columnCount: 7,
                gridLineColor: Color.Transparent,
                gridLineWidth: 1,
                rowMinHeight: 20,
                columnMinWidth: 20);

            // We set a "star" (weighted) width for the seventh column so that the table
            // can expand since both the left and right sides of the table are fixed.
            ta.ColumnRects[6].SetStarWidth(1f);

            ta.RowRects[8].AppendMinHeight(70);

            var fmtNorm = new TextFormat
            {
                Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeui.ttf")),
                FontSize = 16f,
                FontSizeInGraphicUnits = true
            };

            var cs = new CellStyle
            {
                LineWidth = 1,
                LineColor = Color.Coral,
                LinePaddingAll = 1,
                CornerRadius = 5,
                FillColor = Color.Snow,
                PaddingLeftRight = 10,
                PaddingTop = 2,
                PaddingBottom = 5,
                TextAlignment = TextAlignment.Center,
                TextFormat = fmtNorm
            };
            ta.DefaultCellStyle = cs;

            ta.TableFrameStyle = new FrameStyle
            {
                LineWidth = 1,
                LineColor = Color.CornflowerBlue,
                LinePaddingAll = -3,
                CornerRadius = 5,
                FillColor = Color.MistyRose
            };

            // To adjust a column width automatically we need to set
            // the FixedWidth property the CellStyle to false.
            var csFlexW = new CellStyle(cs)
            {
                FixedWidth = false
            };
            var cs270 = new CellStyle(cs)
            {
                RotationAngle = 270
            };
            // A rotated cell also has a fixed width (which is actually a height) by default.
            // If we set FixedWidth to false, the row height will be adjusted automatically.
            var cs270FlexH = new CellStyle(cs)
            {
                RotationAngle = 270,
                ParagraphAlignment = ParagraphAlignment.Center,
                FixedWidth = false,
                MaxWidth = 120
            };

            ta.AddCell(0, 1, 1, 3, "Title 1 with a long text");
            ta.AddCell(new CellStyle(cs) { ParagraphAlignment = ParagraphAlignment.Center },
                0, 4, 1, 3, "Title 2");

            ta.AddCell(cs270, 1, 1, 2, 1, "Vertical Title 1");
            ta.AddCell(cs270, 1, 2, 2, 1, "Vertical Title 2 with additional text");
            ta.AddCell(cs270, 1, 3, 2, 1, "Vertical Title 3");

            ta.AddCell(1, 4, 1, 2, "Subtitle 2.1");

            ta.AddCell(cs270FlexH, 2, 4, "Vertical Subtitle 2.1.1");
            ta.AddCell(cs270FlexH, 2, 5, "Vertical Subtitle 2.1.2 with a long, long, long, and even longer text");

            ta.AddCell(cs270, 3, 0, 3, 1, "Side Title 1");
            ta.AddCell(cs270, 6, 0, 2, 1, "Side Title 2");

            for (int r = 3; r < 8; r++)
                for (int c = 1; c < 4; c++)
                    ta.AddCell(csFlexW, r, c, (r * c).ToString());

            for (int r = 3; r < 8; r++)
                for (int c = 4; c < 6; c++)
                    ta.AddCell(csFlexW, r, c, $"row {r} column {c}");

            ta.AddCell(new CellStyle(cs)
            {
                RotationAngle = 90,
                ParagraphAlignment = ParagraphAlignment.Far,
                TextAlignment = TextAlignment.Leading
            },
                1, 6, 7, 1, "Other Side");
            ta.AddCell(new CellStyle(cs)
            {
                TextAlignment = TextAlignment.Trailing,
                ParagraphAlignment = ParagraphAlignment.Far
            },
                8, 0, 1, 7, "Bottom Side");

            ta.Render();
        }
    }
}