semprag / tex

S&P LaTeX and BibTeX sources
The Unlicense
3 stars 1 forks source link

lucidaot option doesn't work with system fonts #20

Closed fintelkai closed 5 years ago

fintelkai commented 5 years ago

When the Lucida OT fonts are in a local ./fonts directory, typesetting basic.tex (changed to lucidaot option) with XeLaTeX succeeds. But when the fonts are either in /Library/Fonts or in ~/Library/Fonts, the following error happens:

(/usr/local/texlive/2018/texmf-dist/tex/latex/unicode-math/unicode-math-table.t
ex)))kpathsea:make_tex: Invalid filename `[LucidaBrightOT.otf]/OT', contains '['

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! fontspec error: "font-not-found"
! 
! The font "LucidaBrightOT" cannot be found.
! 
! See the fontspec documentation for further information.
! 
! For immediate help type H <return>.
!...............................................  

l.107     BoldItalicFont=*-DemiItalic]

FWIW, typesetting lucidaot-sys-example.tex with XeLaTeX works just fine, so something in sp.cls is not letting fontspec find the system fonts.

fintelkai commented 5 years ago

I think the problem is that LucidaBrightOT is the wrong name for the system font to be found. As in lucidaot-sys-example.tex, the font should be called Lucida Bright OT, with spaces, because that's what installing them through FontBook results in. The following changed lines in sp.cls solve the problem for me:

  \defaultfontfeatures{Ligatures=TeX,Scale=0.9}
  \IfFileExists{./fonts/LucidaBrightOT.otf}{
    % look for Lucida font files in 'fonts' directory if it exists
    \defaultfontfeatures+{Path=./fonts/,Extension=.otf}
    \ClassInfo{sp.cls}{Loading LucidaOT files from local ./fonts/ directory; using globally-installed fonts is preferred}
  }{}
  \setmainfont{Lucida Bright OT}
  \setsansfont{Lucida Sans OT}
  \setmonofont{Lucida Sans Typewriter OT}
  \setmathfont{Lucida Bright Math OT}
  \setmathfont{Lucida Bright Math OT Demibold}[version=bold]

I won't push this change until this has been validated.

chbrown commented 5 years ago

It's been a while since I was debugging these OT vs. texmf, when I made those decisions, so your experience might be fresher. Below are my results of picking this back up.

I wasn't using Font Book, then, for global installs, but the manual cp call in https://github.com/semprag/lucida#opentype -- which simply copies the files to $TEXMFHOME/fonts/opentype/*.otf.

I still have the fonts globally installed that way. And, referring to the names with spaces, as in your snippet of changes, breaks.

As a test, I just removed those files and installed all the OT versions with Font Book. Now neither "LucidaBrightOT" nor "Lucida Bright OT" work. The fonts are displayed in Font Book, and the files seem to have gotten installed properly:

 42396 Jan 14  2012 ~/Library/Fonts/LucidaBlackletterOT.otf
184476 Mar  6  2012 ~/Library/Fonts/LucidaBrightMathOT-Demi.otf
391852 Mar  7  2012 ~/Library/Fonts/LucidaBrightMathOT.otf
 60140 Jan 14  2012 ~/Library/Fonts/LucidaBrightOT-Demi.otf
 43884 Jan 14  2012 ~/Library/Fonts/LucidaBrightOT-DemiItalic.otf
 44240 Jan 14  2012 ~/Library/Fonts/LucidaBrightOT-Italic.otf
 95764 Jan 14  2012 ~/Library/Fonts/LucidaBrightOT.otf
 31228 Jan 14  2012 ~/Library/Fonts/LucidaCalligraphyOT.otf
 43012 Jan 14  2012 ~/Library/Fonts/LucidaHandwritingOT.otf
 35100 Jan 14  2012 ~/Library/Fonts/LucidaSansOT-Demi.otf
 34992 Jan 14  2012 ~/Library/Fonts/LucidaSansOT-DemiItalic.otf
 34404 Jan 14  2012 ~/Library/Fonts/LucidaSansOT-Italic.otf
 35392 Jan 14  2012 ~/Library/Fonts/LucidaSansOT.otf
 35256 Jan 14  2012 ~/Library/Fonts/LucidaSansTypewriterOT-Bold.otf
 33228 Jan 14  2012 ~/Library/Fonts/LucidaSansTypewriterOT-BoldOblique.otf
 33644 Jan 14  2012 ~/Library/Fonts/LucidaSansTypewriterOT-Oblique.otf
 34784 Jan 14  2012 ~/Library/Fonts/LucidaSansTypewriterOT.otf

Did you have to do anything else to have your latex distribution look in Font Book's directories?

My macOS (Sierra) is a minor version behind the current release, though I doubt that's relevant.

Maybe we should rename the font files to their eventual / proper names, despite the spaces?

P.S. not sure if your changes elided the BoldFont/ItalicFont/etc. options purely for concision, or because your Font Book method obviates them, but without them, my latex can't find the proper variants, neither in the global or local case. (And the local case is basically the entire reason for OT support, because there is no global case on Overleaf.)

fintelkai commented 5 years ago

When you did the experiment to move the fonts to ~/Library/Fonts/ and then ran XeLaTeX, did you also move the Extension=.otf option to the local \defaultfontfeatures+? When that is active for the global case, fontspec couldn't find the fonts.

Eliding the BoldFont/ItalicFont/etc. options was because of this in the lucidaot.pdf documentation:

In older versions of fontspec, when specifying OpenType fonts by file-name (though not with system font lookups), it was necessary to explicitly specify the italic, bold, and bold italic file names. It should no longer be necessary, but here’s the fallback code just in case you’re working with an old version [...]

So, it seems that the explicit specification is not needed in recent versions of fontspec. In any case, my shorter code works fine here.

fintelkai commented 5 years ago

Since there may not be anyone else who would like the fonts globally in the system, I can just maintain a branch for myself. But it does seem that there's a cleaner case distinction to be made, between calling the fonts by filename if they're in the local source directory and calling them by font name (with spaces and all) when they've been installed via FontBook.

Can you typeset lucidaot-sys-example.tex with the FontBook fonts?

chbrown commented 5 years ago

Ah, that works. Evidently, the Extension= option puts fontspec into "load by filename" mode. I don't entirely understand how the system font installation resolves the Bold vs. DemiBold and Italic vs. Oblique differentiation, but it just works.

Regarding the variants that "should no longer be necessary", were you able to get that magic to work for local fonts (i.e., without system/Font Book installation, e.g., on Overleaf)? I wasn't.

I've now got a working config with global/local support based on that IfFileExists check:

This solution splits off less configuration into the if-local conditional branch, but over-specifies the font variants in the global (= system install) case.

The alternative would be:

Do you have a preference?

fintelkai commented 5 years ago

I’m fine with your solution. I don’t know that there’s an advantage to the underspecified method, other than that the doc seems to suggest the overspecification is unnecessary.

chbrown commented 5 years ago

If you can show me how to render with Lucida on Overleaf without explicitly specifying the BoldFont/ItalicFont/BoldItalicFont variants, I'd be more than happy to cut out all the overspecification :)

fintelkai commented 5 years ago

I'm investigating. There are aspects to this that I still need to understand.

fintelkai commented 5 years ago

OK, here it is. It is in fact possible to have underspecified calls even if one calls the font locally by filename. What is needed is a little fontspec configuration file for each font family. These were actually included when I downloaded the most recent versions of the Lucida OT fonts from TUG. Here's the entire contents of lucidabrightot.fontspec, for example:

% Public domain, originally written 2014 by Bruno Voisin.
%
% Force lookup by filename, and define the usual variants.
% Thus, when fontspec can read this file, a document can do:
% ...
% \setmainfont{LucidaBrightOT}
% ...
% \textit{I want to be Lucida Italic!}
% ...
% And it should work as expected.  This feature was added to fontspec in
% v2.4a (2014/06/21).  The .fontspec file name itself must be
% all-lowercase in that version.
%
\defaultfontfeatures[LucidaBrightOT]
  {
    ExternalLocation,
    UprightFont = "LucidaBrightOT.otf",
    BoldFont = "LucidaBrightOT-Demi.otf",
    ItalicFont = "LucidaBrightOT-Italic.otf",
    BoldItalicFont = "LucidaBrightOT-DemiItalic.otf",
  }

I can put these (and maybe all the font files, which have also been upgraded) in the sp-lucida repo. And then, we can tailor sp.cls to that kind of installation.

fintelkai commented 5 years ago

Actually, @chbrown: if I send you links to the two zip archives I have (Lucida OT, and the new Lucida OT DK fonts), can you update the sp-lucida repo? I'm afraid to break something or other that you've done in there.

chbrown commented 5 years ago

My lucida working directory is clean and up to date with master, but sure, you can send me links / email those zips to me and I can add them. Either way.

chbrown commented 5 years ago

I hadn't come across such .fontspec config files before, but if they do the trick, cool.

The .fontspec solution seems preferable, but in case that doesn't work out, the following split works for local (unrenamed) font files in a ./fonts/ directory as well as global fonts installed by Font Book (but not when 'installed' into a system-wide TEXMF{HOME,LOCAL} directory as in https://github.com/semprag/lucida#opentype):

  \defaultfontfeatures{Ligatures=TeX,Scale=0.9}
  \IfFileExists{./fonts/LucidaBrightOT.otf}{
    \ClassInfo{sp.cls}{Loading LucidaOT files from local directory (./fonts/)}
    \defaultfontfeatures+{Path=./fonts/,Extension=.otf}
    \setmainfont{LucidaBrightOT}[
      Numbers=OldStyle,
      BoldFont=*-Demi,
      ItalicFont=*-Italic,
      BoldItalicFont=*-DemiItalic]
    \setsansfont{LucidaSansOT}[
      BoldFont=*-Demi,
      ItalicFont=*-Italic,
      BoldItalicFont=*-DemiItalic]
    \setmonofont{LucidaSansTypewriterOT}[
      BoldFont=*-Bold,
      ItalicFont=*-Oblique,
      BoldItalicFont=*-BoldOblique]
    \setmathfont{LucidaBrightMathOT}
    \setmathfont{LucidaBrightMathOT-Demi}[version=bold]
  }{
    \ClassInfo{sp.cls}{Loading LucidaOT fonts from system}
    \setmainfont{Lucida Bright OT}[Numbers=OldStyle]
    \setsansfont{Lucida Sans OT}
    \setmonofont{Lucida Sans Typewriter OT}
    \setmathfont{Lucida Bright Math OT}
    \setmathfont{Lucida Bright Math OT Demibold}[version=bold]
  }
fintelkai commented 5 years ago

I got the following to work on Overleaf:

  1. Fonts in ./fonts/ as before.
  2. A LucidaBrightOT.fontspec file in the main directory. Note that the filename has to contain the same capitalization as the main font. (This is per the fontspec documentation, and contra the LucidaOT distribution (which has lowercase in the file name, which does work locally for me for some reason)). It has the following content:
\defaultfontfeatures[LucidaBrightOT]
  {
    Path=./fonts/,
    UprightFont = "LucidaBrightOT.otf",
    BoldFont = "LucidaBrightOT-Demi.otf",
    ItalicFont = "LucidaBrightOT-Italic.otf",
    BoldItalicFont = "LucidaBrightOT-DemiItalic.otf",
  }
  1. A test file with this preamble, which contains no variant specification:
\documentclass{article}
\usepackage{fontspec}
\usepackage{unicode-math}

\defaultfontfeatures{Scale=.92,Path=./fonts/}
\setmainfont[Ligatures=TeX]{LucidaBrightOT}
\setsansfont[Ligatures=TeX]{LucidaSansOT}
\setmonofont{LucidaSansTypewriterOT}
\setmathfont{LucidaBrightMathOT}

\begin{document}
chbrown commented 5 years ago

Finally getting around to trying out the *.fontspec approach...

@fintelkai I'm not sure how your latest comment substantially differs from the configuration I'd been using on Overleaf as of eb22a19, with f3f8ab9 adding support for TEXMFHOME-based (global-, but not system-) installation of OpenType fonts. The test file in your item 3. won't render with system-installed fonts, even if you remove the Path=./fonts/ setting, will it? (Your earlier comment suggested that the font names were the crux of the discrepancy.)

The original point of this issue (#20) was to support loading OpenType Lucida (via [lucidaot]) from the system-installed fonts as well as from files in ./fonts/ (for Overleaf) with no (or minimal?) changes to the document source file, right?

If so, it seems to me that the starting point is to share the following fontspec package configuration between local/system...

\defaultfontfeatures{Ligatures=TeX,Scale=0.9}
\setmainfont{Lucida Bright OT}[Numbers=OldStyle]
\setsansfont{Lucida Sans OT}
\setmonofont{Lucida Sans Typewriter OT}
\setmathfont{Lucida Bright Math OT}

...and then customize the local case as needed, because it doesn't seem to be possible to customize the system config beyond the names supplied in the \setmainfont{...}, etc., commands.

  1. The first problem I'm encountering is that the fontspec package doesn't seem to locate the <FontName>.fontspec file if it contains spaces. I put a Lucida Bright OT.fontspec in the same folder as my .tex document — nothing. I copied it to ./fonts/Lucida Bright OT.fontspec — nothing. (If I change sp.cls to specify the font name without spaces, \setmainfont{LucidaBrightOT}[...], and rename the file to LucidaBrightOT.fontspec, then I can see from the XeTeX log that it reads the file, although it complains that the UprightFont key "is unknown and is being ignored", though that seems to be a different problem.)
  2. Second, the .fontspec files seem to be plain LaTeX sources that fontspec conditionally looks for when it loads a font. I'm not sure what the advantage they confer vs. putting the same configuration directly in the \IfFileExists{./fonts/...} branch in sp.cls (as in my previous comment in this thread). If the idea is that they are provided by the official Lucida distribution, and thus canonical, this seems to be defeated by having to rename them and put them in just the right place, as well as change the optional argument from [LucidaBrightOT] to [Lucida Bright OT] and replace the deprecated ExternalLocation with a specific Path=... setting.

Nevertheless, I'm about to commit a fix taking a different approach, somewhere in the middle between my previous comment and how the .fontspec files work. This succeeds in all significant cases: 1) Overleaf with fonts in ./fonts/*.otf, 2) Local XeLaTeX with fonts in ./fonts/*.otf, and 3) Local XeLaTeX with the OpenType fonts installed by Font Book.

Let me know if you encounter any issues.