1 /* 2 For general Scribus (>=1.3.2) copyright and licensing information please refer 3 to the COPYING file provided with the program. Following this notice may exist 4 a copyright and/or license notice that predates the release of Scribus 1.3.2 5 for which a new license (GPL+exception) is in place. 6 */ 7 /*************************************************************************** 8 ------------------- 9 begin : Sat Okt 03 2015 10 copyright : (C) 2015 by Franz Schmid 11 email : Franz.Schmid@altmuehlnet.de 12 ***************************************************************************/ 13 #include "sladocumentrtfoutput.h" 14 15 #include <QApplication> 16 #include <QColor> 17 #include <QImage> 18 #include <QString> 19 #include <QTextImageFormat> 20 #include "pageitem.h" 21 #include "prefsmanager.h" 22 #include "fileloader.h" 23 #include "loadsaveplugin.h" 24 #include "scribusdoc.h" 25 #include "selection.h" 26 #include "ui/missing.h" 27 #include "util.h" 28 #include <QDebug> 29 30 namespace RtfReader 31 { SlaDocumentRtfOutput(PageItem * ite,ScribusDoc * doc,bool prefix)32 SlaDocumentRtfOutput::SlaDocumentRtfOutput(PageItem *ite, ScribusDoc *doc, bool prefix) : AbstractRtfOutput() 33 { 34 m_item = ite; 35 m_Doc = doc; 36 m_prefixName = prefix; 37 QString pStyle = CommonStrings::DefaultParagraphStyle; 38 ParagraphStyle newStyle; 39 newStyle.setParent(pStyle); 40 newStyle.setLineSpacingMode(ParagraphStyle::AutomaticLineSpacing); 41 m_textStyle.push(newStyle); 42 m_textCharStyle.push(newStyle.charStyle()); 43 m_textCharStyle.top().setFontSize(120.0); 44 QList<ParagraphStyle::TabRecord> tbs; 45 tbs.clear(); 46 m_textStyle.top().setTabValues(tbs); 47 m_fontTable.clear(); 48 m_fontTableReal.clear(); 49 m_codecList = QTextCodec::availableCodecs(); 50 for (int i = 0; i < m_codecList.count(); ++i) 51 { 52 QByteArray codecName = m_codecList.at(i); 53 m_codecList[i] = codecName.toLower(); 54 } 55 if (m_codecList.contains("cp1252")) 56 m_codec = QTextCodec::codecForName("cp1252"); // Default ANSI codec 57 else 58 m_codec = QTextCodec::codecForLocale(); 59 m_keepn = false; 60 m_isBold = false; 61 m_isItalic = false; 62 } 63 ~SlaDocumentRtfOutput()64 SlaDocumentRtfOutput::~SlaDocumentRtfOutput() 65 { 66 } 67 setEncoding(const int enc)68 void SlaDocumentRtfOutput::setEncoding(const int enc) 69 { 70 QByteArray ba; 71 ba.setNum(enc); 72 QByteArray encTest = "cp" + ba; 73 if (m_codecList.contains(encTest)) 74 m_codec = QTextCodec::codecForName(encTest); 75 else if (m_codecList.contains("cp1252")) 76 m_codec = QTextCodec::codecForName("cp1252"); 77 } 78 startGroup()79 void SlaDocumentRtfOutput::startGroup() 80 { 81 m_textStyle.push(m_textStyle.top()); 82 m_textCharStyle.push(m_textCharStyle.top()); 83 } 84 endGroup()85 void SlaDocumentRtfOutput::endGroup() 86 { 87 m_textCharStyle.pop(); 88 m_textStyle.pop(); 89 } 90 appendText(const QByteArray & text)91 void SlaDocumentRtfOutput::appendText(const QByteArray &text) 92 { 93 int posC = m_item->itemText.length(); 94 QString m_txt = m_codec->toUnicode(text); 95 if (text.count() > 0) 96 { 97 m_txt.replace(QChar(10), SpecialChars::LINEBREAK); 98 m_txt.replace(QChar(12), SpecialChars::FRAMEBREAK); 99 m_txt.replace(QChar(30), SpecialChars::NBHYPHEN); 100 m_txt.replace(QChar(160), SpecialChars::NBSPACE); 101 m_item->itemText.insertChars(posC, m_txt); 102 m_item->itemText.applyStyle(posC, m_textStyle.top()); 103 m_item->itemText.applyCharStyle(posC, m_txt.length(), m_textCharStyle.top()); 104 } 105 } 106 appendUnicodeText(const QString & text)107 void SlaDocumentRtfOutput::appendUnicodeText(const QString &text) 108 { 109 int posC = m_item->itemText.length(); 110 QString m_txt = text; 111 if (text.count() > 0) 112 { 113 m_txt.replace(QChar(10), SpecialChars::LINEBREAK); 114 m_txt.replace(QChar(12), SpecialChars::FRAMEBREAK); 115 m_txt.replace(QChar(30), SpecialChars::NBHYPHEN); 116 m_txt.replace(QChar(160), SpecialChars::NBSPACE); 117 m_item->itemText.insertChars(posC, m_txt); 118 m_item->itemText.applyStyle(posC, m_textStyle.top()); 119 m_item->itemText.applyCharStyle(posC, m_txt.length(), m_textCharStyle.top()); 120 } 121 } 122 insertPar()123 void SlaDocumentRtfOutput::insertPar() 124 { 125 int posT = m_item->itemText.length(); 126 if (posT > 0) 127 { 128 m_item->itemText.insertChars(posT, SpecialChars::PARSEP); 129 m_item->itemText.applyStyle(posT, m_textStyle.top()); 130 } 131 } 132 insertTab()133 void SlaDocumentRtfOutput::insertTab() 134 { 135 int posT = m_item->itemText.length(); 136 m_item->itemText.insertChars(posT, SpecialChars::TAB); 137 m_item->itemText.applyStyle(posT, m_textStyle.top()); 138 } 139 insertLeftQuote()140 void SlaDocumentRtfOutput::insertLeftQuote() 141 { 142 m_item->itemText.insertChars(QString(QChar(0x2018)), true); 143 } 144 insertRightQuote()145 void SlaDocumentRtfOutput::insertRightQuote() 146 { 147 m_item->itemText.insertChars(QString(QChar(0x2019)), true); 148 } 149 insertLeftDoubleQuote()150 void SlaDocumentRtfOutput::insertLeftDoubleQuote() 151 { 152 m_item->itemText.insertChars(QString(QChar(0x201c)), true); 153 } 154 insertRightDoubleQuote()155 void SlaDocumentRtfOutput::insertRightDoubleQuote() 156 { 157 m_item->itemText.insertChars(QString(QChar(0x201d)), true); 158 } 159 insertEnDash()160 void SlaDocumentRtfOutput::insertEnDash() 161 { 162 m_item->itemText.insertChars(QString(QChar(0x2013)), true); 163 } 164 insertEmDash()165 void SlaDocumentRtfOutput::insertEmDash() 166 { 167 m_item->itemText.insertChars(QString(QChar(0x2014)), true); 168 } 169 insertEmSpace()170 void SlaDocumentRtfOutput::insertEmSpace() 171 { 172 m_item->itemText.insertChars(QString(QChar(0x2003)), true); 173 } 174 insertEnSpace()175 void SlaDocumentRtfOutput::insertEnSpace() 176 { 177 m_item->itemText.insertChars(QString(QChar(0x2002)), true); 178 } 179 insertBullet()180 void SlaDocumentRtfOutput::insertBullet() 181 { 182 m_item->itemText.insertChars(QString(QChar(0x2022)), true); 183 } 184 insertNewLine()185 void SlaDocumentRtfOutput::insertNewLine() 186 { 187 int posT = m_item->itemText.length(); 188 if (posT > 0) 189 { 190 m_item->itemText.insertChars(posT, SpecialChars::LINEBREAK); 191 m_item->itemText.applyStyle(posT, m_textStyle.top()); 192 } 193 } 194 setFontItalic(const int value)195 void SlaDocumentRtfOutput::setFontItalic(const int value) 196 { 197 m_isItalic = (value != 0); 198 int posC = m_item->itemText.length(); 199 m_item->itemText.insertChars(posC, "B"); 200 m_item->itemText.applyStyle(posC, m_textStyle.top()); 201 m_item->itemText.applyCharStyle(posC, 1, m_textCharStyle.top()); 202 QString fam = m_item->itemText.charStyle(posC).font().family(); 203 m_item->itemText.removeChars(posC, 1); 204 if (fam.isEmpty()) 205 return; 206 QStringList slist = PrefsManager::instance().appPrefs.fontPrefs.AvailFonts.fontMap[fam]; 207 if (m_isItalic) 208 { 209 if (m_isBold) 210 { 211 if (slist.contains("Bold Italic")) 212 m_textCharStyle.top().setFont(PrefsManager::instance().appPrefs.fontPrefs.AvailFonts[fam + " Bold Italic"]); 213 } 214 else if (slist.contains("Italic")) 215 m_textCharStyle.top().setFont(PrefsManager::instance().appPrefs.fontPrefs.AvailFonts[fam + " Italic"]); 216 } 217 else 218 { 219 if (m_isBold) 220 { 221 if (slist.contains("Bold")) 222 m_textCharStyle.top().setFont(PrefsManager::instance().appPrefs.fontPrefs.AvailFonts[fam + " Bold"]); 223 } 224 else if (slist.contains("Regular")) 225 m_textCharStyle.top().setFont(PrefsManager::instance().appPrefs.fontPrefs.AvailFonts[fam + " Regular"]); 226 } 227 } 228 setFontBold(const int value)229 void SlaDocumentRtfOutput::setFontBold(const int value) 230 { 231 m_isBold = (value != 0); 232 int posC = m_item->itemText.length(); 233 m_item->itemText.insertChars(posC, "B"); 234 m_item->itemText.applyStyle(posC, m_textStyle.top()); 235 m_item->itemText.applyCharStyle(posC, 1, m_textCharStyle.top()); 236 QString fam = m_item->itemText.charStyle(posC).font().family(); 237 m_item->itemText.removeChars(posC, 1); 238 if (fam.isEmpty()) 239 return; 240 QStringList slist = PrefsManager::instance().appPrefs.fontPrefs.AvailFonts.fontMap[fam]; 241 if (m_isBold) 242 { 243 if (m_isItalic) 244 { 245 if (slist.contains("Bold Italic")) 246 m_textCharStyle.top().setFont(PrefsManager::instance().appPrefs.fontPrefs.AvailFonts[fam + " Bold Italic"]); 247 } 248 else if (slist.contains("Bold")) 249 m_textCharStyle.top().setFont(PrefsManager::instance().appPrefs.fontPrefs.AvailFonts[fam + " Bold"]); 250 } 251 else 252 { 253 if (m_isItalic) 254 { 255 if (slist.contains("Italic")) 256 m_textCharStyle.top().setFont(PrefsManager::instance().appPrefs.fontPrefs.AvailFonts[fam + " Italic"]); 257 } 258 else if (slist.contains("Regular")) 259 m_textCharStyle.top().setFont(PrefsManager::instance().appPrefs.fontPrefs.AvailFonts[fam + " Regular"]); 260 } 261 } 262 setFontUnderline(const int value)263 void SlaDocumentRtfOutput::setFontUnderline(const int value) 264 { 265 StyleFlag styleEffects = m_textCharStyle.top().effects(); 266 if (value != 0) 267 styleEffects |= ScStyle_Underline; 268 else 269 styleEffects &= ~ScStyle_Underline; 270 m_textCharStyle.top().setFeatures(styleEffects.featureList()); 271 } 272 setFontWordUnderline(const int value)273 void SlaDocumentRtfOutput::setFontWordUnderline(const int value) 274 { 275 StyleFlag styleEffects = m_textCharStyle.top().effects(); 276 if (value != 0) 277 styleEffects |= ScStyle_UnderlineWords; 278 else 279 styleEffects &= ~ScStyle_UnderlineWords; 280 m_textCharStyle.top().setFeatures(styleEffects.featureList()); 281 } 282 setFontOutline(const int value)283 void SlaDocumentRtfOutput::setFontOutline(const int value) 284 { 285 StyleFlag styleEffects = m_textCharStyle.top().effects(); 286 if (value != 0) 287 styleEffects |= ScStyle_Outline; 288 else 289 styleEffects &= ~ScStyle_Outline; 290 m_textCharStyle.top().setFeatures(styleEffects.featureList()); 291 } 292 setFontShadow(const int value)293 void SlaDocumentRtfOutput::setFontShadow(const int value) 294 { 295 StyleFlag styleEffects = m_textCharStyle.top().effects(); 296 if (value != 0) 297 styleEffects |= ScStyle_Shadowed; 298 else 299 styleEffects &= ~ScStyle_Shadowed; 300 m_textCharStyle.top().setFeatures(styleEffects.featureList()); 301 } 302 setFontSmallCaps(const int value)303 void SlaDocumentRtfOutput::setFontSmallCaps(const int value) 304 { 305 StyleFlag styleEffects = m_textCharStyle.top().effects(); 306 if (value != 0) 307 styleEffects |= ScStyle_SmallCaps; 308 else 309 styleEffects &= ~ScStyle_SmallCaps; 310 m_textCharStyle.top().setFeatures(styleEffects.featureList()); 311 } 312 setFontCaps(const int value)313 void SlaDocumentRtfOutput::setFontCaps(const int value) 314 { 315 StyleFlag styleEffects = m_textCharStyle.top().effects(); 316 if (value != 0) 317 styleEffects |= ScStyle_AllCaps; 318 else 319 styleEffects &= ~ScStyle_AllCaps; 320 m_textCharStyle.top().setFeatures(styleEffects.featureList()); 321 } 322 setFontStrikeOut(const int value)323 void SlaDocumentRtfOutput::setFontStrikeOut(const int value) 324 { 325 StyleFlag styleEffects = m_textCharStyle.top().effects(); 326 if (value != 0) 327 styleEffects |= ScStyle_Strikethrough; 328 else 329 styleEffects &= ~ScStyle_Strikethrough; 330 m_textCharStyle.top().setFeatures(styleEffects.featureList()); 331 } 332 setFontScaleH(const int value)333 void SlaDocumentRtfOutput::setFontScaleH(const int value) 334 { 335 m_textCharStyle.top().setScaleH(value * 10.0); 336 } 337 setFontOffset(const int value)338 void SlaDocumentRtfOutput::setFontOffset(const int value) 339 { 340 m_textCharStyle.top().setBaselineOffset((value * 10000 / 2) / m_textCharStyle.top().fontSize()); 341 } 342 setFontStretch(const int value)343 void SlaDocumentRtfOutput::setFontStretch(const int value) 344 { 345 m_textCharStyle.top().setTracking((value * 10000 / 4) / m_textCharStyle.top().fontSize()); 346 } 347 setFontStretchTw(const int value)348 void SlaDocumentRtfOutput::setFontStretchTw(const int value) 349 { 350 m_textCharStyle.top().setTracking((pixelsFromTwips(value) * 10000) / m_textCharStyle.top().fontSize()); 351 } 352 setFontPointSize(const int pointSize)353 void SlaDocumentRtfOutput::setFontPointSize(const int pointSize) 354 { 355 m_textCharStyle.top().setFontSize(pointSize * 10.0); 356 m_textStyle.top().setLineSpacing(pointSize); 357 } 358 setFontLineSpacing(const int value)359 void SlaDocumentRtfOutput::setFontLineSpacing(const int value) 360 { 361 if (value == 0) 362 m_textStyle.top().setLineSpacingMode(ParagraphStyle::AutomaticLineSpacing); 363 else 364 { 365 m_textStyle.top().setLineSpacingMode(ParagraphStyle::FixedLineSpacing); 366 m_textStyle.top().setLineSpacing(pixelsFromTwips(qAbs(value))); 367 } 368 } 369 setForegroundColour(const int colourIndex)370 void SlaDocumentRtfOutput::setForegroundColour(const int colourIndex) 371 { 372 if ((m_colourTable.count() != 0) && (colourIndex < m_colourTable.count())) 373 m_textCharStyle.top().setFillColor(m_colourTable.value(colourIndex)); 374 } 375 setHighlightColour(const int colourIndex)376 void SlaDocumentRtfOutput::setHighlightColour(const int colourIndex) 377 { 378 // qDebug() << "setHighlightColour"; 379 } 380 setParagraphPatternBackgroundColour(const int colourIndex)381 void SlaDocumentRtfOutput::setParagraphPatternBackgroundColour(const int colourIndex) 382 { 383 if ((m_colourTable.count() != 0) && (colourIndex < m_colourTable.count())) 384 m_textStyle.top().setBackgroundColor(m_colourTable.value(colourIndex)); 385 } 386 setCharacterPatternBackgroundColour(const int colourIndex)387 void SlaDocumentRtfOutput::setCharacterPatternBackgroundColour(const int colourIndex) 388 { 389 if ((m_colourTable.count() != 0) && (colourIndex < m_colourTable.count())) 390 m_textCharStyle.top().setBackColor(m_colourTable.value(colourIndex)); 391 } 392 setFont(const int fontIndex)393 void SlaDocumentRtfOutput::setFont(const int fontIndex) 394 { 395 if (m_fontTableReal.contains(fontIndex)) 396 { 397 m_textCharStyle.top().setFont(PrefsManager::instance().appPrefs.fontPrefs.AvailFonts[m_fontTableReal[fontIndex].fontName()]); 398 if (m_fontTableReal[fontIndex].encoding() > 0) 399 setEncoding(m_fontTableReal[fontIndex].encoding()); 400 } 401 else 402 { 403 if (m_fontTable.contains(fontIndex)) 404 { 405 FontTableEntry fontTableEntry = m_fontTable[fontIndex]; 406 QString fontName = getFontName(fontTableEntry.fontName()); 407 m_textCharStyle.top().setFont(PrefsManager::instance().appPrefs.fontPrefs.AvailFonts[fontName]); 408 fontTableEntry.setFontName(fontName); 409 m_fontTableReal.insert(fontIndex, fontTableEntry); 410 if (fontTableEntry.encoding() > 0) 411 setEncoding(fontTableEntry.encoding()); 412 } 413 } 414 } 415 setDefaultFont(const int fontIndex)416 void SlaDocumentRtfOutput::setDefaultFont(const int fontIndex) 417 { 418 setFont(fontIndex); 419 } 420 appendToColourTable(const QColor & colour)421 void SlaDocumentRtfOutput::appendToColourTable(const QColor &colour) 422 { 423 ScColor tmp; 424 tmp.fromQColor(colour); 425 tmp.setSpotColor(false); 426 tmp.setRegistrationColor(false); 427 QString fNam = m_Doc->PageColors.tryAddColor("FromRtf"+colour.name(), tmp); 428 m_colourTable.append(fNam); 429 } 430 insertFontTableEntry(FontTableEntry fontTableEntry,quint32 fontTableIndex)431 void SlaDocumentRtfOutput::insertFontTableEntry(FontTableEntry fontTableEntry, quint32 fontTableIndex) 432 { 433 m_fontTable.insert(fontTableIndex, fontTableEntry); 434 435 } 436 insertStyleSheetTableEntry(quint32 stylesheetTableIndex,const ParagraphStyle & stylesheetTableEntry)437 void SlaDocumentRtfOutput::insertStyleSheetTableEntry(quint32 stylesheetTableIndex, const ParagraphStyle& stylesheetTableEntry) 438 { 439 ParagraphStyle pStyle(stylesheetTableEntry); 440 if (m_prefixName) 441 pStyle.setName(m_item->itemName() + "_" + stylesheetTableEntry.name()); 442 if (pStyle.charStyle().fontVariant() != "") 443 { 444 int fontInd = pStyle.charStyle().fontVariant().toInt(); 445 pStyle.charStyle().setFontVariant(""); 446 if (m_fontTable.contains(fontInd)) 447 { 448 FontTableEntry fontTableEntry = m_fontTable[fontInd]; 449 QString fontName = getFontName(fontTableEntry.fontName()); 450 pStyle.charStyle().setFont(PrefsManager::instance().appPrefs.fontPrefs.AvailFonts[fontName]); 451 fontTableEntry.setFontName(fontName); 452 m_fontTableReal.insert(fontInd, fontTableEntry); 453 } 454 } 455 StyleSet<ParagraphStyle>tmp; 456 tmp.create(pStyle); 457 m_Doc->redefineStyles(tmp, false); 458 m_stylesTable.insert(stylesheetTableIndex, pStyle); 459 } 460 resolveStyleSheetParents(QHash<quint32,int> & parentSet)461 void SlaDocumentRtfOutput::resolveStyleSheetParents(QHash<quint32, int> &parentSet) 462 { 463 for (QHash<quint32, int>::iterator it = parentSet.begin(); it != parentSet.end(); ++it) 464 { 465 if (it.value() >= 0) 466 { 467 ParagraphStyle old = m_Doc->paragraphStyle(m_stylesTable[it.key()].name()); 468 old.setParent(m_stylesTable[it.value()].name()); 469 StyleSet<ParagraphStyle>tmp2; 470 tmp2.create(old); 471 m_Doc->redefineStyles(tmp2, false); 472 m_stylesTable.insert(it.key(), old); 473 } 474 } 475 } 476 useStyleSheetTableEntry(const int styleIndex)477 void SlaDocumentRtfOutput::useStyleSheetTableEntry(const int styleIndex) 478 { 479 if (m_stylesTable.contains(styleIndex)) 480 { 481 ParagraphStyle newStyle2; 482 newStyle2.setParent(m_stylesTable[styleIndex].name()); 483 m_textStyle.pop(); 484 m_textStyle.push(newStyle2); 485 m_textCharStyle.pop(); 486 m_textCharStyle.push(newStyle2.charStyle()); 487 } 488 } 489 resetParagraphFormat()490 void SlaDocumentRtfOutput::resetParagraphFormat() 491 { 492 QString pStyle = CommonStrings::DefaultParagraphStyle; 493 ParagraphStyle newStyle; 494 newStyle.setParent(pStyle); 495 newStyle.setLineSpacingMode(ParagraphStyle::AutomaticLineSpacing); 496 m_textStyle.pop(); 497 m_textStyle.push(newStyle); 498 QList<ParagraphStyle::TabRecord> tbs; 499 tbs.clear(); 500 m_textStyle.top().setTabValues(tbs); 501 } 502 resetCharacterProperties()503 void SlaDocumentRtfOutput::resetCharacterProperties() 504 { 505 QString pStyle = CommonStrings::DefaultParagraphStyle; 506 ParagraphStyle newStyle; 507 newStyle.setParent(pStyle); 508 newStyle.setLineSpacingMode(ParagraphStyle::AutomaticLineSpacing); 509 m_textCharStyle.pop(); 510 m_textCharStyle.push(newStyle.charStyle()); 511 m_textCharStyle.top().setFontSize(120.0); 512 } 513 setParagraphAlignmentLeft()514 void SlaDocumentRtfOutput::setParagraphAlignmentLeft() 515 { 516 m_textStyle.top().setAlignment(ParagraphStyle::LeftAligned); 517 } 518 setParagraphAlignmentCentred()519 void SlaDocumentRtfOutput::setParagraphAlignmentCentred() 520 { 521 m_textStyle.top().setAlignment(ParagraphStyle::Centered); 522 } 523 setParagraphAlignmentJustified()524 void SlaDocumentRtfOutput::setParagraphAlignmentJustified() 525 { 526 m_textStyle.top().setAlignment(ParagraphStyle::Justified); 527 } 528 setParagraphAlignmentRight()529 void SlaDocumentRtfOutput::setParagraphAlignmentRight() 530 { 531 m_textStyle.top().setAlignment(ParagraphStyle::RightAligned); 532 } 533 setLeftIndent(const int twips)534 void SlaDocumentRtfOutput::setLeftIndent(const int twips) 535 { 536 m_textStyle.top().setLeftMargin(pixelsFromTwips(twips)); 537 } 538 setRightIndent(const int twips)539 void SlaDocumentRtfOutput::setRightIndent(const int twips) 540 { 541 m_textStyle.top().setRightMargin(pixelsFromTwips(twips)); 542 } 543 setSpaceBefore(const int twips)544 void SlaDocumentRtfOutput::setSpaceBefore(const int twips) 545 { 546 m_textStyle.top().setGapBefore(pixelsFromTwips(twips)); 547 } 548 setSpaceAfter(const int twips)549 void SlaDocumentRtfOutput::setSpaceAfter(const int twips) 550 { 551 m_textStyle.top().setGapAfter(pixelsFromTwips(twips)); 552 } 553 keepWithNext()554 void SlaDocumentRtfOutput::keepWithNext() 555 { 556 m_keepn = true; 557 } 558 setDropCapsLines(const int value)559 void SlaDocumentRtfOutput::setDropCapsLines(const int value) 560 { 561 m_textStyle.top().setDropCapLines(value); 562 } 563 setDropCaps()564 void SlaDocumentRtfOutput::setDropCaps() 565 { 566 m_textStyle.top().setHasDropCap(true); 567 } 568 addTabStop(const int value,const int type)569 void SlaDocumentRtfOutput::addTabStop(const int value, const int type) 570 { 571 double tabVal = pixelsFromTwips(value); 572 ParagraphStyle::TabRecord tb; 573 tb.tabType = type; 574 tb.tabPosition = tabVal; 575 tb.tabFillChar = QChar(); 576 QList<ParagraphStyle::TabRecord> tbs = m_textStyle.top().tabValues(); 577 if (tbs.count() == 0) 578 tbs.append(tb); 579 else 580 { 581 bool ins = false; 582 for (int a = 0; a < tbs.count() - 1; a++) 583 { 584 if ((tb > tbs[a]) && (tb < tbs[a+1])) 585 { 586 tbs.insert(a, tb); 587 ins = true; 588 break; 589 } 590 } 591 if (!ins) 592 tbs.append(tb); 593 } 594 m_textStyle.top().setTabValues(tbs); 595 } 596 setFirstLineIndent(const int twips)597 void SlaDocumentRtfOutput::setFirstLineIndent(const int twips) 598 { 599 m_textStyle.top().setFirstIndent(pixelsFromTwips(twips)); 600 } 601 setFontSuperscript()602 void SlaDocumentRtfOutput::setFontSuperscript() 603 { 604 StyleFlag styleEffects = m_textCharStyle.top().effects(); 605 styleEffects |= ScStyle_Superscript; 606 m_textCharStyle.top().setFeatures(styleEffects.featureList()); 607 } 608 setFontSubscript()609 void SlaDocumentRtfOutput::setFontSubscript() 610 { 611 StyleFlag styleEffects = m_textCharStyle.top().effects(); 612 styleEffects |= ScStyle_Subscript; 613 m_textCharStyle.top().setFeatures(styleEffects.featureList()); 614 } 615 setTextDirectionLeftToRight()616 void SlaDocumentRtfOutput::setTextDirectionLeftToRight() 617 { 618 // qDebug() << "setTextDirectionLeftToRight"; 619 } 620 setTextDirectionRightToLeft()621 void SlaDocumentRtfOutput::setTextDirectionRightToLeft() 622 { 623 // qDebug() << "setTextDirectionRightToLeft"; 624 } 625 createImage(const QByteArray & image,int width,int height,int type)626 void SlaDocumentRtfOutput::createImage(const QByteArray &image, int width, int height, int type) 627 { 628 QString imgExt = ""; 629 double ww = pixelsFromTwips(width); 630 double hh = pixelsFromTwips(height); 631 if ((type == 0) || (type == 3) || (type == 4)) 632 { 633 if (type == 0) 634 imgExt = "jpg"; 635 else if (type == 3) 636 imgExt = "pict"; 637 else if (type == 4) 638 imgExt = "pmg"; 639 QTemporaryFile *tempFile = new QTemporaryFile(QDir::tempPath() + QString("/scribus_temp_rtf_XXXXXX.") + imgExt); 640 tempFile->setAutoRemove(false); 641 if (tempFile->open()) 642 { 643 tempFile->write(image); 644 QString fileName = getLongPathName(tempFile->fileName()); 645 tempFile->close(); 646 int posT = m_item->itemText.length(); 647 int z = m_Doc->itemAdd(PageItem::ImageFrame, PageItem::Rectangle, 0, 0, ww, hh, 0, CommonStrings::None, CommonStrings::None); 648 PageItem* item = m_Doc->Items->at(z); 649 item->OldB2 = item->width(); 650 item->OldH2 = item->height(); 651 item->updateClip(); 652 item->isInlineImage = true; 653 item->isTempFile = true; 654 item->AspectRatio = true; 655 item->ScaleType = false; 656 m_Doc->loadPict(fileName, item); 657 m_Doc->Items->takeAt(z); 658 item->isEmbedded = true; 659 item->gXpos = 0; 660 item->gYpos = 0; 661 item->gWidth = item->width(); 662 item->gHeight = item->height(); 663 int fIndex = m_Doc->addToInlineFrames(item); 664 m_item->itemText.insertObject(fIndex); 665 m_item->itemText.applyStyle(posT, m_textStyle.top()); 666 } 667 delete tempFile; 668 } 669 else if ((type == 1) || (type == 2)) 670 { 671 if (type == 1) 672 imgExt = "wmf"; 673 else if (type == 2) 674 imgExt = "emf"; 675 QTemporaryFile *tempFile = new QTemporaryFile(QDir::tempPath() + QString("/scribus_temp_rtf_XXXXXX.") + imgExt); 676 tempFile->setAutoRemove(false); 677 if (tempFile->open()) 678 { 679 tempFile->write(image); 680 QString fileName = getLongPathName(tempFile->fileName()); 681 tempFile->close(); 682 FileLoader *fileLoader = new FileLoader(fileName); 683 int testResult = fileLoader->testFile(); 684 delete fileLoader; 685 if (testResult != -1) 686 { 687 const FileFormat * fmt = LoadSavePlugin::getFormatById(testResult); 688 if (fmt) 689 { 690 fmt->setupTargets(m_Doc, 0, 0, 0, &(PrefsManager::instance().appPrefs.fontPrefs.AvailFonts)); 691 fmt->loadFile(fileName, LoadSavePlugin::lfUseCurrentPage|LoadSavePlugin::lfInteractive|LoadSavePlugin::lfScripted); 692 if (m_Doc->m_Selection->count() > 0) 693 { 694 int posT = m_item->itemText.length(); 695 PageItem* item = m_Doc->groupObjectsSelection(); 696 item->setWidthHeight(ww, hh, true); 697 item->OldB2 = item->width(); 698 item->OldH2 = item->height(); 699 item->updateClip(); 700 m_Doc->Items->removeAll(item); 701 item->isEmbedded = true; 702 item->gXpos = 0; 703 item->gYpos = 0; 704 item->gWidth = item->width(); 705 item->gHeight = item->height(); 706 int fIndex = m_Doc->addToInlineFrames(item); 707 m_item->itemText.insertObject(fIndex); 708 m_item->itemText.applyStyle(posT, m_textStyle.top()); 709 } 710 } 711 } 712 } 713 delete tempFile; 714 } 715 } 716 setPageHeight(const int pageHeight)717 void SlaDocumentRtfOutput::setPageHeight(const int pageHeight) 718 { 719 } 720 setPageWidth(const int pageWidth)721 void SlaDocumentRtfOutput::setPageWidth(const int pageWidth) 722 { 723 } 724 getFontName(const QString & name)725 QString SlaDocumentRtfOutput::getFontName(const QString& name) 726 { 727 QString fontName(name); 728 SCFontsIterator it(PrefsManager::instance().appPrefs.fontPrefs.AvailFonts); 729 for ( ; it.hasNext(); it.next()) 730 { 731 if (it.current().family().toLower() == fontName.toLower()) 732 { 733 if (it.currentKey().toLower() == fontName.toLower()) // exact Match 734 { 735 return fontName; 736 } 737 else 738 { 739 QStringList slist = PrefsManager::instance().appPrefs.fontPrefs.AvailFonts.fontMap[it.current().family()]; 740 slist.sort(); 741 if (slist.count() > 0) 742 { 743 int reInd = slist.indexOf("Regular"); 744 if (reInd < 0) 745 fontName = it.current().family() + " " + slist[0]; 746 else 747 fontName = it.current().family() + " " + slist[reInd]; 748 return fontName; 749 } 750 } 751 } 752 } 753 if (!PrefsManager::instance().appPrefs.fontPrefs.GFontSub.contains(fontName)) 754 { 755 qApp->changeOverrideCursor(QCursor(Qt::ArrowCursor)); 756 MissingFont dia(0, fontName, m_Doc); 757 dia.exec(); 758 qApp->changeOverrideCursor(QCursor(Qt::WaitCursor)); 759 PrefsManager::instance().appPrefs.fontPrefs.GFontSub[fontName] = dia.getReplacementFont(); 760 fontName = dia.getReplacementFont(); 761 } 762 else 763 fontName = PrefsManager::instance().appPrefs.fontPrefs.GFontSub[fontName]; 764 return fontName; 765 } 766 pixelsFromTwips(const int twips)767 double SlaDocumentRtfOutput::pixelsFromTwips(const int twips) 768 { 769 return twips / 1440.0 * 72.0; 770 } 771 } 772