summaryrefslogtreecommitdiff
path: root/src/Text.cc
blob: 16636eb9eed9563e649735f19db04b558fb681ad (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright © 2011, 2015 Richard Kettlewell.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#include <config.h>
#include "Document.h"
#include "Utils.h"
#include "Errors.h"
#include <ostream>
#include <sstream>
#include <cstdio>
#include <cstdarg>

void Document::LinearContainer::renderTextContents(std::ostream &os) const {
  for(auto &node: nodes)
    node->renderText(os);
}

void Document::String::renderText(std::ostream &os) const {
  os << text;
}

void Document::LinearContainer::renderText(std::ostream &os) const {
  renderTextContents(os);
}

void Document::wordWrapText(std::ostream &os,
                            const std::string &s,
                            size_t width,
                            size_t indent,
                            bool indentFirst) {
  size_t x = 0;
  std::string::size_type pos = 0;
  bool first = true;
  while(pos < s.size()) {
    // Skip whitespace
    if(isspace(s[pos])) {
      ++pos;
      continue;
    }
    // Find the length of this word
    std::string::size_type len = 0;
    while(pos + len < s.size() && !isspace(s[pos + len]))
      ++len;
    if(x) {
      if(x + len + 1 <= width) {
        os << ' ';
        ++x;
      } else {
        os << '\n';
        x = 0;
      }
    } else {
      if(indentFirst || !first)
        for(size_t i = 0; i < indent; ++i)
          os << ' ';
    }
    os.write(s.data() + pos, len);
    x += len;
    pos += len;
  }
  if(x)
    os << '\n';
}

void Document::Paragraph::renderText(std::ostream &os) const {
  // Convert to a single string
  std::stringstream ss;
  renderTextContents(ss);
  wordWrapText(os, ss.str(), 80);           // TODO configurable width
  os << '\n';
}

void Document::Verbatim::renderText(std::ostream &os) const {
  renderTextContents(os);
  os << '\n';
}

void Document::List::renderText(std::ostream &os) const {
  for(size_t n = 0; n < nodes.size(); ++n) {
    char prefix[64];
    std::stringstream ss;
    nodes[n]->renderText(ss);
    switch(type) {
    case OrderedList:
      snprintf(prefix, sizeof prefix, " %zu. ", n + 1);
      os << prefix;
      break;
    case UnorderedList:
      strcpy(prefix, " * ");
      break;
    }
    os << prefix;
    wordWrapText(os, ss.str(), 80 - strlen(prefix), strlen(prefix), false);
  }
  os << '\n';
}

void Document::ListEntry::renderText(std::ostream &os) const {
  renderTextContents(os);
}

void Document::Heading::renderText(std::ostream &os) const {
  if(level > 6)
    throw std::runtime_error("heading level too high");
  switch(level) {
  case 1:
    os << "==== ";
    renderTextContents(os);
    os << " ====";
    os << '\n';
    break;
  case 2:
    os << "=== ";
    renderTextContents(os);
    os << " ===";
    os << '\n';
    break;
  case 3:
    os << "== ";
    renderTextContents(os);
    os << " ==";
    os << '\n';
    break;
  case 4:
  case 5:
  case 6:
    os << "* ";
    renderTextContents(os);
    os << '\n';
    break;
  }
  os << '\n';
}

void Document::Cell::renderText(std::ostream &os) const {
  renderTextContents(os);
}

void Document::Table::renderText(std::ostream &os) const {
  // First pass: compute column widths based on single-column cells
  const int tableColumns = width(), tableRows = height();
  std::vector<size_t> columnWidths;
  for(int xpos = 0; xpos < tableColumns; ++xpos) {
    size_t columnWidth = 0;
    for(int ypos = 0; ypos < tableRows; ++ypos) {
      Cell *cell = occupied(xpos, ypos);
      // We only consider single-width cells
      if(cell && cell->w == 1) {
        std::stringstream ss;
        cell->renderText(ss);
        size_t w = ss.str().size();
        if(w > columnWidth)
          columnWidth = w;
      }
    }
    columnWidths.push_back(columnWidth);
  }
  // Second pass: add extra space to accomodate multiple-column cells
  for(int xpos = 0; xpos < tableColumns; ++xpos) {
    for(int ypos = 0; ypos < tableRows; ++ypos) {
      Cell *cell = occupied(xpos, ypos);
      if(cell && cell->x == xpos && cell->w > 1) {
        std::stringstream ss;
        cell->renderText(ss);
        // Determine the space available
        size_t availableWidth = 3 * (cell->w - 1);
        for(int i = 0; i < cell->w; ++i)
          availableWidth += columnWidths[xpos + i];
        if(ss.str().size() <= availableWidth)
          continue;
        // If there's a shortage of space, bump up the first column
        columnWidths[xpos] += ss.str().size() - availableWidth;
      }
    }
  }
  // We lay out as | <data> | <data> | ... |
  // Third pass: render the table
  for(int ypos = 0; ypos < tableRows; ++ypos) {
    for(int xpos = 0; xpos < tableColumns; ++xpos) {
      Cell *cell = occupied(xpos, ypos);
      if(cell) {
        if(cell->y == ypos) {
          if(cell->x == xpos) {
            // First row/column of the cell
            // Determine the space available
            size_t availableWidth = 3 * (cell->w - 1);
            for(int i = 0; i < cell->w; ++i)
              availableWidth += columnWidths[xpos + i];
            if(xpos)
              os << ' ';
            os << "| ";
            std::stringstream ss;
            cell->renderText(ss);
            size_t left = availableWidth - ss.str().size();
            if(cell->heading) {
              for(size_t i = 0; i < left / 2; ++i)
                os << ' ';
              left -= left / 2;
              os << ss.str();
            } else
              os << ss.str();
            for(size_t i = 0; i < left; ++i)
              os << ' ';
          } else {
            // 2nd or subsequent column of the cell
          }
        } else {
          // 2nd or subsequent row of the cell
          if(xpos)
            os << ' ';
          os << "| ";
          for(size_t i = 0; i < columnWidths[xpos]; ++i)
            os << ' ';
        }
      } else {
        // Unfilled cell
        if(xpos)
          os << ' ';
        os << "| ";
        for(size_t i = 0; i < columnWidths[xpos]; ++i)
          os << ' ';
      }
    }
    os << '|';
    os << '\n';
  }
  os << '\n';
}

void Document::Image::renderText(std::ostream &) const {
}

void Document::RootContainer::renderText(std::ostream &os) const {
  renderTextContents(os);
}

void Document::renderText(std::ostream &os) const {
  content.renderText(os);
}