├── README.md └── make.py /README.md: -------------------------------------------------------------------------------- 1 | LaTeX TTF Builder 2 | ================= 3 | 4 | Description 5 | ----------- 6 | 7 | Maker script for generating pdfs from latex that need to use one-off ttf fonts 8 | that you don't want to hassle with going through the error-prone process of 9 | adding to your latex system. 10 | 11 | Credit for the core concepts for this belong to jyzhao at: 12 | http://math.stanford.edu/~jyzhao/latexfonts.php 13 | 14 | Example 15 | ------- 16 | 17 | In general, this script can be run like the following: 18 | 19 | ```bash 20 | python make.py test.tex --ttf kevin.ttf custom2.ttf 21 | ``` 22 | 23 | The script will generate the necessary tfm files for the ttf fonts, build 24 | the tex file and then clean up all temporary files such that you should be 25 | left with the ttf files and tex file. 26 | 27 | The script should fail the first time you use it because it checks to see 28 | if you have defined the command to use the font within your tex file. 29 | 30 | That command will be printed in the output just above the failure. It begins 31 | with a \newcommand.. Copy this line, add it to your tex file and rerun. You 32 | can of course use the new font with the custom font command that the line just 33 | defined for you. 34 | 35 | For example, if if if, I have a font file called *kevin.ttf*, the tex to 36 | define that font command that I would need to add would look like: 37 | 38 | ```latex 39 | \newcommand\kevin[1]{{\usefont{T1}{kevin}{m}{n} #1 }} 40 | ``` 41 | 42 | I would then use the font later in the tex document like this: 43 | 44 | 45 | ```latex 46 | \kevin{some text here that I want in this font} 47 | ``` 48 | 49 | If you have both of those lines in your tex file, you should see the font 50 | in the rendered pdf file. 51 | 52 | 53 | Dependencies 54 | ------------ 55 | 56 | * Python 2.7+ 57 | * pdflatex 58 | * ttf2tfm (which usually comes with most latex packages) 59 | * latex package, such as texmf, etc. 60 | 61 | 62 | Author 63 | ------ 64 | 65 | * Kevin Turner 66 | * @ksturner 67 | -------------------------------------------------------------------------------- /make.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | ''' 3 | Simple python script to automate using ttf fonts with a latex document to 4 | generate pdf files. 5 | 6 | The core concepts for using one-off ttf fonts without installing them to the 7 | system was adapted from the work of jyzhao at: 8 | http://math.stanford.edu/~jyzhao/latexfonts.php 9 | 10 | Author: Kevin Turner 11 | @ksturner 12 | 13 | ''' 14 | import argparse 15 | import logging 16 | import os 17 | import sys 18 | 19 | files = { 20 | 't1.fd': ''' 21 | \ProvidesFile{t1%%%.fd} 22 | \DeclareFontFamily{T1}{%%%}{} 23 | \DeclareFontShape{T1}{%%%}{m}{n}{ <-> %%%}{} 24 | \pdfmapline{+%%%\space <%%%.ttf\space .fd file exists 218 | fn = 't1{}.fd'.format(basename) 219 | cleanup_files.append(fn) 220 | if not os.path.exists(fn) or args.force: 221 | fout = open(fn, 'w') 222 | fout.write(files['t1.fd'].replace('%%%', basename)) 223 | fout.close() 224 | 225 | # ensure the tfm file exists. 226 | fn = '{}.tfm'.format(basename) 227 | cleanup_files.append(fn) 228 | if not os.path.exists(fn) or args.force: 229 | os.system('ttf2tfm {} -p T1-WGL4.enc'.format(ttf)) 230 | 231 | if os.path.exists(fn): 232 | logging.info("created {} file for use.".format(fn)) 233 | m = "\\newcommand\\%%%[1]{{\\usefont{T1}{%%%}{m}{n} #1 }}" 234 | m = m.replace('%%%', basename) 235 | required_lines.append(m) 236 | logging.info('add: '+m) 237 | logging.info('then use: \\%%%{blah blah}'.replace('%%%',basename)) 238 | else: 239 | logging.warn("could NOT create {} file".format(fn)) 240 | 241 | 242 | def main(args): 243 | ''' 244 | Assumes ttf files have been converted and if so, generates the 245 | pdf file for the tex document. 246 | ''' 247 | global cleanup_files, required_lines 248 | data = args.infile.read() 249 | for line in required_lines: 250 | if not line in data: 251 | logging.error("please add '%s' to file %s", 252 | line, args.infile.name) 253 | return 254 | 255 | (basename, ext) = os.path.splitext(args.infile.name) 256 | cmd = 'pdflatex {}'.format(args.infile.name) 257 | logging.info("running: {}".format(cmd)) 258 | os.system(cmd) 259 | 260 | for suffix in ['.aux', '.out', '.log', '.toc']: 261 | fn = '{}{}'.format(basename, suffix) 262 | if os.path.exists(fn): 263 | cleanup_files.append(fn) 264 | 265 | 266 | def cleanup_temp_files(args): 267 | ''' Cleans up any temporary files. ''' 268 | global cleanup_files 269 | for fn in cleanup_files: 270 | (basename, ext) = os.path.splitext(fn) 271 | if not ext in ['.tex', '.png', '.ttf']: 272 | logging.debug('deleting {}'.format(fn)) 273 | os.unlink(fn) 274 | else: 275 | logging.warning("skipping {} from cleanup process".format(fn)) 276 | 277 | 278 | if __name__ == '__main__': 279 | args = setup_parser() 280 | setup_logger(args) 281 | handle_ttfs(args) 282 | main(args) 283 | cleanup_temp_files(args) 284 | --------------------------------------------------------------------------------