| 1 | |||
| 2 | package App::Asciio::stripes::ellipse ; | ||
| 3 | use base App::Asciio::stripes::stripes ; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use utf8 ; | ||
| 8 | |||
| 9 | use List::Util qw(max) ; | ||
| 10 | use Readonly ; | ||
| 11 | |||
| 12 | Readonly my $DEFAULT_BOX_TYPE => | ||
| 13 | [ | ||
| 14 | #~ default bottom low middle high fix single | ||
| 15 | [1, 'up-center-point', '-', '', '', '', '', '_', '', 1, ], | ||
| 16 | [1, 'down-center-point', '.', '', '', '', '', '\'', '-', 1, ], | ||
| 17 | [1, 'left-center-point', '|', '', '', '', '', '(', '', 1, ], | ||
| 18 | [1, 'rigth-center-point', '|', '', '', '', '', ')', '', 1, ], | ||
| 19 | [1, 'left-up-area', '/', '_', '.', '-', '\'', ':', '!', 1, ], | ||
| 20 | [1, 'right-up-area', '\\', '_', '.', '-', '\'', ':', '!', 1, ], | ||
| 21 | [1, 'left-down-area', '\\', '_', '.', '-', '\'', ':', '!', 1, ], | ||
| 22 | [1, 'right-down-area', '/', '_', '.', '-', '\'', ':', '!', 1, ], | ||
| 23 | [1, 'fill-character', ' ', '', '', '', '', '', '', 1, ], | ||
| 24 | |||
| 25 | ] ; | ||
| 26 | |||
| 27 | use App::Asciio::String ; | ||
| 28 | |||
| 29 | #----------------------------------------------------------------------------- | ||
| 30 | |||
| 31 | sub new | ||
| 32 | { | ||
| 33 | my ($class, $element_definition) = @_ ; | ||
| 34 | |||
| 35 | my $self = bless {}, __PACKAGE__ ; | ||
| 36 | |||
| 37 | $self->setup | ||
| 38 | ( | ||
| 39 | $element_definition->{TEXT_ONLY}, | ||
| 40 | $element_definition->{WIDTH} || 1, | ||
| 41 | $element_definition->{HEIGHT} || 1, | ||
| 42 | $element_definition->{EDITABLE}, | ||
| 43 | $element_definition->{RESIZABLE}, | ||
| 44 | $element_definition->{BOX_TYPE} || Clone::clone($DEFAULT_BOX_TYPE), | ||
| 45 | $element_definition->{AUTO_SHRINK}, | ||
| 46 | ) ; | ||
| 47 | |||
| 48 | return $self ; | ||
| 49 | } | ||
| 50 | |||
| 51 | #----------------------------------------------------------------------------- | ||
| 52 | |||
| 53 | sub fill_ellipse_strip | ||
| 54 | { | ||
| 55 | #~ https://enchantia.com/software/graphapp/doc/tech/ellipses.html | ||
| 56 | my ($xc, $yc, $a, $b) = @_; #~ e(x,y) = b^2*x^2 + a^2*y^2 - a^2*b^2 | ||
| 57 | my $x = 0; | ||
| 58 | my $y = $b; | ||
| 59 | my ($rx, $ry) = ($x, $y); | ||
| 60 | my ($width, $height) = (1, 1); | ||
| 61 | my ($a2, $b2) = ($a * $a, $b * $b); | ||
| 62 | my ($crit1, $crit2, $crit3) = (-(int($a2/4) + $a%2 + $b2), -(int($b2/4) + $b%2 + $a2), -(int($b2/4) + $b%2)); | ||
| 63 | my $t = -$a2 * $y; #~ e(x+1/2,y-1/2) - (a^2+b^2)/ | ||
| 64 | my ($dxt, $dyt) = (2 * $b2 * $x, -2 * $a2 * $y); | ||
| 65 | my ($d2xt, $d2yt) = (2 * $b2, 2 * $a2) ; | ||
| 66 | my @strips_arr; | ||
| 67 | |||
| 68 | if ($b == 0) { | ||
| 69 | push @strips_arr, [$xc-$a, $yc, 2*$a+1, 1]; | ||
| 70 | return @strips_arr; | ||
| 71 | } | ||
| 72 | |||
| 73 | while ($y>=0 && $x<=$a) | ||
| 74 | { | ||
| 75 | if ($t + $b2*$x <= $crit1 || #~ e(x+1,y-1/2) <= 0 | ||
| 76 | $t + $a2*$y <= $crit3) | ||
| 77 | { #~ e(x+1/2,y) <= 0 | ||
| 78 | if ($height == 1) | ||
| 79 | { | ||
| 80 | #~ draw nothing | ||
| 81 | } | ||
| 82 | elsif ($ry*2+1 > ($height-1)*2) { | ||
| 83 | push @strips_arr, [$xc-$rx, $yc-$ry, $width, $height-1]; | ||
| 84 | push @strips_arr, [$xc-$rx, $yc+$ry+1, $width, 1-$height]; | ||
| 85 | $ry -= $height-1; | ||
| 86 | $height = 1; | ||
| 87 | } | ||
| 88 | else | ||
| 89 | { | ||
| 90 | push @strips_arr, [$xc-$rx, $yc-$ry, $width, $ry*2+1]; | ||
| 91 | $ry -= $ry; | ||
| 92 | $height = 1; | ||
| 93 | } | ||
| 94 | $x++; | ||
| 95 | $dxt += $d2xt; | ||
| 96 | $t += $dxt; | ||
| 97 | $rx++; | ||
| 98 | $width += 2; | ||
| 99 | } | ||
| 100 | elsif ($t - $a2*$y > $crit2) { #~ e(x+1/2,y-1) > 0 | ||
| 101 | $y--; | ||
| 102 | $dyt += $d2yt; | ||
| 103 | $t += $dyt; | ||
| 104 | $height++; | ||
| 105 | } | ||
| 106 | else { | ||
| 107 | if ($ry*2+1 > $height*2) { | ||
| 108 | push @strips_arr, [$xc-$rx, $yc-$ry, $width, $height]; | ||
| 109 | push @strips_arr, [$xc-$rx, $yc+$ry+1, $width, -$height]; | ||
| 110 | } | ||
| 111 | else { | ||
| 112 | push @strips_arr, [$xc-$rx, $yc-$ry, $width, $ry*2+1]; | ||
| 113 | } | ||
| 114 | $x++; | ||
| 115 | $dxt += $d2xt; | ||
| 116 | $t += $dxt; | ||
| 117 | $y--; | ||
| 118 | $dyt += $d2yt; | ||
| 119 | $t += $dyt; | ||
| 120 | $rx++; | ||
| 121 | $width += 2; | ||
| 122 | $ry -= $height; | ||
| 123 | $height = 1; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | if ($ry > $height) | ||
| 128 | { | ||
| 129 | push @strips_arr, [$xc-$rx, $yc-$ry, $width, $height]; | ||
| 130 | push @strips_arr, [$xc-$rx, $yc+$ry+1, $width, -$height]; | ||
| 131 | } | ||
| 132 | else | ||
| 133 | { | ||
| 134 | push @strips_arr, [$xc-$rx, $yc-$ry, $width, $ry*2+1]; | ||
| 135 | } | ||
| 136 | return @strips_arr; | ||
| 137 | } | ||
| 138 | |||
| 139 | #----------------------------------------------------------------------------- | ||
| 140 | sub find_fit_ellipse | ||
| 141 | { | ||
| 142 | my ($ori_width, $ori_height, $text_width, $text_height) = @_; | ||
| 143 | my ($begin_width, $begin_height, $half_x, $half_y) = (0, 0, 0, 0); | ||
| 144 | my @rectangles; | ||
| 145 | my $rect_index; | ||
| 146 | my ($fit_width, $fit_height, $text_begin_y, $text_begin_x) = (0, 0, 0, 0); | ||
| 147 | my $find_flag = 0; | ||
| 148 | |||
| 149 | |||
| 150 | ($begin_width, $begin_height) = ($ori_width, $ori_height); | ||
| 151 | |||
| 152 | |||
| 153 | # Find a satisfactory rectangle from the starting ellipse (the width increases by 4 at a time, and the height increases by 2 at a time) | ||
| 154 | $begin_width++ if ($begin_width % 2 == 0); | ||
| 155 | $begin_height++ if ($begin_height % 2 == 0); | ||
| 156 | |||
| 157 | for(;;) | ||
| 158 | { | ||
| 159 | ($half_x, $half_y) = (int($begin_width/2), int($begin_height/2)); | ||
| 160 | |||
| 161 | @rectangles = fill_ellipse_strip($half_x, $half_y, $half_x, $half_y); | ||
| 162 | |||
| 163 | #~ rectangle 0 rectangle 1 last center rectangle | ||
| 164 | #~ [3, 0, 5, 1], [3, 7, 5, -1], [1, 1, 9, 1], [1, 6, 9, -1], [0, 2, 11, 3] | ||
| 165 | for($rect_index = 2; $rect_index <= $#rectangles; $rect_index += 2) | ||
| 166 | { | ||
| 167 | $fit_width = $rectangles[$rect_index-2][2]; | ||
| 168 | $fit_height = 0; | ||
| 169 | if($rect_index == $#rectangles) | ||
| 170 | { | ||
| 171 | $fit_height = $rectangles[$rect_index][3]; | ||
| 172 | |||
| 173 | } | ||
| 174 | elsif($rect_index < $#rectangles) | ||
| 175 | { | ||
| 176 | $fit_height = $rectangles[$rect_index+1][1] - $rectangles[$rect_index][1]; | ||
| 177 | } | ||
| 178 | if($fit_width >= $text_width && $fit_height >= $text_height) | ||
| 179 | { | ||
| 180 | $find_flag = 1; | ||
| 181 | last; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | if($find_flag == 1) | ||
| 186 | { | ||
| 187 | last; | ||
| 188 | } | ||
| 189 | else | ||
| 190 | { | ||
| 191 | $begin_width += 4; | ||
| 192 | $begin_height += 2; | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | $text_begin_x = int(($begin_width - $text_width) / 2); | ||
| 197 | $text_begin_y = int(($begin_height - $text_height) / 2); | ||
| 198 | |||
| 199 | return ($begin_width, $begin_height, $text_begin_y, $text_begin_x); | ||
| 200 | } | ||
| 201 | |||
| 202 | |||
| 203 | #----------------------------------------------------------------------------- | ||
| 204 | |||
| 205 | sub setup | ||
| 206 | { | ||
| 207 | my ($self, $text_only, $end_x, $end_y, $editable, $resizable, $box_type, $auto_shrink) = @_; | ||
| 208 | Readonly my $mini_row => 3; | ||
| 209 | Readonly my $mini_col => 3; | ||
| 210 | |||
| 211 | my $fill_char = ' '; | ||
| 212 | |||
| 213 | if($box_type->[8][2]) | ||
| 214 | { | ||
| 215 | $fill_char = substr($box_type->[8][2], 0, 1); | ||
| 216 | } | ||
| 217 | |||
| 218 | ($end_x, $end_y) = (-5, -5) if $auto_shrink; | ||
| 219 | |||
| 220 | my $max_row = max($end_y, $mini_row); | ||
| 221 | my $max_col = max($end_x, $mini_col); | ||
| 222 | |||
| 223 | |||
| 224 | my @text_lines; | ||
| 225 | |||
| 226 | if($text_only) | ||
| 227 | { | ||
| 228 | @text_lines = split("\n", $text_only) ; | ||
| 229 | } | ||
| 230 | my $text_width = max(map {unicode_length $_} @text_lines); | ||
| 231 | my $text_height = @text_lines; | ||
| 232 | |||
| 233 | my ($text_begin_y, $text_begin_x); | ||
| 234 | if($text_only) | ||
| 235 | { | ||
| 236 | ($max_col, $max_row, $text_begin_y, $text_begin_x) = find_fit_ellipse($max_col, $max_row, $text_width, $text_height); | ||
| 237 | } | ||
| 238 | else | ||
| 239 | { | ||
| 240 | $max_col++ if ($max_col > $mini_col && ($max_col % 2) == 0); | ||
| 241 | $max_row++ if ($max_row > $mini_row && ($max_row % 2) == 0); | ||
| 242 | } | ||
| 243 | |||
| 244 | my ($half_x, $half_y) = (int($max_col/2), int($max_row/2)); | ||
| 245 | my @strips = fill_ellipse_strip($half_x, $half_y, $half_x, $half_y); | ||
| 246 | |||
| 247 | #~ | | ||
| 248 | #~ ------|--ooooo-----> | ||
| 249 | #~ |ooooooooo | ||
| 250 | #~ ooooooooooo | ||
| 251 | #~ ooooooooooo | ||
| 252 | #~ ooooooooooo | ||
| 253 | #~ |ooooooooo | ||
| 254 | #~ | ooooo | ||
| 255 | #~ v | ||
| 256 | #~ | | ||
| 257 | #~ ------|--ooooo-----> | ||
| 258 | #~ |oo oo | ||
| 259 | #~ o o | ||
| 260 | #~ o o | ||
| 261 | #~ o o | ||
| 262 | #~ |oo oo | ||
| 263 | #~ | ooooo | ||
| 264 | #~ v | ||
| 265 | #~ 0 4 1 3 2 | ||
| 266 | #~ [3, 0, 5, 1], [3, 7, 5, -1], [1, 1, 9, 1], [1, 6, 9, -1], [0, 2, 11, 3] | ||
| 267 | #~ convert to | ||
| 268 | #~ [3, 0, 5, 1], [1, 1, 9, 1], [0, 2, 11, 3], [1, 5, 9, 1], [3, 6, 5, 1] | ||
| 269 | #~ convert to | ||
| 270 | #~ [3, 0, 5, 1], [1, 1, 9, 1], [0, 2, 11, 1], [0, 2, 11, 1], [0, 2, 11, 1], [1, 5, 9, 1], [3, 6, 5, 1] | ||
| 271 | |||
| 272 | my @new_strips; | ||
| 273 | my $cnt = 0; | ||
| 274 | for my $strip(@strips) | ||
| 275 | { | ||
| 276 | if($cnt % 2 == 0) | ||
| 277 | { | ||
| 278 | push @new_strips, $strip; | ||
| 279 | |||
| 280 | } | ||
| 281 | $cnt++; | ||
| 282 | } | ||
| 283 | $cnt = 0; | ||
| 284 | for my $strip(reverse(@strips)) | ||
| 285 | { | ||
| 286 | if($cnt % 2 != 0) | ||
| 287 | { | ||
| 288 | $strip->[1] = $strip->[1] + $strip->[3]; | ||
| 289 | $strip->[3] = abs($strip->[3]); | ||
| 290 | push @new_strips, $strip; | ||
| 291 | } | ||
| 292 | $cnt++; | ||
| 293 | } | ||
| 294 | |||
| 295 | my @sigle_strips; | ||
| 296 | my $index_remain = 1; | ||
| 297 | for my $strip(@new_strips) | ||
| 298 | { | ||
| 299 | $index_remain = $strip->[3]; | ||
| 300 | while($index_remain != 0) | ||
| 301 | { | ||
| 302 | push @sigle_strips, [$strip->[0], $strip->[2]]; | ||
| 303 | $strip->[3]--; | ||
| 304 | $index_remain = $strip->[3]; | ||
| 305 | } | ||
| 306 | |||
| 307 | } | ||
| 308 | my $resize_point_x; | ||
| 309 | if(3 == $max_row) | ||
| 310 | { | ||
| 311 | $resize_point_x = $sigle_strips[int($max_row/2)+1][1]; | ||
| 312 | } | ||
| 313 | else | ||
| 314 | { | ||
| 315 | $resize_point_x = $sigle_strips[int($max_row/2)+1][1] - 1; | ||
| 316 | } | ||
| 317 | |||
| 318 | my $element_width =$sigle_strips[int($max_row/2)][1]; | ||
| 319 | |||
| 320 | |||
| 321 | my $strip = $sigle_strips[0]; | ||
| 322 | |||
| 323 | my $diff_cnt = $strip->[1]; | ||
| 324 | my $strip_text; | ||
| 325 | my ($high_mark, $mid_mark, $low_mark, $bottom_mark) = (0, 0, 0, 0); | ||
| 326 | if($diff_cnt == 1) | ||
| 327 | { | ||
| 328 | $strip_text = $box_type->[0][7]; | ||
| 329 | } | ||
| 330 | elsif($diff_cnt == 3) | ||
| 331 | { | ||
| 332 | $strip_text = $box_type->[4][4] . $box_type->[0][2] . $box_type->[5][4]; | ||
| 333 | } | ||
| 334 | elsif($diff_cnt == 5) | ||
| 335 | { | ||
| 336 | $strip_text = $box_type->[4][4] . $box_type->[4][5] . $box_type->[0][2] . $box_type->[5][5] . $box_type->[5][4]; | ||
| 337 | } | ||
| 338 | else | ||
| 339 | { | ||
| 340 | my $remaining = $diff_cnt % 6; | ||
| 341 | $mid_mark = int($diff_cnt / 6) * 2; | ||
| 342 | $low_mark = int($diff_cnt / 6); | ||
| 343 | $bottom_mark = int($diff_cnt / 6); | ||
| 344 | if($remaining == 1) | ||
| 345 | { | ||
| 346 | $mid_mark += 1; | ||
| 347 | } | ||
| 348 | elsif($remaining == 2) | ||
| 349 | { | ||
| 350 | $mid_mark += 2; | ||
| 351 | } | ||
| 352 | elsif($remaining == 3) | ||
| 353 | { | ||
| 354 | $mid_mark += 1; | ||
| 355 | $low_mark += 1; | ||
| 356 | } | ||
| 357 | elsif($remaining == 4) | ||
| 358 | { | ||
| 359 | $mid_mark += 2; | ||
| 360 | $low_mark += 1; | ||
| 361 | } | ||
| 362 | elsif($remaining == 5) | ||
| 363 | { | ||
| 364 | $mid_mark += 1; | ||
| 365 | $low_mark += 2; | ||
| 366 | } | ||
| 367 | $strip_text = $box_type->[4][3] x $bottom_mark . $box_type->[4][4] x $low_mark . $box_type->[4][5] x int(($mid_mark - 1) / 2) . $box_type->[0][2] . $box_type->[5][5] x int(($mid_mark - 1) / 2) . $box_type->[5][4] x $low_mark . $box_type->[5][3] x $bottom_mark; | ||
| 368 | } | ||
| 369 | |||
| 370 | my @final_stripes; | ||
| 371 | |||
| 372 | push @final_stripes, | ||
| 373 | { | ||
| 374 | 'HEIGHT' => 1, | ||
| 375 | 'TEXT' => $strip_text, | ||
| 376 | 'WIDTH' => unicode_length($strip_text), | ||
| 377 | 'X_OFFSET' => $strip->[0], | ||
| 378 | 'Y_OFFSET' => 0, | ||
| 379 | }; | ||
| 380 | |||
| 381 | my $strip_index = 1; | ||
| 382 | my ($now_strip, $pre_strip, $padding); | ||
| 383 | my $strip_cnt = 0; | ||
| 384 | |||
| 385 | my ($fill_line, $fill_cnt, $fill_text); | ||
| 386 | |||
| 387 | for $strip_index(1..$#sigle_strips-1) | ||
| 388 | { | ||
| 389 | $fill_line = ''; | ||
| 390 | $padding = 0; | ||
| 391 | if(@text_lines) | ||
| 392 | { | ||
| 393 | if ($strip_index >= $text_begin_y) | ||
| 394 | { | ||
| 395 | $fill_line = shift @text_lines; | ||
| 396 | } | ||
| 397 | } | ||
| 398 | $fill_cnt = unicode_length($fill_line); | ||
| 399 | |||
| 400 | $now_strip = $sigle_strips[$strip_index][1]; | ||
| 401 | if($strip_index < $half_y) | ||
| 402 | { | ||
| 403 | $pre_strip = $sigle_strips[$strip_index-1][1]; | ||
| 404 | } | ||
| 405 | else | ||
| 406 | { | ||
| 407 | $pre_strip = $sigle_strips[$strip_index+1][1]; | ||
| 408 | } | ||
| 409 | $strip_cnt = abs($now_strip - $pre_strip); | ||
| 410 | if($strip_cnt == 0) | ||
| 411 | { | ||
| 412 | if($fill_line) | ||
| 413 | { | ||
| 414 | $padding = $text_begin_x - $sigle_strips[$strip_index][0] - 1; | ||
| 415 | $fill_text = $fill_char x $padding . $fill_line . $fill_char x ($now_strip - 2 - $padding - $fill_cnt); | ||
| 416 | } | ||
| 417 | else | ||
| 418 | { | ||
| 419 | $fill_text = $fill_char x ($now_strip - 2); | ||
| 420 | } | ||
| 421 | |||
| 422 | if($strip_index < $half_y) | ||
| 423 | { | ||
| 424 | $strip_text = $box_type->[4][8] . $fill_text . $box_type->[5][8]; | ||
| 425 | } | ||
| 426 | elsif($strip_index > $half_y) | ||
| 427 | { | ||
| 428 | $strip_text = $box_type->[6][8] . $fill_text . $box_type->[7][8]; | ||
| 429 | } | ||
| 430 | else | ||
| 431 | { | ||
| 432 | $strip_text = $box_type->[2][2] . $fill_text . $box_type->[3][2]; | ||
| 433 | } | ||
| 434 | } | ||
| 435 | elsif($strip_cnt == 2) | ||
| 436 | { | ||
| 437 | if($fill_line) | ||
| 438 | { | ||
| 439 | $padding = $text_begin_x - $sigle_strips[$strip_index][0] - 1; | ||
| 440 | $fill_text = $fill_char x $padding . $fill_line . $fill_char x ($now_strip - 2 - $padding - $fill_cnt); | ||
| 441 | } | ||
| 442 | else | ||
| 443 | { | ||
| 444 | $fill_text = $fill_char x ($now_strip - 2); | ||
| 445 | } | ||
| 446 | |||
| 447 | if($strip_index == $half_y) | ||
| 448 | { | ||
| 449 | if($max_row == 3 || ($max_row == 5 && $max_col > 5)) | ||
| 450 | { | ||
| 451 | $strip_text = $box_type->[2][7] . $fill_text . $box_type->[3][7]; | ||
| 452 | } | ||
| 453 | else | ||
| 454 | { | ||
| 455 | $strip_text = $box_type->[2][2] . $fill_text . $box_type->[3][2]; | ||
| 456 | } | ||
| 457 | } | ||
| 458 | elsif($strip_index < $half_y) | ||
| 459 | { | ||
| 460 | $strip_text = $box_type->[4][2] . $fill_text . $box_type->[5][2]; | ||
| 461 | if(($sigle_strips[$strip_index][1] == $element_width) && | ||
| 462 | ($sigle_strips[$strip_index+1][1] == $element_width) && | ||
| 463 | ($sigle_strips[$strip_index-1][1] != $element_width)) | ||
| 464 | { | ||
| 465 | $strip_text = $box_type->[4][7] . $fill_text . $box_type->[5][7]; | ||
| 466 | } | ||
| 467 | } | ||
| 468 | else | ||
| 469 | { | ||
| 470 | $strip_text = $box_type->[6][2] . $fill_text . $box_type->[7][2]; | ||
| 471 | if(($sigle_strips[$strip_index][1] == $element_width) && | ||
| 472 | ($sigle_strips[$strip_index-1][1] == $element_width) && | ||
| 473 | ($sigle_strips[$strip_index+1][1] != $element_width)) | ||
| 474 | { | ||
| 475 | $strip_text = $box_type->[6][7] . $fill_text . $box_type->[7][7]; | ||
| 476 | } | ||
| 477 | } | ||
| 478 | } | ||
| 479 | elsif($strip_cnt == 4) | ||
| 480 | { | ||
| 481 | if($fill_line) | ||
| 482 | { | ||
| 483 | $padding = $text_begin_x - $sigle_strips[$strip_index][0] - 2; | ||
| 484 | $fill_text = $fill_char x $padding . $fill_line . $fill_char x ($now_strip - 4 - $padding - $fill_cnt); | ||
| 485 | } | ||
| 486 | else | ||
| 487 | { | ||
| 488 | $fill_text = $fill_char x ($now_strip - 4); | ||
| 489 | } | ||
| 490 | |||
| 491 | |||
| 492 | if($strip_index < $half_y) | ||
| 493 | { | ||
| 494 | $strip_text = $box_type->[4][4] . $box_type->[4][6] . $fill_text . $box_type->[5][6] . $box_type->[5][4]; | ||
| 495 | } | ||
| 496 | else | ||
| 497 | { | ||
| 498 | $strip_text = $box_type->[6][6] . $box_type->[6][4] . $fill_text . $box_type->[7][4] . $box_type->[7][6]; | ||
| 499 | } | ||
| 500 | } | ||
| 501 | elsif($strip_cnt == 6) | ||
| 502 | { | ||
| 503 | if($fill_line) | ||
| 504 | { | ||
| 505 | $padding = $text_begin_x - $sigle_strips[$strip_index][0] - 3; | ||
| 506 | $fill_text = $fill_char x $padding . $fill_line . $fill_char x ($now_strip - 6 - $padding - $fill_cnt); | ||
| 507 | } | ||
| 508 | else | ||
| 509 | { | ||
| 510 | $fill_text = $fill_char x ($now_strip - 6); | ||
| 511 | } | ||
| 512 | |||
| 513 | if($strip_index < $half_y) | ||
| 514 | { | ||
| 515 | $strip_text = $box_type->[4][4] . $box_type->[4][6] x 2 . $fill_text . $box_type->[5][6] x 2 . $box_type->[5][4]; | ||
| 516 | } | ||
| 517 | else | ||
| 518 | { | ||
| 519 | $strip_text = $box_type->[6][6] . $box_type->[6][4] x 2 . $fill_text . $box_type->[7][4] x 2 . $box_type->[7][6]; | ||
| 520 | } | ||
| 521 | } | ||
| 522 | else | ||
| 523 | { | ||
| 524 | my $remaining = $strip_cnt % 8; | ||
| 525 | my $left_mark = int($strip_cnt / 8); | ||
| 526 | my $mid_mark = int($strip_cnt / 8); | ||
| 527 | my $bottom_mark = int($strip_cnt / 8); | ||
| 528 | my $right_mark = int($strip_cnt / 8); | ||
| 529 | if($remaining == 2) | ||
| 530 | { | ||
| 531 | if($strip_index < $half_y) | ||
| 532 | { | ||
| 533 | $right_mark += 1; | ||
| 534 | } | ||
| 535 | else | ||
| 536 | { | ||
| 537 | $bottom_mark += 1; | ||
| 538 | } | ||
| 539 | } | ||
| 540 | elsif($remaining == 4) | ||
| 541 | { | ||
| 542 | if($strip_index < $half_y) | ||
| 543 | { | ||
| 544 | $right_mark += 1; | ||
| 545 | $left_mark += 1; | ||
| 546 | } | ||
| 547 | else | ||
| 548 | { | ||
| 549 | $bottom_mark += 1; | ||
| 550 | $mid_mark += 1; | ||
| 551 | } | ||
| 552 | |||
| 553 | } | ||
| 554 | elsif($remaining == 6) | ||
| 555 | { | ||
| 556 | if($strip_index < $half_y) | ||
| 557 | { | ||
| 558 | $right_mark += 1; | ||
| 559 | $left_mark += 2; | ||
| 560 | } | ||
| 561 | else | ||
| 562 | { | ||
| 563 | $bottom_mark += 1; | ||
| 564 | $mid_mark += 2; | ||
| 565 | } | ||
| 566 | |||
| 567 | } | ||
| 568 | |||
| 569 | if($fill_line) | ||
| 570 | { | ||
| 571 | $padding = $text_begin_x - $sigle_strips[$strip_index][0] - ($bottom_mark + $left_mark + $mid_mark + $right_mark); | ||
| 572 | $fill_text = $fill_char x $padding . $fill_line . $fill_char x ($now_strip - (2 * $left_mark + 2 * $mid_mark + 2 * $right_mark + 2 * $bottom_mark) - $padding - $fill_cnt); | ||
| 573 | } | ||
| 574 | else | ||
| 575 | { | ||
| 576 | $fill_text = $fill_char x ($now_strip - (2 * $left_mark + 2 * $mid_mark + 2 * $right_mark + 2 * $bottom_mark)); | ||
| 577 | } | ||
| 578 | if($strip_index < $half_y) | ||
| 579 | { | ||
| 580 | $strip_text = $box_type->[4][3] x $bottom_mark . $box_type->[4][4] x $left_mark . $box_type->[4][5] x $mid_mark . $box_type->[4][6] x $right_mark . $fill_text . $box_type->[5][6] x $right_mark . $box_type->[5][5] x $mid_mark . $box_type->[5][4] x $left_mark . $box_type->[5][3] x $bottom_mark; | ||
| 581 | } | ||
| 582 | else | ||
| 583 | { | ||
| 584 | $strip_text = $box_type->[6][6] x $right_mark . $box_type->[6][5] x $mid_mark . $box_type->[6][4] x $left_mark . $box_type->[6][3] x $bottom_mark . $fill_text . $box_type->[7][3] x $bottom_mark . $box_type->[7][4] x $left_mark . $box_type->[7][5] x $mid_mark . $box_type->[7][6] x $right_mark; | ||
| 585 | } | ||
| 586 | } | ||
| 587 | |||
| 588 | push @final_stripes, | ||
| 589 | { | ||
| 590 | 'HEIGHT' => 1, | ||
| 591 | 'TEXT' => $strip_text, | ||
| 592 | 'WIDTH' => unicode_length($strip_text), | ||
| 593 | 'X_OFFSET' => $sigle_strips[$strip_index][0], | ||
| 594 | 'Y_OFFSET' => $strip_index, | ||
| 595 | }; | ||
| 596 | } | ||
| 597 | |||
| 598 | $strip = $sigle_strips[$#sigle_strips]; | ||
| 599 | $diff_cnt = $strip->[1]; | ||
| 600 | if($diff_cnt == 1) | ||
| 601 | { | ||
| 602 | $strip_text = $box_type->[1][7]; | ||
| 603 | } | ||
| 604 | elsif($diff_cnt == 3) | ||
| 605 | { | ||
| 606 | if($max_row == 3 && $max_col == 5) | ||
| 607 | { | ||
| 608 | $strip_text = $box_type->[6][6] . $box_type->[1][8] . $box_type->[7][6]; | ||
| 609 | } | ||
| 610 | else | ||
| 611 | { | ||
| 612 | $strip_text = $box_type->[6][6] . $box_type->[1][2] . $box_type->[7][6]; | ||
| 613 | } | ||
| 614 | |||
| 615 | } | ||
| 616 | elsif($diff_cnt == 5) | ||
| 617 | { | ||
| 618 | $strip_text = $box_type->[6][6] . $box_type->[6][5] . $box_type->[1][8] . $box_type->[7][5] . $box_type->[7][6]; | ||
| 619 | } | ||
| 620 | else | ||
| 621 | { | ||
| 622 | my $remaining = $diff_cnt % 6; | ||
| 623 | $mid_mark = int($diff_cnt / 6); | ||
| 624 | $high_mark = int($diff_cnt / 6); | ||
| 625 | $low_mark = int($diff_cnt / 6) * 2; | ||
| 626 | if($remaining == 1) | ||
| 627 | { | ||
| 628 | $low_mark += 1; | ||
| 629 | } | ||
| 630 | elsif($remaining == 2) | ||
| 631 | { | ||
| 632 | $low_mark += 2; | ||
| 633 | } | ||
| 634 | elsif($remaining == 3) | ||
| 635 | { | ||
| 636 | $low_mark += 1; | ||
| 637 | $mid_mark += 1; | ||
| 638 | } | ||
| 639 | elsif($remaining == 4) | ||
| 640 | { | ||
| 641 | $low_mark += 2; | ||
| 642 | $mid_mark += 1; | ||
| 643 | } | ||
| 644 | elsif($remaining == 5) | ||
| 645 | { | ||
| 646 | $low_mark += 1; | ||
| 647 | $mid_mark += 2; | ||
| 648 | } | ||
| 649 | $strip_text = $box_type->[6][6] x $high_mark . $box_type->[6][5] x $mid_mark . $box_type->[6][4] x int(($low_mark - 1) / 2) . $box_type->[1][2] . $box_type->[7][4] x int(($low_mark - 1) / 2) . $box_type->[7][5] x $mid_mark . $box_type->[7][6] x $high_mark; | ||
| 650 | |||
| 651 | } | ||
| 652 | push @final_stripes, | ||
| 653 | { | ||
| 654 | 'HEIGHT' => 1, | ||
| 655 | 'TEXT' => $strip_text, | ||
| 656 | 'WIDTH' => unicode_length($strip_text), | ||
| 657 | 'X_OFFSET' => $strip->[0], | ||
| 658 | 'Y_OFFSET' => $#sigle_strips, | ||
| 659 | }; | ||
| 660 | |||
| 661 | # position to the center of the ellipse if text not found | ||
| 662 | $text_begin_x = int($element_width/2) unless(defined $text_begin_x); | ||
| 663 | $text_begin_y = int($max_row/2) unless(defined $text_begin_y); | ||
| 664 | |||
| 665 | $self->set | ||
| 666 | ( | ||
| 667 | STRIPES => \@final_stripes, | ||
| 668 | WIDTH => $element_width, | ||
| 669 | HEIGHT => $max_row, | ||
| 670 | LEFT_CENTER_X => -1, | ||
| 671 | RESIZE_POINT_X => $resize_point_x, | ||
| 672 | TEXT_ONLY => $text_only, | ||
| 673 | TEXT_BEGIN_X => $text_begin_x, | ||
| 674 | TEXT_BEGIN_Y => $text_begin_y, | ||
| 675 | EDITABLE => $editable, | ||
| 676 | RESIZABLE => $resizable, | ||
| 677 | BOX_TYPE => $box_type, | ||
| 678 | AUTO_SHRINK => $auto_shrink, | ||
| 679 | EXTENTS => [0, 0, $element_width, $max_row], | ||
| 680 | |||
| 681 | ); | ||
| 682 | } | ||
| 683 | |||
| 684 | #----------------------------------------------------------------------------- | ||
| 685 | |||
| 686 | sub get_selection_action | ||
| 687 | { | ||
| 688 | my ($self, $x, $y) = @_ ; | ||
| 689 | |||
| 690 | ($x == $self->{RESIZE_POINT_X} && $y == int($self->{HEIGHT}/2) + 1) | ||
| 691 | ? 'resize' | ||
| 692 | : 'move' ; | ||
| 693 | } | ||
| 694 | |||
| 695 | #----------------------------------------------------------------------------- | ||
| 696 | |||
| 697 | sub match_connector | ||
| 698 | { | ||
| 699 | my ($self, $x, $y) = @_ ; | ||
| 700 | |||
| 701 | my $middle_width = int($self->{WIDTH} / 2) ; | ||
| 702 | my $middle_height = int($self->{HEIGHT} / 2) ; | ||
| 703 | |||
| 704 | if($x == $middle_width && $y == -1) | ||
| 705 | { | ||
| 706 | return {X => $x, Y => $y, NAME => 'top_center'} ; | ||
| 707 | } | ||
| 708 | elsif($x == $middle_width && $y == $self->{HEIGHT}) | ||
| 709 | { | ||
| 710 | return {X => $x, Y => $y, NAME => 'bottom_center'} ; | ||
| 711 | } | ||
| 712 | if($x == $self->{LEFT_CENTER_X} && $y == $middle_height) | ||
| 713 | { | ||
| 714 | return {X => $x, Y => $y, NAME => 'left_center'} ; | ||
| 715 | } | ||
| 716 | elsif($x == $self->{WIDTH} && $y == $middle_height) | ||
| 717 | { | ||
| 718 | return {X => $x, Y => $y, NAME => 'right_center'} ; | ||
| 719 | } | ||
| 720 | elsif($x >= 0 && $x < $self->{WIDTH} && $y >= 0 && $y < $self->{HEIGHT}) | ||
| 721 | { | ||
| 722 | return {X => $middle_width, Y => -1, NAME => 'to_be_optimized'} ; | ||
| 723 | } | ||
| 724 | elsif($self->{ALLOW_BORDER_CONNECTION} && $x >= -1 && $x <= $self->{WIDTH} && $y >= -1 && $y <= $self->{HEIGHT}) | ||
| 725 | { | ||
| 726 | return {X => $x, Y => $y, NAME => 'border'} ; | ||
| 727 | } | ||
| 728 | else | ||
| 729 | { | ||
| 730 | return ; | ||
| 731 | } | ||
| 732 | } | ||
| 733 | |||
| 734 | #----------------------------------------------------------------------------- | ||
| 735 | |||
| 736 | sub get_connection_points | ||
| 737 | { | ||
| 738 | my ($self) = @_ ; | ||
| 739 | my $middle_width = int($self->{WIDTH} / 2) ; | ||
| 740 | my $middle_height = int($self->{HEIGHT} / 2) ; | ||
| 741 | |||
| 742 | return | ||
| 743 | ( | ||
| 744 | {X => $middle_width, Y => -1, NAME => 'top_center'}, | ||
| 745 | {X => $middle_width, Y => $self->{HEIGHT}, NAME => 'bottom_center'}, | ||
| 746 | {X => $self->{LEFT_CENTER_X}, Y => $middle_height, NAME => 'left_center'}, | ||
| 747 | {X => $self->{WIDTH}, Y => $middle_height, NAME => 'right_center'}, | ||
| 748 | ) ; | ||
| 749 | } | ||
| 750 | |||
| 751 | #----------------------------------------------------------------------------- | ||
| 752 | |||
| 753 | sub get_extra_points | ||
| 754 | { | ||
| 755 | my ($self) = @_ ; | ||
| 756 | return | ||
| 757 | ( | ||
| 758 | {X => $self->{RESIZE_POINT_X}, Y => int($self->{HEIGHT}/2) + 1 , NAME => 'resize'}, | ||
| 759 | ) ; | ||
| 760 | } | ||
| 761 | |||
| 762 | #----------------------------------------------------------------------------- | ||
| 763 | |||
| 764 | sub get_named_connection | ||
| 765 | { | ||
| 766 | my ($self, $name) = @_ ; | ||
| 767 | my $middle_width = int($self->{WIDTH} / 2) ; | ||
| 768 | my $middle_height = int($self->{HEIGHT} / 2) ; | ||
| 769 | |||
| 770 | if($name eq 'top_center') | ||
| 771 | { | ||
| 772 | return {X => $middle_width, Y => -1, NAME => 'top_center'} ; | ||
| 773 | } | ||
| 774 | elsif($name eq 'bottom_center') | ||
| 775 | { | ||
| 776 | return {X => $middle_width, Y => $self->{HEIGHT}, NAME => 'bottom_center'} ; | ||
| 777 | } | ||
| 778 | elsif($name eq 'left_center') | ||
| 779 | { | ||
| 780 | return {X => $self->{LEFT_CENTER_X}, Y => $middle_height, NAME => 'left_center'}, | ||
| 781 | } | ||
| 782 | elsif($name eq 'right_center') | ||
| 783 | { | ||
| 784 | return {X => $self->{WIDTH}, Y => $middle_height, NAME => 'right_center'}, | ||
| 785 | } | ||
| 786 | else | ||
| 787 | { | ||
| 788 | return ; | ||
| 789 | } | ||
| 790 | } | ||
| 791 | |||
| 792 | #----------------------------------------------------------------------------- | ||
| 793 | |||
| 794 | sub resize | ||
| 795 | { | ||
| 796 | my ($self, $reference_x, $reference_y, $new_x, $new_y) = @_ ; | ||
| 797 | |||
| 798 | my $new_end_x = $new_x ; | ||
| 799 | my $new_end_y = $new_y ; | ||
| 800 | |||
| 801 | if($reference_x == -1 && $reference_y == -1) | ||
| 802 | { | ||
| 803 | if($new_y == -1) | ||
| 804 | { | ||
| 805 | $new_y = -2; | ||
| 806 | } | ||
| 807 | if($new_x == -1) | ||
| 808 | { | ||
| 809 | $new_x = -2; | ||
| 810 | } | ||
| 811 | |||
| 812 | $self->setup | ||
| 813 | ( | ||
| 814 | $self->{TEXT_ONLY}, | ||
| 815 | $self->{WIDTH} + $new_x, | ||
| 816 | $self->{HEIGHT} + $new_y, | ||
| 817 | $self->{EDITABLE}, $self->{RESIZABLE}, | ||
| 818 | $self->{BOX_TYPE}, $self->{AUTO_SHRINK} | ||
| 819 | ) ; | ||
| 820 | } | ||
| 821 | else | ||
| 822 | { | ||
| 823 | if($new_end_x >= 0 && $new_end_y >= 0) | ||
| 824 | { | ||
| 825 | $self->setup | ||
| 826 | ( | ||
| 827 | $self->{TEXT_ONLY}, | ||
| 828 | $new_end_x + 1 - ($self->{WIDTH} - $self->{RESIZE_POINT_X}), # compensate for resize point X not equal to width | ||
| 829 | $new_end_y + 1, | ||
| 830 | $self->{EDITABLE}, $self->{RESIZABLE}, | ||
| 831 | $self->{BOX_TYPE}, $self->{AUTO_SHRINK} | ||
| 832 | ) ; | ||
| 833 | } | ||
| 834 | } | ||
| 835 | |||
| 836 | return(0, 0, $self->{WIDTH}, $self->{HEIGHT}) ; | ||
| 837 | } | ||
| 838 | |||
| 839 | #----------------------------------------------------------------------------- | ||
| 840 | |||
| 841 | sub get_text { my ($self) = @_ ; return($self->{TEXT_ONLY}) ; } | ||
| 842 | |||
| 843 | #----------------------------------------------------------------------------- | ||
| 844 | |||
| 845 | sub get_box_type { my ($self) = @_ ; return($self->{BOX_TYPE}) ; } | ||
| 846 | |||
| 847 | #----------------------------------------------------------------------------- | ||
| 848 | |||
| 849 | sub allow_border_connection { my($self, $allow) = @_ ; $self->{ALLOW_BORDER_CONNECTION} = $allow ; } | ||
| 850 | sub is_border_connection_allowed { my($self) = @_ ; return $self->{ALLOW_BORDER_CONNECTION} ; } | ||
| 851 | |||
| 852 | #----------------------------------------------------------------------------- | ||
| 853 | |||
| 854 | sub flip_auto_shrink { my($self) = @_ ; $self->{AUTO_SHRINK} ^= 1 ; } | ||
| 855 | |||
| 856 | #----------------------------------------------------------------------------- | ||
| 857 | |||
| 858 | sub is_auto_shrink { my($self) = @_ ; return $self->{AUTO_SHRINK} ; } | ||
| 859 | |||
| 860 | #----------------------------------------------------------------------------- | ||
| 861 | |||
| 862 | sub shrink | ||
| 863 | { | ||
| 864 | my ($self) = @_ ; | ||
| 865 | $self->setup | ||
| 866 | ( | ||
| 867 | $self->{TEXT_ONLY}, | ||
| 868 | -5, # magic number are ugly | ||
| 869 | -5, | ||
| 870 | $self->{EDITABLE}, $self->{RESIZABLE}, | ||
| 871 | $self->{BOX_TYPE}, $self->{AUTO_SHRINK} | ||
| 872 | ) ; | ||
| 873 | } | ||
| 874 | |||
| 875 | #----------------------------------------------------------------------------- | ||
| 876 | |||
| 877 | sub rotate_text | ||
| 878 | { | ||
| 879 | my ($self) = @_ ; | ||
| 880 | |||
| 881 | my $text = make_vertical_text($self->{TEXT_ONLY}) ; | ||
| 882 | |||
| 883 | $self->set_text($text) ; | ||
| 884 | $self->shrink() ; | ||
| 885 | |||
| 886 | $self->{VERTICAL_TEXT} ^= 1 ; | ||
| 887 | } | ||
| 888 | |||
| 889 | #----------------------------------------------------------------------------- | ||
| 890 | |||
| 891 | sub set_text | ||
| 892 | { | ||
| 893 | my ($self, $text) = @_ ; | ||
| 894 | $self->setup | ||
| 895 | ( | ||
| 896 | $text, | ||
| 897 | $self->{WIDTH}, | ||
| 898 | $self->{HEIGHT} - 1, | ||
| 899 | $self->{EDITABLE}, $self->{RESIZABLE}, | ||
| 900 | $self->{BOX_TYPE}, $self->{AUTO_SHRINK} | ||
| 901 | ) ; | ||
| 902 | } | ||
| 903 | |||
| 904 | #----------------------------------------------------------------------------- | ||
| 905 | sub set_box_type | ||
| 906 | { | ||
| 907 | my ($self, $box_type) = @_; | ||
| 908 | $self->setup | ||
| 909 | ( | ||
| 910 | $self->{TEXT_ONLY}, | ||
| 911 | $self->{WIDTH}, | ||
| 912 | $self->{HEIGHT} - 1, | ||
| 913 | $self->{EDITABLE}, $self->{RESIZABLE}, | ||
| 914 | $box_type, $self->{AUTO_SHRINK} | ||
| 915 | ) ; | ||
| 916 | } | ||
| 917 | |||
| 918 | #----------------------------------------------------------------------------- | ||
| 919 | |||
| 920 | sub edit | ||
| 921 | { | ||
| 922 | my ($self, $asciio) = @_ ; | ||
| 923 | |||
| 924 | return unless $self->{EDITABLE} ; | ||
| 925 | |||
| 926 | my $text_only = $self->{TEXT_ONLY} ; | ||
| 927 | $text_only = make_vertical_text($text_only) if $self->{VERTICAL_TEXT} ; | ||
| 928 | |||
| 929 | $text_only = $self->display_box_edit_dialog($text_only, '', $asciio, $self->{X}, $self->{Y}, $self->{TEXT_BEGIN_X}, $self->{TEXT_BEGIN_Y}) ; | ||
| 930 | |||
| 931 | my $tab_as_space = $asciio->{TAB_AS_SPACES} ; | ||
| 932 | $text_only =~ s/\t/$tab_as_space/g ; | ||
| 933 | |||
| 934 | $text_only = make_vertical_text($text_only) if $self->{VERTICAL_TEXT} ; | ||
| 935 | |||
| 936 | $self->set_text($text_only) ; | ||
| 937 | } | ||
| 938 | |||
| 939 | #----------------------------------------------------------------------------- | ||
| 940 | |||
| 941 | 1 ; |