beta-0.89.2
[luatex.git] / source / texk / web2c / luatexdir / pdf / pdffont.w
blobb3f1605154ca95e9cdcd8c9f2155f2f53fa5ccb1
1 % pdffont.w
3 % Copyright 2009-2014 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 \def\pdfTeX{pdf\TeX}
22 @ @c
24 #include "ptexlib.h"
26 @ As \pdfTeX{} should also act as a back-end driver, it needs to support virtual
27 fonts too. Information about virtual fonts can be found in the source of some
28 \.{DVI}-related programs.
30 Whenever we want to write out a character in a font to PDF output, we
31 should check whether the used character is a virtual or real character.
32 The |has_packet()| C macro checks for this condition.
34 @ The following code typesets a character to PDF output
37 scaled_whd output_one_char(PDF pdf, halfword p)
39 internal_font_number f = font(p);
40 int c = character(p);
41 int ex_glyph = ex_glyph(p)/1000;
42 scaled_whd ci = get_charinfo_whd(f, c); /* the real width, height and depth of the character */
43 if (!(char_exists(f,c))) {
44 char_warning(f,c);
45 return ci;
47 ci.wd = ext_xn_over_d(ci.wd, 1000000 + ex_glyph(p), 1000000);
48 switch (pdf->posstruct->dir) {
49 case dir_TLT:
50 break;
51 case dir_TRT:
52 pos_left(ci.wd);
53 break;
54 case dir_LTL:
55 pos_down(ci.ht);
56 pos_left(ci.wd);
57 break;
58 case dir_RTT:
59 pos_down(ci.ht);
60 pos_left(ci.wd / 2);
61 break;
62 default:
63 formatted_warning("pdf backend","ignoring bad dir %i when outputting a character",pdf->posstruct->dir);
65 if (has_packet(f, c)) {
66 do_vf_packet(pdf, f, c, ex_glyph);
67 } else {
68 /* |pdf_place_glyph(pdf, f, c, ex_glyph);| */
69 backend_out[glyph_node] (pdf, f, c, ex_glyph);
71 return ci;
74 @ Mark |f| as a used font; set |font_used(f)|, |font_size(f)| and |pdf_font_num(f)|
76 static void pdf_use_font(internal_font_number f, int fontnum)
78 set_font_used(f, true);
79 if ((fontnum > 0) || ((fontnum < 0) && (pdf_font_num(-fontnum) > 0))) {
80 set_pdf_font_num(f, fontnum);
81 } else {
82 normal_error("pdf backend","bad font id");
86 @ To set PDF font we need to find out fonts with the same name, because \TeX\ can
87 load the same font several times for various sizes. For such fonts we define only
88 one font resource. The array |pdf_font_num| holds the object number of font
89 resource. A negative value of an entry of |pdf_font_num| indicates that the
90 corresponding font shares the font resource with the font
93 #define same(n,f,k) (n(f) != NULL && n(k) != NULL && strcmp(n(f), n(k)) == 0)
96 For some lua-loaded (for instance AFM) fonts, it is normal to have
97 a zero cidregistry, and such fonts do not have a fontmap entry yet
98 at this point, so the test should use the other branch
101 static boolean font_shareable(internal_font_number f, internal_font_number k)
103 if (font_cidregistry(f) == NULL && font_cidregistry(k) == NULL && font_encodingbytes(f) != 2 && font_encodingbytes(k) != 2) {
104 if (font_map(k) != NULL && font_map(f) != NULL && (same(font_name, k, f))) {
105 return 1;
107 } else if ((same(font_filename, k, f) && same(font_fullname, k, f))) {
108 return 1;
110 return 0;
113 @ create a font object
115 void pdf_init_font(PDF pdf, internal_font_number f)
117 internal_font_number k;
118 fm_entry *fm;
119 int i, l;
120 if (font_used(f)) {
121 formatted_error("pdf backend","font %i gets initialized twice",(int) f);
124 check whether |f| can share the font object with some |k|: we have 2 cases
125 here: 1) |f| and |k| have the same tfm name (so they have been loaded at
126 different sizes, eg 'cmr10' and 'cmr10 at 11pt'); 2) |f| has been auto
127 expanded from |k|
129 take over slant and extend from map entry, if not already set;
130 this should also be the only place where getfontmap() may be called.
132 fm = getfontmap(font_name(f));
133 if (font_map(f) == NULL && fm != NULL) {
134 font_map(f) = fm;
135 if (is_slantset(fm))
136 font_slant(f) = fm->slant;
137 if (is_extendset(fm))
138 font_extend(f) = fm->extend;
140 i = pdf->head_tab[obj_type_font];
141 while (i != 0) {
142 k = obj_info(pdf, i);
143 if (font_shareable(f, k)) {
144 if (pdf_font_num(k) < 0)
145 pdf_use_font(f, pdf_font_num(k));
146 else
147 pdf_use_font(f, -k);
148 return;
150 i = obj_link(pdf, i);
152 /* create a new font object for |f| */
153 l = pdf_create_obj(pdf, obj_type_font, f);
154 pdf_use_font(f, l);
157 @ set the actual font on PDF page; sets |ff| to the tfm number of the base font
158 sharing the font object with |f|; |ff| is either |f| itself (then it is its own
159 base font), or some font with the same tfm name at different size and/or
160 expansion.
163 internal_font_number pdf_set_font(PDF pdf, internal_font_number f)
165 int ff; /* for use with |set_ff| */
166 if (!font_used(f))
167 pdf_init_font(pdf, f);
168 ff = pdf_font_num(f) < 0 ? -pdf_font_num(f) : f; /* aka |set_ff(f)| */
169 addto_page_resources(pdf, obj_type_font, pdf_font_num(ff));
170 return ff;
173 @ @c
174 void pdf_include_chars(PDF pdf)
176 str_number s;
177 unsigned char *k, *j; /* running index */
178 internal_font_number f;
179 scan_font_ident();
180 f = cur_val;
181 if (f == null_font)
182 normal_error("pdf backend", "invalid font identifier for 'includechars'");
183 pdf_check_vf(cur_val);
184 if (!font_used(f))
185 pdf_init_font(pdf, f);
186 scan_toks(false, true);
187 s = tokens_to_string(def_ref);
188 delete_token_ref(def_ref);
189 j = str_string(s) + str_length(s);
190 for (k = str_string(s); k < j; k++) {
191 pdf_mark_char(f, *k);
193 flush_str(s);