Ligatures.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using GrapeCity.Documents.Pdf;
  9. using GrapeCity.Documents.Text;
  10. using GCTEXT = GrapeCity.Documents.Text;
  11. using GCDRAW = GrapeCity.Documents.Drawing;
  12.  
  13. namespace DsPdfWeb.Demos.Basics
  14. {
  15. // Ligatures (joining two or more letters into a single glyph) are supported by DsPdf,
  16. // provided the selected font supports them, and the corresponding font feature is on.
  17. // For the complete list of font features see featurelist.htm.
  18. // See also FontFeatures.
  19. public class Ligatures
  20. {
  21. public int CreatePDF(Stream stream)
  22. {
  23. // The list of common Latin ligatures:
  24. const string latinLigatures = "fi, fj, fl, ff, ffi, ffl.";
  25. // Set up ligature-related font features:
  26. // All ON:
  27. FontFeature[] allOn = new FontFeature[]
  28. {
  29. new FontFeature(FeatureTag.clig, true), // Contextual Ligatures
  30. new FontFeature(FeatureTag.dlig, true), // Discretionary Ligatures
  31. new FontFeature(FeatureTag.hlig, true), // Historical Ligatures
  32. new FontFeature(FeatureTag.liga, true), // Standard Ligatures
  33. new FontFeature(FeatureTag.rlig, true), // Required Ligatures
  34. };
  35. // All OFF:
  36. FontFeature[] allOff = new FontFeature[]
  37. {
  38. new FontFeature(FeatureTag.clig, false),
  39. new FontFeature(FeatureTag.dlig, false),
  40. new FontFeature(FeatureTag.hlig, false),
  41. new FontFeature(FeatureTag.liga, false),
  42. new FontFeature(FeatureTag.rlig, false),
  43. };
  44. var doc = new GcPdfDocument();
  45. var g = doc.NewPage().Graphics;
  46. // Text insertion point:
  47. var ip = new PointF(72, 72);
  48. var tf = new TextFormat();
  49. tf.Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf"));
  50. tf.FontSize = 20;
  51. g.DrawString($"Common Latin ligatures, font {tf.Font.FontFamilyName}", tf, ip);
  52. ip.Y += 36;
  53. // Turn all ligature features OFF:
  54. tf.FontFeatures = allOff;
  55. g.DrawString($"All ligature features OFF: {latinLigatures}", tf, ip);
  56. ip.Y += 36;
  57. // Turn all ligature features ON:
  58. tf.FontFeatures = allOn;
  59. g.DrawString($"All ligature features ON: {latinLigatures}", tf, ip);
  60. ip.Y += 72;
  61. // Repeat with a different font:
  62. tf.Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "Gabriola.ttf"));
  63. g.DrawString($"Common Latin ligatures, font {tf.Font.FontFamilyName}", tf, ip);
  64. ip.Y += 36;
  65. // Turn all ligature features OFF:
  66. tf.FontFeatures = allOff;
  67. g.DrawString($"All ligature features OFF: {latinLigatures}", tf, ip);
  68. ip.Y += 36;
  69. // Turn all ligature features ON:
  70. tf.FontFeatures = allOn;
  71. g.DrawString($"All ligature features ON: {latinLigatures}", tf, ip);
  72. // Done:
  73. doc.Save(stream);
  74. return doc.Pages.Count;
  75. }
  76. }
  77. }
  78.