RtfFormatReader - fix convert document from consultant+

This commit is contained in:
ElenaSubbotina
2018-03-01 20:01:04 +03:00
parent 16728b5195
commit cfe9398ff0
10 changed files with 233 additions and 44 deletions

View File

@ -71,19 +71,21 @@ HRESULT RtfConvertationManager::ConvertRtfToOOX( std::wstring sSrcFileName, std:
m_poRtfReader = &oReader;
m_poOOXWriter = &oWriter;
m_poRtfReader->m_convertationManager = this;
//m_poRtfReader->m_convertationManager = this;
bool succes = oReader.Load( );
//сохранение будет поэлементое в обработчике OnCompleteItemRtf
//надо только завершить
if( true == m_bParseFirstItem )
{
m_bParseFirstItem = false;
oWriter.SaveByItemStart( );
}
m_poOOXWriter->SaveByItem();
oWriter.SaveByItemEnd( );
//if( true == m_bParseFirstItem )
//{
// m_bParseFirstItem = false;
// oWriter.SaveByItemStart( );
//}
//m_poOOXWriter->SaveByItem();
//oWriter.SaveByItemEnd( );
oWriter.Save();
NSDirectory::DeleteDirectory(oReader.m_sTempFolder);
NSDirectory::DeleteDirectory(oWriter.m_sTempFolder);

View File

@ -462,7 +462,7 @@ bool RtfNormalReader::ExecuteCommand( RtfDocument& oDocument, RtfReader& oReader
{
section.props->m_bFinalize = true;
section.props->m_oProperty = oReader.m_oCurSectionProp;
}
}
_section new_section;
new_section.props = RtfSectionPtr( new RtfSection() );
@ -510,8 +510,8 @@ bool RtfBorderCommand::ExecuteCommand(RtfDocument& oDocument, RtfReader& oReader
else if( "brdrsh" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrsh;
else if( "brdrdb" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrdb;
else if( "brdrdot" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrdot;
else if( "brdrdash" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrdash;
else if( "brdrhair" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrhair;
else if( "brdrdash" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrdash;
else if( "brdrhair" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrhair;
else if( "brdrdashsm" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrdashsm;
else if( "brdrdashd" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrdashd;
else if( "brdrdashdd" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrdashdd;
@ -520,22 +520,24 @@ bool RtfBorderCommand::ExecuteCommand(RtfDocument& oDocument, RtfReader& oReader
else if( "brdrtriple" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrtriple;
else if( "brdrtnthsg" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrtnthsg;
else if( "brdrthtnsg" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrthtnsg;
else if( "brdrtnthtnsg" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrtnthtnsg;
else if( "brdrtnthtnsg" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrtnthtnsg;
else if( "brdrtnthmg" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrtnthmg;
else if( "brdrthtnmg" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrthtnmg;
else if( "brdrtnthtnmg" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrtnthtnmg;
else if( "brdrtnthtnmg" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrtnthtnmg;
else if( "brdrtnthlg" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrtnthlg;
else if( "brdrthtnlg" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrthtnlg;
else if( "brdrtnthtnlg" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrtnthtnlg;
else if( "brdrwavy" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrwavy;
else if( "brdrtnthtnlg" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrtnthtnlg;
else if( "brdrwavy" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrwavy;
else if( "brdrwavydb" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrwavydb;
else if( "brdrdashdotstr" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrdashdotstr;
else if( "brdremboss" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdremboss;
else if( "brdrengrave" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrengrave;
else if( "brdroutset" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdroutset;
else if( "brdrinset" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrinset;
else if( "brdrnone" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrnone;
else if( "brdrw" == sCommand )
else if( "brdrnone" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrnone;
else if( "brdrnil" == sCommand ) oOutput.m_eType = RtfBorder::bt_none;
else if( "brdrn" == sCommand ) oOutput.m_eType = RtfBorder::bt_brdrnone;
else if( "brdrw" == sCommand )
{
if( true == hasParameter )
{
@ -722,6 +724,7 @@ bool RtfSectionCommand::ExecuteCommand(RtfDocument& oDocument, RtfReader& oReade
RtfParagraphReader oParagraphReader(sCommand, oReader);
oAbstrReader.StartSubReader( oParagraphReader, oDocument, oReader );
oParagraphReader.m_oParPropDest.m_bPar = true;
oParagraphReader.m_oParPropDest.Finalize( oReader );
TextItemContainerPtr oNewFooterHeader = oParagraphReader.m_oParPropDest.m_oTextItems;

View File

@ -1579,8 +1579,8 @@ private:
RtfRowProperty oCurRowProperty;
RtfReader* m_oReader;
bool m_bPar; // если последняя команда была par, то не надо добавлять параграф
public:
bool m_bPar; // если последняя команда была par, то не надо добавлять параграф
TextItemContainerPtr m_oTextItems; //для разбивки на TextItem
RtfTab m_oCurTab;
int nTargetItap; //уровень который считается не таблицей ( для того чтобы читать параграфы в таблицах )
@ -2439,6 +2439,7 @@ public:
}
void ExitReader(RtfDocument& oDocument, RtfReader& oReader)
{
oParagraphReaderDestination.m_bPar = true;
oParagraphReaderDestination.Finalize( oReader );
_section section;

View File

@ -1264,8 +1264,18 @@ std::wstring RtfStyle::RenderToOOXBegin(RenderParameter oRenderParameter)
default:
break;
}
//sResult += L"<w:style w:type=\"" + sType + L"\" w:styleId=\"" + XmlUtils::EncodeXmlString( m_sName ) + L"\">";
sResult += L"<w:style w:type=\"" + sType + L"\" w:styleId=\"" + L"Style_" + std::to_wstring(m_nID) + L"\">";
if (m_sID.empty())
{
m_sID = L"Style_" + std::to_wstring(m_nID);
}
sResult += L"<w:style w:type=\"" + sType + L"\" w:styleId=\"" + m_sID + L"\"";
if (m_nID == 0 && PROP_DEF == m_nBasedOn)
{
sResult += L" w:default=\"1\"";
}
sResult += L">";
sResult += L"<w:name w:val=\"" + XmlUtils::EncodeXmlString( m_sName ) + L"\"/>";
if( PROP_DEF != m_nBasedOn )
@ -1277,6 +1287,7 @@ std::wstring RtfStyle::RenderToOOXBegin(RenderParameter oRenderParameter)
//sResult += L"<w:basedOn w:val=\"" + XmlUtils::EncodeXmlString( oBaseStyle->m_sName ) + L"\"/>";
}
}
//if( PROP_DEF != m_nNext )
// sResult += L"<w:next w:val=\"" + XmlUtils::EncodeXmlString( m_sName ) + L"\"/>", );//Todo
//if( PROP_DEF != m_nLink )
@ -2290,11 +2301,11 @@ std::wstring RtfParagraphProperty::RenderToOOX(RenderParameter oRenderParameter)
switch ( m_eTextFollow )
{
case tf_frmtxlrtb: sResult += L"<w:textFlow w:val=\"lrtb\" />"; break;
case tf_frmtxtbrl: sResult += L"<w:textFlow w:val=\"tbrl\" />"; break;
case tf_frmtxbtlr: sResult += L"<w:textFlow w:val=\"btlr\" />"; break;
case tf_frmtxlrtbv: sResult += L"<w:textFlow w:val=\"lrtbV\" />";break;
case tf_frmtxtbrlv: sResult += L"<w:textFlow w:val=\"tbrlV\" />";break;
case tf_frmtxlrtb: sResult += L"<w:textFlow w:val=\"lrtb\"/>"; break;
case tf_frmtxtbrl: sResult += L"<w:textFlow w:val=\"tbrl\"/>"; break;
case tf_frmtxbtlr: sResult += L"<w:textFlow w:val=\"btlr\"/>"; break;
case tf_frmtxlrtbv: sResult += L"<w:textFlow w:val=\"lrtbV\"/>";break;
case tf_frmtxtbrlv: sResult += L"<w:textFlow w:val=\"tbrlV\"/>";break;
default:
break;
}

View File

@ -36,6 +36,49 @@
#include "../../../UnicodeConverter/UnicodeConverter.h"
#include "../../../ASCOfficePPTXFile/Editor/Drawing/Shapes/BaseShape/PPTShape/PptShape.h"
#include "../../../DesktopEditor/raster/BgraFrame.h"
#include "../../../DesktopEditor/raster/Metafile/MetaFile.h"
bool RtfShape::GetPictureResolution(RenderParameter oRenderParameter, int & Width, int &Height)
{
if (!m_oPicture) return false;
//if (!oRenderParameter.appFonts)
//{
// oRenderParameter.appFonts = new CApplicationFonts();
// oRenderParameter.appFonts->Initialize();
//}
std::wstring fileName = m_oPicture->m_sPicFilename;
if (fileName.empty()) return false;
CApplicationFonts appFonts;
appFonts.Initialize();
CBgraFrame image;
MetaFile::CMetaFile meta_file(/*oRenderParameter.*/&appFonts);
if ( meta_file.LoadFromFile(fileName.c_str()))
{
double dX = 0, dY = 0, dW = 0, dH = 0;
meta_file.GetBounds(&dX, &dY, &dW, &dH);
Width = dW;
Height = dH;
}
else if ( image.OpenFile(fileName, 0 ))
{
Width = image.get_Width();
Height = image.get_Height();
return true;
}
return false;
}
void RtfShape::SetDefault()
{
@ -928,25 +971,47 @@ std::wstring RtfShape::RenderToOOXBegin(RenderParameter oRenderParameter)
//sStyle += L"right:" + std::to_wstring() + L";" , m_nRelRight);
sStyle += L"width:" + std::to_wstring(nWidth) + L";height:" + std::to_wstring(nHeight) + L";";
}
else if( 0 != m_oPicture && PROP_DEF != m_oPicture->m_nWidthGoal && PROP_DEF != m_oPicture->m_nHeightGoal
&& PROP_DEF != (int)m_oPicture->m_dScaleX && PROP_DEF != (int)m_oPicture->m_dScaleY )
else if( 0 != m_oPicture)
{
float nWidth = (int)(m_oPicture->m_nWidthGoal * m_oPicture->m_dScaleX / 100.);
if( PROP_DEF != m_oPicture->m_nCropL )
nWidth -= m_oPicture->m_nCropL;
if( PROP_DEF != m_oPicture->m_nCropR )
nWidth -= m_oPicture->m_nCropR;
if (PROP_DEF != m_oPicture->m_nWidthGoal && PROP_DEF != m_oPicture->m_nHeightGoal
&& PROP_DEF != (int)m_oPicture->m_dScaleX && PROP_DEF != (int)m_oPicture->m_dScaleY )//scale default = 100.
{
float nWidth = m_oPicture->m_nWidthGoal * m_oPicture->m_dScaleX / 100.f;
if( PROP_DEF != m_oPicture->m_nCropL )
nWidth -= m_oPicture->m_nCropL;
if( PROP_DEF != m_oPicture->m_nCropR )
nWidth -= m_oPicture->m_nCropR;
float nHeight = (int)(m_oPicture->m_nHeightGoal * m_oPicture->m_dScaleY / 100.);
if( PROP_DEF != m_oPicture->m_nCropT )
nHeight -= m_oPicture->m_nCropT;
if( PROP_DEF != m_oPicture->m_nCropB )
nHeight -= m_oPicture->m_nCropB;
float nHeight = m_oPicture->m_nHeightGoal * m_oPicture->m_dScaleY / 100.f;
if( PROP_DEF != m_oPicture->m_nCropT )
nHeight -= m_oPicture->m_nCropT;
if( PROP_DEF != m_oPicture->m_nCropB )
nHeight -= m_oPicture->m_nCropB;
if (oRenderParameter.nType == RENDER_TO_OOX_PARAM_SHAPE_WSHAPE2)
sStyle += L"width:" + XmlUtils::DoubleToString(RtfUtility::Twip2pt(nWidth), L"%.2f") + L";height:" + XmlUtils::DoubleToString(RtfUtility::Twip2pt(nHeight), L"%.2f") + L";";
if (oRenderParameter.nType == RENDER_TO_OOX_PARAM_SHAPE_WSHAPE2)
sStyle += L"width:" + XmlUtils::DoubleToString(RtfUtility::Twip2pt(nWidth), L"%.2f") + L";height:" + XmlUtils::DoubleToString(RtfUtility::Twip2pt(nHeight), L"%.2f") + L";";
else
sStyle += L"width:" + XmlUtils::DoubleToString(RtfUtility::Twip2pt(nWidth), L"%.2f") + L"pt;height:" + XmlUtils::DoubleToString(RtfUtility::Twip2pt(nHeight), L"%.2f") + L"pt;";
}
else
sStyle += L"width:" + XmlUtils::DoubleToString(RtfUtility::Twip2pt(nWidth), L"%.2f") + L"pt;height:" + XmlUtils::DoubleToString(RtfUtility::Twip2pt(nHeight), L"%.2f") + L"pt;";
{
int fileWidth = 0, fileHeight = 0;
if (PROP_DEF == m_oPicture->m_nWidth || PROP_DEF == m_oPicture->m_nHeight)
{
if (GetPictureResolution(oRenderParameter, fileWidth, fileHeight) || fileWidth < 1 || fileHeight < 1)
{
if (PROP_DEF == m_oPicture->m_nWidth)
m_oPicture->m_nWidth = fileWidth *20 / 4 * 3; // px->twip
if (PROP_DEF == m_oPicture->m_nHeight)
m_oPicture->m_nHeight = fileHeight *20 / 4 * 3;
}
}
if (PROP_DEF != m_oPicture->m_nWidth && PROP_DEF != m_oPicture->m_nHeight)
{
sStyle += L"width:" + XmlUtils::DoubleToString(RtfUtility::Twip2pt(m_oPicture->m_nWidth), L"%.2f") + L"pt;height:" + XmlUtils::DoubleToString(RtfUtility::Twip2pt(m_oPicture->m_nHeight), L"%.2f") + L"pt;";
}
}
}
switch( m_nPositionH )

View File

@ -291,5 +291,7 @@ public:
std::wstring GroupRenderToRtf(RenderParameter oRenderParameter);
std::wstring GroupRenderToOOX(RenderParameter oRenderParameter);
bool GetPictureResolution(RenderParameter oRenderParameter, int & Width, int &Height);
};

View File

@ -219,6 +219,96 @@ bool OOXDocumentWriter::SaveByItemStart( std::wstring sFolder )
m_oFileWriter->Write((BYTE*)sXmlUTF.c_str(), sXmlUTF.length());
return true;
}
bool OOXDocumentWriter::SaveBySection()
{
if( m_oDocument.GetCount() < 1)
return false;
RenderParameter oNewParam;
oNewParam.poDocument = &m_oDocument;
oNewParam.poWriter = &m_oWriter;
oNewParam.poRels = &m_oWriter.m_oDocRels;
oNewParam.nType = RENDER_TO_OOX_PARAM_UNKNOWN;
std::wstring sXml, sXmlSectProp;
bool bParaPrevEmpty = false, bParaCurrEmpty = false;
for (int i = 0; i < m_oDocument[0].props->GetCount(); i++)
{
if (i == m_oDocument[0].props->GetCount() - 1 && m_oDocument.GetCount() > 1)
{
_section section;
if (m_oDocument.GetItem(section, 0))
{
sXmlSectProp = section.props->RenderToOOX(oNewParam);
}
}
RtfParagraph *para = dynamic_cast<RtfParagraph *>(m_oDocument[0].props->operator[](i).get());
bParaCurrEmpty = (para) ? (para->GetCount() < 1) : true;
sXml = m_oDocument[0].props->operator[](i)->RenderToOOX(oNewParam);
if (!sXml.empty() || !sXmlSectProp.empty())
{
if (sXml.empty())
{
sXml = L"<w:p><w:pPr>" + sXmlSectProp + L"</w:pPr></w:p>";
}
else
{
if (!sXmlSectProp.empty())
{
if (bParaPrevEmpty && bParaCurrEmpty)
{
sXml += L"<w:p><w:pPr>" + sXmlSectProp + L"</w:pPr></w:p>";
}
else
{
int nFind = -1, nFindPict = -1, pos = sXml.size();
do
{
nFindPict = sXml.rfind(L"<w:pict>", pos);
nFind = sXml.rfind(L"</w:pPr>", pos);
pos = nFindPict - 1;
}while(nFindPict > 0 && nFind > nFindPict);
if( -1 != nFind)
{
sXml.insert( nFind, sXmlSectProp );
}
else
{
int Find = sXml.rfind( L"<w:p>" );
if( -1 != nFind )
sXml.insert( nFind + 5, L"<w:pPr>" + sXmlSectProp + L"</w:pPr>" );
}
}
}
}
std::string sXmlUTF = NSFile::CUtf8Converter::GetUtf8StringFromUnicode(sXml);
if (m_oFileWriter)
{
m_oFileWriter->Write((BYTE*)sXmlUTF.c_str(), sXmlUTF.length());
}
}
bParaPrevEmpty = bParaCurrEmpty;
}
if (m_oDocument.GetCount() > 1 )
{
m_oDocument.RemoveItem( 0 ); //удаляем секцию кроме последней
}
else
{
m_oDocument[0].props->m_aArray.clear();
}
return true;
}
bool OOXDocumentWriter::SaveByItem()
{
if( true == m_bFirst )

View File

@ -57,9 +57,15 @@ public:
std::wstring CreateXmlEnd( );
bool SaveByItemStart( std::wstring sFolder );
bool SaveByItem ();
bool SaveByItemEnd ();
bool SaveByItem();
bool SaveByItemEnd();
bool SaveBySection();
int GetCountSections()
{
return m_oDocument.GetCount();
}
int GetCount()
{
int nCount = 0;

View File

@ -100,12 +100,15 @@ OOXWriter::~OOXWriter()
}
bool OOXWriter::Save()
{
int nItemsCount = ((OOXDocumentWriter*)m_poDocumentWriter)->GetCount();
int nItemsCount = ((OOXDocumentWriter*)m_poDocumentWriter)->GetCountSections();
SaveByItemStart();
for( int i = 0; i < nItemsCount; i++ )
{
SaveByItem();
SaveBySection();
}
return SaveByItemEnd();
}
bool OOXWriter::SaveByItemStart()
@ -116,6 +119,10 @@ bool OOXWriter::SaveByItem()
{
return ((OOXDocumentWriter*)m_poDocumentWriter)->SaveByItem();
}
bool OOXWriter::SaveBySection()
{
return ((OOXDocumentWriter*)m_poDocumentWriter)->SaveBySection();
}
bool OOXWriter::SaveByItemEnd()
{
OOX::CContentTypes oContentTypes;

View File

@ -75,6 +75,8 @@ public:
bool SaveByItem();
bool SaveByItemEnd();
bool SaveBySection();
private:
RtfDocument& m_oDocument;
};