Call for a typography Unitful package

proposal

#1

How about it? Anyone good with typography lengths? Could be nice to be able to convert and deal with em, px, pt… etc. Just checking to see if there is anyone enthusiastic about typography. Writing one of those supplemental units package for Unitful.jl is relatively easy.
Anyone?


#2

i don’t disagree, however for something more than just scaling px and pt and inches and cm, i’d recommend to read the recent https://manishearth.github.io/blog/2017/08/10/font-size-an-unexpectedly-complex-css-property/ which gives some insight into ‘em’ domain.


#3

How do you display your typography?


#4

Well, that escalated quickly… Yea, I see, so this might be impossible unless there was some kind of pt context to calculate the em in. Difficult.


#5

Good question. I mean, there are many many outputs, none of which are natively juliaian… I personally either output straight into a tex (and then, yea, I could let latex figure it all out), or markdown and then pandoc it out to whatever.


#6

Well actually with FreeType, Cairo and some additional code you could go to all standard outputs (ps/pdf/svg/png). I thinking now for some time to do something on top of FreeType to access all font metrics (for something more special than pango only) but looking at the time i spend on this …
So picking up from above, yes we could have some TypographicDimensions.jl but this will be way bigger than fixed scaling only.


#7

I was about to post about this exact same thing (i.e., the problem of mixing stuff like pt and cm with em and ex, for example). I could really use this as well, and my thinking was that this could/should be handled symbolically, as an irreducable sum of two (or more) separate things. So that 1cm + 1em was just that – and you could, for example, convert that directly to (say) TeX, which could then handle the em based on the context.

However, it seems this goes beyond the current capabilites of Unitful.jl (reasonably enough). Is it a kind of (general) mechanism that could be part of such a package, or ought it involve something else entirely (such as a full-blown symbolic computation package), or something in-between, such as a custom type representing such a sum?

Or … I guess one could define em and ex (and the like) as FixedUnits (preventing conversion, but also preventing addition with other lengths), and then just use vectors or something. Or one could define a length type with one general length field, one em field, one ex field, etc., and then define addition on them. (Seems to cry out for a simpler, more general solution, though…)

(The font metrics etc. are not really of that much interest to me at present – though it may be useful. I remember actually implementing something similar based on real font metrics in Python a couple of decades ago, but that’s more work than it’s worth for my current use, where I’ll just be shipping stuff to TeX, anyway.)


#8

I put together a quick sketch of how this might work, here:

Beyond the obvious stuff (mirroring many of the kinds of methods defined for, say, Complex), one could also have functions for specifying a conversion from em, ex and px to the absolute scale, and then resolve the sums, yielding a single, absolute length. (Or maybe go in the other direction, ending up in one of the relative dimensions, which could then later be used with other fonts/sizes.)


#9

Unless someone knowledgeable objects, I don’t see why this should not be a package…? I kind of like the idea that connecting the EM, EX, and PX lengths to the rest can be handled optionally by some other external add-on package (using Require or some similar mechanism).

By publishing this now, you can let someone else pick it up when they need to and other to use this if it suffices. Cool work!


#10

Sure thing :slight_smile: I’ve polished it a bit, but am trying to improve the typing a bit (to reduce the jit-lag for computing all kinds of type parameter combinations, etc.). And I’ve run into a snag – which I wonder if may be a bug in Unitful, perhaps. It seems you can’t promote more than three unitful quantities?

For example, the following works just fine:

julia> promote(1cm, 1.0kg, 1s)
(1.0 cm, 1.0 kg, 1.0 s)

The following, however, does not:

julia> promote(1cm, 1.0kg, 1s, 1N)
ERROR: MethodError: Cannot `convert` an object of type Tuple{Unitful.Quantity{…}} …

Thoughts? Maybe it’s just a matter of defining a conversion method for tuples of quantities, to make the built-in promotion machinery work? (Haven’t studied the issue in detail.)


#11

(oops! meant should not, got confused with the double negation: I mean this should be a package :slight_smile: )


#12

No idea, you could post an issue on Unitful.


#13

Done: https://github.com/ajkeller34/Unitful.jl/issues/119


#14

Okay, I polished the code a bit, and worked around a couple of things I had some trouble with in Unitful, and have something that seems to work okay:

The initial compile times are kind of high; I guess there might be ways to improve that. (And maybe there is some better architecture, where one could add new length-like dimensions to the mix without having to modify MixedLength.)

Anyway, I think it should be sort of usable – at least for my purposes :slight_smile:


#15

Cool!


#16

Very cool package. The name seems a bit unfortunate since it sounds (to me) like there’s a typo somewhere and I wouldn’t guess that it has anything to do with typography. Given that the explanation is:

Typographic units for Unitful.jl.

perhaps a good name would be TypographicUnits?


#17

I think it needs to have Unitful* in it, in accordance to all the other Unitful packages. So maybe UnitfulTypographics (with the plural)?


#18

Seems like a highly breakable rule :grimacing:


#19

I don’t have any strong naming preferences, really. Just modeled it on UnitfulAstro.jl (and the typo thing subsequently struck me as funny ;-), but I’d be happy to change it.


#20

Yea, I like the pun, and I guess it’s useful to know which units packages belong to which. But hey, what ever works :slight_smile: