beta-0.89.2
[luatex.git] / source / texk / web2c / luatexdir / font / dofont.w
blobdb4c2a5ebf064d07fb160447efb93d38598e7ed6
1 % dofont.w
3 % Copyright 2006-2010 Taco Hoekwater <taco@@luatex.org>
5 % This file is part of LuaTeX.
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.
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.
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/>.
20 @ @c
23 #include "ptexlib.h"
24 #include "lua/luatex-api.h"
26 @ a bit more interfacing is needed for proper error reporting
29 static char *font_error_message(pointer u, char *nom, scaled s)
31 char *str = xmalloc(256);
32 char *c = makecstring(cs_text(u));
33 const char *extra = "metric data not found or bad";
34 if (s >= 0) {
35 snprintf(str, 255, "Font \\%s=%s at %gpt not loadable: %s", c, nom,
36 (double) s / 65536, extra);
37 } else if (s != -1000) {
38 snprintf(str, 255, "Font \\%s=%s scaled %d not loadable: %s", c, nom,
39 (int) (-s), extra);
40 } else {
41 snprintf(str, 255, "Font \\%s=%s not loadable: %s", c, nom, extra);
43 free(c);
44 return str;
47 static int do_define_font(int f, const char *cnom, scaled s, int natural_dir)
50 boolean res; /* was the callback successful? */
51 int callback_id;
52 char *cnam;
53 int r, t;
54 res = 0;
56 callback_id = callback_defined(define_font_callback);
57 if (callback_id > 0) {
58 cnam = xstrdup(cnom);
59 callback_id = run_and_save_callback(callback_id, "Sdd->", cnam, s, f);
60 free(cnam);
61 if (callback_id > 0) { /* success */
62 luaL_checkstack(Luas, 1, "out of stack space");
63 lua_rawgeti(Luas, LUA_REGISTRYINDEX, callback_id);
64 t = lua_type(Luas, -1);
65 if (t == LUA_TTABLE) {
66 res = font_from_lua(Luas, f);
67 destroy_saved_callback(callback_id);
68 /* |lua_pop(Luas, 1);| *//* done by |font_from_lua| */
69 } else if (t == LUA_TNUMBER) {
70 r = (int) lua_tointeger(Luas, -1);
71 destroy_saved_callback(callback_id);
72 delete_font(f);
73 lua_pop(Luas, 1);
74 return r;
75 } else {
76 lua_pop(Luas, 1);
77 delete_font(f);
78 return 0;
81 } else if (callback_id == 0) {
82 res = read_tfm_info(f, cnom, s);
83 if (res) {
84 set_hyphen_char(f, int_par(default_hyphen_char_code));
85 set_skew_char(f, int_par(default_skew_char_code));
88 if (font_name(f) && strlen(font_name(f)) > 255) {
89 /* the font name has to fit in the dvi file's single byte storage */
90 /* no need to test area, as we are never using it */
91 res = 0;
93 if (res) {
94 if (font_type(f) != virtual_font_type) { /* implies lua */
95 do_vf(f);
96 set_font_natural_dir(f, natural_dir);
98 return f;
99 } else {
100 delete_font(f);
101 return 0;
106 int read_font_info(pointer u, char *cnom, scaled s, int natural_dir)
108 int f;
109 char *msg;
111 f = new_font();
112 if ((f = do_define_font(f, cnom, s, natural_dir))) {
113 return f;
114 } else {
115 const char *help[] =
116 { "I wasn't able to read the size data for this font,",
117 "so I will ignore the font specification.",
118 "[Wizards can fix TFM files using TFtoPL/PLtoTF.]",
119 "You might try inserting a different font spec;",
120 "e.g., type `I\\font<same font id>=<substitute font name>'.",
121 NULL
123 if (int_par(suppress_fontnotfound_error_code) == 0) {
124 msg = font_error_message(u, cnom, s);
125 tex_error(msg, help);
126 free(msg);
128 return 0;
132 @ TODO This function is a placeholder. There can easily appears holes in
133 the |font_tables| array, and we could attempt to reuse those
136 int find_font_id(const char *nom, scaled s)
138 int f;
139 f = new_font();
140 if ((f = do_define_font(f, nom, s, -1))) {
141 return f;
142 } else {
143 return 0;