summaryrefslogtreecommitdiff
path: root/support/word2x/text-fmt.cc
blob: 3d032462b99e426b57e1a3e107ea91ef984d8a9f (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* $Id: text-fmt.cc,v 1.13 1997/04/13 04:15:33 dps Exp $ */

/* Everything is declared as static to avoid namespace polution but */
/* is reachable externally via the exported table (and this it the  */
/* only way they get called).					    */
#ifdef __GNUC__
#define alloca __builtin_alloca
#else
#if HAVE_ALLOCA_H
#include <alloca.h>
#else /* Do not have alloca.h */
 #ifdef _AIX
#pragma alloca
#else /* not _AIX */
extern "C" char *alloca(int);
#endif /* _AIX */
#endif /* HAVE_ALLOCA_H */
#endif /* __GNUC__ */

#include <stdio.h>
#include <stdlib.h>
#include "interface.h"
#include "lib.h"
#include "text-table.h"

/* Local data */
struct text_data
{
    text_table *tabl;
    int col;
    int row;
    int margin;
    int l_indent;
};

/* Most of these values are thanks to catdoc by
 * Victor B. Wagner <vitus@agropc.msk.su> et al */
static const cmap char_map[]=
{
    { 0x1E, "-" },      // Unbreakable join
    { 0x1F, "" },	// Soft hypen
    { 0x7f, "" },	// Delete 127s
    { 0x85, "..." },    // Dots
    { 0x91, "`" },      // 91 = left quote
    { 0x92, "'" },      // 92 = right quote
    { 0x93, "\"" },     // 93 = opening double quote
    { 0x94, "\"" },     // 94 = closing double quote
    { 0x95, "o" },	// 95 = Bullet
    { 0x96, "-" },      // em-dash
    { 0x97, "-" },      // en-dash
    { 0x99, "(tm)" },   // Trademark
    { 0xA0, " " },      // Unbreakable space
    { 0xA3, "<=" },	// Less than or = came out as A3
    { 0xA9, "(c)" },    // Copyright
    { 0xAB, "<<" },     // Openning << quotes
    { 0xAE, "(R)" },    // Reserved sign
    { 0xB3, ">=" },	// Greater than or = came out as B3
    { 0xBB, ">>" },	// Closing >> quotes
};

/* Allocate local data */
static void *allocate_txt(void)
{
    struct text_data *tdata;

    tdata=new(struct text_data);
    tdata->tabl=NULL;
    tdata->margin=0;
    tdata->l_indent=0;
    return tdata;
}

static void txt_code(const tok_seq::tok *t, const docfmt *fmt, FILE *out,
		     void *d)
{
    d=d;
    switch(*(t->data.d))
    {
    case CH_PAGE:
	if (fmt->flags.new_pages)
	    fputc('\014', out);
	break;

    default:
	break;
    }
}

/* Free local data */
static void free_txt(void *d)
{
    struct text_data *tdata;

    tdata=(struct text_data *) d;
    if (tdata->tabl!=NULL)
	delete(tdata->tabl);
}


/* list start increases margin */
static void inc_margin(const tok_seq::tok *t, const docfmt *fmt, FILE *out,
		      void *d)
{
    struct text_data *tdata;
    t=t;
    fmt=fmt;
    out=out;

    tdata=(struct text_data *) d;
    tdata->margin+=4;
}


/* list start decreases margin */
static void dec_margin(const tok_seq::tok *t, const docfmt *fmt, FILE *out,
		      void *d)
{
    struct text_data *tdata;
    t=t;
    fmt=fmt;
    out=out;

    tdata=(struct text_data *) d;
    tdata->margin-=4;
}


/* items are easy */
static void txt_item(const tok_seq::tok *t, const docfmt *fmt, FILE *out,
		      void *d)
{
    tblock *ts;
    const char *s;
    int i, m;
    struct text_data *dp;

    fmt=fmt;			// No need for fmt
    dp=(struct text_data *) d;
    ts=map_string(t->data.d, char_map);
    s=*ts;
    fputc('\n', out);
    m=dp->margin-strlen(s)-1;
    for (i=0; i<m; i++)
	fputc(' ', out);
    fputs(s, out);
    fputc(' ', out);
    dp->l_indent=dp->margin;
    delete(ts);
}


/* Paragraphs are easy */
static void fold_para(const tok_seq::tok *t, const docfmt *fmt, FILE *out,
		      void *d)
{
    tblock *b, *ts;
    const char *s;
    char *nl;
    struct text_data *dp;
    int i;

    dp=(struct text_data *) d;
    if (dp->margin==0 || (nl=(char *) alloca(dp->margin+2))==NULL)
	nl="\n";
    else
    {
	*nl='\n';
	for (i=1; i<=dp->margin; i++)
	    *(nl+i)=' ';
	*(nl+dp->margin+1)='\0';
    }
    ts=map_string(t->data.d, char_map);
    b=word_wrap((*ts), nl, nl, fmt->maxline-(dp->margin), 0);
    delete(ts);
    s=*b;
    for (i=dp->l_indent; i<dp->margin; i++)
	fputc(' ', out);
    fputs(s, out);
    fputs("\n\n", out);
    dp->l_indent=0;
    delete(b);
}

/* Start a table === allocate table and initialise */
static void alloc_tbl(const tok_seq::tok *t, const docfmt *fmt, FILE *out,
		      void *d)
{
    struct text_data *tdata;

    out=out;
    fmt=fmt;
    tdata=(struct text_data *) d;
    tdata->col=0;
    tdata->row=0;
    tdata->tabl=new(text_table)(t->data.table.cols, t->data.table.rows);
}


/* End of a table==print the table */
static void format_tbl(const tok_seq::tok *t, const docfmt *fmt, FILE *out,
		       void *d)
{
    struct text_data *tdata;

    t=t;
    tdata=(struct text_data *) d;
    tdata->tabl->print_table(fmt->maxline, out); // Print table
    delete(tdata->tabl);
    tdata->tabl=NULL;
}

/* start row==set column to 0 */
static void start_row(const tok_seq::tok *t, const docfmt *fmt, FILE *out,
		      void *d)
{ 
    struct text_data *tdata;

    out=out;
    fmt=fmt;
    t=t;
    tdata=(struct text_data *) d;
    tdata->col=0;
}

/* end row==add one to row */
static void inc_row(const tok_seq::tok *t, const docfmt *fmt, FILE *out,
		    void *d)
{ 
    struct text_data *tdata;

    out=out;
    fmt=fmt;
    t=t;
    tdata=(struct text_data *) d;
    tdata->row++;
}


/* Start field === set field */
static void set_field(const tok_seq::tok *t, const docfmt *fmt, FILE *out,
		      void *d)
{
    struct tblock *ts;
    struct text_data *tdata;

    out=out;
    fmt=fmt;
    ts=map_string(t->data.d, char_map);
    tdata=(struct text_data *) d;
    tdata->tabl->set(tdata->col, tdata->row, (*ts));
    delete(ts);
}

/* end field==add one to col */
static void inc_col(const tok_seq::tok *t, const docfmt *fmt, FILE *out,
		    void *d)
{ 
    struct text_data *tdata;

    out=out;
    fmt=fmt;
    t=t;
    tdata=(struct text_data *) d;
    tdata->col++;
}

/* pointers to the functions that do the work */
docfmt txtfmt=
{
    { 0 },			    // No page breaks
    76,				    // Width
    "\n",			    // Use \n as line ends
    allocate_txt,		    // Allocate space
    free_txt,			    // Free text
    {
	{ null_proc, null_proc },   // End and start of document---do nothing
	{ fold_para, null_proc },   // Paragraph
	{ alloc_tbl, format_tbl },  // Start/end table
	{ set_field, inc_col },	    // Start/end field
	{ start_row, inc_row },	    // Start/end row
	{ null_proc, null_proc },   // Throw away embed messages
	{ inc_margin, dec_margin }, // list start and end
	{ txt_item, null_proc},	    // Items
	{ txt_code, null_proc},	    // Code
	{ null_proc, null_proc }    // Do not understanding anything else
    }
};