📄 special.cpp.svn-base
字号:
// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; c-brace-offset: 0; -*-// special.cpp// Methods for dviRenderer which deal with "\special" commands found in the// DVI file// Copyright 2000--2004, Stefan Kebekus (kebekus@kde.org).#include <config.h>#include "dviFile.h"#include "dviRenderer.h"#include "hyperlink.h"#include "kvs_debug.h"#include "psgs.h"//#include "renderedDocumentPage.h"#include <klocale.h>#include <kmimetype.h>#include <QFile>#include <QImage>#include <QPainter>#include <QStringList>//#define DEBUG_SPECIALextern QPainter *foreGroundPainter;void dviRenderer::printErrorMsgForSpecials(const QString& msg){ if (dviFile->errorCounter < 25) { kError(kvs::dvi) << msg << endl; dviFile->errorCounter++; if (dviFile->errorCounter == 25) kError(kvs::dvi) << i18n("That makes 25 errors. Further error messages will not be printed.") << endl; }}// Parses a color specification, as explained in the manual to// dvips. If the spec could not be parsed, an invalid color will be// returned.QColor dviRenderer::parseColorSpecification(const QString& colorSpec){ // Initialize the map of known colors, if that is not done yet. if (namedColors.isEmpty()) { namedColors["Red"] = QColor( (int)(255.0*1), (int)(255.0*0), (int)(255.0*0)); namedColors["Tan"] = QColor( (int)(255.0*0.86), (int)(255.0*0.58), (int)(255.0*0.44)); namedColors["Blue"] = QColor( (int)(255.0*0), (int)(255.0*0), (int)(255.0*1)); namedColors["Cyan"] = QColor( (int)(255.0*0), (int)(255.0*1), (int)(255.0*1)); namedColors["Gray"] = QColor( (int)(255.0*0.5), (int)(255.0*0.5), (int)(255.0*0.5)); namedColors["Plum"] = QColor( (int)(255.0*0.5), (int)(255.0*0), (int)(255.0*1)); namedColors["Black"] = QColor( (int)(255.0*0), (int)(255.0*0), (int)(255.0*0)); namedColors["Brown"] = QColor( (int)(255.0*0.4), (int)(255.0*0), (int)(255.0*0)); namedColors["Green"] = QColor( (int)(255.0*0), (int)(255.0*1), (int)(255.0*0)); namedColors["Melon"] = QColor( (int)(255.0*1), (int)(255.0*0.54), (int)(255.0*0.5)); namedColors["Peach"] = QColor( (int)(255.0*1), (int)(255.0*0.5), (int)(255.0*0.3)); namedColors["Sepia"] = QColor( (int)(255.0*0.3), (int)(255.0*0), (int)(255.0*0)); namedColors["White"] = QColor( (int)(255.0*1), (int)(255.0*1), (int)(255.0*1)); namedColors["Maroon"] = QColor( (int)(255.0*0.68), (int)(255.0*0), (int)(255.0*0)); namedColors["Orange"] = QColor( (int)(255.0*1), (int)(255.0*0.39), (int)(255.0*0.13)); namedColors["Orchid"] = QColor( (int)(255.0*0.68), (int)(255.0*0.36), (int)(255.0*1)); namedColors["Purple"] = QColor( (int)(255.0*0.55), (int)(255.0*0.14), (int)(255.0*1)); namedColors["Salmon"] = QColor( (int)(255.0*1), (int)(255.0*0.47), (int)(255.0*0.62)); namedColors["Violet"] = QColor( (int)(255.0*0.21), (int)(255.0*0.12), (int)(255.0*1)); namedColors["Yellow"] = QColor( (int)(255.0*1), (int)(255.0*1), (int)(255.0*0)); namedColors["Apricot"] = QColor( (int)(255.0*1), (int)(255.0*0.68), (int)(255.0*0.48)); namedColors["Emerald"] = QColor( (int)(255.0*0), (int)(255.0*1), (int)(255.0*0.5)); namedColors["Fuchsia"] = QColor( (int)(255.0*0.45), (int)(255.0*0.01), (int)(255.0*0.92)); namedColors["Magenta"] = QColor( (int)(255.0*1), (int)(255.0*0), (int)(255.0*1)); namedColors["SkyBlue"] = QColor( (int)(255.0*0.38), (int)(255.0*1), (int)(255.0*0.88)); namedColors["Thistle"] = QColor( (int)(255.0*0.88), (int)(255.0*0.41), (int)(255.0*1)); namedColors["BrickRed"] = QColor( (int)(255.0*0.72), (int)(255.0*0), (int)(255.0*0)); namedColors["Cerulean"] = QColor( (int)(255.0*0.06), (int)(255.0*0.89), (int)(255.0*1)); namedColors["Lavender"] = QColor( (int)(255.0*1), (int)(255.0*0.52), (int)(255.0*1)); namedColors["Mahogany"] = QColor( (int)(255.0*0.65), (int)(255.0*0), (int)(255.0*0)); namedColors["Mulberry"] = QColor( (int)(255.0*0.64), (int)(255.0*0.08), (int)(255.0*0.98)); namedColors["NavyBlue"] = QColor( (int)(255.0*0.06), (int)(255.0*0.46), (int)(255.0*1)); namedColors["SeaGreen"] = QColor( (int)(255.0*0.31), (int)(255.0*1), (int)(255.0*0.5)); namedColors["TealBlue"] = QColor( (int)(255.0*0.12), (int)(255.0*0.98), (int)(255.0*0.64)); namedColors["BlueGreen"] = QColor( (int)(255.0*0.15), (int)(255.0*1), (int)(255.0*0.67)); namedColors["CadetBlue"] = QColor( (int)(255.0*0.38), (int)(255.0*0.43), (int)(255.0*0.77)); namedColors["Dandelion"] = QColor( (int)(255.0*1), (int)(255.0*0.71), (int)(255.0*0.16)); namedColors["Goldenrod"] = QColor( (int)(255.0*1), (int)(255.0*0.9), (int)(255.0*0.16)); namedColors["LimeGreen"] = QColor( (int)(255.0*0.5), (int)(255.0*1), (int)(255.0*0)); namedColors["OrangeRed"] = QColor( (int)(255.0*1), (int)(255.0*0), (int)(255.0*0.5)); namedColors["PineGreen"] = QColor( (int)(255.0*0), (int)(255.0*0.75), (int)(255.0*0.16)); namedColors["RawSienna"] = QColor( (int)(255.0*0.55), (int)(255.0*0), (int)(255.0*0)); namedColors["RedOrange"] = QColor( (int)(255.0*1), (int)(255.0*0.23), (int)(255.0*0.13)); namedColors["RedViolet"] = QColor( (int)(255.0*0.59), (int)(255.0*0), (int)(255.0*0.66)); namedColors["Rhodamine"] = QColor( (int)(255.0*1), (int)(255.0*0.18), (int)(255.0*1)); namedColors["RoyalBlue"] = QColor( (int)(255.0*0), (int)(255.0*0.5), (int)(255.0*1)); namedColors["RubineRed"] = QColor( (int)(255.0*1), (int)(255.0*0), (int)(255.0*0.87)); namedColors["Turquoise"] = QColor( (int)(255.0*0.15), (int)(255.0*1), (int)(255.0*0.8)); namedColors["VioletRed"] = QColor( (int)(255.0*1), (int)(255.0*0.19), (int)(255.0*1)); namedColors["Aquamarine"] = QColor( (int)(255.0*0.18), (int)(255.0*1), (int)(255.0*0.7)); namedColors["BlueViolet"] = QColor( (int)(255.0*0.1), (int)(255.0*0.05), (int)(255.0*0.96)); namedColors["DarkOrchid"] = QColor( (int)(255.0*0.6), (int)(255.0*0.2), (int)(255.0*0.8)); namedColors["OliveGreen"] = QColor( (int)(255.0*0), (int)(255.0*0.6), (int)(255.0*0)); namedColors["Periwinkle"] = QColor( (int)(255.0*0.43), (int)(255.0*0.45), (int)(255.0*1)); namedColors["Bittersweet"] = QColor( (int)(255.0*0.76), (int)(255.0*0.01), (int)(255.0*0)); namedColors["BurntOrange"] = QColor( (int)(255.0*1), (int)(255.0*0.49), (int)(255.0*0)); namedColors["ForestGreen"] = QColor( (int)(255.0*0), (int)(255.0*0.88), (int)(255.0*0)); namedColors["GreenYellow"] = QColor( (int)(255.0*0.85), (int)(255.0*1), (int)(255.0*0.31)); namedColors["JungleGreen"] = QColor( (int)(255.0*0.01), (int)(255.0*1), (int)(255.0*0.48)); namedColors["ProcessBlue"] = QColor( (int)(255.0*0.04), (int)(255.0*1), (int)(255.0*1)); namedColors["RoyalPurple"] = QColor( (int)(255.0*0.25), (int)(255.0*0.1), (int)(255.0*1)); namedColors["SpringGreen"] = QColor( (int)(255.0*0.74), (int)(255.0*1), (int)(255.0*0.24)); namedColors["YellowGreen"] = QColor( (int)(255.0*0.56), (int)(255.0*1), (int)(255.0*0.26)); namedColors["MidnightBlue"] = QColor( (int)(255.0*0), (int)(255.0*0.44), (int)(255.0*0.57)); namedColors["YellowOrange"] = QColor( (int)(255.0*1), (int)(255.0*0.58), (int)(255.0*0)); namedColors["CarnationPink"] = QColor( (int)(255.0*1), (int)(255.0*0.37), (int)(255.0*1)); namedColors["CornflowerBlue"] = QColor( (int)(255.0*0.35), (int)(255.0*0.87), (int)(255.0*1)); namedColors["WildStrawberry"] = QColor( (int)(255.0*1), (int)(255.0*0.04), (int)(255.0*0.61)); } QString specType = colorSpec.section(' ', 0, 0); if (specType.indexOf("rgb", 0, Qt::CaseInsensitive) == 0) { bool ok; double r = colorSpec.section(' ', 1, 1).toDouble(&ok); if ((ok == false) || (r < 0.0) || (r > 1.0)) return QColor(); double g = colorSpec.section(' ', 2, 2).toDouble(&ok); if ((ok == false) || (g < 0.0) || (g > 1.0)) return QColor(); double b = colorSpec.section(' ', 3, 3).toDouble(&ok); if ((ok == false) || (b < 0.0) || (b > 1.0)) return QColor(); return QColor((int)(r*255.0+0.5), (int)(g*255.0+0.5), (int)(b*255.0+0.5)); } if (specType.indexOf("hsb", 0, Qt::CaseInsensitive) == 0) { bool ok; double h = colorSpec.section(' ', 1, 1).toDouble(&ok); if ((ok == false) || (h < 0.0) || (h > 1.0)) return QColor(); double s = colorSpec.section(' ', 2, 2).toDouble(&ok); if ((ok == false) || (s < 0.0) || (s > 1.0)) return QColor(); double b = colorSpec.section(' ', 3, 3).toDouble(&ok); if ((ok == false) || (b < 0.0) || (b > 1.0)) return QColor(); return QColor::fromHsv((int)(h*359.0+0.5), (int)(s*255.0+0.5), (int)(b*255.0+0.5)); } if (specType.indexOf("cmyk", 0, Qt::CaseInsensitive) == 0) { bool ok; double c = colorSpec.section(' ', 1, 1).toDouble(&ok); if ((ok == false) || (c < 0.0) || (c > 1.0)) return QColor(); double m = colorSpec.section(' ', 2, 2).toDouble(&ok); if ((ok == false) || (m < 0.0) || (m > 1.0)) return QColor(); double y = colorSpec.section(' ', 3, 3).toDouble(&ok); if ((ok == false) || (y < 0.0) || (y > 1.0)) return QColor(); double k = colorSpec.section(' ', 3, 3).toDouble(&ok); if ((ok == false) || (k < 0.0) || (k > 1.0)) return QColor(); // Convert cmyk coordinates to rgb. double r = 1.0 - c - k; if (r < 0.0) r = 0.0; double g = 1.0 - m - k; if (g < 0.0) g = 0.0; double b = 1.0 - y - k; if (b < 0.0) b = 0.0; return QColor((int)(r*255.0+0.5), (int)(g*255.0+0.5), (int)(b*255.0+0.5)); } if (specType.indexOf("gray", 0, Qt::CaseInsensitive) == 0) { bool ok; double g = colorSpec.section(' ', 1, 1).toDouble(&ok); if ((ok == false) || (g < 0.0) || (g > 1.0)) return QColor(); return QColor((int)(g*255.0+0.5), (int)(g*255.0+0.5), (int)(g*255.0+0.5)); } // Check if the color is one of the known named colors. QMap<QString, QColor>::Iterator f = namedColors.find(specType); if (f != namedColors.end()) return *f; return QColor(specType);}void dviRenderer::color_special(const QString& _cp){ QString const cp = _cp.trimmed(); QString command = cp.section(' ', 0, 0); if (command == "pop") { // Take color off the stack if (colorStack.isEmpty()) printErrorMsgForSpecials( i18n("Error in DVIfile '%1', page %2. Color pop command issued when the color stack is empty." , dviFile->filename, current_page)); else colorStack.pop(); return; } if (command == "push") { // Get color specification const QColor col = parseColorSpecification(cp.section(' ', 1)); // Set color if (col.isValid()) colorStack.push(col); else colorStack.push(Qt::black); return; } // Get color specification and set the color for the rest of this // page QColor col = parseColorSpecification(cp); // Set color if (col.isValid()) globalColor = col; else globalColor = Qt::black; return;}void dviRenderer::html_href_special(const QString& _cp){ QString cp = _cp; cp.truncate(cp.indexOf('"'));#ifdef DEBUG_SPECIAL kDebug(kvs::dvi) << "HTML-special, href " << cp.toLatin1() << endl;#endif HTML_href = new QString(cp);}void dviRenderer::html_anchor_end(){#ifdef DEBUG_SPECIAL kDebug(kvs::dvi) << "HTML-special, anchor-end" << endl;#endif if (HTML_href != NULL) { delete HTML_href; HTML_href = NULL; }}void dviRenderer::source_special(const QString& cp){ // only when rendering really takes place: set source_href to the // current special string. When characters are rendered, the // rendering routine will then generate a DVI_HyperLink and add it // to the proper list. This DVI_HyperLink is used to match mouse // positions with the hyperlinks for inverse search. if (source_href) *source_href = cp; else source_href = new QString(cp);}void parse_special_argument(const QString& strg, const char* argument_name, int* variable){ int index = strg.indexOf(argument_name); if (index >= 0) { QString tmp = strg.mid(index + strlen(argument_name)); index = tmp.indexOf(' '); if (index >= 0) tmp.truncate(index); bool OK; float const tmp_float = tmp.toFloat(&OK); if (OK) *variable = int(tmp_float+0.5); else // Maybe we should open a dialog here. kError(kvs::dvi) << i18n("Malformed parameter in the epsf special command.\n" "Expected a float to follow %1 in %2", argument_name, strg) << endl; }}void dviRenderer::epsf_special(const QString& cp){#ifdef DEBUG_SPECIAL kDebug(kvs::dvi) << "epsf-special: psfile=" << cp <<endl;#endif QString include_command = cp.simplified(); // The line is supposed to start with "..ile=", and then comes the // filename. Figure out what the filename is and stow it away. Of // course, this does not work if the filename contains spaces // (already the simplified() above is wrong). If you have // files like this, go away. QString EPSfilename_orig = include_command; EPSfilename_orig.truncate(EPSfilename_orig.indexOf(' ')); // Strip enclosing quotation marks which are included by some LaTeX // macro packages (but not by others). This probably means that // graphic files are no longer found if the filename really does // contain quotes, but we don't really care that much. if ((EPSfilename_orig.at(0) == '\"') && (EPSfilename_orig.at(EPSfilename_orig.length()-1) == '\"')) { EPSfilename_orig = EPSfilename_orig.mid(1,EPSfilename_orig.length()-2); } QString EPSfilename = ghostscript_interface::locateEPSfile(EPSfilename_orig, baseURL); // Now parse the arguments. int llx = 0; int lly = 0; int urx = 0; int ury = 0; int rwi = 0; int rhi = 0; int angle = 0; // just to avoid ambiguities; the filename could contain keywords include_command = include_command.mid(include_command.indexOf(' ')); parse_special_argument(include_command, "llx=", &llx); parse_special_argument(include_command, "lly=", &lly); parse_special_argument(include_command, "urx=", &urx); parse_special_argument(include_command, "ury=", &ury);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -