GuntherRademacher / rr

RR - Railroad Diagram Generator
Apache License 2.0
467 stars 51 forks source link

How to generate font width tables? #1

Closed benesch closed 4 years ago

benesch commented 4 years ago

Hi @GuntherRademacher, thanks very much for open sourcing this! It's a huge help. If you have a minute, could you shed some light on how you generated the font width tables in Normal.java and Bold.java? (I'd like to swap them out for a different font.) Thanks very much!

GuntherRademacher commented 4 years ago

Thanks for asking, @benesch. You are right - this was missing. I have now pushed the tool that was used to generate those files, FontWidth.html.

The contract of Normal.get(i0) is to return the width required when rendering codepoint i0, with font-weight:normal, in tenths of a pixel. My approach to implementing this is:

  1. use browser to measure width for each codepoint in range 0-0x10FFFF. This is done in FontWidth.html
  2. put the result, a comma-separated list of integers, in file Normal.java.compress
  3. run 'Generate' of https://bottlecaps.de/rex on that file, without any command line options. This results in Normal.java
  4. adapt package name in Normal.java manually

The same applies to Bold.get(i0), using font-weight:bold. For best results, measure widths on multiple browsers and select the maximum measured width for each codepoint.

If you see better alternatives for static width calculation, please let me know.

benesch commented 4 years ago

Indeed, this works perfectly. Thanks very much, @GuntherRademacher!

By the way, I was impatient at first and generated a font width table with code points up until 0xFF. Uploading this width table to REx resulted in a Java file with a syntax error. I've made a gist with the problematic input and output: https://gist.github.com/benesch/7f68ea2c6be87403a217e7ebd0882142. The error is on line 15. This wasn't a problem with a larger font table, which I guess tickles a different code path in REx's code generation. Not at all problematic for us—happy to just use larger font width tables—but thought you might want to know nonetheless.

benesch commented 4 years ago

Oh, also, I wanted to mention that our fork is here: https://github.com/MaterializeInc/rr/commits/master. Besides the font changes, which are likely irrelevant to everyone else, we added some patches to extract unstyled SVGs, so we can chop them up across our documentation site to our liking.

I'm not currently planning to upstream anything, but if you see anything you'd like to incorporate, I'm happy to try to clean up the patches and send up some PRs.

GuntherRademacher commented 4 years ago

Thanks, @benesch, for reporting the badly generated file. This has been fixed in REx v5.52.

I don't currently have a usecase for unstyled SVGs, but if you want those changes integrated here, that's fine with me.

benesch commented 4 years ago

I don't currently have a usecase for unstyled SVGs, but if you want those changes integrated here, that's fine with me.

We have to maintain our own fork for several other reasons, so no need to integrate them here unless you have a use case for them.

Thanks so much for all your help with this, @GuntherRademacher! Very much appreciated.