djvu structure

This commit is contained in:
Kulikova Svetlana
2021-07-23 18:18:18 +03:00
parent 6ad4a538eb
commit d652e3c32f
11 changed files with 434 additions and 17 deletions

View File

@ -336,6 +336,169 @@ void CDjVuFileImplementation::ConvertToPdf(const std::wstring& wsD
oPdf.SaveToFile(wsDstPath);
}
class CData
{
protected:
unsigned char* m_pData;
size_t m_lSize;
unsigned char* m_pDataCur;
size_t m_lSizeCur;
public:
CData()
{
m_pData = NULL;
m_lSize = 0;
m_pDataCur = m_pData;
m_lSizeCur = m_lSize;
}
virtual ~CData()
{
Clear();
}
inline void AddSize(size_t nSize)
{
if (NULL == m_pData)
{
m_lSize = 1000;
if (nSize > m_lSize)
m_lSize = nSize;
m_pData = (unsigned char*)malloc(m_lSize * sizeof(unsigned char));
m_lSizeCur = 0;
m_pDataCur = m_pData;
return;
}
if ((m_lSizeCur + nSize) > m_lSize)
{
while ((m_lSizeCur + nSize) > m_lSize)
m_lSize *= 2;
unsigned char* pRealloc = (unsigned char*)realloc(m_pData, m_lSize * sizeof(unsigned char));
if (NULL != pRealloc)
{
m_pData = pRealloc;
m_pDataCur = m_pData + m_lSizeCur;
}
else
{
unsigned char* pMalloc = (unsigned char*)malloc(m_lSize * sizeof(unsigned char));
memcpy(pMalloc, m_pData, m_lSizeCur * sizeof(unsigned char));
free(m_pData);
m_pData = pMalloc;
m_pDataCur = m_pData + m_lSizeCur;
}
}
}
public:
void AddInt(unsigned int value)
{
AddSize(4);
memcpy(m_pDataCur, &value, sizeof(unsigned int));
m_pDataCur += 4;
m_lSizeCur += 4;
}
void WriteString(unsigned char* value, unsigned int len)
{
AddSize(len + 4);
memcpy(m_pDataCur, &len, sizeof(unsigned int));
m_pDataCur += 4;
m_lSizeCur += 4;
memcpy(m_pDataCur, value, len);
m_pDataCur += len;
m_lSizeCur += len;
}
unsigned char* GetBuffer()
{
return m_pData;
}
void Clear()
{
if (m_pData) free(m_pData);
m_pData = NULL;
m_lSize = 0;
m_pDataCur = m_pData;
m_lSizeCur = 0;
}
void ClearWithoutAttack()
{
m_pData = NULL;
m_lSize = 0;
m_pDataCur = m_pData;
m_lSizeCur = 0;
}
void ClearNoAttack()
{
m_pDataCur = m_pData;
m_lSizeCur = 0;
}
unsigned int GetSize()
{
return (unsigned int)m_lSizeCur;
}
void SkipLen()
{
AddInt(0);
}
void WriteLen()
{
unsigned int len = (unsigned int)m_lSizeCur;
memcpy(m_pData, &len, sizeof(unsigned int));
}
};
void getBookmars(const GP<DjVmNav>& nav, int& pos, int count, CData& out, int level)
{
while (count > 0 && pos < nav->getBookMarkCount())
{
GP<DjVmNav::DjVuBookMark> gpBookMark;
nav->getBookMark(gpBookMark, pos++);
GUTF8String str = gpBookMark->url;
int endpos;
DWORD nPage = str.toULong(1, endpos);
if (endpos == (int)str.length())
{
out.AddInt(nPage);
out.AddInt(level);
GUTF8String description = gpBookMark->displayname;
out.WriteString((BYTE*)description.getbuf(), description.length());
}
getBookmars(nav, pos, gpBookMark->count, out, level + 1);
count--;
}
}
BYTE* CDjVuFileImplementation::GetStructure()
{
GP<DjVmNav> nav = m_pDoc->get_djvm_nav();
if (!nav)
return NULL;
int pos = 0;
int count = nav->getBookMarkCount();
if (count <= 0)
return NULL;
CData oRes;
oRes.SkipLen();
getBookmars(nav, pos, count, oRes, 1);
oRes.WriteLen();
BYTE* bRes = oRes.GetBuffer();
oRes.ClearWithoutAttack();
return bRes;
}
void CDjVuFileImplementation::CreateFrame(IRenderer* pRenderer, GP<DjVuImage>& pPage, int nPage, XmlUtils::CXmlNode& text)
{
int nWidth = pPage->get_real_width();