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 fWoff = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeMonoBoldOblique.woff"));
  25. var fSerif = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeSerif.ttf"));
  26. var fFoglihten = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FoglihtenNo07.otf"));
  27. var fSega = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "SEGA.ttf"));
  28. var fEmoji = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FirefoxEmoji.ttf"));
  29. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, true, dpi, dpi);
  30. using var g = bmp.CreateGraphics(Color.White);
  31. TextFormat tf = new TextFormat
  32. {
  33. Font = fWoff,
  34. FontSize = 40
  35. };
  36. g.DrawString("FreeMonoBoldOblique.woff", tf, new RectangleF(4, 4, 800, 50));
  37.  
  38. tf = new TextFormat
  39. {
  40. Font = fSerif,
  41. FontSize = 40
  42. };
  43. g.DrawString("FreeSerif.ttf", tf, new RectangleF(4, 64, 800, 50));
  44.  
  45. tf = new TextFormat
  46. {
  47. Font = fFoglihten,
  48. FontSize = 40
  49. };
  50. g.DrawString("FoglihtenNo07.otf", tf, new RectangleF(4, 139, 800, 50));
  51.  
  52. tf = new TextFormat
  53. {
  54. Font = fSega,
  55. FontSize = 40
  56. };
  57. g.DrawString("SEGA.ttf", tf, new RectangleF(4, 190, 800, 50));
  58.  
  59. var pals = fEmoji.CreateFontTables(TableTag.CpalDraw).GetPalettes();
  60. tf = new TextFormat
  61. {
  62. Font = fEmoji,
  63. FontSize = 40,
  64. Palette = pals[0] ?? null
  65. };
  66. g.DrawString("FirefoxEmoji.ttf \U0001F433\U0001F349\U0001F367", tf, new RectangleF(4, 240, 800, 50));
  67. return bmp;
  68. }
  69. }
  70. }
  71.