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 HWPFile
*HWPFile::cur_doc
= 0;
40 static int ccount
= 0;
41 static int pcount
= 0;
42 static int datecodecount
= 0;
50 , error_code(HWP_NoError
)
65 std::list
< ColumnInfo
* >::iterator it_column
= columnlist
.begin();
66 for (; it_column
!= columnlist
.end(); ++it_column
)
69 std::list
< HWPPara
* >::iterator it
= plist
.begin();
70 for (; it
!= plist
.end(); ++it
)
73 std::list
< Table
* >::iterator tbl
= tables
.begin();
74 for (; tbl
!= tables
.end(); ++tbl
)
77 std::list
< HyperText
* >::iterator hyp
= hyperlist
.begin();
78 for (; hyp
!= hyperlist
.end(); ++hyp
)
84 int HWPFile::ReadHwpFile(HStream
& stream
)
86 if (Open(stream
) != HWP_NoError
)
98 int detect_hwp_version(const char *str
)
100 if (memcmp(V20SIGNATURE
, str
, HWPIDLen
) == 0)
102 else if (memcmp(V21SIGNATURE
, str
, HWPIDLen
) == 0)
104 else if (memcmp(V30SIGNATURE
, str
, HWPIDLen
) == 0)
111 int HWPFile::Open(HStream
& stream
)
113 HStreamIODev
*hstreamio
= new HStreamIODev(stream
);
115 if (!hstreamio
->open())
119 return SetState(HWP_EMPTY_FILE
);
122 HIODev
*pPrev
= SetIODevice(hstreamio
);
125 char idstr
[HWPIDLen
];
127 if (ReadBlock(idstr
, HWPIDLen
) <= 0
128 || HWP_V30
!= (version
= detect_hwp_version(idstr
)))
130 return SetState(HWP_UNSUPPORTED_VERSION
);
136 int HWPFile::State(void) const
142 int HWPFile::SetState(int errcode
)
144 error_code
= errcode
;
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
;
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
;
257 spNode
->reuse_shape
= 0;
260 spNode
->pshape
.pagebreak
= spNode
->etcflag
;
262 AddParaShape( &spNode
->pshape
);
265 aplist
.back()->SetNext(spNode
);
266 aplist
.push_back(spNode
);
267 spNode
= new HWPPara
;
275 bool HWPFile::TagsRead(void)
284 if (size
<= 0 && tag
> 0){
288 if (tag
== FILETAG_END_OF_COMPRESSED
||
289 tag
== FILETAG_END_OF_UNCOMPRESSED
)
293 case FILETAG_EMBEDDED_PICTURE
:
295 EmPicture
*emb
= new EmPicture(size
);
297 if (true == emb
->Read(*this))
298 emblist
.push_back(emb
);
303 case FILETAG_OLE_OBJECT
:
306 oledata
= new OlePicture(size
);
307 oledata
->Read(*this);
309 case FILETAG_HYPERTEXT
:
311 if( (size
% 617) != 0 )
314 for( int i
= 0 ; i
< size
/617 ; i
++)
316 HyperText
*hypert
= new HyperText
;
318 hyperlist
.push_back(hypert
);
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;
346 _hwpInfo
.back_info
.type
= 0;
349 _hwpInfo
.back_info
.isset
= true;
353 case FILETAG_PRESENTATION
:
354 case FILETAG_PREVIEW_IMAGE
:
355 case FILETAG_PREVIEW_TEXT
:
363 ColumnDef
*HWPFile::GetColumnDef(int num
)
365 std::list
<ColumnInfo
*>::iterator it
= columnlist
.begin();
367 for(int i
= 0; it
!= columnlist
.end() ; ++it
, i
++){
372 if( it
!= columnlist
.end() )
373 return (*it
)->coldef
;
377 /* @return À妽º´Â 1ºÎÅÍ ½ÃÀÛÇÑ´Ù. */
378 int HWPFile::GetPageMasterNum(int page
)
380 std::list
<ColumnInfo
*>::iterator it
= columnlist
.begin();
384 for( i
= 1 ; it
!= columnlist
.end() ; ++it
, i
++){
386 if( page
< now
->start_page
)
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
)
405 EmPicture
*HWPFile::GetEmPicture(Picture
* pic
)
407 char *name
= pic
->picinfo
.picembed
.embname
;
413 std::list
< EmPicture
* >::iterator it
= emblist
.begin();
414 for (; it
!= emblist
.end(); ++it
)
415 if (strcmp(name
, (*it
)->name
) == 0)
420 EmPicture
*HWPFile::GetEmPictureByName(char * name
)
426 std::list
< EmPicture
* >::iterator it
= emblist
.begin();
427 for (; it
!= emblist
.end(); ++it
)
428 if (strcmp(name
, (*it
)->name
) == 0)
434 void HWPFile::AddBox(FBox
* box
)
436 // LATER if we don't use box->next(),
437 // AddBox() and GetBoxHead() are useless;
440 box
->prev
= blist
.back();
441 box
->prev
->next
= box
;
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
++ ){
462 CharShape
*HWPFile::getCharShape(int index
)
464 std::list
<CharShape
*>::iterator it
= cslist
.begin();
466 for( int i
= 0; it
!= cslist
.end(); ++it
, i
++ ){
475 FBoxStyle
*HWPFile::getFBoxStyle(int index
)
477 std::list
<FBoxStyle
*>::iterator it
= fbslist
.begin();
479 for( int i
= 0; it
!= fbslist
.end(); ++it
, i
++ ){
487 DateCode
*HWPFile::getDateCode(int index
)
489 std::list
<DateCode
*>::iterator it
= datecodes
.begin();
491 for( int i
= 0; it
!= datecodes
.end(); ++it
, i
++ ){
499 HeaderFooter
*HWPFile::getHeaderFooter(int index
)
501 std::list
<HeaderFooter
*>::iterator it
= headerfooters
.begin();
503 for( int i
= 0; it
!= headerfooters
.end(); ++it
, i
++ ){
511 ShowPageNum
*HWPFile::getPageNumber(int index
)
513 std::list
<ShowPageNum
*>::iterator it
= pagenumbers
.begin();
515 for( int i
= 0; it
!= pagenumbers
.end(); ++it
, i
++ ){
524 Table
*HWPFile::getTable(int index
)
526 std::list
<Table
*>::iterator it
= tables
.begin();
528 for( int i
= 0; it
!= tables
.end(); ++it
, i
++ ){
536 void HWPFile::AddParaShape(ParaShape
* pshape
)
539 for(int j
= 0 ; j
< MAXTABS
-1 ; j
++)
541 if( j
> 0 && pshape
->tabs
[j
].position
== 0 )
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
) )
549 if( pshape
->tabs
[j
].type
|| pshape
->tabs
[j
].dot_continue
||
550 (pshape
->tabs
[j
].position
!= 1000 * (j
+ 1)) )
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
);
563 pshape
->index
= value
;
567 void HWPFile::AddCharShape(CharShape
* cshape
)
569 int value
= compareCharShape(cshape
);
572 cshape
->index
= ++ccount
;
573 cslist
.push_back(cshape
);
576 cshape
->index
= value
;
579 void HWPFile::AddColumnInfo()
581 ColumnInfo
*cinfo
= new ColumnInfo(m_nCurrentPage
);
582 columnlist
.push_back(cinfo
);
586 void HWPFile::SetColumnDef(ColumnDef
*coldef
)
588 ColumnInfo
*cinfo
= columnlist
.back();
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();
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
;
648 int HWPFile::compareParaShape(ParaShape
*shape
)
650 int count
= pslist
.size();
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
;
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
;
699 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */