| 1 |
/* |
| 2 |
|
| 3 |
Copyright 2006-2010 Taco Hoekwater <[email protected]> |
| 4 |
|
| 5 |
This file is part of LuaTeX. |
| 6 |
|
| 7 |
LuaTeX 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 2 of the License, or (at your |
| 10 |
option) any later version. |
| 11 |
|
| 12 |
LuaTeX is distributed in the hope that it will be useful, but WITHOUT |
| 13 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 15 |
License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License along |
| 18 |
with LuaTeX; if not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
*/ |
| 21 |
|
| 22 |
#include "ptexlib.h" |
| 23 |
#include "lua/luatex-api.h" |
| 24 |
|
| 25 |
/*tex |
| 26 |
|
| 27 |
A bit more interfacing is needed for proper error reporting. |
| 28 |
|
| 29 |
*/ |
| 30 |
|
| 31 |
static char *font_error_message(pointer u, char *nom, scaled s) |
| 32 |
{ |
| 33 |
char *str = xmalloc(256); |
| 34 |
char *c = makecstring(cs_text(u)); |
| 35 |
const char *extra = "metric data not found or bad"; |
| 36 |
if (s >= 0) { |
| 37 |
snprintf(str, 255, "Font \\%s=%s at %gpt not loadable: %s", c, nom, (double) s / 65536, extra); |
| 38 |
} else if (s != -1000) { |
| 39 |
snprintf(str, 255, "Font \\%s=%s scaled %d not loadable: %s", c, nom, (int) (-s), extra); |
| 40 |
} else { |
| 41 |
snprintf(str, 255, "Font \\%s=%s not loadable: %s", c, nom, extra); |
| 42 |
} |
| 43 |
free(c); |
| 44 |
return str; |
| 45 |
} |
| 46 |
|
| 47 |
static int do_define_font(int f, const char *cnom, scaled s, int natural_dir) |
| 48 |
{ |
| 49 |
boolean res = 0; |
| 50 |
char *cnam; |
| 51 |
int r, t; |
| 52 |
int callback_id = callback_defined(define_font_callback); |
| 53 |
if (callback_id > 0) { |
| 54 |
cnam = xstrdup(cnom); |
| 55 |
callback_id = run_and_save_callback(callback_id, "Sdd->", cnam, s, f); |
| 56 |
free(cnam); |
| 57 |
if (callback_id > 0) { |
| 58 |
/*tex Success. */ |
| 59 |
luaL_checkstack(Luas, 1, "out of stack space"); |
| 60 |
lua_rawgeti(Luas, LUA_REGISTRYINDEX, callback_id); |
| 61 |
t = lua_type(Luas, -1); |
| 62 |
if (t == LUA_TTABLE) { |
| 63 |
res = font_from_lua(Luas, f); |
| 64 |
destroy_saved_callback(callback_id); |
| 65 |
} else if (t == LUA_TNUMBER) { |
| 66 |
r = (int) lua_tointeger(Luas, -1); |
| 67 |
destroy_saved_callback(callback_id); |
| 68 |
delete_font(f); |
| 69 |
lua_pop(Luas, 1); |
| 70 |
return r; |
| 71 |
} else { |
| 72 |
lua_pop(Luas, 1); |
| 73 |
delete_font(f); |
| 74 |
return 0; |
| 75 |
} |
| 76 |
} |
| 77 |
} else if (callback_id == 0) { |
| 78 |
res = read_tfm_info(f, cnom, s); |
| 79 |
if (res) { |
| 80 |
set_hyphen_char(f, default_hyphen_char_par); |
| 81 |
set_skew_char(f, default_skew_char_par); |
| 82 |
} |
| 83 |
} |
| 84 |
if (font_name(f) && strlen(font_name(f)) > 255) { |
| 85 |
/*tex |
| 86 |
|
| 87 |
The font name has to fit in the dvi file's single byte storage. There |
| 88 |
is no need to test area, as we are never using it. |
| 89 |
|
| 90 |
If we have an error in DVI mode we just abort as the DVI file is unuseable |
| 91 |
anyway then. |
| 92 |
*/ |
| 93 |
if (get_o_mode() == OMODE_DVI) { |
| 94 |
formatted_error("backend","font name too long for DVI: %s",font_name(f)); |
| 95 |
} |
| 96 |
} |
| 97 |
if (res) { |
| 98 |
if (font_type(f) != virtual_font_type) { |
| 99 |
/*tex This implies \LUA. */ |
| 100 |
do_vf(f); |
| 101 |
set_font_natural_dir(f, natural_dir); |
| 102 |
} |
| 103 |
return f; |
| 104 |
} else { |
| 105 |
delete_font(f); |
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
int read_font_info(pointer u, char *cnom, scaled s, int natural_dir) |
| 112 |
{ |
| 113 |
int f; |
| 114 |
char *msg; |
| 115 |
|
| 116 |
f = new_font(); |
| 117 |
if ((f = do_define_font(f, cnom, s, natural_dir))) { |
| 118 |
return f; |
| 119 |
} else { |
| 120 |
const char *help[] = { |
| 121 |
"I wasn't able to read the size data for this font,", |
| 122 |
"so I will ignore the font specification.", |
| 123 |
"[Wizards can fix TFM files using TFtoPL/PLtoTF.]", |
| 124 |
"You might try inserting a different font spec;", |
| 125 |
"e.g., type `I\\font<same font id>=<substitute font name>'.", |
| 126 |
NULL |
| 127 |
}; |
| 128 |
if (suppress_fontnotfound_error_par == 0) { |
| 129 |
msg = font_error_message(u, cnom, s); |
| 130 |
tex_error(msg, help); |
| 131 |
free(msg); |
| 132 |
} |
| 133 |
return 0; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
int find_font_id(const char *nom, scaled s) |
| 138 |
{ |
| 139 |
int f; |
| 140 |
f = new_font(); |
| 141 |
if ((f = do_define_font(f, nom, s, -1))) { |
| 142 |
return f; |
| 143 |
} else { |
| 144 |
return 0; |
| 145 |
} |
| 146 |
} |