# How to override default font with virtual font

My setup: TL 2014 (packaged with Debian 8)

I'm trying to create a virtual font and override original font with it. The idea is to copy necessary files of original font with prefix 'o' and in virtual font refer to that.

All the files will be tested in current directory:

mkdir mydir && cd mydir


Copy the default font to be overriden:

cp kpsewhich cmr10.tfm ocmr10.tfm


Create virtual font (replace M to i):

tftopl cmr10 > cmr10.vpl
patch cmr10.vpl << EOF
0a1
> (MAPFONT D 0 (FONTNAME ocmr10))
564a566
>    (MAP (SETCHAR C i))
EOF
vptovf cmr10.vpl


Create test file and compile it:

echo "\font\tenrm=cmr10 \tenrm M \bye" > test.tex
tex test.tex


Now if we run dvipdfmx test.dvi and then view resulting pdf document, everything is processed without errors, but the document contains original symbol (i.e., as if no virtual font was used). If we open dvi file via xdvi test.dvi directly instead, the correct symbol is printed, but the following warnings are produced:

kpathsea: Running mktexpk --mfmode / --bdpi 600 --mag 1+0/600 --dpi 600 ocmr10
mktexpk: don't know how to create bitmap font for ocmr10.
mktexpk: perhaps ocmr10 is missing from the map file.
kpathsea: Appending font creation commands to missfont.log.


What should be done to override a default font with virtual font so that it will be processed by all programs?

• At the end you want a real glyph, not a virtual one. So your virtual font shouldn't be the end of the line. Create a virtual font ocmr10.vpl which gets its glyphs from the real cmr10, and then use it as \font\tenrm=ocmr10 \tenrm ... Jun 29 '15 at 7:18
• That's the point. I need that virtual font has the same name as original font (i.e., in cmr10.vpl I refer to original font, which must of course be renamed, and in my document I need to be able to refer simply to cmr10). Yes, this is not trivial. I hope someone can recommend a solution. Jun 29 '15 at 7:47
• Don't do it. You would have to change the original cmr10.tfm and it is really a very bad idea to have manipulated versions of this files. It would break all your other documents. I also don't see any reason to do it. If you are able to change core files like the standard fonts you should also be able to change the font definitions in a document. Jun 29 '15 at 8:19
• @UlrikeFischer I wouldn't like to distract from the main topic. Do you know how to move this discussion to the chat? Jun 29 '15 at 8:28
• Please don't be offended by this question: no offence is intended. Have you ever created the support files necessary to get an arbitrary type1 font to work with (pdf)LaTeX? That is, have you done this in a case where you are not dealing with the additional complication of wanting to combine characters from different fonts, but where you are just creating the files necessary to use a bog standard font correctly?
– cfr
Jun 30 '15 at 1:00

In the case of dvipdfmx, it seems that cmr10.vf is not read if the original map is effective. The follwoing gives an output which Igor wants:

\special{pdf:mapline -cmr10 CMR10 <cmr10.pfb}
\special{pdf:mapline +ocmr10 CMR10 <cmr10.pfb}
\nopagenumbers
\font\xxx=cmr10
\xxx M
\bye

• Thanks, your solution works. See EDIT - you are right about vf-file. Please change \xxx A to \xxx M because I changed the initial example, as Ulrike Fischer suggested. The question remains why xdvi shows warnings and how to fix them? Jun 29 '15 at 9:37
• I realized that maybe it is possible to remap only with encoding file and we don't need virtual font at all - is it possible to create such an encoding file that it will take selectve symbols of the font from another location in the glyph container (possibly even from another glyph container)? And how to activate such encoding file system wide, not with \special commands? Jun 29 '15 at 11:12
• @IgorLiferenko: You can exchange glyphs (from the same real font) with encoding files, but not the metrics. TeX will place the glyphs and do the line breaking with the information from the tfm file. Jun 29 '15 at 13:03
• @UlrikeFischer Unless you also create a suitable .tfm, of course. I'm not entirely sure what is known at this point....
– cfr
Jun 30 '15 at 1:03

Disclaimer: I do not recommend changing the metrics of standard font files, especially not on system level. Using a new font name and configuring the TeX file/package/... to use the new font name instead seems the cleaner approach to me.

In the virtual font, ocmr10.tfm is referred. But only the metrics exists. An actual font mapping is needed, but you cannot use the original font cmr10, because you have overwritten the cmr10.tfm file with the patched virtual version.

When pdfTeX in PDF mode is used, the mapping from ocmr10.tfm to the Type 1 version is easy:

 \pdfmapline{+ocmr10 CMR10 <cmr10.pfb}


In other cases, a map file ocmr.map is needed with contents

ocmr10 CMR10 <cmr10.pfb


TeX do not yet need the map file, because it only uses the VF and TFM files, but the drivers has to be configured to use the map file. But each driver, you want to use, has to be configured accordingly to use the .map file.

Alternatively the map file can be managed via updmap, but this works on the whole TeX system.

Another point is that there are two different cmr10.tfm. thus the search paths for TeX and drivers has to be configured, that the right version is found first.

## dvips

Setting up the font for dvips is quite complex.

The map file cannot be directly be given on the command line, instead the map file is specified via a configuration file config.ocmr:

    p -deletecm.map
p +ocm.map


The file file deletecm.map contains the old entry for cmr10:

cmr10 CMR10 <cmr10.pfb


(For experiments I had used "p -cm.map", which excludes all CM fonts). This mapping for cmr10 must be removed, otherwise the virtual font is not looked up and the PFB font file is used directly.

The configuration file is not found in the current directly, thus the call for dvips needs a changed environment variable TEXCONFIG, which includes the current directory:

TEXCONFIG=:. dvips -P ocmr test


Or you can create a TDS tree, e.g. ./texmf.home, where the files are put into the correct subdirectories. Then the programs are called with TEXMFHOME pointing to this TDS tree:

TEXMFHOME=./texmf.home tex test
TEXMFHOME=./texmf.home dvips -P ocmr test

• I don't think that creating a map is enough. If I got the description right then imho the wrong metrics are still used. Jun 29 '15 at 8:52
• It seems there is a typo: ocrm10 (should be ocmr10?). Could you recommend how to use the map in dvips or dvipdfmx? dvips -u+ocmr.map does not work. To answer for what it is needed - I'm just curious, and think that it's interesting problem which will help me to better understand the basis. Jun 29 '15 at 8:55
• @IgorLiferenko: Then I suggest that you try to exchange M and i -- to make sure that you get also the metrics right. With A and B you will not see if you get them wrong. Jun 29 '15 at 9:00
• @UlrikeFischer I changed the example. How do you suggest to check the metrics? Jun 29 '15 at 9:49
• Well type some text with M and i and look if they occupy the correct space. Jun 29 '15 at 9:52

A bit long as a comment so I'm adding this as answer. As it looks as if you don't take the font metrics into account here an example to show you that you can't simply exchange glyphs.

The following document uses only one "real" font: sftt1000.pfb. The first paragraph sets this font using the metrics from the normal roman family and the second the correct metrics from the tt-font.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{lipsum}
%replace the real font, needs pdflatex:
\pdfmapline{=ecrm1000 SFRM1000 " T1Encoding ReEncodeFont " <cm-super-t1.enc <sftt1000.pfb}
\begin{document}
\lipsum[1]

\ttfamily
\lipsum[1]
\end{document}


• Can you recommend any description of the whole encoding map specification? This topic is quite interesting to me. Actually, I'm interested if I can compose a separate tfm file and then fill it with glyphs from different font containers (say, slots 0--127 from the default, and slots 128--255 from the other). I'm going to use a manually crafted tfm which will correspond to the substituted glyphs. Jun 29 '15 at 14:04
• @IgorLiferenko You need to use a virtual font if you want to combine glyphs from different fonts into a single TeX font (i.e. a single .tfm). This is what the virtual font mechanism was invented for, as I understand it. It made it possible to use, for example, the fi/fl/etc. ligatures supplied with type1 postscript fonts, even though these were typically in a different .pfb from the regular, lowercase glyphs.
– cfr
Jun 30 '15 at 0:57

WARNING
This brings incompatibilities into your TeX installation. Do this only if you understand what you are doing.

Thanks to everyone! With the help of your answers I obtained the solution. Just to sum it up, here are all the steps:

All the files will be tested in current directory:

mkdir mydir && cd mydir


Copy the default font to be overriden:

cp kpsewhich cmr10.tfm ocmr10.tfm


Create virtual font (replace A to B):

NOTE this is example serves simply to demonstrate the basic principles, we assume that metrics in the tfm file already corresponds to the new glyphs (in the real case it will be properly created virtual font which combines several "physical" fonts in it).

tftopl cmr10 > cmr10.vpl
patch cmr10.vpl << EOF
0a1
> (MAPFONT D 0 (FONTNAME ocmr10))
482a4845
>    (MAP (SETCHAR C B))
EOF
vptovf cmr10.vpl


Now we have vf and tfm files for cmr10:

$ls cmr10.vf cmr10.tfm cmr10.vpl ocmr10.tfm  Create test file and compile it: NOTE the \font\tenrm=cmr10 \tenrm part will not be needed when we rebuild the format, it is here only to simplify the example echo "\font\tenrm=cmr10 \tenrm A \bye" > test.tex tex test.tex  For xdvi, copy former pk for new ocmr10: cp kpsewhich cmr10.pk ocmr10.600pk  (If there is no pk-file for a given font, copy mf-file instead) For dvipdfmx, map file must be changed. In my case, I don't need system tree to be protected from updates, so I simply change pdftex.map: sudo perl -i -pe 's/^cmr10/ocmr10/' kpsewhich pdftex.map  Now check that everything works: $ xdvi test.dvi
\$ dvipdfmx test.dvi
< view pdf file >

• Please add a warning telling people not to do this at home! Seriously, this should have a large warning on it. It just really ought not be done at all but, if it is, it should be done only by consenting adults in the privacy of their own homes. There may be children reading this page!
– cfr
Jun 30 '15 at 3:05