| 1 | /* |
| 2 | * text backend for Halibut |
| 3 | */ |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <assert.h> |
| 8 | #include "halibut.h" |
| 9 | |
| 10 | typedef enum { LEFT, LEFTPLUS, CENTRE } alignment; |
| 11 | typedef struct { |
| 12 | alignment align; |
| 13 | int number_at_all, just_numbers; |
| 14 | wchar_t *underline; |
| 15 | wchar_t *number_suffix; |
| 16 | } alignstruct; |
| 17 | |
| 18 | typedef struct { |
| 19 | int indent, indent_code; |
| 20 | int listindentbefore, listindentafter; |
| 21 | int width; |
| 22 | alignstruct atitle, achapter, *asect; |
| 23 | int nasect; |
| 24 | int include_version_id; |
| 25 | int indent_preambles; |
| 26 | int charset; |
| 27 | word bullet; |
| 28 | wchar_t *lquote, *rquote, *rule; |
| 29 | char *filename; |
| 30 | wchar_t *listsuffix, *startemph, *endemph; |
| 31 | } textconfig; |
| 32 | |
| 33 | typedef struct { |
| 34 | FILE *fp; |
| 35 | int charset; |
| 36 | charset_state state; |
| 37 | } textfile; |
| 38 | |
| 39 | static void text_heading(textfile *, word *, word *, word *, alignstruct, |
| 40 | int, int, textconfig *); |
| 41 | static void text_rule(textfile *, int, int, textconfig *); |
| 42 | static void text_para(textfile *, word *, wchar_t *, word *, int, int, int, |
| 43 | textconfig *); |
| 44 | static void text_codepara(textfile *, word *, int, int); |
| 45 | static void text_versionid(textfile *, word *, textconfig *); |
| 46 | |
| 47 | static void text_output(textfile *, const wchar_t *); |
| 48 | static void text_output_many(textfile *, int, wchar_t); |
| 49 | |
| 50 | static alignment utoalign(wchar_t *p) { |
| 51 | if (!ustricmp(p, L"centre") || !ustricmp(p, L"center")) |
| 52 | return CENTRE; |
| 53 | if (!ustricmp(p, L"leftplus")) |
| 54 | return LEFTPLUS; |
| 55 | return LEFT; |
| 56 | } |
| 57 | |
| 58 | static textconfig text_configure(paragraph *source) { |
| 59 | textconfig ret; |
| 60 | paragraph *p; |
| 61 | int n; |
| 62 | |
| 63 | /* |
| 64 | * Non-negotiables. |
| 65 | */ |
| 66 | ret.bullet.next = NULL; |
| 67 | ret.bullet.alt = NULL; |
| 68 | ret.bullet.type = word_Normal; |
| 69 | ret.atitle.just_numbers = FALSE; /* ignored */ |
| 70 | ret.atitle.number_at_all = TRUE; /* ignored */ |
| 71 | |
| 72 | /* |
| 73 | * Defaults. |
| 74 | */ |
| 75 | ret.indent = 7; |
| 76 | ret.indent_code = 2; |
| 77 | ret.listindentbefore = 1; |
| 78 | ret.listindentafter = 3; |
| 79 | ret.width = 68; |
| 80 | ret.atitle.align = CENTRE; |
| 81 | ret.atitle.underline = L"\x2550\0=\0\0"; |
| 82 | ret.achapter.align = LEFT; |
| 83 | ret.achapter.just_numbers = FALSE; |
| 84 | ret.achapter.number_at_all = TRUE; |
| 85 | ret.achapter.number_suffix = L": "; |
| 86 | ret.achapter.underline = L"\x203E\0-\0\0"; |
| 87 | ret.nasect = 1; |
| 88 | ret.asect = snewn(ret.nasect, alignstruct); |
| 89 | ret.asect[0].align = LEFTPLUS; |
| 90 | ret.asect[0].just_numbers = TRUE; |
| 91 | ret.asect[0].number_at_all = TRUE; |
| 92 | ret.asect[0].number_suffix = L" "; |
| 93 | ret.asect[0].underline = L"\0"; |
| 94 | ret.include_version_id = TRUE; |
| 95 | ret.indent_preambles = FALSE; |
| 96 | ret.bullet.text = L"\x2022\0-\0\0"; |
| 97 | ret.rule = L"\x2500\0-\0\0"; |
| 98 | ret.filename = dupstr("output.txt"); |
| 99 | ret.startemph = L"_\0_\0\0"; |
| 100 | ret.endemph = uadv(ret.startemph); |
| 101 | ret.listsuffix = L"."; |
| 102 | ret.charset = CS_ASCII; |
| 103 | /* |
| 104 | * Default quote characters are Unicode matched single quotes, |
| 105 | * falling back to the TeXlike `'. |
| 106 | */ |
| 107 | ret.lquote = L"\x2018\0\x2019\0`\0'\0\0"; |
| 108 | ret.rquote = uadv(ret.lquote); |
| 109 | |
| 110 | /* |
| 111 | * Two-pass configuration so that we can pick up global config |
| 112 | * (e.g. `quotes') before having it overridden by specific |
| 113 | * config (`text-quotes'), irrespective of the order in which |
| 114 | * they occur. |
| 115 | */ |
| 116 | for (p = source; p; p = p->next) { |
| 117 | if (p->type == para_Config) { |
| 118 | if (!ustricmp(p->keyword, L"quotes")) { |
| 119 | if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) { |
| 120 | ret.lquote = uadv(p->keyword); |
| 121 | ret.rquote = uadv(ret.lquote); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | for (p = source; p; p = p->next) { |
| 128 | if (p->type == para_Config) { |
| 129 | if (!ustricmp(p->keyword, L"text-indent")) { |
| 130 | ret.indent = utoi(uadv(p->keyword)); |
| 131 | } else if (!ustricmp(p->keyword, L"text-charset")) { |
| 132 | ret.charset = charset_from_ustr(&p->fpos, uadv(p->keyword)); |
| 133 | } else if (!ustricmp(p->keyword, L"text-filename")) { |
| 134 | sfree(ret.filename); |
| 135 | ret.filename = dupstr(adv(p->origkeyword)); |
| 136 | } else if (!ustricmp(p->keyword, L"text-indent-code")) { |
| 137 | ret.indent_code = utoi(uadv(p->keyword)); |
| 138 | } else if (!ustricmp(p->keyword, L"text-width")) { |
| 139 | ret.width = utoi(uadv(p->keyword)); |
| 140 | } else if (!ustricmp(p->keyword, L"text-list-indent")) { |
| 141 | ret.listindentbefore = utoi(uadv(p->keyword)); |
| 142 | } else if (!ustricmp(p->keyword, L"text-listitem-indent")) { |
| 143 | ret.listindentafter = utoi(uadv(p->keyword)); |
| 144 | } else if (!ustricmp(p->keyword, L"text-chapter-align")) { |
| 145 | ret.achapter.align = utoalign(uadv(p->keyword)); |
| 146 | } else if (!ustricmp(p->keyword, L"text-chapter-underline")) { |
| 147 | ret.achapter.underline = uadv(p->keyword); |
| 148 | } else if (!ustricmp(p->keyword, L"text-chapter-numeric")) { |
| 149 | ret.achapter.just_numbers = utob(uadv(p->keyword)); |
| 150 | } else if (!ustricmp(p->keyword, L"text-chapter-shownumber")) { |
| 151 | ret.achapter.number_at_all = utob(uadv(p->keyword)); |
| 152 | } else if (!ustricmp(p->keyword, L"text-chapter-suffix")) { |
| 153 | ret.achapter.number_suffix = uadv(p->keyword); |
| 154 | } else if (!ustricmp(p->keyword, L"text-section-align")) { |
| 155 | wchar_t *q = uadv(p->keyword); |
| 156 | int n = 0; |
| 157 | if (uisdigit(*q)) { |
| 158 | n = utoi(q); |
| 159 | q = uadv(q); |
| 160 | } |
| 161 | if (n >= ret.nasect) { |
| 162 | int i; |
| 163 | ret.asect = sresize(ret.asect, n+1, alignstruct); |
| 164 | for (i = ret.nasect; i <= n; i++) |
| 165 | ret.asect[i] = ret.asect[ret.nasect-1]; |
| 166 | ret.nasect = n+1; |
| 167 | } |
| 168 | ret.asect[n].align = utoalign(q); |
| 169 | } else if (!ustricmp(p->keyword, L"text-section-underline")) { |
| 170 | wchar_t *q = uadv(p->keyword); |
| 171 | int n = 0; |
| 172 | if (uisdigit(*q)) { |
| 173 | n = utoi(q); |
| 174 | q = uadv(q); |
| 175 | } |
| 176 | if (n >= ret.nasect) { |
| 177 | int i; |
| 178 | ret.asect = sresize(ret.asect, n+1, alignstruct); |
| 179 | for (i = ret.nasect; i <= n; i++) |
| 180 | ret.asect[i] = ret.asect[ret.nasect-1]; |
| 181 | ret.nasect = n+1; |
| 182 | } |
| 183 | ret.asect[n].underline = q; |
| 184 | } else if (!ustricmp(p->keyword, L"text-section-numeric")) { |
| 185 | wchar_t *q = uadv(p->keyword); |
| 186 | int n = 0; |
| 187 | if (uisdigit(*q)) { |
| 188 | n = utoi(q); |
| 189 | q = uadv(q); |
| 190 | } |
| 191 | if (n >= ret.nasect) { |
| 192 | int i; |
| 193 | ret.asect = sresize(ret.asect, n+1, alignstruct); |
| 194 | for (i = ret.nasect; i <= n; i++) |
| 195 | ret.asect[i] = ret.asect[ret.nasect-1]; |
| 196 | ret.nasect = n+1; |
| 197 | } |
| 198 | ret.asect[n].just_numbers = utob(q); |
| 199 | } else if (!ustricmp(p->keyword, L"text-section-shownumber")) { |
| 200 | wchar_t *q = uadv(p->keyword); |
| 201 | int n = 0; |
| 202 | if (uisdigit(*q)) { |
| 203 | n = utoi(q); |
| 204 | q = uadv(q); |
| 205 | } |
| 206 | if (n >= ret.nasect) { |
| 207 | int i; |
| 208 | ret.asect = sresize(ret.asect, n+1, alignstruct); |
| 209 | for (i = ret.nasect; i <= n; i++) |
| 210 | ret.asect[i] = ret.asect[ret.nasect-1]; |
| 211 | ret.nasect = n+1; |
| 212 | } |
| 213 | ret.asect[n].number_at_all = utob(q); |
| 214 | } else if (!ustricmp(p->keyword, L"text-section-suffix")) { |
| 215 | wchar_t *q = uadv(p->keyword); |
| 216 | int n = 0; |
| 217 | if (uisdigit(*q)) { |
| 218 | n = utoi(q); |
| 219 | q = uadv(q); |
| 220 | } |
| 221 | if (n >= ret.nasect) { |
| 222 | int i; |
| 223 | ret.asect = sresize(ret.asect, n+1, alignstruct); |
| 224 | for (i = ret.nasect; i <= n; i++) { |
| 225 | ret.asect[i] = ret.asect[ret.nasect-1]; |
| 226 | } |
| 227 | ret.nasect = n+1; |
| 228 | } |
| 229 | ret.asect[n].number_suffix = q; |
| 230 | } else if (!ustricmp(p->keyword, L"text-title-align")) { |
| 231 | ret.atitle.align = utoalign(uadv(p->keyword)); |
| 232 | } else if (!ustricmp(p->keyword, L"text-title-underline")) { |
| 233 | ret.atitle.underline = uadv(p->keyword); |
| 234 | } else if (!ustricmp(p->keyword, L"text-versionid")) { |
| 235 | ret.include_version_id = utob(uadv(p->keyword)); |
| 236 | } else if (!ustricmp(p->keyword, L"text-indent-preamble")) { |
| 237 | ret.indent_preambles = utob(uadv(p->keyword)); |
| 238 | } else if (!ustricmp(p->keyword, L"text-bullet")) { |
| 239 | ret.bullet.text = uadv(p->keyword); |
| 240 | } else if (!ustricmp(p->keyword, L"text-rule")) { |
| 241 | ret.rule = uadv(p->keyword); |
| 242 | } else if (!ustricmp(p->keyword, L"text-list-suffix")) { |
| 243 | ret.listsuffix = uadv(p->keyword); |
| 244 | } else if (!ustricmp(p->keyword, L"text-emphasis")) { |
| 245 | if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) { |
| 246 | ret.startemph = uadv(p->keyword); |
| 247 | ret.endemph = uadv(ret.startemph); |
| 248 | } |
| 249 | } else if (!ustricmp(p->keyword, L"text-quotes")) { |
| 250 | if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) { |
| 251 | ret.lquote = uadv(p->keyword); |
| 252 | ret.rquote = uadv(ret.lquote); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Now process fallbacks on quote characters, underlines, the |
| 260 | * rule character, the emphasis characters, and bullets. |
| 261 | */ |
| 262 | while (*uadv(ret.rquote) && *uadv(uadv(ret.rquote)) && |
| 263 | (!cvt_ok(ret.charset, ret.lquote) || |
| 264 | !cvt_ok(ret.charset, ret.rquote))) { |
| 265 | ret.lquote = uadv(ret.rquote); |
| 266 | ret.rquote = uadv(ret.lquote); |
| 267 | } |
| 268 | |
| 269 | while (*uadv(ret.endemph) && *uadv(uadv(ret.endemph)) && |
| 270 | (!cvt_ok(ret.charset, ret.startemph) || |
| 271 | !cvt_ok(ret.charset, ret.endemph))) { |
| 272 | ret.startemph = uadv(ret.endemph); |
| 273 | ret.endemph = uadv(ret.startemph); |
| 274 | } |
| 275 | |
| 276 | while (*ret.atitle.underline && *uadv(ret.atitle.underline) && |
| 277 | !cvt_ok(ret.charset, ret.atitle.underline)) |
| 278 | ret.atitle.underline = uadv(ret.atitle.underline); |
| 279 | |
| 280 | while (*ret.achapter.underline && *uadv(ret.achapter.underline) && |
| 281 | !cvt_ok(ret.charset, ret.achapter.underline)) |
| 282 | ret.achapter.underline = uadv(ret.achapter.underline); |
| 283 | |
| 284 | for (n = 0; n < ret.nasect; n++) { |
| 285 | while (*ret.asect[n].underline && *uadv(ret.asect[n].underline) && |
| 286 | !cvt_ok(ret.charset, ret.asect[n].underline)) |
| 287 | ret.asect[n].underline = uadv(ret.asect[n].underline); |
| 288 | } |
| 289 | |
| 290 | while (*ret.bullet.text && *uadv(ret.bullet.text) && |
| 291 | !cvt_ok(ret.charset, ret.bullet.text)) |
| 292 | ret.bullet.text = uadv(ret.bullet.text); |
| 293 | |
| 294 | while (*ret.rule && *uadv(ret.rule) && |
| 295 | !cvt_ok(ret.charset, ret.rule)) |
| 296 | ret.rule = uadv(ret.rule); |
| 297 | |
| 298 | return ret; |
| 299 | } |
| 300 | |
| 301 | paragraph *text_config_filename(char *filename) |
| 302 | { |
| 303 | return cmdline_cfg_simple("text-filename", filename, NULL); |
| 304 | } |
| 305 | |
| 306 | void text_backend(paragraph *sourceform, keywordlist *keywords, |
| 307 | indexdata *idx, void *unused) { |
| 308 | paragraph *p; |
| 309 | textconfig conf; |
| 310 | word *prefix, *body, *wp; |
| 311 | word spaceword; |
| 312 | textfile tf; |
| 313 | wchar_t *prefixextra; |
| 314 | int nesting, nestindent; |
| 315 | int indentb, indenta; |
| 316 | |
| 317 | IGNORE(unused); |
| 318 | IGNORE(keywords); /* we don't happen to need this */ |
| 319 | IGNORE(idx); /* or this */ |
| 320 | |
| 321 | conf = text_configure(sourceform); |
| 322 | |
| 323 | /* |
| 324 | * Open the output file. |
| 325 | */ |
| 326 | tf.fp = fopen(conf.filename, "w"); |
| 327 | if (!tf.fp) { |
| 328 | error(err_cantopenw, conf.filename); |
| 329 | return; |
| 330 | } |
| 331 | tf.charset = conf.charset; |
| 332 | tf.state = charset_init_state; |
| 333 | |
| 334 | /* Do the title */ |
| 335 | for (p = sourceform; p; p = p->next) |
| 336 | if (p->type == para_Title) |
| 337 | text_heading(&tf, NULL, NULL, p->words, |
| 338 | conf.atitle, conf.indent, conf.width, &conf); |
| 339 | |
| 340 | nestindent = conf.listindentbefore + conf.listindentafter; |
| 341 | nesting = (conf.indent_preambles ? 0 : -conf.indent); |
| 342 | |
| 343 | /* Do the main document */ |
| 344 | for (p = sourceform; p; p = p->next) switch (p->type) { |
| 345 | |
| 346 | case para_QuotePush: |
| 347 | nesting += 2; |
| 348 | break; |
| 349 | case para_QuotePop: |
| 350 | nesting -= 2; |
| 351 | assert(nesting >= 0); |
| 352 | break; |
| 353 | |
| 354 | case para_LcontPush: |
| 355 | nesting += nestindent; |
| 356 | break; |
| 357 | case para_LcontPop: |
| 358 | nesting -= nestindent; |
| 359 | assert(nesting >= 0); |
| 360 | break; |
| 361 | |
| 362 | /* |
| 363 | * Things we ignore because we've already processed them or |
| 364 | * aren't going to touch them in this pass. |
| 365 | */ |
| 366 | case para_IM: |
| 367 | case para_BR: |
| 368 | case para_Biblio: /* only touch BiblioCited */ |
| 369 | case para_VersionID: |
| 370 | case para_NoCite: |
| 371 | case para_Title: |
| 372 | break; |
| 373 | |
| 374 | /* |
| 375 | * Chapter titles. |
| 376 | */ |
| 377 | case para_Chapter: |
| 378 | case para_Appendix: |
| 379 | case para_UnnumberedChapter: |
| 380 | text_heading(&tf, p->kwtext, p->kwtext2, p->words, |
| 381 | conf.achapter, conf.indent, conf.width, &conf); |
| 382 | nesting = 0; |
| 383 | break; |
| 384 | |
| 385 | case para_Heading: |
| 386 | case para_Subsect: |
| 387 | text_heading(&tf, p->kwtext, p->kwtext2, p->words, |
| 388 | conf.asect[p->aux>=conf.nasect ? conf.nasect-1 : p->aux], |
| 389 | conf.indent, conf.width, &conf); |
| 390 | break; |
| 391 | |
| 392 | case para_Rule: |
| 393 | text_rule(&tf, conf.indent + nesting, conf.width - nesting, &conf); |
| 394 | break; |
| 395 | |
| 396 | case para_Normal: |
| 397 | case para_Copyright: |
| 398 | case para_DescribedThing: |
| 399 | case para_Description: |
| 400 | case para_BiblioCited: |
| 401 | case para_Bullet: |
| 402 | case para_NumberedList: |
| 403 | if (p->type == para_Bullet) { |
| 404 | prefix = &conf.bullet; |
| 405 | prefixextra = NULL; |
| 406 | indentb = conf.listindentbefore; |
| 407 | indenta = conf.listindentafter; |
| 408 | } else if (p->type == para_NumberedList) { |
| 409 | prefix = p->kwtext; |
| 410 | prefixextra = conf.listsuffix; |
| 411 | indentb = conf.listindentbefore; |
| 412 | indenta = conf.listindentafter; |
| 413 | } else if (p->type == para_Description) { |
| 414 | prefix = NULL; |
| 415 | prefixextra = NULL; |
| 416 | indentb = conf.listindentbefore; |
| 417 | indenta = conf.listindentafter; |
| 418 | } else { |
| 419 | prefix = NULL; |
| 420 | prefixextra = NULL; |
| 421 | indentb = indenta = 0; |
| 422 | } |
| 423 | if (p->type == para_BiblioCited) { |
| 424 | body = dup_word_list(p->kwtext); |
| 425 | for (wp = body; wp->next; wp = wp->next); |
| 426 | wp->next = &spaceword; |
| 427 | spaceword.next = p->words; |
| 428 | spaceword.alt = NULL; |
| 429 | spaceword.type = word_WhiteSpace; |
| 430 | spaceword.text = NULL; |
| 431 | } else { |
| 432 | wp = NULL; |
| 433 | body = p->words; |
| 434 | } |
| 435 | text_para(&tf, prefix, prefixextra, body, |
| 436 | conf.indent + nesting + indentb, indenta, |
| 437 | conf.width - nesting - indentb - indenta, &conf); |
| 438 | if (wp) { |
| 439 | wp->next = NULL; |
| 440 | free_word_list(body); |
| 441 | } |
| 442 | break; |
| 443 | |
| 444 | case para_Code: |
| 445 | text_codepara(&tf, p->words, |
| 446 | conf.indent + nesting + conf.indent_code, |
| 447 | conf.width - nesting - 2 * conf.indent_code); |
| 448 | break; |
| 449 | } |
| 450 | |
| 451 | /* Do the version ID */ |
| 452 | if (conf.include_version_id) { |
| 453 | for (p = sourceform; p; p = p->next) |
| 454 | if (p->type == para_VersionID) |
| 455 | text_versionid(&tf, p->words, &conf); |
| 456 | } |
| 457 | |
| 458 | /* |
| 459 | * Tidy up |
| 460 | */ |
| 461 | text_output(&tf, NULL); /* end charset conversion */ |
| 462 | fclose(tf.fp); |
| 463 | sfree(conf.asect); |
| 464 | sfree(conf.filename); |
| 465 | } |
| 466 | |
| 467 | static void text_output(textfile *tf, const wchar_t *s) |
| 468 | { |
| 469 | char buf[256]; |
| 470 | int ret, len; |
| 471 | const wchar_t **sp; |
| 472 | |
| 473 | if (!s) { |
| 474 | sp = NULL; |
| 475 | len = 1; |
| 476 | } else { |
| 477 | sp = &s; |
| 478 | len = ustrlen(s); |
| 479 | } |
| 480 | |
| 481 | while (len > 0) { |
| 482 | ret = charset_from_unicode(sp, &len, buf, lenof(buf), |
| 483 | tf->charset, &tf->state, NULL); |
| 484 | if (!sp) |
| 485 | len = 0; |
| 486 | fwrite(buf, 1, ret, tf->fp); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | static void text_output_many(textfile *tf, int n, wchar_t c) |
| 491 | { |
| 492 | wchar_t s[2]; |
| 493 | s[0] = c; |
| 494 | s[1] = L'\0'; |
| 495 | while (n--) |
| 496 | text_output(tf, s); |
| 497 | } |
| 498 | |
| 499 | static void text_rdaddw(rdstring *rs, word *text, word *end, textconfig *cfg) { |
| 500 | for (; text && text != end; text = text->next) switch (text->type) { |
| 501 | case word_HyperLink: |
| 502 | case word_HyperEnd: |
| 503 | case word_UpperXref: |
| 504 | case word_LowerXref: |
| 505 | case word_XrefEnd: |
| 506 | case word_IndexRef: |
| 507 | break; |
| 508 | |
| 509 | case word_Normal: |
| 510 | case word_Emph: |
| 511 | case word_Code: |
| 512 | case word_WeakCode: |
| 513 | case word_WhiteSpace: |
| 514 | case word_EmphSpace: |
| 515 | case word_CodeSpace: |
| 516 | case word_WkCodeSpace: |
| 517 | case word_Quote: |
| 518 | case word_EmphQuote: |
| 519 | case word_CodeQuote: |
| 520 | case word_WkCodeQuote: |
| 521 | assert(text->type != word_CodeQuote && |
| 522 | text->type != word_WkCodeQuote); |
| 523 | if (towordstyle(text->type) == word_Emph && |
| 524 | (attraux(text->aux) == attr_First || |
| 525 | attraux(text->aux) == attr_Only)) |
| 526 | rdadds(rs, cfg->startemph); |
| 527 | else if (towordstyle(text->type) == word_Code && |
| 528 | (attraux(text->aux) == attr_First || |
| 529 | attraux(text->aux) == attr_Only)) |
| 530 | rdadds(rs, cfg->lquote); |
| 531 | if (removeattr(text->type) == word_Normal) { |
| 532 | if (cvt_ok(cfg->charset, text->text) || !text->alt) |
| 533 | rdadds(rs, text->text); |
| 534 | else |
| 535 | text_rdaddw(rs, text->alt, NULL, cfg); |
| 536 | } else if (removeattr(text->type) == word_WhiteSpace) { |
| 537 | rdadd(rs, L' '); |
| 538 | } else if (removeattr(text->type) == word_Quote) { |
| 539 | rdadds(rs, quoteaux(text->aux) == quote_Open ? |
| 540 | cfg->lquote : cfg->rquote); |
| 541 | } |
| 542 | if (towordstyle(text->type) == word_Emph && |
| 543 | (attraux(text->aux) == attr_Last || |
| 544 | attraux(text->aux) == attr_Only)) |
| 545 | rdadds(rs, cfg->endemph); |
| 546 | else if (towordstyle(text->type) == word_Code && |
| 547 | (attraux(text->aux) == attr_Last || |
| 548 | attraux(text->aux) == attr_Only)) |
| 549 | rdadds(rs, cfg->rquote); |
| 550 | break; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | static int text_width(void *, word *); |
| 555 | |
| 556 | static int text_width_list(void *ctx, word *text) { |
| 557 | int w = 0; |
| 558 | while (text) { |
| 559 | w += text_width(ctx, text); |
| 560 | text = text->next; |
| 561 | } |
| 562 | return w; |
| 563 | } |
| 564 | |
| 565 | static int text_width(void *ctx, word *text) { |
| 566 | textconfig *cfg = (textconfig *)ctx; |
| 567 | int wid; |
| 568 | int attr; |
| 569 | |
| 570 | switch (text->type) { |
| 571 | case word_HyperLink: |
| 572 | case word_HyperEnd: |
| 573 | case word_UpperXref: |
| 574 | case word_LowerXref: |
| 575 | case word_XrefEnd: |
| 576 | case word_IndexRef: |
| 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | assert(text->type < word_internal_endattrs); |
| 581 | |
| 582 | wid = 0; |
| 583 | attr = towordstyle(text->type); |
| 584 | if (attr == word_Emph || attr == word_Code) { |
| 585 | if (attraux(text->aux) == attr_Only || |
| 586 | attraux(text->aux) == attr_First) |
| 587 | wid += ustrwid(attr == word_Emph ? cfg->startemph : cfg->lquote, |
| 588 | cfg->charset); |
| 589 | } |
| 590 | if (attr == word_Emph || attr == word_Code) { |
| 591 | if (attraux(text->aux) == attr_Only || |
| 592 | attraux(text->aux) == attr_Last) |
| 593 | wid += ustrwid(attr == word_Emph ? cfg->startemph : cfg->lquote, |
| 594 | cfg->charset); |
| 595 | } |
| 596 | |
| 597 | switch (text->type) { |
| 598 | case word_Normal: |
| 599 | case word_Emph: |
| 600 | case word_Code: |
| 601 | case word_WeakCode: |
| 602 | if (cvt_ok(cfg->charset, text->text) || !text->alt) |
| 603 | wid += ustrwid(text->text, cfg->charset); |
| 604 | else |
| 605 | wid += text_width_list(ctx, text->alt); |
| 606 | return wid; |
| 607 | |
| 608 | case word_WhiteSpace: |
| 609 | case word_EmphSpace: |
| 610 | case word_CodeSpace: |
| 611 | case word_WkCodeSpace: |
| 612 | case word_Quote: |
| 613 | case word_EmphQuote: |
| 614 | case word_CodeQuote: |
| 615 | case word_WkCodeQuote: |
| 616 | assert(text->type != word_CodeQuote && |
| 617 | text->type != word_WkCodeQuote); |
| 618 | if (removeattr(text->type) == word_Quote) { |
| 619 | if (quoteaux(text->aux) == quote_Open) |
| 620 | wid += ustrwid(cfg->lquote, cfg->charset); |
| 621 | else |
| 622 | wid += ustrwid(cfg->rquote, cfg->charset); |
| 623 | } else |
| 624 | wid++; /* space */ |
| 625 | } |
| 626 | |
| 627 | return wid; |
| 628 | } |
| 629 | |
| 630 | static void text_heading(textfile *tf, word *tprefix, word *nprefix, |
| 631 | word *text, alignstruct align, |
| 632 | int indent, int width, textconfig *cfg) { |
| 633 | rdstring t = { 0, 0, NULL }; |
| 634 | int margin, length; |
| 635 | int firstlinewidth, wrapwidth; |
| 636 | wrappedline *wrapping, *p; |
| 637 | |
| 638 | if (align.number_at_all) { |
| 639 | if (align.just_numbers && nprefix) { |
| 640 | text_rdaddw(&t, nprefix, NULL, cfg); |
| 641 | rdadds(&t, align.number_suffix); |
| 642 | } else if (!align.just_numbers && tprefix) { |
| 643 | text_rdaddw(&t, tprefix, NULL, cfg); |
| 644 | rdadds(&t, align.number_suffix); |
| 645 | } |
| 646 | } |
| 647 | margin = length = ustrwid(t.text ? t.text : L"", cfg->charset); |
| 648 | |
| 649 | if (align.align == LEFTPLUS) { |
| 650 | margin = indent - margin; |
| 651 | if (margin < 0) margin = 0; |
| 652 | firstlinewidth = indent + width - margin - length; |
| 653 | wrapwidth = width; |
| 654 | } else if (align.align == LEFT || align.align == CENTRE) { |
| 655 | margin = 0; |
| 656 | firstlinewidth = indent + width - length; |
| 657 | wrapwidth = indent + width; |
| 658 | } |
| 659 | |
| 660 | wrapping = wrap_para(text, firstlinewidth, wrapwidth, |
| 661 | text_width, cfg, 0); |
| 662 | for (p = wrapping; p; p = p->next) { |
| 663 | text_rdaddw(&t, p->begin, p->end, cfg); |
| 664 | length = ustrwid(t.text ? t.text : L"", cfg->charset); |
| 665 | if (align.align == CENTRE) { |
| 666 | margin = (indent + width - length)/2; |
| 667 | if (margin < 0) margin = 0; |
| 668 | } |
| 669 | text_output_many(tf, margin, L' '); |
| 670 | text_output(tf, t.text); |
| 671 | text_output(tf, L"\n"); |
| 672 | if (*align.underline) { |
| 673 | text_output_many(tf, margin, L' '); |
| 674 | while (length > 0) { |
| 675 | text_output(tf, align.underline); |
| 676 | length -= ustrwid(align.underline, cfg->charset); |
| 677 | } |
| 678 | text_output(tf, L"\n"); |
| 679 | } |
| 680 | if (align.align == LEFTPLUS) |
| 681 | margin = indent; |
| 682 | else |
| 683 | margin = 0; |
| 684 | sfree(t.text); |
| 685 | t = empty_rdstring; |
| 686 | } |
| 687 | wrap_free(wrapping); |
| 688 | text_output(tf, L"\n"); |
| 689 | |
| 690 | sfree(t.text); |
| 691 | } |
| 692 | |
| 693 | static void text_rule(textfile *tf, int indent, int width, textconfig *cfg) { |
| 694 | text_output_many(tf, indent, L' '); |
| 695 | while (width > 0) { |
| 696 | text_output(tf, cfg->rule); |
| 697 | width -= ustrwid(cfg->rule, cfg->charset); |
| 698 | } |
| 699 | text_output_many(tf, 2, L'\n'); |
| 700 | } |
| 701 | |
| 702 | static void text_para(textfile *tf, word *prefix, wchar_t *prefixextra, |
| 703 | word *text, int indent, int extraindent, int width, |
| 704 | textconfig *cfg) { |
| 705 | wrappedline *wrapping, *p; |
| 706 | rdstring pfx = { 0, 0, NULL }; |
| 707 | int e; |
| 708 | int firstlinewidth = width; |
| 709 | |
| 710 | if (prefix) { |
| 711 | text_rdaddw(&pfx, prefix, NULL, cfg); |
| 712 | if (prefixextra) |
| 713 | rdadds(&pfx, prefixextra); |
| 714 | text_output_many(tf, indent, L' '); |
| 715 | text_output(tf, pfx.text); |
| 716 | /* If the prefix is too long, shorten the first line to fit. */ |
| 717 | e = extraindent - ustrwid(pfx.text ? pfx.text : L"", cfg->charset); |
| 718 | if (e < 0) { |
| 719 | firstlinewidth += e; /* this decreases it, since e < 0 */ |
| 720 | if (firstlinewidth < 0) { |
| 721 | e = indent + extraindent; |
| 722 | firstlinewidth = width; |
| 723 | text_output(tf, L"\n"); |
| 724 | } else |
| 725 | e = 0; |
| 726 | } |
| 727 | sfree(pfx.text); |
| 728 | } else |
| 729 | e = indent + extraindent; |
| 730 | |
| 731 | wrapping = wrap_para(text, firstlinewidth, width, |
| 732 | text_width, cfg, 0); |
| 733 | for (p = wrapping; p; p = p->next) { |
| 734 | rdstring t = { 0, 0, NULL }; |
| 735 | text_rdaddw(&t, p->begin, p->end, cfg); |
| 736 | text_output_many(tf, e, L' '); |
| 737 | text_output(tf, t.text); |
| 738 | text_output(tf, L"\n"); |
| 739 | e = indent + extraindent; |
| 740 | sfree(t.text); |
| 741 | } |
| 742 | wrap_free(wrapping); |
| 743 | text_output(tf, L"\n"); |
| 744 | } |
| 745 | |
| 746 | static void text_codepara(textfile *tf, word *text, int indent, int width) { |
| 747 | for (; text; text = text->next) if (text->type == word_WeakCode) { |
| 748 | int wid = ustrwid(text->text, tf->charset); |
| 749 | if (wid > width) |
| 750 | error(err_text_codeline, &text->fpos, wid, width); |
| 751 | text_output_many(tf, indent, L' '); |
| 752 | text_output(tf, text->text); |
| 753 | text_output(tf, L"\n"); |
| 754 | } |
| 755 | |
| 756 | text_output(tf, L"\n"); |
| 757 | } |
| 758 | |
| 759 | static void text_versionid(textfile *tf, word *text, textconfig *cfg) { |
| 760 | rdstring t = { 0, 0, NULL }; |
| 761 | |
| 762 | rdadd(&t, L'['); |
| 763 | text_rdaddw(&t, text, NULL, cfg); |
| 764 | rdadd(&t, L']'); |
| 765 | rdadd(&t, L'\n'); |
| 766 | |
| 767 | text_output(tf, t.text); |
| 768 | sfree(t.text); |
| 769 | } |