compile
[kdegraphics.git] / okular / generators / dvi / vf.cpp
blob8cc59dd69f9420d4648e34d6ffc7e55d56b124bd
1 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; c-brace-offset: 0; -*-
2 /*
3 * Copyright (c) 1994 Paul Vojta. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
27 #include <config.h>
29 #include "dvi.h"
30 #include "dviRenderer.h"
31 #include "kvs_debug.h"
32 #include "xdvi.h"
34 #include <klocale.h>
36 #include <cstdio>
37 #include <cstdlib>
38 #include <cstring>
40 extern void oops(const QString& message);
42 /***
43 *** VF font reading routines.
44 *** Public routine is read_index---because virtual characters are presumed
45 *** to be short, we read the whole virtual font in at once, instead of
46 *** faulting in characters as needed.
47 ***/
49 #define LONG_CHAR 242
52 * These are parameters which determine whether macros are combined for
53 * storage allocation purposes. Small macros ( <= VF_PARM_1 bytes) are
54 * combined into chunks of size VF_PARM_2.
57 #ifndef VF_PARM_1
58 #define VF_PARM_1 20
59 #endif
60 #ifndef VF_PARM_2
61 #define VF_PARM_2 256
62 #endif
65 * The main routine
68 void TeXFontDefinition::read_VF_index()
70 #ifdef DEBUG_FONTS
71 kDebug(kvs::dvi) << "font::read_VF_index()";
72 #endif
73 FILE *VF_file = file;
74 unsigned char cmnd;
75 // available space for macros
76 unsigned char *avail, *availend;
78 flags |= FONT_VIRTUAL;
79 set_char_p = &dviRenderer::set_vf_char;
80 #ifdef DEBUG_FONTS
81 kDebug(kvs::dvi) << "TeXFontDefinition::read_VF_index: reading VF pixel file " << filename;
82 #endif
83 // Read preamble.
84 fseek(VF_file, (long) one(VF_file), 1); /* skip comment */
85 quint32 const file_checksum = four(VF_file);
87 if (file_checksum && checksum && file_checksum != checksum)
88 kError(kvs::dvi) << "Checksum mismatch dvi = " << checksum << "u, vf = " << file_checksum <<
89 "u) in font file" << filename << endl;
90 (void) four(VF_file); /* skip design size */
92 // Read the fonts.
93 first_font = NULL;
94 while ((cmnd = one(VF_file)) >= FNTDEF1 && cmnd <= FNTDEF4) {
95 int TeXnumber = num(VF_file, (int) cmnd - FNTDEF1 + 1);
96 quint32 checksum = four(VF_file);
97 quint32 scale = four(VF_file);
98 quint32 design = four(VF_file);
99 Q_UNUSED(design);
100 quint16 len = one(VF_file) + one(VF_file); /* sequence point in the middle */
101 char *fontname = new char[len + 1];
102 fread(fontname, sizeof(char), len, VF_file);
103 fontname[len] = '\0';
105 #ifdef DEBUG_FONTS
106 kDebug(kvs::dvi) << "Virtual font defines subfont \"" << fontname << "\" scale=" << scale << " design=" << design;
107 #endif
109 // According to Knuth's documentation found in the web source code
110 // of the "vftovp" program (which seems to be the standard
111 // definition of virtual fonts), the "scale" is a fixed point
112 // number which describes extra enlargement that the virtual font
113 // imposes. One obtains the enlargement by dividing 2^20.
114 double enlargement_factor = double(scale)/(1<<20) * enlargement;
116 // TeXFontDefinition *newfontp = font_pool->appendx(fontname, checksum, (quint32)(scaled_size_in_DVI_units*enlargement_factor), enlargement_factor);
117 TeXFontDefinition *newfontp = font_pool->appendx(fontname, checksum, (quint32)((double(scale)/(1<<20))*scaled_size_in_DVI_units), enlargement_factor);
119 // Insert font in dictionary and make sure the dictionary is big
120 // enough.
121 if (vf_table.size()-2 <= vf_table.count())
122 // Not quite optimal. The size of the dictionary should be a
123 // prime. I don't care.
124 vf_table.resize(vf_table.size()*2);
125 vf_table.insert(TeXnumber, newfontp);
127 if (first_font == NULL)
128 first_font = newfontp;
131 // Prepare macro array.
132 macrotable = new macro[max_num_of_chars_in_font];
133 if (macrotable == 0) {
134 kError(kvs::dvi) << "Could not allocate memory for a macro table.";
135 exit(0);
138 // Read macros.
139 avail = availend = NULL;
140 for (; cmnd <= LONG_CHAR; cmnd = one(VF_file)) {
141 macro *m;
142 int len;
143 unsigned long cc;
144 long width;
146 if (cmnd == LONG_CHAR) { /* long form packet */
147 len = four(VF_file);
148 cc = four(VF_file);
149 width = four(VF_file);
150 if (cc >= 256) {
151 kError(kvs::dvi) << "Virtual character" << cc << "in font"
152 << fontname << "ignored.";
153 fseek(VF_file, (long) len, 1);
154 continue;
156 } else { /* short form packet */
157 len = cmnd;
158 cc = one(VF_file);
159 width = num(VF_file, 3);
161 m = &macrotable[cc];
163 m->dvi_advance_in_units_of_design_size_by_2e20 = width;
164 if (len > 0) {
165 if (len <= availend - avail) {
166 m->pos = avail;
167 avail += len;
168 } else {
169 m->free_me = true;
170 if (len <= VF_PARM_1) {
171 m->pos = avail = new unsigned char [VF_PARM_2];
172 availend = avail + VF_PARM_2;
173 avail += len;
174 } else
175 m->pos = new unsigned char[len];
177 fread((char *) m->pos, 1, len, VF_file);
178 m->end = m->pos + len;
181 if (cmnd != POST)
182 oops(i18n("Wrong command byte found in VF macro list: %1", cmnd));
184 fclose (VF_file);
185 file = NULL;