EmptyTable.cs
// 
// このコードは、DioDocs for Imaging のサンプルの一部として提供されています。
// © 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 GrapeCity.Documents.Layout.Composition;
using GCTEXT = GrapeCity.Documents.Text;
using GCDRAW = GrapeCity.Documents.Drawing;

namespace DsImagingWeb.Demos
{
    // このサンプルでは、GrapeCity.Documents.Layout.Composition.Surface および
    // GrapeCity.Documents.Drawing.TableRenderer クラスを使用して、
    // シンプルな空のテーブルを描画する方法を示します。
    // また、TableRenderer クラスを使用して作成できる
    // テーブルレイアウトの主な要素を紹介します。
    public class EmptyTable
    {
        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 readonly TextFormat FormatDesc = new TextFormat()
        {
            Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeSans.ttf")),
            FontSize = 24,
            FontSizeInGraphicUnits = true,
            ForeColor = Color.White
        };

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

            // ページをグレーで塗りつぶします。
            view.CreateVisual((g, v) => {
                g.FillRectangle(v.AsRectF(), Color.Gray);
            }).LayoutRect.AnchorExact(null);

            var vTable = view.CreateVisual();
            var tableRect = vTable.LayoutRect;
            tableRect.AnchorTopLeftRight(null, 36, 36, 36);

            // paddingAll パラメータは、テーブルの矩形に対するグリッドの余白を追加します。
            var ta = new TableRenderer(g,
                tableRect, FixedTableSides.TopLeftRight,
                rowCount: 3,
                columnCount: 3,
                gridLineColor: Color.Yellow,
                gridLineWidth: 5,
                rowMinHeight: 100,
                paddingAll: 40);

            var columns = ta.ColumnRects;
            columns[0].SetStarWidth(1);
            columns[1].SetWidth(100);
            columns[2].SetStarWidth(3);

            ta.TableFrameStyle = new FrameStyle
            {
                LinePaddingAll = 20,
                LineColor = Color.SlateBlue,
                FillColor = Color.LightGreen,
                LineWidth = 10f
            };

            ta.ApplyCellConstraints();

            vTable.Draw = (g, v) =>
            {
                var rc = v.AsRectF();
                g.FillRectangle(rc, Color.White);

                var tl = g.CreateTextLayout();
                tl.AppendLine("Table rectangle (tableRect) is White,", FormatDesc);
                tl.AppendLine("Padding of the table frame is 20 (TableFrameStyle.LinePaddingAll),", FormatDesc);
                tl.AppendLine("Frame line width is 10 (TableFrameStyle.LineWidth),", FormatDesc);
                tl.AppendLine("Frame line color is SlateBlue (TableFrameStyle.LineColor),", FormatDesc);
                tl.AppendLine("Frame area color is LightGreen (TableFrameStyle.FillColor),", FormatDesc);
                tl.AppendLine("Padding of the table grid is 40 (the paddingAll parameter),", FormatDesc);
                tl.AppendLine("Table grid line width is 5 (the gridLineWidth parameter),", FormatDesc);
                tl.AppendLine("Grid line color is Yellow (the gridLineColor parameter).", FormatDesc);
                g.DrawTextLayout(tl, new PointF(0, rc.Bottom + 10));

                g.Transform = v.Layer.Surface.BaseTransform;
                ta.Render();
            };

            surf.Render(g);
        }
    }
}