2016-09-27 22:01:08 +03:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2012-10-03 18:08:49 +04:00
|
|
|
#include "cmWIXRichTextFormatWriter.h"
|
|
|
|
|
|
|
|
#include <cmVersion.h>
|
|
|
|
|
|
|
|
cmWIXRichTextFormatWriter::cmWIXRichTextFormatWriter(
|
2016-05-16 17:34:04 +03:00
|
|
|
std::string const& filename)
|
|
|
|
: File(filename.c_str(), std::ios::binary)
|
2012-10-03 18:08:49 +04:00
|
|
|
{
|
|
|
|
StartGroup();
|
|
|
|
WriteHeader();
|
|
|
|
WriteDocumentPrefix();
|
|
|
|
}
|
|
|
|
|
|
|
|
cmWIXRichTextFormatWriter::~cmWIXRichTextFormatWriter()
|
|
|
|
{
|
|
|
|
EndGroup();
|
|
|
|
|
|
|
|
/* I haven't seen this in the RTF spec but
|
|
|
|
* wordpad terminates its RTF like this */
|
2013-12-20 21:12:01 +04:00
|
|
|
File << "\r\n";
|
|
|
|
File.put(0);
|
2012-10-03 18:08:49 +04:00
|
|
|
}
|
|
|
|
|
2014-02-25 02:21:12 +04:00
|
|
|
void cmWIXRichTextFormatWriter::AddText(std::string const& text)
|
2012-10-03 18:08:49 +04:00
|
|
|
{
|
|
|
|
typedef unsigned char rtf_byte_t;
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
for (size_t i = 0; i < text.size(); ++i) {
|
2012-10-03 18:08:49 +04:00
|
|
|
rtf_byte_t c = rtf_byte_t(text[i]);
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
switch (c) {
|
|
|
|
case '\\':
|
|
|
|
File << "\\\\";
|
|
|
|
break;
|
|
|
|
case '{':
|
|
|
|
File << "\\{";
|
|
|
|
break;
|
|
|
|
case '}':
|
|
|
|
File << "\\}";
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
File << "\\par\r\n";
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
continue;
|
|
|
|
default: {
|
|
|
|
if (c <= 0x7F) {
|
2013-12-20 21:12:01 +04:00
|
|
|
File << c;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
|
|
|
if (c <= 0xC0) {
|
|
|
|
EmitInvalidCodepoint(c);
|
|
|
|
} else if (c < 0xE0 && i + 1 < text.size()) {
|
|
|
|
EmitUnicodeCodepoint((text[i + 1] & 0x3F) | ((c & 0x1F) << 6));
|
|
|
|
i += 1;
|
|
|
|
} else if (c < 0xF0 && i + 2 < text.size()) {
|
|
|
|
EmitUnicodeCodepoint((text[i + 2] & 0x3F) |
|
|
|
|
((text[i + 1] & 0x3F) << 6) |
|
|
|
|
((c & 0xF) << 12));
|
|
|
|
i += 2;
|
|
|
|
} else if (c < 0xF8 && i + 3 < text.size()) {
|
|
|
|
EmitUnicodeCodepoint(
|
|
|
|
(text[i + 3] & 0x3F) | ((text[i + 2] & 0x3F) << 6) |
|
|
|
|
((text[i + 1] & 0x3F) << 12) | ((c & 0x7) << 18));
|
|
|
|
i += 3;
|
|
|
|
} else {
|
|
|
|
EmitInvalidCodepoint(c);
|
2012-10-03 18:08:49 +04:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
} break;
|
2012-10-03 18:08:49 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-10-03 18:08:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmWIXRichTextFormatWriter::WriteHeader()
|
|
|
|
{
|
|
|
|
ControlWord("rtf1");
|
|
|
|
ControlWord("ansi");
|
|
|
|
ControlWord("ansicpg1252");
|
|
|
|
ControlWord("deff0");
|
|
|
|
ControlWord("deflang1031");
|
|
|
|
|
|
|
|
WriteFontTable();
|
2013-12-26 18:23:54 +04:00
|
|
|
WriteColorTable();
|
2012-10-03 18:08:49 +04:00
|
|
|
WriteGenerator();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmWIXRichTextFormatWriter::WriteFontTable()
|
|
|
|
{
|
|
|
|
StartGroup();
|
|
|
|
ControlWord("fonttbl");
|
|
|
|
|
|
|
|
StartGroup();
|
|
|
|
ControlWord("f0");
|
|
|
|
ControlWord("fswiss");
|
|
|
|
ControlWord("fcharset0 Arial;");
|
|
|
|
EndGroup();
|
|
|
|
|
|
|
|
EndGroup();
|
|
|
|
}
|
|
|
|
|
2013-12-26 18:23:54 +04:00
|
|
|
void cmWIXRichTextFormatWriter::WriteColorTable()
|
|
|
|
{
|
|
|
|
StartGroup();
|
|
|
|
ControlWord("colortbl ;");
|
|
|
|
ControlWord("red255");
|
|
|
|
ControlWord("green0");
|
|
|
|
ControlWord("blue0;");
|
|
|
|
ControlWord("red0");
|
|
|
|
ControlWord("green255");
|
|
|
|
ControlWord("blue0;");
|
|
|
|
ControlWord("red0");
|
|
|
|
ControlWord("green0");
|
|
|
|
ControlWord("blue255;");
|
|
|
|
EndGroup();
|
|
|
|
}
|
|
|
|
|
2012-10-03 18:08:49 +04:00
|
|
|
void cmWIXRichTextFormatWriter::WriteGenerator()
|
|
|
|
{
|
|
|
|
StartGroup();
|
|
|
|
NewControlWord("generator");
|
2013-12-20 21:12:01 +04:00
|
|
|
File << " CPack WiX Generator (" << cmVersion::GetCMakeVersion() << ");";
|
2012-10-03 18:08:49 +04:00
|
|
|
EndGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmWIXRichTextFormatWriter::WriteDocumentPrefix()
|
|
|
|
{
|
|
|
|
ControlWord("viewkind4");
|
|
|
|
ControlWord("uc1");
|
|
|
|
ControlWord("pard");
|
|
|
|
ControlWord("f0");
|
|
|
|
ControlWord("fs20");
|
|
|
|
}
|
|
|
|
|
2014-02-25 02:21:12 +04:00
|
|
|
void cmWIXRichTextFormatWriter::ControlWord(std::string const& keyword)
|
2012-10-03 18:08:49 +04:00
|
|
|
{
|
2013-12-20 21:12:01 +04:00
|
|
|
File << "\\" << keyword;
|
2012-10-03 18:08:49 +04:00
|
|
|
}
|
|
|
|
|
2014-02-25 02:21:12 +04:00
|
|
|
void cmWIXRichTextFormatWriter::NewControlWord(std::string const& keyword)
|
2012-10-03 18:08:49 +04:00
|
|
|
{
|
2013-12-20 21:12:01 +04:00
|
|
|
File << "\\*\\" << keyword;
|
2012-10-03 18:08:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmWIXRichTextFormatWriter::StartGroup()
|
|
|
|
{
|
2013-12-20 21:12:01 +04:00
|
|
|
File.put('{');
|
2012-10-03 18:08:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmWIXRichTextFormatWriter::EndGroup()
|
|
|
|
{
|
2013-12-20 21:12:01 +04:00
|
|
|
File.put('}');
|
2012-10-03 18:08:49 +04:00
|
|
|
}
|
2013-12-26 18:23:54 +04:00
|
|
|
|
|
|
|
void cmWIXRichTextFormatWriter::EmitUnicodeCodepoint(int c)
|
|
|
|
{
|
|
|
|
// Do not emit byte order mark (BOM)
|
2016-05-16 17:34:04 +03:00
|
|
|
if (c == 0xFEFF) {
|
2013-12-26 18:23:54 +04:00
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (c <= 0xFFFF) {
|
2013-12-26 18:23:54 +04:00
|
|
|
EmitUnicodeSurrogate(c);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2013-12-26 18:23:54 +04:00
|
|
|
c -= 0x10000;
|
|
|
|
EmitUnicodeSurrogate(((c >> 10) & 0x3FF) + 0xD800);
|
|
|
|
EmitUnicodeSurrogate((c & 0x3FF) + 0xDC00);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-12-26 18:23:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmWIXRichTextFormatWriter::EmitUnicodeSurrogate(int c)
|
|
|
|
{
|
|
|
|
ControlWord("u");
|
2016-05-16 17:34:04 +03:00
|
|
|
if (c <= 32767) {
|
2013-12-26 18:23:54 +04:00
|
|
|
File << c;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2013-12-26 18:23:54 +04:00
|
|
|
File << (c - 65536);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-12-26 18:23:54 +04:00
|
|
|
File << "?";
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmWIXRichTextFormatWriter::EmitInvalidCodepoint(int c)
|
|
|
|
{
|
|
|
|
ControlWord("cf1 ");
|
|
|
|
File << "[INVALID-BYTE-" << int(c) << "]";
|
|
|
|
ControlWord("cf0 ");
|
|
|
|
}
|