On Tue, Oct 16, 2018 at 7:48 PM Pablo Rodriguez <oinos@gmx.es> wrote:

I tried to experiment (modifying TeX Gyre Pagella from 1000 to 9999
UPM), but for some reason, I wasn’t able to load the font.

hm. 
Weird.

Assuming  ttx from fontools
$> ttx texgyrepagella-regular.otf
$> cp texgyrepagella-regular.ttx texgyrepagella-regular-500.ttx 
$> cp texgyrepagella-regular.ttx texgyrepagella-regular-2000.ttx 

Then we can edit  the unitsPerEm tag:

texgyrepagella-regular-2000.ttx:    <unitsPerEm value="2000"/>
texgyrepagella-regular-500.ttx:    <unitsPerEm value="500"/>

Also, better to  edit the names too:

texgyrepagella-regular-2000.ttx:    <CFFFont name="TeXGyrePagella-Regular2000">
texgyrepagella-regular-2000.ttx:      <FullName value="TeXGyrePagella-Regular2000"/>
texgyrepagella-regular-2000.ttx:      <FamilyName value="TeXGyrePagella"/>

texgyrepagella-regular-500.ttx:    <CFFFont name="TeXGyrePagella-Regular500">
texgyrepagella-regular-500.ttx:      <FullName value="TeXGyrePagella-Regular500"/>
texgyrepagella-regular-500.ttx:      <FamilyName value="TeXGyrePagella"/>

texgyrepagella-regular.ttx:    <CFFFont name="TeXGyrePagella-Regular">
texgyrepagella-regular.ttx:      <FullName value="TeXGyrePagella-Regular"/>
texgyrepagella-regular.ttx:      <FamilyName value="TeXGyrePagella"/>

Then recompile the font
$>ttx texgyrepagella-regular-500.ttx 
$>ttx texgyrepagella-regular-2000.ttx 

and the following examples with context:

%%% test-500-1000-2000.tex
\nopdfcompression
\usemodule[fnt-10]
\starttext

{\tfd file:texgyrepagella-regular-500.otf}
\ShowCompleteFont{file:texgyrepagella-regular-500.otf}{10pt}{1}

{\tfd file:texgyrepagella-regular.otf}
\ShowCompleteFont{file:texgyrepagella-regular.otf}{10pt}{1}

{\tfd file:texgyrepagella-regular-2000.otf}
\ShowCompleteFont{file:texgyrepagella-regular-2000.otf}{10pt}{1}

\stoptext


%%% test-hello-500-1000-2000.tex
\nopdfcompression
\definefont[TestD][file:texgyrepagella-regular-500.otf at 10bp]
\definefont[Test][file:texgyrepagella-regular.otf at 10bp]
\definefont[TestMM][file:texgyrepagella-regular-2000.otf at 10bp]
\starttext
\startTEXpage
{\TestD Hello 500}
{\Test Hello 1000}
{\TestMM Hello 2000}
\stopTEXpage
\stoptext

(yes, we have "10pt" vs "10bp") 

You should see that the texgyrepagella-regular-500.otf is bigger than texgyrepagella-regular.otf
and  the texgyrepagella-regular-2000.otf is smaller than texgyrepagella-regular.otf
This is ok: texgyrepagella-regular has     unitsPerEm value="1000"
so changing *only* the unitsPerEm has an effect of scaling.

Consider the "M" :
$ grep 'name="M"' *ttx|grep mtx
texgyrepagella-regular-2000.ttx:    <mtx name="M" width="946" lsb="16"/>
texgyrepagella-regular-500.ttx:    <mtx name="M" width="946" lsb="16"/>
texgyrepagella-regular.ttx:    <mtx name="M" width="946" lsb="16"/>

where "width" is the "advance width"  in  "font design units" 
(it is the  """  distance the current text position shall move (by  translating text space) when the glyph is painted. """ , see the pdf reference).

So in texgyrepagella-regular.ttx, M has a "width" of  946  on  1000  units,  
in texgyrepagella-regular-500.ttx, M has a "width" of  946 on  500   that looks  like 2x the width of M in regular
in texgyrepagella-regular-2000.ttx, M has a "width" of  946 on  2000  that looks  like  0.5x the width of M in regular
--- under the assumption that we have changes *only* the value of unitsPerEm.

