Hello,
Trying to check the capabilities of Aspose.Font component in .NET.
My basic requirement is, I have different types of font files like .ttf, .otf, .woff, .woff2, etc. Need to read each file from a file path, and extract font weight (300/400/600/etc.) and font style (normal, bold, italic, etc.)
I didn’t find the good example on this how to do the same, can you please help me on it?
Regards,
Srinivas
@srinudhulipalla
Thanks for the report. I have not confirmed this yet and can only confirm it after reproducing on our side. I need a few specifics to give an accurate answer for Aspose.Font when converting ttf, otf, woff, woff2 to unknown.
- Which version of Aspose.Font for .NET are you using (e.g., 23.x, 22.x)?
- Could you share a short code snippet you’ve tried to load a font and access its properties?
- Are you working with any specific font formats (e.g., TrueType, OpenType) that give unexpected results?
A support engineer will review this information and get back to you shortly.
Once I have the missing details, I’ll try to reproduce this. If I can confirm it, I’ll log it internally and follow up here.
A support engineer will follow up in this thread if needed.
For your queries,
I am using Aspose.Font version - 26.2.0
Below is the code example I tried and the code I got from AI, but it didn’t work.
public static (int weight, string style) GetFontInfo(string fontPath)
{
var fontDefinition = new FontDefinition(
FontType.TTF,
new FileSystemStreamSource(fontPath)
);
Font font = Font.Open(fontDefinition);
if (font is TtfFont ttf)
{
int weight = ttf.Os2Table.UsWeightClass;
bool isItalic = ttf.Os2Table.FsSelection.Italic;
string style = isItalic ? "italic" : "normal";
weight = NormalizeWeight(weight);
return (weight, style);
}
return (400, "normal");
}
private static int NormalizeWeight(int weight)
{
if (weight < 100) return 100;
if (weight > 900) return 900;
return (int)(Math.Round(weight / 100.0) * 100);
}
I want to try this code for all types of font formats like .ttf, .otf, .woff, .woff2
@srinudhulipalla
Thanks for the report. I have not confirmed this yet and can only confirm it after reproducing on our side. Based on your description, this might be a bug in Aspose.Font. To confirm and log it internally, I need a few details.
- What error or unexpected result do you see when running the code with .otf, .woff, or .woff2 files?
- Are you using a licensed (or trial) build of Aspose.Font 26.2.0, and on which .NET runtime (e.g., .NET 6, .NET Framework 4.8) is the code executed?
A support engineer will review this information and follow up in this thread.
Once I have the missing details, I’ll try to reproduce this. If I can confirm it, I’ll log it internally and follow up here.
A support engineer will follow up in this thread if needed.
I just found some new code, is this the right way to use Aspose.Font package to get the font weights and styles from it?
using System;
using System.IO;
using Aspose.Font;
using Aspose.Font.Sources;
using Aspose.Font.Ttf;
public class FontMetadata
{
public string Family { get; set; }
public int Weight { get; set; } // 300, 400, 700, etc.
public string Style { get; set; } // Normal, Bold, Italic, etc.
}
public static class AsposeFontHelper
{
static AsposeFontHelper()
{
//set license here
}
public static FontMetadata GetDetails(string filePath)
{
string extension = Path.GetExtension(filePath).ToLower().Replace(".", "");
FontDefinition fontDefinition = new FontDefinition(
FontType.TTF,
new FontFileDefinition(extension, new FileSystemStreamSource(filePath))
);
Font openedFont = Font.Open(fontDefinition);
if (openedFont is not TtfFont font)
throw new InvalidOperationException("Unsupported font type");
int weight = font.TtfTables.Os2Table?.USWeightClass ?? 400;
bool isItalic =
(font.TtfTables.Os2Table?.FSSelection & 0x01) != 0 ||
(font.TtfTables.HeadTable?.MacStyle & 0x02) != 0;
bool isBold =
(font.TtfTables.Os2Table?.FSSelection & 0x20) != 0 ||
(font.TtfTables.HeadTable?.MacStyle & 0x01) != 0;
string style =
isBold && isItalic ? "Bold Italic" :
isBold ? "Bold" :
isItalic ? "Italic" :
"Normal";
return new FontMetadata
{
Family = font.FontName,
Weight = weight,
Style = style
};
}
}
@srinudhulipalla
This is something that we need to investigate and provide code samples. For the sake of it, an investigation has been logged in our issue management with below ID:
Issue ID(s): FONTNET-862
We will look into its details and keep you posted with the status of ticket resolution. Please be patient and spare us some time.
We are sorry for the inconvenience.
@asad.ali
Thanks for your response. What I need is to extract the font weight and font style from the any font file. It became bit hard to find the good example for the same. I took bit AI help on this and tested, and posted the example here which I tried.
But not sure, if this is the right example to do the same using Aspose.Font. Need your guidence here for the same. Thanks.
@srinudhulipalla
We have recorded your concerns and will surely inform you as soon as the ticket is resolved.
@asad.ali
FONTNET-862 ---- Status : Resolved
I can see the issue status as Resolved. May I know what is the resolution? I just asked a question “How to get the font weight and font style? and also gave the code example for your referrence.”
@srinudhulipalla
First method GetFontInfo(string fontPath) has issues with compilation and logic.
Compilation issues - using following call ttf.Os2Table is wrong, cause Os2Table is the property of TtfTableRepository class, not TtfFont.
It seems that this code was generated by AI, but many LLM models have such bug as using of non-existing properties(methods).
Correct code - ttf.TtfTables.Os2Table.
Same bug - code
bool isItalic = ttf.Os2Table.FsSelection.Italic;
There is no property Os2Table.FsSelection, property with similiar name Os2Table.FSSelection has type ushort, so it can’t have property Italic.
As for incorrect logic used in this method GetFontInfo() - there are no any checks about empty Os2Table and appropriate corrections to final values (weight, isItalic) for these cases.
Also logic of method NormalizeWeight() is unclear. It seems that
raw value of field Os2Table.USWeightClass is enough according to specification and no any changes required. But may be you want to get CSS weight? If so - yes, using of NormalizeWeight() is good.
On the other hand, code from method GetDetails(string filePath) looks well and it’s enough to get weight/style for all formats listed (.ttf, .otf, .woff, .woff2). The only flaw there is logic to set Family - Family = font.FontName, in many cases font.FontName may be full font name instead of family, but field Family is out of main customer question (font weight/style), so I suggest simply remove field Family from class FontMetadata and linked code. And may be, if you needed in CSS weight exactly, it has sense to use method NormalizeWeight() inside GetDetails().
If using method GetDetails() will result an errors/exceptions. Would you please share the font that can be used to reproduce the problem? We can further investigate accordingly.