JapaneseText.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 sample shows how to draw Japanese text on a GcBitmap.
  19. public class JapaneseText
  20. {
  21. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  22. {
  23. string text = "日本語(にほんご、にっぽんご)は、主として、日本列島で使用されてきた言語である。日本手話を母語とする者などを除いて、ほぼ全ての日本在住者は日本語を第一言語とする。日本国は法令上、公用語を明記していないが、事実上の公用語となっており、学校教育の「国語」で教えられる。使用者は、日本国内を主として約\uFF11億\uFF13千万人。日本語の文法体系や音韻体系を反映する手話として日本語対応手話がある。";
  24. var ia = new ImageAlign(ImageAlignHorz.Left, ImageAlignVert.Top, true, true, true, false, false);
  25.  
  26. int pageWidth = pixelSize.Width;
  27. int pageHeight = pixelSize.Height;
  28.  
  29. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, true, dpi, dpi);
  30. using (var g = bmp.CreateGraphics(Color.White))
  31. {
  32. g.Renderer.Multithreaded = true;
  33.  
  34. // The TextLayout that will hold and render the text:
  35. var tl = g.CreateTextLayout();
  36. tl.FirstLineIndent = 18;
  37. tl.ParagraphSpacing = 6;
  38. tl.FlowDirection = FlowDirection.VerticalRightToLeft;
  39. tl.TextAlignment = TextAlignment.Justified;
  40. var tf = new TextFormat()
  41. {
  42. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSansJP-Regular.ttf")),
  43. FontSize = 14,
  44. };
  45. // Repeat sample text to fill the page:
  46. for (int i = 0; i < 12; ++i)
  47. {
  48. tl.Append(text, tf);
  49. tl.AppendLine();
  50. }
  51.  
  52. // Layout text in 4 horizontal columns:
  53. // (The logic/code in this sample is identical to ArabicText):
  54. const int NCOLS = 4;
  55. var margin = 36f;
  56. var gap = 18f;
  57. var colHeight = (pageHeight - margin * 2 - gap * (NCOLS - 1)) / NCOLS;
  58. tl.MaxWidth = pageWidth;
  59. tl.MaxHeight = pageHeight;
  60. tl.MarginLeft = tl.MarginRight = margin;
  61. tl.MarginTop = margin;
  62. tl.MarginBottom = margin + (colHeight + gap) * (NCOLS - 1);
  63. // We can specify arbitrary rectangles for the text to flow around.
  64. // In this case, we add 3 areas to draw some images:
  65. tl.ObjectRects = new List<ObjectRect>()
  66. {
  67. new ObjectRect(pageWidth - margin - 267, margin, 267, 200),
  68. new ObjectRect(margin + 100, margin + 60, 133, 100),
  69. new ObjectRect(margin, pageHeight - margin - 301, 200, 301),
  70. };
  71. using (var clouds = Common.Util.ImageFromFile(Path.Combine("Resources", "Images", "clouds.jpg")))
  72. g.DrawImage(clouds, tl.ObjectRects[0].ToRectangleF(), null, ia);
  73. using (var firth = Common.Util.ImageFromFile(Path.Combine("Resources", "Images", "firth.jpg")))
  74. g.DrawImage(firth, tl.ObjectRects[1].ToRectangleF(), null, ia);
  75. using (var lavender = Common.Util.ImageFromFile(Path.Combine("Resources", "Images", "lavender.jpg")))
  76. g.DrawImage(lavender, tl.ObjectRects[2].ToRectangleF(), null, ia);
  77.  
  78. // THE call: it calculates the glyphs needed to draw the text, and lays it out:
  79. tl.PerformLayout(true);
  80.  
  81. for (int col = 0; col < NCOLS; ++col)
  82. {
  83. int nextcol = (col < NCOLS - 1) ? col + 1 : 0;
  84. // TextSplitOptions tell TextLayout.Split() how to layout the remaining text.
  85. // In this case we advance from column to column by updating top and bottom margins:
  86. var tso = new TextSplitOptions(tl)
  87. {
  88. RestMarginTop = margin + (colHeight + gap) * nextcol,
  89. RestMarginBottom = margin + (colHeight + gap) * (NCOLS - 1 - nextcol)
  90. };
  91. var split = tl.Split(tso, out TextLayout rest);
  92. g.DrawTextLayout(tl, PointF.Empty);
  93. if (split != SplitResult.Split)
  94. break;
  95. tl = rest;
  96. }
  97.  
  98. // Draw border around the whole image:
  99. g.DrawRectangle(new RectangleF(0, 0, bmp.Width, bmp.Height), Color.DarkSlateBlue, 4);
  100. }
  101. return bmp;
  102. }
  103. }
  104. }
  105.