update credits
[LibreOffice.git] / hwpfilter / source / hwpfile.cxx
blob0082693f7b160c196ca30715c4c4247ff71071f9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "precompile.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include "hwplib.h"
27 #include "hwpfile.h"
28 #include "hiodev.h"
29 #include "hfont.h"
30 #include "hstyle.h"
31 #include "hbox.h"
32 #include "hpara.h"
33 #include "htags.h"
34 #include "hcode.h"
35 #include "hstream.h"
37 #include <osl/diagnose.h>
39 HWPFile *HWPFile::cur_doc = 0;
40 static int ccount = 0;
41 static int pcount = 0;
42 static int datecodecount = 0;
44 HWPFile::HWPFile()
45 : version(HWP_V30)
46 , compressed(false)
47 , encrypted(false)
48 , linenumber(0)
49 , info_block_len(0)
50 , error_code(HWP_NoError)
51 , oledata(0)
52 , m_nCurrentPage(1)
53 , m_nMaxSettedPage(0)
54 , hiodev(0)
55 , currenthyper(0)
57 SetCurrentDoc(this);
60 HWPFile::~HWPFile()
62 delete oledata;
63 delete hiodev;
65 std::list < ColumnInfo* >::iterator it_column = columnlist.begin();
66 for (; it_column != columnlist.end(); ++it_column)
67 delete *it_column;
69 std::list < HWPPara* >::iterator it = plist.begin();
70 for (; it != plist.end(); ++it)
71 delete *it;
73 std::list < Table* >::iterator tbl = tables.begin();
74 for (; tbl != tables.end(); ++tbl)
75 delete *tbl;
77 std::list < HyperText* >::iterator hyp = hyperlist.begin();
78 for (; hyp != hyperlist.end(); ++hyp)
80 delete *hyp;
84 int HWPFile::ReadHwpFile(HStream & stream)
86 if (Open(stream) != HWP_NoError)
87 return State();
88 InfoRead();
89 FontRead();
90 StyleRead();
91 AddColumnInfo();
92 ParaListRead();
93 TagsRead();
95 return State();
98 int detect_hwp_version(const char *str)
100 if (memcmp(V20SIGNATURE, str, HWPIDLen) == 0)
101 return HWP_V20;
102 else if (memcmp(V21SIGNATURE, str, HWPIDLen) == 0)
103 return HWP_V21;
104 else if (memcmp(V30SIGNATURE, str, HWPIDLen) == 0)
105 return HWP_V30;
106 return 0;
109 // HIODev wrapper
111 int HWPFile::Open(HStream & stream)
113 HStreamIODev *hstreamio = new HStreamIODev(stream);
115 if (!hstreamio->open())
117 delete hstreamio;
119 return SetState(HWP_EMPTY_FILE);
122 HIODev *pPrev = SetIODevice(hstreamio);
123 delete pPrev;
125 char idstr[HWPIDLen];
127 if (ReadBlock(idstr, HWPIDLen) <= 0
128 || HWP_V30 != (version = detect_hwp_version(idstr)))
130 return SetState(HWP_UNSUPPORTED_VERSION);
132 return HWP_NoError;
136 int HWPFile::State(void) const
138 return error_code;
142 int HWPFile::SetState(int errcode)
144 error_code = errcode;
145 return error_code;
149 int HWPFile::Read1b(void)
151 return hiodev ? hiodev->read1b() : -1;
155 int HWPFile::Read2b(void)
157 return hiodev ? hiodev->read2b() : -1;
161 long HWPFile::Read4b(void)
163 return hiodev ? hiodev->read4b() : -1;
167 int HWPFile::Read1b(void *ptr, size_t nmemb)
169 return hiodev ? hiodev->read1b(ptr, nmemb) : 0;
173 int HWPFile::Read2b(void *ptr, size_t nmemb)
175 return hiodev ? hiodev->read2b(ptr, nmemb) : 0;
179 int HWPFile::Read4b(void *ptr, size_t nmemb)
181 return hiodev ? hiodev->read4b(ptr, nmemb) : 0;
185 size_t HWPFile::ReadBlock(void *ptr, size_t size)
187 return hiodev ? hiodev->readBlock(ptr, size) : 0;
191 size_t HWPFile::SkipBlock(size_t size)
193 return hiodev ? hiodev->skipBlock(size) : 0;
197 bool HWPFile::SetCompressed(bool flag)
199 return hiodev ? hiodev->setCompressed(flag) : false;
203 HIODev *HWPFile::SetIODevice(HIODev * new_hiodev)
205 HIODev *old_hiodev = hiodev;
207 hiodev = new_hiodev;
209 return old_hiodev;
213 // end of HIODev wrapper
215 bool HWPFile::InfoRead(void)
217 return _hwpInfo.Read(*this);
221 bool HWPFile::FontRead(void)
223 return _hwpFont.Read(*this);
227 bool HWPFile::StyleRead(void)
229 return _hwpStyle.Read(*this);
233 bool HWPFile::ParaListRead(void)
235 return ReadParaList(plist);
238 bool HWPFile::ReadParaList(std::list < HWPPara* > &aplist, unsigned char flag)
240 HWPPara *spNode = new HWPPara;
241 unsigned char tmp_etcflag;
242 unsigned char prev_etcflag = 0;
243 while (spNode->Read(*this, flag))
245 if( !(spNode->etcflag & 0x04) ){
246 tmp_etcflag = spNode->etcflag;
247 spNode->etcflag = prev_etcflag;
248 prev_etcflag = tmp_etcflag;
250 if (spNode->nch && spNode->reuse_shape)
252 if (!aplist.empty()){
253 spNode->pshape = aplist.back()->pshape;
255 else{
256 spNode->nch = 0;
257 spNode->reuse_shape = 0;
260 spNode->pshape.pagebreak = spNode->etcflag;
261 if( spNode->nch )
262 AddParaShape( &spNode->pshape );
264 if (!aplist.empty())
265 aplist.back()->SetNext(spNode);
266 aplist.push_back(spNode);
267 spNode = new HWPPara;
269 delete spNode;
271 return true;
275 bool HWPFile::TagsRead(void)
277 ulong tag;
278 long size;
280 while (1)
282 tag = Read4b();
283 size = Read4b();
284 if (size <= 0 && tag > 0){
285 continue;
288 if (tag == FILETAG_END_OF_COMPRESSED ||
289 tag == FILETAG_END_OF_UNCOMPRESSED)
290 return true;
291 switch (tag)
293 case FILETAG_EMBEDDED_PICTURE:
295 EmPicture *emb = new EmPicture(size);
297 if (true == emb->Read(*this))
298 emblist.push_back(emb);
299 else
300 delete emb;
302 break;
303 case FILETAG_OLE_OBJECT:
304 if (oledata)
305 delete oledata;
306 oledata = new OlePicture(size);
307 oledata->Read(*this);
308 break;
309 case FILETAG_HYPERTEXT:
311 if( (size % 617) != 0 )
312 SkipBlock( size );
313 else
314 for( int i = 0 ; i < size/617 ; i++)
316 HyperText *hypert = new HyperText;
317 hypert->Read(*this);
318 hyperlist.push_back(hypert);
320 break;
322 case 6:
324 ReadBlock(_hwpInfo.back_info.reserved1, 8);
325 _hwpInfo.back_info.luminance = Read4b();
326 _hwpInfo.back_info.contrast = Read4b();
327 _hwpInfo.back_info.effect = sal::static_int_cast<char>(Read1b());
328 ReadBlock(_hwpInfo.back_info.reserved2, 7);
329 ReadBlock(_hwpInfo.back_info.filename, 260);
330 ReadBlock(_hwpInfo.back_info.color, 3);
331 unsigned short nFlag = sal::static_int_cast<unsigned short>(Read2b());
332 _hwpInfo.back_info.flag = nFlag >> 8 ;
333 int nRange = Read4b();
334 _hwpInfo.back_info.range = nRange >> 24;
335 ReadBlock(_hwpInfo.back_info.reserved3, 27);
336 _hwpInfo.back_info.size = Read4b();
338 _hwpInfo.back_info.data = new char[(unsigned int)_hwpInfo.back_info.size];
339 ReadBlock(_hwpInfo.back_info.data, _hwpInfo.back_info.size);
341 if( _hwpInfo.back_info.size > 0 )
342 _hwpInfo.back_info.type = 2;
343 else if( _hwpInfo.back_info.filename[0] )
344 _hwpInfo.back_info.type = 1;
345 else
346 _hwpInfo.back_info.type = 0;
349 _hwpInfo.back_info.isset = true;
351 break;
353 case FILETAG_PRESENTATION:
354 case FILETAG_PREVIEW_IMAGE:
355 case FILETAG_PREVIEW_TEXT:
356 default:
357 SkipBlock(size);
363 ColumnDef *HWPFile::GetColumnDef(int num)
365 std::list<ColumnInfo*>::iterator it = columnlist.begin();
367 for(int i = 0; it != columnlist.end() ; ++it, i++){
368 if( i == num )
369 break;
372 if( it != columnlist.end() )
373 return (*it)->coldef;
374 else
375 return 0;
377 /* @return À妽º´Â 1ºÎÅÍ ½ÃÀÛÇÑ´Ù. */
378 int HWPFile::GetPageMasterNum(int page)
380 std::list<ColumnInfo*>::iterator it = columnlist.begin();
381 ColumnInfo *now = 0;
382 int i;
384 for( i = 1 ; it != columnlist.end() ; ++it, i++){
385 now = *it;
386 if( page < now->start_page )
387 return i-1;
389 return i-1;
392 HyperText *HWPFile::GetHyperText()
394 std::list<HyperText*>::iterator it = hyperlist.begin();
396 for( int i = 0; it != hyperlist.end(); ++it, i++ ){
397 if( i == currenthyper )
398 break;
401 currenthyper++;
402 return *it;
405 EmPicture *HWPFile::GetEmPicture(Picture * pic)
407 char *name = pic->picinfo.picembed.embname;
409 name[0] = 'H';
410 name[1] = 'W';
411 name[2] = 'P';
413 std::list < EmPicture* >::iterator it = emblist.begin();
414 for (; it != emblist.end(); ++it)
415 if (strcmp(name, (*it)->name) == 0)
416 return *it;
417 return 0;
420 EmPicture *HWPFile::GetEmPictureByName(char * name)
422 name[0] = 'H';
423 name[1] = 'W';
424 name[2] = 'P';
426 std::list < EmPicture* >::iterator it = emblist.begin();
427 for (; it != emblist.end(); ++it)
428 if (strcmp(name, (*it)->name) == 0)
429 return *it;
430 return 0;
434 void HWPFile::AddBox(FBox * box)
436 // LATER if we don't use box->next(),
437 // AddBox() and GetBoxHead() are useless;
438 if (!blist.empty())
440 box->prev = blist.back();
441 box->prev->next = box;
443 else
444 box->prev = 0;
445 blist.push_back(box);
449 ParaShape *HWPFile::getParaShape(int index)
451 std::list<ParaShape*>::iterator it = pslist.begin();
453 for( int i = 0; it != pslist.end(); ++it, i++ ){
454 if( i == index )
455 break;
458 return *it;
462 CharShape *HWPFile::getCharShape(int index)
464 std::list<CharShape*>::iterator it = cslist.begin();
466 for( int i = 0; it != cslist.end(); ++it, i++ ){
467 if( i == index )
468 break;
471 return *it;
475 FBoxStyle *HWPFile::getFBoxStyle(int index)
477 std::list<FBoxStyle*>::iterator it = fbslist.begin();
479 for( int i = 0; it != fbslist.end(); ++it, i++ ){
480 if( i == index )
481 break;
484 return *it;
487 DateCode *HWPFile::getDateCode(int index)
489 std::list<DateCode*>::iterator it = datecodes.begin();
491 for( int i = 0; it != datecodes.end(); ++it, i++ ){
492 if( i == index )
493 break;
496 return *it;
499 HeaderFooter *HWPFile::getHeaderFooter(int index)
501 std::list<HeaderFooter*>::iterator it = headerfooters.begin();
503 for( int i = 0; it != headerfooters.end(); ++it, i++ ){
504 if( i == index )
505 break;
508 return *it;
511 ShowPageNum *HWPFile::getPageNumber(int index)
513 std::list<ShowPageNum*>::iterator it = pagenumbers.begin();
515 for( int i = 0; it != pagenumbers.end(); ++it, i++ ){
516 if( i == index )
517 break;
520 return *it;
524 Table *HWPFile::getTable(int index)
526 std::list<Table*>::iterator it = tables.begin();
528 for( int i = 0; it != tables.end(); ++it, i++ ){
529 if( i == index )
530 break;
533 return *it;
536 void HWPFile::AddParaShape(ParaShape * pshape)
538 int nscount = 0;
539 for(int j = 0 ; j < MAXTABS-1 ; j++)
541 if( j > 0 && pshape->tabs[j].position == 0 )
542 break;
543 if( pshape->tabs[0].position == 0 ){
544 if( pshape->tabs[j].type || pshape->tabs[j].dot_continue ||
545 (pshape->tabs[j].position != 1000 *j) )
546 nscount = j;
548 else{
549 if( pshape->tabs[j].type || pshape->tabs[j].dot_continue ||
550 (pshape->tabs[j].position != 1000 * (j + 1)) )
551 nscount = j;
554 if( nscount )
555 pshape->tabs[MAXTABS-1].type = sal::static_int_cast<char>(nscount);
556 int value = compareParaShape(pshape);
557 if( value == 0 || nscount )
559 pshape->index = ++pcount;
560 pslist.push_back(pshape);
562 else
563 pshape->index = value;
567 void HWPFile::AddCharShape(CharShape * cshape)
569 int value = compareCharShape(cshape);
570 if( value == 0 )
572 cshape->index = ++ccount;
573 cslist.push_back(cshape);
575 else
576 cshape->index = value;
579 void HWPFile::AddColumnInfo()
581 ColumnInfo *cinfo = new ColumnInfo(m_nCurrentPage);
582 columnlist.push_back(cinfo);
583 setMaxSettedPage();
586 void HWPFile::SetColumnDef(ColumnDef *coldef)
588 ColumnInfo *cinfo = columnlist.back();
589 if( cinfo->bIsSet )
590 return;
591 cinfo->coldef = coldef;
592 cinfo->bIsSet = true;
595 void HWPFile::AddDateFormat(DateCode * hbox)
597 hbox->key = sal::static_int_cast<char>(++datecodecount);
598 datecodes.push_back(hbox);
601 void HWPFile::AddPageNumber(ShowPageNum * hbox)
603 pagenumbers.push_back(hbox);
606 void HWPFile::AddHeaderFooter(HeaderFooter * hbox)
608 headerfooters.push_back(hbox);
611 void HWPFile::AddTable(Table * hbox)
613 tables.push_back(hbox);
616 void HWPFile::AddFBoxStyle(FBoxStyle * fbstyle)
618 fbslist.push_back(fbstyle);
621 int HWPFile::compareCharShape(CharShape *shape)
623 int count = cslist.size();
624 if( count > 0 )
626 CharShape *cshape=0;
627 for(int i = 0; i< count; i++)
629 cshape = getCharShape(i);
631 if( shape->size == cshape->size &&
632 shape->font[0] == cshape->font[0] &&
633 shape->ratio[0] == cshape->ratio[0] &&
634 shape->space[0] == cshape->space[0] &&
635 shape->color[1] == cshape->color[1] &&
636 shape->color[0] == cshape->color[0] &&
637 shape->shade == cshape->shade &&
638 shape->attr == cshape->attr )
640 return cshape->index;
644 return 0;
648 int HWPFile::compareParaShape(ParaShape *shape)
650 int count = pslist.size();
651 if( count > 0 )
653 ParaShape *pshape=0;
654 for(int i = 0; i< count; i++)
656 pshape = getParaShape(i);
657 if( shape->left_margin == pshape->left_margin &&
658 shape->right_margin == pshape->right_margin &&
659 shape->pspacing_prev == pshape->pspacing_prev &&
660 shape->pspacing_next == pshape->pspacing_next &&
661 shape->indent == pshape->indent &&
662 shape->lspacing == pshape->lspacing &&
663 shape->arrange_type == pshape->arrange_type &&
664 shape->outline == pshape->outline &&
665 shape->pagebreak == pshape->pagebreak)
667 if( shape->cshape->size == pshape->cshape->size &&
668 shape->cshape->font[0] == pshape->cshape->font[0] &&
669 shape->cshape->ratio[0] == pshape->cshape->ratio[0] &&
670 shape->cshape->space[0] == pshape->cshape->space[0] &&
671 shape->cshape->color[1] == pshape->cshape->color[1] &&
672 shape->cshape->color[0] == pshape->cshape->color[0] &&
673 shape->cshape->shade == pshape->cshape->shade &&
674 shape->cshape->attr == pshape->cshape->attr )
676 return pshape->index;
681 return 0;
685 HWPFile *GetCurrentDoc(void)
687 return HWPFile::cur_doc;
691 HWPFile *SetCurrentDoc(HWPFile * hwpfp)
693 HWPFile *org = HWPFile::cur_doc;
695 HWPFile::cur_doc = hwpfp;
696 return org;
699 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */