pont-us / PuffinPlot

A program to plot and analyse palaeomagnetic data
GNU General Public License v3.0
3 stars 0 forks source link

Simplify text rendering #360

Open pont-us opened 3 years ago

pont-us commented 3 years ago

The use of AttributedCharacterIterators for text rendering on plots is a little problematic, since they don't always play nicely with vector export libraries (e.g. JFreeSVG renders them as paths). The only thing they're actually needed for is a couple of superscripts for orders of magnitude – this should be possible to fake with an affine transform on a plain drawString(String…) call, and a FontMetrics calculation to position the superscript correctly relative to the main string. As an added bonus this should finally produce correct superscripts on Mac OS X printouts!

Further: the "official" method for achieving superscript / subscript in SVG (setting baseline-shift attribute on a tspan) seems to be poorly supported – Inkscape can handle it, but it seems few browsers can, and I don't know about other graphics applications. (FreeHEP export turns Java character attribute superscript into an explicit transform and a separate text object, so works anywhere.)

I increasingly feel that the best long-term way forward for graphics export is to give up on using Graphics2D - PDF/SVG bridges (see #give-up-on-graphics2d-backends). But this item is to some extent orthogonal: getting rid of AttributedCharacterIterators will make things simpler and more reliable no matter how I'm producing the plots.

[2019-11-19 Tue] One more possibility for implementation: just use the Unicode superscript codepoints. Unicode has had these (for all digits, plus, and hyphen-minus) since v1.0.0 (1991). Caveats:

  1. Presence in Unicode does not of course guarantee presence in a given font. On a quick check of my installed fonts, older ones (e.g. the Times, Arial, and Courier New versions from (I assume) mscorefonts) often lack them, but there's no guarantee even for newer fonts. Noto Sans and Serif have the digits but lack plus and hyphen-minus – same for Ubuntu and even Myriad Pro!

  2. According to Wikipedia, the super- and subscript codepoints are often abused by font designers as numerators and denominators, so can look a bit off when actually used as originally intended.

  3. Even if screen display is fine, printing and export to SVG or PDF presents another potential problem.

All in all: I can probably only risk using Unicode superscript codepoints if I want to hardwire one or a few "safe" fonts and test them thoroughly (which I don't).