//
// 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 System.Linq;
using GrapeCity.Documents.Drawing;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Imaging;
using GCTEXT = GrapeCity.Documents.Text;
using GCDRAW = GrapeCity.Documents.Drawing;
namespace DsImagingWeb.Demos
{
// This demo illustrates the results of using the GcGraphics.DrawRotatedText() method
// with different combinations of arguments.
// The DrawRotatedText() method draws text rotated within a specified rectangle
// similar to how rotated text is drawn in MS Excel cells without borders.
// See also the DrawSlantedText_0 demo.
public class DrawRotatedText
{
static string[] s_names = new []
{
"Rotate Counter-clockwise",
"Rotate Clockwise",
"Vertical Stacking",
"Varying RotatedTextAlignment",
"Vertical Stacking Align",
"Horizontal Stacking Align",
};
public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
{
sampleParams ??= GetSampleParamsList()[0];
var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
using var g = bmp.CreateGraphics(Color.White);
// Set up some values to manage layout:
var margin = g.Resolution;
var gap = g.Resolution;
var w = pixelSize.Width * 0.3f;
var h = w;
if (sampleParams[0] == s_names[0])
{
// Draw text at different negative angles:
Draw(g, q0(), angle: -90, false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
Draw(g, q1(), angle: -60, false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
Draw(g, q2(), angle: -45, false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
Draw(g, q3(), angle: -30, false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
}
else if (sampleParams[0] == s_names[1])
{
// Draw text at different positive angles:
Draw(g, q0(), angle: -90, false, RotatedTextAlignment.TopRight, TextAlignment.Leading);
Draw(g, q1(), angle: -60, false, RotatedTextAlignment.TopRight, TextAlignment.Leading);
Draw(g, q2(), angle: -45, false, RotatedTextAlignment.TopRight, TextAlignment.Leading);
Draw(g, q3(), angle: -30, false, RotatedTextAlignment.TopRight, TextAlignment.Leading);
}
else if (sampleParams[0] == s_names[2])
{
// Draw text using different vertical stacking:
Draw(g, q0(), angle: -30, verticalStacking: true, RotatedTextAlignment.TopRight, TextAlignment.Trailing);
Draw(g, q1(), angle: -30, verticalStacking: false, RotatedTextAlignment.TopRight, TextAlignment.Trailing);
Draw(g, q2(), angle: -30, verticalStacking: false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
Draw(g, q3(), angle: -30, verticalStacking: true, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
}
else if (sampleParams[0] == s_names[3])
{
// RotatedTextAlignment affects the location of text (red) within the target rectangle (green):
Draw(g, q0(), angle: -30, verticalStacking: true, RotatedTextAlignment.TopRight, TextAlignment.Trailing);
Draw(g, q1(), angle: 60, verticalStacking: false, RotatedTextAlignment.BottomRight, TextAlignment.Trailing);
Draw(g, q2(), angle: 30, verticalStacking: true, RotatedTextAlignment.TopLeft, TextAlignment.Leading);
Draw(g, q3(), angle: -60, verticalStacking: false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
}
else if (sampleParams[0] == s_names[4])
{
// Draw vertically stacked text using different TextAlignment values:
Draw(g, q0(), angle: 30, verticalStacking: true, RotatedTextAlignment.TopLeft, TextAlignment.Leading);
Draw(g, q1(), angle: 30, verticalStacking: true, RotatedTextAlignment.TopLeft, TextAlignment.Trailing);
Draw(g, q2(), angle: 30, verticalStacking: true, RotatedTextAlignment.TopLeft, TextAlignment.Center);
Draw(g, q3(), angle: 30, verticalStacking: true, RotatedTextAlignment.TopLeft, TextAlignment.Distributed);
}
else if (sampleParams[0] == s_names[5])
{
// Draw horizontally stacked text using different TextAlignment values:
Draw(g, q0(), angle: -70, verticalStacking: false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
Draw(g, q1(), angle: -70, verticalStacking: false, RotatedTextAlignment.BottomLeft, TextAlignment.Trailing);
Draw(g, q2(), angle: -70, verticalStacking: false, RotatedTextAlignment.BottomLeft, TextAlignment.Center);
Draw(g, q3(), angle: -70, verticalStacking: false, RotatedTextAlignment.BottomLeft, TextAlignment.Distributed);
}
return bmp;
RectangleF q0()
{
return new RectangleF(margin, margin, w, h);
}
RectangleF q1()
{
return new RectangleF(pixelSize.Width - margin - w, margin, w, h);
}
RectangleF q2()
{
return new RectangleF(margin, margin + h + gap, w, h);
}
RectangleF q3()
{
return new RectangleF(pixelSize.Width - margin - w, margin + h + gap, w, h);
}
}
static void Draw(GcGraphics g, RectangleF rect, int angle, bool verticalStacking,
RotatedTextAlignment rotatedAlign, TextAlignment textAlign)
{
// Draw a legend stating the current DrawRotatedText arguments' values:
var tlLegend = g.CreateTextLayout();
tlLegend.DefaultFormat.FontName = "Calibri";
tlLegend.DefaultFormat.FontSize = 9;
tlLegend.AppendLine($"Rotation angle: {angle}°");
tlLegend.AppendLine($"Text alignment: {textAlign}");
tlLegend.AppendLine($"Rotated text alignment: {rotatedAlign}");
tlLegend.AppendLine($"Is vertical stacking: {verticalStacking}");
g.DrawTextLayout(tlLegend, rect.Location);
// The target rectangle for the DrawRotatedText call:
var d = tlLegend.ContentHeight + 12;
rect.Y += d;
rect.Height -= d;
var tl = g.CreateTextLayout();
tl.DefaultFormat.FontName = "Calibri";
tl.DefaultFormat.FontSize = 12;
tl.Append("The quick brown fox jumps over the lazy dog. ");
tl.Append("The quick brown fox jumps over the lazy dog.");
tl.TextAlignment = textAlign;
// Draw rotated text:
g.DrawRotatedText(tl, angle, verticalStacking, rect, rotatedAlign);
// Outline the target rectangle in green:
g.DrawRectangle(rect, new GCDRAW::Pen(Color.PaleGreen, 3));
// Outline the actual text rectangle in red:
var tlCopy = tl.Clone(true);
var tlRect = g.MeasureRotatedText(tlCopy, angle, verticalStacking, rect, rotatedAlign);
g.DrawRectangle(tlRect, new GCDRAW::Pen(Color.Red, 1));
}
public static List<string[]> GetSampleParamsList()
{
return new List<string[]>()
{
// Strings are name, description, info. Rest are arbitrary strings:
new string[] { s_names[0], "Draw rotated text at different negative angles", null},
new string[] { s_names[1], "Draw rotated text at different positive angles", null},
new string[] { s_names[2], "Draw rotated text using different vertical stacking", null},
new string[] { s_names[3], "Draw text using different rotated text alignments", null},
new string[] { s_names[4], "Draw vertically stacked text using different TextAlignment values", null},
new string[] { s_names[5], "Draw horizontally stacked text using different TextAlignment values", null, },
};
}
}
}