1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
37 #include <osl/diagnose.h>
39 #define HWPHeadLen 128
40 #define HWPSummaryLen 1008
42 #define FILESTG_SIGNATURE 0xF8995567
43 #define FILESTG_SIGNATURE_NORMAL 0xF8995568
45 HWPFile
*HWPFile::cur_doc
= 0;
46 static int ccount
= 0;
47 static int pcount
= 0;
48 static int datecodecount
= 0;
56 , error_code(HWP_NoError
)
71 std::list
< ColumnInfo
* >::iterator it_column
= columnlist
.begin();
72 for (; it_column
!= columnlist
.end(); ++it_column
)
75 std::list
< HWPPara
* >::iterator it
= plist
.begin();
76 for (; it
!= plist
.end(); ++it
)
79 std::list
< Table
* >::iterator tbl
= tables
.begin();
80 for (; tbl
!= tables
.end(); ++tbl
)
83 std::list
< HyperText
* >::iterator hyp
= hyperlist
.begin();
84 for (; hyp
!= hyperlist
.end(); ++hyp
)
90 int HWPFile::ReadHwpFile(HStream
& stream
)
92 if (Open(stream
) != HWP_NoError
)
104 int detect_hwp_version(const char *str
)
106 if (memcmp(V20SIGNATURE
, str
, HWPIDLen
) == 0)
108 else if (memcmp(V21SIGNATURE
, str
, HWPIDLen
) == 0)
110 else if (memcmp(V30SIGNATURE
, str
, HWPIDLen
) == 0)
117 int HWPFile::Open(HStream
& stream
)
119 HStreamIODev
*hstreamio
= new HStreamIODev(stream
);
121 if (!hstreamio
->open())
125 return SetState(HWP_EMPTY_FILE
);
128 HIODev
*pPrev
= SetIODevice(hstreamio
);
131 char idstr
[HWPIDLen
];
133 if (ReadBlock(idstr
, HWPIDLen
) <= 0
134 || HWP_V30
!= (version
= detect_hwp_version(idstr
)))
136 return SetState(HWP_UNSUPPORTED_VERSION
);
142 int HWPFile::State(void) const
148 int HWPFile::SetState(int errcode
)
150 error_code
= errcode
;
155 int HWPFile::Read1b(void)
157 return hiodev
? hiodev
->read1b() : -1;
161 int HWPFile::Read2b(void)
163 return hiodev
? hiodev
->read2b() : -1;
167 long HWPFile::Read4b(void)
169 return hiodev
? hiodev
->read4b() : -1;
173 int HWPFile::Read1b(void *ptr
, size_t nmemb
)
175 return hiodev
? hiodev
->read1b(ptr
, nmemb
) : 0;
179 int HWPFile::Read2b(void *ptr
, size_t nmemb
)
181 return hiodev
? hiodev
->read2b(ptr
, nmemb
) : 0;
185 int HWPFile::Read4b(void *ptr
, size_t nmemb
)
187 return hiodev
? hiodev
->read4b(ptr
, nmemb
) : 0;
191 size_t HWPFile::ReadBlock(void *ptr
, size_t size
)
193 return hiodev
? hiodev
->readBlock(ptr
, size
) : 0;
197 size_t HWPFile::SkipBlock(size_t size
)
199 return hiodev
? hiodev
->skipBlock(size
) : 0;
203 bool HWPFile::SetCompressed(bool flag
)
205 return hiodev
? hiodev
->setCompressed(flag
) : false;
209 HIODev
*HWPFile::SetIODevice(HIODev
* new_hiodev
)
211 HIODev
*old_hiodev
= hiodev
;
219 // end of HIODev wrapper
221 bool HWPFile::InfoRead(void)
223 return _hwpInfo
.Read(*this);
227 bool HWPFile::FontRead(void)
229 return _hwpFont
.Read(*this);
233 bool HWPFile::StyleRead(void)
235 return _hwpStyle
.Read(*this);
239 bool HWPFile::ParaListRead(void)
241 return ReadParaList(plist
);
244 bool HWPFile::ReadParaList(std::list
< HWPPara
* > &aplist
, unsigned char flag
)
246 HWPPara
*spNode
= new HWPPara
;
247 unsigned char tmp_etcflag
;
248 unsigned char prev_etcflag
= 0;
249 while (spNode
->Read(*this, flag
))
251 if( !(spNode
->etcflag
& 0x04) ){
252 tmp_etcflag
= spNode
->etcflag
;
253 spNode
->etcflag
= prev_etcflag
;
254 prev_etcflag
= tmp_etcflag
;
256 if (spNode
->nch
&& spNode
->reuse_shape
)
258 if (!aplist
.empty()){
259 spNode
->pshape
= aplist
.back()->pshape
;
263 spNode
->reuse_shape
= 0;
266 spNode
->pshape
.pagebreak
= spNode
->etcflag
;
268 AddParaShape( &spNode
->pshape
);
271 aplist
.back()->SetNext(spNode
);
272 aplist
.push_back(spNode
);
273 spNode
= new HWPPara
;
281 bool HWPFile::TagsRead(void)
290 if (size
<= 0 && tag
> 0){
294 if (tag
== FILETAG_END_OF_COMPRESSED
||
295 tag
== FILETAG_END_OF_UNCOMPRESSED
)
299 case FILETAG_EMBEDDED_PICTURE
:
301 EmPicture
*emb
= new EmPicture(size
);
303 if (true == emb
->Read(*this))
304 emblist
.push_back(emb
);
309 case FILETAG_OLE_OBJECT
:
312 oledata
= new OlePicture(size
);
313 oledata
->Read(*this);
315 case FILETAG_HYPERTEXT
:
317 if( (size
% 617) != 0 )
320 for( int i
= 0 ; i
< size
/617 ; i
++)
322 HyperText
*hypert
= new HyperText
;
324 hyperlist
.push_back(hypert
);
330 ReadBlock(_hwpInfo
.back_info
.reserved1
, 8);
331 _hwpInfo
.back_info
.luminance
= Read4b();
332 _hwpInfo
.back_info
.contrast
= Read4b();
333 _hwpInfo
.back_info
.effect
= sal::static_int_cast
<char>(Read1b());
334 ReadBlock(_hwpInfo
.back_info
.reserved2
, 7);
335 ReadBlock(_hwpInfo
.back_info
.filename
, 260);
336 ReadBlock(_hwpInfo
.back_info
.color
, 3);
337 unsigned short nFlag
= sal::static_int_cast
<unsigned short>(Read2b());
338 _hwpInfo
.back_info
.flag
= nFlag
>> 8 ;
339 int nRange
= Read4b();
340 _hwpInfo
.back_info
.range
= nRange
>> 24;
341 ReadBlock(_hwpInfo
.back_info
.reserved3
, 27);
342 _hwpInfo
.back_info
.size
= Read4b();
344 _hwpInfo
.back_info
.data
= new char[(unsigned int)_hwpInfo
.back_info
.size
];
345 ReadBlock(_hwpInfo
.back_info
.data
, _hwpInfo
.back_info
.size
);
347 if( _hwpInfo
.back_info
.size
> 0 )
348 _hwpInfo
.back_info
.type
= 2;
349 else if( _hwpInfo
.back_info
.filename
[0] )
350 _hwpInfo
.back_info
.type
= 1;
352 _hwpInfo
.back_info
.type
= 0;
355 _hwpInfo
.back_info
.isset
= true;
359 case FILETAG_PRESENTATION
:
360 case FILETAG_PREVIEW_IMAGE
:
361 case FILETAG_PREVIEW_TEXT
:
369 ColumnDef
*HWPFile::GetColumnDef(int num
)
371 std::list
<ColumnInfo
*>::iterator it
= columnlist
.begin();
373 for(int i
= 0; it
!= columnlist
.end() ; ++it
, i
++){
378 if( it
!= columnlist
.end() )
379 return (*it
)->coldef
;
383 /* @return À妽º´Â 1ºÎÅÍ ½ÃÀÛÇÑ´Ù. */
384 int HWPFile::GetPageMasterNum(int page
)
386 std::list
<ColumnInfo
*>::iterator it
= columnlist
.begin();
390 for( i
= 1 ; it
!= columnlist
.end() ; ++it
, i
++){
392 if( page
< now
->start_page
)
398 HyperText
*HWPFile::GetHyperText()
400 std::list
<HyperText
*>::iterator it
= hyperlist
.begin();
402 for( int i
= 0; it
!= hyperlist
.end(); ++it
, i
++ ){
403 if( i
== currenthyper
)
411 EmPicture
*HWPFile::GetEmPicture(Picture
* pic
)
413 char *name
= pic
->picinfo
.picembed
.embname
;
419 std::list
< EmPicture
* >::iterator it
= emblist
.begin();
420 for (; it
!= emblist
.end(); ++it
)
421 if (strcmp(name
, (*it
)->name
) == 0)
426 EmPicture
*HWPFile::GetEmPictureByName(char * name
)
432 std::list
< EmPicture
* >::iterator it
= emblist
.begin();
433 for (; it
!= emblist
.end(); ++it
)
434 if (strcmp(name
, (*it
)->name
) == 0)
440 void HWPFile::AddBox(FBox
* box
)
442 // LATER if we don't use box->next(),
443 // AddBox() and GetBoxHead() are useless;
446 box
->prev
= blist
.back();
447 box
->prev
->next
= box
;
451 blist
.push_back(box
);
455 ParaShape
*HWPFile::getParaShape(int index
)
457 std::list
<ParaShape
*>::iterator it
= pslist
.begin();
459 for( int i
= 0; it
!= pslist
.end(); ++it
, i
++ ){
468 CharShape
*HWPFile::getCharShape(int index
)
470 std::list
<CharShape
*>::iterator it
= cslist
.begin();
472 for( int i
= 0; it
!= cslist
.end(); ++it
, i
++ ){
481 FBoxStyle
*HWPFile::getFBoxStyle(int index
)
483 std::list
<FBoxStyle
*>::iterator it
= fbslist
.begin();
485 for( int i
= 0; it
!= fbslist
.end(); ++it
, i
++ ){
493 DateCode
*HWPFile::getDateCode(int index
)
495 std::list
<DateCode
*>::iterator it
= datecodes
.begin();
497 for( int i
= 0; it
!= datecodes
.end(); ++it
, i
++ ){
505 HeaderFooter
*HWPFile::getHeaderFooter(int index
)
507 std::list
<HeaderFooter
*>::iterator it
= headerfooters
.begin();
509 for( int i
= 0; it
!= headerfooters
.end(); ++it
, i
++ ){
517 ShowPageNum
*HWPFile::getPageNumber(int index
)
519 std::list
<ShowPageNum
*>::iterator it
= pagenumbers
.begin();
521 for( int i
= 0; it
!= pagenumbers
.end(); ++it
, i
++ ){
530 Table
*HWPFile::getTable(int index
)
532 std::list
<Table
*>::iterator it
= tables
.begin();
534 for( int i
= 0; it
!= tables
.end(); ++it
, i
++ ){
542 void HWPFile::AddParaShape(ParaShape
* pshape
)
545 for(int j
= 0 ; j
< MAXTABS
-1 ; j
++)
547 if( j
> 0 && pshape
->tabs
[j
].position
== 0 )
549 if( pshape
->tabs
[0].position
== 0 ){
550 if( pshape
->tabs
[j
].type
|| pshape
->tabs
[j
].dot_continue
||
551 (pshape
->tabs
[j
].position
!= 1000 *j
) )
555 if( pshape
->tabs
[j
].type
|| pshape
->tabs
[j
].dot_continue
||
556 (pshape
->tabs
[j
].position
!= 1000 * (j
+ 1)) )
561 pshape
->tabs
[MAXTABS
-1].type
= sal::static_int_cast
<char>(nscount
);
562 int value
= compareParaShape(pshape
);
563 if( value
== 0 || nscount
)
565 pshape
->index
= ++pcount
;
566 pslist
.push_back(pshape
);
569 pshape
->index
= value
;
573 void HWPFile::AddCharShape(CharShape
* cshape
)
575 int value
= compareCharShape(cshape
);
578 cshape
->index
= ++ccount
;
579 cslist
.push_back(cshape
);
582 cshape
->index
= value
;
585 void HWPFile::AddColumnInfo()
587 ColumnInfo
*cinfo
= new ColumnInfo(m_nCurrentPage
);
588 columnlist
.push_back(cinfo
);
592 void HWPFile::SetColumnDef(ColumnDef
*coldef
)
594 ColumnInfo
*cinfo
= columnlist
.back();
597 cinfo
->coldef
= coldef
;
598 cinfo
->bIsSet
= true;
601 void HWPFile::AddDateFormat(DateCode
* hbox
)
603 hbox
->key
= sal::static_int_cast
<char>(++datecodecount
);
604 datecodes
.push_back(hbox
);
607 void HWPFile::AddPageNumber(ShowPageNum
* hbox
)
609 pagenumbers
.push_back(hbox
);
612 void HWPFile::AddHeaderFooter(HeaderFooter
* hbox
)
614 headerfooters
.push_back(hbox
);
617 void HWPFile::AddTable(Table
* hbox
)
619 tables
.push_back(hbox
);
622 void HWPFile::AddFBoxStyle(FBoxStyle
* fbstyle
)
624 fbslist
.push_back(fbstyle
);
627 int HWPFile::compareCharShape(CharShape
*shape
)
629 int count
= cslist
.size();
633 for(int i
= 0; i
< count
; i
++)
635 cshape
= getCharShape(i
);
637 if( shape
->size
== cshape
->size
&&
638 shape
->font
[0] == cshape
->font
[0] &&
639 shape
->ratio
[0] == cshape
->ratio
[0] &&
640 shape
->space
[0] == cshape
->space
[0] &&
641 shape
->color
[1] == cshape
->color
[1] &&
642 shape
->color
[0] == cshape
->color
[0] &&
643 shape
->shade
== cshape
->shade
&&
644 shape
->attr
== cshape
->attr
)
646 return cshape
->index
;
654 int HWPFile::compareParaShape(ParaShape
*shape
)
656 int count
= pslist
.size();
660 for(int i
= 0; i
< count
; i
++)
662 pshape
= getParaShape(i
);
663 if( shape
->left_margin
== pshape
->left_margin
&&
664 shape
->right_margin
== pshape
->right_margin
&&
665 shape
->pspacing_prev
== pshape
->pspacing_prev
&&
666 shape
->pspacing_next
== pshape
->pspacing_next
&&
667 shape
->indent
== pshape
->indent
&&
668 shape
->lspacing
== pshape
->lspacing
&&
669 shape
->arrange_type
== pshape
->arrange_type
&&
670 shape
->outline
== pshape
->outline
&&
671 shape
->pagebreak
== pshape
->pagebreak
)
673 if( shape
->cshape
->size
== pshape
->cshape
->size
&&
674 shape
->cshape
->font
[0] == pshape
->cshape
->font
[0] &&
675 shape
->cshape
->ratio
[0] == pshape
->cshape
->ratio
[0] &&
676 shape
->cshape
->space
[0] == pshape
->cshape
->space
[0] &&
677 shape
->cshape
->color
[1] == pshape
->cshape
->color
[1] &&
678 shape
->cshape
->color
[0] == pshape
->cshape
->color
[0] &&
679 shape
->cshape
->shade
== pshape
->cshape
->shade
&&
680 shape
->cshape
->attr
== pshape
->cshape
->attr
)
682 return pshape
->index
;
691 HWPFile
*GetCurrentDoc(void)
693 return HWPFile::cur_doc
;
697 HWPFile
*SetCurrentDoc(HWPFile
* hwpfp
)
699 HWPFile
*org
= HWPFile::cur_doc
;
701 HWPFile::cur_doc
= hwpfp
;
705 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */