source code cleanup

svn path=/trunk/kdegraphics/kdvi/; revision=206779
This commit is contained in:
Stefan Kebekus 2003-02-13 21:28:22 +00:00
parent 295246c780
commit dc729a55bf
13 changed files with 106 additions and 102 deletions

View file

@ -50,32 +50,23 @@
*/
#include "dvi_init.h"
#include "dviwin.h"
#include <kdebug.h>
#include <klocale.h>
#include <qbitmap.h>
#include <qfileinfo.h>
#include <stdlib.h>
extern "C" {
#include "dvi.h"
}
#include "dvi_init.h"
#include "fontpool.h"
#include "glyph.h"
#include "xdvi.h"
void dvifile::process_preamble(void)
{
command_pointer = dvi_Data;
Q_UINT8 magic_number = readUINT8();
if (magic_number != PRE) {
errorMsg = i18n("The DVI file does not start with the preamble.");
@ -88,14 +79,18 @@ void dvifile::process_preamble(void)
"program, such as oxdvi.");
return;
}
numerator = readUINT32();
denominator = readUINT32();
/** numerator, denominator and the magnification value that describe
how many centimeters there are in one TeX unit, as explained in
section A.3 of the DVI driver standard, Level 0, published by
the TUG DVI driver standards committee. */
Q_UINT32 numerator = readUINT32();
Q_UINT32 denominator = readUINT32();
magnification = readUINT32();
dimconv = (((double) numerator * magnification) / ((double) denominator * 1000.0));
// @@@@ This does not fit the description of dimconv in the header file!!!
dimconv = dimconv * (((long) pixels_per_inch)<<16) / 254000;
cmPerDVIunit = (double(numerator) / double(denominator)) * (double(magnification) / 1000.0) * (1.0 / 1e5);
// Read the generatorString (such as "TeX output ..." from the
// DVI-File). The variable "magic_number" holds the length of the
// string.
@ -149,11 +144,14 @@ void dvifile::read_postamble(void)
Q_UINT8 cmnd = readUINT8();
while (cmnd >= FNTDEF1 && cmnd <= FNTDEF4) {
Q_UINT32 TeXnumber = readUINT(cmnd-FNTDEF1+1);
Q_UINT32 checksum = readUINT32();
Q_UINT32 scale = readUINT32();
Q_UINT32 design = readUINT32();
Q_UINT16 len = readUINT8() + readUINT8();
Q_UINT32 checksum = readUINT32(); // Checksum of the font, as found by TeX in the TFM file
// Read scale and design factor, and the name of the font. All
// these are explained in section A.4 of the DVI driver standard,
// Level 0, published by the TUG DVI driver standards committee
Q_UINT32 scale = readUINT32();
Q_UINT32 design = readUINT32();
Q_UINT16 len = readUINT8() + readUINT8(); // Length of the font name, including the directory name
char *fontname = new char[len + 1];
strncpy(fontname, (char *)command_pointer, len );
fontname[len] = '\0';
@ -163,17 +161,20 @@ void dvifile::read_postamble(void)
kdDebug() << "Postamble: define font \"" << fontname << "\" scale=" << scale << " design=" << design << endl;
#endif
// Calculate the fsize as: fsize = 0.001 * scale / design * magnification * MFResolutions[MetafontMode]
struct font *fontp = font_pool->appendx(fontname, checksum, scale, 0.001*scale/design*magnification*MFResolutions[font_pool->getMetafontMode()], dimconv);
// According to section A.4 of the DVI driver standard, this font
// shall be enlarged by the following factor before it is used.
double enlargement_factor = (double(scale) * double(magnification))/(double(design) * 1000.0);
struct font *fontp = font_pool->appendx(fontname, checksum, scale, enlargement_factor, cmPerDVIunit);
// Insert font in dictionary and make sure the dictionary is big
// enough.
if (tn_table.size()-2 <= tn_table.count())
// Not quite optimal. The size of the dictionary should be a
// prime. I don't care.
// prime for optimal performance. I don't care.
tn_table.resize(tn_table.size()*2);
tn_table.insert(TeXnumber, fontp);
// Read the next command
cmnd = readUINT8();
}

View file

@ -35,20 +35,8 @@ class dvifile : public bigEndianByteReader
KDVI ensures that the user is only informed once. */
bool sourceSpecialMarker;
/** Numerator and denominator of the TeX units, as explained in
section A.3 of the DVI driver standard, Level 0, published by
the TUG DVI driver standards committee. */
Q_UINT32 numerator, denominator;
/** Magnification value, as explained in section A.3 of the DVI
driver standard, Level 0, published by the TUG DVI driver
standards committee. */
Q_UINT32 magnification;
/** dimconv = numerator*magnification/(1000*denominator), as
explained in section A.3 of the DVI driver standard, Level 0,
published by the TUG DVI driver standards committee. */
double dimconv;
double cmPerDVIunit;
private:
/** process_preamble reads the information in the preamble and
@ -61,9 +49,11 @@ class dvifile : public bigEndianByteReader
void read_postamble(void);
void prepare_pages(void);
/** Offset in DVI file of last page, set in read_postamble(). */
Q_UINT32 last_page_offset;
Q_UINT32 beginning_of_postamble;
Q_UINT32 magnification;
};
#endif //ifndef _DVIFILE_H

View file

@ -44,6 +44,7 @@
#include "fontprogress.h"
#include "infodialog.h"
#include "optiondialog.h"
#include "xdvi.h"
#include "zoomlimits.h"
@ -60,8 +61,6 @@ struct drawinf currinf;
QIntDict<font> tn_table;
int _pixels_per_inch;
// The following are really used
unsigned int page_w;
unsigned int page_h;
@ -254,7 +253,6 @@ void dviWindow::setMetafontMode( unsigned int mode )
MetafontMode = font_pool->setMetafontMode(mode);
basedpi = MFResolutions[MetafontMode];
_pixels_per_inch = MFResolutions[MetafontMode]; //@@@
#ifdef DEBUG_DVIWIN
kdDebug(4300) << "basedpi " << basedpi << endl;
#endif
@ -527,7 +525,7 @@ bool dviWindow::setFile(QString fname, QString ref, bool sourceMarker)
memset((char *) &currinf.data, 0, sizeof(currinf.data));
currinf.fonttable = tn_table;
currinf._virtual = NULL;
draw_part(dviFile->dimconv, false);
draw_part(65536.0*fontPixelPerDVIunit(), false);
if (!PostScriptOutPutString->isEmpty())
PS_interface->setPostScript(current_page, *PostScriptOutPutString);

View file

@ -36,7 +36,7 @@ class KAction;
class KEdFind;
class KPrinter;
class KShellProcess;
extern const int MFResolutions[];
class DVI_Hyperlink {
public:
@ -294,7 +294,9 @@ private:
/** List of anchors in a document */
QMap<QString, DVI_Anchor> anchorList;
int basedpi;
unsigned int basedpi;
double fontPixelPerDVIunit() {return dviFile->cmPerDVIunit * MFResolutions[MetafontMode]/2.54;};
int makepk;
QPixmap *pixmap;
unsigned int MetafontMode;

View file

@ -229,7 +229,7 @@ void dviWindow::set_vf_char(unsigned int cmd, unsigned int ch)
Q_UINT8 *end_ptr_sav = end_pointer;
command_pointer = m->pos;
end_pointer = m->end;
draw_part(currinf.fontp->dimconv, true);
draw_part(currinf.fontp->x_dimconv/16.0, true);
command_pointer = command_ptr_sav;
end_pointer = end_ptr_sav;
currinf = oldinfo;
@ -343,8 +343,8 @@ void dviWindow::draw_part(double current_dimconv, bool is_vfmacro)
}
command_pointer += 11 * 4;
DVI_H = basedpi << 16; // Reminder: DVI-coordinates start at (1",1") from top of page
DVI_V = basedpi << 16;
PXL_V = pixel_conv(DVI_V);
DVI_V = basedpi;
PXL_V = int(DVI_V/shrink_factor);
WW = XX = YY = ZZ = 0;
break;
@ -445,8 +445,8 @@ void dviWindow::draw_part(double current_dimconv, bool is_vfmacro)
if (abs(DDtmp) >= 10*(currinf.fontp->scaled_size/6))
textLinkList[textLinkList.size()-1].linkText += '\n';
}
DVI_V += xspell_conv(DDtmp);
PXL_V = pixel_conv(DVI_V);
DVI_V += xspell_conv(DDtmp)/65536;
PXL_V = int(DVI_V/shrink_factor);
}
break;
@ -466,8 +466,8 @@ void dviWindow::draw_part(double current_dimconv, bool is_vfmacro)
if (abs(YYtmp) >= 10*(currinf.fontp->scaled_size/6))
textLinkList[textLinkList.size()-1].linkText += '\n';
}
DVI_V += YY;
PXL_V = pixel_conv(DVI_V);
DVI_V += YY/65536;
PXL_V = int(DVI_V/shrink_factor);
break;
case Z1:
@ -486,8 +486,8 @@ void dviWindow::draw_part(double current_dimconv, bool is_vfmacro)
if (abs(ZZtmp) >= 10*(currinf.fontp->scaled_size/6))
textLinkList[textLinkList.size()-1].linkText += '\n';
}
DVI_V += ZZ;
PXL_V = pixel_conv(DVI_V);
DVI_V += ZZ/65536;
PXL_V = int(DVI_V/shrink_factor);
break;
case FNT1:
@ -596,7 +596,7 @@ void dviWindow::draw_page(void)
memset((char *) &currinf.data, 0, sizeof(currinf.data));
currinf.fonttable = tn_table;
currinf._virtual = 0;
draw_part(dviFile->dimconv, false);
draw_part(65536.0*fontPixelPerDVIunit(), false);
if (HTML_href != 0) {
delete HTML_href;
HTML_href = 0;

View file

@ -77,21 +77,25 @@ void font::fontNameReceiver(QString fname)
}
font::font(char *nfontname, float nfsize, long chk, Q_INT32 scale, double dconv, class fontPool *pool, float shrinkFact)
font::font(const char *nfontname, double resolution_in_dpi, long chk, Q_INT32 scale, double pixelPerDVIunit, class fontPool *pool, double shrinkFact,
double _enlargement, double _cmPerDVIunit)
{
#ifdef DEBUG_FONT
kdDebug() << "constructing font " << nfontname << " at " << (int) (nfsize + 0.5) << " dpi" << endl;
kdDebug() << "constructing font " << nfontname << " at " << (int) (resolution_in_dpi + 0.5) << " dpi" << endl;
#endif
shrinkFactor = shrinkFact;
enlargement = _enlargement;
cmPerDVIunit = _cmPerDVIunit;
font_pool = pool;
fontname = nfontname;
fsize = nfsize;
naturalResolution_in_dpi = resolution_in_dpi;
checksum = chk;
flags = font::FONT_IN_USE;
file = NULL;
filename = "";
dimconv = dconv;
x_dimconv = scale*pixelPerDVIunit;
scaled_size = scale;
glyphtable = 0;
@ -111,7 +115,7 @@ font::font(char *nfontname, float nfsize, long chk, Q_INT32 scale, double dconv,
font::~font()
{
#ifdef DEBUG_FONT
kdDebug() << "discarding font " << fontname << " at " << (int)(fsize + 0.5) << " dpi" << endl;
kdDebug() << "discarding font " << fontname << " at " << (int)(naturalResolution_in_dpi + 0.5) << " dpi" << endl;
#endif
if (fontname != 0)
@ -133,7 +137,8 @@ font::~font()
}
}
void font::setShrinkFactor(float sf)
void font::setShrinkFactor(float sf)
{
shrinkFactor = sf;
@ -145,13 +150,14 @@ void font::setShrinkFactor(float sf)
}
}
/** mark_as_used marks the font, and all the fonts it referrs to, as
used, i.e. their FONT_IN_USE-flag is set. */
void font::mark_as_used(void)
{
#ifdef DEBUG_FONT
kdDebug() << "marking font " << fontname << " at " << (int) (fsize + 0.5) << " dpi" << endl;
kdDebug() << "marking font " << fontname << " at " << (int) (naturalResolution_in_dpi + 0.5) << " dpi" << endl;
#endif
if (flags & font::FONT_IN_USE)
@ -172,6 +178,7 @@ void font::mark_as_used(void)
QPixmap font::characterPixmap(unsigned int ch)
{
// Paranoia checks
if (ch > max_num_of_chars_in_font) {
kdError(4300) << "Tried to access character with number " << ch << endl;
return nullPixmap;
@ -184,10 +191,9 @@ QPixmap font::characterPixmap(unsigned int ch)
return nullPixmap;
}
// Otherwise, we rescale the bitmap in order to produce the required
// pixmap. Rescaling a character, however, is an art that requires
// some explanation...
// All is fine? Then we rescale the bitmap in order to produce the
// required pixmap. Rescaling a character, however, is an art that
// requires some explanation...
//
// If we would just divide the size of the character and the
// coordinates by the shrink factor, then the result would look

18
font.h
View file

@ -26,9 +26,6 @@ typedef void (dviWindow::*set_char_proc)(unsigned int, unsigned int);
#include "dviwin.h"
#include "glyph.h"
class QPixmap;
//#define NOMAGSTP (-29999)
// Per character information for virtual fonts
@ -62,7 +59,8 @@ public:
};
font(char *nfontname, float nfsize, long chk, Q_INT32 scale, double dconv, class fontPool *pool, float shrinkFact);
font(const char *nfontname, double resolution_in_dpi, long chk, Q_INT32 scale, double pixelsPerDVIunit, class fontPool *pool, double shrinkFact,
double _enlargement, double _cmPerDVIunit);
~font();
@ -75,12 +73,15 @@ public:
void mark_as_used(void);
class fontPool *font_pool; // Pointer to the pool that contains this font.
char *fontname; // name of font, such as "cmr10"
const char *fontname; // name of font, such as "cmr10"
unsigned char flags; // flags byte (see values below)
double dimconv; // size conversion factor
double x_dimconv; // size conversion factor
Q_INT32 scaled_size; // Scaled size from the font definition command; in DVI units
set_char_proc set_char_p; // proc used to set char
float fsize; // size information (dots per inch)
// Resolution at which this font was rendered by MetaFont
double naturalResolution_in_dpi;
FILE *file; // open font file or NULL
QString filename; // name of font file
@ -95,6 +96,9 @@ private:
QPixmap nullPixmap;
QPixmap *characterPixmaps[max_num_of_chars_in_font];
// FIND OUT ABOUT THOSE! @@@@@@@@@@@@@@@@@@@@@@
double enlargement;
double cmPerDVIunit;
long checksum; // checksum
// Functions related to virtual fonts

View file

@ -15,6 +15,7 @@
#include "font.h"
#include "fontpool.h"
#include "fontprogress.h"
#include "xdvi.h"
// List of permissible MetaFontModes which are supported by kdvi.
@ -110,13 +111,16 @@ void fontPool::setEnlargeFonts( bool flag )
}
class font *fontPool::appendx(char *fontname, long checksum, Q_INT32 scale, float fsize, double scale_dimconv)
class font *fontPool::appendx(const char *fontname, long checksum, Q_INT32 scale, double enlargement, double cmPerDVIunit)
{
// Reuse font if possible: check if a font with that name and fsize
// is already in the fontpool, and use that, if possible.
double fsize = enlargement * MFResolutions[getMetafontMode()];
// Reuse font if possible: check if a font with that name and
// natural resolution is already in the fontpool, and use that, if
// possible.
class font *fontp = fontList.first();
while( fontp != 0 ) {
if (strcmp(fontname, fontp->fontname) == 0 && (int (fsize+0.5)) == (int)(fontp->fsize + 0.5)) {
if (strcmp(fontname, fontp->fontname) == 0 && (int (fsize+0.5)) == (int)(fontp->naturalResolution_in_dpi + 0.5)) {
// if font is already in the list
fontp->mark_as_used();
delete [] fontname;
@ -126,8 +130,12 @@ class font *fontPool::appendx(char *fontname, long checksum, Q_INT32 scale, floa
}
// If font doesn't exist yet, we have to generate a new font.
fontp = new font(fontname, fsize, checksum, scale, scale*scale_dimconv/(1<<20), this,
(enlargeFonts == true) ? shrinkFactor/1.1 : shrinkFactor);
// Calculate the number of pixel per DVI unit
double pixelPerDVIunit = cmPerDVIunit * MFResolutions[getMetafontMode()] / 2.54;
fontp = new font(fontname, fsize, checksum, scale, pixelPerDVIunit, this,
(enlargeFonts == true) ? shrinkFactor/1.1 : shrinkFactor, enlargement, cmPerDVIunit);
if (fontp == 0) {
kdError(4300) << i18n("Could not allocate memory for a font structure!") << endl;
exit(0);
@ -159,7 +167,7 @@ QString fontPool::status(void)
else
type = i18n("regular");
tmp << QString ("<tr><td>%1</td> <td>%2</td> <td>%3</td> <td>%4</td></tr>").arg(fontp->fontname).arg((int)(fontp->fsize+0.5)).arg(type).arg(fontp->filename);
tmp << QString ("<tr><td>%1</td> <td>%2</td> <td>%3</td> <td>%4</td></tr>").arg(fontp->fontname).arg((int)(fontp->naturalResolution_in_dpi+0.5)).arg(type).arg(fontp->filename);
fontp=fontList.next();
}
@ -249,7 +257,7 @@ int fontPool::check_if_fonts_are_loaded(unsigned char pass)
while ( fontp != 0 ) {
if ((fontp->flags & font::FONT_KPSE_NAME) == 0) {
numFontsInJob++;
*proc << KShellProcess::quote(QString("%2.%1pk").arg((int)(fontp->fsize + 0.5)).arg(fontp->fontname));
*proc << KShellProcess::quote(QString("%2.%1pk").arg((int)(fontp->naturalResolution_in_dpi + 0.5)).arg(fontp->fontname));
// In the first pass, we look also for virtual fonts.
if (pass == 0)
*proc << KShellProcess::quote(QString("%1.vf").arg(fontp->fontname));
@ -310,7 +318,7 @@ void fontPool::kpsewhich_terminated(KProcess *)
class font *fontp=fontList.first();
while ( fontp != 0 ) {
if (fontp->filename.isEmpty() == true) {
QString fontname = QString("%1.%2pk").arg(fontp->fontname).arg((int)(fontp->fsize + 0.5));
QString fontname = QString("%1.%2pk").arg(fontp->fontname).arg((int)(fontp->naturalResolution_in_dpi + 0.5));
QStringList matchingFiles = fileNameList.grep(fontname);
if (matchingFiles.isEmpty() != true) {
#ifdef DEBUG_FONTPOOL

View file

@ -84,8 +84,8 @@ Q_OBJECT
currently loaded, it's file will be located and font::load_font
will be called. Since this is done using a concurrently running
process, there is no guarantee that the loading is already
performed when the method returns. */
class font *appendx(char *fontname, long checksum, Q_INT32 scale, float fsize, double scale_dimconv);
performed when the method returns. */
class font *appendx(const char *fontname, long checksum, Q_INT32 scale, double enlargement, double cmPerDVIunit);
/** Prints very basic debugging information about the fonts in the
pool to the kdDebug output stream. */

2
pk.cpp
View file

@ -270,7 +270,7 @@ void font::read_PK_char(unsigned int ch)
g->x = snum(fp, n);
g->y = snum(fp, n);
g->dvi_adv = (int)(dimconv * fpwidth + 0.5);
g->dvi_adv = (int)(x_dimconv/16.0 * fpwidth + 0.5);
alloc_bitmap(&g->bitmap);
cp = (BMUNIT *) g->bitmap.bits;

View file

@ -30,7 +30,7 @@ void dviWindow::html_anchor_special(QString cp)
kdDebug(4300) << "page " << current_page << endl;
#endif
anchorList[cp] = DVI_Anchor(current_page, DVI_V/65536);
anchorList[cp] = DVI_Anchor(current_page, DVI_V);
}
}
@ -95,7 +95,7 @@ void dviWindow::source_special(QString cp)
break;
Q_UINT32 sourceLineNumber = cp.left(j).toUInt();
QString sourceFileName = QFileInfo(cp.mid(j).stripWhiteSpace()).absFilePath();
DVI_SourceFileAnchor sfa(sourceFileName, sourceLineNumber, current_page, DVI_V/65536);
DVI_SourceFileAnchor sfa(sourceFileName, sourceLineNumber, current_page, DVI_V);
sourceHyperLinkAnchors.push_back(sfa);
}
}
@ -190,7 +190,7 @@ void dviWindow::epsf_special(QString cp)
if (PostScriptOutPutString) {
if (QFile::exists(EPSfilename)) {
double PS_H = (DVI_H*300.0)/(65536*basedpi)-300;
double PS_V = (DVI_V*300.0)/(65536*basedpi)-300;
double PS_V = (DVI_V*300.0)/basedpi - 300;
PostScriptOutPutString->append( QString(" %1 %2 moveto\n").arg(PS_H).arg(PS_V) );
PostScriptOutPutString->append( "@beginspecial " );
PostScriptOutPutString->append( QString(" %1 @llx").arg(llx) );
@ -223,8 +223,8 @@ void dviWindow::epsf_special(QString cp)
bbox_height = rhi;
}
bbox_width *= 0.1 * dviFile->dimconv / shrink_factor;
bbox_height *= 0.1 * dviFile->dimconv / shrink_factor;
bbox_width *= 0.1 * 65536.0*fontPixelPerDVIunit() / shrink_factor;
bbox_height *= 0.1 * 65536.0*fontPixelPerDVIunit() / shrink_factor;
QRect bbox(PXL_H - currwin.base_x, PXL_V - currwin.base_y - (int)bbox_height, (int)bbox_width, (int)bbox_height);
foreGroundPaint.save();
@ -266,7 +266,7 @@ void dviWindow::quote_special(QString cp)
if (currwin.win == mane.win && PostScriptOutPutString) {
double PS_H = (DVI_H*300.0)/(65536*basedpi)-300;
double PS_V = (DVI_V*300.0)/(65536*basedpi)-300;
double PS_V = (DVI_V*300.0)/basedpi - 300;
PostScriptOutPutString->append( QString(" %1 %2 moveto\n").arg(PS_H).arg(PS_V) );
PostScriptOutPutString->append( " @beginspecial @setspecial \n" );
PostScriptOutPutString->append( cp );
@ -282,7 +282,7 @@ void dviWindow::ps_special(QString cp)
if (currwin.win == mane.win && PostScriptOutPutString) {
double PS_H = (DVI_H*300.0)/(65536*basedpi)-300;
double PS_V = (DVI_V*300.0)/(65536*basedpi)-300;
double PS_V = (DVI_V*300.0)/basedpi - 300;
if (cp.find("ps::[begin]", 0, false) == 0) {
PostScriptOutPutString->append( QString(" %1 %2 moveto\n").arg(PS_H).arg(PS_V) );

7
vf.cpp
View file

@ -106,8 +106,9 @@ void font::read_VF_index(void)
// by vfparent->dimconv. The design size is given in units of 2
// -20 pt, so we convert into SPELL units by multiplying by
// (pixels_per_inch * 2**16) / (72.27 * 2**20).
struct font *newfontp = font_pool->appendx(fontname, checksum, scale,
(72.27 * (1<<4)) * dimconv * scale / design, dimconv);
// @@@@@@@@@@@@@@ CHECK THAT @@@@@@@@@@@@@@@@@@@@@@@ THIS CALL OF appendx PROBABLY HAS A WRONG enlargement. ORIGINAL CODE WAS:
// struct font *newfontp = font_pool->appendx(fontname, checksum, scale, (72.27 * (1<<4)) * dimconv * scale / design, dimconv);
struct font *newfontp = font_pool->appendx(fontname, checksum, scale, enlargement, cmPerDVIunit);
// Insert font in dictionary and make sure the dictionary is big
// enough.
@ -152,7 +153,7 @@ void font::read_VF_index(void)
width = num(VF_file, 3);
}
m = &macrotable[cc];
m->dvi_adv = (int)(width * dimconv + 0.5);
m->dvi_adv = (int)(width * x_dimconv/16.0 + 0.5);
if (len > 0) {
if (len <= availend - avail) {
m->pos = avail;

8
xdvi.h
View file

@ -16,7 +16,7 @@
#define pixel_conv(x) ((int) ((x) / (shrink_factor * 65536)))
#define pixel_round(x) ((int) ROUNDUP(x, shrink_factor * 65536))
#define spell_conv0(n, f) ((long) (n * f))
#define spell_conv(n) spell_conv0(n, dimconv)
#ifdef BMBYTE
#define BMUNIT unsigned char
@ -67,14 +67,8 @@ extern QIntDict<struct font> tn_table;
* Command line flags.
*/
extern int _pixels_per_inch;
extern unsigned long num (FILE *, int);
extern long snum(FILE *, int);
#define pixels_per_inch _pixels_per_inch
extern struct WindowRec mane, currwin;
#define shrink_factor currwin.shrinkfactor