Index: abiword-plugins/wp/impexp/OpenWriter/xp/ie_imp_OpenWriter.cpp =================================================================== RCS file: /cvsroot/abiword-plugins/wp/impexp/OpenWriter/xp/ie_imp_OpenWriter.cpp,v retrieving revision 1.16 diff -u -r1.16 ie_imp_OpenWriter.cpp --- abiword-plugins/wp/impexp/OpenWriter/xp/ie_imp_OpenWriter.cpp 24 Aug 2004 22:07:15 -0000 1.16 +++ abiword-plugins/wp/impexp/OpenWriter/xp/ie_imp_OpenWriter.cpp 30 Aug 2004 07:49:41 -0000 @@ -44,6 +44,163 @@ #include "ut_debugmsg.h" +class OpenWriter_StylesStream_Listener; + +/*****************************************************************************/ +/*****************************************************************************/ + +/*! + * Class representing OOo page setup properties. + * Imported values are separated for use within Abi's + * and
tags. + */ +class ABI_EXPORT OO_PageStyle +{ +public: + + OO_PageStyle() : m_name("") + { + } + + ~OO_PageStyle() + { + } + + /*! + * Parse attributes array and map keys and values + * to Abi's. + */ + void appendPageMaster(const XML_Char * name, const XML_Char ** atts) + { + // FIXME ATM only one page setup per document can be imported + if (m_name != "") + { + UT_ASSERT(UT_SHOULD_NOT_HAPPEN); + } + + m_name = name; + parse(atts); + } + + /*! + * Returns the name of the current page setup style. + */ + const XML_Char * getName() const + { + return reinterpret_cast(m_name.c_str()); + } + + /*! + * Returns attribute array for the tag. + */ + const XML_Char ** getAbiPageAtts(const XML_Char * masterName) + { + if (UT_strcmp (m_name.c_str(), masterName)) + // FIXME can there be more than one master-page? + UT_ASSERT(UT_SHOULD_NOT_HAPPEN); + + return (const XML_Char **)m_pageAtts; + } + + /*! + * Returns props string for the
tag. + */ + const XML_Char * getAbiSectionProps() const + { + return reinterpret_cast(m_sectionProps.c_str()); + } + +private: + + void parse (const XML_Char ** props) + { + const XML_Char * val = NULL; + int propCtr = 0; + double width = 0; + double height = 0; + + // will go into the tag + val = UT_getAttribute ("fo:page-width", props); + if (val) + { + width = rint(UT_convertToDimension(val, DIM_MM)); + m_width = UT_String_sprintf("%f", width); + m_pageAtts[propCtr++] = "width"; + m_pageAtts[propCtr++] = m_width.c_str(); + } + + val = UT_getAttribute ("fo:page-height", props); + if (val) + { + height = rint(UT_convertToDimension(val, DIM_MM)); + m_height = UT_String_sprintf("%f", height); + m_pageAtts[propCtr++] = "height"; + m_pageAtts[propCtr++] = m_height.c_str(); + } + + m_pageAtts[propCtr++] = "units"; + m_pageAtts[propCtr++] = "mm"; + + val = UT_getAttribute ("style:print-orientation", props); + if (val) + { + m_pageAtts[propCtr++] = "orientation"; + m_pageAtts[propCtr++] = strdup (val); + } + + m_pageAtts[propCtr++] = "page-scale"; + m_pageAtts[propCtr++] = "1.0"; + + // width and height are rounded to full mm because that's how they are + // predefined in Abi and there seem to be rounding errors in oow's exporter + fp_PageSize ps(width, height, DIM_MM); + m_pageAtts[propCtr++] = "pagetype"; + m_pageAtts[propCtr++] = ps.getPredefinedName(); + + m_pageAtts[propCtr] = 0; + + // will go as props into the
tag + val = UT_getAttribute ("fo:margin-left", props); + if (val) + m_marginLeft = UT_String_sprintf("page-margin-left: %s;", val); + + val = UT_getAttribute ("fo:margin-top", props); + if (val) + m_marginTop = UT_String_sprintf("page-margin-top: %s;", val); + + val = UT_getAttribute ("fo:margin-right", props); + if (val) + m_marginRight = UT_String_sprintf("page-margin-right: %s;", val); + + val = UT_getAttribute ("fo:margin-bottom", props); + if (val) + m_marginBottom = UT_String_sprintf("page-margin-bottom: %s;", val); + +#define APPEND_STYLE(sty) if (sty.size()) m_sectionProps += sty; + APPEND_STYLE(m_marginLeft); + APPEND_STYLE(m_marginTop); + APPEND_STYLE(m_marginRight); + APPEND_STYLE(m_marginBottom); +#undef APPEND_STYLE + if (m_sectionProps.size () > 0) + m_sectionProps [m_sectionProps.size()-1] = 0; + } + + UT_String m_name; + + UT_String m_width; + UT_String m_height; + + UT_String m_marginLeft; + UT_String m_marginTop; + UT_String m_marginRight; + UT_String m_marginBottom; + + static const int MAX_PAGE_ATTS = 13; // 2*(width height orientation pagetype units page-scale) 0 + const XML_Char * m_pageAtts[MAX_PAGE_ATTS]; + UT_String m_sectionProps; +}; // class OO_PageStyle + /*****************************************************************************/ /*****************************************************************************/ @@ -333,6 +490,7 @@ void defineSimpleStyle (const UT_UTF8String & name, const XML_Char **props); const XML_Char* mapStyle (const XML_Char * name) const; const OO_Style * mapStyleObj (const XML_Char * name) const; + OpenWriter_StylesStream_Listener * m_pSSListener; private: @@ -438,7 +596,7 @@ * Create a new OpenWriter importer object */ IE_Imp_OpenWriter::IE_Imp_OpenWriter (PD_Document * pDocument) - : IE_Imp (pDocument), m_oo (0) + : IE_Imp (pDocument), m_oo (0), m_pSSListener(0) { } @@ -716,7 +874,7 @@ { public: OpenWriter_StylesStream_Listener ( IE_Imp_OpenWriter * importer ) - : OpenWriter_Stream_Listener ( importer ), m_ooStyle (0) + : OpenWriter_Stream_Listener ( importer ), m_ooStyle (0), m_pageMaster(0) { } @@ -726,7 +884,15 @@ virtual void startElement (const XML_Char * name, const XML_Char ** atts) { - if (!strcmp (name, "style:style")) { + if (!UT_strcmp (name, "style:page-master")) { + m_pageMaster = UT_getAttribute("style:name", atts); + } + else if (!UT_strcmp (name, "style:master-page")) { + const XML_Char * masterName = UT_getAttribute("style:page-master-name", atts); + const XML_Char ** pageAtts = m_ooPageStyle.getAbiPageAtts(masterName); + getDocument()->setPageSizeFromFile(pageAtts); + } + else if (!strcmp (name, "style:style")) { const XML_Char * attr = NULL; attr = UT_getAttribute("style:name",atts); @@ -756,6 +922,10 @@ } DELETEP(m_ooStyle); } + else if (!strcmp (name, "style:properties") && m_pageMaster) { + // page setup + m_ooPageStyle.appendPageMaster(m_pageMaster, atts); + } else if (!strcmp (name, "style:properties")) { getDocument()->getStyle (m_parent.utf8_str(), &m_pParentStyle); m_ooStyle = new OO_Style (atts, m_pParentStyle); @@ -764,7 +934,10 @@ virtual void endElement (const XML_Char * name) { - if (!strcmp (name, "style:style")) { + if (!UT_strcmp (name, "style:page-master")) { + m_pageMaster = NULL; + } + else if (!strcmp (name, "style:style")) { if (m_name.size ()) { int propCtr = 0; @@ -805,6 +978,16 @@ { } + const XML_Char * getSectionProps() const + { + if (!UT_strcmp (m_ooPageStyle.getName(), "")) + { + UT_ASSERT(UT_SHOULD_NOT_HAPPEN); + return NULL; + } + return m_ooPageStyle.getAbiSectionProps(); + } + private: UT_UTF8String m_name; @@ -813,6 +996,8 @@ enum { CHARACTER, PARAGRAPH } m_type; OO_Style *m_ooStyle; PD_Style *m_pParentStyle; + OO_PageStyle m_ooPageStyle; + const XML_Char * m_pageMaster; }; /*! @@ -820,8 +1005,8 @@ */ UT_Error IE_Imp_OpenWriter::_handleStylesStream () { - OpenWriter_StylesStream_Listener listener ( this ); - return handleStream (m_oo, "styles.xml", listener); + m_pSSListener = new OpenWriter_StylesStream_Listener(this); + return handleStream (m_oo, "styles.xml", *m_pSSListener); } /*****************************************************************************/ @@ -842,6 +1027,7 @@ UT_GenericVector m_vecInlineFmt; UT_Stack m_stackFmtStartIndex; + const OpenWriter_StylesStream_Listener * m_pSSListener; UT_uint32 m_imgCnt; @@ -850,9 +1036,9 @@ int m_cel; public: - OpenWriter_ContentStream_Listener ( IE_Imp_OpenWriter * importer ) + OpenWriter_ContentStream_Listener ( IE_Imp_OpenWriter * importer, OpenWriter_StylesStream_Listener * pSSListener ) : OpenWriter_Stream_Listener ( importer ), m_bAcceptingText(false), m_bInSection(false), m_bSectionHasPara(false), - m_imgCnt(0), m_row(0), m_col(0), m_cel(0) + m_pSSListener(pSSListener), m_imgCnt(0), m_row(0), m_col(0), m_cel(0) { } @@ -870,11 +1056,7 @@ const XML_Char * oo_sty = UT_getAttribute ("text:style-name", atts); const XML_Char * abi_sty = _mapStyle(oo_sty); - const XML_Char * props[3]; - props[0] = "props"; - props[1] = abi_sty; - props[2] = 0; - getDocument()->appendStrux(PTX_Section, (const XML_Char**)props); + _appendSection(abi_sty); m_bInSection = true; m_bSectionHasPara = false; } @@ -882,7 +1064,7 @@ { if (!m_bInSection) { - getDocument()->appendStrux(PTX_Section, NULL); + _appendSection(NULL); m_bInSection = true; m_bSectionHasPara = false; } @@ -970,7 +1152,7 @@ { if (!m_bInSection) { - getDocument()->appendStrux(PTX_Section, NULL); + _appendSection(NULL); m_bInSection = true; m_bSectionHasPara = false; } @@ -1050,7 +1232,11 @@ virtual void endElement (const XML_Char * name) { - if (!UT_strcmp(name, "text:p" ) || !UT_strcmp(name, "text:h" )) + if (!UT_strcmp(name, "text:section" )) + { + m_bInSection = false; + } + else if (!UT_strcmp(name, "text:p" ) || !UT_strcmp(name, "text:h" )) { _flush (); m_bAcceptingText= false; @@ -1194,6 +1380,18 @@ DELETEP(importer); } + void _appendSection(const XML_Char * props) + { + UT_String allProps(props); + allProps += m_pSSListener->getSectionProps(); + + const XML_Char * atts[3]; + atts[0] = "props"; + atts[1] = allProps.c_str(); + atts[2] = 0; + getDocument()->appendStrux(PTX_Section, (const XML_Char**)atts); + } + void _insertBookmark (const XML_Char * name, const XML_Char * type) { const XML_Char* propsArray[5]; @@ -1321,6 +1519,6 @@ */ UT_Error IE_Imp_OpenWriter::_handleContentStream () { - OpenWriter_ContentStream_Listener listener ( this ); + OpenWriter_ContentStream_Listener listener (this, m_pSSListener); return handleStream (m_oo, "content.xml", listener); }