JapaneseText.vb
  1. ''
  2. '' This code is part of Document Solutions for Imaging demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports System.Numerics
  8. Imports GrapeCity.Documents.Drawing
  9. Imports GrapeCity.Documents.Text
  10. Imports GrapeCity.Documents.Imaging
  11. Imports GCTEXT = GrapeCity.Documents.Text
  12. Imports GCDRAW = GrapeCity.Documents.Drawing
  13.  
  14. '' This sample shows how to draw Japanese text on a GcBitmap.
  15. Public Class JapaneseText
  16. Function GenerateImage(
  17. ByVal pixelSize As Size,
  18. ByVal dpi As Single,
  19. ByVal opaque As Boolean,
  20. Optional ByVal sampleParams As String() = Nothing) As GcBitmap
  21.  
  22. Dim Text = "日本語(にほんご、にっぽんご)は、主として、日本列島で使用されてきた言語である。日本手話を母語とする者などを除いて、ほぼ全ての日本在住者は日本語を第一言語とする。日本国は法令上、公用語を明記していないが、事実上の公用語となっており、学校教育の「国語」で教えられる。使用者は、日本国内を主として約\uFF11億\uFF13千万人。日本語の文法体系や音韻体系を反映する手話として日本語対応手話がある。"
  23. Dim ia = New ImageAlign(ImageAlignHorz.Left, ImageAlignVert.Top, True, True, True, False, False)
  24.  
  25. Dim pageWidth = pixelSize.Width
  26. Dim pageHeight = pixelSize.Height
  27.  
  28. Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, True, dpi, dpi)
  29. Using g = bmp.CreateGraphics(Color.White)
  30. g.Renderer.Multithreaded = True
  31.  
  32. '' The TextLayout that will hold And render the text:
  33. Dim tl = g.CreateTextLayout()
  34. tl.FirstLineIndent = 18
  35. tl.ParagraphSpacing = 6
  36. tl.FlowDirection = FlowDirection.VerticalRightToLeft
  37. tl.TextAlignment = TextAlignment.Justified
  38. tl.DefaultFormat.Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf"))
  39. Dim tf = New TextFormat() With
  40. {
  41. .Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "yumin.ttf")),
  42. .FontSize = 14
  43. }
  44. '' Repeat test text to fill the page
  45. For i = 0 To 11
  46. tl.Append(Text, tf)
  47. tl.AppendLine()
  48. Next
  49.  
  50. '' Layout text in 4 horizontal columns
  51. '' (The logic/code in this sample Is identical to ArabicText)
  52. Const NCOLS = 4
  53. Dim margin = 36.0F
  54. Dim gap = 18.0F
  55. Dim colHeight = (pageHeight - margin * 2 - gap * (NCOLS - 1)) / NCOLS
  56. tl.MaxWidth = pageWidth
  57. tl.MaxHeight = pageHeight
  58. tl.MarginLeft = margin
  59. tl.MarginRight = margin
  60. tl.MarginTop = margin
  61. tl.MarginBottom = margin + (colHeight + gap) * (NCOLS - 1)
  62. '' We can specify arbitrary rectangles for the text to flow around.
  63. '' In this case, we add 3 areas to draw some images:
  64. tl.ObjectRects = New List(Of ObjectRect)() From
  65. {
  66. New ObjectRect(pageWidth - margin - 267, margin, 267, 200),
  67. New ObjectRect(margin + 100, margin + 60, 133, 100),
  68. New ObjectRect(margin, pageHeight - margin - 301, 200, 301)
  69. }
  70. Using clouds = Util.ImageFromFile(Path.Combine("Resources", "Images", "clouds.jpg"))
  71. g.DrawImage(clouds, tl.ObjectRects(0).ToRectangleF(), Nothing, ia)
  72. End Using
  73. Using firth = Util.ImageFromFile(Path.Combine("Resources", "Images", "firth.jpg"))
  74. g.DrawImage(firth, tl.ObjectRects(1).ToRectangleF(), Nothing, ia)
  75. End Using
  76. Using lavender = Util.ImageFromFile(Path.Combine("Resources", "Images", "lavender.jpg"))
  77. g.DrawImage(lavender, tl.ObjectRects(2).ToRectangleF(), Nothing, ia)
  78. End Using
  79.  
  80. '' THE call: it calculates the glyphs needed To draw the text, And lays it out:
  81. tl.PerformLayout(True)
  82.  
  83. For col = 0 To NCOLS - 1
  84. Dim nextcol = If(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. Dim tso = New TextSplitOptions(tl) With
  88. {
  89. .RestMarginTop = margin + (colHeight + gap) * nextcol,
  90. .RestMarginBottom = margin + (colHeight + gap) * (NCOLS - 1 - nextcol)
  91. }
  92. Dim rest As TextLayout = Nothing
  93. Dim split = tl.Split(tso, rest)
  94. g.DrawTextLayout(tl, PointF.Empty)
  95. If (split <> SplitResult.Split) Then
  96. Exit For
  97. End If
  98. tl = rest
  99. Next
  100.  
  101. '' Draw border around the whole image
  102. g.DrawRectangle(New RectangleF(0, 0, bmp.Width, bmp.Height), Color.DarkSlateBlue, 4)
  103. End Using
  104. Return bmp
  105. End Function
  106. End Class
  107.