RedPanda-CPP/RedPandaIDE/qsynedit/exporter/synrtfexporter.cpp

151 lines
4.5 KiB
C++
Raw Normal View History

#include "synrtfexporter.h"
SynRTFExporter::SynRTFExporter():SynExporter()
{
mDefaultFilter = "Rich Text Format Documents (*.rtf)|*.rtf";
mClipboardFormat = "application/x-qt-windows-mime;value=\"Rich Format Text\"";
//mClipboardFormat = "Rich Format Text";
// setup array of chars to be replaced
mReplaceReserved['\\'] = "\\\\";
mReplaceReserved['{'] = "\\{";
mReplaceReserved['}'] = "\\}";
}
QString SynRTFExporter::ColorToRTF(const QColor &AColor) const
{
return QString("\\red%1\\green%2\\blue%3;")
.arg(AColor.red())
.arg(AColor.green())
.arg(AColor.blue());
}
int SynRTFExporter::GetColorIndex(const QColor &AColor)
{
int index = mListColors.indexOf(AColor);
if (index<0) {
mListColors.append(AColor);
index = mListColors.length()-1;
}
return index;
}
QString SynRTFExporter::GetFontTable()
{
QString Result = "{\\fonttbl{\\f0\\fmodern\\fcharset134 "
+ ReplaceReservedChars(mFont.family());
Result = Result + ";}}" + lineBreak();
return Result;
}
void SynRTFExporter::FormatAttributeDone(bool , bool , SynFontStyles FontStylesChanged)
{
// nothing to do about the color, but reset the font style
if (FontStylesChanged.testFlag(SynFontStyle::fsBold)) {
mAttributesChanged = true;
AddData("\\b0");
}
if (FontStylesChanged.testFlag(SynFontStyle::fsItalic)) {
mAttributesChanged = true;
AddData("\\i0");
}
if (FontStylesChanged.testFlag(SynFontStyle::fsUnderline)) {
mAttributesChanged = true;
AddData("\\ul0");
}
if (FontStylesChanged.testFlag(SynFontStyle::fsStrikeOut)) {
mAttributesChanged = true;
AddData("\\strike0");
}
}
void SynRTFExporter::FormatAttributeInit(bool BackgroundChanged, bool ForegroundChanged, SynFontStyles FontStylesChanged)
{
// background color
if (BackgroundChanged) {
AddData(QString("\\chshdng0\\chcbpat%1\\cb%2\\highlight%3 ")
.arg(GetColorIndex(mLastBG))
.arg(GetColorIndex(mLastBG))
.arg(GetColorIndex(mLastBG)));
mAttributesChanged = true;
}
// text color
if (ForegroundChanged) {
AddData(QString("\\cf%1").arg(GetColorIndex(mLastFG)));
mAttributesChanged = true;
}
// font styles
// nothing to do about the color, but reset the font style
if (FontStylesChanged.testFlag(SynFontStyle::fsBold)) {
mAttributesChanged = true;
AddData("\\b0");
}
if (FontStylesChanged.testFlag(SynFontStyle::fsItalic)) {
mAttributesChanged = true;
AddData("\\i0");
}
if (FontStylesChanged.testFlag(SynFontStyle::fsUnderline)) {
mAttributesChanged = true;
AddData("\\ul0");
}
if (FontStylesChanged.testFlag(SynFontStyle::fsStrikeOut)) {
mAttributesChanged = true;
AddData("\\strike0");
}
if (mAttributesChanged) {
AddData(" ");
mAttributesChanged = false;
}
}
void SynRTFExporter::FormatAfterLastAttribute()
{
// no need to reset the font style here...
}
void SynRTFExporter::FormatBeforeFirstAttribute(bool BackgroundChanged, bool ForegroundChanged, SynFontStyles FontStylesChanged)
{
FormatAttributeInit(BackgroundChanged, ForegroundChanged, FontStylesChanged);
}
void SynRTFExporter::FormatNewLine()
{
AddNewLine();
AddData("\\par ");
}
QString SynRTFExporter::GetFooter()
{
return "}";
}
QString SynRTFExporter::GetFormatName()
{
return "RTF";
}
QString SynRTFExporter::GetHeader()
{
QString Result = "{\\rtf1\\ansi\\deff0\\deftab720" + GetFontTable();
// all the colors
Result = Result + "{\\colortbl";
for (int i = 0; i<mListColors.count();i++) {
Result = Result + ColorToRTF(mListColors[i]);
}
Result = Result + ColorToRTF(mBackgroundColor);
Result = Result + "}" + lineBreak();
// title and creator comment
Result = Result + "{\\info{\\comment Generated by the QSynEdit RTF " +
"exporter}" + lineBreak();
Result = Result + "{\\title " + mTitle + "}}" + lineBreak();
// if (mUseBackground)
// Result = Result + { TODO } #13#10;
Result = Result + QString("\\deflang1033\\pard\\plain\\f0\\fs%1 ").arg(2 * mFont.pointSize());
if (mUseBackground)
Result = Result + QString("\\chshdng0\\chcbpat%1\\cb%2\\highlight%3 ")
.arg(GetColorIndex(mLastBG))
.arg(GetColorIndex(mBackgroundColor))
.arg(GetColorIndex(mBackgroundColor));
return Result;
}