I need to use #font-face feature and my fonts are in TrueType (TTF) format, so how to convert TTF to OpenType (OTF) format.
If you are on Linux, you can use FontForge, which can be scripted from Python.
#!/usr/bin/python
import fontforge
font = fontforge.open("STIXGeneral.otf")
font.generate("STIXGeneral.ttf")
Here is a longer python script that does this for a whole directory at a time:
http://fonts.fileformat.info/bin/otf2ttf.py
It was painfully hard to find how to do it correctly. Here is how I got it to work on OS X
$ brew install fontforge
$ fontforge -c 'Open("my.ttf"); Generate("my.otf")'
I was desperately looking for pip install fontforge which does not exist and I haven't got it to work with python - I guess you need to compile it with --enable-pyextension or something.
you can use TTF file format directly in css :
#font-face {
font-family: Vinegar;
src: url(http://www.4bit.co.uk/testing/design01/vinegar.ttf);
}
h3 {
font-family: Vinegar, "Times New Roman", Times, serif;
}
It is working!
Or you can use this link to generate your font face!
A quick Google search for ttf otf converter gave me a number of results, such as:
https://onlinefontconverter.com
http://www.freefontconverter.com
http://www.font2web.com
No idea how well they work, but you can try them.
For cross browser/mobile support you definitely need at least three formats:
Embedded OpenType: eot for Internet Explorer 6-8.
There is a command line converter: http://code.google.com/p/ttf2eot/
Web Open Font Format: woff the W3C recommendation for webfonts: http://www.w3.org/TR/WOFF/
A converter can be fond here: http://people.mozilla.org/~jkew/woff/
and TrueType: ttf for Safari and Opera
(You could add Scalable Vector Graphics: svg for older iOS support…)
The bulletproof #font-face Syntax is this:
#font-face {
font-family: 'Vinegar';
src: url('vinegar.eot?') format('embedded-opentype'),
url('vinegar.woff') format('woff'),
url('vinegar.ttf') format('truetype'),
url('vinegar.svg#svgVinegar') format('svg');
}
Further resources:
http://www.paulirish.com/2009/bulletproof-font-face-implementation-syntax/
http://www.fontspring.com/blog/the-new-bulletproof-font-face-syntax
You might also want to check out this tool:
https://github.com/zoltan-dulac/css3FontConverter
As mentionned by others, fontforge scripting has switched to python.
I found the easiest way was to invoke python from the command line.
I could convert multiple ttf fonts to otf on Arch Linux this way, but it should work on other distros by installing fontforge with your favorite package manager.
[user#host]$ sudo pacman -S fontforge
[user#host]$ cd /path/to/your/fonts/folder
[user#host]$ python
>>> import fontforge
>>> import os
>>> fonts = [f for f in os.listdir('.') if f.endswith('.ttf')]
>>> for font in fonts:
... f = fontforge.open(font)
... f.generate(font[:-3] + 'otf') # changes extension from ttf to otf
...
>>> exit()
With the Font Squirrel #font-face generator.
You could also try this:
http://www.freefontconverter.com/
Related
I'm using CSS, the font file .ttf, and the font "Dirty Headline".
my code looks like this:
#font-face {
font-family: 'Dirty Headline';
src: url("./fonts/Dirty Headline.ttf") format('truetype');
}
The Problem
On Chrome on Linux, the font works fine. However, on Mac on Chrome, the font doesn't work.
On Windows on firefox, it works fine, however on Linux on firefox it doesn't.
Why does my font not work on some browsers and operating systems? Also, How can I fix this?
***** Edit *****
I changed my code to
#font-face {
font-family: "Dirty Headline";
src: url("./fonts/Dirty Headline.ttf") format("truetype"),
url("./fonts/Dirty Headline.woff") format("woff"),
url("./fonts/Dirty Headline.woff2") format("woff2"),
url("./fonts/Dirty Headline.otf") format("opentype"),
url("./fonts/Dirty Headline.eot") format("embedded-opentype");
}
still the same result, I'm unsure how to debug this
Font formats varies depending on browser and OS ; your can create a set of files using tools like fontsquirrel (assuming of course you have the correct license for this use).
Plus formats like woff and woff2 are far lighter than ttf.
Web Open Font Format (WOFF) developed in 2009 as a wrapper format for TrueType and OpenType is supported by all modern browsers! Try converting the font to this format and use it!
I am trying to use a custom font, which is not available via Google Fonts. I unzipped the font and put it into src/assets/fonts of my React project.
In my index.css I am loading the font like:
#font-face {
font-family: 'LemonMilk';
src: local('LemonMilk'), url(./assets/fonts/LemonMilk.otf) format('otf');
}
I tested the app both on the localhost and hosted it on the web for testing and it is working fine, since the font is installed locally on my machine, but if I open the app on a different machine it is not working. I also deleted the font from my machine and the fallback font started to show since then. What am I missing ? Thank you
It's looking like Your browser has a problem with understanding the font file. Try to make the webfont package from the font file. There is lot of tools to approach it:
https://www.fontsquirrel.com/tools/webfont-generator
Webfont package will contain the font in all major supported formats and will generate the css #font directive for You.
DISCLAIMER: Ensure that You have the rights to use the font.
well i suggest creating a different css file for your fonts then import it to your main css also this site generate font-face for your font with css file!
The correct font name is Lemon/Milk and you'll need to convert it to other format like WOFF WOFF2 as well.
Please Follow this:
Go the this Website free online font generator
Upload your font and check TTF, EOT, WOFF, WOFF2 (See browser support)
Click Convert > Download
Upload the newly converted fonts to your server
Finally your CSS should look like similar to this.
#font-face {
font-family: 'Lemon/Milk';
src: url('LemonMilkbold.eot');
src: url('LemonMilkbold.eot?#iefix') format('embedded-opentype'),
url('LemonMilkbold.woff2') format('woff2'),
url('LemonMilkbold.woff') format('woff'),
url('LemonMilkbold.ttf') format('truetype');
font-weight: bold;
font-style: normal;
}
Please Note that you'll need to convert any other font weight too. and don't forget to add the correct path
I want to defer font loading on my site inspired by deferred font loading logic for Smashing Magazine.
Main part of this is converting fonts to base64 and preparing your CSS file. My steps so far:
Pick fonts on Google Web Fonts and download them.
Use Font Squirrel Webfont Generator to convert downloaded TTF files to CSS file with base64 embedded WOFF fonts (Expert options -> CSS -> Base64 Encode).
Load CSS file async (not important here).
CSS snippet for Open Sans Bold:
#font-face {
font-family: 'Open Sans';
src: url(data:application/x-font-woff;charset=utf-8;base64,<base64_encoded>) format('woff');
font-weight: 700;
font-style: normal;
}
The problem is, that converted fonts look a lot different. Take a look at Open Sans Bold:
Especially notice accents being way off and absolutely horrible letter a. Other font families and variants look very noticeably different as well (size and shape distortions, etc.).
So the question is: How do you properly encode TTF files from Google Web Fonts (or other source) to base64 format and use it in a way that the result is identical to the original file?
In the Font Squirrel Expert options, make sure to set the 'TrueType Hinting' option to 'Keep Existing'. Either of the other options will cause the TrueType instructions (hints) to be modified, which will in turn affect the rendering of the font.
Alternatively, if you're happy with the rendering of the font directly from GWF, you can just take that file and do the base64 encoding yourself. In OS X or Linux, use the built-in base64 command in Terminal/shell:
$ base64 myfont.ttf > fontbase64.txt
For Windows, you'll need to download a program to encode in base64 (there are several free/Open Source tools available). Copy the contents of that file, then use in your CSS as:
#font-face {
font-family: 'myfont';
src: url(data:font/truetype;charset=utf-8;base64,<<copied base64 string>>) format('truetype');
font-weight: normal;
font-style: normal;
}
(Note that you may need to make some adjustments to the various #font-face info to match your particular font data; this is just an example template)
Use this code snippet to base64 encode your font directly in the browser (OS independent, no need to install anything)
function base64convert (files) {
console.clear()
const reader = new FileReader()
reader.onload = (e) => {
console.log(e.target.result)
}
reader.readAsDataURL(files[0])
}
<input type="file" onchange="base64convert(this.files)">
Then copy the output and paste it into your CSS:
#font-face {
font-family: 'myfont';
src: url("<<copied base64 string>>");
}
A much simpler way to get the base64 code for Google Fonts is to use the following tool:
https://amio.github.io/embedded-google-fonts/
input the URL to your font and you get the base64 code back straight away :)
I've seen that some Google Webfonts now use WOFF 2.0. Can I somehow convert my existing WOFF-fonts to this new (and supposedly better format)? And how?
Certainly! You can convert the existing woff files to the new format. You will get more than 30% improvement on the compression ratio.
Even though browser support is limited at this moment
http://caniuse.com/#feat=woff2
It will eventually change. You should use the #font-face fall back and start putting these kind of directives in your css now.
#font-face {
font-family: MyFont;
src:
url('font.woff2') format('woff2'),
url('font.woff') format('woff');
}
You can use the following tools to convert from ttf/woff to woff2.0
http://everythingfonts.com/woff-to-woff2
https://github.com/google/woff2
I could't find any WOFF to WOFF2 converters, but you can easy convert your current WOFF font to TTF with any service like this. And then using this WOFF2 cheat sheet, convert TTF to WOFF2.
I tested those steps with my fonts, converted font WOFF -> TTF -> WOFF2 works fine.
I managed to convert with
https://everythingfonts.com/woff-to-woff2
did it for my fontawseome icons
Font Squirrel generator now includes WOFF2 in it's generated fonts. So you can create it that way from your TTF file.
On Ubuntu, you can convert WOFF to OTF/TTF and then to WOFF2:
woff2sfnt myfont.woff > myfont.ttf
woff2_compress myfont.ttf
Package dependencies:
sudo apt install woff-tools woff2
I have some web applications that follow metro style of the Microsoft (ie.: new outlook).
But I'm having troubles with the fonts that I used.
The default font is "Segoe" family, when an user enter in the application in a system that have the desktop font Segoe UI, everything is alright. But in some cases users are using Mac or Ubuntu that don't come with the "Segoe UI" and the navigator uses the secundary font (in my case, Tahoma).
In the new Outlook don't matter what OS you are using, it always uses Segoe UI family (I think that they are using web fonts)
Some people spoke to me use web fonts, but I didn't find in nowhere (I searched in alot of web fonts sites) web fonts of Segoe family.
Does someone have any idea how I solve this situation?
First of all there are difrent types of fonts for every browser. To make sure that it will work in every browser You need to put at least 3 types: eot, ttf, woff (and svg). The best way to get those is to use one of the links: http://www.fontsquirrel.com/fontface/generator, http://fontface.codeandmore.com/
It's very simple and You will get a set of ready to use fonts. After downloading Your set You will find the example file where You will see how to use Your new fonts.
In Your case it can be like:
#font-face {
font-family: 'Segoe';
src: url('/font_path/Segoe.eot');
src: url('/font_path/Segoe.eot?#iefix') format('embedded-opentype'),
url('/font_path/Segoe.woff') format('woff'),
url('/font_path/Segoe.ttf') format('truetype'),
url('/font_path/Segoe.svg#Segoe') format('svg');
font-weight: normal;
font-style: normal;
}
/font_path/ is the relative path to your fonts according to this css file. Usually it's ../fonts/.
Why You need all those?
ttf, otf - for: FireFox, Chrome < 6, Safari and Opera
oet - for: Internet Explorer < 9
svg - for: Safari Mobile (iPhone, iPad for iOS < 4.2), Android browser
woff - for: Internet Explorer >= 9, FireFox >= 3.6, Chrome >= 6
Segoe.eot - and others are links (relative in this case) to those font files.
EDIT
Because fontsquirrel.com don't render some fonts, andfontface.codeandmore.com have changed to commercial sometimes You will have to google for some other online font generator.
EDIT
If fontsquirrel.com won't help You try to use: http://www.font2web.com/
Use CSS to specify a location where the font can be downloaded if it is not available in the OS. Add this to the start of your CSS file:
#font-face {
font-family: Segoe;
src: url('/fonts/segoe.ttf');
}
The ttf format works for most browsers. For IE, add the location of an eot version to your conditional IE stylesheet:
#font-face {
font-family: Segoe;
src: url('/fonts/segoe.eot');
}
Our users facing the same issue with Segoe. Solution is to remove the font foreign language in TTF Names using Fontforge
http://fontface.codeandmore.com/blog/ie-7-8-error-with-eot-css3111/