TextTrimming.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 demonstrates how to display ellipsis
  15. '' if a string does Not fit in the allocated space.
  16. Public Class TextTrimming
  17. Function GenerateImage(
  18. ByVal pixelSize As Size,
  19. ByVal dpi As Single,
  20. ByVal opaque As Boolean,
  21. Optional ByVal sampleParams As String() = Nothing) As GcBitmap
  22.  
  23. Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, True, dpi, dpi)
  24. Dim Inch = dpi
  25. Const fontSize = 12
  26. Using g = bmp.CreateGraphics(Color.White)
  27. Dim str = "This is a long line of text which does not fit in the allocated space."
  28. Dim wid = Inch * 4
  29. Dim dy = 0.3F
  30.  
  31. Dim rc = Util.AddNote(
  32. "TextLayout allows displaying ellipsis (or other character) " +
  33. "at the end of a text line that did not fit in the allocated space." + vbCrLf +
  34. "To use trimming, set TrimmingGranularity to Character or Word " +
  35. "(the default is None). Trimming will kick in if WrapMode is NoWrap, " +
  36. "and the text is too long. Wrapped text may also display trimming if " +
  37. "the layout width is too narrow to fit a single word." + vbCrLf +
  38. "Below are examples of text untrimmed, trimmed to character and to word. " +
  39. "The next line demonstrates a different trimming character (tilde in this case). " +
  40. "Finally, the last line shows how to trim text (respecting TrimmingGranularity) " +
  41. "without adding any trimming character.",
  42. g)
  43. Dim top = rc.Bottom + 36
  44.  
  45. Dim ip = New PointF(rc.Left, top)
  46.  
  47. Dim tl = g.CreateTextLayout()
  48. tl.DefaultFormat.Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf"))
  49. tl.DefaultFormat.FontSize = fontSize
  50. tl.MaxWidth = wid
  51. tl.WrapMode = WrapMode.NoWrap
  52.  
  53. '' TrimmingGranularity Is None by default
  54. tl.Append(str)
  55. tl.PerformLayout(True)
  56. g.DrawTextLayout(tl, ip)
  57. ip.Y += tl.ContentHeight + dy
  58.  
  59. '' Character trimming
  60. tl.TrimmingGranularity = TrimmingGranularity.Character
  61. '' NOTE that the recalculateGlyphsBeforeLayout parameter to PerformLayout
  62. '' may be false after the first call, as the text/font has Not changed:
  63. tl.PerformLayout(False)
  64. g.DrawTextLayout(tl, ip)
  65. ip.Y += tl.ContentHeight + dy
  66.  
  67. '' Word trimming
  68. tl.TrimmingGranularity = TrimmingGranularity.Word
  69. tl.PerformLayout(False)
  70. g.DrawTextLayout(tl, ip)
  71. ip.Y += tl.ContentHeight + dy
  72.  
  73. '' tl.EllipsisCharCode Is HorizontalEllipsis (0x2026) by default.
  74. '' Change it to a tilde:
  75. tl.EllipsisCharCode = &H7E
  76. tl.PerformLayout(False)
  77. g.DrawTextLayout(tl, ip)
  78. ip.Y += tl.ContentHeight + dy
  79.  
  80. '' Finally, we may set tl.EllipsisCharCode to 0 to trim text
  81. '' without rendering any trimming character:
  82. tl.EllipsisCharCode = 0
  83. tl.PerformLayout(False)
  84. g.DrawTextLayout(tl, ip)
  85. ip.Y += tl.ContentHeight + dy
  86.  
  87. g.DrawRectangle(New RectangleF(rc.Left, top, wid, ip.Y - top), Color.OrangeRed)
  88.  
  89. '' Draw border around the whole image
  90. g.DrawRectangle(New RectangleF(0, 0, bmp.Width, bmp.Height), Color.DarkSlateBlue, 4)
  91. End Using
  92. '' Done
  93. Return bmp
  94. End Function
  95. End Class
  96.