Fix ToBin function

This commit is contained in:
Alexey Nagaev
2025-05-23 21:03:47 +03:00
parent 5942b88c8d
commit 44bb82919f
8 changed files with 50 additions and 27 deletions

View File

@ -423,20 +423,26 @@ public:
std::vector<std::wstring> arShapes;
if (0 == mode)
arShapes = oRenderer.ScanPage(m_pFile, nPageIndex);
else
else if (1 == mode)
arShapes = oRenderer.ScanPagePptx(m_pFile, nPageIndex);
int nLen = (int)arShapes.size();
NSWasm::CData oRes;
oRes.SkipLen();
oRes.AddInt(nLen);
for (int i = 0; i < nLen; ++i)
oRes.WriteString(arShapes[i]);
oRes.WriteLen();
// mode 2 is binary format
if (mode != 2)
{
oRes.SkipLen();
oRes.AddInt(nLen);
for (int i = 0; i < nLen; ++i)
oRes.WriteString(arShapes[i]);
oRes.WriteLen();
}
else
{
oRes = oRenderer.ScanPageBin(m_pFile, nPageIndex);
}
BYTE* res = oRes.GetBuffer();
oRes.ClearWithoutAttack();
return res;

View File

@ -1570,6 +1570,11 @@ CFile.prototype["scanPage"] = function(page, mode)
if (!reader) return [];
if (mode == 2) {
ptr.free();
return reader.data;
}
let shapesCount = reader.readInt();
let shapes = new Array(shapesCount);

View File

@ -152,6 +152,13 @@ namespace NSWasm
m_pDataCur += 4;
m_lSizeCur += 4;
}
void AddSInt(int value)
{
AddSize(4);
memcpy(m_pDataCur, &value, sizeof(int));
m_pDataCur += 4;
m_lSizeCur += 4;
}
void AddInt(unsigned int value, size_t pos)
{
if (pos < m_lSizeCur)
@ -234,15 +241,16 @@ namespace NSWasm
}
void WriteStringUtf16(const std::wstring& sStr)
{
unsigned int size = NSFile::CUtf8Converter::GetUtf16SizeFromUnicode(sStr.c_str(), (LONG)sStr.length(), false);
unsigned int size = static_cast<unsigned int>(sStr.size());
int output = 0;
size_t max_addition_size = 4 * size + 3 + 2;
AddSize(size + 4);
AddSize(max_addition_size);
memcpy(m_pDataCur, &size, sizeof(unsigned int));
m_pDataCur += 4;
m_lSizeCur += 4;
m_pDataCur += sizeof(unsigned int); // + 4
m_lSizeCur += sizeof(unsigned int);
NSFile::CUtf8Converter::GetUtf16StringFromUnicode_4bytes(sStr.c_str(), (LONG)sStr.length(), m_pDataCur, output, false);
NSFile::CUtf8Converter::GetUtf16StringFromUnicode_4bytes(sStr.c_str(), (LONG)size, m_pDataCur, output, false);
m_pDataCur += static_cast<unsigned int>(output);
m_lSizeCur += static_cast<unsigned int>(output);

View File

@ -145,7 +145,7 @@ std::vector<std::wstring> CDocxRenderer::ScanPagePptx(IOfficeDrawingFile* pFile,
m_pInternal->m_oDocument.Clear();
return xml_shapes;
}
std::vector<NSWasm::CData> CDocxRenderer::ScanPageBin(IOfficeDrawingFile* pFile, size_t nPage)
NSWasm::CData CDocxRenderer::ScanPageBin(IOfficeDrawingFile* pFile, size_t nPage)
{
m_pInternal->m_oDocument.Clear();
m_pInternal->m_oDocument.Init(false);

View File

@ -179,7 +179,7 @@ public:
int Convert(IOfficeDrawingFile* pFile, const std::wstring& sDstFile, bool bIsOutCompress = true);
std::vector<std::wstring> ScanPage(IOfficeDrawingFile* pFile, size_t nPage);
std::vector<std::wstring> ScanPagePptx(IOfficeDrawingFile* pFile, size_t nPage);
std::vector<NSWasm::CData> ScanPageBin(IOfficeDrawingFile* pFile, size_t nPage);
NSWasm::CData ScanPageBin(IOfficeDrawingFile* pFile, size_t nPage);
void SetExternalImageStorage(NSDocxRenderer::IImageStorage* pStorage);
private:

View File

@ -460,23 +460,24 @@ namespace NSDocxRenderer
return xml_shapes;
}
std::vector<NSWasm::CData> CPage::GetShapesBin()
NSWasm::CData CPage::GetShapesBin()
{
ReorderShapesForPptx();
std::vector<NSWasm::CData> bin_shapes;
NSWasm::CData writer;
writer.SkipLen();
writer.AddInt(static_cast<unsigned int>(m_arShapes.size()));
for (const auto& shape : m_arShapes)
{
if (!shape) continue;
NSWasm::CData writer;
shape->ToBin(writer);
bin_shapes.push_back(std::move(writer));
}
// if (!m_arCompleteObjectsXml.empty())
// xml_shapes.insert(xml_shapes.end(), m_arCompleteObjectsXml.begin(), m_arCompleteObjectsXml.end());
return bin_shapes;
writer.WriteLen();
return writer;
}
void CPage::AddCompleteXml(const std::wstring oXml)
{

View File

@ -81,7 +81,7 @@ namespace NSDocxRenderer
std::vector<std::wstring> GetXmlShapes();
std::vector<std::wstring> GetXmlShapesPptx();
std::vector<NSWasm::CData> GetShapesBin();
NSWasm::CData GetShapesBin();
void AddCompleteXml(const std::wstring oXml);
private:

View File

@ -492,7 +492,7 @@ namespace NSDocxRenderer
}
void CContText::ToBin(NSWasm::CData& oWriter) const
{
LONG lCalculatedSpacing = 0;
int lCalculatedSpacing = 0;
if (!m_oText.empty())
{
double dSpacing = (m_dWidth - m_oSelectedSizes.dWidth) / (m_oText.length());
@ -520,16 +520,19 @@ namespace NSDocxRenderer
if (m_bIsDoubleStrikeout) strike = 0;
else if (m_bIsStrikeoutPresent) strike = 2;
oWriter.WriteBYTE(16); oWriter.WriteBYTE(strike);
oWriter.WriteBYTE(15); oWriter.AddInt(lCalculatedSpacing);
oWriter.WriteBYTE(15); oWriter.AddSInt(lCalculatedSpacing);
oWriter.WriteBYTE(18); oWriter.WriteBYTE(m_bIsUnderlinePresent ? 13 : 12);
oWriter.WriteBYTE(17); oWriter.AddInt(static_cast<unsigned int>(m_pFontStyle->dFontSize));
unsigned int font_size = static_cast<unsigned int>(m_pFontStyle->dFontSize) * 100;
const unsigned int min_font_size = 100;
oWriter.WriteBYTE(17); oWriter.AddInt(std::max(font_size, std::max(font_size, min_font_size)));
oWriter.WriteBYTE(2);
if (m_eVertAlignType == eVertAlignType::vatSubscript)
oWriter.AddInt(-25000);
oWriter.AddSInt(-25000);
else if (m_eVertAlignType == eVertAlignType::vatSuperscript)
oWriter.AddInt(30000);
oWriter.AddSInt(30000);
else
oWriter.AddInt(0);
oWriter.WriteBYTE(kBin_g_nodeAttributeEnd);