30#include <tqtextcodec.h>
31#include <tqtextstream.h>
34#include <kgenericfactory.h>
36#include <KoFilterChain.h>
40#include "libmswrite.h"
42#include "ImportDialog.h"
43#include "mswriteimport.h"
46class MSWriteImportFactory : KGenericFactory <MSWriteImport, KoFilter>
49 MSWriteImportFactory () : KGenericFactory <MSWriteImport, KoFilter> (
"kwordmswriteimport")
54 virtual void setupTranslations (
void)
56 TDEGlobal::locale()->insertCatalogue (
"kofficefilters");
60K_EXPORT_COMPONENT_FACTORY (libmswriteimport, MSWriteImportFactory ())
67class WRIDevice : public MSWrite::Device
73 WRIDevice () : m_infp (NULL)
82 bool openFile (
const char *fileName)
84 m_infp = fopen (fileName,
"rb");
87 error (MSWrite::Error::FileError,
"could not open file for reading\n");
100 error (MSWrite::Error::FileError,
"could not close input file\n");
110 bool read (MSWrite::Byte *buf,
const MSWrite::DWord numBytes)
112 if (fread (buf, 1, (
size_t) numBytes, m_infp) != (
size_t) numBytes)
114 error (MSWrite::Error::FileError,
"could not read from input file\n");
121 bool write (
const MSWrite::Byte *,
const MSWrite::DWord)
123 error (MSWrite::Error::InternalError,
"writing to an input file?\n");
127 bool seek (
const long offset,
const int whence)
129 if (fseek (m_infp, offset, whence))
131 error (MSWrite::Error::InternalError,
"could not seek input file\n");
140 return ftell (m_infp);
143 void debug (
const char *s)
145 kdDebug (30509) << s;
148 void debug (
const int i)
150 kdDebug (30509) << i;
153 void error (
const int errorCode,
const char *message,
154 const char * =
"",
const int = 0,
155 MSWrite::DWord = NoToken)
157 if (errorCode == MSWrite::Error::Warn)
158 kdWarning (30509) << message;
162 kdError (30509) << message;
172class KWordGenerator :
public MSWrite::Generator,
public MSWrite::NeedsDevice
180 WRIObject (
const WRIObject &rhs);
183 MSWrite::Byte *m_data;
184 MSWrite::DWord m_dataLength;
185 MSWrite::DWord m_dataUpto;
186 TQString m_nameInStore;
188 WRIObject () : m_data (NULL), m_dataLength (0), m_dataUpto (0)
197 WRIObject operator= (
const WRIObject &rhs)
201 m_dataLength = rhs.m_dataLength;
202 m_dataUpto = rhs.m_dataUpto;
203 m_nameInStore = rhs.m_nameInStore;
207 m_data =
new MSWrite::Byte [m_dataLength];
209 memcpy (m_data, rhs.m_data, m_dataLength);
220 int m_pageWidth, m_pageHeight;
221 int m_left, m_right, m_top, m_bottom;
222 int m_leftMargin, m_rightMargin, m_topMargin, m_bottomMargin;
223 int m_headerFromTop, m_footerFromTop;
224 bool m_hasHeader, m_isHeaderOnFirstPage;
225 bool m_hasFooter, m_isFooterOnFirstPage;
226 bool m_writeHeaderFirstTime, m_writeFooterFirstTime;
230 enum inWhatPossiblities
238 int m_startingPageNumber;
240 KoFilterChain *m_chain;
241 KoStoreDevice *m_outfile;
244 TQTextCodec *m_codec;
245 TQTextDecoder *m_decoder;
248 bool m_simulateLineSpacing;
249 bool m_simulateImageOffset;
252 TQString m_formatOutput;
253 int m_charInfoCountStart, m_charInfoCountLen;
254 bool m_pageBreak, m_needAnotherParagraph;
255 int m_pageBreakOffset;
256 int m_lineSpacingFromAbove;
262 TQString m_objectFrameset;
264 MSWrite::List <WRIObject> m_objectList;
266 double m_objectHorizOffset;
269 MSWriteImport *m_koLink;
274 TQString m_heldOutput;
276 void delayOutput (
const bool yes)
281 bool delayOutputFlush (
void)
283 TQCString strUtf8 = m_heldOutput.utf8 ();
284 int strLength = strUtf8.length ();
286 if (m_outfile->writeBlock (strUtf8, strLength) != strLength)
287 ErrorAndQuit (MSWrite::Error::FileError,
"could not write delayed output\n");
294 KWordGenerator () : m_hasHeader (false), m_isHeaderOnFirstPage (false),
295 m_hasFooter (false), m_isFooterOnFirstPage (false),
296 m_writeHeaderFirstTime (true), m_writeFooterFirstTime (true),
299 m_simulateLineSpacing (false),
300 m_simulateImageOffset (true),
301 m_pageBreak (false), m_needAnotherParagraph (false),
302 m_lineSpacingFromAbove (0),
308 m_codec = TQTextCodec::codecForName (
"CP 1252");
311 m_decoder = m_codec->makeDecoder();
313 kdWarning (30509) <<
"Cannot convert from Win Charset!" << endl;
316 virtual ~KWordGenerator ()
321 void setFilterChain (KoFilterChain *chain)
326 bool writeDocumentBegin (
const MSWrite::Word,
327 const MSWrite::PageLayout *pageLayout)
329 kdDebug (30509) <<
"writeDocumentBegin()" << endl;
332 m_outfile = m_chain->storageFile (
"root", KoStore::Write);
334 ErrorAndQuit (MSWrite::Error::FileError,
"could not open root in store\n");
342 m_pageWidth = Twip2Point (pageLayout->getPageWidth ());
343 m_pageHeight = Twip2Point (pageLayout->getPageHeight ());
346 m_left = Twip2Point (pageLayout->getLeftMargin ());
347 m_right = m_left + Twip2Point (pageLayout->getTextWidth ()) - 1;
348 m_top = Twip2Point (pageLayout->getTopMargin ());
349 m_bottom = m_top + Twip2Point (pageLayout->getTextHeight ()) - 1;
352 m_leftMargin = m_left;
353 m_rightMargin = Twip2Point (pageLayout->getRightMargin ());
355 m_bottomMargin = Twip2Point (pageLayout->getBottomMargin ());
357 kdDebug (30509) <<
"leftMargin: " << m_leftMargin << endl;
358 kdDebug (30509) <<
"rightMargin: " << m_rightMargin << endl;
359 kdDebug (30509) <<
"topMargin: " << m_topMargin << endl;
360 kdDebug (30509) <<
"bottomMargin: " << m_bottomMargin << endl;
363 m_headerFromTop = Twip2Point (pageLayout->getHeaderFromTop ());
364 m_footerFromTop = Twip2Point (pageLayout->getFooterFromTop ());
366 kdDebug (30509) <<
"headerFromTop: " << m_headerFromTop
367 <<
" footerFromTop: " << m_footerFromTop << endl;
369 m_startingPageNumber = pageLayout->getPageNumberStart ();
374 bool writeDocumentBeginForReal (
void)
376 kdDebug (30509) <<
"writeDocumentBeginForReal()" << endl;
383 if (m_headerFromTop < m_topMargin)
384 m_topMargin = m_headerFromTop;
387 if (m_pageHeight - m_footerFromTop < m_bottomMargin)
388 m_bottomMargin = m_pageHeight - m_footerFromTop;
390 kdDebug (30509) <<
"adjusted::: leftMargin: " << m_leftMargin
391 <<
" rightMargin: " << m_rightMargin
392 <<
" topMargin: " << m_topMargin
393 <<
" bottomMargin: " << m_bottomMargin
398 writeTextInternal (
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
399 writeTextInternal (
"<!DOCTYPE DOC PUBLIC \"-//KDE//DTD kword 1.3//EN\" "
400 "\"http://www.koffice.org/DTD/kword-1.3.dtd\">");
401 writeTextInternal (
"<DOC xmlns=\"http://www.koffice.org/DTD/kword\" "
402 "mime=\"application/x-kword\" "
403 "syntaxVersion=\"3\" editor=\"KWord\">");
405 writeTextInternal (
"<PAPER format=\"1\" "
406 "width=\"%i\" height=\"%i\" "
407 "orientation=\"0\" columns=\"1\" "
408 "hType=\"%i\" fType=\"%i\">",
409 m_pageWidth, m_pageHeight,
410 m_isHeaderOnFirstPage ? 0 : 2,
411 m_isFooterOnFirstPage ? 0 : 2);
413 writeTextInternal (
"<PAPERBORDERS left=\"%i\" right=\"%i\" "
414 "top=\"%i\" bottom=\"%i\"/>",
415 m_leftMargin, m_rightMargin,
416 m_topMargin, m_bottomMargin);
418 writeTextInternal (
"</PAPER>");
420 writeTextInternal (
"<ATTRIBUTES processing=\"0\" "
421 "tabStopValue=\"%lf\" "
422 "hasHeader=\"%i\" hasFooter=\"%i\"/>",
424 m_hasHeader ? 1 : 0, m_hasFooter ? 1 : 0);
427 if (m_startingPageNumber != 1)
428 writeTextInternal (
"<VARIABLESETTINGS startingPageNumber=\"%i\"/>",
429 m_startingPageNumber);
431 writeTextInternal (
"<FRAMESETS>");
436 bool writeDocumentEnd (
const MSWrite::Word,
const MSWrite::PageLayout *)
438 kdDebug (30509) <<
"writeDocumentEnd()" << endl;
441 writeTextInternal (m_objectFrameset);
443 writeTextInternal (
"</FRAMESETS>");
444 writeTextInternal (
"<STYLES>");
445 writeTextInternal (
"<STYLE>");
446 writeTextInternal (
"<NAME value=\"Standard\"/>");
447 writeTextInternal (
"<FLOW align=\"left\"/>");
448 writeTextInternal (
"<INDENTS first=\"0\" left=\"0\" right=\"0\"/>");
449 writeTextInternal (
"<OFFSETS before=\"0\" after=\"0\"/>");
451 writeTextInternal (
"<FORMAT id=\"1\">");
452 writeTextInternal (
"<COLOR blue=\"0\" red=\"0\" green=\"0\"/>");
453 writeTextInternal (
"<FONT name=\"helvetica\"/>");
454 writeTextInternal (
"<SIZE value=\"12\"/>");
455 writeTextInternal (
"<WEIGHT value=\"50\"/>");
456 writeTextInternal (
"<ITALIC value=\"0\"/>");
457 writeTextInternal (
"<UNDERLINE value=\"0\"/>");
458 writeTextInternal (
"<STRIKEOUT value=\"0\"/>");
459 writeTextInternal (
"<VERTALIGN value=\"0\"/>");
460 writeTextInternal (
"</FORMAT>");
462 writeTextInternal (
"<FOLLOWING name=\"Standard\"/>");
463 writeTextInternal (
"</STYLE>");
464 writeTextInternal (
"</STYLES>");
467 writeTextInternal (
"<PICTURES>");
468 writeTextInternal (m_pictures);
469 writeTextInternal (
"</PICTURES>");
472 writeTextInternal (
"</DOC>");
476 m_outfile = (KoStoreDevice *) NULL;
485 MSWrite::List <WRIObject>::Iterator it;
486 MSWrite::List <WRIObject>::Iterator end(m_objectList.end ());
487 for (it = m_objectList.begin (); it != end; ++it)
489 kdDebug (30509) <<
"outputting object \'" << (*it).m_nameInStore
490 <<
"\' (length: " << (*it).m_dataLength <<
")"
494 ErrorAndQuit (MSWrite::Error::InternalError,
"image data not initialised\n");
497 m_outfile = m_chain->storageFile ((*it).m_nameInStore, KoStore::Write);
499 ErrorAndQuit (MSWrite::Error::FileError,
"could not open image in store\n");
501 if (m_outfile->writeBlock ((
const char *) (*it).m_data, (*it).m_dataLength)
502 != (TQ_LONG) (*it).m_dataLength)
503 ErrorAndQuit (MSWrite::Error::FileError,
"could not write image to store\n");
514 bool writeFooterBegin (
void)
516 kdDebug (30509) <<
"writeFooterBegin()" << endl;
529 bool writeFooterEnd (
void)
531 kdDebug (30509) <<
"writeFooterEnd()" << endl;
535 if (!m_writeFooterFirstTime)
536 writeTextInternal (
"</FRAMESET>");
542 bool writeHeaderBegin (
void)
544 kdDebug (30509) <<
"writeHeaderBegin()" << endl;
557 bool writeHeaderEnd (
void)
559 kdDebug (30509) <<
"writeHeaderEnd()" << endl;
563 if (!m_writeHeaderFirstTime)
564 writeTextInternal (
"</FRAMESET>");
570 bool writeBodyBegin (
void)
572 kdDebug (30509) <<
"writeBodyBegin()" << endl;
578 writeDocumentBeginForReal ();
580 writeTextInternal (
"<FRAMESET frameType=\"1\" frameInfo=\"0\" name=\"Text Frameset 1\" visible=\"1\">");
582 writeTextInternal (
"<FRAME runaround=\"1\" autoCreateNewFrame=\"1\" newFrameBehavior=\"0\" copy=\"0\""
583 " top=\"%i\" bottom=\"%i\" left=\"%i\" right=\"%i\"/>",
584 m_top, m_bottom, m_left, m_right);
589 bool writeBodyEnd (
void)
591 kdDebug (30509) <<
"writeBodyEnd()" << endl;
597 if (m_needAnotherParagraph)
599 kdDebug (30509) <<
"needAnotherParagraph in bodyEndWrite()" << endl;
600 writeTextInternal (
"<PARAGRAPH><TEXT></TEXT><LAYOUT></LAYOUT></PARAGRAPH>");
601 m_needAnotherParagraph =
false;
604 writeTextInternal (
"</FRAMESET>");
612 bool writeParaInfoBegin (
const MSWrite::FormatParaProperty *paraProperty,
613 const MSWrite::OLE *ole,
614 const MSWrite::Image *image)
619 m_charInfoCountStart = 0;
620 m_charInfoCountLen = 0;
622 if (inWhat == Header)
624 m_isHeaderOnFirstPage = paraProperty->getIsOnFirstPage ();
626 if (m_writeHeaderFirstTime)
633 writeTextInternal (
"<FRAMESET frameType=\"1\" frameInfo=\"1\" name=\"First Page Header\" visible=\"%i\">",
634 m_isHeaderOnFirstPage ? 1 : 0);
635 writeTextInternal (
"<FRAME runaround=\"1\" copy=\"0\" newFrameBehavior=\"2\" autoCreateNewFrame=\"0\""
636 " top=\"%i\" bottom=\"%i\" left=\"%i\" right=\"%i\"/>",
637 m_headerFromTop, m_headerFromTop, m_left, m_right);
638 writeTextInternal (
"</FRAMESET>");
640 writeTextInternal (
"<FRAMESET frameType=\"1\" frameInfo=\"2\" name=\"Even Pages Header\" visible=\"0\">");
641 writeTextInternal (
"<FRAME runaround=\"1\" copy=\"0\" newFrameBehavior=\"2\" autoCreateNewFrame=\"0\""
642 " top=\"%i\" bottom=\"%i\" left=\"%i\" right=\"%i\"/>",
643 m_headerFromTop, m_headerFromTop, m_left, m_right);
644 writeTextInternal (
"</FRAMESET>");
647 writeTextInternal (
"<FRAMESET frameType=\"1\" frameInfo=\"3\" name=\"Odd Pages Header\" visible=\"1\">");
648 writeTextInternal (
"<FRAME runaround=\"1\" copy=\"1\" newFrameBehavior=\"2\" autoCreateNewFrame=\"0\""
649 " top=\"%i\" bottom=\"%i\" left=\"%i\" right=\"%i\"/>",
650 m_headerFromTop, m_headerFromTop, m_left, m_right);
652 m_writeHeaderFirstTime =
false;
655 else if (inWhat == Footer)
657 m_isFooterOnFirstPage = paraProperty->getIsOnFirstPage ();
659 if (m_writeFooterFirstTime)
666 writeTextInternal (
"<FRAMESET frameType=\"1\" frameInfo=\"4\" name=\"First Page Footer\" visible=\"%i\">",
667 m_isFooterOnFirstPage ? 1 : 0);
668 writeTextInternal (
"<FRAME runaround=\"1\" copy=\"0\" newFrameBehavior=\"2\" autoCreateNewFrame=\"0\""
669 " top=\"%i\" bottom=\"%i\" left=\"%i\" right=\"%i\"/>",
670 m_footerFromTop, m_footerFromTop, m_left, m_right);
671 writeTextInternal (
"</FRAMESET>");
673 writeTextInternal (
"<FRAMESET frameType=\"1\" frameInfo=\"5\" name=\"Even Pages Footer\" visible=\"0\">");
674 writeTextInternal (
"<FRAME runaround=\"1\" copy=\"0\" newFrameBehavior=\"2\" autoCreateNewFrame=\"0\""
675 " top=\"%i\" bottom=\"%i\" left=\"%i\" right=\"%i\"/>",
676 m_footerFromTop, m_footerFromTop, m_left, m_right);
677 writeTextInternal (
"</FRAMESET>");
680 writeTextInternal (
"<FRAMESET frameType=\"1\" frameInfo=\"6\" name=\"Odd Pages Footer\" visible=\"1\">");
681 writeTextInternal (
"<FRAME runaround=\"1\" copy=\"1\" newFrameBehavior=\"2\" autoCreateNewFrame=\"0\""
682 " top=\"%i\" bottom=\"%i\" left=\"%i\" right=\"%i\"/>",
683 m_footerFromTop, m_footerFromTop, m_left, m_right);
685 m_writeFooterFirstTime =
false;
689 if (!writeTextInternal (
"<PARAGRAPH><TEXT>"))
return false;
693 kdDebug (30509) <<
"Paragraph is an image!" << endl;
696 TQString fileInStore;
702 imageName =
"Picture ";
703 imageName += TQString::number (m_numPictures + 1);
709 fileInStore =
"pictures/picture" + TQString::number (m_numPictures + 1);
711 kdDebug (30509) <<
"\tGetting type..." << endl;
714 if (image->getIsBMP ())
715 fileInStore +=
".bmp";
716 else if (image->getIsWMF () )
717 fileInStore +=
".wmf";
719 ErrorAndQuit (MSWrite::Error::InternalError,
"unsupported picture type\n");
724 kdDebug (30509) <<
"\tIndicating anchored image in formatting" << endl;
725 if (!writeTextInternal (
"#"))
return false;
727 m_formatOutput +=
"<FORMAT id=\"6\" pos=\"0\" len=\"1\">";
728 m_formatOutput +=
"<ANCHOR type=\"frameset\" instance=\"";
729 m_formatOutput += imageName;
730 m_formatOutput +=
"\"/>";
731 m_formatOutput +=
"</FORMAT>";
736 kdDebug (30509) <<
"\tWriting framesets!" << endl;
738 m_objectFrameset +=
"<FRAMESET frameType=\"2\" frameInfo=\"0\" name=\"";
739 m_objectFrameset += imageName;
740 m_objectFrameset +=
"\" visible=\"1\">";
742 m_objectFrameset +=
"<FRAME runaround=\"1\" copy=\"0\" newFrameBehavior=\"1\"";
744 const double imageLeft = double (m_left) + Twip2Point (
double (image->getIndent ()));
745 m_objectFrameset +=
" left=\"";
746 m_objectFrameset += TQString::number (imageLeft);
747 m_objectFrameset +=
"\"";
749 const double imageWidth = Twip2Point (
double (image->getDisplayedWidth ()));
750 m_objectFrameset +=
" right=\"";
751 m_objectFrameset += TQString::number (imageLeft + imageWidth - 1);
752 m_objectFrameset +=
"\"";
754 m_objectFrameset +=
" top=\"";
755 m_objectFrameset += TQString::number (m_top);
756 m_objectFrameset +=
"\"";
758 const double imageHeight = Twip2Point (
double (image->getDisplayedHeight ()));
759 m_objectFrameset +=
" bottom=\"";
760 m_objectFrameset += TQString::number (
double (m_top) + imageHeight - 1);
761 m_objectFrameset +=
"\"/>";
763 m_objectFrameset +=
"<PICTURE keepAspectRatio=\"false\">";
764 m_objectFrameset +=
"<KEY msec=\"0\" hour=\"0\" second=\"0\" minute=\"0\" day=\"1\" month=\"1\" year=\"1970\"";
765 m_objectFrameset +=
" filename=\"";
766 m_objectFrameset += fileInStore;
767 m_objectFrameset +=
"\"/>";
768 m_objectFrameset +=
"</PICTURE>";
770 m_objectFrameset +=
"</FRAMESET>";
771 #ifdef DEBUG_XML_OUTPUT
772 m_objectFrameset +=
"\n";
775 m_pictures +=
"<KEY msec=\"0\" hour=\"0\" second=\"0\" minute=\"0\" day=\"1\" month=\"1\" year=\"1970\"";
776 m_pictures +=
" name=\"";
777 m_pictures += fileInStore;
779 m_pictures +=
" filename=\"";
780 m_pictures += fileInStore;
781 m_pictures +=
"\"/>";
788 kdDebug (30509) <<
"\tStoring object" << endl;
790 if (!m_objectList.addToBack ())
791 ErrorAndQuit (MSWrite::Error::OutOfMemory,
"could not allocate memory for object\n");
793 WRIObject &obj = *m_objectList.begin (
false);
794 obj.m_nameInStore = fileInStore;
795 obj.m_dataLength = image->getExternalImageSize ();
796 obj.m_data =
new MSWrite::Byte [obj.m_dataLength];
798 ErrorAndQuit (MSWrite::Error::OutOfMemory,
"could not allocate memory for object data\n");
801 m_objectHorizOffset = double (Twip2Point (image->getIndent ()));
802 m_paraIsImage =
true;
808 if (!writeTextInternal (
"[OLE unsupported]"))
return false;
811 m_paraIsImage =
false;
818 bool writeParaInfoEnd (
const MSWrite::FormatParaProperty *paraProperty,
819 const MSWrite::OLE * ,
820 const MSWrite::Image *image)
827 WRIObject &obj = *m_objectList.begin (
false);
830 if (obj.m_dataUpto != obj.m_dataLength)
831 kdWarning (30509) <<
"obj.dataUpto (" << obj.m_dataUpto
832 <<
") != obj.dataLength (" << obj.m_dataLength
839 output +=
"<LAYOUT>";
840 output +=
"<NAME value=\"Standard\"/>";
842 int align = paraProperty->getAlignment ();
844 if (align != MSWrite::Alignment::Left)
846 output +=
"<FLOW align=\"";
852 case MSWrite::Alignment::Centre:
855 case MSWrite::Alignment::Right:
858 case MSWrite::Alignment::Justify:
865 double indentFirst = Twip2Point (
double (paraProperty->getLeftIndentFirstLine ()));
866 double indentLeft = Twip2Point (
double (paraProperty->getLeftIndent ()));
867 double indentRight = Twip2Point (
double (paraProperty->getRightIndent ()));
870 debug (
"raw indent: first: %i left: %i right: %i\n",
871 indentFirst, indentLeft, indentRight);
874 if (m_paraIsImage && m_objectHorizOffset)
876 if (align == MSWrite::Align::Center)
879 kdDebug (30509) <<
"ignoring image offset with centred image" << endl;
880 m_objectHorizOffset = 0;
887 if (m_simulateImageOffset && m_objectHorizOffset > indentLeft)
889 kdDebug (30509) <<
"image is further away from left margin by itself, rather than using indentLeft ("
890 << m_objectHorizOffset <<
" > " << indentLeft <<
")" << endl;
891 indentLeft = m_objectHorizOffset;
897 if (indentFirst || indentLeft || indentRight)
899 output +=
"<INDENTS";
900 if (indentFirst) output +=
" first=\"" + TQString::number (indentFirst) +
"\"";
901 if (indentLeft) output +=
" left=\"" + TQString::number (indentLeft) +
"\"";
902 if (indentRight) output +=
" right=\"" + TQString::number (indentRight) +
"\"";
906 MSWrite::Word lineSpacing = paraProperty->getLineSpacing ();
908 if (lineSpacing != MSWrite::LineSpacing::Single)
911 output +=
"<LINESPACING type=\"atleast\" spacingvalue=\"" + TQString::number (Twip2Point (lineSpacing)) +
"\"/>";
913 output +=
"<LINESPACING type=\"";
918 case MSWrite::LineSpacing::OneAndAHalf:
919 output +=
"oneandhalf";
921 case MSWrite::LineSpacing::Double:
925 kdWarning (30509) <<
"non-\"standard\" linespacing value: " << lineSpacing << endl;
926 output +=
"atleast\" ";
927 output +=
"spacingvalue=\"";
928 output += TQString::number (Twip2Point (lineSpacing));
937 if (m_simulateLineSpacing)
942 if (lineSpacing != MSWrite::LineSpacing::Single)
944 output +=
"<OFFSETS before=\"";
951 case MSWrite::LineSpacing::OneAndAHalf:
954 case MSWrite::LineSpacing::Double:
958 kdWarning (30509) <<
"unknown linespacing value: " << lineSpacing << endl;
963 amount -= m_lineSpacingFromAbove;
964 if (amount <= 0) amount = 0;
966 output += TQString::number (amount);
973 case MSWrite::LineSpacing::Single:
974 m_lineSpacingFromAbove = 0;
976 case MSWrite::LineSpacing::OneAndAHalf:
977 m_lineSpacingFromAbove = 7;
979 case MSWrite::LineSpacing::Double:
980 m_lineSpacingFromAbove = 14;
983 m_lineSpacingFromAbove = 0;
991 debug (
"\tpagebrk: output: offset: %i chars in paragraph: %i\n",
992 m_pageBreakOffset, m_charInfoCountStart + m_charInfoCountLen);
996 if (m_pageBreakOffset == 0 && m_charInfoCountStart + m_charInfoCountLen > 0)
998 output +=
"<PAGEBREAKING hardFrameBreak=\"true\"/>";
999 m_needAnotherParagraph =
false;
1004 output +=
"<PAGEBREAKING hardFrameBreakAfter=\"true\"/>";
1005 m_needAnotherParagraph =
true;
1008 m_pageBreak =
false;
1011 m_needAnotherParagraph =
false;
1014 for (
int i = 0; i < paraProperty->getNumTabulator (); i++)
1016 const MSWrite::FormatParaPropertyTabulator *tab = paraProperty->getTabulator (i);
1017 if (tab->getIsDummy ())
break;
1019 output +=
"<TABULATOR";
1021 if (tab->getIsDecimal ())
1022 output +=
" type=\"3\" alignchar=\".\"";
1024 output +=
" type=\"0\"";
1026 output +=
" ptpos=\"" + TQString::number (Twip2Point (
double (tab->getIndent ()))) +
"\"/>";
1032 output +=
"</LAYOUT>";
1033 #ifdef DEBUG_XML_OUTPUT
1037 output +=
"<FORMATS>";
1038 #ifdef DEBUG_XML_OUTPUT
1042 output += m_formatOutput; m_formatOutput =
"";
1043 output +=
"</FORMATS>";
1044 #ifdef DEBUG_XML_OUTPUT
1048 output +=
"</PARAGRAPH>";
1049 #ifdef DEBUG_XML_OUTPUT
1053 if (!writeTextInternal (output))
return false;
1058 bool writeCharInfoBegin (
const MSWrite::FormatCharProperty * )
1066 bool writeCharInfoEnd (
const MSWrite::FormatCharProperty *charProperty,
1072 m_formatOutput +=
"<FORMAT id=\"";
1073 if (charProperty->getIsPageNumber ())
1074 m_formatOutput +=
"4";
1076 m_formatOutput +=
"1";
1077 m_formatOutput +=
"\" ";
1079 m_formatOutput +=
"pos=\""; m_formatOutput += TQString::number (m_charInfoCountStart); m_formatOutput +=
"\" ";
1080 m_formatOutput +=
"len=\""; m_formatOutput += TQString::number (m_charInfoCountLen); m_formatOutput +=
"\">";
1082 m_charInfoCountStart += m_charInfoCountLen;
1083 m_charInfoCountLen = 0;
1085 if (charProperty->getIsPageNumber ())
1087 m_formatOutput +=
"<VARIABLE>";
1088 m_formatOutput +=
"<TYPE key=\"NUMBER\" type=\"4\"/>";
1089 m_formatOutput +=
"<PGNUM subtype=\"0\" value=\"1\"/>";
1090 m_formatOutput +=
"</VARIABLE>";
1093 m_formatOutput +=
"<FONT name=\"";
1094 m_formatOutput += (
const char *) (charProperty->getFont ()->getName ());
1095 m_formatOutput +=
"\"/>";
1096 m_formatOutput +=
"<SIZE value=\"";
1097 m_formatOutput += TQString::number (charProperty->getFontSize ());
1098 m_formatOutput +=
"\"/>";
1100 if (charProperty->getIsBold ())
1101 m_formatOutput +=
"<WEIGHT value=\"75\"/>";
1105 if (charProperty->getIsItalic ())
1106 m_formatOutput +=
"<ITALIC value=\"1\"/>";
1110 if (charProperty->getIsUnderlined ())
1111 m_formatOutput +=
"<UNDERLINE value=\"1\"/>";
1117 if (charProperty->getIsSubscript ())
1118 m_formatOutput +=
"<VERTALIGN value=\"1\"/>";
1119 else if (charProperty->getIsSuperscript ())
1120 m_formatOutput +=
"<VERTALIGN value=\"2\"/>";
1124 m_formatOutput +=
"</FORMAT>";
1129 bool writeBinary (
const MSWrite::Byte *buffer,
const MSWrite::DWord length)
1131 kdDebug (30509) <<
"writeBinary()" << endl;
1137 WRIObject &obj = *m_objectList.begin (
false);
1140 ErrorAndQuit (MSWrite::Error::InternalError,
"object data not initialised\n");
1143 if (obj.m_dataUpto + length > obj.m_dataLength)
1145 kdDebug (30509) <<
"object image overrun: "
1146 << obj.m_dataUpto <<
" + " << length
1147 <<
" > " << obj.m_dataLength << endl;
1148 ErrorAndQuit (MSWrite::Error::InternalError,
"object image overrun\n");
1151 memcpy (obj.m_data + obj.m_dataUpto, buffer, length);
1152 obj.m_dataUpto += length;
1163 bool writeText (
const MSWrite::Byte *
string)
1166 TQString strUnicode;
1172 strUnicode = m_decoder->toUnicode ((
const char *)
string, strlen ((
const char *)
string));
1178 strUnicode = (
const char *)
string;
1182 m_charInfoCountLen += strUnicode.length ();
1185 strUnicode.replace (
'&',
"&");
1186 strUnicode.replace (
'<',
"<");
1187 strUnicode.replace (
'>',
">");
1188 strUnicode.replace (
'\"',
""");
1189 strUnicode.replace (
'\'',
"'");
1191 return writeTextInternal (strUnicode);
1194 bool writeTextInternal (
const MSWrite::Byte *str)
1197 return textWrite_lowLevel (TQString (str));
1203 m_heldOutput += (
const char *) str;
1208 int strLength = strlen ((
const char *) str);
1210 if (m_outfile->writeBlock ((
const char *) str, strLength) != strLength)
1212 ErrorAndQuit (MSWrite::Error::FileError,
"could not write to maindoc.xml\n");
1220 bool writeTextInternal (
const TQString &str)
1225 m_heldOutput += str;
1230 TQCString strUtf8 = str.utf8 ();
1231 int strLength = strUtf8.length ();
1233 if (m_outfile->writeBlock (strUtf8, strLength) != strLength)
1235 ErrorAndQuit (MSWrite::Error::FileError,
"could not write to maindoc.xml (2)\n");
1242 bool writeTextInternal (
const int num)
1244 return writeTextInternal (TQString::number (num));
1247 bool writeTextInternal (
const char *format, ...)
1250 va_start (list, format);
1260 vsnprintf (
string,
sizeof (
string) - 1, format, list);
1261 string [
sizeof (string) - 1] =
'\0';
1263 ret = writeTextInternal ((
const MSWrite::Byte *)
string);
1272 bool writePageNew (
const int)
1278 bool writePageBreak (
void)
1282 m_pageBreakOffset = m_charInfoCountStart + m_charInfoCountLen;
1288 bool writePageNumber (
void)
1290 m_charInfoCountLen++;
1291 return writeTextInternal (
"#");
1294 bool writeCarriageReturn (
void)
1301 bool writeNewLine (
const bool endOfParagraph)
1303 if (!endOfParagraph)
1305 m_charInfoCountLen++;
1306 return writeTextInternal (
"\n");
1313 bool writeOptionalHyphen (
void)
1315 m_charInfoCountLen++;
1316 return writeTextInternal (
"\xC2\xAD");
1319 void setKOfficeLink (MSWriteImport *kofficeLink)
1321 m_koLink = kofficeLink;
1324 void sigProgress (
const int value)
1326 m_koLink->sigProgress (value);
1336MSWriteImport::MSWriteImport (KoFilter *,
const char *,
const TQStringList &)
1337 : m_device (NULL), m_parser (NULL), m_generator (NULL)
1341MSWriteImport::~MSWriteImport ()
1348KoFilter::ConversionStatus MSWriteImport::convert (
const TQCString &from,
const TQCString &to)
1350 kdDebug (30509) <<
"MSWriteImport $Date: 2006-02-12 19:28:12 +0100 (Sun, 12 Feb 2006) $ using LibMSWrite "
1351 << MSWrite::Version << endl;
1353 if (to !=
"application/x-kword" || from !=
"application/x-mswrite")
1355 kdError (30509) <<
"Internal error! Filter not implemented?" << endl;
1356 return KoFilter::NotImplemented;
1361 MSWriteImportDialog dialog;
1370 debug (
"DIALOG EXEC!!!\n");
1371 if (!dialog.exec ())
1373 error (
"Dialog was aborted! Aborting filter!\n");
1374 return KoFilter::UserCancelled;
1377 debug (
"DIALOG GET!!!\n");
1380 m_codec = dialog.getCodec ();
1381 m_simulateLinespacing = dialog.getSimulateLinespacing ();
1382 m_simulateImageOffset = dialog.getSimulateImageOffset ();
1383 debug (
"Import options: simulateLinespacing: %i\tsimulateImageOffset: %i\n",
1384 m_simulateLinespacing, m_simulateImageOffset);
1386 debug (
"DIALOG DELETE\n");
1391 m_device =
new WRIDevice;
1394 kdError (30509) <<
"Could not allocate memory for device" << endl;
1395 return KoFilter::OutOfMemory;
1399 if (!m_device->openFile (TQFile::encodeName (m_chain->inputFile ())))
1401 kdError (30509) <<
"Could not open \'" << m_chain->inputFile () <<
"\'" << endl;
1402 return KoFilter::FileNotFound;
1407 m_parser =
new MSWrite::InternalParser;
1410 kdError (30509) <<
"Could not allocate memory for parser" << endl;
1411 return KoFilter::OutOfMemory;
1415 m_parser->setDevice (m_device);
1419 m_generator =
new KWordGenerator;
1422 kdError (30509) <<
"Could not allocate memory for generator" << endl;
1423 return KoFilter::OutOfMemory;
1427 m_generator->setDevice (m_device);
1430 m_generator->setFilterChain (m_chain);
1433 m_generator->setKOfficeLink (
this);
1437 m_parser->setGenerator (m_generator);
1441 if (!m_parser->parse ())
1445 int errorCode = m_device->bad ();
1448 case MSWrite::Error::Ok:
1449 kdDebug (30509) <<
"Error::Ok but aborted???" << endl;
1450 return KoFilter::InternalError;
1452 case MSWrite::Error::Warn:
1453 kdDebug (30509) <<
"Error::Warn" << endl;
1454 return KoFilter::InternalError;
1456 case MSWrite::Error::InvalidFormat:
1457 kdDebug (30509) <<
"Error::InvalidFormat" << endl;
1458 return KoFilter::WrongFormat;
1460 case MSWrite::Error::OutOfMemory:
1461 kdDebug (30509) <<
"Error::OutOfMemory" << endl;
1462 return KoFilter::OutOfMemory;
1464 case MSWrite::Error::InternalError:
1465 kdDebug (30509) <<
"Error::InternalError" << endl;
1466 return KoFilter::InternalError;
1468 case MSWrite::Error::Unsupported:
1469 kdDebug (30509) <<
"Error::Unsupported" << endl;
1470 return KoFilter::InternalError;
1472 case MSWrite::Error::FileError:
1473 kdDebug (30509) <<
"Error::FileError" << endl;
1474 return KoFilter::StupidError;
1477 kdWarning (30509) <<
"Unknown error: " << errorCode << endl;
1478 return KoFilter::StupidError;
1481 return KoFilter::OK;
1484#include <mswriteimport.moc>