fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / hwpfilter / source / drawing.h
blob42e84ec464aa8bbef5d1605b30eafa059600004e
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 INCLUDED_HWPFILTER_SOURCE_DRAWING_H
21 #define INCLUDED_HWPFILTER_SOURCE_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)))
62 static int HWPDOLineFunc(int, HWPDrawingObject *, int, void *, int);
63 static int HWPDORectFunc(int, HWPDrawingObject *, int, void *, int);
64 static int HWPDOEllipseFunc(int, HWPDrawingObject *, int, void *, int);
65 static int HWPDOArcFunc(int, HWPDrawingObject *, int, void *, int);
66 static int HWPDOFreeFormFunc(int, HWPDrawingObject *, int, void *, int);
67 static int HWPDOTextBoxFunc(int, HWPDrawingObject *, int, void *, int);
68 static int HWPDOEllipse2Func(int, HWPDrawingObject *, int, void *, int);
69 static int HWPDOArc2Func(int, HWPDrawingObject *, int, void *, int);
70 static int HWPDOContainerFunc(int, HWPDrawingObject *, int, void *, int);
71 static HWPPara *LoadParaList();
73 HWPDOFuncType HWPDOFuncTbl[] =
75 HWPDOContainerFunc,
76 HWPDOLineFunc,
77 HWPDORectFunc,
78 HWPDOEllipseFunc,
79 HWPDOArcFunc,
80 HWPDOFreeFormFunc,
81 HWPDOTextBoxFunc,
82 HWPDOFreeFormFunc,
83 HWPDOEllipse2Func,
84 HWPDOArc2Func,
85 HWPDOFreeFormFunc,
88 static HMemIODev *hmem = 0;
90 static int count = 0;
92 inline bool HAVE_FCOLOR(HWPDrawingObject * hdo)
94 return hdo->property.fill_color != HWPDO_COLOR_NONE;
98 inline bool HAVE_PATTERN(HWPDrawingObject * hdo)
100 return (hdo->property.pattern_type & HWPDO_PAT_TYPE_BITS)
101 != HWPDO_PAT_SOLID && hdo->property.pattern_color != HWPDO_COLOR_NONE;
105 inline bool HAVE_GRADATION(HWPDrawingObject * hdo)
107 return hdo->property.gstyle > BEGIN_GRADATION &&
108 hdo->property.gstyle < END_GRADATION &&
109 hdo->property.fromcolor != HWPDO_COLOR_NONE &&
110 hdo->property.tocolor != HWPDO_COLOR_NONE;
114 inline bool HAVE_BITMAP_PATTERN(HWPDrawingObject * hdo)
116 return hdo->property.gstyle == BITMAP_PATTERN &&
117 hdo->property.szPatternFile[0];
121 inline bool HAS_PAT(HWPDrawingObject * hdo)
123 return HAVE_FCOLOR(hdo) || HAVE_PATTERN(hdo) ||
124 HAVE_GRADATION(hdo) || HAVE_BITMAP_PATTERN(hdo);
127 static void SetHdoParallRgn(HWPDrawingObject * hdo, int width, int height)
129 hdo->property.parall.pt[0].x = 0;
130 hdo->property.parall.pt[0].y = 0;
131 hdo->property.parall.pt[1].x = width;
132 hdo->property.parall.pt[1].y = 0;
133 hdo->property.parall.pt[2].x = width;
134 hdo->property.parall.pt[2].y = height;
137 static bool SkipPrivateBlock(int type)
139 int n;
141 if (type == OBJRET_FILE_NO_PRIVATE_BLOCK)
143 if (!hmem->read4b(n))
144 return false;
145 if (hmem->state() || hmem->skipBlock(n) != n)
146 return false;
148 if (!hmem->read4b(n))
149 return false;
150 if (hmem->state())
151 return false;
152 return hmem->skipBlock(n) == n;
155 static int SizeExpected;
156 static int SizeRead;
158 static int ReadSizeField(int size)
160 SizeExpected = size;
161 if (!hmem->read4b(SizeRead))
162 return -1;
163 if (hmem->state())
164 return -1;
165 return SizeRead;
168 static bool SkipUnusedField(void)
170 return (SizeExpected >= SizeRead) &&
171 hmem->skipBlock(SizeRead - SizeExpected) != 0;
175 #define HDOFILE_HEADER_SIZE (2*4+16) // 16=sizeof(ZZRect)
176 #define HDOFILE_COMMON_SIZE (7*4+16+44)
178 #define HDOFILE_HAS_NEXT 0x01
179 #define HDOFILE_HAS_CHILD 0x02
181 static bool LoadCommonHeader(HWPDrawingObject * hdo, unsigned short * link_info)
183 uint size, common_size;
185 if (!hmem)
186 return false;
187 if (!hmem->read4b(size))
188 return false;
189 if (hmem->state())
190 return false;
191 if (size < HDOFILE_COMMON_SIZE)
192 return false;
194 common_size = HDOFILE_COMMON_SIZE;
195 unsigned short tmp16;
196 if (!hmem->read2b(tmp16))
197 return false;
198 hdo->type = tmp16;
199 if (!hmem->read2b(tmp16))
200 return false;
201 *link_info = tmp16;
202 if (!hmem->read4b(hdo->offset.x))
203 return false;
204 if (!hmem->read4b(hdo->offset.y))
205 return false;
206 if (!hmem->read4b(hdo->extent.w))
207 return false;
208 if (!hmem->read4b(hdo->extent.h))
209 return false;
210 if (!hmem->read4b(hdo->offset2.x))
211 return false;
212 if (!hmem->read4b(hdo->offset2.y))
213 return false;
215 if (hmem->state())
216 return false;
218 if (!hmem->read4b(hdo->vrect.x))
219 return false;
220 if (!hmem->read4b(hdo->vrect.y))
221 return false;
222 if (!hmem->read4b(hdo->vrect.w))
223 return false;
224 if (!hmem->read4b(hdo->vrect.h))
225 return false;
227 // read bare property 44 bytes
228 if (!hmem->read4b(hdo->property.line_pstyle))
229 return false;
230 if (!hmem->read4b(hdo->property.line_hstyle))
231 return false;
232 if (!hmem->read4b(hdo->property.line_tstyle))
233 return false;
234 if (!hmem->read4b(hdo->property.line_color))
235 return false;
236 unsigned int tmp32;
237 if (!hmem->read4b(tmp32))
238 return false;
239 hdo->property.line_width = static_cast<hunit>(tmp32);
240 if (!hmem->read4b(hdo->property.fill_color))
241 return false;
242 if (!hmem->read4b(hdo->property.pattern_type))
243 return false;
244 if (!hmem->read4b(hdo->property.pattern_color))
245 return false;
246 if (!hmem->read4b(tmp32))
247 return false;
248 hdo->property.hmargin = static_cast<hunit>(tmp32);
249 if (!hmem->read4b(tmp32))
250 return false;
251 hdo->property.vmargin = static_cast<hunit>(tmp32);
252 if (!hmem->read4b(hdo->property.flag))
253 return false;
254 // read rotation property 32 bytes
255 if ((size >= common_size + 32)
256 && (hdo->property.flag & HWPDO_FLAG_ROTATION))
258 if (!hmem->read4b(hdo->property.rot_originx))
259 return false;
260 if (!hmem->read4b(hdo->property.rot_originy))
261 return false;
262 for (int ii = 0; ii < 3; ++ii)
264 if (!hmem->read4b(hdo->property.parall.pt[ii].x))
265 return false;
266 if (!hmem->read4b(hdo->property.parall.pt[ii].y))
267 return false;
269 common_size += 32;
271 else
272 SetHdoParallRgn(hdo, hdo->extent.w, hdo->extent.h);
274 // read gradient property 28 bytes
275 if ((size >= common_size + 28) &&
276 (hdo->property.flag & HWPDO_FLAG_GRADATION))
278 if (!hmem->read4b(hdo->property.fromcolor))
279 return false;
280 if (!hmem->read4b(hdo->property.tocolor))
281 return false;
282 if (!hmem->read4b(hdo->property.gstyle))
283 return false;
284 if (!hmem->read4b(hdo->property.angle))
285 return false;
286 if (!hmem->read4b(hdo->property.center_x))
287 return false;
288 if (!hmem->read4b(hdo->property.center_y))
289 return false;
290 if (!hmem->read4b(hdo->property.nstep))
291 return false;
292 common_size += 28;
295 // read bitmap property 278 bytes
296 if ((size >= common_size + 278) && \
297 (hdo->property.flag & HWPDO_FLAG_BITMAP))
299 if (!hmem->read4b(hdo->property.offset1.x))
300 return false;
301 if (!hmem->read4b(hdo->property.offset1.y))
302 return false;
303 if (!hmem->read4b(hdo->property.offset2.x))
304 return false;
305 if (!hmem->read4b(hdo->property.offset2.y))
306 return false;
307 if (!hmem->readBlock(hdo->property.szPatternFile, 261))
308 return false;
309 if (!hmem->read1b(hdo->property.pictype))
310 return false;
311 common_size += 278;
313 if( ( size >= common_size + 3 ) && ( hdo->property.flag & HWPDO_FLAG_WATERMARK ) )
314 //if( ( size >= common_size ) && ( hdo->property.flag >> 20 & 0x01 ) )
316 if (size - common_size >= 5)
317 hmem->skipBlock(2);
318 unsigned char tmp8;
319 if (!hmem->read1b(tmp8))
320 return false;
321 hdo->property.luminance = tmp8;
322 if (!hmem->read1b(tmp8))
323 return false;
324 hdo->property.contrast = tmp8;
325 if (!hmem->read1b(tmp8))
326 return false;
327 hdo->property.greyscale = tmp8;
329 common_size += 5;
331 else
333 hdo->property.luminance = 0;
334 hdo->property.contrast = 0;
335 hdo->property.greyscale = 0;
337 hdo->property.pPara = 0L;
339 if( ( size > common_size ) && (hdo->property.flag & HWPDO_FLAG_AS_TEXTBOX) )
341 hmem->skipBlock(8);
342 hdo->property.pPara = LoadParaList();
343 if( hdo->property.pPara )
344 return true;
345 else
346 return false;
349 if (size <= common_size)
350 return true;
351 return hmem->skipBlock(size - common_size ) != 0;
354 static HWPDrawingObject *LoadDrawingObject(void)
356 fprintf(stderr, "LoadDrawingObject\n");
358 HWPDrawingObject *hdo, *head, *prev;
359 int res;
361 unsigned short link_info;
363 head = prev = NULL;
366 hdo = new HWPDrawingObject;
367 if (!LoadCommonHeader(hdo, &link_info))
369 goto error;
371 if (hdo->type < 0 || hdo->type >= HWPDO_NITEMS)
373 hdo->type = HWPDO_RECT;
374 if (!SkipPrivateBlock(OBJRET_FILE_NO_PRIVATE_BLOCK))
376 goto error;
379 else
381 switch (res = HWPDOFunc(hdo, OBJFUNC_LOAD, NULL, 0))
383 case OBJRET_FILE_ERROR:
384 goto error;
385 case OBJRET_FILE_OK:
386 break;
387 case OBJRET_FILE_NO_PRIVATE_BLOCK:
388 case OBJRET_FILE_NO_PRIVATE_BLOCK_2:
389 if (!SkipPrivateBlock(res))
390 goto error;
391 break;
394 if (link_info & HDOFILE_HAS_CHILD)
396 hdo->child = LoadDrawingObject();
397 if (hdo->child == NULL)
399 goto error;
402 if (prev == NULL)
403 head = hdo;
404 else
405 prev->next = hdo;
406 prev = hdo;
408 while (link_info & HDOFILE_HAS_NEXT);
410 return head;
411 error:
412 // drawing object can be list.
413 // hdo = current item, head = list;
415 if (hdo != NULL)
417 if (hdo->type < 0 || hdo->type >= HWPDO_NITEMS)
419 hdo->type = HWPDO_RECT;
422 HWPDOFunc(hdo, OBJFUNC_FREE, NULL, 0);
423 delete hdo;
425 if( prev )
427 prev->next = NULL;
428 return head;
430 else
431 return 0;
435 static bool LoadDrawingObjectBlock(Picture * pic)
437 int size;
438 if (!hmem->read4b(size))
439 return false;
441 if (hmem->state() || size < HDOFILE_HEADER_SIZE)
442 return false;
444 if (!hmem->read4b(pic->picinfo.picdraw.zorder))
445 return false;
446 if (!hmem->read4b(pic->picinfo.picdraw.mbrcnt))
447 return false;
448 if (!hmem->read4b(pic->picinfo.picdraw.vrect.x))
449 return false;
450 if (!hmem->read4b(pic->picinfo.picdraw.vrect.y))
451 return false;
452 if (!hmem->read4b(pic->picinfo.picdraw.vrect.w))
453 return false;
454 if (!hmem->read4b(pic->picinfo.picdraw.vrect.h))
455 return false;
457 if (size > HDOFILE_HEADER_SIZE &&
458 !hmem->skipBlock(size - HDOFILE_HEADER_SIZE))
459 return false;
461 pic->picinfo.picdraw.hdo = LoadDrawingObject();
462 if (pic->picinfo.picdraw.hdo == 0)
463 return false;
464 return true;
467 // object manipulation function
468 static int
469 HWPDODefaultFunc(int , HWPDrawingObject * , int cmd, void *, int)
471 if (cmd == OBJFUNC_LOAD)
472 return OBJRET_FILE_NO_PRIVATE_BLOCK;
473 return OBJRET_FILE_OK;
476 static int
477 HWPDOLineFunc(int type, HWPDrawingObject * hdo, int cmd, void *argp, int argv)
479 int ret = OBJRET_FILE_OK;
480 switch (cmd)
482 case OBJFUNC_LOAD:
483 if (ReadSizeField(4) < 4)
484 return OBJRET_FILE_ERROR;
485 if (!hmem->read4b(hdo->u.line_arc.flip))
486 return OBJRET_FILE_ERROR;
487 if (hmem->state())
488 return OBJRET_FILE_ERROR;
489 if (!SkipUnusedField())
490 return OBJRET_FILE_ERROR;
491 ret = OBJRET_FILE_NO_PRIVATE_BLOCK_2;
492 break;
493 default:
494 ret = HWPDODefaultFunc(type, hdo, cmd, argp, argv);
495 break;
497 return ret;
501 // rectangle
503 static int
504 HWPDORectFunc(int type, HWPDrawingObject * hdo, int cmd, void *argp, int argv)
506 return HWPDODefaultFunc(type, hdo, cmd, argp, argv);
510 // ellipse
512 static int
513 HWPDOEllipseFunc(int type, HWPDrawingObject * hdo,
514 int cmd, void *argp, int argv)
516 return HWPDODefaultFunc(type, hdo, cmd, argp, argv);
519 #define WTMM(x) ((double)(x) / 1800. * 25.4)
520 static int
521 HWPDOEllipse2Func(int type, HWPDrawingObject * hdo,
522 int cmd, void *argp, int argv)
524 switch (cmd)
526 case OBJFUNC_LOAD:
527 if (ReadSizeField(16) < 16)
528 return OBJRET_FILE_ERROR;
529 if (!hmem->read4b(hdo->u.arc.radial[0].x))
530 return OBJRET_FILE_ERROR;
531 if (!hmem->read4b(hdo->u.arc.radial[0].y))
532 return OBJRET_FILE_ERROR;
533 if (!hmem->read4b(hdo->u.arc.radial[1].x))
534 return OBJRET_FILE_ERROR;
535 if (!hmem->read4b(hdo->u.arc.radial[1].y))
536 return OBJRET_FILE_ERROR;
537 if (ReadSizeField(0) < 0)
538 return OBJRET_FILE_ERROR;
539 break;
540 default:
541 return HWPDODefaultFunc(type, hdo, cmd, argp, argv);
543 return OBJRET_FILE_OK;
547 // arc
549 static int
550 HWPDOArcFunc(int type, HWPDrawingObject * hdo, int cmd, void *argp, int argv)
552 switch (cmd)
554 case OBJFUNC_LOAD:
555 if (ReadSizeField(4) < 4)
556 return OBJRET_FILE_ERROR;
557 if (!hmem->read4b(hdo->u.line_arc.flip))
558 return OBJRET_FILE_ERROR;
559 if (hmem->state())
560 return OBJRET_FILE_ERROR;
561 if (!SkipUnusedField())
562 return OBJRET_FILE_ERROR;
563 break;
564 default:
565 return HWPDODefaultFunc(type, hdo, cmd, argp, argv);
567 return OBJRET_FILE_OK;
571 static int
572 HWPDOArc2Func(int type, HWPDrawingObject * hdo, int cmd, void *argp, int argv)
574 int ret = OBJRET_FILE_OK;
575 switch (cmd)
577 case OBJFUNC_LOAD:
578 ret = OBJRET_FILE_NO_PRIVATE_BLOCK;
579 break;
580 default:
581 ret = HWPDODefaultFunc(type, hdo, cmd, argp, argv);
582 break;
584 return ret;
588 static int
589 HWPDOFreeFormFunc(int type, HWPDrawingObject * hdo,
590 int cmd, void *argp, int argv)
592 switch (cmd)
594 case OBJFUNC_LOAD:
596 hdo->u.freeform.pt = 0;
597 if (ReadSizeField(4) < 4)
598 return OBJRET_FILE_ERROR;
599 if (!hmem->read4b(hdo->u.freeform.npt))
600 return OBJRET_FILE_ERROR;
601 if (hmem->state())
602 return OBJRET_FILE_ERROR;
603 if (!SkipUnusedField())
604 return OBJRET_FILE_ERROR;
606 int size = hdo->u.freeform.npt * sizeof(ZZPoint);
608 if (ReadSizeField(size) < size)
609 return OBJRET_FILE_ERROR;
610 if (hdo->u.freeform.npt)
612 hdo->u.freeform.pt =
613 ::comphelper::newArray_null<ZZPoint>(hdo->u.freeform.npt);
614 if (hdo->u.freeform.pt == NULL)
616 hdo->u.freeform.npt = 0;
617 return OBJRET_FILE_ERROR;
619 for (int ii = 0; ii < hdo->u.freeform.npt; ++ii)
621 bool bFailure = false;
622 if (!hmem->read4b(hdo->u.freeform.pt[ii].x))
623 bFailure = true;
624 if (!hmem->read4b(hdo->u.freeform.pt[ii].y))
625 bFailure = true;
626 if (hmem->state())
627 bFailure = true;
628 if (bFailure)
630 delete[]hdo->u.freeform.pt;
631 hdo->u.freeform.npt = 0;
632 return OBJRET_FILE_ERROR;
636 if (!SkipUnusedField())
637 return OBJRET_FILE_ERROR;
638 return OBJRET_FILE_OK;
640 case OBJFUNC_FREE:
641 if (hdo->u.freeform.pt)
642 delete[]hdo->u.freeform.pt;
643 break;
644 default:
645 return HWPDODefaultFunc(type, hdo, cmd, argp, argv);
647 return OBJRET_FILE_OK;
651 // text box
653 static void FreeParaList(HWPPara * para)
655 if (para->Next())
656 FreeParaList(para->Next());
657 delete para;
661 static HWPPara *LoadParaList()
663 if (!hmem)
664 return 0;
666 HWPFile *hwpf = GetCurrentDoc();
667 HIODev *hio = hwpf->SetIODevice(hmem);
669 std::list < HWPPara* > plist;
671 hwpf->ReadParaList(plist);
672 hwpf->SetIODevice(hio);
674 return plist.size()? plist.front() : 0;
678 static int
679 HWPDOTextBoxFunc(int type, HWPDrawingObject * hdo,
680 int cmd, void *argp, int argv)
682 switch (cmd)
684 case OBJFUNC_LOAD:
685 if (ReadSizeField(0) < 0 || !SkipUnusedField())
686 return OBJRET_FILE_ERROR;
687 if (ReadSizeField(0) < 0)
688 return OBJRET_FILE_ERROR;
689 hdo->u.textbox.h = LoadParaList();
690 return hdo->u.textbox.h ? OBJRET_FILE_OK : OBJRET_FILE_ERROR;
691 case OBJFUNC_FREE:
692 if (hdo->u.textbox.h)
694 FreeParaList(hdo->u.textbox.h);
695 hdo->u.textbox.h = NULL;
697 break;
698 default:
699 return HWPDODefaultFunc(type, hdo, cmd, argp, argv);
701 return OBJRET_FILE_OK;
706 static int
707 HWPDOContainerFunc(int type, HWPDrawingObject * hdo,
708 int cmd, void *argp, int argv)
710 return HWPDODefaultFunc(type, hdo, cmd, argp, argv);
714 HWPDrawingObject::HWPDrawingObject()
716 memset(this, 0, sizeof(HWPDrawingObject));
717 index = ++count;
721 HWPDrawingObject::~HWPDrawingObject()
723 if (child)
724 delete child;
726 if (next)
727 delete next;
729 HWPDOFunc(this, OBJFUNC_FREE, NULL, 0);
731 #endif
733 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */