I found the bug report a little hard to understand and tried to recreate the problem. But I couldn't. I tweeted back to Pablo who recommended I experiment with Google Web Fonts, which is what he had used. And lo and behold, I discover a whole treasure of open source fonts available for commercial use and embeddable in EPUB books. I send a heads-up to Eric Hellman who is doing a similar project with funding ebooks up front so that they too can be free. The Zen of Ebook Production.
I had looked at Google Web Fonts ages ago when it was still just on the web. You had to link to them and there was the worry that if Google went down, so would your fonts. This also made them unavailable for EPUB use. No longer. Now you can download Google Web Fonts (and there are some really nice ones) and embed them in your own books. Of course, you don't have to use Google Fonts, all the info here is valid for any embedded TrueType or Opentype font. Here's what you do:
Use this code in your CSS:
@font-face {
font-family : NixieOne;
font-weight: normal;
font-style: normal;
src : url("fonts/NixieOne.ttf");
}
NixieOne is the name of the font I used. The name you use for
font-family
shouldn't matter, but I have found that sometimes it does. To be safe, double-click the downloaded .ttf or .otf file, and use the name that's listed in the menu. You don't need to install it to use it for EPUB.The
font-weight
and font-style
items are required. It seems, though I find this unclear in the CSS3 spec (info welcome), that these should not be necessary as they are the default values. However, Webkit (on which Safari and iBooks are based), does not default to them, and without them, will not allow you to apply bold or italic formatting at all. So, use them! Then in your CSS, when you want to use the font for a given selector, use
font-family
, as shown here:h1 {
font-family: NixieOne;
...
}
And now back to the bug in iBooks. It turns out that if you embed a font—seems that it happens with both TrueType and OpenType fonts—and that font is formatted in bold, then iBooks will render it incorrectly, most noticeably with thinner fonts and at larger sizes. (This is a simple, single stroke font and should not look this blurry.)
Note that it doesn't matter if you set the
font-weight: bold
yourself as in:p {
font-family: NixieOne;
font-weight: bold;
}
or if it's in the default stylesheet for the selector as in:
h1 {
font-family: NixieOne;
}
Since
h1
(h2
, h3
, etc.) are all bold by default, this text will also be rendered incorrectly:The solution is to first remove the bold:
h1 {
font-family: NixieOne;
font-weight: normal;
}
and then, if desired, create the bold with something other than
font-weight
. The folks at StackOverflow recommended -webkit-text-stroke
and text-shadow
.Here's 1px stroke:
h1 {
font-family: NixieOne;
font-weight: normal;
-webkit-text-stroke: 1px black;
}
and with 2px stroke:
h1 {
font-family: NixieOne;
font-weight: normal;
-webkit-text-stroke: 2px black;
}
and here's text-shadow with a value of 1px:
h1 {
font-family: NixieOne;
font-weight: normal;
text-shadow: 1px 1px #000;
}
I think all three are reasonable options and the choice depends on aesthetics more than anything else.
More thanks for helping figure this all out. Paul Irish for his @font-face gotchas page. Webkit for explaining -webkit-text-stroke. And finally to Sandra Williams, who sent me this page just as I was finishing this post. Someone else will have to follow through with that code—especially given the caveat about Safari at the end. Still, it looked interesting.
Hi,
ReplyDeleteIf you use embeding fonts the "all h tags" and enter the following code. Flawless.
CSS:
.head {height:25px; border-left:10px solid black; padding:5px 0 0 10px; font-size:32px;}
.serif {font-family:serif;}
.sans {font-family:sans-serif;}
@font-face {
font-family: "EmbedFontName";
src: url("fonts/embedfont.otf") format("opentype");
font-style: normal;
font-weight: normal;
}
.embed {
font-family: "EmbedFontName" !important;
font-style: normal;
}
XHTML:
<p class="head embed">Embeding font for h1 tag</p>
<p> ... </p>
DBOOKMARKS
noondb@gmail.com
@Dbookmarks : The problem occurs when bold formatting is applied. Your code doesn't have any bold, so it works fine.
ReplyDeleteIs OTF the only acceptable font format for epub files? Or its possible to include TTF, SVG and others? I've downloaded some nice fonts from here: http://www.fontsquirrel.com/ but the fonts are .EOT, .SVG, .TTF, and .WOFF. I can convert to OTF but is that necessary?
ReplyDeleteJust one other question: I have tried to embed a font in my ebook file and am failing miserably. Or at least the tools I have to view the Epub after conversion are not picking up the font. Adobe Editions, Calibre, Kindle Preview, etc are all defaulting to the second font family indicated in the CSS.
ReplyDeleteHere are the two rules I am using in the CSS:
@font-face {
font-family: 'AnastasiaRegular';
src: url("font/Anastasia.otf")format('opentype');
font-weight: normal;
font-style: normal;}
h1{
font-family: 'AnastasiaRegular', sans-serif;
font-size: 2.5em;
line-height: 1em;
font-variant: small-caps;
}
When I run the file through Epubcheck I don't get any errors. And if I preview the xhtml file in Chrome, Firefox or Safari, it displays the font correctly.
Have I missed a step somewhere?
We should spend some time talking about which of the fonts we have are also open to use legally. I have no idea which of my fonts are royalty free and which are not.
ReplyDeleteAlthough you embed TTFs in this article, they're not working for me:
ReplyDeleteI also loaded some Google Webfonts and embedded them as described above. But they are not working in iBooks. The only fonts working are OTFs. How can I convert the Webfonts to OTF? Or is there a workaround?
Hey! I wanted to thank you--I've struggled with getting a proprietary TTF font to display in my ebooks (it's the one we use for our print editions). The note about checking the name of the font in the pop-up box in FontBook was a huge help; it turns out that the font name and the name of the file were not the same! Fixed a problem I've been pulling my hair out about for almost a year!
ReplyDelete@David: Glad that helped. The name really shouldn't matter, but I have found that indeed, it does some times. I'm not sure why.
ReplyDeleteHi Liz
ReplyDeleteI have a small font issue and I'm hoping you are someone in your following may be able to help with:
I have been working on epubs for the past six months.
I have a set of epub training guides I've working on since November. I embed the font, ScalaSansOT. I have yet to have a problem. it has embedded perfectly.
Recently I have been working on the next few books in the series and a few past ones and it refuses to embed the font properly. I have done everything. It just will not embed.
Please help, I'm at my wits end with this.
Thanks so much
Donna
Hmm. Not sure what it could be. I would try regular troubleshooting techniques (maybe some ideas here: http://www.pigsgourdsandwikis.com/2011/02/troubleshooting-epub-ebook-files.html) check for typos, check the name, try with a different font, see if it works in Safari, etc. etc.
DeleteHowdy from across the river, Liz!
ReplyDeleteHey, I'm confused!! On Apple's site, there's all kinds of "it's impossible to embed fonts in an iBooks Author file" posts. Yet y'all here are saying it's indeed possible. Are all the other posts just plain wrong?
Thanks much,
Confused in Hadley
Embedding fonts in EPUB files to be read on iBooks is what's discussed here (and possible). I have heard, like you, that you cannot embed fonts in iBooks Author books, which are NOT EPUB files, though also viewed with iBooks. But I haven't tested it myself.
DeleteLotsa snow on this side :)
Thanks, Liz, for posting this. I'm new to e-books, so I appreciate you sharing all this!
ReplyDeleteThank you for this! I could not figure out what the heck was wrong with my code until I crossed my fingers and typed "font shadow error in ibooks" in Google. I did not expect to find a solution in 5 minutes thanks to you.
ReplyDeleteDefinitely major thanks for posting this. Same as Ryan, I couldn't figure out why my embedded font was "doubling up" on iBooks, but not on any of the other devices (Nook, Kindle Fire, etc.).
ReplyDeleteThanks for this. The font-weight solution fixed a problem I'd been having.
ReplyDeleteLiz, you're amazing. Just wanted to add, after much pulling of hair I realized that:
ReplyDeleteprefix="ibooks: http://vocabulary.itunes.apple.com/rdf/ibooks/vocabulary-extensions-1.0/"
needs to be in the package section of the .opf for iBooks to respect the meta property:
meta property="ibooks:specified-fonts"
Which allows font embedding... Going through epubcheck and solving all of the errors will lead one to fix that, but thought it might be faster to spell it out.
I have an open type font that is licensed for e-pub, called Crimson. There is both an open type version for print and one for web. I assume that the one for web could also be used with the @font-face for e-pub. Is that correct?
ReplyDeleteI would read the licensing information carefully.
Delete