Embedding fonts in EPUB using Scrivener + Sigil

Hi all. Sorry for the lengthy post, but for people who have been struggling to figure out how to get their fancy fonts to show up in EPUB format, these instructions might prove rather useful. I’m all ears if someone knows of a simpler way!

And please, do use these instructions for good, not evil. Make sure all your fonts are public domain, free and/or licensed, and please refrain from using Comic Sans as body text.

Also note that there may be certain caveats depending on the fonts you use. For example, a block of text with an em dash may appear in Scrivener to be all in FancyFont, but if FancyFont does not in fact include an em dash character, these instructions will fail on that block of text. Close inspection of the em dash in Scrivener will reveal that it’s really formatted in the default system font, since it can’t be rendered in FancyFont. The best solution for this problem is to simply choose fonts that include all the characters you need.

———
PREPARING FONTS
———

If you haven’t already done so, the first thing you’ll need to do is prepare your fonts.

  1. Using Mac Finder or Windows Explorer, create a folder for storing all fonts and files relating to these instructions. For future reference, you might want to place a copy of these instructions in the same folder, like in a README.txt file.

  2. Create OpenType versions of every font used in your project. You can do this online using a service like freefontconverter.com/. As a rule, give each font a name using letters, numbers and underscores only!

  3. Create a file named “fonts.css”.

  4. For the main body font in your project, add the following code to your “fonts.css” file:

    @font-face {
    font-family: foobar;
    font-weight: normal;
    font-style: normal;
    src: url(…/Fonts/Foo_Bar.otf);
    }
    body {
    font-family: ‘foobar’, serif;
    }

    The above code assumes you have an OpenType font named “Foo_Bar.otf”,
    which you are referencing with the name “foobar”. Adjust these values
    as desired, but ensure all such names consist only of letters, numbers
    and underscores.

    Also, if you use a certain line spacing for your body text that does
    not seem to translate to your EPUB, try using this code for the body
    definition…

    body {
    font-family: ‘foobar’, serif; line-height: 1.4em;
    }

    …where “1.4” corresponds to the value in the line height selector in
    Scrivener.

  5. For every other font in your project, add the following code to your “fonts.css” file.

    @font-face {
    font-family: snafoo;
    font-weight: normal;
    font-style: normal;
    src: url(…/Fonts/Sna_Foo.otf);
    }
    .snafoo {
    font-family: ‘snafoo’, serif;
    }

    The above code assumes you have an OpenType font named “Sna_Foo.otf”,
    which you are referencing with the name “snafoo”.

———
TAGGING YOUR MANUSCRIPT
——-

Now in your Scrivener project, you need to wrap every instance of nonstandard fonts in special tags. By convention, these tags are denoted with colons and should appear like this in your manuscript:

The quick brown fox :snafoo:jumped over:: the lazy dog.

In this example, the words “jumped over” are to be presented in the “Sna_Foo.otf” font. The opening :snafoo: tag says “start using Sna_Foo.otf here” and the closing :: tag says “stop using Sna_Foo.otf here”.

Note that the tags must be rendered in the font you are specifying. So in the above example, :snafoo:jumped over:: should all be rendered in the “snafoo” font in Scrivener.

It’s important that the name you reference for the font matches up to the name you’re using in the “fonts.css” file. In this example, we must use :snafoo:, not :Sna_Foo.otf: or :sna_foo: or :SnaFoo:.

It’s also important to note that these tags must be included in every paragraph that includes a nonstandard font. For example, if you have a group of 3 consecutive paragraphs that all use the “snafoo” font, you can’t simply include the :snafoo: tag at the start of paragraph 1 and the :: tag at the end of paragraph 3. You must wrap the beginning and end of all 3 paragraphs in these tags!

Also note that if a closing tag follows a blurb that ends in a colon, you’ll need to separate the three colons with a space. And if your manuscript already makes use of double colons, you might need to use a different tag convention to avoid conflicts with your text. In this case, you might be able to just use a triple colon to denote the closing font tag.

—–
PREPARING THE EPUB FILE

Once you have your project properly tagged, you can compile an EPUB in Scrivener at any time and use the steps below to get your fonts to show up. You’ll need a copy of Sigil, which is available at code.google.com/p/sigil/, and you’ll first need to complete the instructions above, if you haven’t already.

  1. Open the EPUB in Sigil.

  2. In Sigil’s “Book Browser” pane, right-click on the “Fonts” folder, and choose “Add Existing Files…”.

  3. Navigate to the folder with all your OpenType fonts, highlight everything in the folder (including the “fonts.css” file) and click “Open”. All fonts should now appear in the “Fonts” folder, and the “fonts.css” file should appear in the “Styles” folder.

  4. Now add a line to every XHTML file in the “Text” folder to properly reference your “fonts.css” file. You can use a find/replace to do this with a single click.

  • From the main menu, select Edit > Find/Replace > Find/Replace

  • Use the following settings:
    Find:
    Replace:
    Mode: Normal
    Look: All HTML Files

  • Click the “Replace All” button

  1. Now replace all your special font tags with the proper HTML code for applying each font.
  • From the main menu, select Edit > Find/Replace > Find/Replace

  • Use the following settings:
    Find: :([A-Za-z0-9_]+):(.*)::
    Replace: \2
    Mode: Regex
    Look: All HTML Files

    Note: If you’re using the triple-colon closing tag convention
    (see TAGGING YOUR MANUSCRIPT above), you’ll have to enter this
    into the “Find” box instead: :([A-Za-z0-9_]+):(.*):::

  • Click the “Replace All” button

  1. Save the file, then from the main menu, select View > Book View, and check out your fonts!

—–
FILTER TAGS OUT OF OTHER COMPILE MODES

You’ll probably want to configure other Scrivener compile modes to filter out all your special EPUB font tags. To do so…

  1. Click to compile your draft, choose the format you want, and click “Replacements” (under the “Compilation Options” pane).

  2. Click “Preset Replacements”.

  3. For every font you’re using, click the + button to add a new rule, and…

  • In the “Replace” box, enter the font’s opening tag, e.g. :snafoo:

  • Leave all the other stuff blank

  1. Click the + button to add one more rule…
  • In the “Replace” box, enter ::

  • Leave all the other stuff blank

  1. Save the compile format (click the “Format As” selector, choose “Manage Compile Format Presets”, ensure the correct preset is selected, and click the “Update” button).