bump product version to 4.1.6.2
[LibreOffice.git] / hwpfilter / source / drawing.h
blobdfc31bf5723701ff7edc619eb7eb1e1f80e66f95
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 #ifndef _DRAWING_H_
21 #define _DRAWING_H_
23 #include "precompile.h"
25 #include <list>
26 #include <math.h>
28 #include <osl/diagnose.h>
30 #include <comphelper/newarray.hxx>
32 #include "hwplib.h"
33 #include "hwpfile.h"
34 #include "hiodev.h"
35 #include "hbox.h"
36 #include "drawdef.h"
38 enum
40 OBJFUNC_LOAD,
41 OBJFUNC_FREE,
42 OBJFUNC_DISPLAY,
43 OBJFUNC_NITEM
46 enum
48 BEGIN_GRADATION = 0, LINEAR, RADIAL, CONICAL, SQUARE,
49 END_GRADATION, BITMAP_PATTERN
52 #define OBJRET_FILE_OK 0
53 #define OBJRET_FILE_ERROR (-1)
54 #define OBJRET_FILE_NO_PRIVATE_BLOCK (-2)
55 #define OBJRET_FILE_NO_PRIVATE_BLOCK_2 (-3)
57 typedef int (*HWPDOFuncType) (int, HWPDrawingObject *, int, void *, int);
59 #define HWPDOFunc(hdo, cmd, argp, argv) \
60 (HWPDOFuncTbl[(hdo)->type]((hdo)->type, (hdo), (cmd), (argp), (argv)))
61 #define HWPDOFunc2(type, cmd, argp, argv) \
62 (HWPDOFuncTbl[(type)]((type), NULL, (cmd), (argp), (argv)))
64 static int HWPDOLineFunc(int, HWPDrawingObject *, int, void *, int);
65 static int HWPDORectFunc(int, HWPDrawingObject *, int, void *, int);
66 static int HWPDOEllipseFunc(int, HWPDrawingObject *, int, void *, int);
67 static int HWPDOArcFunc(int, HWPDrawingObject *, int, void *, int);
68 static int HWPDOFreeFormFunc(int, HWPDrawingObject *, int, void *, int);
69 static int HWPDOTextBoxFunc(int, HWPDrawingObject *, int, void *, int);
70 static int HWPDOEllipse2Func(int, HWPDrawingObject *, int, void *, int);
71 static int HWPDOArc2Func(int, HWPDrawingObject *, int, void *, int);
72 static int HWPDOContainerFunc(int, HWPDrawingObject *, int, void *, int);
73 static HWPPara *LoadParaList();
75 HWPDOFuncType HWPDOFuncTbl[] =
77 HWPDOContainerFunc,
78 HWPDOLineFunc,
79 HWPDORectFunc,
80 HWPDOEllipseFunc,
81 HWPDOArcFunc,
82 HWPDOFreeFormFunc,
83 HWPDOTextBoxFunc,
84 HWPDOFreeFormFunc,
85 HWPDOEllipse2Func,
86 HWPDOArc2Func,
87 HWPDOFreeFormFunc,
90 static HMemIODev *hmem = 0;
92 static int count = 0;
94 inline bool HAVE_FCOLOR(HWPDrawingObject * hdo)
96 return hdo->property.fill_color != HWPDO_COLOR_NONE;
100 inline bool HAVE_PATTERN(HWPDrawingObject * hdo)
102 return (hdo->property.pattern_type & HWPDO_PAT_TYPE_BITS)
103 != HWPDO_PAT_SOLID && hdo->property.pattern_color != HWPDO_COLOR_NONE;
107 inline bool HAVE_GRADATION(HWPDrawingObject * hdo)
109 return hdo->property.gstyle > BEGIN_GRADATION &&
110 hdo->property.gstyle < END_GRADATION &&
111 hdo->property.fromcolor != HWPDO_COLOR_NONE &&
112 hdo->property.tocolor != HWPDO_COLOR_NONE;
116 inline bool HAVE_BITMAP_PATTERN(HWPDrawingObject * hdo)
118 return hdo->property.gstyle == BITMAP_PATTERN &&
119 hdo->property.szPatternFile[0];
123 inline bool HAS_PAT(HWPDrawingObject * hdo)
125 return HAVE_FCOLOR(hdo) || HAVE_PATTERN(hdo) ||
126 HAVE_GRADATION(hdo) || HAVE_BITMAP_PATTERN(hdo);
130 static void SetHdoParallRgn(HWPDrawingObject * hdo, int width, int height)
132 hdo->property.parall.pt[0].x = 0;
133 hdo->property.parall.pt[0].y = 0;
134 hdo->property.parall.pt[1].x = width;
135 hdo->property.parall.pt[1].y = 0;
136 hdo->property.parall.pt[2].x = width;
137 hdo->property.parall.pt[2].y = height;
141 static bool SkipPrivateBlock(int type)
143 int n;
145 if (type == OBJRET_FILE_NO_PRIVATE_BLOCK)
147 n = hmem->read4b();
148 if (hmem->state() || hmem->skipBlock(n) != n)
149 return false;
151 n = hmem->read4b();
152 if (hmem->state())
153 return false;
154 return hmem->skipBlock(n) == n;
158 static int SizeExpected;
159 static int SizeRead;
161 static int ReadSizeField(int size)
163 SizeExpected = size;
164 SizeRead = hmem->read4b();
165 if (hmem->state())
166 return -1;
167 return SizeRead;
171 static bool SkipUnusedField(void)
173 return (SizeExpected < SizeRead) ?
174 hmem->skipBlock(SizeRead - SizeExpected) != 0 : true;
178 #define HDOFILE_HEADER_SIZE (2*4+16) // 16=sizeof(ZZRect)
179 #define HDOFILE_COMMON_SIZE (7*4+16+44)
181 #define HDOFILE_HAS_NEXT 0x01
182 #define HDOFILE_HAS_CHILD 0x02
184 static bool LoadCommonHeader(HWPDrawingObject * hdo, WORD * link_info)
186 uint size, common_size;
188 if( !hmem )
189 return FALSE;
190 size = hmem->read4b();
191 if (hmem->state())
193 return FALSE;
195 if (size < HDOFILE_COMMON_SIZE)
197 return FALSE;
200 common_size = HDOFILE_COMMON_SIZE;
201 hdo->type = hmem->read2b();
202 *link_info = sal::static_int_cast<WORD>(hmem->read2b());
203 hdo->offset.x = hmem->read4b();
204 hdo->offset.y = hmem->read4b();
205 hdo->extent.w = hmem->read4b();
206 hdo->extent.h = hmem->read4b();
207 hdo->offset2.x = hmem->read4b();
208 hdo->offset2.y = hmem->read4b();
210 if (hmem->state())
211 return FALSE;
213 hdo->vrect.x = hmem->read4b();
214 hdo->vrect.y = hmem->read4b();
215 hdo->vrect.w = hmem->read4b();
216 hdo->vrect.h = hmem->read4b();
218 // read bare property 44 bytes
219 hdo->property.line_pstyle = hmem->read4b();
220 hdo->property.line_hstyle = hmem->read4b();
221 hdo->property.line_tstyle = hmem->read4b();
222 hdo->property.line_color = hmem->read4b();
223 hdo->property.line_width = (hunit) hmem->read4b();
224 hdo->property.fill_color = hmem->read4b();
225 hdo->property.pattern_type = hmem->read4b();
226 hdo->property.pattern_color = hmem->read4b();
227 hdo->property.hmargin = (hunit) hmem->read4b();
228 hdo->property.vmargin = (hunit) hmem->read4b();
229 hdo->property.flag = hmem->read4b();
230 // read ratation property 32 bytes
231 if ((size >= common_size + 32)
232 && (hdo->property.flag & HWPDO_FLAG_ROTATION))
234 hdo->property.rot_originx = hmem->read4b();
235 hdo->property.rot_originy = hmem->read4b();
236 for (int ii = 0; ii < 3; ii++)
238 hdo->property.parall.pt[ii].x = hmem->read4b();
239 hdo->property.parall.pt[ii].y = hmem->read4b();
241 common_size += 32;
243 else
244 SetHdoParallRgn(hdo, hdo->extent.w, hdo->extent.h);
246 // read gradient property 28 bytes
247 if ((size >= common_size + 28) &&
248 (hdo->property.flag & HWPDO_FLAG_GRADATION))
250 hdo->property.fromcolor = hmem->read4b();
251 hdo->property.tocolor = hmem->read4b();
252 hdo->property.gstyle = hmem->read4b();
253 hdo->property.angle = hmem->read4b();
254 hdo->property.center_x = hmem->read4b();
255 hdo->property.center_y = hmem->read4b();
256 hdo->property.nstep = hmem->read4b();
257 common_size += 28;
260 // read bitmap property 278 bytes
261 if ((size >= common_size + 278) && \
262 (hdo->property.flag & HWPDO_FLAG_BITMAP))
264 hdo->property.offset1.x = hmem->read4b();
265 hdo->property.offset1.y = hmem->read4b();
266 hdo->property.offset2.x = hmem->read4b();
267 hdo->property.offset2.y = hmem->read4b();
268 if (!hmem->readBlock(hdo->property.szPatternFile, 261))
269 return FALSE;
270 hdo->property.pictype = sal::static_int_cast<char>(hmem->read1b());
271 common_size += 278;
273 if( ( size >= common_size + 3 ) && ( hdo->property.flag & HWPDO_FLAG_WATERMARK ) )
274 //if( ( size >= common_size ) && ( hdo->property.flag >> 20 & 0x01 ) )
276 if( size - common_size >= 5 )
277 hmem->skipBlock( 2 );
278 hdo->property.luminance = hmem->read1b();
279 hdo->property.contrast = hmem->read1b();
280 hdo->property.greyscale = hmem->read1b();
281 common_size += 5;
283 else{
284 hdo->property.luminance = 0;
285 hdo->property.contrast = 0;
286 hdo->property.greyscale = 0;
288 hdo->property.pPara = 0L;
290 if( ( size > common_size ) && (hdo->property.flag & HWPDO_FLAG_AS_TEXTBOX) )
292 hmem->skipBlock(8);
293 hdo->property.pPara = LoadParaList();
294 if( hdo->property.pPara )
295 return TRUE;
296 else
297 return FALSE;
300 if( size <= common_size )
301 return TRUE;
302 return hmem->skipBlock(size - common_size ) != 0;
306 static HWPDrawingObject *LoadDrawingObject(void)
308 HWPDrawingObject *hdo, *head, *prev;
309 int res;
311 WORD link_info;
313 head = prev = NULL;
316 if ((hdo = new HWPDrawingObject) == NULL)
318 goto error;
320 if (!LoadCommonHeader(hdo, &link_info))
322 goto error;
324 if (hdo->type < 0 || hdo->type >= HWPDO_NITEMS)
326 hdo->type = HWPDO_RECT;
327 if (!SkipPrivateBlock(OBJRET_FILE_NO_PRIVATE_BLOCK))
329 goto error;
332 else
334 switch (res = HWPDOFunc(hdo, OBJFUNC_LOAD, NULL, 0))
336 case OBJRET_FILE_ERROR:
337 goto error;
338 case OBJRET_FILE_OK:
339 break;
340 case OBJRET_FILE_NO_PRIVATE_BLOCK:
341 case OBJRET_FILE_NO_PRIVATE_BLOCK_2:
342 if (!SkipPrivateBlock(res))
343 goto error;
344 break;
347 if (link_info & HDOFILE_HAS_CHILD)
349 hdo->child = LoadDrawingObject();
350 if (hdo->child == NULL)
352 goto error;
355 if (prev == NULL)
356 head = hdo;
357 else
358 prev->next = hdo;
359 prev = hdo;
361 while (link_info & HDOFILE_HAS_NEXT);
363 return head;
364 error:
365 // drawing object can be list.
366 // hdo = current item, head = list;
368 if (hdo != NULL)
370 HWPDOFunc(hdo, OBJFUNC_FREE, NULL, 0);
371 delete hdo;
373 if( prev )
375 prev->next = NULL;
376 return head;
378 else
379 return 0;
383 static bool LoadDrawingObjectBlock(Picture * pic)
385 int size = hmem->read4b();
387 if (hmem->state() || size < HDOFILE_HEADER_SIZE)
388 return false;
390 pic->picinfo.picdraw.zorder = hmem->read4b();
391 pic->picinfo.picdraw.mbrcnt = hmem->read4b();
392 pic->picinfo.picdraw.vrect.x = hmem->read4b();
393 pic->picinfo.picdraw.vrect.y = hmem->read4b();
394 pic->picinfo.picdraw.vrect.w = hmem->read4b();
395 pic->picinfo.picdraw.vrect.h = hmem->read4b();
397 if (size > HDOFILE_HEADER_SIZE &&
398 !hmem->skipBlock(size - HDOFILE_HEADER_SIZE))
399 return false;
401 pic->picinfo.picdraw.hdo = LoadDrawingObject();
402 if (pic->picinfo.picdraw.hdo == 0)
403 return false;
404 return true;
408 // object manipulation function
410 static int
411 HWPDODefaultFunc(int , HWPDrawingObject * , int cmd, void *, int)
413 if (cmd == OBJFUNC_LOAD)
414 return OBJRET_FILE_NO_PRIVATE_BLOCK;
415 return true;
419 static int
420 HWPDOLineFunc(int type, HWPDrawingObject * hdo, int cmd, void *argp, int argv)
422 int ret = true;
423 switch (cmd)
425 case OBJFUNC_LOAD:
426 if (ReadSizeField(4) < 4)
427 return OBJRET_FILE_ERROR;
428 hdo->u.line_arc.flip = hmem->read4b();
429 if (hmem->state())
430 return OBJRET_FILE_ERROR;
431 if (!SkipUnusedField())
432 return OBJRET_FILE_ERROR;
433 ret = OBJRET_FILE_NO_PRIVATE_BLOCK_2;
434 break;
435 default:
436 ret = HWPDODefaultFunc(type, hdo, cmd, argp, argv);
437 break;
439 return ret;
443 // rectangle
445 static int
446 HWPDORectFunc(int type, HWPDrawingObject * hdo, int cmd, void *argp, int argv)
448 return HWPDODefaultFunc(type, hdo, cmd, argp, argv);
452 // ellipse
454 static int
455 HWPDOEllipseFunc(int type, HWPDrawingObject * hdo,
456 int cmd, void *argp, int argv)
458 return HWPDODefaultFunc(type, hdo, cmd, argp, argv);
461 #define WTMM(x) ((double)(x) / 1800. * 25.4)
462 static int
463 HWPDOEllipse2Func(int type, HWPDrawingObject * hdo,
464 int cmd, void *argp, int argv)
466 switch (cmd)
468 case OBJFUNC_LOAD:
469 if (ReadSizeField(16) < 16)
470 return OBJRET_FILE_ERROR;
471 hdo->u.arc.radial[0].x = hmem->read4b();
472 hdo->u.arc.radial[0].y = hmem->read4b();
473 hdo->u.arc.radial[1].x = hmem->read4b();
474 hdo->u.arc.radial[1].y = hmem->read4b();
476 if (ReadSizeField(0) < 0)
477 return OBJRET_FILE_ERROR;
478 break;
479 default:
480 return HWPDODefaultFunc(type, hdo, cmd, argp, argv);
482 return true;
486 // arc
488 static int
489 HWPDOArcFunc(int type, HWPDrawingObject * hdo, int cmd, void *argp, int argv)
491 switch (cmd)
493 case OBJFUNC_LOAD:
494 if (ReadSizeField(4) < 4)
495 return OBJRET_FILE_ERROR;
496 hdo->u.line_arc.flip = hmem->read4b();
497 if (hmem->state())
498 return OBJRET_FILE_ERROR;
499 if (!SkipUnusedField())
500 return OBJRET_FILE_ERROR;
501 break;
502 default:
503 return HWPDODefaultFunc(type, hdo, cmd, argp, argv);
505 return true;
509 static int
510 HWPDOArc2Func(int type, HWPDrawingObject * hdo, int cmd, void *argp, int argv)
512 int ret = true;
513 switch (cmd)
515 case OBJFUNC_LOAD:
516 ret = OBJRET_FILE_NO_PRIVATE_BLOCK;
517 break;
518 default:
519 ret = HWPDODefaultFunc(type, hdo, cmd, argp, argv);
520 break;
522 return ret;
526 static int
527 HWPDOFreeFormFunc(int type, HWPDrawingObject * hdo,
528 int cmd, void *argp, int argv)
530 switch (cmd)
532 case OBJFUNC_LOAD:
534 hdo->u.freeform.pt = 0;
535 if (ReadSizeField(4) < 4)
536 return OBJRET_FILE_ERROR;
537 hdo->u.freeform.npt = hmem->read4b();
538 if (hmem->state())
539 return OBJRET_FILE_ERROR;
540 if (!SkipUnusedField())
541 return OBJRET_FILE_ERROR;
543 int size = hdo->u.freeform.npt * sizeof(ZZPoint);
545 if (ReadSizeField(size) < size)
546 return OBJRET_FILE_ERROR;
547 if (hdo->u.freeform.npt)
549 hdo->u.freeform.pt =
550 ::comphelper::newArray_null<ZZPoint>(hdo->u.freeform.npt);
551 if (hdo->u.freeform.pt == NULL)
553 hdo->u.freeform.npt = 0;
554 return OBJRET_FILE_ERROR;
556 for (int ii = 0; ii < hdo->u.freeform.npt; ii++)
558 hdo->u.freeform.pt[ii].x = hmem->read4b();
559 hdo->u.freeform.pt[ii].y = hmem->read4b();
560 if (hmem->state())
562 delete[]hdo->u.freeform.pt;
563 hdo->u.freeform.npt = 0;
564 return OBJRET_FILE_ERROR;
568 if (!SkipUnusedField())
569 return OBJRET_FILE_ERROR;
570 return OBJRET_FILE_OK;
572 case OBJFUNC_FREE:
573 if (hdo->u.freeform.pt)
574 delete[]hdo->u.freeform.pt;
575 break;
576 default:
577 return HWPDODefaultFunc(type, hdo, cmd, argp, argv);
579 return true;
583 // text box
585 static void FreeParaList(HWPPara * para)
587 if (para->Next())
588 FreeParaList(para->Next());
589 delete para;
593 static HWPPara *LoadParaList()
595 if (!hmem)
596 return 0;
598 HWPFile *hwpf = GetCurrentDoc();
599 HIODev *hio = hwpf->SetIODevice(hmem);
601 std::list < HWPPara* > plist;
603 hwpf->ReadParaList(plist);
604 hwpf->SetIODevice(hio);
606 return plist.size()? plist.front() : 0;
610 static int
611 HWPDOTextBoxFunc(int type, HWPDrawingObject * hdo,
612 int cmd, void *argp, int argv)
614 switch (cmd)
616 case OBJFUNC_LOAD:
617 if (ReadSizeField(0) < 0 || !SkipUnusedField())
618 return OBJRET_FILE_ERROR;
619 if (ReadSizeField(0) < 0)
620 return OBJRET_FILE_ERROR;
621 hdo->u.textbox.h = LoadParaList();
622 return hdo->u.textbox.h ? OBJRET_FILE_OK : OBJRET_FILE_ERROR;
623 case OBJFUNC_FREE:
624 if (hdo->u.textbox.h)
626 FreeParaList(hdo->u.textbox.h);
627 hdo->u.textbox.h = NULL;
629 break;
630 default:
631 return HWPDODefaultFunc(type, hdo, cmd, argp, argv);
633 return true;
638 static int
639 HWPDOContainerFunc(int type, HWPDrawingObject * hdo,
640 int cmd, void *argp, int argv)
642 return HWPDODefaultFunc(type, hdo, cmd, argp, argv);
646 HWPDrawingObject::HWPDrawingObject()
648 memset(this, 0, sizeof(HWPDrawingObject));
649 index = ++count;
653 HWPDrawingObject::~HWPDrawingObject()
655 if (child)
656 delete child;
658 if (next)
659 delete next;
661 HWPDOFunc(this, OBJFUNC_FREE, NULL, 0);
663 #endif
665 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */