CharsAndFonts.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 text using different fonts (.ttf, .otf, .woff)
  19. // and characters with codes greater than 0xFFFF.
  20. public class CharsAndFonts
  21. {
  22. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  23. {
  24. var garlicFont = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "Woff", "Garlicembrace.woff"));
  25. var foglihtenFont = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FoglihtenNo07.otf"));
  26. var pericFont = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "peric.ttf"));
  27. var emojiFont = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "seguiemj.ttf"));
  28. var timesFont = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf"));
  29. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, true, dpi, dpi);
  30. using (var g = bmp.CreateGraphics(Color.White))
  31. {
  32. TextFormat tf = new TextFormat
  33. {
  34. Font = garlicFont,
  35. FontSize = 40
  36. };
  37. g.DrawString("Garlicembrace.woff", tf, new RectangleF(4, 4, 500, 50));
  38.  
  39. tf = new TextFormat
  40. {
  41. Font = timesFont,
  42. FontSize = 40
  43. };
  44. g.DrawString("Times New Roman", tf, new RectangleF(4, 64, 580, 50));
  45.  
  46. var tl = g.CreateTextLayout();
  47. tf = new TextFormat
  48. {
  49. Font = foglihtenFont,
  50. FontSize = 40
  51. };
  52. tl.Append("FoglihtenNo07.otf", tf);
  53. g.DrawTextLayout(tl, new PointF(4, 139));
  54.  
  55. tf = new TextFormat
  56. {
  57. Font = pericFont,
  58. FontSize = 40
  59. };
  60. g.DrawString("peric.ttf", tf, new RectangleF(4, 190, 500, 50));
  61.  
  62. var pals = emojiFont.CreateFontTables(TableTag.CpalDraw).GetPalettes();
  63. tf = new TextFormat
  64. {
  65. Font = emojiFont,
  66. FontSize = 40,
  67. Palette = pals[0]
  68. };
  69. g.DrawString("seguiemj.ttf \U0001F433\U0001F349\U0001F367", tf, new RectangleF(4, 240, 550, 50));
  70. }
  71. return bmp;
  72. }
  73. }
  74. }
  75.