unitsPerEm is a number that says that, well, "1em is made by <unitsPerEm> values":
it is a dimensionless unit that  defines a "glyph space of (x,y) real  coordinates " ( not arbitrarily, there are implementation limits to respect ).
So, in principle we can say "Let's  start to design a font in a grid 1000x1000 of font design units" as well
"Let's start to design a font in a grid of 2048x2048 units".
In any case, at some point we have to translate these numbers from the dimensionless "glyph space" to a dimensional "text space"
and this is done by the FontMatrix --- another set of numbers that we  can choose arbitrarily, at least apparently.

Given an  unitsPerEm=1000, if we choose a FontMatrix =  [ 0.001 0 0 0.001 0 0 ]
then, for both horizontal and vertical dimensions, 1000 units in a "glyph space" are 1000*0.001 = 1 Postscript point  in the "text space":
i.e 1000 units => 1 Postscript point = 1/72 inch ~ 0.0003528 m (1 Postscript point is 1bp in TeX and it's not exactly the same of 1pt). 

When an application loads a font to typeset some text
it has to specify the size of the glyph in a dimensional unit , because in the end everything must be rasterized on a  physical device
-- screen or  paper -- to be viewed .
In TeX, by saying "at 10pt"  we mean "for this font, multiply every dimension in "text space" by 9.9626401"
(10pt are 9.9626401bp , ie 9.9626401 Postscript points).
The font designer should then explicitly says (in some doc) something like "this font is for reading at 10pt"
or "this is for title at 18pt" or something  similar (of course he  means Postscript points, not TeX points) 
and from here we can have an idea of "design size" of the font.
So, if we have 1000 unitsPerEm, 
a FontMatrix = [ 0.001 0 0 0.001 0 0 ],
and we choose 10bp for the actual text, 
1000 units are 10bp, or 1em = 10bp

In the previous experiments we have left unchanged the FontMatrix:
texgyrepagella-regular-2000.ttx:      <FontMatrix value="0.001 0 0 0.001 0 0"/>
texgyrepagella-regular-500.ttx:      <FontMatrix value="0.001 0 0 0.001 0 0"/>
texgyrepagella-regular.ttx:      <FontMatrix value="0.001 0 0 0.001 0 0"/>
as well as all the other dimensions. 
In texgyrepagella-regular-500.ttx we have 500 unitsPerEm and hence 
500 units in a "glyph space" that should be  500*0.001 = 0.5 Postscript points in the "text space".
But if we read the pdf specification , we see this:

"""
The glyph coordinate system is the space in which an individual character’s glyph is defined. All path
coordinates and metrics shall be interpreted in glyph space. For all font types except Type 3, the units of glyph
space are one-thousandth of a unit of text space; for a Type 3 font, the transformation from glyph space to text
space shall be defined by a font matrix specified in an explicit FontMatrix entry in the font. 
"""

As result, the combination of unitsPerEm and FontMatrix must give the same 
result of the "canonical values "  
unitsPerEm=1000  and FontMatrix [ 0.001 0 0 0.001 0 0 ] = [1/1000 0 0 1/1000 0 0]:
this means that 
unitsPerEm=500  should have a correct FontMatrix = [1/500 0 0 1/500 0 0] = [2/1000 0 0 2/1000 0 0].
This explains why in  texgyrepagella-regular-500 the glyphs are 2 times bigger:
its FontMatrix [ 0.001 0 0 0.001 0 0 ] = [1/1000 0 0 1/1000 0 0] needs a factor 2
to give one-thousandth of a unit of text space.
The same -- mutatis mutandis --  for texgyrepagella-regular-2000:
unitsPerEm=2000 means a correct  FontMatrix = [1/2000 0 0 1/2000 0 0] = [0.5/1000 0 0 0.5/1000 0 0]:
but in our case the FontMatrix is again  [ 0.001 0 0 0.001 0 0 ], so the factor is 1/2 .

We can see these values into test-hello-500-1000-2000.pdf 

<< /Length 350 >>        
stream
0 g 0 G
0 g 0 G
BT
/F8 19.999982 Tf 
1 0 0 1 0 28.93918 Tm [<003F>832<0033>479<0049>291<0049>291<0052>296<0039>500<007A>501<007A>]TJ
/F9 9.999991 Tf
1 0 0 1 0 14.5196 Tm [<003F0033004900490052>-250<0053007A007A007A>]TJ
/F10 4.999995 Tf
1 0 0 1 0 0.10003 Tm [<003F>-416<0033>-239<0049>-146<0049>-145<0052>-523<006C>-251<007A>-250<007A>-250<007A>]TJ
ET
endstream
endobj

7 0 obj
<< /Font << /F8 10 0 R /F9 11 0 R /F10 12 0 R >> /ProcSet [ /PDF /Text ] >>

10 0 obj
<< /Type /Font /Subtype /Type0 /Encoding /Identity-H /BaseFont /JDCZQP+TeXGyrePagella-Regular500 /DescendantFonts [ 29 0 R ] /ToUnicode 28 0 R >>
11 0 obj
<< /Type /Font /Subtype /Type0 /Encoding /Identity-H /BaseFont /EDXNJZ+TeXGyrePagella-Regular /DescendantFonts [ 24 0 R ] /ToUnicode 23 0 R >>
12 0 obj
<< /Type /Font /Subtype /Type0 /Encoding /Identity-H /BaseFont /BNOEVZ+TeXGyrePagella-Regular2000 /DescendantFonts [ 19 0 R ] /ToUnicode 18 0 R >>

/F9 is TeXGyrePagella-Regular, and has a Tf of  9.999991 ~  10  ( it should be 10, but the TeX point and the scaled math have their effects) 
/F8 is  ~ 20, and /F10 ~ 5.

In test-500-1000-2000.pdf there are a bit different values, because we are using 10pt.

unitsPerEm different from 1000 exist: for example, if we look at the Fell types 
the IMFeENrm29C.otf has 

$ grep unitsPerEm IMFeENrm29C.ttx 
    <unitsPerEm value="2048"/>

$ calc -p "1/2048" 
0.00048828125

and the FontMatrix correctly says
$ grep FontMatrix IMFeENrm29C.ttx 
      <FontMatrix value="0.000488281 0 0 0.000488281 0 0"/>


(a luatex plain example is 
%% test-fell.tex
\pdfvariable compresslevel0
\pdfvariable objcompresslevel0
\font\TestIMFe=file:IMFeENrm29C.otf at 10bp
{\TestIMFe Hello IMFeENrm29C}
\bye

$> mtxrun--script plain test-fell.tex
)

This relation between unitsPerEm and FontMatrix is enforced  in   CFF2 (the new "font variations" ,

"""
The Top DICT FontMatrix operator is required if the unitsPerEm value in the 'head' table is other than 1000. 
If unitsPerEm is 1000, then the FontMatrix operator may be omitted. 
When included, the FontMatrix operand array must be 1/unitsPerEm 0 0 1/unitsPerEm 0 0. 
The default values shown above assume that unitsPerEm is 1000.
"""
where "default values shown above"   are 0.001 0 0 0.001 0 0 .

There are some limits:
1)  unitsPerEm:  Valid range is from 16 to 16384. This value should be a power of 2 for
 fonts that have TrueType outlines.
2) the CFF has a  type2 charstring,  where the real values are signed Q16.16 
(signed 16bit integer with 16bit fraction,  range [-2^15,2^15-2^-1], resolution 2^-16, encoded in 5 bytes)
and also  integer signed 16bit integer, encoded in 3 bytes. This unusual encoding  is for 
backward compatibility with the type1 charstring, 
where   a 32-bit signed  integer is  encoded in 5 bytes, given a range [-2^31,2^31-1] --- but a note says that 
" Numbers with absolute values greater than 32,000 must be followed
 by a div operator such that the result of the div is less than 32,000."

It's hence possible to compare (CFF) fonts, but we have to  consider the unitsPerEm and the FontMatrix: 
as we have seen  for the width of "M", same glyphs-space values  can get  different text-space values.
In conclusion:  better to avoid to manipulate unitsPErEm and FontMatrix
and keep them to the reference values of 1000 and  [ 0.001 0 0 0.001 0 0 ] .

To see another effect of the FontMatrix, you can make a 
texgyrepagella-regular-skew45.ttx
with only 
      <FontMatrix value="0.001 0.001 0 0.001 0 0"/>
(also modify the names in  "TeXGyrePagella-Regularskew45" as done before)
Now the unitsPerEm=1000 and the FontMatrix are ok for the xscale (first 0.001)  and yscale (fourth 0.001),
but the second 0.001 is a skew of 45°  wrt to the x axis 
(and, as seen before, it's better to avoid such things in a CFF2 font).

--
luigi