groff before CVS: release 1.05
[s-roff.git] / lib / fontfile.c
blob77c57c79755f9dc6eca82fef18404d109c1eb13a
1 // -*- C++ -*-
2 /* Copyright (C) 1989, 1990, 1991 Free Software Foundation, Inc.
3 Written by James Clark (jjc@jclark.uucp)
5 This file is part of groff.
7 groff is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 1, or (at your option) any later
10 version.
12 groff is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License along
18 with groff; see the file LICENSE. If not, write to the Free Software
19 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #include <stdio.h>
22 #include <string.h>
23 #include <assert.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include "font.h"
27 #include "lib.h"
28 #include "fontpath.h"
30 const char *const FONT_ENV_VAR = "GROFF_FONT_PATH";
32 int font::res = 0;
33 int font::hor = 1;
34 int font::vert = 1;
35 int font::unitwidth = 0;
36 int font::paperwidth = 0;
37 int font::paperlength = 0;
38 int font::biggestfont = 0;
39 int font::spare2 = 0;
40 int font::sizescale = 1;
41 int font::tcommand = 0;
42 const char **font::font_name_table = 0;
43 int *font::sizes = 0;
44 char *font::dev_name = 0;
45 char *font::cl_font_dirs = 0;
46 const char *font::family = 0;
47 const char **font::style_table = 0;
49 void font::command_line_font_dir(const char *dir)
51 if (cl_font_dirs == 0) {
52 cl_font_dirs = new char[strlen(dir)+1];
53 strcpy(cl_font_dirs, dir);
55 else {
56 int len = strlen(cl_font_dirs);
57 int need_colon = 0;
58 if (len > 0 && cl_font_dirs[len-1] != ':')
59 need_colon = 1;
60 char *old_dirs = cl_font_dirs;
61 cl_font_dirs = new char[len + need_colon + strlen(dir) + 1];
62 strcpy(cl_font_dirs, old_dirs);
63 if (need_colon)
64 strcat(cl_font_dirs, ":");
65 strcat(cl_font_dirs, dir);
66 a_delete old_dirs;
70 void font::forget_command_line_font_dirs()
72 a_delete cl_font_dirs;
73 cl_font_dirs = 0;
76 FILE *font::open_file(const char *name, char **pathp)
78 assert(dev_name != 0);
79 const char *dir_vec[3];
80 dir_vec[0] = cl_font_dirs;
81 dir_vec[1] = getenv(FONT_ENV_VAR);
82 dir_vec[2] = FONTPATH;
83 for (int i = 0; i < 3; i++)
84 if (dir_vec[i] != 0) {
85 const char *dirs = dir_vec[i];
86 while (*dirs != '\0') {
87 const char *p = strchr(dirs, ':');
88 if (p != dirs) {
89 if (p == 0)
90 p = strchr(dirs, '\0');
91 int need_slash = 0;
92 if (p > dirs && p[-1] != '/')
93 need_slash = 1;
94 char *path = new char[(p - dirs) + need_slash + 3
95 + strlen(dev_name) + 1
96 + strlen(name) + 1];
97 memcpy(path, dirs, p - dirs);
98 path[p - dirs] = '\0';
99 if (need_slash)
100 strcat(path, "/");
101 strcat(path, "dev");
102 strcat(path, dev_name);
103 strcat(path, "/");
104 strcat(path, name);
105 errno = 0;
106 FILE *fp = fopen(path, "r");
107 if (fp != 0) {
108 *pathp = path;
109 return fp;
111 a_delete path;
112 if (*p == '\0')
113 break;
115 dirs = p + 1;
118 return 0;
121 void font::set_device_name(const char *s)
123 dev_name = new char[strlen(s)+1];
124 strcpy(dev_name, s);
127 const char *font::get_device_name()
129 return dev_name;