.gnumeric: if we see a shared array formula, fix it.
[gnumeric.git] / plugins / html / roff.c
blob930cb3d229da830e2ee4cab3b97ae4ee8e76639b
1 /*
2 * roff.c
4 * Copyright (C) 1999, 2000 Rasca, Berlin
5 * EMail: thron@gmx.de
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <https://www.gnu.org/licenses/>.
21 #include <gnumeric-config.h>
22 #include <glib/gi18n-lib.h>
23 #include <gnumeric.h>
24 #include "workbook-view.h"
25 #include "workbook.h"
26 #include "sheet.h"
27 #include "style.h"
28 #include "roff.h"
29 #include "font.h"
30 #include "cell.h"
31 #include "cellspan.h"
32 #include <goffice/goffice.h>
34 #include <string.h>
35 #include <gsf/gsf-output.h>
38 * escape special characters .. needs work
40 static int
41 roff_fprintf (GsfOutput *output, GnmCell *cell)
43 int len, i;
44 char const *p;
45 char * s;
46 GnmStyle const *style;
48 if (gnm_cell_is_empty (cell))
49 return 0;
51 style = gnm_cell_get_style (cell);
52 if (style != NULL && gnm_style_get_contents_hidden (style))
53 return 0;
55 s = gnm_cell_get_rendered_text (cell);
56 len = strlen (s);
57 p = s;
58 for (i = 0; i < len; i++) {
59 switch (*p) {
60 case '.':
61 gsf_output_printf (output, "\\.");
62 break;
63 case '\\':
64 gsf_output_printf (output, "\\\\");
65 break;
66 default:
67 gsf_output_printf (output, "%c", *p);
68 break;
70 p++;
72 g_free (s);
73 return len;
76 /* default point size */
77 #define DEFSIZE 10
80 * write every sheet of the workbook to a roff file
82 * FIXME: Should roff quote sheet name (and everything else)
84 void
85 roff_file_save (GOFileSaver const *fs, GOIOContext *io_context,
86 WorkbookView const *wb_view, GsfOutput *output)
88 GSList *sheets, *ptr;
89 GnmCell *cell;
90 int row, col, fontsize, v_size;
91 Workbook *wb = wb_view_get_workbook (wb_view);
93 g_return_if_fail (wb != NULL);
95 gsf_output_printf (output, ".\\\" TROFF file\n");
96 gsf_output_printf (output, ".fo ''%%''\n");
97 sheets = workbook_sheets (wb);
98 for (ptr = sheets ; ptr != NULL ; ptr = ptr->next) {
99 Sheet *sheet = ptr->data;
100 GnmRange r = sheet_get_extent (sheet, FALSE, TRUE);
102 gsf_output_printf (output, "%s\n\n", sheet->name_unquoted);
103 gsf_output_printf (output, ".TS H\n");
104 gsf_output_printf (output, "allbox;\n");
106 for (row = r.start.row; row <= r.end.row; row++) {
107 ColRowInfo const * ri;
108 ri = sheet_row_get_info (sheet, row);
109 if (ri->needs_respan)
110 row_calc_spans ((ColRowInfo *) ri, row, sheet);
112 if (row > r.start.row)
113 gsf_output_printf (output, ".T&\n");
114 /* define alignments, bold etc. per cell */
115 v_size = DEFSIZE;
116 for (col = r.start.col; col <= r.end.col; col++) {
117 cell = sheet_cell_get (sheet, col, row);
118 if (col > r.start.col)
119 gsf_output_printf (output, " ");
120 if (!cell) {
121 gsf_output_printf (output, "l");
122 } else {
123 GnmStyle const *style = gnm_cell_get_style (cell);
124 if (!style)
125 break;
126 if (gnm_style_get_align_h (style) & GNM_HALIGN_RIGHT)
127 gsf_output_printf (output, "r");
128 else if (gnm_style_get_align_h (style) == GNM_HALIGN_CENTER ||
129 /* FIXME : center across selection is different */
130 gnm_style_get_align_h (style) == GNM_HALIGN_CENTER_ACROSS_SELECTION ||
131 gnm_style_get_align_h (style) == GNM_HALIGN_DISTRIBUTED)
132 gsf_output_printf (output, "c");
133 else
134 gsf_output_printf (output, "l");
135 if (font_is_monospaced (style)) {
136 if (gnm_style_get_font_bold (style) &&
137 gnm_style_get_font_italic (style))
138 gsf_output_printf (output, "fCBI");
139 else if (gnm_style_get_font_bold (style))
140 gsf_output_printf (output, "fCB");
141 else if (gnm_style_get_font_italic (style))
142 gsf_output_printf (output, "fCI");
143 else
144 gsf_output_printf (output, "fCR");
145 } else if (font_is_helvetica (style)) {
146 if (gnm_style_get_font_bold (style) &&
147 gnm_style_get_font_italic (style))
148 gsf_output_printf (output, "fHBI");
149 else if (gnm_style_get_font_bold (style))
150 gsf_output_printf (output, "fHB");
151 else if (gnm_style_get_font_italic (style))
152 gsf_output_printf (output, "fHI");
153 else
154 gsf_output_printf (output, "fHR");
155 } else {
156 /* default is times */
157 if (gnm_style_get_font_bold (style) &&
158 gnm_style_get_font_italic (style))
159 gsf_output_printf (output, "fTBI");
160 else if (gnm_style_get_font_bold (style))
161 gsf_output_printf (output, "fTB");
162 else if (gnm_style_get_font_italic (style))
163 gsf_output_printf (output, "fTI");
165 fontsize = gnm_style_get_font_size (style);
166 if (fontsize) {
167 gsf_output_printf (output, "p%d", fontsize);
168 v_size = v_size > fontsize ? v_size :
169 fontsize;
173 gsf_output_printf (output, ".\n");
174 gsf_output_printf (output, ".vs %.2fp\n", 2.5 + v_size);
175 for (col = r.start.col; col <= r.end.col; col++) {
176 if (col > r.start.col)
177 gsf_output_printf (output, "\t");
178 cell = sheet_cell_get (sheet, col, row);
179 if (!cell) { /* empty cell */
180 gsf_output_printf (output, " ");
181 } else {
182 roff_fprintf (output, cell);
185 gsf_output_printf (output, "\n");
186 if (row == r.start.row)
187 gsf_output_printf (output, ".TH\n");
189 gsf_output_printf (output, ".TE\n\n");
191 g_slist_free (sheets);