1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: hwpfile.cpp,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "precompile.h"
49 #define HWPHeadLen 128
50 #define HWPSummaryLen 1008
52 #define V20SIGNATURE "HWP Document File V2.00 \032\1\2\3\4\5"
53 #define V21SIGNATURE "HWP Document File V2.10 \032\1\2\3\4\5"
54 #define V30SIGNATURE "HWP Document File V3.00 \032\1\2\3\4\5"
56 #define FILESTG_SIGNATURE 0xF8995567
57 #define FILESTG_SIGNATURE_NORMAL 0xF8995568
59 HWPFile
*HWPFile::cur_doc
= 0;
60 static int ccount
= 0;
61 static int pcount
= 0;
62 static int datecodecount
= 0;
64 HWPFile::HWPFile(void)
71 * TODO : Ãß°¡µÈ ½ºÅ¸Àϸ®½ºÆ®¿¡ ´ëÇÑ ¸Þ¸ð¸® ÇØÁ¦
73 HWPFile::~HWPFile(void)
78 LinkedListIterator
< ColumnInfo
> it_column(&columnlist
);
79 for (; it_column
.current(); it_column
++)
80 delete it_column
.current();
82 LinkedListIterator
< HWPPara
> it(&plist
);
83 for (; it
.current(); it
++)
86 LinkedListIterator
< Table
> tbl(&tables
);
87 for (; tbl
.current(); tbl
++)
90 LinkedListIterator
< HyperText
> hyp(&hyperlist
);
91 for (; hyp
.current(); hyp
++)
98 void HWPFile::Init(void)
105 error_code
= HWP_NoError
;
111 m_nMaxSettedPage
= 0;
115 int HWPFile::ReadHwpFile(HStream
& stream
)
117 if (Open(stream
) != HWP_NoError
)
119 // printf("HWPFile::ReadHwpFile\n");
121 // printf("HWPFile::InfoRead Done.\n");
123 // printf("HWPFile::FontRead Done.\n");
125 // printf("HWPFile::StyleRead Done.\n");
128 // printf("HWPFile::ParaListRead Done.\n");
130 //printf("HWPFile::TagsRead Done. State: %d\n", State());
136 static int hwp_version(char *str
)
138 if (memcmp(V20SIGNATURE
, str
, HWPIDLen
) == 0)
140 else if (memcmp(V21SIGNATURE
, str
, HWPIDLen
) == 0)
142 else if (memcmp(V30SIGNATURE
, str
, HWPIDLen
) == 0)
150 int HWPFile::Open(HStream
& stream
)
152 HStreamIODev
*hstreamio
;
154 if (0 == (hstreamio
= new HStreamIODev(stream
)))
156 printf(" hstreamio is not instanciate \n");
157 return SetState(errno
);
159 if (!hstreamio
->open())
163 return SetState(HWP_EMPTY_FILE
);
165 SetIODevice(hstreamio
);
167 char idstr
[HWPIDLen
];
169 if (ReadBlock(idstr
, HWPIDLen
) <= 0
170 || HWP_V30
!= (version
= hwp_version(idstr
)))
172 return SetState(HWP_UNSUPPORTED_VERSION
);
178 int HWPFile::State(void) const
184 int HWPFile::SetState(int errcode
)
186 error_code
= errcode
;
191 int HWPFile::Read1b(void)
193 return hiodev
? hiodev
->read1b() : -1;
197 int HWPFile::Read2b(void)
199 return hiodev
? hiodev
->read2b() : -1;
203 long HWPFile::Read4b(void)
205 return hiodev
? hiodev
->read4b() : -1;
209 int HWPFile::Read1b(void *ptr
, size_t nmemb
)
211 return hiodev
? hiodev
->read1b(ptr
, nmemb
) : 0;
215 int HWPFile::Read2b(void *ptr
, size_t nmemb
)
217 return hiodev
? hiodev
->read2b(ptr
, nmemb
) : 0;
221 int HWPFile::Read4b(void *ptr
, size_t nmemb
)
223 return hiodev
? hiodev
->read4b(ptr
, nmemb
) : 0;
227 size_t HWPFile::ReadBlock(void *ptr
, size_t size
)
229 return hiodev
? hiodev
->readBlock(ptr
, size
) : 0;
233 size_t HWPFile::SkipBlock(size_t size
)
235 return hiodev
? hiodev
->skipBlock(size
) : 0;
239 bool HWPFile::SetCompressed(bool flag
)
241 return hiodev
? hiodev
->setCompressed(flag
) : false;
245 HIODev
*HWPFile::SetIODevice(HIODev
* new_hiodev
)
247 HIODev
*old_hiodev
= hiodev
;
254 // end of HIODev wrapper
256 bool HWPFile::InfoRead(void)
258 return _hwpInfo
.Read(*this);
262 bool HWPFile::FontRead(void)
264 return _hwpFont
.Read(*this);
268 bool HWPFile::StyleRead(void)
270 return _hwpStyle
.Read(*this);
274 bool HWPFile::ParaListRead(void)
276 return ReadParaList(plist
);
279 bool HWPFile::ReadParaList(LinkedList
< HWPPara
> &aplist
, unsigned char flag
)
281 LinkedListIterator
< HWPPara
> it(&aplist
);
283 HWPPara
*spNode
= new HWPPara
;
284 unsigned char tmp_etcflag
;
285 unsigned char prev_etcflag
= 0;
286 while (spNode
->Read(*this, flag
))
288 if( !(spNode
->etcflag
& 0x04) ){
289 tmp_etcflag
= spNode
->etcflag
;
290 spNode
->etcflag
= prev_etcflag
;
291 prev_etcflag
= tmp_etcflag
;
293 if (spNode
->nch
&& spNode
->reuse_shape
)
296 spNode
->pshape
= aplist
.last()->pshape
;
300 spNode
->reuse_shape
= 0;
303 spNode
->pshape
.pagebreak
= spNode
->etcflag
;
305 AddParaShape( &spNode
->pshape
);
308 aplist
.last()->SetNext(spNode
);
309 aplist
.insert(spNode
, -1);
310 spNode
= new HWPPara
;
318 bool HWPFile::TagsRead(void)
327 if (size
<= 0 && tag
> 0){
332 if (tag
== FILETAG_END_OF_COMPRESSED
||
333 tag
== FILETAG_END_OF_UNCOMPRESSED
)
337 case FILETAG_EMBEDDED_PICTURE
:
339 EmPicture
*emb
= new EmPicture(size
);
341 if (true == emb
->Read(*this))
342 emblist
.insert(emb
, -1);
347 case FILETAG_OLE_OBJECT
:
348 oledata
= new OlePicture(size
);
349 oledata
->Read(*this);
351 case FILETAG_HYPERTEXT
:
353 if( (size
% 617) != 0 )
356 for( int i
= 0 ; i
< size
/617 ; i
++)
358 HyperText
*hypert
= new HyperText
;
360 hyperlist
.insert(hypert
, -1);
366 ReadBlock(_hwpInfo
.back_info
.reserved1
, 8);
367 _hwpInfo
.back_info
.luminance
= Read4b();
368 _hwpInfo
.back_info
.contrast
= Read4b();
369 _hwpInfo
.back_info
.effect
= sal::static_int_cast
<char>(Read1b());
370 ReadBlock(_hwpInfo
.back_info
.reserved2
, 7);
371 ReadBlock(_hwpInfo
.back_info
.filename
, 260);
372 ReadBlock(_hwpInfo
.back_info
.color
, 3);
373 unsigned short nFlag
= sal::static_int_cast
<unsigned short>(Read2b());
374 _hwpInfo
.back_info
.flag
= nFlag
>> 8 ;
375 int nRange
= Read4b();
376 _hwpInfo
.back_info
.range
= nRange
>> 24;
377 ReadBlock(_hwpInfo
.back_info
.reserved3
, 27);
378 _hwpInfo
.back_info
.size
= Read4b();
380 _hwpInfo
.back_info
.data
= new char[(unsigned int)_hwpInfo
.back_info
.size
];
381 ReadBlock(_hwpInfo
.back_info
.data
, _hwpInfo
.back_info
.size
);
383 if( _hwpInfo
.back_info
.size
> 0 )
384 _hwpInfo
.back_info
.type
= 2;
385 else if( _hwpInfo
.back_info
.filename
[0] )
386 _hwpInfo
.back_info
.type
= 1;
388 _hwpInfo
.back_info
.type
= 0;
391 _hwpInfo
.back_info
.isset
= true;
395 case FILETAG_PRESENTATION
:
396 case FILETAG_PREVIEW_IMAGE
:
397 case FILETAG_PREVIEW_TEXT
:
406 ColumnDef
*HWPFile::GetColumnDef(int num
)
408 ColumnInfo
*cinfo
= columnlist
.find(num
);
410 return cinfo
->coldef
;
414 /* @return À妽º´Â 1ºÎÅÍ ½ÃÀÛÇÑ´Ù. */
415 int HWPFile::GetPageMasterNum(int page
)
417 LinkedListIterator
<ColumnInfo
> it(&columnlist
);
419 //ColumnInfo *prev = 0;
423 for( i
= 1 ; it
.current() ; it
++, i
++){
425 if( page
< now
->start_page
)
431 HyperText
*HWPFile::GetHyperText()
433 return hyperlist
.find(currenthyper
++);
436 EmPicture
*HWPFile::GetEmPicture(Picture
* pic
)
438 char *name
= pic
->picinfo
.picembed
.embname
;
444 LinkedListIterator
< EmPicture
> it(&emblist
);
445 for (; it
.current(); it
++)
446 if (strcmp(name
, it
.current()->name
) == 0)
451 EmPicture
*HWPFile::GetEmPictureByName(char * name
)
457 LinkedListIterator
< EmPicture
> it(&emblist
);
458 for (; it
.current(); it
++)
459 if (strcmp(name
, it
.current()->name
) == 0)
465 void HWPFile::AddBox(FBox
* box
)
467 // LATER if we don't use box->next(),
468 // AddBox() and GetBoxHead() are useless;
471 box
->prev
= blist
.last();
472 box
->prev
->next
= box
;
476 blist
.insert(box
, -1);
480 ParaShape
*HWPFile::getParaShape(int index
)
482 return pslist
.find(index
);
486 CharShape
*HWPFile::getCharShape(int index
)
488 return cslist
.find(index
);
492 FBoxStyle
*HWPFile::getFBoxStyle(int index
)
494 return fbslist
.find(index
);
497 DateCode
*HWPFile::getDateCode(int index
)
499 return datecodes
.find(index
);
502 HeaderFooter
*HWPFile::getHeaderFooter(int index
)
504 return headerfooters
.find(index
);
507 ShowPageNum
*HWPFile::getPageNumber(int index
)
509 return pagenumbers
.find(index
);
512 Table
*HWPFile::getTable(int index
)
514 return tables
.find(index
);
517 void HWPFile::AddParaShape(ParaShape
* pshape
)
520 for(int j
= 0 ; j
< MAXTABS
-1 ; j
++)
522 if( j
> 0 && pshape
->tabs
[j
].position
== 0 )
524 if( pshape
->tabs
[0].position
== 0 ){
525 if( pshape
->tabs
[j
].type
|| pshape
->tabs
[j
].dot_continue
||
526 (pshape
->tabs
[j
].position
!= 1000 *j
) )
530 if( pshape
->tabs
[j
].type
|| pshape
->tabs
[j
].dot_continue
||
531 (pshape
->tabs
[j
].position
!= 1000 * (j
+ 1)) )
536 pshape
->tabs
[MAXTABS
-1].type
= sal::static_int_cast
<char>(nscount
);
537 int value
= compareParaShape(pshape
);
538 if( value
== 0 || nscount
)
540 pshape
->index
= ++pcount
;
541 pslist
.insert(pshape
, -1);
544 pshape
->index
= value
;
548 void HWPFile::AddCharShape(CharShape
* cshape
)
550 int value
= compareCharShape(cshape
);
553 cshape
->index
= ++ccount
;
554 cslist
.insert(cshape
, -1);
557 cshape
->index
= value
;
560 void HWPFile::AddColumnInfo()
562 ColumnInfo
*cinfo
= new ColumnInfo(m_nCurrentPage
);
563 columnlist
.insert(cinfo
, -1);
567 void HWPFile::SetColumnDef(ColumnDef
*coldef
)
569 ColumnInfo
*cinfo
= columnlist
.last();
572 cinfo
->coldef
= coldef
;
573 cinfo
->bIsSet
= true;
576 void HWPFile::AddDateFormat(DateCode
* hbox
)
578 hbox
->key
= sal::static_int_cast
<char>(++datecodecount
);
579 datecodes
.insert(hbox
, -1);
582 void HWPFile::AddPageNumber(ShowPageNum
* hbox
)
584 pagenumbers
.insert(hbox
, -1);
587 void HWPFile::AddHeaderFooter(HeaderFooter
* hbox
)
589 headerfooters
.insert(hbox
, -1);
592 void HWPFile::AddTable(Table
* hbox
)
594 tables
.insert(hbox
, -1);
597 void HWPFile::AddFBoxStyle(FBoxStyle
* fbstyle
)
599 fbslist
.insert(fbstyle
, -1);
602 int HWPFile::compareCharShape(CharShape
*shape
)
604 int count
= cslist
.count();
608 for(int i
= 0; i
< count
; i
++)
610 cshape
= cslist
.find(i
);
612 if( shape
->size
== cshape
->size
&&
613 shape
->font
[0] == cshape
->font
[0] &&
614 shape
->ratio
[0] == cshape
->ratio
[0] &&
615 shape
->space
[0] == cshape
->space
[0] &&
616 shape
->color
[1] == cshape
->color
[1] &&
617 shape
->color
[0] == cshape
->color
[0] &&
618 shape
->shade
== cshape
->shade
&&
619 shape
->attr
== cshape
->attr
)
621 return cshape
->index
;
629 int HWPFile::compareParaShape(ParaShape
*shape
)
631 int count
= pslist
.count();
635 for(int i
= 0; i
< count
; i
++)
637 pshape
= pslist
.find(i
);
638 if( shape
->left_margin
== pshape
->left_margin
&&
639 shape
->right_margin
== pshape
->right_margin
&&
640 shape
->pspacing_prev
== pshape
->pspacing_prev
&&
641 shape
->pspacing_next
== pshape
->pspacing_next
&&
642 shape
->indent
== pshape
->indent
&&
643 shape
->lspacing
== pshape
->lspacing
&&
644 shape
->arrange_type
== pshape
->arrange_type
&&
645 shape
->outline
== pshape
->outline
&&
646 shape
->pagebreak
== pshape
->pagebreak
)
648 if( shape
->cshape
->size
== pshape
->cshape
->size
&&
649 shape
->cshape
->font
[0] == pshape
->cshape
->font
[0] &&
650 shape
->cshape
->ratio
[0] == pshape
->cshape
->ratio
[0] &&
651 shape
->cshape
->space
[0] == pshape
->cshape
->space
[0] &&
652 shape
->cshape
->color
[1] == pshape
->cshape
->color
[1] &&
653 shape
->cshape
->color
[0] == pshape
->cshape
->color
[0] &&
654 shape
->cshape
->shade
== pshape
->cshape
->shade
&&
655 shape
->cshape
->attr
== pshape
->cshape
->attr
)
657 return pshape
->index
;
666 HWPFile
*GetCurrentDoc(void)
668 return HWPFile::cur_doc
;
672 HWPFile
*SetCurrentDoc(HWPFile
* hwpfp
)
674 HWPFile
*org
= HWPFile::cur_doc
;
676 HWPFile::cur_doc
= hwpfp
;