I'm trying to draw text over a bitmap image and I've done some research and found that .NET/GDI+ doesn't support OTF fonts. I read somewhere that you could use TextRenderer.DrawText to render OTF fonts with GDI, but I can't seem to figure out how, nor does the quality compare to Graphics.DrawString in the least bit.
First of all, is it possible to use OTF fonts at all in VS or did I read something incorrectly?
Second, If the answer is TextRenderer.DrawText, how do I use the OTF fonts? It looks like I have to use the System.Drawing.Font class, but I don't think those support the fonts and that's the issue, correct?
How do I get the fonts to render clearly like the TTF fonts using GDI+ (Graphics.DrawString)
Using Graphics.DrawString I was able to use a RectangleF (with specified width, 0 height) as a boundary for my text, and the text wrapped correctly. With TextRenderer.DrawText, the ability to use RectangleF disappears and only allows Rectangle which doesn't seem to allow 0 height, while still allowing text to display (ie. text must be within the rectangle, and text wrapping does not work). Am I doing it wrong?
Any help would be greatly appreciated. If this cannot be done, is there a way to convert the OTF fonts, or find the Gotham font family online in TTF version? I really need these fonts!
Related
Understanding The Problem
I've run into a problem. I have a need to convert an svg file with multiple custom fonts into a png. This is supported by svglib. I actually was able to convert one of the fonts into a proper format for svglib to properly transform an svg to a png with the font. The problem is, I don't necessarily know what fonts are going to be "good" fonts and what fonts are going to be "bad" fonts (otf fonts that load for some reason and otf fonts that don't load for some reason), so ideally, I'd have a function in Python 3.x.x to convert all fonts to the same workable format. According to this google group on the subject, its a complicated beast (as of 2013). However, I could load any font (ttf / otf / woff) into this site and use it to get python to pump out a png file, just by having the font in the same directory as the svg file with the same name as the font file as the font family, with the proper results, but I couldn't get the converted font using Python's fonttools to work with the below provided code:
Code
from reportlab.graphics import renderPM
from svglib.svglib import svg2rlg
from fontTools import ttLib
from io import BytesIO
from PIL import Image
# Here I tried to take the font (which I had initially as a woff) and convert it to a ttf.
#woff2.decompress("./my_font.woff", "./my_font.ttf")
# Here I tried to take the original otf and convert it to a ttf
# font = ttLib.TTFont("my_font.otf")
# font.flavor = None
# font.save("my_font.ttf")
buff = BytesIO()
drawing = svg2rlg("./test.svg")
renderPM.drawToFile(drawing, buff, fmt="PNG")
img = Image.open(buff)
# l, w = image.size
# img = img.resize()
new_buff = BytesIO()
img.save("new_test.png", dpi=(600, 600))
Font For Minimal Reproduceable Example
I can upload a demo working font and the same font that is not working with the given format, but before I did that, I wanted to make sure that was part of the SO community guidelines to distribute potentially copyrighted (and other items of this nature with different licenses and EULAs) works - maybe a comment can confirm or deny this (I don't have the EULA, so I will probably find a link to a font that will be reproduceable). I think easiest is for me to find a link to a font online that I can reproduce the problem with, so I will get to looking and update the post with a link, but I mean... come on! I can't be the only person trying to solve this problem?!
Working font example
A non working font example - I could be wrong, but I think if you convert from a ttf to a woff back to a ttf using fonttools that you will be able to reproduce this issue. I'm still looking for an online example.
Problem Statement
So to make sure the problem statement is well articulated, the goal is to: Take any otf / ttf / or woff (even for simplicity sake - just a woff) file and convert it to a ttf file via Python that is supported by reportlab (which would mean it would be supported by svglib).
As unfortunate as it is, I couldn't supply a font to help with the "minimal, reproducible example clause", because finding a font online to replicate this with is very challenging (maybe I wasn't searching in the right place). However, I could convert the font in fontforge and opted for that. I could use the scripting portion of fontforge to accomplish this, so I can use this to convert fonts in bulk, and despite being GPL, after reaching out to the community from fontforge, they said:
We do not consider fonts created with FontForge or scripts run in FontForge to be derivative works of FontForge, which is standard practice for content creation tools and script interpreters. The FontForge license thus has no implication upon the ownership of those fonts and scripts or the owner's right to use and to distribute them. FontForge depends heavily upon a number of GPL libraries at the moment, and that is unlikely to change.
So for my purposes, I am content with this solution.
i'm building a website and wanted to display a bunch of international (pretty obscure) characters on the splashscreen. out of concern for platform-specific system fonts, instead of typing in those characters as text, i want to embed them as SVGs. does anyone know of an easy way to take the glyphs i'm seeing in my keynote / pdf and convert them to SVGs?
tried a bunch of things - converted keynote to pdf and used several online pdf->svg converters, but that misses some obscure characters and returns a misformatted svg. tried copying and pasting the text into figma and exporting as svg, but figma doesn't render more rare international fonts. i just want to take what i'm seeing on my screen and make those SVGs.
The only thing that occurs to me is to open the fonts or drawings in an editor like Adobe Illustrator or Inskcape and edit them there and download them as svg
Do svg fonts use ligature tables? If so, can you point me to an example of what one would look like (it doesn't have to have glyph data)?
I just wanted to make sure before I go changing font data that I exported from out of font forge.
Thank you!
I'm going to assume you are referring to the type of font with an .svg extension. And not OpenType fonts that contain SVG-based glyphs.
Information on the capabilities of SVG fonts can be found in the SVG Specification.
The answer is no. SVG fonts are very primitive. They support kerning, and that's about it.
It's also worth pointing out that SVG fonts are deprecated now. It has been removed from the draft SVG 2 specification. And support is gradually being removed from browsers.
I was browsing SVG fonts in MDN, where it is mentioned that <font-face>, <missing-glyph>, <hkern> and <vkern> are depreciated. Only <glyph> is not depreciated. It does not mention anything about a recommended way of specifying the font face properties.
The W3C SVG recommendation also does not say anything about an alternative way of specifying SVG fonts, apart from noting that everything in the <font-face> can be done equivalently in CSS. It does not provide any alternative for <hkern> or <vkern>. I intend to create a standalone SVG file, not to be modified by the site CSS, so I would like to keep the whole font definition in the SVG.
So, what is this obscure new way of specifying SVG fonts?
"SVG fonts" as a data file that uses SVG markup to define a font resource has been deprecated; it turned out to be a bad idea, and ended up not addressing the issues that typography on the web needed addressing. It was added in SVG 1.1 but removed again in SVG 2.0, and almost all browsers that did end up adding support for it removed that support again since.
Instead, all browsers now support "webfonts": regular OpenType fonts packed for the web using the "Web Open Font Format", aka WOFF/WOFF2, based on the OpenType format, which supports several different outline types:
TrueType (quadratic curves and compound glyphs, often with ttf extension, but the extension is literally irrelevant)
Type2 in CFF/CFF2 (cubic curves and arbitrary subroutines, often with otf extension, but again: the extension is wholly irrelevant)
Embedded bitmaps (yes, OpenType fonts can indeed be true bitmap fonts, with as many different bitmaps as necessary to cover as many pixel sizes as necessary)
SVG (that might be surprising, but SVG is the exact same kind of vector graphics language as TT and CFF/CFF2 are, so it made sense to allow glyph outline data to be specified using SVG as well, particularly for fonts that need explicit colour palettes, like emoji fonts)
So if you absolutely need to keep your SVG data around, then make yourself an OpenType-with-SVG-outlines font, and then pack that for the web as a modern WOFF2 (or older WOFF) and you're good to go. There are plenty of online tools to do that for you, but you can also just use something like the open source FontForge application if you want a font that only includes what you need, instead of what online tools foist into them.
I'm using Silverlight and need to display some OTF fonts. Now Silverlight supports OTF fonts in version 4 but it does not seem to support OTF fonts with PostScript outlines. I have some OTF fonts with postscript outlines that won't show up. Is there a (free) way of converting between OTF with postscript outlines to TrueType fonts or OTF with TrueType outlines. (Incidentally I've tried TransType but am having no joy with it).
FontForge should be capable.
open the original OTF in FontForge
then choose File > Generate Fonts
and choose TrueType (TTF) as the export type
Maybe I'm having a senior moment, but this question makes no sense to me.
An OTF Font font does not contain "PostScript outlines" or "TrueType outlines". It contains OTF data, period. Edit: this is only true from the application's point of view. As #mustISignUp points out, internally there are many possibilities.
Silverlight supports OTF and TrueType fonts (among others). Those are two independent formats. It doesn't support PostScript fonts.
Thus I assume that your real problem is: you have a PostScript font, and you need to convert it into a format that Silverlight supports.
The first question I have to ask: what font? Did you design it? Are you sure there's no TTF or OTF equivalent available?
There are many, many font conversion utilities available -- is that all you need?
Edit Okay, your comment makes it clear that the above assumptions are correct. So the questions following are still the relevant ones: what font is this? Is it an original? And also: is high quality high importance (it usually is)?
Further Edit
It turns out that there is a bug in Silverlight which is causing a problem: Silverlight and OpenType fonts with Postscript outlines - rendering problem. So we're both right: an application that supports OTF correctly transparently supports OTF-with-Postcript-Inside, but Silverlight isn't supporting it correctly so all bets are off. Alas.