DrawRotatedText.cs
  1. //
  2. // This code is part of Document Solutions for Imaging demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using GrapeCity.Documents.Drawing;
  11. using GrapeCity.Documents.Text;
  12. using GrapeCity.Documents.Imaging;
  13. using GCTEXT = GrapeCity.Documents.Text;
  14. using GCDRAW = GrapeCity.Documents.Drawing;
  15.  
  16. namespace DsImagingWeb.Demos
  17. {
  18. // This demo illustrates the results of using the GcGraphics.DrawRotatedText() method
  19. // with different combinations of arguments.
  20. // The DrawRotatedText() method draws text rotated within a specified rectangle
  21. // similar to how rotated text is drawn in MS Excel cells without borders.
  22. // See also the DrawSlantedText_0 demo.
  23. public class DrawRotatedText
  24. {
  25. static string[] s_names = new []
  26. {
  27. "Rotate Counter-clockwise",
  28. "Rotate Clockwise",
  29. "Vertical Stacking",
  30. "Varying RotatedTextAlignment",
  31. "Vertical Stacking Align",
  32. "Horizontal Stacking Align",
  33. };
  34.  
  35. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  36. {
  37. sampleParams ??= GetSampleParamsList()[0];
  38.  
  39. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  40. using var g = bmp.CreateGraphics(Color.White);
  41. // Set up some values to manage layout:
  42. var margin = g.Resolution;
  43. var gap = g.Resolution;
  44. var w = pixelSize.Width * 0.3f;
  45. var h = w;
  46.  
  47. if (sampleParams[0] == s_names[0])
  48. {
  49. // Draw text at different negative angles:
  50. Draw(g, q0(), angle: -90, false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
  51. Draw(g, q1(), angle: -60, false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
  52. Draw(g, q2(), angle: -45, false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
  53. Draw(g, q3(), angle: -30, false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
  54. }
  55. else if (sampleParams[0] == s_names[1])
  56. {
  57. // Draw text at different positive angles:
  58. Draw(g, q0(), angle: -90, false, RotatedTextAlignment.TopRight, TextAlignment.Leading);
  59. Draw(g, q1(), angle: -60, false, RotatedTextAlignment.TopRight, TextAlignment.Leading);
  60. Draw(g, q2(), angle: -45, false, RotatedTextAlignment.TopRight, TextAlignment.Leading);
  61. Draw(g, q3(), angle: -30, false, RotatedTextAlignment.TopRight, TextAlignment.Leading);
  62. }
  63. else if (sampleParams[0] == s_names[2])
  64. {
  65. // Draw text using different vertical stacking:
  66. Draw(g, q0(), angle: -30, verticalStacking: true, RotatedTextAlignment.TopRight, TextAlignment.Trailing);
  67. Draw(g, q1(), angle: -30, verticalStacking: false, RotatedTextAlignment.TopRight, TextAlignment.Trailing);
  68. Draw(g, q2(), angle: -30, verticalStacking: false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
  69. Draw(g, q3(), angle: -30, verticalStacking: true, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
  70. }
  71. else if (sampleParams[0] == s_names[3])
  72. {
  73. // RotatedTextAlignment affects the location of text (red) within the target rectangle (green):
  74. Draw(g, q0(), angle: -30, verticalStacking: true, RotatedTextAlignment.TopRight, TextAlignment.Trailing);
  75. Draw(g, q1(), angle: 60, verticalStacking: false, RotatedTextAlignment.BottomRight, TextAlignment.Trailing);
  76. Draw(g, q2(), angle: 30, verticalStacking: true, RotatedTextAlignment.TopLeft, TextAlignment.Leading);
  77. Draw(g, q3(), angle: -60, verticalStacking: false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
  78. }
  79. else if (sampleParams[0] == s_names[4])
  80. {
  81. // Draw vertically stacked text using different TextAlignment values:
  82. Draw(g, q0(), angle: 30, verticalStacking: true, RotatedTextAlignment.TopLeft, TextAlignment.Leading);
  83. Draw(g, q1(), angle: 30, verticalStacking: true, RotatedTextAlignment.TopLeft, TextAlignment.Trailing);
  84. Draw(g, q2(), angle: 30, verticalStacking: true, RotatedTextAlignment.TopLeft, TextAlignment.Center);
  85. Draw(g, q3(), angle: 30, verticalStacking: true, RotatedTextAlignment.TopLeft, TextAlignment.Distributed);
  86. }
  87. else if (sampleParams[0] == s_names[5])
  88. {
  89. // Draw horizontally stacked text using different TextAlignment values:
  90. Draw(g, q0(), angle: -70, verticalStacking: false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
  91. Draw(g, q1(), angle: -70, verticalStacking: false, RotatedTextAlignment.BottomLeft, TextAlignment.Trailing);
  92. Draw(g, q2(), angle: -70, verticalStacking: false, RotatedTextAlignment.BottomLeft, TextAlignment.Center);
  93. Draw(g, q3(), angle: -70, verticalStacking: false, RotatedTextAlignment.BottomLeft, TextAlignment.Distributed);
  94. }
  95. return bmp;
  96.  
  97. RectangleF q0()
  98. {
  99. return new RectangleF(margin, margin, w, h);
  100. }
  101. RectangleF q1()
  102. {
  103. return new RectangleF(pixelSize.Width - margin - w, margin, w, h);
  104. }
  105. RectangleF q2()
  106. {
  107. return new RectangleF(margin, margin + h + gap, w, h);
  108. }
  109. RectangleF q3()
  110. {
  111. return new RectangleF(pixelSize.Width - margin - w, margin + h + gap, w, h);
  112. }
  113. }
  114.  
  115. static void Draw(GcGraphics g, RectangleF rect, int angle, bool verticalStacking,
  116. RotatedTextAlignment rotatedAlign, TextAlignment textAlign)
  117. {
  118. // Draw a legend stating the current DrawRotatedText arguments' values:
  119. var tlLegend = g.CreateTextLayout();
  120. tlLegend.DefaultFormat.FontName = "Calibri";
  121. tlLegend.DefaultFormat.FontSize = 9;
  122. tlLegend.AppendLine($"Rotation angle: {angle}°");
  123. tlLegend.AppendLine($"Text alignment: {textAlign}");
  124. tlLegend.AppendLine($"Rotated text alignment: {rotatedAlign}");
  125. tlLegend.AppendLine($"Is vertical stacking: {verticalStacking}");
  126. g.DrawTextLayout(tlLegend, rect.Location);
  127.  
  128. // The target rectangle for the DrawRotatedText call:
  129. var d = tlLegend.ContentHeight + 12;
  130. rect.Y += d;
  131. rect.Height -= d;
  132.  
  133. var tl = g.CreateTextLayout();
  134. tl.DefaultFormat.FontName = "Calibri";
  135. tl.DefaultFormat.FontSize = 12;
  136. tl.Append("The quick brown fox jumps over the lazy dog. ");
  137. tl.Append("The quick brown fox jumps over the lazy dog.");
  138. tl.TextAlignment = textAlign;
  139. // Draw rotated text:
  140. g.DrawRotatedText(tl, angle, verticalStacking, rect, rotatedAlign);
  141.  
  142. // Outline the target rectangle in green:
  143. g.DrawRectangle(rect, new GCDRAW::Pen(Color.PaleGreen, 3));
  144. // Outline the actual text rectangle in red:
  145. var tlCopy = tl.Clone(true);
  146. var tlRect = g.MeasureRotatedText(tlCopy, angle, verticalStacking, rect, rotatedAlign);
  147. g.DrawRectangle(tlRect, new GCDRAW::Pen(Color.Red, 1));
  148. }
  149.  
  150. public static List<string[]> GetSampleParamsList()
  151. {
  152. return new List<string[]>()
  153. {
  154. // Strings are name, description, info. Rest are arbitrary strings:
  155. new string[] { s_names[0], "Draw rotated text at different negative angles", null},
  156. new string[] { s_names[1], "Draw rotated text at different positive angles", null},
  157. new string[] { s_names[2], "Draw rotated text using different vertical stacking", null},
  158. new string[] { s_names[3], "Draw text using different rotated text alignments", null},
  159. new string[] { s_names[4], "Draw vertically stacked text using different TextAlignment values", null},
  160. new string[] { s_names[5], "Draw horizontally stacked text using different TextAlignment values", null, },
  161. };
  162. }
  163. }
  164. }
  165.