[css3-fonts] subscripts and superscripts

The following proposal was whiteboarded by dbaron and fantasai.
It solves lots of funky use cases.
It does not solve fonts with bad font metrics. :)

Definitions:

   *magic*: none | super | sub
   inherited: yes
   applies to: non-replaced inline elements

   superscript size ratio
      The font-size ratio of superscripts to base text, as defined
      by the font metrics (or some heuristic).
   subscript size ratio
      The font-size ratio of subscripts to base text, as defined
      by the font metrics (or some heuristic).
   superscript shift ratio
      The baseline shift associated with superscripting,
      i.e. the distance from the alphabetic to superscript
      baselines in the baseline table.
   subscript shift ratio
      The baseline shift associated with subscripting,
      i.e. the distance from the alphabetic to subscript
      baselines in the baseline table.

Behavior:

   If the value of 'magic' matches the value of 'vertical-align',
   then the computed font size is set by multiplying the
   superscript/subscript proportion (as appropriate) by the
   parent's font size. The specified font-size is ignored.

   The element's baseline is positioned per vertical-align using
   the appropriate shift value (the ratio multiplied by the computed
   font size).

   When drawing text for an inline with 'magic' other than 'none',
   if the appropriate superscript/subscript glyph is available in
   the font, then instead of drawing the usual glyph at the current
   font-size and baseline position,
     1. divide the computed font-size by the superscript/subscript
        proportion to get the appropriate "base font size"
     2. multiply the superscript/subscript shift ratio by the base
        font size and shift the baseline downward by that amount to
        get the "base baseline position"
     3. draw the superscript/subscript using the base font size and
        baseline position

Cases handled:
   - basic superscripts and subscripts, using special glyphs
     in the fonts when available, else synthesizing
   - superscripts and subscripts containing images or other
     atomic inlines
   - nested superscripts and subscripts
   - font size changes within a superscript or subscript
   - mixing any of the above

Cases not handled:
   - mixing special glyphs with anything else if the font's
     subscript or superscript metrics are inaccurate
   - combining magic with length or percentage values of
     'vertical-align'

Alternate #1:
   Instead of ignoring the specified font size in the first paragraph
   and using the parent's computed size, use the element's own font
   size.
     Advantages:
       * author can change the font size of the superscript/
         subscript without nesting elements
     Disadvantages:
       * not backwards compatible with the tradition of
         "vertical-align: super; font-size: smaller" to get superscripts,
         so the fallback behavior would have to be specified as simply
         "vertical-align; super".

Alternate #2:
   Instead of keying off vertical-align, make the property entirely
   separate and non-inherited. In the drawing text phase, this means
   that when 'magic' is 'none', we have to look up the ancestor chain
   (instead of relying on an inherited property value) to check whether
   we need to draw special glyphs.
     Advantages:
       * author only needs to set one declaration
     Disadvantages:
       * style sheets won't be backwards compatible if author doesn't
         set 'vertical-align
       * still need to set both properties to definitely turn things off

Alternate #3:
   Make 'magic' an on/off property, and look up the ancestor chain for
   'vertical-align: sub/super' to determine whether and what kind of
   special glyphs to use.
     Advantages:
       * Setting "magic: on" on the root element makes everything magically
         work
     Disadvantages:
       * Lots of ancestor chain walking?

dbaron notes that the ancestor chain walking can be optimized by setting
bitflags, (almost as if there were an inherited property, even though
there's not).

~fantasai

Received on Wednesday, 13 April 2011 00:44:28 UTC