Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active September 14, 2020 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/10f0e5f274b436379dbc8fd4fb183fc5 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/10f0e5f274b436379dbc8fd4fb183fc5 to your computer and use it in GitHub Desktop.
Aspose.Font for .NET
Gists for Aspose.Font for .NET
// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
License lic = new License();
lic.SetLicense("path-to-licence-file.lic");
// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
string dataDir = @"c:\temp\";
// Load an existing Visio file in the stream
FileStream LicStream = new FileStream(dataDir + "Aspose.Font.lic", FileMode.Open);
License license = new License();
license.SetLicense(LicStream);
// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
byte[] fontMemoryData = File.ReadAllBytes(dataDir + "OpenSans-Regular.cff");
FontDefinition fd = new FontDefinition(FontType.CFF, new FontFileDefinition("cff", new ByteContentStreamSource(fontMemoryData)));
CffFont cffFont = Aspose.Font.Font.Open(fd) as CffFont;
// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
string fileName = dataDir + "OpenSans-Regular.cff"; //Font file name with full path
FontDefinition fd = new FontDefinition(FontType.CFF, new FontFileDefinition("cff", new FileSystemStreamSource(fileName)));
CffFont ttfFont = Aspose.Font.Font.Open(fd) as CffFont;
// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
//byte array to load Font from
string dataDir = RunExamples.GetDataDir_Data();
byte[] fontMemoryData = File.ReadAllBytes(dataDir + "OpenSans-Regular.cff");
FontDefinition fd = new FontDefinition(FontType.CFF, new FontFileDefinition("cff", new ByteContentStreamSource(fontMemoryData)));
CffFont cffFont = Aspose.Font.Font.Open(fd) as CffFont;
//Work with data from just loaded CffFont object
//Save CffFont to disk
//Output Font file name with full path
string outputFile = RunExamples.GetDataDir_Data() + "OpenSans-Regular_out.cff";
cffFont.Save(outputFile);
// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
string fileName = dataDir + "Montserrat-Regular.ttf"; //Font file name with full path
FontDefinition fd = new FontDefinition(FontType.TTF, new FontFileDefinition("ttf", new FileSystemStreamSource(fileName)));
TtfFont ttfFont = Aspose.Font.Font.Open(fd) as TtfFont;
bool latinText = true;
for (uint code = 65; code < 123; code++)
{
GlyphId gid = ttfFont.Encoding.DecodeToGid(code);
if (gid == null || gid == GlyphUInt32Id.NotDefId)
{
latinText = false;
}
}
if (latinText)
{
Console.WriteLine(string.Format("Font {0} supports latin symbols.", ttfFont.FontName));
}
else
{
Console.WriteLine(string.Format("Latin symbols are not supported by font {0}.", ttfFont.FontName));
}
// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
//Font to check
string fileName = dataDir + "Montserrat-Regular.ttf"; //Font file name with full path
FontDefinition fd = new FontDefinition(FontType.TTF, new FontFileDefinition("ttf", new FileSystemStreamSource(fileName)));
TtfFont font = Aspose.Font.Font.Open(fd) as TtfFont;
LicenseFlags licenseFlags = null;
if (font.TtfTables.Os2Table != null)
{
licenseFlags = font.TtfTables.Os2Table.GetLicenseFlags();
}
if (licenseFlags == null || licenseFlags.FSTypeAbsent)
{
Console.WriteLine(string.Format("Font {0} has no embedded license restrictions", font.FontName));
}
else
{
if (licenseFlags.IsEditableEmbedding)
{
Console.WriteLine(
string.Format("Font {0} may be embedded, and may be temporarily loaded on other systems.", font.FontName)
+ " In addition, editing is permitted, including ability to format new text"
+ " using the embedded font, and changes may be saved.");
}
else if (licenseFlags.IsInstallableEmbedding)
{
Console.WriteLine(
string.Format("Font {0} may be embedded, and may be permanently installed", font.FontName)
+ " for use on a remote systems, or for use by other users.");
}
else if (licenseFlags.IsPreviewAndPrintEmbedding)
{
Console.WriteLine(
string.Format("Font {0} may be embedded, and may be temporarily loaded", font.FontName)
+ " on other systems for purposes of viewing or printing the document.");
}
else if (licenseFlags.IsRestrictedLicenseEmbedding)
{
Console.WriteLine(
string.Format("Font {0} must not be modified, embedded or exchanged in any manner", font.FontName)
+ " without first obtaining explicit permission of the legal owner.");
}
}
// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
string fileName = dataDir + "Montserrat-Regular.ttf"; //Font file name with full path
FontDefinition fd = new FontDefinition(FontType.TTF, new FontFileDefinition("ttf", new FileSystemStreamSource(fileName)));
TtfFont font = Aspose.Font.Font.Open(fd) as TtfFont;
string name = font.FontName;
Console.WriteLine("Font name: " + name);
Console.WriteLine("Glyph count: " + font.NumGlyphs);
string metrics = string.Format(
"Font metrics: ascender - {0}, descender - {1}, typo ascender = {2}, typo descender = {3}, UnitsPerEm = {4}",
font.Metrics.Ascender, font.Metrics.Descender,
font.Metrics.TypoAscender, font.Metrics.TypoDescender, font.Metrics.UnitsPerEM);
Console.WriteLine(metrics);
//Get cmap unicode encoding table from font as object TtfCMapFormatBaseTable to access information about font glyph for symbol 'A'.
//Also check that font has object TtfGlyfTable (table 'glyf') to access glyph.
Aspose.Font.TtfCMapFormats.TtfCMapFormatBaseTable cmapTable = null;
if (font.TtfTables.CMapTable != null)
{
cmapTable = font.TtfTables.CMapTable.FindUnicodeTable();
}
if (cmapTable != null && font.TtfTables.GlyfTable != null)
{
Console.WriteLine("Font cmap unicode table: PlatformID = " + cmapTable.PlatformId + ", PlatformSpecificID = " + cmapTable.PlatformSpecificId);
//Code for 'A' symbol
char unicode = (char)65;
//Glyph index for 'A'
uint glIndex = cmapTable.GetGlyphIndex(unicode);
if (glIndex != 0)
{
//Glyph for 'A'
Glyph glyph = font.GetGlyphById(glIndex);
if (glyph != null)
{
//Print glyph metrics
Console.WriteLine("Glyph metrics for 'A' symbol:");
string bbox = string.Format(
"Glyph BBox: Xmin = {0}, Xmax = {1}" + ", Ymin = {2}, Ymax = {3}",
glyph.GlyphBBox.XMin, glyph.GlyphBBox.XMax,
glyph.GlyphBBox.YMin, glyph.GlyphBBox.YMax);
Console.WriteLine(bbox);
Console.WriteLine("Width:" + font.Metrics.GetGlyphWidth(new GlyphUInt32Id(glIndex)));
}
}
}
// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
string fileName= dataDir + "Montserrat-Regular.ttf"; //Font file name with full path
FontDefinition fd = new FontDefinition(FontType.TTF, new FontFileDefinition("ttf", new FileSystemStreamSource(fileName)));
TtfFont ttfFont = Aspose.Font.Font.Open(fd) as TtfFont;
// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
string fileName1 = dataDir + "Montserrat-Bold.ttf"; //Font file name with full path
FontDefinition fd1 = new FontDefinition(FontType.TTF, new FontFileDefinition("ttf", new FileSystemStreamSource(fileName1)));
TtfFont ttfFont1 = Aspose.Font.Font.Open(fd1) as TtfFont;
string fileName2 = dataDir + "Lora-Bold.ttf"; //Font file name with full path
FontDefinition fd2 = new FontDefinition(FontType.TTF, new FontFileDefinition("ttf", new FileSystemStreamSource(fileName2)));
TtfFont ttfFont2 = Aspose.Font.Font.Open(fd2) as TtfFont;
DrawText("Hello world", ttfFont1, 14, Brushes.White, Brushes.Black, dataDir + "hello1_montserrat_out.jpg");
DrawText("Hello world", ttfFont2, 14, Brushes.Yellow, Brushes.Red, dataDir + "hello2_lora_out.jpg");
// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
class GlyphOutlinePainter : IGlyphOutlinePainter
{
private System.Drawing.Drawing2D.GraphicsPath _path;
private System.Drawing.PointF _currentPoint;
public GlyphOutlinePainter(System.Drawing.Drawing2D.GraphicsPath path)
{
_path = path;
}
public void MoveTo(MoveTo moveTo)
{
_path.CloseFigure();
_currentPoint.X = (float)moveTo.X;
_currentPoint.Y = (float)moveTo.Y;
}
public void LineTo(LineTo lineTo)
{
float x = (float)lineTo.X;
float y = (float)lineTo.Y;
_path.AddLine(_currentPoint.X, _currentPoint.Y, x, y);
_currentPoint.X = x;
_currentPoint.Y = y;
}
public void CurveTo(CurveTo curveTo)
{
float x3 = (float)curveTo.X3;
float y3 = (float)curveTo.Y3;
_path.AddBezier(
_currentPoint.X,
_currentPoint.Y,
(float)curveTo.X1,
(float)curveTo.Y1,
(float)curveTo.X2,
(float)curveTo.Y2,
x3,
y3);
_currentPoint.X = x3;
_currentPoint.Y = y3;
}
public void ClosePath()
{
_path.CloseFigure();
}
}
// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
static void DrawText(string text, IFont font, double fontSize,
Brush backgroundBrush, Brush textBrush, string outFile)
{
//Get glyph identifiers for every symbol in text line
GlyphId[] gids = new GlyphId[text.Length];
for (int i = 0; i < text.Length; i++)
gids[i] = font.Encoding.DecodeToGid(text[i]);
// set common drawing settings
double dpi = 300;
double resolutionCorrection = dpi / 72; // 72 is font's internal dpi
// prepare output bitmap
Bitmap outBitmap = new Bitmap(960, 720);
outBitmap.SetResolution((float)dpi, (float)dpi);
Graphics outGraphics = Graphics.FromImage(outBitmap);
outGraphics.FillRectangle(backgroundBrush, 0, 0, outBitmap.Width, outBitmap.Height);
outGraphics.SmoothingMode = SmoothingMode.HighQuality;
//declare coordinate variables and previous gid
GlyphId previousGid = null;
double glyphXCoordinate = 0;
double glyphYCoordinate = fontSize * resolutionCorrection;
//loop which paints every glyph in gids
foreach (GlyphId gid in gids)
{
// if the font contains the gid
if (gid != null)
{
Glyph glyph = font.GlyphAccessor.GetGlyphById(gid);
if (glyph == null)
continue;
// path that accepts drawing instructions
GraphicsPath path = new GraphicsPath();
// Create IGlyphOutlinePainter implementation
GlyphOutlinePainter outlinePainter = new GlyphOutlinePainter(path);
// Create the renderer
Aspose.Font.Renderers.IGlyphRenderer renderer = new
Aspose.Font.Renderers.GlyphOutlineRenderer(outlinePainter);
// get common glyph properties
double kerning = 0;
// get kerning value
if (previousGid != null)
{
kerning = (font.Metrics.GetKerningValue(previousGid, gid) /
glyph.SourceResolution) * fontSize * resolutionCorrection;
kerning += FontWidthToImageWith(font.Metrics.GetGlyphWidth(previousGid),
glyph.SourceResolution, fontSize);
}
// glyph positioning - increase glyph X coordinate according to kerning distance
glyphXCoordinate += kerning;
// Glyph placement matrix
TransformationMatrix glyphMatrix =
new TransformationMatrix(
new double[]
{
fontSize*resolutionCorrection,
0,
0,
// negative because of bitmap coordinate system begins from the top
- fontSize*resolutionCorrection,
glyphXCoordinate,
glyphYCoordinate
});
// render current glyph
renderer.RenderGlyph(font, gid, glyphMatrix);
// fill the path
path.FillMode = FillMode.Winding;
outGraphics.FillPath(textBrush, path);
}
//set current gid as previous to get correct kerning for next glyph
previousGid = gid;
}
//Save results
outBitmap.Save(outFile);
}
// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
static double FontWidthToImageWith(double width, int fontSourceResulution, double fontSize, double dpi = 300)
{
double resolutionCorrection = dpi / 72; // 72 is font's internal dpi
return (width / fontSourceResulution) * fontSize * resolutionCorrection;
}
// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
//byte array to load Font from
string dataDir = RunExamples.GetDataDir_Data();
byte[] fontMemoryData = File.ReadAllBytes(dataDir + "Montserrat-Regular.ttf");
FontDefinition fd = new FontDefinition(FontType.TTF, new FontFileDefinition("ttf", new ByteContentStreamSource(fontMemoryData)));
TtfFont ttfFont = Aspose.Font.Font.Open(fd) as TtfFont;
//Work with data from just loaded TtfFont object
//Save TtfFont to disk
//Output Font file name with full path
string outputFile = RunExamples.GetDataDir_Data() + "Montserrat-Regular_out.ttf";
ttfFont.Save(outputFile);
// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
string fileName = dataDir + "courier.pfb"; //Font file name with full path
FontDefinition fd = new FontDefinition(FontType.Type1, new FontFileDefinition("pfb", new FileSystemStreamSource(fileName)));
Type1Font font = Aspose.Font.Font.Open(fd) as Type1Font;
bool latinText = true;
for (uint code = 65; code < 123; code++)
{
GlyphId gid = font.Encoding.DecodeToGid(code);
if (gid == null || gid == GlyphUInt32Id.NotDefId)
{
latinText = false;
}
}
if (latinText)
{
Console.WriteLine(string.Format("Font {0} supports latin symbols.", font.FontName));
}
else
{
Console.WriteLine(string.Format("Latin symbols are not supported by font {0}.", font.FontName));
}
// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
string fileName = dataDir + "courier.pfb"; //Font file name with full path
FontDefinition fd = new FontDefinition(FontType.Type1, new FontFileDefinition("pfb", new FileSystemStreamSource(fileName)));
Type1Font font = Aspose.Font.Font.Open(fd) as Type1Font;
string name = font.FontName;
Console.WriteLine("Font name: " + name);
Console.WriteLine("Glyph count: " + font.NumGlyphs);
string metrics = string.Format(
"Font metrics: ascender - {0}, descender - {1}, typo ascender = {2}, typo descender = {3}, UnitsPerEm = {4}",
font.Metrics.Ascender, font.Metrics.Descender,
font.Metrics.TypoAscender, font.Metrics.TypoDescender, font.Metrics.UnitsPerEM);
Console.WriteLine(metrics);
// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
string fileName = dataDir + "courier.pfb"; //Font file name with full path
FontDefinition fd = new FontDefinition(FontType.Type1, new FontFileDefinition("pfb", new FileSystemStreamSource(fileName)));
Type1Font font = Aspose.Font.Font.Open(fd) as Type1Font;
// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
string fileName = dataDir + "courier.pfb"; //Font file name with full path
FontDefinition fd = new FontDefinition(FontType.Type1, new FontFileDefinition("pfb", new FileSystemStreamSource(fileName)));
Type1Font font = Aspose.Font.Font.Open(fd) as Type1Font;
DrawText("Hello world", font, 14, Brushes.White, Brushes.Black, dataDir + "hello1_type1_out.jpg");
DrawText("Hello world", font, 14, Brushes.Yellow, Brushes.Red, dataDir + "hello2_type1_out.jpg");
// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
class GlyphOutlinePainter : IGlyphOutlinePainter
{
private System.Drawing.Drawing2D.GraphicsPath _path;
private System.Drawing.PointF _currentPoint;
public GlyphOutlinePainter(System.Drawing.Drawing2D.GraphicsPath path)
{
_path = path;
}
public void MoveTo(MoveTo moveTo)
{
_path.CloseFigure();
_currentPoint.X = (float)moveTo.X;
_currentPoint.Y = (float)moveTo.Y;
}
public void LineTo(LineTo lineTo)
{
float x = (float)lineTo.X;
float y = (float)lineTo.Y;
_path.AddLine(_currentPoint.X, _currentPoint.Y, x, y);
_currentPoint.X = x;
_currentPoint.Y = y;
}
public void CurveTo(CurveTo curveTo)
{
float x3 = (float)curveTo.X3;
float y3 = (float)curveTo.Y3;
_path.AddBezier(
_currentPoint.X,
_currentPoint.Y,
(float)curveTo.X1,
(float)curveTo.Y1,
(float)curveTo.X2,
(float)curveTo.Y2,
x3,
y3);
_currentPoint.X = x3;
_currentPoint.Y = y3;
}
public void ClosePath()
{
_path.CloseFigure();
}
}
// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
static void DrawText(string text, IFont font, double fontSize,
Brush backgroundBrush, Brush textBrush, string outFile)
{
//Get glyph identifiers for every symbol in text line
GlyphId[] gids = new GlyphId[text.Length];
for (int i = 0; i < text.Length; i++)
gids[i] = font.Encoding.DecodeToGid(text[i]);
// set common drawing settings
double dpi = 300;
double resolutionCorrection = dpi / 72; // 72 is font's internal dpi
// prepare output bitmap
Bitmap outBitmap = new Bitmap(960, 720);
outBitmap.SetResolution((float)dpi, (float)dpi);
Graphics outGraphics = Graphics.FromImage(outBitmap);
outGraphics.FillRectangle(backgroundBrush, 0, 0, outBitmap.Width, outBitmap.Height);
outGraphics.SmoothingMode = SmoothingMode.HighQuality;
//declare coordinate variables and previous gid
GlyphId previousGid = null;
double glyphXCoordinate = 0;
double glyphYCoordinate = fontSize * resolutionCorrection;
//loop which paints every glyph in gids
foreach (GlyphId gid in gids)
{
// if the font contains the gid
if (gid != null)
{
Glyph glyph = font.GlyphAccessor.GetGlyphById(gid);
if (glyph == null)
continue;
// path that accepts drawing instructions
GraphicsPath path = new GraphicsPath();
// Create IGlyphOutlinePainter implementation
GlyphOutlinePainter outlinePainter = new GlyphOutlinePainter(path);
// Create the renderer
Aspose.Font.Renderers.IGlyphRenderer renderer = new
Aspose.Font.Renderers.GlyphOutlineRenderer(outlinePainter);
// get common glyph properties
double kerning = 0;
// get kerning value
if (previousGid != null)
{
kerning = (font.Metrics.GetKerningValue(previousGid, gid) /
glyph.SourceResolution) * fontSize * resolutionCorrection;
kerning += FontWidthToImageWith(font.Metrics.GetGlyphWidth(previousGid),
glyph.SourceResolution, fontSize);
}
// glyph positioning - increase glyph X coordinate according to kerning distance
glyphXCoordinate += kerning;
// Glyph placement matrix
TransformationMatrix glyphMatrix =
new TransformationMatrix(
new double[]
{
fontSize*resolutionCorrection,
0,
0,
// negative because of bitmap coordinate system begins from the top
- fontSize*resolutionCorrection,
glyphXCoordinate,
glyphYCoordinate
});
// render current glyph
renderer.RenderGlyph(font, gid, glyphMatrix);
// fill the path
path.FillMode = FillMode.Winding;
outGraphics.FillPath(textBrush, path);
}
//set current gid as previous to get correct kerning for next glyph
previousGid = gid;
}
//Save results
outBitmap.Save(outFile);
}
// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
static double FontWidthToImageWith(double width, int fontSourceResulution, double fontSize, double dpi = 300)
{
double resolutionCorrection = dpi / 72; // 72 is font's internal dpi
return (width / fontSourceResulution) * fontSize * resolutionCorrection;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment