Can I read and preview fonts (mainly ttf and otf) in C#? What other languages can/should I use?
Read:
Info like font family, font name
Preview:
Use the font to display some text
Any way to display all supported font characters?
Caution: don't use System.Drawing / System.Windows.Forms if you want to preview OTF fonts. Unless they're TTF's in disguise, you won't get them to show. System.Drawing, based on GDI+, only supports TTF fonts!
However, if you can use .NET 3.0, you could use
Fonts.GetFontFamilies(location)
from System.Windows.Media namespace (just reference PresentationCore.dll).
From a FontFamily, you can get the individual Typefaces (.ttc files contain more than one 'font', but a FontFamily also combines the various weights and variants). And from a Typeface, you can call TryGetGlyphTypeface to get the GlyphTypeface, which has a CharacterToGlyphMap property, which should tell you which unicode codepoints are physically supported.
It also seems possible to use GlyphTypeface directly, but I see no way that you can handle .ttc files. However, if that's not relevant, just create a GlyphTypeface per file.
I'd advice against trying all Unicode codepoints sequentially though.
To preview a font you can render it out to a form like this:
public partial class MyForm: Form
{
.
.
.
public void ShowMyFont()
{
Graphics graphics = this.CreateGraphics();
graphics.DrawString("Hello world!", new Font("Arial", 12), Brushes.Black, 0, 0);
}
}
Related
I want to get all supported characters of a font, that font is stored in localhost:53625/fonts/WINGDNG2.TTF. I has try this thread in Stackoverflow
Get supported characters of a font - in C#
but have no luck.
When
Fonts.GetFontFamilies(#"C:\WINDOWS\Fonts\Arial.TTF");
return a list contain 1 family,
Fonts.GetFontFamilies(new
Uri(http://localhost:53625/fonts/WINGDNG2.TTF));
return a list contain 0 family.
Thank you for reading my question.
p/s: My project is a website using ASP.NET MVC5, so the function can be in C# (server side) or in Javascript (client site). If there are any codes can give me a list of supported characters of a font (with specific path like above), which side is no problem.
Instead of enumerating the Font Families, you can instantiate GlyphTypeface directly, if you know the font file location:
GlyphTypeface glyphTypeface = new GlyphTypeface(new Uri("file:///C:\\WINDOWS\\Fonts\\Kooten.ttf"));
Then access the GlyphCharacterMap to get all the supported characters:
IDictionary<int, ushort> characterMap = glyph.CharacterToGlyphMap;
https://msdn.microsoft.com/en-us/library/system.windows.media.glyphtypeface(v=vs.110).aspx
i'm using latha font for tamil typing ,I need to change latha font to Kal (Another font family). But it doesn't work for me,also i'm trying to convert latha to unicode to kal.
Need some code in c#
If you need to convert text only use < http://software.nhm.in/products/converter >. It handles many encodings and it is free.
Some manufacturers provide Unicode version of their font also in addition to the original encoding.
If you need to type in Latha font using a different Keyboard interface then get the free layout driver from < http://www.thamizha.com/project/ekalappai >
My own < http://www.mediafire.com/download/jjaqdbuk99062ki/Tamil_Driver_1.03.zip > You can type in Inscript Keyboard mode
Give more details about Kal fonts.
Why the following code does not throw an exception?
FontFamily font = new FontFamily("bla bla bla");
I need to know if a specific font (as combination of FontFamily, FontStyle, FontWeight, ...) exists in my current OS. How have I to do?
This is by design. Programs frequently ask for fonts that are not present on the machine, especially in a country far flung from the programmer's domicile. The font mapper produces an alternative. Font substitution is in general very common. You are looking at Arial right now if you are on a Windows machine. But I can paste 你好世界 into this post and you'll see it render accurately, even though Arial doesn't have glyphs for Chinese characters.
So hint number one is to not actually worry about what fonts are available. The Windows api has EnumFontFamiliesEx() to enumerate available font families. But that's not exposed in WPF, some friction with OpenType there, a font standard that's rather poorly integrated with Windows. Another shadow cast when Adobe gets involved with anything Microsoft does, it seems.
Some confusion in the comments about Winforms' FontFamily class. Which is actually usable in this case, its GetFamilies() method returns an array of available families. But only TrueType, not OpenType fonts.
You can use the class System.Drawing.Text.InstalledFontCollection
http://msdn.microsoft.com/en-us/library/system.drawing.text.installedfontcollection.aspx
WPF have a framework specific method Fonts.SystemFontFamilies
http://msdn.microsoft.com/en-us/library/system.windows.media.fonts.systemfontfamilies.aspx
To answer the question of why it isn't throwing an exception, according to FontFamily Constructor on MSDN the exception wasn't added until framework version 3.5.
I suspect that you are targeting version 3.0 or below.
Cheers!
You can browse the available fonts on the System using the Fonts.SystemFontFamilies collection - use some Linq to match on whatever conditions you need;
// true
bool exists = (from f in Fonts.SystemFontFamilies where f.Source.Equals("Arial") select f).Any();
// false
exists = (from f in Fonts.SystemFontFamilies where f.Source.Equals("blahblah") select f).Any();
Is there the way to create iTextSharp font using additional info (such as gdiCharSet etc) from System.Drawing.Font object?
Short answer: Yes.
Long answer: Each attribute is a bit different, but all that information can be expressed within PDF in general, and iText[Sharp] in specific.
You can specify a font's encoding when you create it, but you must do so in a way iText understands. Specifically, encoding values are strings within iText[Sharp]. BaseFont has a number of public static string members that list many of the available encodings, including several code pages that will map nicely to some of the GdiCharSet values. Others, not so much.
I generally suggest using "Identity-H" and subsetting your fonts (which happens automagically with Identity-H, you can't avoid it, which is a Good Thing) unless you need to keep the file size to a bare minimum. There are several single-byte encodings, the most common of which is "WinAnsiEncoding", BaseFont.WINANSI (the default IIRC). The string can also be the name of a "CMap" (such as Identity-H).
CMaps are generally language specific, and encoding specific. UTF & Japanese, or Big5 (a Chinese encoding as I recall), or what have you. Identity-H (and Identity-V) are font specific instead. They simply map values in the content stream to glyph indexes in the font (which can vary wildly from one font to the next, or between versions of a given font: that's why you're required to embed subsets of Identity-* fonts).
In PDF (and therefore iText[Sharp]), "bold" and "italic" are part of the font's identity, not a property. "Arial-Bold", "Arial-Italic", etc.
Strikeout and underline are decorations added after the fact (though I believe iText will let you set a flag at the font level for such things in com.itextpdf.text.Font's constructor).
iText won't give you direct access to the height, though a font's "descriptor" will let you define it.
The point size isn't a property either, you set it and the font (and color, default black) before you draw some text.
I'm working with ITextSharp for a project and am looking for a reasonable way to get a string list of different fonts it has available for use.
I thought maybe I could just use reflection and loop over a class of available fonts, but there does not seem to be an easy way to do this. What I really want to do is provide a drop down of available/supported fonts for my users to select from Does anyone have any thoughts on how I might accomplish this?
This webpage has a great reference for how to work with the 14 embedded fonts in iTextSharp, as well as how to embed and use any fonts of your choosing.
To get the list of fonts included in iTextSharp:
Dim myCol As ICollection
//Returns the list of all font families included in iTextSharp.
myCol = iTextSharp.text.FontFactory.RegisteredFamilies
//Returns the list of all fonts included in iTextSharp.
myCol = iTextSharp.text.FontFactory.RegisteredFonts
An example of a font family is Helvetica. An example of a font is Helvetica-Bold or Helvetica-Italic.
First call FontFactory.RegisterDirectories(); to get all fonts on the system registered.
Then call FontFactory.RegisteredFonts; to get all the fonts.