File indexing completed on 2024-05-12 04:33:59
0001 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; c-brace-offset: 0; -*- 0002 0003 // special.cpp 0004 0005 // Methods for dviRenderer which deal with "\special" commands found in the 0006 // DVI file 0007 0008 // Copyright 2000--2004, Stefan Kebekus (kebekus@kde.org). 0009 0010 #include <config.h> 0011 0012 #include "debug_dvi.h" 0013 #include "dviFile.h" 0014 #include "dviRenderer.h" 0015 #include "hyperlink.h" 0016 #include "psgs.h" 0017 //#include "renderedDocumentPage.h" 0018 0019 #include <KLocalizedString> 0020 #include <QMimeDatabase> 0021 #include <QMimeType> 0022 0023 #include "debug_dvi.h" 0024 #include <QByteArray> 0025 #include <QFile> 0026 #include <QFontDatabase> 0027 #include <QImage> 0028 #include <QPainter> 0029 0030 void dviRenderer::printErrorMsgForSpecials(const QString &msg) 0031 { 0032 if (dviFile->errorCounter < 25) { 0033 qCCritical(OkularDviDebug) << msg; 0034 dviFile->errorCounter++; 0035 if (dviFile->errorCounter == 25) { 0036 qCCritical(OkularDviDebug) << i18n("That makes 25 errors. Further error messages will not be printed."); 0037 } 0038 } 0039 } 0040 0041 // Parses a color specification, as explained in the manual to 0042 // dvips. If the spec could not be parsed, an invalid color will be 0043 // returned. 0044 0045 QColor dviRenderer::parseColorSpecification(const QString &colorSpec) 0046 { 0047 // Initialize the map of known colors, if that is not done yet. 0048 if (namedColors.isEmpty()) { 0049 namedColors[QStringLiteral("Red")] = QColor((int)(255.0 * 1), (int)(255.0 * 0), (int)(255.0 * 0)); 0050 namedColors[QStringLiteral("Tan")] = QColor((int)(255.0 * 0.86), (int)(255.0 * 0.58), (int)(255.0 * 0.44)); 0051 namedColors[QStringLiteral("Blue")] = QColor((int)(255.0 * 0), (int)(255.0 * 0), (int)(255.0 * 1)); 0052 namedColors[QStringLiteral("Cyan")] = QColor((int)(255.0 * 0), (int)(255.0 * 1), (int)(255.0 * 1)); 0053 namedColors[QStringLiteral("Gray")] = QColor((int)(255.0 * 0.5), (int)(255.0 * 0.5), (int)(255.0 * 0.5)); 0054 namedColors[QStringLiteral("Plum")] = QColor((int)(255.0 * 0.5), (int)(255.0 * 0), (int)(255.0 * 1)); 0055 namedColors[QStringLiteral("Black")] = QColor((int)(255.0 * 0), (int)(255.0 * 0), (int)(255.0 * 0)); 0056 namedColors[QStringLiteral("Brown")] = QColor((int)(255.0 * 0.4), (int)(255.0 * 0), (int)(255.0 * 0)); 0057 namedColors[QStringLiteral("Green")] = QColor((int)(255.0 * 0), (int)(255.0 * 1), (int)(255.0 * 0)); 0058 namedColors[QStringLiteral("Melon")] = QColor((int)(255.0 * 1), (int)(255.0 * 0.54), (int)(255.0 * 0.5)); 0059 namedColors[QStringLiteral("Peach")] = QColor((int)(255.0 * 1), (int)(255.0 * 0.5), (int)(255.0 * 0.3)); 0060 namedColors[QStringLiteral("Sepia")] = QColor((int)(255.0 * 0.3), (int)(255.0 * 0), (int)(255.0 * 0)); 0061 namedColors[QStringLiteral("White")] = QColor((int)(255.0 * 1), (int)(255.0 * 1), (int)(255.0 * 1)); 0062 namedColors[QStringLiteral("Maroon")] = QColor((int)(255.0 * 0.68), (int)(255.0 * 0), (int)(255.0 * 0)); 0063 namedColors[QStringLiteral("Orange")] = QColor((int)(255.0 * 1), (int)(255.0 * 0.39), (int)(255.0 * 0.13)); 0064 namedColors[QStringLiteral("Orchid")] = QColor((int)(255.0 * 0.68), (int)(255.0 * 0.36), (int)(255.0 * 1)); 0065 namedColors[QStringLiteral("Purple")] = QColor((int)(255.0 * 0.55), (int)(255.0 * 0.14), (int)(255.0 * 1)); 0066 namedColors[QStringLiteral("Salmon")] = QColor((int)(255.0 * 1), (int)(255.0 * 0.47), (int)(255.0 * 0.62)); 0067 namedColors[QStringLiteral("Violet")] = QColor((int)(255.0 * 0.21), (int)(255.0 * 0.12), (int)(255.0 * 1)); 0068 namedColors[QStringLiteral("Yellow")] = QColor((int)(255.0 * 1), (int)(255.0 * 1), (int)(255.0 * 0)); 0069 namedColors[QStringLiteral("Apricot")] = QColor((int)(255.0 * 1), (int)(255.0 * 0.68), (int)(255.0 * 0.48)); 0070 namedColors[QStringLiteral("Emerald")] = QColor((int)(255.0 * 0), (int)(255.0 * 1), (int)(255.0 * 0.5)); 0071 namedColors[QStringLiteral("Fuchsia")] = QColor((int)(255.0 * 0.45), (int)(255.0 * 0.01), (int)(255.0 * 0.92)); 0072 namedColors[QStringLiteral("Magenta")] = QColor((int)(255.0 * 1), (int)(255.0 * 0), (int)(255.0 * 1)); 0073 namedColors[QStringLiteral("SkyBlue")] = QColor((int)(255.0 * 0.38), (int)(255.0 * 1), (int)(255.0 * 0.88)); 0074 namedColors[QStringLiteral("Thistle")] = QColor((int)(255.0 * 0.88), (int)(255.0 * 0.41), (int)(255.0 * 1)); 0075 namedColors[QStringLiteral("BrickRed")] = QColor((int)(255.0 * 0.72), (int)(255.0 * 0), (int)(255.0 * 0)); 0076 namedColors[QStringLiteral("Cerulean")] = QColor((int)(255.0 * 0.06), (int)(255.0 * 0.89), (int)(255.0 * 1)); 0077 namedColors[QStringLiteral("Lavender")] = QColor((int)(255.0 * 1), (int)(255.0 * 0.52), (int)(255.0 * 1)); 0078 namedColors[QStringLiteral("Mahogany")] = QColor((int)(255.0 * 0.65), (int)(255.0 * 0), (int)(255.0 * 0)); 0079 namedColors[QStringLiteral("Mulberry")] = QColor((int)(255.0 * 0.64), (int)(255.0 * 0.08), (int)(255.0 * 0.98)); 0080 namedColors[QStringLiteral("NavyBlue")] = QColor((int)(255.0 * 0.06), (int)(255.0 * 0.46), (int)(255.0 * 1)); 0081 namedColors[QStringLiteral("SeaGreen")] = QColor((int)(255.0 * 0.31), (int)(255.0 * 1), (int)(255.0 * 0.5)); 0082 namedColors[QStringLiteral("TealBlue")] = QColor((int)(255.0 * 0.12), (int)(255.0 * 0.98), (int)(255.0 * 0.64)); 0083 namedColors[QStringLiteral("BlueGreen")] = QColor((int)(255.0 * 0.15), (int)(255.0 * 1), (int)(255.0 * 0.67)); 0084 namedColors[QStringLiteral("CadetBlue")] = QColor((int)(255.0 * 0.38), (int)(255.0 * 0.43), (int)(255.0 * 0.77)); 0085 namedColors[QStringLiteral("Dandelion")] = QColor((int)(255.0 * 1), (int)(255.0 * 0.71), (int)(255.0 * 0.16)); 0086 namedColors[QStringLiteral("Goldenrod")] = QColor((int)(255.0 * 1), (int)(255.0 * 0.9), (int)(255.0 * 0.16)); 0087 namedColors[QStringLiteral("LimeGreen")] = QColor((int)(255.0 * 0.5), (int)(255.0 * 1), (int)(255.0 * 0)); 0088 namedColors[QStringLiteral("OrangeRed")] = QColor((int)(255.0 * 1), (int)(255.0 * 0), (int)(255.0 * 0.5)); 0089 namedColors[QStringLiteral("PineGreen")] = QColor((int)(255.0 * 0), (int)(255.0 * 0.75), (int)(255.0 * 0.16)); 0090 namedColors[QStringLiteral("RawSienna")] = QColor((int)(255.0 * 0.55), (int)(255.0 * 0), (int)(255.0 * 0)); 0091 namedColors[QStringLiteral("RedOrange")] = QColor((int)(255.0 * 1), (int)(255.0 * 0.23), (int)(255.0 * 0.13)); 0092 namedColors[QStringLiteral("RedViolet")] = QColor((int)(255.0 * 0.59), (int)(255.0 * 0), (int)(255.0 * 0.66)); 0093 namedColors[QStringLiteral("Rhodamine")] = QColor((int)(255.0 * 1), (int)(255.0 * 0.18), (int)(255.0 * 1)); 0094 namedColors[QStringLiteral("RoyalBlue")] = QColor((int)(255.0 * 0), (int)(255.0 * 0.5), (int)(255.0 * 1)); 0095 namedColors[QStringLiteral("RubineRed")] = QColor((int)(255.0 * 1), (int)(255.0 * 0), (int)(255.0 * 0.87)); 0096 namedColors[QStringLiteral("Turquoise")] = QColor((int)(255.0 * 0.15), (int)(255.0 * 1), (int)(255.0 * 0.8)); 0097 namedColors[QStringLiteral("VioletRed")] = QColor((int)(255.0 * 1), (int)(255.0 * 0.19), (int)(255.0 * 1)); 0098 namedColors[QStringLiteral("Aquamarine")] = QColor((int)(255.0 * 0.18), (int)(255.0 * 1), (int)(255.0 * 0.7)); 0099 namedColors[QStringLiteral("BlueViolet")] = QColor((int)(255.0 * 0.1), (int)(255.0 * 0.05), (int)(255.0 * 0.96)); 0100 namedColors[QStringLiteral("DarkOrchid")] = QColor((int)(255.0 * 0.6), (int)(255.0 * 0.2), (int)(255.0 * 0.8)); 0101 namedColors[QStringLiteral("OliveGreen")] = QColor((int)(255.0 * 0), (int)(255.0 * 0.6), (int)(255.0 * 0)); 0102 namedColors[QStringLiteral("Periwinkle")] = QColor((int)(255.0 * 0.43), (int)(255.0 * 0.45), (int)(255.0 * 1)); 0103 namedColors[QStringLiteral("Bittersweet")] = QColor((int)(255.0 * 0.76), (int)(255.0 * 0.01), (int)(255.0 * 0)); 0104 namedColors[QStringLiteral("BurntOrange")] = QColor((int)(255.0 * 1), (int)(255.0 * 0.49), (int)(255.0 * 0)); 0105 namedColors[QStringLiteral("ForestGreen")] = QColor((int)(255.0 * 0), (int)(255.0 * 0.88), (int)(255.0 * 0)); 0106 namedColors[QStringLiteral("GreenYellow")] = QColor((int)(255.0 * 0.85), (int)(255.0 * 1), (int)(255.0 * 0.31)); 0107 namedColors[QStringLiteral("JungleGreen")] = QColor((int)(255.0 * 0.01), (int)(255.0 * 1), (int)(255.0 * 0.48)); 0108 namedColors[QStringLiteral("ProcessBlue")] = QColor((int)(255.0 * 0.04), (int)(255.0 * 1), (int)(255.0 * 1)); 0109 namedColors[QStringLiteral("RoyalPurple")] = QColor((int)(255.0 * 0.25), (int)(255.0 * 0.1), (int)(255.0 * 1)); 0110 namedColors[QStringLiteral("SpringGreen")] = QColor((int)(255.0 * 0.74), (int)(255.0 * 1), (int)(255.0 * 0.24)); 0111 namedColors[QStringLiteral("YellowGreen")] = QColor((int)(255.0 * 0.56), (int)(255.0 * 1), (int)(255.0 * 0.26)); 0112 namedColors[QStringLiteral("MidnightBlue")] = QColor((int)(255.0 * 0), (int)(255.0 * 0.44), (int)(255.0 * 0.57)); 0113 namedColors[QStringLiteral("YellowOrange")] = QColor((int)(255.0 * 1), (int)(255.0 * 0.58), (int)(255.0 * 0)); 0114 namedColors[QStringLiteral("CarnationPink")] = QColor((int)(255.0 * 1), (int)(255.0 * 0.37), (int)(255.0 * 1)); 0115 namedColors[QStringLiteral("CornflowerBlue")] = QColor((int)(255.0 * 0.35), (int)(255.0 * 0.87), (int)(255.0 * 1)); 0116 namedColors[QStringLiteral("WildStrawberry")] = QColor((int)(255.0 * 1), (int)(255.0 * 0.04), (int)(255.0 * 0.61)); 0117 } 0118 0119 QString specType = colorSpec.section(QLatin1Char(' '), 0, 0); 0120 0121 if (specType.indexOf(QStringLiteral("rgb"), 0, Qt::CaseInsensitive) == 0) { 0122 bool ok; 0123 0124 double r = colorSpec.section(QLatin1Char(' '), 1, 1).toDouble(&ok); 0125 if ((ok == false) || (r < 0.0) || (r > 1.0)) { 0126 return QColor(); 0127 } 0128 0129 double g = colorSpec.section(QLatin1Char(' '), 2, 2).toDouble(&ok); 0130 if ((ok == false) || (g < 0.0) || (g > 1.0)) { 0131 return QColor(); 0132 } 0133 0134 double b = colorSpec.section(QLatin1Char(' '), 3, 3).toDouble(&ok); 0135 if ((ok == false) || (b < 0.0) || (b > 1.0)) { 0136 return QColor(); 0137 } 0138 0139 return QColor((int)(r * 255.0 + 0.5), (int)(g * 255.0 + 0.5), (int)(b * 255.0 + 0.5)); 0140 } 0141 0142 if (specType.indexOf(QStringLiteral("hsb"), 0, Qt::CaseInsensitive) == 0) { 0143 bool ok; 0144 0145 double h = colorSpec.section(QLatin1Char(' '), 1, 1).toDouble(&ok); 0146 if ((ok == false) || (h < 0.0) || (h > 1.0)) { 0147 return QColor(); 0148 } 0149 0150 double s = colorSpec.section(QLatin1Char(' '), 2, 2).toDouble(&ok); 0151 if ((ok == false) || (s < 0.0) || (s > 1.0)) { 0152 return QColor(); 0153 } 0154 0155 double b = colorSpec.section(QLatin1Char(' '), 3, 3).toDouble(&ok); 0156 if ((ok == false) || (b < 0.0) || (b > 1.0)) { 0157 return QColor(); 0158 } 0159 0160 return QColor::fromHsv((int)(h * 359.0 + 0.5), (int)(s * 255.0 + 0.5), (int)(b * 255.0 + 0.5)); 0161 } 0162 0163 if (specType.indexOf(QStringLiteral("cmyk"), 0, Qt::CaseInsensitive) == 0) { 0164 bool ok; 0165 0166 double c = colorSpec.section(QLatin1Char(' '), 1, 1).toDouble(&ok); 0167 if ((ok == false) || (c < 0.0) || (c > 1.0)) { 0168 return QColor(); 0169 } 0170 0171 double m = colorSpec.section(QLatin1Char(' '), 2, 2).toDouble(&ok); 0172 if ((ok == false) || (m < 0.0) || (m > 1.0)) { 0173 return QColor(); 0174 } 0175 0176 double y = colorSpec.section(QLatin1Char(' '), 3, 3).toDouble(&ok); 0177 if ((ok == false) || (y < 0.0) || (y > 1.0)) { 0178 return QColor(); 0179 } 0180 0181 double k = colorSpec.section(QLatin1Char(' '), 3, 3).toDouble(&ok); 0182 if ((ok == false) || (k < 0.0) || (k > 1.0)) { 0183 return QColor(); 0184 } 0185 0186 // Convert cmyk coordinates to rgb. 0187 double r = 1.0 - c - k; 0188 if (r < 0.0) { 0189 r = 0.0; 0190 } 0191 double g = 1.0 - m - k; 0192 if (g < 0.0) { 0193 g = 0.0; 0194 } 0195 double b = 1.0 - y - k; 0196 if (b < 0.0) { 0197 b = 0.0; 0198 } 0199 0200 return QColor((int)(r * 255.0 + 0.5), (int)(g * 255.0 + 0.5), (int)(b * 255.0 + 0.5)); 0201 } 0202 0203 if (specType.indexOf(QStringLiteral("gray"), 0, Qt::CaseInsensitive) == 0) { 0204 bool ok; 0205 0206 double g = colorSpec.section(QLatin1Char(' '), 1, 1).toDouble(&ok); 0207 if ((ok == false) || (g < 0.0) || (g > 1.0)) { 0208 return QColor(); 0209 } 0210 0211 return QColor((int)(g * 255.0 + 0.5), (int)(g * 255.0 + 0.5), (int)(g * 255.0 + 0.5)); 0212 } 0213 0214 // Check if the color is one of the known named colors. 0215 QMap<QString, QColor>::Iterator f = namedColors.find(specType); 0216 if (f != namedColors.end()) { 0217 return *f; 0218 } 0219 0220 return QColor(specType); 0221 } 0222 0223 void dviRenderer::color_special(const QString &msg) 0224 { 0225 QString const cp = msg.trimmed(); 0226 0227 QString command = cp.section(QLatin1Char(' '), 0, 0); 0228 0229 if (command == QLatin1String("pop")) { 0230 // Take color off the stack 0231 if (colorStack.isEmpty()) { 0232 printErrorMsgForSpecials(i18n("Error in DVIfile '%1', page %2. Color pop command issued when the color stack is empty.", dviFile->filename, current_page)); 0233 } else { 0234 colorStack.pop(); 0235 } 0236 return; 0237 } 0238 0239 if (command == QLatin1String("push")) { 0240 // Get color specification 0241 const QColor col = parseColorSpecification(cp.section(QLatin1Char(' '), 1)); 0242 // Set color 0243 if (col.isValid()) { 0244 colorStack.push(col); 0245 } else { 0246 colorStack.push(Qt::black); 0247 } 0248 return; 0249 } 0250 0251 // Get color specification and set the color for the rest of this 0252 // page 0253 QColor col = parseColorSpecification(cp); 0254 // Set color 0255 if (col.isValid()) { 0256 globalColor = col; 0257 } else { 0258 globalColor = Qt::black; 0259 } 0260 return; 0261 } 0262 0263 void dviRenderer::html_href_special(const QString &msg) 0264 { 0265 QString cp = msg; 0266 cp.truncate(cp.indexOf(QLatin1Char('"'))); 0267 0268 #ifdef DEBUG_SPECIAL 0269 qCDebug(OkularDviDebug) << "HTML-special, href " << cp.toLatin1(); 0270 #endif 0271 HTML_href = new QString(cp); 0272 } 0273 0274 void dviRenderer::html_anchor_end() 0275 { 0276 #ifdef DEBUG_SPECIAL 0277 qCDebug(OkularDviDebug) << "HTML-special, anchor-end"; 0278 #endif 0279 0280 if (HTML_href != nullptr) { 0281 delete HTML_href; 0282 HTML_href = nullptr; 0283 } 0284 } 0285 0286 void dviRenderer::source_special(const QString &cp) 0287 { 0288 // only when rendering really takes place: set source_href to the 0289 // current special string. When characters are rendered, the 0290 // rendering routine will then generate a DVI_HyperLink and add it 0291 // to the proper list. This DVI_HyperLink is used to match mouse 0292 // positions with the hyperlinks for inverse search. 0293 if (source_href) { 0294 *source_href = cp; 0295 } else { 0296 source_href = new QString(cp); 0297 } 0298 } 0299 0300 void parse_special_argument(const QString &strg, const char *argument_name, int *variable) 0301 { 0302 int index = strg.indexOf(QString::fromLocal8Bit(argument_name)); 0303 if (index >= 0) { 0304 QString tmp = strg.mid(index + strlen(argument_name)); 0305 index = tmp.indexOf(QLatin1Char(' ')); 0306 if (index >= 0) { 0307 tmp.truncate(index); 0308 } 0309 0310 bool OK; 0311 float const tmp_float = tmp.toFloat(&OK); 0312 0313 if (OK) { 0314 *variable = int(tmp_float + 0.5); 0315 } else { 0316 // Maybe we should open a dialog here. 0317 qCCritical(OkularDviDebug) << i18n( 0318 "Malformed parameter in the epsf special command.\n" 0319 "Expected a float to follow %1 in %2", 0320 QString::fromLocal8Bit(argument_name), 0321 strg); 0322 } 0323 } 0324 } 0325 0326 void dviRenderer::epsf_special(const QString &cp) 0327 { 0328 #ifdef DEBUG_SPECIAL 0329 qCDebug(OkularDviDebug) << "epsf-special: psfile=" << cp; 0330 #endif 0331 0332 QString include_command = cp.simplified(); 0333 0334 // The line is supposed to start with "..ile=", and then comes the 0335 // filename. Figure out what the filename is and stow it away. Of 0336 // course, this does not work if the filename contains spaces 0337 // (already the simplified() above is wrong). If you have 0338 // files like this, go away. 0339 QString EPSfilename_orig = include_command; 0340 EPSfilename_orig.truncate(EPSfilename_orig.indexOf(QLatin1Char(' '))); 0341 0342 // Strip enclosing quotation marks which are included by some LaTeX 0343 // macro packages (but not by others). This probably means that 0344 // graphic files are no longer found if the filename really does 0345 // contain quotes, but we don't really care that much. 0346 if ((EPSfilename_orig.at(0) == QLatin1Char('\"')) && (EPSfilename_orig.at(EPSfilename_orig.length() - 1) == QLatin1Char('\"'))) { 0347 EPSfilename_orig = EPSfilename_orig.mid(1, EPSfilename_orig.length() - 2); 0348 } 0349 QString EPSfilename = ghostscript_interface::locateEPSfile(EPSfilename_orig, baseURL); 0350 0351 // Now parse the arguments. 0352 int llx = 0; 0353 int lly = 0; 0354 int urx = 0; 0355 int ury = 0; 0356 int rwi = 0; 0357 int rhi = 0; 0358 int angle = 0; 0359 0360 // just to avoid ambiguities; the filename could contain keywords 0361 include_command = include_command.mid(include_command.indexOf(QLatin1Char(' '))); 0362 0363 parse_special_argument(include_command, "llx=", &llx); 0364 parse_special_argument(include_command, "lly=", &lly); 0365 parse_special_argument(include_command, "urx=", &urx); 0366 parse_special_argument(include_command, "ury=", &ury); 0367 parse_special_argument(include_command, "rwi=", &rwi); 0368 parse_special_argument(include_command, "rhi=", &rhi); 0369 parse_special_argument(include_command, "angle=", &angle); 0370 0371 // If we have a png, gif, jpeg or mng file, we need to draw it here. 0372 QMimeDatabase db; 0373 QMimeType const mime_type = db.mimeTypeForFile(EPSfilename, QMimeDatabase::MatchContent); 0374 QString const &mime_type_name = mime_type.isValid() ? mime_type.name() : QString(); 0375 bool const isGFX = (mime_type_name == QLatin1String("image/png") || mime_type_name == QLatin1String("image/gif") || mime_type_name == QLatin1String("image/jpeg") || mime_type_name == QLatin1String("video/x-mng")); 0376 0377 // So, if we do not have a PostScript file, but a graphics file, and 0378 // if that file exists, we draw it here. 0379 if (isGFX && QFile::exists(EPSfilename)) { 0380 // Don't show PostScript, just draw the bounding box. For this, 0381 // calculate the size of the bounding box in Pixels. 0382 double bbox_width = urx - llx; 0383 double bbox_height = ury - lly; 0384 0385 if ((rwi != 0) && (bbox_width != 0)) { 0386 bbox_height *= rwi / bbox_width; 0387 bbox_width = rwi; 0388 } 0389 if ((rhi != 0) && (bbox_height != 0)) { 0390 bbox_width *= rhi / bbox_height; 0391 bbox_height = rhi; 0392 } 0393 0394 double fontPixelPerDVIunit = dviFile->getCmPerDVIunit() * 1200.0 / 2.54; 0395 0396 bbox_width *= 0.1 * 65536.0 * fontPixelPerDVIunit / shrinkfactor; 0397 bbox_height *= 0.1 * 65536.0 * fontPixelPerDVIunit / shrinkfactor; 0398 0399 QImage image(EPSfilename); 0400 image = image.scaled((int)(bbox_width), (int)(bbox_height), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); 0401 foreGroundPainter->drawImage(((int)((currinf.data.dvi_h) / (shrinkfactor * 65536))), currinf.data.pxl_v - (int)bbox_height, image); 0402 return; 0403 } 0404 0405 if (!_postscript || !QFile::exists(EPSfilename)) { 0406 // Don't show PostScript, just draw the bounding box. For this, 0407 // calculate the size of the bounding box in Pixels. 0408 double bbox_width = urx - llx; 0409 double bbox_height = ury - lly; 0410 0411 if ((rwi != 0) && (bbox_width != 0)) { 0412 bbox_height *= rwi / bbox_width; 0413 bbox_width = rwi; 0414 } 0415 if ((rhi != 0) && (bbox_height != 0)) { 0416 bbox_width *= rhi / bbox_height; 0417 bbox_height = rhi; 0418 } 0419 0420 double fontPixelPerDVIunit = dviFile->getCmPerDVIunit() * 1200.0 / 2.54; 0421 0422 bbox_width *= 0.1 * 65536.0 * fontPixelPerDVIunit / shrinkfactor; 0423 bbox_height *= 0.1 * 65536.0 * fontPixelPerDVIunit / shrinkfactor; 0424 0425 QRect bbox(((int)((currinf.data.dvi_h) / (shrinkfactor * 65536))), currinf.data.pxl_v - (int)bbox_height, (int)bbox_width, (int)bbox_height); 0426 0427 foreGroundPainter->save(); 0428 0429 if (QFile::exists(EPSfilename)) { 0430 foreGroundPainter->setBrush(Qt::lightGray); 0431 } else { 0432 foreGroundPainter->setBrush(Qt::red); 0433 } 0434 foreGroundPainter->setPen(Qt::black); 0435 foreGroundPainter->drawRoundedRect(bbox, 2, 2); 0436 QFont f = foreGroundPainter->font(); 0437 f.setPointSize(8); 0438 foreGroundPainter->setFont(f); 0439 /* if the fonts are mapped for some reason to X bitmap fonts, 0440 the call to drawText() in the non-GUI thread will produce a crash. 0441 Ensure that the rendering of the text is performed only if 0442 the threaded font rendering is available */ 0443 if (QFile::exists(EPSfilename)) { 0444 foreGroundPainter->drawText(bbox, (int)(Qt::AlignCenter), EPSfilename); 0445 } else { 0446 foreGroundPainter->drawText(bbox, (int)(Qt::AlignCenter), i18n("File not found: \n %1", EPSfilename_orig)); 0447 } 0448 foreGroundPainter->restore(); 0449 } 0450 0451 return; 0452 } 0453 0454 void dviRenderer::TPIC_flushPath_special() 0455 { 0456 #ifdef DEBUG_SPECIAL 0457 qCDebug(OkularDviDebug) << "TPIC special flushPath"; 0458 #endif 0459 0460 if (number_of_elements_in_path == 0) { 0461 printErrorMsgForSpecials(QStringLiteral("TPIC special flushPath called when path was empty.")); 0462 return; 0463 } 0464 0465 QPen pen(Qt::black, (int)(penWidth_in_mInch * resolutionInDPI / 1000.0 + 0.5)); // Sets the pen size in milli-inches 0466 foreGroundPainter->setPen(pen); 0467 foreGroundPainter->drawPolyline(TPIC_path.constData(), number_of_elements_in_path); 0468 number_of_elements_in_path = 0; 0469 } 0470 0471 void dviRenderer::TPIC_addPath_special(const QString &cp) 0472 { 0473 #ifdef DEBUG_SPECIAL 0474 qCDebug(OkularDviDebug) << "TPIC special addPath: " << cp; 0475 #endif 0476 0477 // Adds a point to the path list 0478 QString cp_noWhiteSpace = cp.trimmed(); 0479 bool ok; 0480 float xKoord = cp_noWhiteSpace.section(QLatin1Char(' '), 0, 0).toFloat(&ok); 0481 if (ok == false) { 0482 printErrorMsgForSpecials(QStringLiteral("TPIC special; cannot parse first argument in 'pn %1'.").arg(cp)); 0483 return; 0484 } 0485 float yKoord = cp_noWhiteSpace.section(QLatin1Char(' '), 1, 1).toFloat(&ok); 0486 if (ok == false) { 0487 printErrorMsgForSpecials(QStringLiteral("TPIC special; cannot parse second argument in 'pn %1'.").arg(cp)); 0488 return; 0489 } 0490 0491 float mag = dviFile->getMagnification() / 1000.0; 0492 0493 int x = (int)(currinf.data.dvi_h / (shrinkfactor * 65536.0) + mag * xKoord * resolutionInDPI / 1000.0 + 0.5); 0494 int y = (int)(currinf.data.pxl_v + mag * yKoord * resolutionInDPI / 1000.0 + 0.5); 0495 0496 // Initialize the point array used to store the path 0497 if (TPIC_path.size() == 0) { 0498 number_of_elements_in_path = 0; 0499 } 0500 if (TPIC_path.size() == number_of_elements_in_path) { 0501 TPIC_path.resize(number_of_elements_in_path + 100); 0502 } 0503 TPIC_path.setPoint(number_of_elements_in_path++, x, y); 0504 } 0505 0506 void dviRenderer::TPIC_setPen_special(const QString &cp) 0507 { 0508 #ifdef DEBUG_SPECIAL 0509 qCDebug(OkularDviDebug) << "TPIC special setPen: " << cp; 0510 #endif 0511 0512 // Sets the pen size in milli-inches 0513 bool ok; 0514 penWidth_in_mInch = cp.trimmed().toFloat(&ok); 0515 if (ok == false) { 0516 printErrorMsgForSpecials(QStringLiteral("TPIC special; cannot parse argument in 'pn %1'.").arg(cp)); 0517 penWidth_in_mInch = 0.0; 0518 return; 0519 } 0520 } 0521 0522 void dviRenderer::applicationDoSpecial(char *cp) 0523 { 0524 QString special_command = QString::fromLocal8Bit(cp); 0525 0526 // First come specials which is only interpreted during rendering, 0527 // and NOT during the prescan phase 0528 0529 // font color specials 0530 if (qstrnicmp(cp, "color", 5) == 0) { 0531 color_special(special_command.mid(5)); 0532 return; 0533 } 0534 0535 // HTML reference 0536 if (qstrnicmp(cp, "html:<A href=", 13) == 0) { 0537 html_href_special(special_command.mid(14)); 0538 return; 0539 } 0540 0541 // HTML anchor end 0542 if (qstrnicmp(cp, "html:</A>", 9) == 0) { 0543 html_anchor_end(); 0544 return; 0545 } 0546 0547 // TPIC specials 0548 if (qstrnicmp(cp, "pn", 2) == 0) { 0549 TPIC_setPen_special(special_command.mid(2)); 0550 return; 0551 } 0552 if (qstrnicmp(cp, "pa ", 3) == 0) { 0553 TPIC_addPath_special(special_command.mid(3)); 0554 return; 0555 } 0556 if (qstrnicmp(cp, "fp", 2) == 0) { 0557 TPIC_flushPath_special(); 0558 return; 0559 } 0560 0561 // Encapsulated Postscript File 0562 if (qstrnicmp(cp, "PSfile=", 7) == 0) { 0563 epsf_special(special_command.mid(7)); 0564 return; 0565 } 0566 0567 // source special 0568 if (qstrnicmp(cp, "src:", 4) == 0) { 0569 source_special(special_command.mid(4)); 0570 return; 0571 } 0572 0573 // Unfortunately, in some TeX distribution the hyperref package uses 0574 // the dvips driver by default, rather than the hypertex driver. As 0575 // a result, the DVI files produced are full of PostScript that 0576 // specifies links and anchors, and KDVI would call the ghostscript 0577 // interpreter for every page which makes it really slow. This is a 0578 // major nuisance, so that we try to filter and interpret the 0579 // hypertex generated PostScript here. 0580 if (special_command.startsWith(QLatin1String("ps:SDict begin"))) { 0581 // Hyperref: start of hyperref rectangle. At this stage it is not 0582 // yet clear if the rectangle will contain a hyperlink, an anchor, 0583 // or another type of object. We suspect that this rectangle will 0584 // define a hyperlink, allocate a QString and set HTML_href to 0585 // point to this string. The string contains the name of the 0586 // destination which ---due to the nature of the PostScript 0587 // language--- will be defined only after characters are drawn and 0588 // the hyperref rectangle has been closed. We use "glopglyph" as a 0589 // temporary name. Since the pointer HTML_href is not NULL, the 0590 // character drawing routines will now underline all characters in 0591 // blue to point out that they correspond to a hyperlink. Also, as 0592 // soon as characters are drawn, the drawing routines will 0593 // allocate a Hyperlink and add it to the top of the vector 0594 // currentlyDrawnPage->hyperLinkList. 0595 if (special_command == QLatin1String("ps:SDict begin H.S end")) { 0596 // At this stage, the vector 'hyperLinkList' should not contain 0597 // links with unspecified destinations (i.e. destination set to 0598 // 'glopglyph'). As a protection against bad DVI files, we make 0599 // sure to remove all link rectangles which point to 0600 // 'glopglyph'. 0601 while (!currentlyDrawnPage->hyperLinkList.isEmpty()) { 0602 if (currentlyDrawnPage->hyperLinkList.last().linkText == QLatin1String("glopglyph")) { 0603 currentlyDrawnPage->hyperLinkList.pop_back(); 0604 } else { 0605 break; 0606 } 0607 } 0608 0609 HTML_href = new QString(QStringLiteral("glopglyph")); 0610 return; 0611 } 0612 0613 // Hyperref: end of hyperref rectangle of unknown type or hyperref 0614 // link rectangle. In these cases we set HTML_href to NULL, which 0615 // causes the character drawing routines to stop drawing 0616 // characters underlined in blue. Note that the name of the 0617 // destination is still set to "glopglyph". In a well-formed DVI 0618 // file, this special command is immediately followed by another 0619 // special, where the destination is specified. This special is 0620 // treated below. 0621 if ((special_command == QLatin1String("ps:SDict begin H.R end")) || special_command.endsWith(QLatin1String("H.L end"))) { 0622 if (HTML_href != nullptr) { 0623 delete HTML_href; 0624 HTML_href = nullptr; 0625 } 0626 return; // end of hyperref rectangle 0627 } 0628 0629 // Hyperref: end of anchor rectangle. If this special is 0630 // encountered, the rectangle, which was started with "ps:SDict 0631 // begin H.S end" does not contain a link, but an anchor for a 0632 // link. Anchors, however, have already been dealt with in the 0633 // prescan phase and will not be considered here. Thus, we set 0634 // HTML_href to NULL so that character drawing routines will no 0635 // longer underline hyperlinks in blue, and remove the link from 0636 // the hyperLinkList. NOTE: in a well-formed DVI file, the "H.A" 0637 // special comes directly after the "H.S" special. A 0638 // hyperlink-anchor rectangle therefore never contains characters, 0639 // so no character will by accidentally underlined in blue. 0640 if (special_command.endsWith(QLatin1String("H.A end"))) { 0641 if (HTML_href != nullptr) { 0642 delete HTML_href; 0643 HTML_href = nullptr; 0644 } 0645 while (!currentlyDrawnPage->hyperLinkList.isEmpty()) { 0646 if (currentlyDrawnPage->hyperLinkList.last().linkText == QLatin1String("glopglyph")) { 0647 currentlyDrawnPage->hyperLinkList.pop_back(); 0648 } else { 0649 break; 0650 } 0651 } 0652 return; // end of hyperref anchor 0653 } 0654 0655 // Hyperref: specification of a hyperref link rectangle's 0656 // destination. As mentioned above, the destination of a hyperlink 0657 // is specified only AFTER the rectangle has been specified. We 0658 // will therefore go through the list of rectangles stored in 0659 // currentlyDrawnPage->hyperLinkList, find those whose destination 0660 // is open and fill in the value found here. NOTE: the character 0661 // drawing routines sometimes split a single hyperlink rectangle 0662 // into several rectangles (e.g. if the font changes, or when a 0663 // line break is encountered) 0664 if (special_command.startsWith(QLatin1String("ps:SDict begin [")) && special_command.endsWith(QLatin1String(" pdfmark end"))) { 0665 if (!currentlyDrawnPage->hyperLinkList.isEmpty()) { 0666 QString targetName = special_command.section(QLatin1Char('('), 1, 1).section(QLatin1Char(')'), 0, 0); 0667 QVector<Hyperlink>::iterator it; 0668 for (it = currentlyDrawnPage->hyperLinkList.begin(); it != currentlyDrawnPage->hyperLinkList.end(); ++it) { 0669 if (it->linkText == QLatin1String("glopglyph")) { 0670 it->linkText = targetName; 0671 } 0672 } 0673 } 0674 return; // hyperref definition of link/anchor/bookmark/etc 0675 } 0676 } 0677 0678 // Detect text rotation specials that are included by the graphicx 0679 // package. If one of these specials is found, the state of the 0680 // painter is saved, and the coordinate system is rotated 0681 // accordingly 0682 if (special_command.startsWith(QLatin1String("ps: gsave currentpoint currentpoint translate ")) && special_command.endsWith(QLatin1String(" neg rotate neg exch neg exch translate"))) { 0683 bool ok; 0684 double angle = special_command.section(QLatin1Char(' '), 5, 5).toDouble(&ok); 0685 if (ok == true) { 0686 int x = ((int)((currinf.data.dvi_h) / (shrinkfactor * 65536))); 0687 int y = currinf.data.pxl_v; 0688 0689 foreGroundPainter->save(); 0690 // Rotate about the current point 0691 foreGroundPainter->translate(x, y); 0692 foreGroundPainter->rotate(-angle); 0693 foreGroundPainter->translate(-x, -y); 0694 } else { 0695 printErrorMsgForSpecials(i18n("Error in DVIfile '%1', page %2. Could not interpret angle in text rotation special.", dviFile->filename, current_page)); 0696 } 0697 } 0698 0699 // The graphicx package marks the end of rotated text with this 0700 // special. The state of the painter is restored. 0701 if (special_command == QLatin1String("ps: currentpoint grestore moveto")) { 0702 foreGroundPainter->restore(); 0703 } 0704 0705 // The following special commands are not used here; they are of 0706 // interest only during the prescan phase. We recognize them here 0707 // anyway, to make sure that KDVI doesn't complain about 0708 // unrecognized special commands. 0709 if ((cp[0] == '!') || (cp[0] == '"') || (qstrnicmp(cp, "html:<A name=", 13) == 0) || (qstrnicmp(cp, "ps:", 3) == 0) || (qstrnicmp(cp, "papersize", 9) == 0) || (qstrnicmp(cp, "header", 6) == 0) || 0710 (qstrnicmp(cp, "background", 10) == 0)) { 0711 return; 0712 } 0713 0714 printErrorMsgForSpecials(i18n("The special command '%1' is not implemented.", special_command)); 0715 return; 0716 }