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. tl.DefaultFormat.Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf"));
  41. var tf = new TextFormat()
  42. {
  43. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "yumin.ttf")),
  44. FontSize = 14,
  45. };
  46. // Repeat test text to fill the page:
  47. for (int i = 0; i < 12; ++i)
  48. {
  49. tl.Append(text, tf);
  50. tl.AppendLine();
  51. }
  52.  
  53. // Layout text in 4 horizontal columns:
  54. // (The logic/code in this sample is identical to ArabicText):
  55. const int NCOLS = 4;
  56. var margin = 36f;
  57. var gap = 18f;
  58. var colHeight = (pageHeight - margin * 2 - gap * (NCOLS - 1)) / NCOLS;
  59. tl.MaxWidth = pageWidth;
  60. tl.MaxHeight = pageHeight;
  61. tl.MarginLeft = tl.MarginRight = margin;
  62. tl.MarginTop = margin;
  63. tl.MarginBottom = margin + (colHeight + gap) * (NCOLS - 1);
  64. // We can specify arbitrary rectangles for the text to flow around.
  65. // In this case, we add 3 areas to draw some images:
  66. tl.ObjectRects = new List<ObjectRect>()
  67. {
  68. new ObjectRect(pageWidth - margin - 267, margin, 267, 200),
  69. new ObjectRect(margin + 100, margin + 60, 133, 100),
  70. new ObjectRect(margin, pageHeight - margin - 301, 200, 301),
  71. };
  72. using (var clouds = Common.Util.ImageFromFile(Path.Combine("Resources", "Images", "clouds.jpg")))
  73. g.DrawImage(clouds, tl.ObjectRects[0].ToRectangleF(), null, ia);
  74. using (var firth = Common.Util.ImageFromFile(Path.Combine("Resources", "Images", "firth.jpg")))
  75. g.DrawImage(firth, tl.ObjectRects[1].ToRectangleF(), null, ia);
  76. using (var lavender = Common.Util.ImageFromFile(Path.Combine("Resources", "Images", "lavender.jpg")))
  77. g.DrawImage(lavender, tl.ObjectRects[2].ToRectangleF(), null, ia);
  78.  
  79. // THE call: it calculates the glyphs needed to draw the text, and lays it out:
  80. tl.PerformLayout(true);
  81.  
  82. for (int col = 0; col < NCOLS; ++col)
  83. {
  84. int nextcol = (col < NCOLS - 1) ? col + 1 : 0;
  85. // TextSplitOptions tell TextLayout.Split() how to layout the remaining text.
  86. // In this case we advance from column to column by updating top and bottom margins:
  87. var tso = new TextSplitOptions(tl)
  88. {
  89. RestMarginTop = margin + (colHeight + gap) * nextcol,
  90. RestMarginBottom = margin + (colHeight + gap) * (NCOLS - 1 - nextcol)
  91. };
  92. var split = tl.Split(tso, out TextLayout rest);
  93. g.DrawTextLayout(tl, PointF.Empty);
  94. if (split != SplitResult.Split)
  95. break;
  96. tl = rest;
  97. }
  98.  
  99. // Draw border around the whole image:
  100. g.DrawRectangle(new RectangleF(0, 0, bmp.Width, bmp.Height), Color.DarkSlateBlue, 4);
  101. }
  102. return bmp;
  103. }
  104. }
  105. }
  106.