1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2 
3 /* AbiWord
4  * Copyright (C) 2001 AbiSource, Inc.
5  * Copyright (C) 2001 Petr Tomasek <tomasek@etf.cuni.cz>
6  * Copyright (C) 2002 Dom Lachowicz <cinamod@hotmail.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  */
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include "ut_types.h"
29 #include "ut_assert.h"
30 #include "ut_debugmsg.h"
31 #include "pd_Document.h"
32 #include "ut_string_class.h"
33 #include "xap_Prefs.h"
34 #include "ie_imp_T602.h"
35 #include "xap_App.h"
36 #include "xap_Module.h"
37 
38 #ifdef ABI_PLUGIN_BUILTIN
39 #define abi_plugin_register abipgn_t602_register
40 #define abi_plugin_unregister abipgn_t602_unregister
41 #define abi_plugin_supports_version abipgn_t602_supports_version
42 // dll exports break static linking
43 #define ABI_BUILTIN_FAR_CALL extern "C"
44 #else
45 #define ABI_BUILTIN_FAR_CALL ABI_FAR_CALL
46 ABI_PLUGIN_DECLARE("T602")
47 #endif
48 
49 #define X_CheckDocError(v) if ((!v)) { UT_DEBUGMSG(("X_CheckDocError: ie_imp_T602.cpp:%d\n", __LINE__)); return UT_IE_IMPORTERROR; }
50 #define X_CheckT602Error(v) if ((v != UT_OK)) { UT_DEBUGMSG(("X_CheckT602Error: ie_imp_T602.cpp:%d\n", __LINE__)); return UT_IE_IMPORTERROR; }
51 
52 /****************************************************************************/
53 
54 // completely generic code to allow this to be a plugin
55 
56 static IE_Imp_T602_Sniffer * m_sniffer = 0;
57 
58 ABI_BUILTIN_FAR_CALL
abi_plugin_register(XAP_ModuleInfo * mi)59 int abi_plugin_register (XAP_ModuleInfo * mi)
60 {
61 
62 	if (!m_sniffer)
63 	{
64 		m_sniffer = new IE_Imp_T602_Sniffer ();
65 	}
66 
67 	UT_ASSERT (m_sniffer);
68 
69 	mi->name    = "T602 Importer";
70 	mi->desc   = "Imports T602 documents into abiword. T602 was "
71 		"popular czech and slovak text editor in early nineties "
72 		"produced by Software602 (http://www.software602.cz/).";
73 	mi->version = ABI_VERSION_STRING;
74 	mi->author  = "Petr Tomasek <tomasek@etf.cuni.cz>";
75 	mi->usage   = "No Usage";
76 
77 	IE_Imp::registerImporter (m_sniffer);
78 	return 1;
79 }
80 
81 ABI_BUILTIN_FAR_CALL
abi_plugin_unregister(XAP_ModuleInfo * mi)82 int abi_plugin_unregister (XAP_ModuleInfo * mi)
83 {
84 	mi->name    = 0;
85 	mi->desc    = 0;
86 	mi->version = 0;
87 	mi->author  = 0;
88 	mi->usage   = 0;
89 
90 	UT_ASSERT (m_sniffer);
91 
92 	IE_Imp::unregisterImporter (m_sniffer);
93 	delete m_sniffer;
94 	m_sniffer = 0;
95 
96 	return 1;
97 }
98 
99 ABI_BUILTIN_FAR_CALL
abi_plugin_supports_version(UT_uint32,UT_uint32,UT_uint32)100 int abi_plugin_supports_version (UT_uint32 /*major*/, UT_uint32 /*minor*/,
101 				 UT_uint32 /*release*/)
102 {
103   return 1;
104 }
105 
106 /****************************************************************************/
107 
IE_Imp_T602_Sniffer()108 IE_Imp_T602_Sniffer::IE_Imp_T602_Sniffer () :
109   IE_ImpSniffer("AbiT602::T602")
110 {
111   //
112 }
113 
114 // supported suffixes
115 static IE_SuffixConfidence IE_Imp_T602_Sniffer__SuffixConfidence[] = {
116 	{ "602", 	UT_CONFIDENCE_PERFECT 	},
117 	{ "t602", 	UT_CONFIDENCE_PERFECT 	},
118 	{ "txt", 	UT_CONFIDENCE_POOR 		},
119 	{ "", 	UT_CONFIDENCE_ZILCH 	}
120 };
121 
getSuffixConfidence()122 const IE_SuffixConfidence * IE_Imp_T602_Sniffer::getSuffixConfidence ()
123 {
124 	return IE_Imp_T602_Sniffer__SuffixConfidence;
125 }
126 
recognizeContents(const char * szBuf,UT_uint32 iNumbytes)127 UT_Confidence_t IE_Imp_T602_Sniffer::recognizeContents (const char * szBuf,
128 						     UT_uint32 iNumbytes)
129 {
130   if ((iNumbytes>3) && (!strncmp (szBuf,"@CT ",4)))
131     return UT_CONFIDENCE_PERFECT;
132   return UT_CONFIDENCE_ZILCH;
133 }
134 
constructImporter(PD_Document * pDocument,IE_Imp ** ppie)135 UT_Error IE_Imp_T602_Sniffer::constructImporter (PD_Document * pDocument,
136 													  IE_Imp ** ppie)
137 {
138 	IE_Imp_T602 * p = new IE_Imp_T602(pDocument);
139 	*ppie = p;
140 	return UT_OK;
141 }
142 
getDlgLabels(const char ** pszDesc,const char ** pszSuffixList,IEFileType * ft)143 bool	IE_Imp_T602_Sniffer::getDlgLabels (const char ** pszDesc,
144 											const char ** pszSuffixList,
145 												IEFileType * ft)
146 {
147 	*pszDesc = "T602 (.602,.txt)";
148 	*pszSuffixList = "*.602; *.txt";
149 	*ft = getFileType();
150 	return true;
151 }
152 
153 /****************************************************************************/
154 
IE_Imp_T602(PD_Document * pDocument)155 IE_Imp_T602::IE_Imp_T602(PD_Document * pDocument)
156   : IE_Imp (pDocument), m_importFile(NULL), m_charset(1), m_family("Courier"),
157     m_basefamily("Courier"), m_softcr(1), m_basesize( 10 ),
158     m_size(10), m_lmargin ( "1.0000in" ), m_rmargin ( "1.0000in" ),
159     m_bold(0), m_italic(0), m_underline(0),
160     m_tpos(0), m_big(0), m_color("000000"), m_sfont(0), m_eol(true),
161     m_lheight(1), m_footer(0), m_header(0), m_fhc(1), m_writeheader(true)
162 {
163 }
164 
~IE_Imp_T602()165 IE_Imp_T602::~IE_Imp_T602()
166 {
167 }
168 
169 /****************************************************************************/
170 
171 /* This is ugly hack. Since iconv doesn't know the
172  * keybcs2 and koi8cs encodings, we have to provide them ourselves :-(
173  * (The tables were done using 'recode' and some scripting)
174  */
175 
176 // charset table keybcs2 -> ucs2
177 static const UT_uint16 keybcs22ucs[]= {
178 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
179 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f,
180 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017,
181 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e, 0x001f,
182 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
183 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f,
184 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
185 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f,
186 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
187 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f,
188 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
189 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f,
190 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
191 0x0068, 0x0069, 0x006a, 0x006b, 0x006c, 0x006d, 0x006e, 0x006f,
192 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
193 0x0078, 0x0079, 0x007a, 0x007b, 0x007c, 0x007d, 0x007e, 0x007f,
194 0x010c, 0x00fc, 0x00e9, 0x010f, 0x00e4, 0x010e, 0x0164, 0x010d,
195 0x011b, 0x011a, 0x0139, 0x00cd, 0x013e, 0x013a, 0x00c4, 0x00c1,
196 0x00c9, 0x017e, 0x017d, 0x00f4, 0x00f6, 0x00d3, 0x016f, 0x00da,
197 0x00fd, 0x00d6, 0x00dc, 0x0160, 0x013d, 0x00dd, 0x0158, 0x0165,
198 0x00e1, 0x00ed, 0x00f3, 0x00fa, 0x0148, 0x0147, 0x016e, 0x00d4,
199 0x0161, 0x0159, 0x0155, 0x0154, 0x00bc, 0x00a7, 0x00ab, 0x00bb,
200 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,
201 0x2555, 0x2563, 0x2551, 0x2557, 0x255d, 0x255c, 0x255b, 0x2510,
202 0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c, 0x255e, 0x255f,
203 0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c, 0x2567,
204 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256b,
205 0x256a, 0x2518, 0x250c, 0x2588, 0x2584, 0x258c, 0x2590, 0x2580,
206 0x03b1, 0x03b2, 0x0393, 0x03c0, 0x03a3, 0x03c3, 0x03bc, 0x03c4,
207 0x03a6, 0x0398, 0x03a9, 0x03b4, 0x221e, 0x2205, 0x03b5, 0x2229,
208 0x2261, 0x00b1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00f7, 0x2248,
209 0x2218, 0x00b7, 0x2219, 0x221a, 0x207f, 0x00b2, 0x25a0, 0x00a0
210 };
211 
212 // charset table cp852 -> ucs2
213 static const UT_uint16 cp8522ucs[]= {
214 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
215 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f,
216 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017,
217 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e, 0x001f,
218 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
219 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f,
220 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
221 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f,
222 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
223 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f,
224 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
225 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f,
226 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
227 0x0068, 0x0069, 0x006a, 0x006b, 0x006c, 0x006d, 0x006e, 0x006f,
228 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
229 0x0078, 0x0079, 0x007a, 0x007b, 0x007c, 0x007d, 0x007e, 0x007f,
230 0x00c7, 0x00fc, 0x00e9, 0x00e2, 0x00e4, 0x016f, 0x0107, 0x00e7,
231 0x0142, 0x00eb, 0x0150, 0x0151, 0x00ee, 0x0179, 0x00c4, 0x0106,
232 0x00c9, 0x0139, 0x013a, 0x00f4, 0x00f6, 0x013d, 0x013e, 0x015a,
233 0x015b, 0x00d6, 0x00dc, 0x0164, 0x0165, 0x0141, 0x00d7, 0x010d,
234 0x00e1, 0x00ed, 0x00f3, 0x00fa, 0x0104, 0x0105, 0x017d, 0x017e,
235 0x0118, 0x0119, 0x00ac, 0x017a, 0x010c, 0x015f, 0x00ab, 0x00bb,
236 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00c1, 0x00c2, 0x011a,
237 0x015e, 0x2563, 0x2551, 0x2557, 0x255d, 0x017b, 0x017c, 0x2510,
238 0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c, 0x0102, 0x0103,
239 0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c, 0x00a4,
240 0x0111, 0x0110, 0x010e, 0x00cb, 0x010f, 0x0147, 0x00cd, 0x00ce,
241 0x011b, 0x2518, 0x250c, 0x2588, 0x2584, 0x0162, 0x016e, 0x2580,
242 0x00d3, 0x00df, 0x00d4, 0x0143, 0x0144, 0x0148, 0x0160, 0x0161,
243 0x0154, 0x00da, 0x0155, 0x0170, 0x00fd, 0x00dd, 0x0163, 0x00b4,
244 0x00ad, 0x02dd, 0x02db, 0x02c7, 0x02d8, 0x00a7, 0x00f7, 0x00b8,
245 0x00b0, 0x00a8, 0x02d9, 0x0171, 0x0158, 0x0159, 0x25a0, 0x00a0
246 };
247 
248 // charset table koi8cs -> ucs2
249 static const UT_uint16 koi8cs2ucs[]= {
250 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
251 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f,
252 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017,
253 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e, 0x001f,
254 0x0020, 0x0021, 0x0022, 0x0023, 0x00a4, 0x0025, 0x0026, 0x0027,
255 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f,
256 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
257 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f,
258 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
259 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f,
260 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
261 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f,
262 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
263 0x0068, 0x0069, 0x006a, 0x006b, 0x006c, 0x006d, 0x006e, 0x006f,
264 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
265 0x0078, 0x0079, 0x007a, 0x007b, 0x007c, 0x007d, 0x007e, 0x007f,
266 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
267 0x0088, 0x0089, 0x008a, 0x008b, 0x008c, 0x008d, 0x008e, 0x008f,
268 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
269 0x0098, 0x0099, 0x009a, 0x009b, 0x009c, 0x009d, 0x009e, 0x009f,
270 0x00a0, 0x0104, 0x02d8, 0x0141, 0x0024, 0x013d, 0x015a, 0x00a7,
271 0x00a8, 0x0160, 0x015e, 0x0164, 0x0179, 0x00ad, 0x017d, 0x017b,
272 0x00b0, 0x0105, 0x02db, 0x0142, 0x00b4, 0x013e, 0x015b, 0x02c7,
273 0x00b8, 0x0161, 0x015f, 0x0165, 0x017a, 0x02dd, 0x017e, 0x017c,
274 0x0154, 0x00c1, 0x00c2, 0x0102, 0x00c4, 0x0139, 0x0106, 0x00c7,
275 0x010c, 0x00c9, 0x0118, 0x00cb, 0x011a, 0x00cd, 0x00ce, 0x010e,
276 0x0110, 0x0143, 0x0147, 0x00d3, 0x00d4, 0x0150, 0x00d6, 0x00d7,
277 0x0158, 0x016e, 0x00da, 0x0170, 0x00dc, 0x00dd, 0x0162, 0x00df,
278 0x0155, 0x00e1, 0x00e2, 0x0103, 0x00e4, 0x013a, 0x0107, 0x00e7,
279 0x010d, 0x00e9, 0x0119, 0x00eb, 0x011b, 0x00ed, 0x00ee, 0x010f,
280 0x0111, 0x0144, 0x0148, 0x00f3, 0x00f4, 0x0151, 0x00f6, 0x00f7,
281 0x0159, 0x016f, 0x00fa, 0x0171, 0x00fc, 0x00fd, 0x0163, 0x02d9
282 };
283 
_conv(unsigned char c)284 UT_uint16 IE_Imp_T602::_conv(unsigned char c)
285 {
286 
287   switch (m_charset)
288     {
289     case 0:
290       return keybcs22ucs[c];
291       break;
292     case 1:
293       return cp8522ucs[c];
294       break;
295     case 2:
296       return koi8cs2ucs[c];
297       break;
298     }
299   return 0;
300 }
301 
_getbyte(unsigned char & c)302 bool IE_Imp_T602::_getbyte(unsigned char &c)
303 {
304 	UT_ASSERT(m_importFile);
305 	return (gsf_input_read (m_importFile, 1, &c) != NULL);
306 }
307 
_writeheader()308 UT_Error IE_Imp_T602::_writeheader()
309 {
310 	X_CheckT602Error(_writeSP())
311 	X_CheckT602Error(_writePP())
312 	X_CheckT602Error(_writeTP())
313 	m_writeheader=false;
314 	return UT_OK;
315 }
316 
_writeTP()317 UT_Error IE_Imp_T602::_writeTP()
318 {
319   UT_DEBUGMSG(("T602: Append text properties\n"));
320   UT_String buff;
321   const gchar* pps[3];
322   UT_String_sprintf(buff,"font-family: %s; font-size: %dpt; color:%s; font-weight: %s; "
323 		    "font-style: %s; text-decoration: %s; text-position: %s",
324 		    m_family.c_str(), m_size, m_color.c_str(),
325 		    m_bold ? "bold" : "normal",
326 		    m_italic ? "italic" : "normal",
327 		    m_underline ? "underline" : "none",
328 		    (m_tpos==1) ? "subscript":
329 		    (m_tpos==2 ? "superscript" : "none"));
330 
331   UT_DEBUGMSG(("T602: text-prop:\"%s\"]\n",buff.c_str()));
332   pps[0]="props";
333   pps[1]=buff.c_str();
334   pps[2]=NULL;
335   X_CheckDocError(appendFmt(pps))
336   return UT_OK;
337 }
338 
_writePP()339 UT_Error IE_Imp_T602::_writePP()
340 {
341   UT_DEBUGMSG(("T602: Append paragraph properties\n"));
342   UT_String buff;
343   const gchar* pps[3];
344 
345   // Don't put %1.1f here!! Locales could fuck it...
346   UT_String_sprintf(buff,"line-height: %d.%d",
347 		    (m_lheight+1)/2,((m_lheight+1)%2)*5);
348 
349 UT_DEBUGMSG(("T602: par-prop:\"%s\"]\n",buff.c_str()));
350   pps[0]="props";
351   pps[1]=buff.c_str();
352   pps[2]=NULL;
353   X_CheckDocError(appendStrux(PTX_Block,pps))
354   return UT_OK;
355 }
356 
_writeSP()357 UT_Error IE_Imp_T602::_writeSP()
358 {
359   UT_DEBUGMSG(("T602: Append section\n"));
360   const gchar* sps[7];
361   UT_String bf1, bf2, buff;
362   int i=2;
363 
364   sps[0]="props";
365   UT_String_sprintf(buff,"page-margin-left: %s; page-margin-right: %s",
366 		  m_lmargin.c_str(),
367 		  m_rmargin.c_str());
368   sps[1]=buff.c_str();
369   sps[i]=NULL;
370 
371   if (!m_footer && !m_header)
372     {
373       X_CheckDocError(appendStrux(PTX_Section,sps))
374     }
375   else
376     {
377   if (m_header)
378   {
379     sps[i]="header";
380    UT_String_sprintf(bf1,"%d",m_header);
381    sps[i+1]=static_cast<const gchar *>(bf1.c_str());
382    i=2;
383   }
384   if (m_footer)
385   {
386    sps[i]="footer";
387    UT_String_sprintf(bf2,"%d",m_footer);
388    sps[i+1]=static_cast<const gchar *>(bf2.c_str());
389    i+=2;
390   }
391   sps[i]=NULL;
392   X_CheckDocError(appendStrux(PTX_Section,sps))
393     }
394   return UT_OK;
395 }
396 
_write_fh(UT_String & fh,UT_uint32 id,bool hea)397 UT_Error IE_Imp_T602::_write_fh(UT_String & fh, UT_uint32 id, bool hea)
398 {
399   UT_DEBUGMSG(("T602: Append footer/header section\n"));
400   const gchar* fhps[5];
401   const gchar* fps[5];
402   UT_String bf1;
403   UT_String buff;
404   int i = 0;
405   bool slash=false;
406 
407   UT_String_sprintf(bf1,"%d",id);
408   fhps[0]="id";
409   fhps[1]=static_cast<const gchar *>(bf1.c_str());
410   fhps[2]="type";
411   fhps[3]=(hea?"header":"footer");
412   fhps[4]=NULL;
413   X_CheckDocError(appendStrux(PTX_Section,fhps))
414   X_CheckT602Error(_writePP())
415   X_CheckT602Error(_writeTP())
416 
417     // Page-numbers: prepare text properties...
418   UT_String_sprintf(buff,"font-family: %s; font-size: %dpt; color:%s; font-weight: %s; "
419 		    "font-style: %s; text-decoration: %s; text-position: %s",
420 		    m_family.c_str(), m_size, m_color.c_str(),
421 		    m_bold ? "bold" : "normal",
422 		    m_italic ? "italic" : "normal",
423 		    m_underline ? "underline" : "none",
424 		    (m_tpos==1) ? "subscript":
425 		    (m_tpos==2 ? "superscript" : "none"));
426 
427   UT_DEBUGMSG(("T602: page-numbers: text-prop:\"%s\"\n",buff.c_str()));
428 
429   fps[0]="type";
430   fps[1]="page_number";
431   fps[2]="props";
432   fps[3]=buff.c_str();
433   fps[4]=NULL;
434 
435   for (i=0; fh[i]!='\0'; i++)
436   {
437      if ((fh[i]=='\\') && (!slash))
438        {
439 	 slash=true;
440        }
441      else if ((fh[i]=='#') && (!slash))
442         {
443 	  X_CheckDocError(appendObject(PTO_Field,fps))
444           slash=false;
445 	}
446      else
447 	{
448         X_CheckT602Error(_inschar(fh[i], false))
449 	slash=false;
450 	}
451   }
452   return UT_OK;
453 }
454 
_ins(UT_uint16 c)455 UT_Error IE_Imp_T602::_ins(UT_uint16 c)
456 {
457   UT_UCSChar ch = static_cast<UT_UCSChar >(c);
458   X_CheckDocError(appendSpan(&ch,1))
459   return UT_OK;
460 }
461 
462 
_dotcom(unsigned char ch)463 UT_Error IE_Imp_T602::_dotcom(unsigned char ch)
464 {
465   unsigned char c;
466   char buff[1024];
467   int i;
468   int x;
469 
470   i=0;
471   while (_getbyte(c) && (c!=0x0d) && (c!=0x8d) && (i<1023))
472   {
473 	 if ((c!=0x0a) && (c!=0x1a)) buff[i]=c;
474 	 i++;
475   }
476 
477 // buffer too small, throw it out !
478   if ((c!=0x0d) && (c!=0x8d) && (c!=0x1a))
479   {
480           if (m_writeheader) X_CheckT602Error(_writeheader())
481 	  if (ch=='.') X_CheckT602Error(_inschar('.', false))
482 	  while (_getbyte(c) && (c!=0x0d) && (c!=0x8d))
483 		  if ((ch=='.') && (c!=0x0a) && (c!=0x1a))
484 			  X_CheckT602Error(_inschar(c, false))
485   }
486   else
487   { // O.K. let's parse the .dot command...
488     buff[i]='\0';
489     UT_DEBUGMSG(("Dot command: \"%s\"\n",static_cast<char *>(buff)));
490     if (!strncmp(buff,"CT ",3))
491     {
492       m_charset=atoi(buff+3);
493       UT_DEBUGMSG(("-->Charset %d\n",m_charset));
494     }
495     else if (!strncmp(buff,"PA",2))
496     {
497       UT_DEBUGMSG(("-->Page break\n"));
498       if (m_writeheader) X_CheckT602Error(_writeheader())
499       X_CheckT602Error(_ins(UCS_FF))
500     }
501     else if (!strncmp(buff,"LH ",3))
502     {
503       x=atoi(buff+3);
504       if (x==6) m_lheight=1;
505       else m_lheight=6-x;
506       UT_DEBUGMSG(("-->Line height %1.1f\n",static_cast<float>(m_lheight+1)/2));
507       m_writeheader=true; // FIXME m_writePP ????
508     }
509     else if (!strncmp(buff,"PI ",3))
510     {
511       UT_DEBUGMSG(("T602: Images not supported!! [%s]\n",buff+3));
512     }
513     else if (!strncmp(buff,"IX ",3))
514     {
515       UT_DEBUGMSG(("T602: Indexes not supported!! [%s]\n",buff+3));
516     }
517     else if (!strncmp(buff,"KA ",3))
518     {
519       UT_DEBUGMSG(("T602: Chapters not supported!!\n"));
520     }
521     else if (!strncmp(buff,"HE ",3))
522     {
523       UT_DEBUGMSG(("-->Header: \"%s\"\n",buff+3));
524       if (buff[3]=='0')
525        {
526         m_header=0;
527 	m_writeheader= true;
528        }
529       else
530        {
531         m_fhc++;
532 	m_header=m_fhc;
533 	m_hbuff = buff+3;
534 	m_writeheader= true;
535        }
536     }
537      else if (!strncmp(buff,"FO ",3))
538     {
539       UT_DEBUGMSG(("-->Footer: \"%s\"\n",buff+3));
540       if (buff[3]=='0')
541        {
542         m_footer=0;
543         m_writeheader= true;
544        }
545       else
546        {
547         m_fhc++;
548 	m_footer=m_fhc;
549 	m_fbuff = buff+3;
550 	m_writeheader= true;
551        }
552     }
553 
554   }
555   m_eol=true;
556   return UT_OK;
557 }
558 
559 
_inschar(unsigned char c,bool eol)560 UT_Error IE_Imp_T602::_inschar(unsigned char c, bool eol)
561 {
562 switch (c)
563     {
564 //- Text Properties:
565 //Bold
566      case 0x02:
567 	m_bold ^=1;
568 	X_CheckT602Error(_writeTP())
569 	break;
570 //Italic
571      case 0x04:
572 	m_italic ^=1;
573 	X_CheckT602Error(_writeTP())
574 	break;
575 //Underline
576      case 0x13:
577 	m_underline ^=1;
578 	X_CheckT602Error(_writeTP())
579 	break;
580 //Subscript
581      case 0x16:
582 	m_tpos ^=1;
583 	X_CheckT602Error(_writeTP())
584 	break;
585 //Superscript
586      case 0x14:
587 	m_tpos ^=2;
588 	X_CheckT602Error(_writeTP())
589 	break;
590 //Wide
591      case 0x0f:
592 	m_big ^=1;
593 	if (m_big & 1)
594 	  m_size=static_cast<int>(1.5*m_basesize);
595 	else
596 	  m_size=m_basesize;
597 	X_CheckT602Error(_writeTP())
598 	break;
599 //Tall
600      case 0x10:
601 	m_big ^=2;
602 	if (m_big & 2)
603 	  m_size=static_cast<int>(1.2*m_basesize);
604 	else
605 	  m_size=m_basesize;
606 	X_CheckT602Error(_writeTP())
607 	break;
608 //Big
609      case 0x1d:
610 	m_big ^=4;
611 	if (m_big & 4)
612 	  { m_size=2*m_basesize; m_bold=1; }
613 	else
614 	  { m_size=m_basesize; m_bold=0; }
615 	X_CheckT602Error(_writeTP())
616 	break;
617 
618 //- Extra text Properties:
619 //Elite
620      case 0x01:
621 	m_sfont ^=1;
622 	if (m_sfont & 1)
623 	  { m_size=static_cast<int>(0.8*m_basesize); m_family="Arial";
624 		  /* FIXME? -> .profile?*/ }
625 	else
626 	  { m_size=m_basesize; m_family=m_basefamily; }
627 	X_CheckT602Error(_writeTP())
628 	break;
629 //Condens
630      case 0x03:
631 	m_sfont ^=2;
632 	if (m_sfont & 2)
633 	  m_size=static_cast<int>(.7*m_basesize);
634 	else
635 	  m_size=m_basesize;
636 	X_CheckT602Error(_writeTP())
637 	break;
638 //User 1
639      case 0x11:
640 	m_sfont ^=4;
641 	if (m_sfont & 4)
642 	  m_color="ff0000";
643 	else
644 	  m_color="000000";
645 	X_CheckT602Error(_writeTP())
646 	break;
647 //User 2
648      case 0x12:
649 	m_sfont ^=8;
650 	if (m_sfont & 8)
651 	  m_color="ee00ff";
652 	else
653 	  m_color="000000";
654 	X_CheckT602Error(_writeTP())
655 	break;
656 //User 3
657      case 0x15:
658 	m_sfont ^=16;
659 	if (m_sfont & 16)
660 	  m_color="ffaa00";
661 	else
662 	  m_color="000000";
663 	X_CheckT602Error(_writeTP())
664 	break;
665 //User 4
666      case 0x17:
667 	m_sfont ^=32;
668 	if (m_sfont & 32)
669 	  m_color="1100ff";
670 	else
671 	  m_color="000000";
672 	X_CheckT602Error(_writeTP())
673 	break;
674 //User 5
675      case 0x18:
676 	m_sfont ^=64;
677 	if (m_sfont & 64)
678 	  m_color="bbbb00";
679 	else
680 	  m_color="000000";
681 	X_CheckT602Error(_writeTP())
682 	break;
683 //User 6
684      case 0x19:
685 	m_sfont ^=128;
686 	if (m_sfont & 128)
687 	  m_color="00bb00";
688 	else
689 	  m_color="000000";
690 	X_CheckT602Error(_writeTP())
691 	break;
692 
693 //-- Hacks
694 //Soft hyphen. FIXME: We're putting there simple '-'...
695      case 0xad:
696 	X_CheckT602Error(_ins(_conv('-')))
697     	m_eol=false;
698 	break;
699 //Non breaking space. FIXME: how to do this in abiword?
700      case 0xfe:
701 	X_CheckT602Error(_ins(_conv(' ')))
702     	m_eol=false;
703 	break;
704 
705 //-- Ignore
706      case 0x0a:
707 //	m_eol=true;
708      case 0x1a:
709 	break;
710 
711 //Line breaks (cr/soft-cr)
712      case 0x0d:
713 	m_eol=true;
714 	X_CheckDocError(appendStrux(PTX_Block,NULL))
715 	break;
716     case 0x8d:  // FIXME dat moznost volby ?
717       if (m_softcr)
718 	{
719 	  m_eol=true;
720 	  X_CheckDocError(appendStrux(PTX_Block,NULL))
721 	    }
722       else
723 	X_CheckT602Error(_ins(_conv(' ')))
724       break;
725 //Line commands
726      case '.':
727      case '@':
728 	if (!eol)
729 	  {
730 	  X_CheckT602Error(_ins(_conv(c)))
731 	  }
732 	else
733 	  {
734 	  X_CheckT602Error(_dotcom(c))
735 	  }
736 	break;
737 //Normal character
738      default:
739     	m_eol=false;
740 	X_CheckT602Error(_ins(_conv(c)))
741 	break;
742     }
743 return UT_OK;
744 }
745 
746 
_loadFile(GsfInput * input)747 UT_Error IE_Imp_T602::_loadFile(GsfInput * input)
748 {
749    UT_Error error;
750    unsigned char c;
751 
752    m_importFile = (GsfInput *)g_object_ref (G_OBJECT (input));
753 
754    error=UT_OK;
755 
756    while (_getbyte(c))
757     {
758     if (m_eol && m_writeheader && (c!='.') && (c!='@') &&
759 	(c!=0x0a) && (c!=0x1a))
760    		X_CheckT602Error(_writeheader())
761     X_CheckT602Error(_inschar(c, m_eol))
762     }
763 
764 // Abiword handles footer/header only once for the whole file(?) :-(
765 // FIXME: can we do something with it?
766 
767    if (m_footer)
768      {
769        X_CheckT602Error(_write_fh(m_fbuff, m_footer, false));
770      }
771    if (m_header)
772      {
773        X_CheckT602Error(_write_fh(m_hbuff, m_header, true));
774      }
775 
776    g_object_unref(G_OBJECT(m_importFile));
777    return error;
778 }
779