summaryrefslogtreecommitdiff
path: root/Source/CPack/WiX/cmWIXRichTextFormatWriter.cxx
blob: 2c99a2284bc285fcec0787851acd3283b044893c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
#include "cmWIXRichTextFormatWriter.h"

#include "cmVersion.h"

cmWIXRichTextFormatWriter::cmWIXRichTextFormatWriter(
  std::string const& filename)
  : File(filename.c_str(), std::ios::binary)
{
  StartGroup();
  WriteHeader();
  WriteDocumentPrefix();
}

cmWIXRichTextFormatWriter::~cmWIXRichTextFormatWriter()
{
  EndGroup();

  /* I haven't seen this in the RTF spec but
   *  wordpad terminates its RTF like this */
  File << "\r\n";
  File.put(0);
}

void cmWIXRichTextFormatWriter::AddText(std::string const& text)
{
  typedef unsigned char rtf_byte_t;

  for (size_t i = 0; i < text.size(); ++i) {
    rtf_byte_t c = rtf_byte_t(text[i]);

    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) {
          File << c;
        } 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);
          }
        }
      } break;
    }
  }
}

void cmWIXRichTextFormatWriter::WriteHeader()
{
  ControlWord("rtf1");
  ControlWord("ansi");
  ControlWord("ansicpg1252");
  ControlWord("deff0");
  ControlWord("deflang1031");

  WriteFontTable();
  WriteColorTable();
  WriteGenerator();
}

void cmWIXRichTextFormatWriter::WriteFontTable()
{
  StartGroup();
  ControlWord("fonttbl");

  StartGroup();
  ControlWord("f0");
  ControlWord("fswiss");
  ControlWord("fcharset0 Arial;");
  EndGroup();

  EndGroup();
}

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();
}

void cmWIXRichTextFormatWriter::WriteGenerator()
{
  StartGroup();
  NewControlWord("generator");
  File << " CPack WiX Generator (" << cmVersion::GetCMakeVersion() << ");";
  EndGroup();
}

void cmWIXRichTextFormatWriter::WriteDocumentPrefix()
{
  ControlWord("viewkind4");
  ControlWord("uc1");
  ControlWord("pard");
  ControlWord("f0");
  ControlWord("fs20");
}

void cmWIXRichTextFormatWriter::ControlWord(std::string const& keyword)
{
  File << "\\" << keyword;
}

void cmWIXRichTextFormatWriter::NewControlWord(std::string const& keyword)
{
  File << "\\*\\" << keyword;
}

void cmWIXRichTextFormatWriter::StartGroup()
{
  File.put('{');
}

void cmWIXRichTextFormatWriter::EndGroup()
{
  File.put('}');
}

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 ");
}