Skip to content

PostScript Type42 embedding is broken in various ways #18191

Closed

Description

@anntzer

Bug report

Bug summary

As in the title.

Code for reproduction

from pylab import *
rcParams["ps.fonttype"] = 42
figtext(.5, .5, "€"); savefig("/tmp/euro.ps"); close("all")
figtext(.5, .5, "0€"); savefig("/tmp/zeroeuro.ps"); close("all")
figtext(.5, .5, "$\sqrt{2}$"); savefig("/tmp/sqrt.ps"); close("all")

Actual outcome

In euro.ps the font is actually embedded as Type 3, not Type 42 (as can be checked by opening the file in a text editor).
In zeroeuro.ps the font is correctly embedded as Type 42, but the euro sign fails to display correctly (we get a rectangular box instead).
sqrt.ps causes an error with the viewer (Error: /invalidfont in definefont).

Given that in all likelihood this has been broken for a loooooong time (as ttconv is rarely touched) it may be worth waiting for a decision on #18143 first before trying to fix this.

On the other hand, note that this also affects the ps.fonttype = 3 case when rendering more than 255 characters, because we fall back to type 42 in that case (example below). By the spec, a type 3 font cannot contain more than 255 glyphs, but empirically, things work just fine for us, likely because we always refer to glyphs by glyph name rather than by glyph index? If we want to be super safe wrt. the spec, I guess we could split a >255-glyph font into multiple fonts and call setfont as needed (we're positioning glyphs one at a time anyways).

import textwrap
from pylab import *
figtext(0, .5, "\n".join(textwrap.wrap("".join(c for c in map(chr, range(512)) if c.isprintable()))))
savefig("/tmp/test.ps")

As a side point, it seems likely that type 42 embedding is also broken for the pdf backend, per #12636...

Matplotlib version

  • Operating system: linux
  • Matplotlib version: master
  • Matplotlib backend (print(matplotlib.get_backend())): ps
  • Python version: 38
  • Jupyter version (if applicable):
  • Other libraries:

Activity

  1. aitikgupta commented on Mar 27, 2021

    @aitikgupta
    Contributor

    fails to display correctly (we get a rectangular box instead)

    Or sometimes fail to display anything at all.

    import matplotlib.pyplot as plt
    from matplotlib.font_manager import FontProperties
    plt.rcParams["ps.fonttype"] = 42
    
    path = '/path/to/matplotlib/doc/_static/fonts/Carlogo-bolditalic.ttf'
    prop = FontProperties(fname=path)
    
    plt.figtext(0.5, 0.5, "normal_text", font=prop)
    plt.savefig('normal_text.ps')

    The font has the glyphs (Type 3 works fine), but the output is blank (even though the whole font is embedded).

  2. aitikgupta commented on Jul 13, 2021

    @aitikgupta
    Contributor

    Updating this issue:
    euro.ps, zeroeuro.ps and test.ps outputs are now correct with #20391 pulled in.
    (both of them are subsetted, as well as are embedded as Type 42 (test.ps is a hybrid Type3/Type42 output, since chars > 255))

    sqrt.ps however, is still broken. I wonder if recent PRs by @sauerburger ends up fixing this as well?

  3. sauerburger commented on Jul 13, 2021

    @sauerburger
    Member

    I think the issue with sqrt.ps was the missing header bytes in the embedded font and should be fixed with #20597.

  4. aitikgupta commented on Jul 13, 2021

    @aitikgupta
    Contributor

    I just checked, #20597 indeed fixes the sqrt.ps bug too!

  5. added this to the v3.5.0 milestone on Jul 20, 2021
  6. aitikgupta commented on Jul 22, 2021

    @aitikgupta
    Contributor

    Since we don't have more examples/references about how and what more is broken for Type42 embedding, I think we can close this with #20391.. every example here has been fixed either by subsetting PR or #20597

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions