Skip to content

Try different SDF granularities #38

Closed

Description

@yhahn

We can get better compression ratios by reducing the granularity of the SDFs -- the following commit rounds each value to the nearest multiple of 8:

c213c12

Visually it's hard to see much difference once rendered and here is the space saving for a 0-255 range:

master 43124 bytes
compress-more 23208 bytes

This is a nice boost we can get now roughly for free since it doesn't affect the format/integrating projects -- later we may try other encodings for the bitmap (PNG, etc.) but those will be format-breaking.

Next actions

  • Try this and other settings (4 and 2? 16 is where I started to see artifacts...) with a variety of different fonts
  • This could be variable/env based so if you want a very high quality SDF you can set your settings that way

Activity

  1. self-assigned this
    on Jun 18, 2014
  2. mikemorris commented on Jul 1, 2014

    @mikemorris
    Contributor
    granularity bytes image
    1 43124 1
    2 36195 2
    4 29376 4
    8 23208 8
    16 18583 16
    32 14703 32
  3. mourner commented on Jul 1, 2014

    @mourner
    Member

    8 looks great to me

  4. mikemorris commented on Jul 1, 2014

    @mikemorris
    Contributor

    This should likely be configurable by font. Simple serif and sans-serif fonts render quite cleanly with significantly reduced granularity, but curvy, swooping fonts like Mister K Pro Regular degrade in quality more quickly.

    granularity bytes image
    1 34290 1
    2 28780 2
    4 23552 4
    8 18864 8

    I may have the style mixed up a bit through, as I was trying to render Mister K at a larger size and even the rendering without reduced granularity appears much lower quality than @amyleew's. Is the screenshot below from a Mapnik rendering or GL? Could this particular font simply be ill-suited for SDF rendering?

  5. mikemorris commented on Jul 1, 2014

    @mikemorris
    Contributor

    Mister K Onstage Pro Regular

    Looks like 48px SDFs with granularity 4 is the sweet spot for this font, granularity 8 looks a bit lighter than full quality. The 24px SDFs (even at granularity 1) are woefully inadequate for this font.

    sdf size granularity bytes image
    48px 1 76339 1
    48px 2 63117 2
    48px 4 49998 4
    48px 8 39182 8
    24px 1 34290 24px
  6. amyleew commented on Jul 1, 2014

    @amyleew

    @mikemorris Was testing the FF Font - Mister K. Screenshot directly from TM2.

  7. mikemorris commented on Jul 2, 2014

    @mikemorris
    Contributor

    So 48px SDFs at granularity 4 look great for rendering curvy fonts like Mister K (even at large font sizes like 72px), but unecessarily bloat the SDFs for simpler fonts like Open Sans Regular at more typical font sizes.

    Open Sans Regular

    size granularity bytes
    24px 8 23301
    24px 1 43274
    48px 4 63604

    Maybe this calls for investigating 2-channel signed distance fields?

    /cc @yhahn @kkaefer

  8. kkaefer commented on Jul 3, 2014

    @kkaefer
    Member

    Interesting results! We should definitely explore more here and make the font size configurable (currently it is hard coded to 24px). We could also explore not computing the SDF on the server, but on the client, and just send the antialiased glyph to the client. Similarly, another paper describes how to use the gradients in the distance field to compute sharper outlines and corners.

  9. mikemorris commented on Oct 3, 2016

    @mikemorris
    Contributor

    You may find this interesting @rasagy, refs mapbox/mapbox-gl-js#3286

  10. removed their assignment
    on Oct 19, 2016
  11. initialdmg commented on Aug 28, 2019

    @initialdmg

    I need to try a 48px CJK font, where to change the granularity param now? If distmap.c was moved into sdf-glyph-foundry? I just found the font scale ratio param , while i'm not sure if it is for the font size.

    Btw, no need to modify mapbox-gl-js to use generated 48px glyphs, right?

  12. mikemorris commented on Aug 28, 2019

    @mikemorris
    Contributor

    IIRC the font size you're looking for is hardcoded as 24 at

    double size = 24 * scale_factor;

    It looks like it's also passed in to sdf_glyph_foundry hardcoded separately a few lines below at

    sdf_glyph_foundry::RenderSDF(glyph, 24, 3, 0.25, ft_face);

    This should likely be made consistent to use the same variable instead of hardcoding values twice, and possibly made more modular, as the file size/clarity tradeoff can be different for more complex font styles.

    Btw, no need to modify mapbox-gl-js to use generated 48px glyphs, right?

    I don't remember, there may be some logic in there expecting glyphs of a certain size, or it could be reading those metrics from the protobufs. If this was made modular, would need to ensure the frontend rendering displayed glyphs as expected independent of node-fontnik render params.

  13. jcary741 commented on Oct 13, 2026

    @jcary741

    Closing as stale.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions