CppunitTest_sc_tiledrendering2: move to tiledrendering folder
[LibreOffice.git] / hwpfilter / source / hbox.cxx
blob855b73e25c6cc3d8e833c339997d5f0a26361c4e
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 #include "precompile.h"
23 #include "hwpfile.h"
24 #include "hbox.h"
25 #include "hpara.h"
26 #include "hutil.h"
27 #include "htags.h"
28 #include "drawdef.h"
29 #include "hcode.h"
30 #include "datecode.h"
32 #include <o3tl/sprintf.hxx>
33 #include <rtl/character.hxx>
34 #include <rtl/strbuf.hxx>
35 #include <rtl/string.hxx>
37 int HBox::boxCount = 0;
39 HBox::HBox(hchar hch)
41 hh = hch;
42 boxCount++;
46 HBox::~HBox()
48 boxCount--;
52 int HBox::WSize()
54 static const int wsize[32] =
56 1, 4, 4, 4, 4, 4, 4, 42, /* dateform */
57 48, 4, 4, 4, 4, 1, 4, 4, /* hidden */
58 4, 4, 4, 4, 4, 4, 12, 5, /* chcompose */
59 3, 3, 123, 4, 32, 4, 2, 2
62 if (hh < 32)
63 return wsize[hh];
64 else
65 return 1;
69 // skip block
70 SkipData::SkipData(hchar hch)
71 : HBox(hch)
75 SkipData::~SkipData()
80 // FieldCode [5]
81 FieldCode::FieldCode()
82 : HBox(CH_FIELD)
83 , location_info(0)
87 FieldCode::~FieldCode()
91 // book mark(6)
92 Bookmark::Bookmark()
93 : HBox(CH_BOOKMARK)
94 , dummy(0)
95 , type(0)
99 Bookmark::~Bookmark()
103 // date format(7)
104 DateFormat::DateFormat()
105 : HBox(CH_DATE_FORM)
106 , dummy(0)
110 // date code(8)
111 DateCode::DateCode()
112 : HBox(CH_DATE_CODE)
113 , format{0}
114 , date{0}
115 , dummy(0)
116 , key(0)
120 const hchar kor_week[] =
122 0xB7A9, 0xB6A9, 0xD1C1, 0xAE81, 0xA1A2, 0x8B71, 0xC9A1
124 const hchar china_week[] =
126 0x4CC8, 0x4BE4, 0x525A, 0x48D8, 0x45AB, 0x4270, 0x50B4
128 const char eng_week[] = { "SunMonTueWedThuFriSat" };
129 const char eng_mon[] = { "JanFebMarAprMayJunJulAugSepOctNovDec" };
130 const char * const en_mon[] =
132 "January", "February", "March", "April", "May", "June", "July",
133 "August", "September", "October", "November", "December"
135 const char * const en_week[] =
137 "Sunday", "Monday", "Tuesday", "Wednesday",
138 "Thursday", "Friday", "Saturday"
141 hchar_string DateCode::GetString()
143 hchar_string ret;
144 const hchar *fmt;
145 int i, num;
146 const char *form;
147 char cbuf[256];
148 bool is_pm, add_zero;
150 add_zero = false;
151 format[DATE_SIZE - 1] = 0;
152 fmt = format[0] ? format : defaultform;
154 for (; *fmt && (ret.size() < DATE_SIZE); fmt++)
156 form = add_zero ? "%02d" : "%d";
158 add_zero = false;
159 is_pm = (date[HOUR] >= 12);
160 *cbuf = 0;
161 num = -1;
163 switch (*fmt)
165 case '0':
166 add_zero = true;
167 break;
168 case '1':
169 num = date[YEAR];
170 form = "%04d";
171 break;
172 case '!':
173 num = date[YEAR] % 100;
174 break;
175 case '2':
176 num = date[MONTH];
177 break;
178 case '@':
180 static_assert((std::size(eng_mon) - 1) / 3 == 12);
181 size_t nIndex = o3tl::make_unsigned(date[MONTH] - 1) % 12;
182 memcpy(cbuf, eng_mon + nIndex * 3, 3);
183 cbuf[3] = '.';
184 cbuf[4] = 0;
185 break;
187 case '*':
189 size_t nIndex = o3tl::make_unsigned(date[MONTH] - 1) % std::size(en_mon);
190 strncat(cbuf, en_mon[nIndex], sizeof(cbuf) - strlen(cbuf) - 1);
191 break;
193 case '3': /* 'D' is day of korean */
194 num = date[DAY];
195 break;
196 case '#':
197 num = date[DAY];
198 switch (date[DAY] % 10)
200 case 1:
201 form = "%dst";
202 break;
203 case 2:
204 form = "%dnd";
205 break;
206 case 3:
207 form = "%drd";
208 break;
209 default:
210 form = "%dth";
211 break;
213 break;
214 case '4':
215 num = date[HOUR] - ((date[HOUR] > 12) ? 12 : 0);
216 break;
217 case '$':
218 num = date[HOUR];
219 break;
220 case '5':
221 case '%':
222 num = date[MIN];
223 break;
224 case '6':
226 size_t nIndex = o3tl::make_unsigned(date[WEEK]) % std::size(kor_week);
227 ret.push_back(kor_week[nIndex]);
228 break;
230 case '^':
232 static_assert((std::size(eng_week) - 1) / 3 == 7);
233 size_t nIndex = o3tl::make_unsigned(date[WEEK]) % 7;
234 memcpy(cbuf, eng_week + nIndex * 3, 3);
235 cbuf[3] = '.';
236 cbuf[4] = 0;
237 break;
239 case '_':
241 size_t nIndex = o3tl::make_unsigned(date[WEEK]) % std::size(en_week);
242 strncat(cbuf, en_week[nIndex], sizeof(cbuf) - strlen(cbuf) - 1);
243 break;
245 case '7':
246 ret.push_back(0xB5A1);
247 ret.push_back(is_pm ? 0xD281 : 0xB8E5);
248 break;
249 case '&':
250 strncat(cbuf, is_pm ? "p.m." : "a.m.", sizeof(cbuf) - strlen(cbuf) - 1);
251 break;
252 case '+':
253 strncat(cbuf, is_pm ? "P.M." : "A.M.", sizeof(cbuf) - strlen(cbuf) - 1);
254 break;
255 case '8': // 2.5 feature
256 case '9':
257 #if 0
258 // LATER
259 mkcurfilename(cbuf, *fmt);
260 for (i = 0; cbuf[i] != 0 && slen > 1; i++)
261 { //for hangle filename
262 if (cbuf[i] & 0x80 && cbuf[i + 1] != 0)
264 *d++ = (cbuf[i] << 8) | cbuf[i + 1];
265 i++;
267 else
268 *d++ = cbuf[i];
269 slen--;
271 #endif
272 cbuf[0] = 0;
273 break;
274 case '~': // 3.0b feature
275 if (fmt[1] == 0)
276 break;
277 fmt++;
278 if (*fmt == '6')
280 size_t nIndex = o3tl::make_unsigned(date[WEEK]) % std::size(china_week);
281 ret.push_back(china_week[nIndex]);
282 break;
284 break;
285 default:
286 if (*fmt == '\\' && *++fmt == 0)
287 goto done;
288 ret.push_back(*fmt);
290 if (num != -1)
291 o3tl::sprintf(cbuf, form, num);
292 for (i = 0; 0 != cbuf[i]; i++)
294 ret.push_back(*(cbuf + i));
297 done:
298 return ret;
301 // tab(9)
302 Tab::Tab()
303 : HBox(CH_TAB)
304 , width(0)
305 , leader(0)
306 , dummy(0)
310 // floating box
311 FBox::FBox(hchar hch)
312 : HBox(hch)
313 , zorder(0)
314 , option(0)
315 , ctrl_ch(0)
316 , box_xs(0)
317 , box_ys(0)
318 , cap_xs(0)
319 , cap_ys(0)
320 , xs(0)
321 , ys(0)
322 , cap_margin(0)
323 , xpos_type(0)
324 , ypos_type(0)
325 , smart_linesp(0)
326 , boundsy(0)
327 , boundey(0)
328 , boundx(0)
329 , draw(0)
330 , pgx(0)
331 , pgy(0)
332 , pgno(0)
333 , showpg(0)
337 FBox::~FBox()
341 // tbox(10) TABLE BOX MATH BUTTON HYPERTEXT
342 TxtBox::TxtBox()
343 : FBox(CH_TEXT_BOX)
344 , dummy(0)
345 , dummy1(0)
346 , cap_len(0)
347 , next_box(0)
348 , dummy2(0)
349 , reserved1(0)
350 , cap_pos(0)
351 , num(0)
352 , dummy3(0)
353 , baseline(0)
354 , type(0)
355 , nCell(0)
356 , protect(0)
357 , m_pTable(nullptr)
359 reserved[0] = reserved[1] = 0;
362 TxtBox::~TxtBox()
366 // picture(11)
368 Picture::Picture()
369 : FBox(CH_PICTURE)
370 , reserved{0}
371 , dummy(0)
372 , follow_block_size(0)
373 , dummy1(0)
374 , dummy2(0)
375 , reserved1(0)
376 , cap_pos(0)
377 , num(0)
378 , pictype(0)
379 , skip{0}
380 , scale{0}
381 , picinfo{}
382 , reserved3{0}
383 , ishyper(false)
387 Picture::~Picture()
389 if (pictype == PICTYPE_DRAW)
390 delete picinfo.picdraw.hdo;
394 // line(14)
395 // hidden(15)
396 Hidden::~Hidden()
401 // header/footer(16)
402 HeaderFooter::~HeaderFooter()
407 // footnote(17)
408 Footnote::~Footnote()
413 // auto number(18)
414 // new number(19)
415 // show page number (20)
416 // Start/Hide odd-numbered side (21)
418 // mail merge(22)
419 hchar_string MailMerge::GetString()
421 return hchar_string();
425 // character composition(23)
426 // hyphen(24)
427 // toc mark(25)
428 // index mark(26)
429 // outline(28)
431 #define OL_HANGL_JASO 0
432 #define OL_HANGL_KANATA 1
434 static hchar olHanglJaso(unsigned int num, int type)
436 static const unsigned char han_init[] =
437 { 0x88, 0x90, 0x94, 0x9c, 0xa0, 0xa4, 0xac, 0xb4, 0xb8, 0xc0, 0xc4, 0xc8, 0xcc, 0xd0 };
438 static const unsigned char jung[] = { 3, 5, 7, 11, 13, 19, 20, 26, 27, 29, 30 };
439 static const unsigned char jung2[] = { 3, 7, 13, 20, 27, 29, 30 };
441 hchar hh = 0;
443 if (type == OL_HANGL_JASO)
445 num = num % (14 + SAL_N_ELEMENTS(jung));
447 if (num < 14)
448 hh = (han_init[num] << 8) | 'A';
449 else
450 hh = (jung[num - 14] << 5) | 0x8401;
452 else
454 if (num < 14)
455 hh = (han_init[num] << 8) | 'a';
456 else
458 int j = (num / 14) % SAL_N_ELEMENTS(jung2);
460 num = num % 14;
461 hh = (han_init[num] << 8) | (jung2[j] << 5) | 1;
464 return hh;
468 static const hchar *GetOutlineStyleChars(int style)
470 static const hchar out_bul_style_entry[5][MAX_OUTLINE_LEVEL+1] = // extern
472 { // 0 OLSTY_BULLET1
473 0x2f18, 0x2f12, 0x2f08, 0x2f02, 0x2f06, 0x2f00, 0x2043, 0x0000
475 { // 1
476 0x2f18, 0x2f12, 0x2f06, 0x2f00, 0x2f36, 0x2f30, 0x2043, 0x0000
478 { // 2
479 0x2f26, 0x2f20, 0x2f06, 0x2f00, 0x2f16, 0x2f10, 0x2043, 0x0000
481 { // 3
482 0x2f18, 0x2f16, 0x2f12, 0x2f10, 0x2f06, 0x2f00, 0x2043, 0x0000
485 0xAC61, 0xB677, 0xB861, 0xB8F7, 0xB781, 0x0000, 0x0000, 0x0000
488 if (style >= OLSTY_BULLET1 && style <= OLSTY_BULLET5)
489 return out_bul_style_entry[style - OLSTY_BULLET1];
490 return nullptr;
494 static void getOutlineNumStr(int style, int level, int num, hchar * hstr)
496 enum
498 U_ROM = 0x01, L_ROM = 0x02, U_ENG = 0x04, L_ENG = 0x08,
499 HAN = 0x10, NUM = 0x20, L_BR = 0x40, R_BR = 0x80
501 static const unsigned char type_tbl[][MAX_OUTLINE_LEVEL] =
504 U_ROM, HAN, NUM, HAN | R_BR, L_BR | NUM | R_BR,
505 L_BR | HAN | R_BR, L_ROM | R_BR
508 U_ROM, U_ENG, NUM, L_ENG | R_BR, L_BR | NUM | R_BR,
509 L_BR | L_ENG | R_BR, L_ROM | R_BR
512 NUM, HAN, L_BR | NUM | R_BR, L_BR | HAN | R_BR, NUM |
513 R_BR, HAN | R_BR, L_ENG
516 char fmt = type_tbl[style - OLSTY_NUMSIG1][level];
517 char buf[80], *ptr;
519 if (num < 1)
520 num = 1;
521 if (fmt & L_BR)
522 *hstr++ = '(';
523 if (fmt & NUM)
525 auto const numbuf = OString::number(num);
526 str2hstr(numbuf.buf, hstr);
527 hstr += numbuf.length;
529 else if (fmt & (U_ROM | L_ROM))
531 num2roman(num, buf);
532 if (fmt & U_ROM)
534 ptr = buf;
535 while (*ptr)
537 *ptr = sal::static_int_cast<char>(
538 rtl::toAsciiUpperCase(static_cast<unsigned char>(*ptr)));
539 ptr++;
542 str2hstr(buf, hstr);
543 hstr += strlen(buf);
545 else
547 num = (num - 1) % 26;
548 if (fmt & U_ENG)
549 *hstr++ = sal::static_int_cast<hchar>('A' + num);
550 else if (fmt & L_ENG)
551 *hstr++ = sal::static_int_cast<hchar>('a' + num);
552 else if (fmt & HAN)
553 *hstr++ = olHanglJaso(num, OL_HANGL_KANATA);
555 *hstr++ = (fmt & R_BR) ? ')' : '.';
556 *hstr = 0;
560 enum
561 { OUTLINE_ON, OUTLINE_NUM };
563 /* level starts from zero. ex) '1.1.1.' is the level 2.
564 number has the value. ex) '1.2.1' has '1,2,1'
565 style has the value which starts from 1 according to the definition in hbox.h
567 OUString Outline::GetUnicode() const
569 const hchar *p;
570 hchar buffer[255];
572 buffer[0] = 0;
573 if (kind == OUTLINE_NUM)
575 int levelnum;
576 switch (shape)
578 case OLSTY_NUMS1:
579 case OLSTY_NUMS2:
581 OStringBuffer buf;
582 for (unsigned int i = 0; i <= level; ++i)
584 if (i >= std::size(number))
585 break;
587 levelnum = ((number[i] < 1) ? 1 : number[i]);
588 buf.append(OString::number(levelnum));
589 if (!(shape == OLSTY_NUMS2 && i && i == level))
590 buf.append('.');
592 str2hstr(buf.getStr(), buffer);
593 return hstr2OUString(buffer);
595 case OLSTY_NUMSIG1:
596 case OLSTY_NUMSIG2:
597 case OLSTY_NUMSIG3:
599 if (level < std::size(number))
600 getOutlineNumStr(shape, level, number[level], buffer);
601 return hstr2OUString(buffer);
603 case OLSTY_BULLET1:
604 case OLSTY_BULLET2:
605 case OLSTY_BULLET3:
606 case OLSTY_BULLET4:
607 case OLSTY_BULLET5:
609 if (level < MAX_OUTLINE_LEVEL)
611 p = GetOutlineStyleChars(shape);
612 buffer[0] = p[level];
613 buffer[1] = 0;
615 return hstr2OUString(buffer);
617 case OLSTY_USER:
618 case OLSTY_BULUSER:
620 char dest[80];
621 int l = 0;
622 unsigned i = level;
623 if (i < std::size(deco) && deco[i][0]) {
624 buffer[l++] = deco[i][0];
626 if (i < std::size(user_shape))
628 /* level starts from zero. ex) '1.1.1.' is the level 2.
629 number has the value. ex) '1.2.1' has '1,2,1'
630 style has the value which starts from 1 according to the definition in hbox.h */
631 switch( user_shape[i] )
633 case 0:
634 buffer[l++] = '1' + number[i] - 1;
635 break;
636 case 1: /* Uppercase Roman */
637 case 2: /* Lowercase Roman */
638 num2roman(number[i], dest);
639 if( user_shape[i] == 1 ){
640 char *ptr = dest;
641 while( *ptr )
643 *ptr = sal::static_int_cast<char>(rtl::toAsciiUpperCase(static_cast<unsigned char>(*ptr)));
644 ptr++;
647 str2hstr(dest, buffer + l);
648 l += strlen(dest);
649 break;
650 case 3:
651 buffer[l++] = 'A' + number[i] -1;
652 break;
653 case 4:
654 buffer[l++] = 'a' + number[i] -1;
655 break;
656 case 5:
657 buffer[l++] = olHanglJaso(number[i] -1, OL_HANGL_KANATA);
658 break;
659 case 6:
660 buffer[l++] = olHanglJaso(number[i] -1, OL_HANGL_JASO);
661 break;
662 case 7: /* Chinese numbers: the number represented by the general */
663 buffer[l++] = '1' + number[i] -1;
664 break;
665 case 8: /* Circled numbers */
666 buffer[l++] = 0x2e00 + number[i];
667 break;
668 case 9: /* Circled lowercase alphabet */
669 buffer[l++] = 0x2c20 + number[i];
670 break;
671 case 10: /* Circled Korean Alphabet */
672 buffer[l++] = 0x2c50 + number[i] -1;
673 break;
674 case 11: /* Circled Korean Characters */
675 buffer[l++] = 0x2c40 + number[i] -1;
676 break;
677 case 12: /* Sequenced numbers. */
679 OStringBuffer buf;
680 int j;
681 for (j = 0; j <= level; j++)
683 levelnum = ((number[j] < 1) ? 1 : number[j]);
684 buf.append(OString::number(levelnum));
685 if (!((j && j == level) || (j == level && deco[i][1])))
686 buf.append('.');
688 str2hstr(buf.getStr(), buffer + l);
689 l += buf.getLength();
690 break;
692 default:
693 buffer[l++] = user_shape[i];
694 break;
697 if (i < std::size(deco) && deco[i][1]) {
698 buffer[l++] = deco[i][1];
700 buffer[l] = 0;
701 return hstr2OUString(buffer);
705 return hstr2OUString(buffer);
709 /* Bundle of spaces (30) */
710 /* Fixed-width spaces (31) */
712 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */