MmMm SharedCopy of Facing Fonts in HTML « coding@scribd

Facing Fonts in HTML

May 17, 2010

This is the first of a four-part series on the technology behind Scribd’s HTML viewing experience. You might like to read part 2, “The Perils of Stacking” and part 3, “Repolygonizing Fonts,” if you haven’t already. Part 4, “Plan B: Font Fallbacks” is coming soon.

PDF to HTML converters have been around for ages. They take a document, analyze its structure, and produce (with the help of some images) an HTML document that has roughly the same text at roughly the same position. This is fun to play with, but not really useful for practical applications since fonts, layout and style are often very different from the original.

With the new Scribd HTML document conversion, we aimed for a higher goal: Let’s create a version of a PDF file that looks exactly the same, but is also completely valid HTML. It turns out that since all major browsers now support @font-face, this is actually possible.

Encoding documents in this way has numerous advantages: no proprietary plugins like Flash or Silverlight are required to view documents; we take full advantage of built-in browser functionality like zooming, searching, text selection, etc.; state-of-the-art embedded devices are supported out of the box; and even on older browsers it degrades gracefully (to HTML text with built-in fonts).

So let’s back up a bit and review what @font-face is:

Font face is a way of making a browser use a custom TrueType font for text on a web page. For example, this text uses the Scrivano typeface:

font-face

Try selecting this text and right-clicking to assure yourself that this is indeed just standard browser element

This is accomplished by introducing a @font-face css declaration in the page style block:

@font-face {font-family: Scrivano;
            src: url("http://someserver.com/scrivano.ttf");}

Now you can use the “Scrivano” font family as if it was a built-in font. Before we dive into how one can use this for converting documents to HTML, there’s a caveat: in order for @font-face to really work, we need to store three font files: One for Internet Explorer (.eot) one for embedded devices (.svg) and one for Firefox, Safari, Chrome et al (.ttf). Once this is done, however, @font-face works in pretty much every modern browser, including the ones that come with mobile phones (iPhone and Android). So this is how the above looks, fully-fledged:

@font-face {
  font-family: 'Scrivano';
  src: url('scrivano.eot');
  src: url("scrivano.svg") format('svg');
  src: local('\u263a'), url('scrivano.otf')
  format('truetype');
}

(For a detailed discussion about the \u263a hack in there, see Paul Irish’s bulletproof font-face syntax)

Now, let’s talk about documents again. As in most documents, text is the predominant element, making @font-face invaluable for transcoding PDF files to @font-face-enabled HTML. Of course, the fonts we need for displaying HTML text as closely to the original as possible need to be constructed from scratch: PDF documents store fonts in a myriad number of formats (Type1, Type3, OpenType etc.), none of which is directly usable as a web font. Additionally, the same font may be used in different places with different encodings or even different transforms.

I don’t want to bore anyone with the specifics of decoding arbitrary PDF font and build a TTF, EOT and SVG out of it. However, the transformed font variant is actually fun to talk about. Diagonal text in documents is surprisingly common; let’s look at, for example, this figure with diagonal axis descriptors, from a document about fractals:

How do you encode the diagonal text in this document in a HTML page?

Short of using element transformations (-moz-transform, DXImageTransform etc.) which we found to be rather impractical, we encode the above HTML with a custom font created by transforming the original font. Here’s how our generated font looks in FontForge:

From the above font screenshot you also notice that we reduce fonts to only the characters that are actually used in the document; that helps save space and network bandwidth. Usually, fonts in the pdfs are already reduced, so this is not always necessary.

Naturally, for fonts with diagonal characters every character needs to be offset to a different vertical position (we encode fonts as left-to-right). In fact, this is how other HTML converters basically work: they place every single character on the page using a div with position:absolute:

<!-- crude pdf to html conversion -->
<div style="position:absolute;top:237px;left:250px;">H</div>
<div style="position:absolute;top:237px;left:264px;">e</div>
<div style="position:absolute;top:237px;left:271px;">l</div>
<!-- etc. -->

At Scribd, we invested a lot of time in optimizing this, to the degree that we can now convert almost all documents to “nice” HTML markup. We detect character spacing, line-heights, paragraphs, justification and a lot of other attributes of the input document that can be encoded natively in the HTML. So a PDF document uploaded to Scribd may, in it’s HTML version, look like this (style attributes omitted for legibility):

Original document:

HTML version:

<p>
  <span>domain block is in a different image than the range block), as</span>
  <span>opposed to mappings which stay in the image (domain block</span>
  <span>and range block are in the same image) - also see Fig. 5.</span>
  <span>It's also possible to, instead of counting the number of</span>
</p>

Together with <img> tags for graphic elements on pages, we can now represent every PDF document in HTML while preserving fonts, layout and style, with text selectability, searchability, and making full use of the optimized rendering engines built into browsers.

Want to see it in action? Check out this technical paper, browse our featured documents or upload your own!

-Matthias Kramm

Next: The Perils of Stacking

Entry Filed under: Scribd Reader. .

14 Comments Add your own

  • 1. How Scribd’s HTML document reader works (part 1)  |  May 17, 2010 at 3:54 pm

    [...] full post on Hacker News If you enjoyed this article, please consider sharing it! Tagged with: document [...]

    Reply
  • 2. Troy Gilbert  |  May 17, 2010 at 4:10 pm

    Are there licensing issues in regards to fonts being generated from the PDF? Or is this issue avoided by the creation of the PDF in the first place?

    Reply
  • 3. Dan  |  May 17, 2010 at 4:46 pm

    Is this PDF to HTML tech going to be included in your API now or anytime down the road?

    Reply
  • 5. Matthew McEachen  |  May 17, 2010 at 10:51 pm

    The generation of rotated fonts is quite clever–thanks for sharing!

    Have you found that font kerning gets mucked up with that technique, though?

    Kudos to your adoption of html5!

    Reply
    • 6. matthiaskramm  |  May 18, 2010 at 1:21 pm

      Thanks for the positive feedback! As for kerning: we do reposition elements in the HTML if we detect kerning-caused negative character spacing (i.e. glyphs being closer together than by what would be dicated by their advance values) in the orignal document. There’s a tradeoff with regard to HTML size, though.

      Reply
  • 7. Ajaxian » Scribd: Font face trickery and more  |  May 18, 2010 at 9:10 am

    [...] Scribd is my "favourite company of the month". First they show off their move from Flash to HTML5 and now they are generously taking time to share with us details on their implementation in a three part series. [...]

    Reply
  • 8. mario  |  May 18, 2010 at 11:17 am

    The one thing I do not get about this PDF to HTML conversion is, why go to that at all? What’s wrong with just serving the PDF? It’s not like there are no PDF viewers on any platform. Evince does quite a good job, and it’s faster than the flash thingy you have on scribd. It will probably still be faster than having my future HTML5 browser load a dozen fonts and svg resources..

    Reply
  • 9. kl  |  May 18, 2010 at 1:54 pm

    Please don’t rely on proprietary webkit and moz extensions – it screws up in Opera, even though Opera *does* support CSS transformations.

    Besides, using not-yet-standard CSS transformations just for image zoom is overkill — image scaling is easy to do in plain ‘ol 1993 HTML.

    Reply
  • 10. Top Posts — WordPress.com  |  May 18, 2010 at 5:16 pm

    [...] Facing Fonts in HTML This is the first of a three-part series on the technology behind Scribd’s HTML viewing experience. Part 2, [...] [...]

    Reply
  • [...] The Small Print: Writing UI Instructions SliderNav: iPhone-Style Contact Lists Using JQuery and CSS Facing Fonts in HTML Typekit and Google Announce Open Source Collaboration Learn: Anatomy of a Typeface Introducing the [...]

    Reply
  • 12. Josh Feathwood  |  June 2, 2010 at 5:36 am

    Wow, that’s incredible. I’ve been waiting for those things for years.

    It’s a pity that it doesn’t work in Opera.

    Cheers, Josh

    Reply
  • 13. The Perils of Stacking « coding@scribd  |  June 24, 2010 at 2:52 pm

    [...] on the technology behind Scribd’s HTML viewing experience. You might like to read part 1, “Facing Fonts in HTML” and part 3, “Repolygonizing Fonts,” if you haven’t already. Part 4, “Plan B: Font [...]

    Reply
  • 14. Repolygonizing Fonts « coding@scribd  |  June 24, 2010 at 6:05 pm

    [...] on the technology behind Scribd’s HTML viewing experience. You might like to read part 1, “Facing Fonts in HTML” and part 2, “The Perils of Stacking,” if you haven’t already. Part 4, “Plan B: Font [...]

    Reply

Leave a Comment

Logged in as choonkeat. Logout.

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

May 2010
S M T W T F S
    Jun »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Most Recent Posts


Fonts by Typekit

Web annotations