TateChuYoko.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Text.RegularExpressions;
  9. using GrapeCity.Documents.Drawing;
  10. using GrapeCity.Documents.Pdf;
  11. using GrapeCity.Documents.Text;
  12. using GCTEXT = GrapeCity.Documents.Text;
  13. using GCDRAW = GrapeCity.Documents.Drawing;
  14.  
  15. namespace DsPdfWeb.Demos.Basics
  16. {
  17. // This sample demonstrates how to render short upright Latin text or numbers
  18. // in a block of vertical text. This is used in Chinese, Japanese and
  19. // Korean vertical text. In CSS this is referred to using the Japanese
  20. // name 縦中横 (tate chu yoko). To achieve this in GcGraphics,
  21. // the TextFormat.UprightInVerticalText property should be set.
  22. // A number of other properties on TextLayout and TextFormat
  23. // allow you to fine-tune the behavior, as shown in this sample.
  24. public class TateChuYoko
  25. {
  26. public int CreatePDF(Stream stream)
  27. {
  28. var doc = new GcPdfDocument();
  29. var page = doc.NewPage();
  30. var g = page.Graphics;
  31.  
  32. var rc = Common.Util.AddNote(
  33. "Vertical text often includes short runs of horizontal numbers or Latin text. " +
  34. "In CSS this is referred to using the Japanese name 縦中横 (tate chu yoko). " +
  35. "It occurs in Chinese, Japanese and Korean vertical text. " +
  36. "We support this by providing TextFormat.UprightInVerticalText and a few " +
  37. "related properties on TextLayout and TextFormat.",
  38. page);
  39.  
  40. var fntJp = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "YuGothM.ttc"));
  41. var fntLat = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "MyriadPro-Cond.otf"));
  42. var hiliteFore = Color.DarkSlateBlue;
  43. var hiliteBack = Color.FromArgb(unchecked((int)0xffffff99));
  44.  
  45. var tl = g.CreateTextLayout();
  46.  
  47. // Set text flow and other layout properties:
  48. tl.FlowDirection = FlowDirection.VerticalRightToLeft;
  49. tl.MaxWidth = page.Size.Width;
  50. tl.MaxHeight = page.Size.Height;
  51. tl.MarginAll = 72;
  52. tl.MarginTop = rc.Bottom + 36;
  53. tl.ParagraphSpacing = 12;
  54. tl.LineSpacingScaleFactor = 1.4f;
  55.  
  56. // g.FillRectangle(
  57. // new RectangleF(tl.MarginLeft, tl.MarginTop, tl.MaxWidth.Value - tl.MarginLeft - tl.MarginRight, tl.MaxHeight.Value - tl.MarginTop - tl.MarginBottom),
  58. // Color.AliceBlue);
  59.  
  60. // Text format for upright text (short Latin words or numbers)
  61. // (GlyphWidths turns on corresponding font features, but makes a difference
  62. // only of those features are present in the font):
  63. var fUpright = new TextFormat()
  64. {
  65. Font = fntLat,
  66. FontSize = 14,
  67. UprightInVerticalText = true,
  68. GlyphWidths = GlyphWidths.QuarterWidths,
  69. TextRunAsCluster = true,
  70. };
  71. // Text format for vertical Japanese and sideways Latin text:
  72. var fVertical = new TextFormat(fUpright)
  73. {
  74. Font = fntJp,
  75. UprightInVerticalText = false,
  76. GlyphWidths = GlyphWidths.Default,
  77. TextRunAsCluster = false,
  78. };
  79.  
  80. // Make sure runs of sideways text do not affect vertical spacing:
  81. fVertical.UseVerticalLineGapForSideways = true;
  82. // This commented fragment effectively would do the same as the
  83. // UseVerticalLineGapForSideways property setting above:
  84. /*
  85. var scale = fVertical.FontSize * tl.FontScaleFactor / fntJp.UnitsPerEm;
  86. if (!fVertical.FontSizeInGraphicUnits)
  87. scale *= tl.Resolution / 72f;
  88. fVertical.LineGap = fntJp.VerticalLineGap * scale;
  89. */
  90.  
  91. // Two additional text formants for highlighted headers:
  92. var fUpHdr = new TextFormat(fUpright)
  93. {
  94. ForeColor = hiliteFore,
  95. BackColor = hiliteBack
  96. };
  97. var fVertHdr = new TextFormat(fVertical)
  98. {
  99. ForeColor = hiliteFore,
  100. BackColor = hiliteBack
  101. };
  102.  
  103. tl.Append("PDF", fUpright);
  104. tl.Append("ファイルをコードから", fVertical);
  105. tl.Append("API", fUpright);
  106. tl.Append("を利用することで操作できます。クロスプラットフォーム環境で動作するアプリケーションの開発を支援する", fVertical);
  107. tl.Append("API", fUpright);
  108. tl.Append("ライブラリです。", fVertical);
  109.  
  110. // Smaller font size for the rest of the text:
  111. fUpright.FontSize -= 2;
  112. fVertical.FontSize -= 2;
  113.  
  114. // 1:
  115. tl.AppendParagraphBreak();
  116. tl.Append("PDF", fUpHdr);
  117. tl.Append("用の包括的な", fVertHdr);
  118. tl.Append("API", fUpHdr);
  119.  
  120. tl.AppendSoftBreak();
  121. tl.Append("PDF", fUpright);
  122. tl.Append("バージョン「", fVertical);
  123. tl.Append("1.7", fUpright);
  124. tl.Append("」に準拠した", fVertical);
  125. tl.Append("API", fUpright);
  126. tl.Append("を提供し、レイアウトや機能を損なうことなく、豊富な機能を備えた", fVertical);
  127. tl.Append("PDF", fUpright);
  128. tl.Append("文書を生成、編集、保存できます。", fVertical);
  129.  
  130. // 2:
  131. tl.AppendParagraphBreak();
  132. tl.Append("完全なテキスト描画", fVertHdr);
  133.  
  134. tl.AppendSoftBreak();
  135. tl.Append("PDF", fUpright);
  136. tl.Append("文書にテキストの描画情報が保持されます。テキストと段落の書式、特殊文字、複数の言語、縦書き、テキスト角度などが保持さるので、完全な形でテキスト描画を再現できます。", fVertical);
  137.  
  138. // 3:
  139. tl.AppendParagraphBreak();
  140. tl.Append(".NET Standard 2.0 準拠", fVertHdr);
  141.  
  142. tl.AppendSoftBreak();
  143. tl.Append(".NET Core、.NET Framework、Xamarinで動作するアプリケーションを開発できます。Windows、macOS、Linuxなどクロスプラットフォーム環境で動作可能です。", fVertical);
  144.  
  145. // 4:
  146. tl.AppendParagraphBreak();
  147. tl.Append("100", fUpHdr);
  148. tl.Append("を超える", fVertHdr);
  149. tl.Append("PDF", fUpHdr);
  150. tl.Append("操作機能", fVertHdr);
  151.  
  152. tl.AppendSoftBreak();
  153. tl.Append("ページの追加や削除、ページサイズ、向きの変更だけでなく、ファイルの圧縮、", fVertical);
  154. tl.Append("Web", fUpright);
  155. tl.Append("に最適化した", fVertical);
  156. tl.Append("PDF", fUpright);
  157. tl.Append("の生成など高度な機能も", fVertical);
  158. tl.Append("API", fUpright);
  159. tl.Append("操作で実現します。また、署名からセキュリティ機能まで様々な機能を含んだ", fVertical);
  160. tl.Append("PDF", fUpright);
  161. tl.Append("フォームを生成可能です。", fVertical);
  162.  
  163. // 5:
  164. tl.AppendParagraphBreak();
  165. tl.Append("高速、軽量アーキテクチャ", fVertHdr);
  166.  
  167. tl.AppendSoftBreak();
  168. tl.Append("軽量", fVertical);
  169. tl.Append("API", fUpright);
  170. tl.Append("アーキテクチャでメモリと時間を節約できます。", fVertical);
  171. tl.AppendSoftBreak();
  172. tl.Append("また、他の生成用ツールに依存せずドキュメントを生成可能です。", fVertical);
  173.  
  174. // 6:
  175. tl.AppendParagraphBreak();
  176. tl.Append("クラウドアプリケーション展開", fVertHdr);
  177. tl.Append("", fUpHdr);
  178.  
  179. tl.AppendSoftBreak();
  180. tl.Append("Azure、AWSなどのサービスに配置するクラウドアプリケーションの開発で利用可能です。仮想マシン、コンテナ、サーバーレスなどの方法で配置できます。", fVertical);
  181.  
  182. tl.PerformLayout(true);
  183. g.DrawTextLayout(tl, PointF.Empty);
  184.  
  185. // Done:
  186. doc.Save(stream);
  187. return doc.Pages.Count;
  188. }
  189. }
  190. }
  191.