2012-10-03 18:08:49 +04:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2012 Kitware, Inc.
|
|
|
|
|
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
|
|
|
|
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
|
|
|
|
|
|
|
#include "cmWIXRichTextFormatWriter.h"
|
|
|
|
|
|
|
|
#include <cmVersion.h>
|
|
|
|
|
|
|
|
cmWIXRichTextFormatWriter::cmWIXRichTextFormatWriter(
|
2014-02-25 02:21:12 +04:00
|
|
|
std::string const& filename):
|
2013-12-20 21:12:01 +04:00
|
|
|
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;
|
|
|
|
|
2012-12-04 23:37:41 +04: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]);
|
|
|
|
|
|
|
|
switch(c)
|
|
|
|
{
|
|
|
|
case '\\':
|
2013-12-20 21:12:01 +04:00
|
|
|
File << "\\\\";
|
2012-10-03 18:08:49 +04:00
|
|
|
break;
|
|
|
|
case '{':
|
2013-12-20 21:12:01 +04:00
|
|
|
File << "\\{";
|
2012-10-03 18:08:49 +04:00
|
|
|
break;
|
|
|
|
case '}':
|
2013-12-20 21:12:01 +04:00
|
|
|
File << "\\}";
|
2012-10-03 18:08:49 +04:00
|
|
|
break;
|
|
|
|
case '\n':
|
2013-12-20 21:12:01 +04:00
|
|
|
File << "\\par\r\n";
|
2012-10-03 18:08:49 +04:00
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
if(c <= 0x7F)
|
|
|
|
{
|
2013-12-20 21:12:01 +04:00
|
|
|
File << c;
|
2012-10-03 18:08:49 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-26 18:23:54 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
if(c == 0xFEFF)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if(c <= 0xFFFF)
|
|
|
|
{
|
|
|
|
EmitUnicodeSurrogate(c);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c -= 0x10000;
|
|
|
|
EmitUnicodeSurrogate(((c >> 10) & 0x3FF) + 0xD800);
|
|
|
|
EmitUnicodeSurrogate((c & 0x3FF) + 0xDC00);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmWIXRichTextFormatWriter::EmitUnicodeSurrogate(int c)
|
|
|
|
{
|
|
|
|
ControlWord("u");
|
|
|
|
if(c <= 32767)
|
|
|
|
{
|
|
|
|
File << c;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
File << (c - 65536);
|
|
|
|
}
|
|
|
|
File << "?";
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmWIXRichTextFormatWriter::EmitInvalidCodepoint(int c)
|
|
|
|
{
|
|
|
|
ControlWord("cf1 ");
|
|
|
|
File << "[INVALID-BYTE-" << int(c) << "]";
|
|
|
|
ControlWord("cf0 ");
|
|
|
|
}
|