1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "precompile.h"
46 #include <osl/diagnose.h>
48 #define HWPHeadLen 128
49 #define HWPSummaryLen 1008
51 #define FILESTG_SIGNATURE 0xF8995567
52 #define FILESTG_SIGNATURE_NORMAL 0xF8995568
54 HWPFile
*HWPFile::cur_doc
= 0;
55 static int ccount
= 0;
56 static int pcount
= 0;
57 static int datecodecount
= 0;
65 , error_code(HWP_NoError
)
80 std::list
< ColumnInfo
* >::iterator it_column
= columnlist
.begin();
81 for (; it_column
!= columnlist
.end(); ++it_column
)
84 std::list
< HWPPara
* >::iterator it
= plist
.begin();
85 for (; it
!= plist
.end(); ++it
)
88 std::list
< Table
* >::iterator tbl
= tables
.begin();
89 for (; tbl
!= tables
.end(); ++tbl
)
92 std::list
< HyperText
* >::iterator hyp
= hyperlist
.begin();
93 for (; hyp
!= hyperlist
.end(); ++hyp
)
99 int HWPFile::ReadHwpFile(HStream
& stream
)
101 if (Open(stream
) != HWP_NoError
)
113 int detect_hwp_version(const char *str
)
115 if (memcmp(V20SIGNATURE
, str
, HWPIDLen
) == 0)
117 else if (memcmp(V21SIGNATURE
, str
, HWPIDLen
) == 0)
119 else if (memcmp(V30SIGNATURE
, str
, HWPIDLen
) == 0)
126 int HWPFile::Open(HStream
& stream
)
128 HStreamIODev
*hstreamio
= new HStreamIODev(stream
);
130 if (!hstreamio
->open())
134 return SetState(HWP_EMPTY_FILE
);
137 HIODev
*pPrev
= SetIODevice(hstreamio
);
140 char idstr
[HWPIDLen
];
142 if (ReadBlock(idstr
, HWPIDLen
) <= 0
143 || HWP_V30
!= (version
= detect_hwp_version(idstr
)))
145 return SetState(HWP_UNSUPPORTED_VERSION
);
151 int HWPFile::State(void) const
157 int HWPFile::SetState(int errcode
)
159 error_code
= errcode
;
164 int HWPFile::Read1b(void)
166 return hiodev
? hiodev
->read1b() : -1;
170 int HWPFile::Read2b(void)
172 return hiodev
? hiodev
->read2b() : -1;
176 long HWPFile::Read4b(void)
178 return hiodev
? hiodev
->read4b() : -1;
182 int HWPFile::Read1b(void *ptr
, size_t nmemb
)
184 return hiodev
? hiodev
->read1b(ptr
, nmemb
) : 0;
188 int HWPFile::Read2b(void *ptr
, size_t nmemb
)
190 return hiodev
? hiodev
->read2b(ptr
, nmemb
) : 0;
194 int HWPFile::Read4b(void *ptr
, size_t nmemb
)
196 return hiodev
? hiodev
->read4b(ptr
, nmemb
) : 0;
200 size_t HWPFile::ReadBlock(void *ptr
, size_t size
)
202 return hiodev
? hiodev
->readBlock(ptr
, size
) : 0;
206 size_t HWPFile::SkipBlock(size_t size
)
208 return hiodev
? hiodev
->skipBlock(size
) : 0;
212 bool HWPFile::SetCompressed(bool flag
)
214 return hiodev
? hiodev
->setCompressed(flag
) : false;
218 HIODev
*HWPFile::SetIODevice(HIODev
* new_hiodev
)
220 HIODev
*old_hiodev
= hiodev
;
228 // end of HIODev wrapper
230 bool HWPFile::InfoRead(void)
232 return _hwpInfo
.Read(*this);
236 bool HWPFile::FontRead(void)
238 return _hwpFont
.Read(*this);
242 bool HWPFile::StyleRead(void)
244 return _hwpStyle
.Read(*this);
248 bool HWPFile::ParaListRead(void)
250 return ReadParaList(plist
);
253 bool HWPFile::ReadParaList(std::list
< HWPPara
* > &aplist
, unsigned char flag
)
255 HWPPara
*spNode
= new HWPPara
;
256 unsigned char tmp_etcflag
;
257 unsigned char prev_etcflag
= 0;
258 while (spNode
->Read(*this, flag
))
260 if( !(spNode
->etcflag
& 0x04) ){
261 tmp_etcflag
= spNode
->etcflag
;
262 spNode
->etcflag
= prev_etcflag
;
263 prev_etcflag
= tmp_etcflag
;
265 if (spNode
->nch
&& spNode
->reuse_shape
)
268 spNode
->pshape
= aplist
.back()->pshape
;
272 spNode
->reuse_shape
= 0;
275 spNode
->pshape
.pagebreak
= spNode
->etcflag
;
277 AddParaShape( &spNode
->pshape
);
280 aplist
.back()->SetNext(spNode
);
281 aplist
.push_back(spNode
);
282 spNode
= new HWPPara
;
290 bool HWPFile::TagsRead(void)
299 if (size
<= 0 && tag
> 0){
303 if (tag
== FILETAG_END_OF_COMPRESSED
||
304 tag
== FILETAG_END_OF_UNCOMPRESSED
)
308 case FILETAG_EMBEDDED_PICTURE
:
310 EmPicture
*emb
= new EmPicture(size
);
312 if (true == emb
->Read(*this))
313 emblist
.push_back(emb
);
318 case FILETAG_OLE_OBJECT
:
321 oledata
= new OlePicture(size
);
322 oledata
->Read(*this);
324 case FILETAG_HYPERTEXT
:
326 if( (size
% 617) != 0 )
329 for( int i
= 0 ; i
< size
/617 ; i
++)
331 HyperText
*hypert
= new HyperText
;
333 hyperlist
.push_back(hypert
);
339 ReadBlock(_hwpInfo
.back_info
.reserved1
, 8);
340 _hwpInfo
.back_info
.luminance
= Read4b();
341 _hwpInfo
.back_info
.contrast
= Read4b();
342 _hwpInfo
.back_info
.effect
= sal::static_int_cast
<char>(Read1b());
343 ReadBlock(_hwpInfo
.back_info
.reserved2
, 7);
344 ReadBlock(_hwpInfo
.back_info
.filename
, 260);
345 ReadBlock(_hwpInfo
.back_info
.color
, 3);
346 unsigned short nFlag
= sal::static_int_cast
<unsigned short>(Read2b());
347 _hwpInfo
.back_info
.flag
= nFlag
>> 8 ;
348 int nRange
= Read4b();
349 _hwpInfo
.back_info
.range
= nRange
>> 24;
350 ReadBlock(_hwpInfo
.back_info
.reserved3
, 27);
351 _hwpInfo
.back_info
.size
= Read4b();
353 _hwpInfo
.back_info
.data
= new char[(unsigned int)_hwpInfo
.back_info
.size
];
354 ReadBlock(_hwpInfo
.back_info
.data
, _hwpInfo
.back_info
.size
);
356 if( _hwpInfo
.back_info
.size
> 0 )
357 _hwpInfo
.back_info
.type
= 2;
358 else if( _hwpInfo
.back_info
.filename
[0] )
359 _hwpInfo
.back_info
.type
= 1;
361 _hwpInfo
.back_info
.type
= 0;
364 _hwpInfo
.back_info
.isset
= true;
368 case FILETAG_PRESENTATION
:
369 case FILETAG_PREVIEW_IMAGE
:
370 case FILETAG_PREVIEW_TEXT
:
378 ColumnDef
*HWPFile::GetColumnDef(int num
)
380 std::list
<ColumnInfo
*>::iterator it
= columnlist
.begin();
382 for(int i
= 0; it
!= columnlist
.end() ; ++it
, i
++){
387 if( it
!= columnlist
.end() )
388 return (*it
)->coldef
;
392 /* @return À妽º´Â 1ºÎÅÍ ½ÃÀÛÇÑ´Ù. */
393 int HWPFile::GetPageMasterNum(int page
)
395 std::list
<ColumnInfo
*>::iterator it
= columnlist
.begin();
399 for( i
= 1 ; it
!= columnlist
.end() ; ++it
, i
++){
401 if( page
< now
->start_page
)
407 HyperText
*HWPFile::GetHyperText()
409 std::list
<HyperText
*>::iterator it
= hyperlist
.begin();
411 for( int i
= 0; it
!= hyperlist
.end(); ++it
, i
++ ){
412 if( i
== currenthyper
)
420 EmPicture
*HWPFile::GetEmPicture(Picture
* pic
)
422 char *name
= pic
->picinfo
.picembed
.embname
;
428 std::list
< EmPicture
* >::iterator it
= emblist
.begin();
429 for (; it
!= emblist
.end(); ++it
)
430 if (strcmp(name
, (*it
)->name
) == 0)
435 EmPicture
*HWPFile::GetEmPictureByName(char * name
)
441 std::list
< EmPicture
* >::iterator it
= emblist
.begin();
442 for (; it
!= emblist
.end(); ++it
)
443 if (strcmp(name
, (*it
)->name
) == 0)
449 void HWPFile::AddBox(FBox
* box
)
451 // LATER if we don't use box->next(),
452 // AddBox() and GetBoxHead() are useless;
455 box
->prev
= blist
.back();
456 box
->prev
->next
= box
;
460 blist
.push_back(box
);
464 ParaShape
*HWPFile::getParaShape(int index
)
466 std::list
<ParaShape
*>::iterator it
= pslist
.begin();
468 for( int i
= 0; it
!= pslist
.end(); ++it
, i
++ ){
477 CharShape
*HWPFile::getCharShape(int index
)
479 std::list
<CharShape
*>::iterator it
= cslist
.begin();
481 for( int i
= 0; it
!= cslist
.end(); ++it
, i
++ ){
490 FBoxStyle
*HWPFile::getFBoxStyle(int index
)
492 std::list
<FBoxStyle
*>::iterator it
= fbslist
.begin();
494 for( int i
= 0; it
!= fbslist
.end(); ++it
, i
++ ){
502 DateCode
*HWPFile::getDateCode(int index
)
504 std::list
<DateCode
*>::iterator it
= datecodes
.begin();
506 for( int i
= 0; it
!= datecodes
.end(); ++it
, i
++ ){
514 HeaderFooter
*HWPFile::getHeaderFooter(int index
)
516 std::list
<HeaderFooter
*>::iterator it
= headerfooters
.begin();
518 for( int i
= 0; it
!= headerfooters
.end(); ++it
, i
++ ){
526 ShowPageNum
*HWPFile::getPageNumber(int index
)
528 std::list
<ShowPageNum
*>::iterator it
= pagenumbers
.begin();
530 for( int i
= 0; it
!= pagenumbers
.end(); ++it
, i
++ ){
539 Table
*HWPFile::getTable(int index
)
541 std::list
<Table
*>::iterator it
= tables
.begin();
543 for( int i
= 0; it
!= tables
.end(); ++it
, i
++ ){
551 void HWPFile::AddParaShape(ParaShape
* pshape
)
554 for(int j
= 0 ; j
< MAXTABS
-1 ; j
++)
556 if( j
> 0 && pshape
->tabs
[j
].position
== 0 )
558 if( pshape
->tabs
[0].position
== 0 ){
559 if( pshape
->tabs
[j
].type
|| pshape
->tabs
[j
].dot_continue
||
560 (pshape
->tabs
[j
].position
!= 1000 *j
) )
564 if( pshape
->tabs
[j
].type
|| pshape
->tabs
[j
].dot_continue
||
565 (pshape
->tabs
[j
].position
!= 1000 * (j
+ 1)) )
570 pshape
->tabs
[MAXTABS
-1].type
= sal::static_int_cast
<char>(nscount
);
571 int value
= compareParaShape(pshape
);
572 if( value
== 0 || nscount
)
574 pshape
->index
= ++pcount
;
575 pslist
.push_back(pshape
);
578 pshape
->index
= value
;
582 void HWPFile::AddCharShape(CharShape
* cshape
)
584 int value
= compareCharShape(cshape
);
587 cshape
->index
= ++ccount
;
588 cslist
.push_back(cshape
);
591 cshape
->index
= value
;
594 void HWPFile::AddColumnInfo()
596 ColumnInfo
*cinfo
= new ColumnInfo(m_nCurrentPage
);
597 columnlist
.push_back(cinfo
);
601 void HWPFile::SetColumnDef(ColumnDef
*coldef
)
603 ColumnInfo
*cinfo
= columnlist
.back();
606 cinfo
->coldef
= coldef
;
607 cinfo
->bIsSet
= true;
610 void HWPFile::AddDateFormat(DateCode
* hbox
)
612 hbox
->key
= sal::static_int_cast
<char>(++datecodecount
);
613 datecodes
.push_back(hbox
);
616 void HWPFile::AddPageNumber(ShowPageNum
* hbox
)
618 pagenumbers
.push_back(hbox
);
621 void HWPFile::AddHeaderFooter(HeaderFooter
* hbox
)
623 headerfooters
.push_back(hbox
);
626 void HWPFile::AddTable(Table
* hbox
)
628 tables
.push_back(hbox
);
631 void HWPFile::AddFBoxStyle(FBoxStyle
* fbstyle
)
633 fbslist
.push_back(fbstyle
);
636 int HWPFile::compareCharShape(CharShape
*shape
)
638 int count
= cslist
.size();
642 for(int i
= 0; i
< count
; i
++)
644 cshape
= getCharShape(i
);
646 if( shape
->size
== cshape
->size
&&
647 shape
->font
[0] == cshape
->font
[0] &&
648 shape
->ratio
[0] == cshape
->ratio
[0] &&
649 shape
->space
[0] == cshape
->space
[0] &&
650 shape
->color
[1] == cshape
->color
[1] &&
651 shape
->color
[0] == cshape
->color
[0] &&
652 shape
->shade
== cshape
->shade
&&
653 shape
->attr
== cshape
->attr
)
655 return cshape
->index
;
663 int HWPFile::compareParaShape(ParaShape
*shape
)
665 int count
= pslist
.size();
669 for(int i
= 0; i
< count
; i
++)
671 pshape
= getParaShape(i
);
672 if( shape
->left_margin
== pshape
->left_margin
&&
673 shape
->right_margin
== pshape
->right_margin
&&
674 shape
->pspacing_prev
== pshape
->pspacing_prev
&&
675 shape
->pspacing_next
== pshape
->pspacing_next
&&
676 shape
->indent
== pshape
->indent
&&
677 shape
->lspacing
== pshape
->lspacing
&&
678 shape
->arrange_type
== pshape
->arrange_type
&&
679 shape
->outline
== pshape
->outline
&&
680 shape
->pagebreak
== pshape
->pagebreak
)
682 if( shape
->cshape
->size
== pshape
->cshape
->size
&&
683 shape
->cshape
->font
[0] == pshape
->cshape
->font
[0] &&
684 shape
->cshape
->ratio
[0] == pshape
->cshape
->ratio
[0] &&
685 shape
->cshape
->space
[0] == pshape
->cshape
->space
[0] &&
686 shape
->cshape
->color
[1] == pshape
->cshape
->color
[1] &&
687 shape
->cshape
->color
[0] == pshape
->cshape
->color
[0] &&
688 shape
->cshape
->shade
== pshape
->cshape
->shade
&&
689 shape
->cshape
->attr
== pshape
->cshape
->attr
)
691 return pshape
->index
;
700 HWPFile
*GetCurrentDoc(void)
702 return HWPFile::cur_doc
;
706 HWPFile
*SetCurrentDoc(HWPFile
* hwpfp
)
708 HWPFile
*org
= HWPFile::cur_doc
;
710 HWPFile::cur_doc
= hwpfp
;
714 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */