lilypond-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

pygments support in PDFs with LM fonts


From: Werner LEMBERG
Subject: pygments support in PDFs with LM fonts
Date: Wed, 01 Dec 2021 13:06:51 +0000 (UTC)

Here is my solution for using the LM typewriter fonts for pygments
support in texinfo.  It consists of the following.

* Files `lm-ot1.tex` and `lm-ot1-map.tex`.  They should be executed as
  follows

    export TEXINPUTS="<TeXLive>/fonts/afm/public/lm;"
    tex lm-ot1.tex
    tex lm-ot1-maps.tex

  to produce a bunch of files.  This expects that the 'lm' TeXLive
  package is installed.

* Run the created script `pltotf-script.sh`.  After this, the
  following files are of interest:

    lmttr.tfm   Metric files in OT1TT encoding for the LM fonts.
    lmttro.tfm
    lmttb.tfm
    lmttbo.tfm
    ot1tt.enc   An encoding file needed for the LM fonts.
    lmtt.map    A font mapping file to tell the TeX engine how to
                use the LM fonts in the PDF output.

  All other generated files can be deleted.

  TeX uses the following environment variables for finding the various
  files.

    *.tfm:  TFMFONTS (or TEXFONTS)
    *.enc:  ENCFONTS
    *.map:  TEXFONTMAPS

  For testing it is sufficient to place the files into the current
  directory; however, for integration into LilyPond the environment
  variables are necessary.

* The demo file `pygments.texinfo` contains the new macro stuff and
  some tests; I've also appended the resulting PDF.

As can be seen, the LM fonts are a *light* version of CM typewriter.
This is not ideal IMHO, but...


     Werner
% lm-ot1.tex
%
% Written 2021 by Werner Lemberg <wl@gnu.org>.
%
% This is an input file for the `fontinst` plain TeX macro package to create
% metric files in OT1TT encoding for the 'LMMonoLt10' family, which comes
% with the 'Latin Modern' font bundle.  The necessary `.afm` input files are
% part of the 'lm' TeXLive package.
%
% Usage:
%
%   export TEXINPUTS="/path/to/afm/files;"
%   tex lm-ot1.tex
%
% Using a semicolon above allows the `TEXINPUTS` environment variable to be
% the same on Unix and Windows.

\input fontinst.sty

\recordtransforms{lm-ot1-transforms.recs}
  \transformfont{lmttr}{\reencodefont{ot1tt}{\fromafm{lmtl10}}}
  \transformfont{lmttb}{\reencodefont{ot1tt}{\fromafm{lmtk10}}}
  \transformfont{lmttro}{\reencodefont{ot1tt}{\fromafm{lmtlo10}}}
  \transformfont{lmttbo}{\reencodefont{ot1tt}{\fromafm{lmtko10}}}

  % Since we don't need an `.fd` file, replace `\installfonts ...
  % \endinstallfonts` with an ordinary group.
  \bgroup
    \installrawfont{lmttr}{lmttr,newlatin option nosc}{ot1tt}%
                     {OT1}{lmtt}{m}{n}{}
    \installrawfont{lmttb}{lmttb,newlatin option nosc}{ot1tt}%
                     {OT1}{lmtt}{b}{n}{}
    \installrawfont{lmttro}{lmttro,newlatin option nosc}{ot1tt}%
                     {OT1}{lmtt}{m}{sl}{}
    \installrawfont{lmttbo}{lmttbo,newlatin option nosc}{ot1tt}%
                     {OT1}{lmtt}{b}{sl}{}
  \egroup
\endrecordtransforms

\bye
% lm-ot1-map.tex
%
% Written 2021 by Werner Lemberg <wl@gnu.org>.
%
% This is an input file for the `fontinst` plain TeX macro package.  It
% does the following.
%
% (1) Create a font map file `lmtt.map`, which is suitable as an argument to
%     the pdfTeX primitive `\pdfmapfile` (also accepted by luatex and
%     XeTeX).
% (2) Create a script `pltotf-script.sh`, which should be executed to create
%     the final TFM files with the `pltotf` program.
%
% Usage:
%
%   tex lm-ot1-map.tex
%
% Call this after processing file `lm-ot1.tex`.

\input finstmsc.sty

\resetstr{PSfontsuffix}{.pfb}
\resetstr{encodingname}{ot1tt}

\adddriver{dvips}{lmtt.map}
\adddriver{pltotf}{pltotf-script.sh}

\input lm-ot1-transforms.recs

\donedrivers

\bye
\input texinfo.tex

@tex

% Modify `@setcolor` to make it work in `@example` environments.

\gdef\setcolor{\begingroup \spaceisspace \dosetcolor}
\gdef\dosetcolor#1{%
    \xdef\currentcolordefs{\gdef\noexpand\thiscolor{#1}}%
  \endgroup
  \domark
  \pdfsetcolor{#1}%
}


% Internal font setup and related commands for `example` environments.  For
% pygments, we need bold and bold italic typewriter, which is not available
% for the Computer Modern font family used by texinfo.  Instead, we use the
% font family 'LMMonoLt10' that comes with the 'Latin Modern' package, which
% is a light version of 'cmtt'.

% Load fonts (in 'OT1TT' encoding).
\global\font\lmtexttt = lmttr scaled \mainmagstep
\global\font\lmtextttsl = lmttro scaled \mainmagstep
\global\font\lmtextttbx = lmttb scaled \mainmagstep
\global\font\lmtextttbxsl = lmttbo scaled \mainmagstep
\global\font\lmsmalltt = lmttr scaled 900
\global\font\lmsmallttsl = lmttro scaled 900
\global\font\lmsmallttbx = lmttb scaled 900
\global\font\lmsmallttbxsl = lmttbo scaled 900

% Register CMaps.
\csname cmapOT1TT\endcsname{\lmtexttt}
\csname cmapOT1TT\endcsname{\lmtextttsl}
\csname cmapOT1TT\endcsname{\lmtextttbx}
\csname cmapOT1TT\endcsname{\lmtextttbxsl}
\csname cmapOT1TT\endcsname{\lmsmalltt}
\csname cmapOT1TT\endcsname{\lmsmallttsl}
\csname cmapOT1TT\endcsname{\lmsmallttbx}
\csname cmapOT1TT\endcsname{\lmsmallttbxsl}

% Load font map file.
\pdfmapfile{+lmtt.map}

% Redefine `@example` and `@smallexample` environments to use the just
% defined fonts.  To avoid redefinitions of texinfo macros, we use
% `\aftergroup` so that the actual setup of the two environments happen
% outside of the `@tex` group.
\gdef\lymakedispenvdef{%
  \makedispenvdef{example}{\lyfontsetup}}

\gdef\lyfontsetup{%
  \nonfillstart
%
  \let\texttt \lmtexttt
  \let\textttsl \lmtextttsl
  \let\textttbx \lmtextttbx
  \let\textttbxsl \lmtextttbxsl
  \let\smalltt \lmsmalltt
  \let\smallttsl \lmsmallttsl
  \let\smallttbx \lmsmallttbx
  \let\smallttbxsl \lmsmallttbxsl
%
  \expandafter\let\expandafter\ttfont \csname\curfontsize tt\endcsname
  \expandafter\let\expandafter\ttslfont \csname\curfontsize ttsl\endcsname
%
  \tt
  \setcodequotes
  \let\kbdfont = \kbdexamplefont
  \parsearg\gobble
}

\aftergroup\lymakedispenvdef

% Internal font switches for `@tb` and `@tbsl`.
\xdef\textstring{text}

\gdef\dotb#1{{%
  \ifx\curfontsize\textstring
    \textttbx
  \else
    \smallttbx
  \fi
  \plainfrenchspacing
  #1}%
  \null}

\gdef\dotbsl#1{{%
  \ifx\curfontsize\textstring
    \textttbxsl
  \else
    \smallttbxsl
  \fi
  \plainfrenchspacing
  #1}%
  \null}

@end tex


@c A generic macro `@color` to set the color globally.

@iftex
@macro color {clr}
@setcolor{\clr\}
@end macro
@end iftex

@ifnottex
@macro color {clr}
@end macro
@end ifnottex


@c Since stuff like `@t{@b{...}}` doesn't work in texinfo, provide `@tb` as
@c a command to access bold typewriter.

@iftex
@macro tb {arg}
@dotb{\arg\}
@end macro
@end iftex

@ifnottex
@macro tb {arg}
\arg\
@end macro
@end ifnottex


@c Provide another command `@tbsl` to access bold slanted typewriter.

@iftex
@macro tbsl {arg}
@dotbsl{\arg\}
@end macro
@end iftex

@ifnottex
@macro tbsl {arg}
\arg\
@end macro
@end ifnottex


@c =========================================================================


@node tests for pygments support
@section tests for pygments support

@noindent
@code{example} environment:

@example
example@t{typewriter}example
example@color{1 0 0}@t{typewriter}@color{0 0 0}example

example@slanted{typewriter}example
example@color{1 0 0}@slanted{typewriter slanted}@color{0 0 0}example

example@tb{typewriter bold}example
example@color{0 1 0}@tb{typewriter bold}@color{0 0 0}example

example@tbsl{typewriter bold slanted}example
example@color{0 0 1}@tbsl{typewriter bold slanted}@color{0 0 0}example
@end example

@noindent
@code{smallexample} environment:

@smallexample
example@t{typewriter}example
example@color{1 0 0}@t{typewriter}@color{0 0 0}example

example@slanted{typewriter}example
example@color{1 0 0}@slanted{typewriter slanted}@color{0 0 0}example

example@tb{typewriter bold}example
example@color{0 1 0}@tb{typewriter bold}@color{0 0 0}example

example@tbsl{typewriter bold slanted}example
example@color{0 0 1}@tbsl{typewriter bold slanted}@color{0 0 0}example
@end smallexample

@bye

Attachment: pygments.pdf
Description: Adobe PDF document


reply via email to

[Prev in Thread] Current Thread [Next in Thread]