summaryrefslogtreecommitdiff
path: root/Source/CPack/WiX/cmWIXRichTextFormatWriter.cxx
blob: 774c22c76771efddf43b4d1f2d69cc755c209807 (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
/*============================================================================
  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(
  const std::string& 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(const std::string& 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
          {
          file << "[NON-ASCII-" << int(c) << "]";
          }
        }
      break;
      }
    }
}

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

  WriteFontTable();
  WriteGenerator();
}

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

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

  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(const std::string& keyword)
{
  file << "\\" << keyword;
}

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

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

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