Fix problem with spaces.

This commit is contained in:
SEAlGo
2022-05-23 11:05:54 +03:00
parent 25a10fb642
commit 7f3a658611
7 changed files with 2563 additions and 2518 deletions

View File

@ -36,11 +36,11 @@
class CDocxRenderer_Private
{
public:
public:
NSDocxRenderer::CDocument m_oDocument;
std::wstring m_sTempDirectory;
public:
public:
CDocxRenderer_Private(NSFonts::IApplicationFonts* pFonts, IRenderer* pRenderer) : m_oDocument(pRenderer, pFonts)
{
}

View File

@ -60,7 +60,7 @@ namespace NSDocxRenderer
class CDocxRenderer_Private;
class DOCXRENDERER_DECL_EXPORT CDocxRenderer : public IRenderer
{
public:
public:
CDocxRenderer(NSFonts::IApplicationFonts* pAppFonts);
virtual ~CDocxRenderer();
@ -204,7 +204,7 @@ public:
HRESULT SetTextAssociationType(const NSDocxRenderer::TextAssociationType& eType);
int Convert(IOfficeDrawingFile* pFile, const std::wstring& sDstFile, bool bIsOutCompress = true);
private:
private:
CDocxRenderer_Private* m_pInternal;
};

View File

@ -28,8 +28,7 @@ namespace NSDocxRenderer
m_lClipMode = 0;
}
CDocument::~CDocument()
{
CDocument::~CDocument() {
m_lClipMode = 0;
RELEASEINTERFACE(m_pFontManager);
}
@ -841,14 +840,14 @@ namespace NSDocxRenderer
m_oDocumentStream.CloseFile();
m_oDocumentStream.CreateFileW(m_strTempDirectory + L"/word/document.xml");
m_oDocumentStream.WriteStringUTF8(L"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\
<w:document xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" \
xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" \
xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" \
xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\" \
xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" \
xmlns:o=\"urn:schemas-microsoft-com:office:office\" \
xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:w10=\"urn:schemas-microsoft-com:office:word\">\
<w:body>");
<w:document xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" \
xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" \
xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" \
xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\" \
xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" \
xmlns:o=\"urn:schemas-microsoft-com:office:office\" \
xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:w10=\"urn:schemas-microsoft-com:office:word\">\
<w:body>");
m_lPagesCount = 0;
m_oWriter.Clear();
@ -863,12 +862,12 @@ xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:w10=\"urn:schemas-microsoft-com:
NSStringUtils::CStringBuilder oWriter;
oWriter.WriteString(L"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\
<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">\
<Relationship Id=\"rId1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles\" Target=\"styles.xml\"/>\
<Relationship Id=\"rId2\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings\" Target=\"settings.xml\"/>\
<Relationship Id=\"rId3\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings\" Target=\"webSettings.xml\"/>\
<Relationship Id=\"rId4\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable\" Target=\"fontTable.xml\"/>\
<Relationship Id=\"rId5\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme\" Target=\"theme/theme.xml\"/>");
<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">\
<Relationship Id=\"rId1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles\" Target=\"styles.xml\"/>\
<Relationship Id=\"rId2\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings\" Target=\"settings.xml\"/>\
<Relationship Id=\"rId3\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings\" Target=\"webSettings.xml\"/>\
<Relationship Id=\"rId4\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable\" Target=\"fontTable.xml\"/>\
<Relationship Id=\"rId5\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme\" Target=\"theme/theme.xml\"/>");
for (std::map<DWORD, CImageInfo>::iterator iterImage = m_oManager.m_mapImageData.begin(); iterImage != m_oManager.m_mapImageData.end(); iterImage++)
{
@ -899,7 +898,7 @@ xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:w10=\"urn:schemas-microsoft-com:
// сохраним fontTable
oWriter.WriteString(L"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\
<w:fonts xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">");
<w:fonts xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">");
CFontTable* pFontTable = &m_oCurrentPage.m_oManager.m_oFontTable;
for (std::map<std::wstring, CFontTableEntry>::iterator iterFont = pFontTable->m_mapTable.begin(); iterFont != pFontTable->m_mapTable.end(); iterFont++)

View File

@ -2,7 +2,7 @@
namespace NSDocxRenderer
{
void DeleteSpaces(NSStringUtils::CStringUTF32& oText)
void RemoveSpacesAtBothEnds(NSStringUtils::CStringUTF32& oText)
{
size_t nLen = oText.length();
size_t nStart = 0;
@ -41,8 +41,9 @@ namespace NSDocxRenderer
m_dSpaceWidthMM = 0;
m_dCalculateWidth = 0;
m_dSpaceByText = 0;
m_bIsNeedSpaceAtTheEnd = false;
}
void CContText::Clear()
@ -85,6 +86,8 @@ namespace NSDocxRenderer
m_dCalculateWidth = oSrc.m_dCalculateWidth;
m_dSpaceByText = oSrc.m_dSpaceByText;
m_bIsNeedSpaceAtTheEnd = oSrc.m_bIsNeedSpaceAtTheEnd;
return *this;
}
@ -115,7 +118,7 @@ namespace NSDocxRenderer
if (m_dWidth != m_dWidthWithoutSpaces)
{
DeleteSpaces(m_oText);
RemoveSpacesAtBothEnds(m_oText);
m_dWidth = m_dWidthWithoutSpaces;
}
@ -126,7 +129,7 @@ namespace NSDocxRenderer
if (m_oFont.Italic)
oWriter.WriteString(L"<w:i w:val=\"true\"/>");
if (bIsAddSpace)
if (bIsAddSpace || m_bIsNeedSpaceAtTheEnd)
{
m_dWidth += m_dSpaceWidthMM;
m_oText += L" ";
@ -139,7 +142,7 @@ namespace NSDocxRenderer
if (0x02 == (0x02 & m_lPickFontStyle))
oWriter.WriteString(L"<w:i w:val=\"true\"/>");
if (bIsAddSpace)
if (bIsAddSpace || m_bIsNeedSpaceAtTheEnd)
{
m_dWidth += pManagerLight->GetSpaceWidth();
m_oText += L" ";
@ -248,6 +251,11 @@ namespace NSDocxRenderer
oWriter.WriteString(L"</w:r>");
}
void CContText::AddSpaceToEnd()
{
m_bIsNeedSpaceAtTheEnd = true;
}
CTextLine::CTextLine() : m_arConts()
{
m_dBaselinePos = 0;
@ -407,7 +415,7 @@ namespace NSDocxRenderer
void CTextLine::CalculateWidth()
{
m_dWidth = 2; //это хак - по идее должно == 0.
m_dWidth = 1; //это хак - по идее должно == 0.
for (size_t i = 0; i < m_arConts.size(); ++i)
{
m_dWidth += m_arConts[i]->m_dWidthWithoutSpaces;
@ -417,39 +425,12 @@ namespace NSDocxRenderer
void CTextLine::AddSpaceToEnd()
{
/*double dX = 0;
size_t isNeedIndex = 0;
for (size_t i = 0; i < m_arConts.size(); i++)
{
if (dX < m_arConts[i]->m_dX)
{
dX = m_arConts[i]->m_dX;
isNeedIndex = i;
}
}
CContText* pCurrent = m_arConts[isNeedIndex];
CContText* pCurrent = m_arConts.back();
if (pCurrent->m_oText[pCurrent->m_oText.length()-1] != uint32_t(' '))
{
NSStringUtils::CStringUTF32 oText(pCurrent->m_oText);
oText += uint32_t(' ');
pCurrent->m_oText = oText;
pCurrent->m_dWidth += pCurrent->m_dSpaceWidthMM;
m_dWidth += pCurrent->m_dSpaceWidthMM;
}*/
/*CContText* pCurrent = m_arConts.back();
if (pCurrent->m_oText[pCurrent->m_oText.length()-1] != uint32_t(' '))
{
pCurrent->m_oText += uint32_t(' ');
//pCurrent->m_dWidth += pCurrent->m_dSpaceWidthMM;
//m_dWidth += pCurrent->m_dSpaceWidthMM;
}*/
m_arConts.back()->m_oText += uint32_t(' ');
pCurrent->AddSpaceToEnd();
}
}
bool CTextLine::IsForceBlock()

View File

@ -85,7 +85,7 @@ namespace NSDocxRenderer
}
}
void DeleteSpaces(NSStringUtils::CStringUTF32& oText);
void RemoveSpacesAtBothEnds(NSStringUtils::CStringUTF32& oText);
class CContText
{
@ -97,7 +97,7 @@ namespace NSDocxRenderer
LONG m_lPickFontStyle;
NSStringUtils::CStringUTF32 m_oText;
NSStringUtils::CStringUTF32 m_oGidText;
NSStringUtils::CStringUTF32 m_oGidText; //что это?
double m_dX;
double m_dY;
@ -114,6 +114,8 @@ namespace NSDocxRenderer
double m_dCalculateWidth;
double m_dSpaceByText;
bool m_bIsNeedSpaceAtTheEnd;
public:
CContText();
~CContText(){}
@ -137,6 +139,8 @@ namespace NSDocxRenderer
void WriteTo(double dSpacingMM,
NSStringUtils::CStringBuilder& oWriter,
CFontManagerLight* pManagerLight);
void AddSpaceToEnd();
};
class CTextLine

View File

@ -26,8 +26,9 @@ namespace NSDocxRenderer
m_dLastTextX_block = m_dLastTextX;
}
void CPage::Init(NSStructures::CFont* pFont, NSStructures::CPen* pPen, NSStructures::CBrush* pBrush,
NSStructures::CShadow* pShadow, NSStructures::CEdgeText* pEdge, Aggplus::CMatrix* pMatrix, Aggplus::CGraphicsPathSimpleConverter* pSimple)
void CPage::Init(NSStructures::CFont* pFont, NSStructures::CPen* pPen, NSStructures::CBrush* pBrush,
NSStructures::CShadow* pShadow, NSStructures::CEdgeText* pEdge, Aggplus::CMatrix* pMatrix,
Aggplus::CGraphicsPathSimpleConverter* pSimple)
{
m_pFont = pFont;
m_pPen = pPen;
@ -50,7 +51,7 @@ void CPage::Init(NSStructures::CFont* pFont, NSStructures::CPen* pPen, NSStructu
m_dLastTextX_block = m_dLastTextX;
}
void CPage::Clear()
void CPage::Clear()
{
ClearTextLines();
ClearGraphicItems();
@ -120,8 +121,8 @@ void CPage::Clear()
if ((NULL == m_pCurrentLine) || (TextAssociationTypeBlockChar == m_eTextAssociationType))
{
// пустая (в плане текста) страница
m_pCurrentLine = new CTextLine();
m_pCurrentLine = new CTextLine();
m_pCurrentLine->m_dBaselinePos = dBaseLinePos;
m_arTextLine.push_back(m_pCurrentLine);
return;
@ -744,7 +745,7 @@ void CPage::Clear()
pParagraph->m_dRight = m_dWidth - (pTextLine->m_dX + pTextLine->m_dWidth);
pParagraph->m_dWidth = pTextLine->m_dWidth;
double dBeforeSpacing = (pTextLine->m_dBaselinePos - previousStringOffset - pTextLine->m_dHeight - pTextLine->m_dBaselineOffset);
double dBeforeSpacing = pTextLine->m_dBaselinePos - previousStringOffset - pTextLine->m_dHeight - pTextLine->m_dBaselineOffset;
pParagraph->m_dSpaceBefore = std::max(dBeforeSpacing, 0.0);
pParagraph->m_dBaselinePos = pTextLine->m_dBaselinePos;
@ -765,7 +766,10 @@ void CPage::Clear()
pParagraph->m_arLines.push_back(pTextLine);
pParagraph->m_numLines++;
if (m_eTextAssociationType != TextAssociationTypePlainLine)
{
CalculateTextAlignmentType(*pParagraph);
}
m_arParagraphs.push_back(pParagraph);
}
@ -854,7 +858,7 @@ void CPage::Clear()
pParagraph->m_bIsNeedFirstLineIndent = true;
//Объединим 2 параграфа-строчки
pParagraph->m_arLines.back()->AddSpaceToEnd(); //todo - невсегда добавляется пробел, если текущая строка сложная (разные m_arConts)
pParagraph->m_arLines.back()->AddSpaceToEnd();
for (size_t i = 0; i < nextP->m_arLines.size(); ++i)
{
pParagraph->m_arLines.push_back(new CTextLine(*nextP->m_arLines[i]));
@ -879,7 +883,8 @@ void CPage::Clear()
if (nextIndex < m_arParagraphs.size()-1)
{
currP = nextP;
nextP = m_arParagraphs[++nextIndex];
++nextIndex;
nextP = m_arParagraphs[nextIndex];
while(nextIndex < m_arParagraphs.size() &&
fabs(currP->m_dHeight - nextP->m_dHeight) < THE_SAME_STRING_Y_PRECISION_MM &&
@ -892,8 +897,8 @@ void CPage::Clear()
index++;
//Объединим 2 параграфа-строчки
pParagraph->m_arLines.back()->AddSpaceToEnd(); //todo - невсегда добавляется пробел, если текущая строка сложная (разные m_arConts)
for (size_t i = 0; i < nextP->m_arLines.size(); ++i)
pParagraph->m_arLines.back()->AddSpaceToEnd();
for (size_t i = 0; i < nextP->m_arLines.size()-1; ++i)
{
pParagraph->m_arLines.push_back(new CTextLine(*nextP->m_arLines[i]));
pParagraph->m_numLines++;
@ -907,9 +912,10 @@ void CPage::Clear()
corrHeight = (corrHeight + currP->m_dHeight)/2;
corrBeforeSpacing += nextP->m_dSpaceBefore;
if (nextIndex < m_arParagraphs.size()) {
if (nextIndex < m_arParagraphs.size()-1) {
currP = nextP;
nextP = m_arParagraphs[++nextIndex];
++nextIndex;
nextP = m_arParagraphs[nextIndex];
} else {
break;
}

View File

@ -87,15 +87,52 @@ int main(int argc, char *argv[])
if (!NSDirectory::Exists(sTempDirOut))
NSDirectory::CreateDirectory(sTempDirOut);
std::wstring sSourceFile = L"PATH_TO_TEST_FILE";
std::wstring sDestFile = NSFile::GetProcessDirectory() + L"/output.docx";
//Добавляем все файлы из определенного каталога
//std::vector<std::wstring> sSourceFiles = NSDirectory::GetFiles(L"C:\\Docs\\Рекомендуемая литература");
std::vector<std::wstring> sSourceFiles;
//Или добавляем любой нужный файл
sSourceFiles.push_back(L"C:\\Development\\test\\andersen_skazki_tom_1.pdf");
sSourceFiles.push_back(L"C:\\Development\\test\\08 Правила дорожного движения РФ(108p).pdf");
sSourceFiles.push_back(L"C:\\Docs\\Рекомендуемая литература\\Мартин Фаулер - Рефакторинг.pdf");
//sSourceFiles.push_back(L"C:\\Development\\test\\(1917) - Das geheimnisvolle Haus.pdf");
//sSourceFiles.push_back(L"C:\\Docs\\Рекомендуемая литература\\Embedded_Linux_system_design_and_development_ru.pdf");
//sSourceFiles.push_back(L"C:\\Docs\\Рекомендуемая литература\\Алан Купер - Психбольница в руках пациентов.pdf");
///sSourceFiles.push_back(L"C:\\Docs\\Рекомендуемая литература\\Брюс Эккель - Философия C++.pdf");
//sSourceFiles.push_back(L"C:\\Docs\\Рекомендуемая литература\\Кент Бек - Экстремальное программирование.pdf");
//sSourceFiles.push_back(L"C:\\Docs\\Рекомендуемая литература\\Приёмы объектно-ориентированного проектирования. Паттерны проектирования.pdf");
//sSourceFiles.push_back(L"C:\\Docs\\Рекомендуемая литература\\Роберт Мартин - Чистый Код.pdf");
std::wstring sTextDirOut = NSFile::GetProcessDirectory() + L"/text";
if (!NSDirectory::Exists(sTextDirOut))
NSDirectory::CreateDirectory(sTextDirOut);
std::wstring sPlainParagraphDirOut = sTextDirOut + L"/PlainParagraph";
if (!NSDirectory::Exists(sPlainParagraphDirOut))
NSDirectory::CreateDirectory(sPlainParagraphDirOut);
std::wstring sPlainLineDirOut = sTextDirOut + L"/PlainLine";
if (!NSDirectory::Exists(sPlainLineDirOut))
NSDirectory::CreateDirectory(sPlainLineDirOut);
/*std::wstring sBlockCharDirOut = sTextDirOut + L"/BlockChar";
if (!NSDirectory::Exists(sBlockCharDirOut))
NSDirectory::CreateDirectory(sBlockCharDirOut);
std::wstring sBlockLineDirOut = sTextDirOut + L"/BlockLine";
if (!NSDirectory::Exists(sBlockLineDirOut))
NSDirectory::CreateDirectory(sBlockCharDirOut);*/
IOfficeDrawingFile* pReader = NULL;
COfficeFileFormatChecker oChecker;
if (oChecker.isOfficeFile(sSourceFile))
int nFileType = 0;
CDocxRenderer oDocxRenderer(pFonts);
oDocxRenderer.SetTempFolder(sTempDirOut);
for (size_t index = 0; index < sSourceFiles.size(); index++)
{
switch (oChecker.nFileType)
if (oChecker.isOfficeFile(sSourceFiles[index]))
{
nFileType = oChecker.nFileType;
switch (nFileType)
{
case AVS_OFFICESTUDIO_FILE_CROSSPLATFORM_PDF:
pReader = new PdfReader::CPdfReader(pFonts);
@ -120,7 +157,7 @@ int main(int argc, char *argv[])
pReader->SetTempDirectory(sTempDir);
#ifndef LOAD_FILE_AS_BINARY
pReader->LoadFromFile(sSourceFile);
pReader->LoadFromFile(sSourceFiles[index]);
#else
BYTE* pFileBinary = NULL;
DWORD nFileBinaryLen = 0;
@ -142,19 +179,37 @@ int main(int argc, char *argv[])
}
}
#else
CDocxRenderer oDocxRenderer(pFonts);
// проверить все режимы
NSDocxRenderer::TextAssociationType taType;
//taType = NSDocxRenderer::TextAssociationTypeBlockChar;
//taType = NSDocxRenderer::TextAssociationTypeBlockLine;
taType = NSDocxRenderer::TextAssociationTypePlainLine;
//taType = NSDocxRenderer::TextAssociationTypePlainParagraph;
oDocxRenderer.SetTextAssociationType(taType);
std::wstring sExtention = NSFile::GetFileExtention(sSourceFiles[index]);
std::wstring sFileNameWithExtention = NSFile::GetFileName(sSourceFiles[index]);
std::wstring sFileName = sFileNameWithExtention.substr(0, sFileNameWithExtention.size() - 1 - sExtention.size());
std::wstring sDocx = L"/" + sFileName + L".docx";
std::wstring sZip = L"/" + sFileName + L".zip";
oDocxRenderer.SetTempFolder(sTempDirOut);
oDocxRenderer.Convert(pReader, sDestFile);
NSDocxRenderer::TextAssociationType taType;
taType = NSDocxRenderer::TextAssociationTypePlainParagraph;
oDocxRenderer.SetTextAssociationType(taType);
oDocxRenderer.Convert(pReader, sPlainParagraphDirOut+sDocx);
oDocxRenderer.Convert(pReader, sPlainParagraphDirOut+sZip);
taType = NSDocxRenderer::TextAssociationTypePlainLine;
oDocxRenderer.SetTextAssociationType(taType);
oDocxRenderer.Convert(pReader, sPlainLineDirOut + sDocx);
oDocxRenderer.Convert(pReader, sPlainLineDirOut + sZip);
/*taType = NSDocxRenderer::TextAssociationTypeBlockLine;
oDocxRenderer.SetTextAssociationType(taType);
oDocxRenderer.Convert(pReader, sBlockLineDirOut + sDocx);
oDocxRenderer.Convert(pReader, sBlockLineDirOut + sZip);
taType = NSDocxRenderer::TextAssociationTypeBlockChar;
oDocxRenderer.SetTextAssociationType(taType);
oDocxRenderer.Convert(pReader, sBlockCharDirOut + sDocx);
oDocxRenderer.Convert(pReader, sBlockCharDirOut + sZip);*/
#endif
}
delete pReader;
pFonts->Release();