| 1 | /* |
| 2 | * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. |
| 3 | |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | * of this software and associated documentation files (the "Software"), to deal |
| 6 | * in the Software without restriction, including without limitation the rights |
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | * copies of the Software, and to permit persons to whom the Software is |
| 9 | * furnished to do so, subject to the following conditions: |
| 10 | |
| 11 | * The above copyright notice and this permission notice shall be included in all |
| 12 | * copies or substantial portions of the Software. |
| 13 | |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | * SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | /* |
| 24 | * The FreeType Project LICENSE |
| 25 | * ---------------------------- |
| 26 | |
| 27 | * 2006-Jan-27 |
| 28 | |
| 29 | * Copyright 1996-2002, 2006 by |
| 30 | * David Turner, Robert Wilhelm, and Werner Lemberg |
| 31 | |
| 32 | |
| 33 | |
| 34 | * Introduction |
| 35 | * ============ |
| 36 | |
| 37 | * The FreeType Project is distributed in several archive packages; |
| 38 | * some of them may contain, in addition to the FreeType font engine, |
| 39 | * various tools and contributions which rely on, or relate to, the |
| 40 | * FreeType Project. |
| 41 | |
| 42 | * This license applies to all files found in such packages, and |
| 43 | * which do not fall under their own explicit license. The license |
| 44 | * affects thus the FreeType font engine, the test programs, |
| 45 | * documentation and makefiles, at the very least. |
| 46 | |
| 47 | * This license was inspired by the BSD, Artistic, and IJG |
| 48 | * (Independent JPEG Group) licenses, which all encourage inclusion |
| 49 | * and use of free software in commercial and freeware products |
| 50 | * alike. As a consequence, its main points are that: |
| 51 | |
| 52 | * o We don't promise that this software works. However, we will be |
| 53 | * interested in any kind of bug reports. (`as is' distribution) |
| 54 | |
| 55 | * o You can use this software for whatever you want, in parts or |
| 56 | * full form, without having to pay us. (`royalty-free' usage) |
| 57 | |
| 58 | * o You may not pretend that you wrote this software. If you use |
| 59 | * it, or only parts of it, in a program, you must acknowledge |
| 60 | * somewhere in your documentation that you have used the |
| 61 | * FreeType code. (`credits') |
| 62 | |
| 63 | * We specifically permit and encourage the inclusion of this |
| 64 | * software, with or without modifications, in commercial products. |
| 65 | * We disclaim all warranties covering The FreeType Project and |
| 66 | * assume no liability related to The FreeType Project. |
| 67 | |
| 68 | |
| 69 | * Finally, many people asked us for a preferred form for a |
| 70 | * credit/disclaimer to use in compliance with this license. We thus |
| 71 | * encourage you to use the following text: |
| 72 | |
| 73 | * """ |
| 74 | * Portions of this software are copyright � <year> The FreeType |
| 75 | * Project (www.freetype.org). All rights reserved. |
| 76 | * """ |
| 77 | |
| 78 | * Please replace <year> with the value from the FreeType version you |
| 79 | * actually use. |
| 80 | |
| 81 | * Legal Terms |
| 82 | * =========== |
| 83 | |
| 84 | * 0. Definitions |
| 85 | * -------------- |
| 86 | |
| 87 | * Throughout this license, the terms `package', `FreeType Project', |
| 88 | * and `FreeType archive' refer to the set of files originally |
| 89 | * distributed by the authors (David Turner, Robert Wilhelm, and |
| 90 | * Werner Lemberg) as the `FreeType Project', be they named as alpha, |
| 91 | * beta or final release. |
| 92 | |
| 93 | * `You' refers to the licensee, or person using the project, where |
| 94 | * `using' is a generic term including compiling the project's source |
| 95 | * code as well as linking it to form a `program' or `executable'. |
| 96 | * This program is referred to as `a program using the FreeType |
| 97 | * engine'. |
| 98 | |
| 99 | * This license applies to all files distributed in the original |
| 100 | * FreeType Project, including all source code, binaries and |
| 101 | * documentation, unless otherwise stated in the file in its |
| 102 | * original, unmodified form as distributed in the original archive. |
| 103 | * If you are unsure whether or not a particular file is covered by |
| 104 | * this license, you must contact us to verify this. |
| 105 | |
| 106 | * The FreeType Project is copyright (C) 1996-2000 by David Turner, |
| 107 | * Robert Wilhelm, and Werner Lemberg. All rights reserved except as |
| 108 | * specified below. |
| 109 | |
| 110 | * 1. No Warranty |
| 111 | * -------------- |
| 112 | |
| 113 | * THE FREETYPE PROJECT IS PROVIDED `AS IS' WITHOUT WARRANTY OF ANY |
| 114 | * KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, |
| 115 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 116 | * PURPOSE. IN NO EVENT WILL ANY OF THE AUTHORS OR COPYRIGHT HOLDERS |
| 117 | * BE LIABLE FOR ANY DAMAGES CAUSED BY THE USE OR THE INABILITY TO |
| 118 | * USE, OF THE FREETYPE PROJECT. |
| 119 | |
| 120 | * 2. Redistribution |
| 121 | * ----------------- |
| 122 | |
| 123 | * This license grants a worldwide, royalty-free, perpetual and |
| 124 | * irrevocable right and license to use, execute, perform, compile, |
| 125 | * display, copy, create derivative works of, distribute and |
| 126 | * sublicense the FreeType Project (in both source and object code |
| 127 | * forms) and derivative works thereof for any purpose; and to |
| 128 | * authorize others to exercise some or all of the rights granted |
| 129 | * herein, subject to the following conditions: |
| 130 | |
| 131 | * o Redistribution of source code must retain this license file |
| 132 | * (`FTL.TXT') unaltered; any additions, deletions or changes to |
| 133 | * the original files must be clearly indicated in accompanying |
| 134 | * documentation. The copyright notices of the unaltered, |
| 135 | * original files must be preserved in all copies of source |
| 136 | * files. |
| 137 | |
| 138 | * o Redistribution in binary form must provide a disclaimer that |
| 139 | * states that the software is based in part of the work of the |
| 140 | * FreeType Team, in the distribution documentation. We also |
| 141 | * encourage you to put an URL to the FreeType web page in your |
| 142 | * documentation, though this isn't mandatory. |
| 143 | |
| 144 | * These conditions apply to any software derived from or based on |
| 145 | * the FreeType Project, not just the unmodified files. If you use |
| 146 | * our work, you must acknowledge us. However, no fee need be paid |
| 147 | * to us. |
| 148 | |
| 149 | * 3. Advertising |
| 150 | * -------------- |
| 151 | |
| 152 | * Neither the FreeType authors and contributors nor you shall use |
| 153 | * the name of the other for commercial, advertising, or promotional |
| 154 | * purposes without specific prior written permission. |
| 155 | |
| 156 | * We suggest, but do not require, that you use one or more of the |
| 157 | * following phrases to refer to this software in your documentation |
| 158 | * or advertising materials: `FreeType Project', `FreeType Engine', |
| 159 | * `FreeType library', or `FreeType Distribution'. |
| 160 | |
| 161 | * As you have not signed this license, you are not required to |
| 162 | * accept it. However, as the FreeType Project is copyrighted |
| 163 | * material, only this license, or another one contracted with the |
| 164 | * authors, grants you the right to use, distribute, and modify it. |
| 165 | * Therefore, by using, distributing, or modifying the FreeType |
| 166 | * Project, you indicate that you understand and accept all the terms |
| 167 | * of this license. |
| 168 | |
| 169 | * 4. Contacts |
| 170 | * ----------- |
| 171 | |
| 172 | * There are two mailing lists related to FreeType: |
| 173 | |
| 174 | * o [email protected] |
| 175 | |
| 176 | * Discusses general use and applications of FreeType, as well as |
| 177 | * future and wanted additions to the library and distribution. |
| 178 | * If you are looking for support, start in this list if you |
| 179 | * haven't found anything to help you in the documentation. |
| 180 | |
| 181 | * o [email protected] |
| 182 | |
| 183 | * Discusses bugs, as well as engine internals, design issues, |
| 184 | * specific licenses, porting, etc. |
| 185 | |
| 186 | * Our home page can be found at |
| 187 | |
| 188 | * http://www.freetype.org |
| 189 | */ |
| 190 | |
| 191 | #include <limits.h> |
| 192 | #include <memory.h> |
| 193 | #include "tvgSwCommon.h" |
| 194 | |
| 195 | /************************************************************************/ |
| 196 | /* Internal Class Implementation */ |
| 197 | /************************************************************************/ |
| 198 | |
| 199 | constexpr auto PIXEL_BITS = 8; //must be at least 6 bits! |
| 200 | constexpr auto ONE_PIXEL = (1L << PIXEL_BITS); |
| 201 | |
| 202 | struct Band |
| 203 | { |
| 204 | SwCoord min, max; |
| 205 | }; |
| 206 | |
| 207 | struct RleWorker |
| 208 | { |
| 209 | SwRle* rle; |
| 210 | |
| 211 | SwPoint cellPos; |
| 212 | SwPoint cellMin; |
| 213 | SwPoint cellMax; |
| 214 | SwCoord cellXCnt; |
| 215 | SwCoord cellYCnt; |
| 216 | |
| 217 | Area area; |
| 218 | SwCoord cover; |
| 219 | |
| 220 | SwCell* cells; |
| 221 | ptrdiff_t maxCells; |
| 222 | ptrdiff_t cellsCnt; |
| 223 | |
| 224 | SwPoint pos; |
| 225 | |
| 226 | SwPoint bezStack[32 * 3 + 1]; |
| 227 | SwPoint lineStack[32 + 1]; |
| 228 | int levStack[32]; |
| 229 | |
| 230 | SwOutline* outline; |
| 231 | |
| 232 | int bandSize; |
| 233 | int bandShoot; |
| 234 | |
| 235 | SwCell* buffer; |
| 236 | uint32_t bufferSize; |
| 237 | |
| 238 | SwCell** yCells; |
| 239 | int32_t yCnt; |
| 240 | |
| 241 | bool invalid; |
| 242 | bool antiAlias; |
| 243 | }; |
| 244 | |
| 245 | |
| 246 | static inline SwPoint UPSCALE(const SwPoint& pt) |
| 247 | { |
| 248 | return {SwCoord(((unsigned long) pt.x) << (PIXEL_BITS - 6)), SwCoord(((unsigned long) pt.y) << (PIXEL_BITS - 6))}; |
| 249 | } |
| 250 | |
| 251 | |
| 252 | static inline SwPoint TRUNC(const SwPoint& pt) |
| 253 | { |
| 254 | return {pt.x >> PIXEL_BITS, pt.y >> PIXEL_BITS}; |
| 255 | } |
| 256 | |
| 257 | |
| 258 | static inline SwCoord TRUNC(const SwCoord x) |
| 259 | { |
| 260 | return x >> PIXEL_BITS; |
| 261 | } |
| 262 | |
| 263 | |
| 264 | static inline SwPoint SUBPIXELS(const SwPoint& pt) |
| 265 | { |
| 266 | return {SwCoord(((unsigned long) pt.x) << PIXEL_BITS), SwCoord(((unsigned long) pt.y) << PIXEL_BITS)}; |
| 267 | } |
| 268 | |
| 269 | |
| 270 | static inline SwCoord SUBPIXELS(const SwCoord x) |
| 271 | { |
| 272 | return SwCoord(((unsigned long) x) << PIXEL_BITS); |
| 273 | } |
| 274 | |
| 275 | |
| 276 | // Approximate sqrt(x*x+y*y) using the `alpha max plus beta min' algorithm. |
| 277 | // We use alpha = 1, beta = 3/8, giving us results with a largest error |
| 278 | // less than 7% compared to the exact value. |
| 279 | static inline int32_t HYPOT(SwPoint pt) |
| 280 | { |
| 281 | if (pt.x < 0) pt.x = -pt.x; |
| 282 | if (pt.y < 0) pt.y = -pt.y; |
| 283 | return ((pt.x > pt.y) ? (pt.x + (3 * pt.y >> 3)) : (pt.y + (3 * pt.x >> 3))); |
| 284 | } |
| 285 | |
| 286 | |
| 287 | // Used to prevent integer overflow when calculating the distance between points. |
| 288 | // This function uses 64-bit arithmetic to safely compute the difference between coordinates. |
| 289 | static inline uint32_t SAFE_HYPOT(SwPoint& pt1, SwPoint& pt2) |
| 290 | { |
| 291 | auto x = uint32_t(abs(int64_t(pt1.x) - int64_t(pt2.x))); |
| 292 | auto y = uint32_t(abs(int64_t(pt1.y) - int64_t(pt2.y))); |
| 293 | return (x > y) ? (x + (3 * y >> 3)) : (y + (3 * x >> 3)); |
| 294 | } |
| 295 | |
| 296 | |
| 297 | static void _horizLine(RleWorker& rw, int32_t x, int32_t y, int32_t area, int32_t aCount) |
| 298 | { |
| 299 | x += rw.cellMin.x; |
| 300 | y += rw.cellMin.y; |
| 301 | |
| 302 | //Clip Y range |
| 303 | if (y < rw.cellMin.y || y >= rw.cellMax.y) return; |
| 304 | |
| 305 | /* compute the coverage line's coverage, depending on the outline fill rule */ |
| 306 | /* the coverage percentage is area/(PIXEL_BITS*PIXEL_BITS*2) */ |
| 307 | auto coverage = static_cast<int>(area >> (PIXEL_BITS * 2 + 1 - 8)); //range 0 - 255 |
| 308 | |
| 309 | if (coverage < 0) coverage = -coverage; |
| 310 | |
| 311 | if (rw.outline->fillRule == FillRule::EvenOdd) { |
| 312 | coverage &= 511; |
| 313 | if (coverage > 255) coverage = 511 - coverage; |
| 314 | } else { |
| 315 | //normal non-zero winding rule |
| 316 | if (coverage > 255) coverage = 255; |
| 317 | } |
| 318 | |
| 319 | if (coverage == 0) return; |
| 320 | |
| 321 | //span has ushort coordinates. check limit overflow |
| 322 | if (x >= SHRT_MAX) { |
| 323 | TVGERR("SW_ENGINE", "X-coordinate overflow!"); |
| 324 | return; |
| 325 | } |
| 326 | if (y >= SHRT_MAX) { |
| 327 | TVGERR("SW_ENGINE", "Y-coordinate overflow!"); |
| 328 | return; |
| 329 | } |
| 330 | |
| 331 | auto rle = rw.rle; |
| 332 | |
| 333 | if (!rw.antiAlias) coverage = 255; |
| 334 | |
| 335 | //see whether we can add this span to the current list |
| 336 | if (rle->size > 0) { |
| 337 | auto span = rle->spans + rle->size - 1; |
| 338 | if ((span->coverage == coverage) && (span->y == y) && (span->x + span->len == x)) { |
| 339 | //Clip x range |
| 340 | SwCoord xOver = 0; |
| 341 | if (x + aCount >= rw.cellMax.x) xOver -= (x + aCount - rw.cellMax.x); |
| 342 | if (x < rw.cellMin.x) xOver -= (rw.cellMin.x - x); |
| 343 | |
| 344 | //span->len += (aCount + xOver) - 1; |
| 345 | span->len += (aCount + xOver); |
| 346 | return; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | //span pool is full, grow it. |
| 351 | if (rle->size >= rle->alloc) { |
| 352 | auto newSize = (rle->size > 0) ? (rle->size * 2) : 256; |
| 353 | if (rle->alloc < newSize) { |
| 354 | rle->alloc = newSize; |
| 355 | rle->spans = static_cast<SwSpan*>(realloc(rle->spans, rle->alloc * sizeof(SwSpan))); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | //Clip x range |
| 360 | SwCoord xOver = 0; |
| 361 | if (x + aCount >= rw.cellMax.x) xOver -= (x + aCount - rw.cellMax.x); |
| 362 | if (x < rw.cellMin.x) { |
| 363 | xOver -= (rw.cellMin.x - x); |
| 364 | x = rw.cellMin.x; |
| 365 | } |
| 366 | |
| 367 | //Nothing to draw |
| 368 | if (aCount + xOver <= 0) return; |
| 369 | |
| 370 | //add a span to the current list |
| 371 | auto span = rle->spans + rle->size; |
| 372 | span->x = x; |
| 373 | span->y = y; |
| 374 | span->len = (aCount + xOver); |
| 375 | span->coverage = coverage; |
| 376 | rle->size++; |
| 377 | } |
| 378 | |
| 379 | |
| 380 | static void _sweep(RleWorker& rw) |
| 381 | { |
| 382 | if (rw.cellsCnt == 0) return; |
| 383 | |
| 384 | for (int y = 0; y < rw.yCnt; ++y) { |
| 385 | auto cover = 0; |
| 386 | auto x = 0; |
| 387 | auto cell = rw.yCells[y]; |
| 388 | |
| 389 | while (cell) { |
| 390 | if (cell->x > x && cover != 0) _horizLine(rw, x, y, cover * (ONE_PIXEL * 2), cell->x - x); |
| 391 | cover += cell->cover; |
| 392 | auto area = cover * (ONE_PIXEL * 2) - cell->area; |
| 393 | if (area != 0 && cell->x >= 0) _horizLine(rw, cell->x, y, area, 1); |
| 394 | x = cell->x + 1; |
| 395 | cell = cell->next; |
| 396 | } |
| 397 | |
| 398 | if (cover != 0) _horizLine(rw, x, y, cover * (ONE_PIXEL * 2), rw.cellXCnt - x); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | |
| 403 | static SwCell* _findCell(RleWorker& rw) |
| 404 | { |
| 405 | auto x = rw.cellPos.x; |
| 406 | if (x > rw.cellXCnt) x = rw.cellXCnt; |
| 407 | |
| 408 | auto pcell = &rw.yCells[rw.cellPos.y]; |
| 409 | |
| 410 | while(true) { |
| 411 | auto cell = *pcell; |
| 412 | if (!cell || cell->x > x) break; |
| 413 | if (cell->x == x) return cell; |
| 414 | pcell = &cell->next; |
| 415 | } |
| 416 | |
| 417 | if (rw.cellsCnt >= rw.maxCells) return nullptr; |
| 418 | |
| 419 | auto cell = rw.cells + rw.cellsCnt++; |
| 420 | cell->x = x; |
| 421 | cell->area = 0; |
| 422 | cell->cover = 0; |
| 423 | cell->next = *pcell; |
| 424 | *pcell = cell; |
| 425 | |
| 426 | return cell; |
| 427 | } |
| 428 | |
| 429 | |
| 430 | static bool _recordCell(RleWorker& rw) |
| 431 | { |
| 432 | if (rw.area | rw.cover) { |
| 433 | auto cell = _findCell(rw); |
| 434 | |
| 435 | if (cell == nullptr) return false; |
| 436 | |
| 437 | cell->area += rw.area; |
| 438 | cell->cover += rw.cover; |
| 439 | } |
| 440 | |
| 441 | return true; |
| 442 | } |
| 443 | |
| 444 | |
| 445 | static bool _setCell(RleWorker& rw, SwPoint pos) |
| 446 | { |
| 447 | /* Move the cell pointer to a new position. We set the `invalid' */ |
| 448 | /* flag to indicate that the cell isn't part of those we're interested */ |
| 449 | /* in during the render phase. This means that: */ |
| 450 | /* */ |
| 451 | /* . the new vertical position must be within min_ey..max_ey-1. */ |
| 452 | /* . the new horizontal position must be strictly less than max_ex */ |
| 453 | /* */ |
| 454 | /* Note that if a cell is to the left of the clipping region, it is */ |
| 455 | /* actually set to the (min_ex-1) horizontal position. */ |
| 456 | |
| 457 | /* All cells that are on the left of the clipping region go to the |
| 458 | min_ex - 1 horizontal position. */ |
| 459 | pos.x -= rw.cellMin.x; |
| 460 | pos.y -= rw.cellMin.y; |
| 461 | |
| 462 | //exceptions |
| 463 | if (pos.x < 0) pos.x = -1; |
| 464 | else if (pos.x > rw.cellMax.x) pos.x = rw.cellMax.x; |
| 465 | |
| 466 | //Are we moving to a different cell? |
| 467 | if (pos != rw.cellPos) { |
| 468 | //Record the current one if it is valid |
| 469 | if (!rw.invalid) { |
| 470 | if (!_recordCell(rw)) return false; |
| 471 | } |
| 472 | rw.area = rw.cover = 0; |
| 473 | rw.cellPos = pos; |
| 474 | } |
| 475 | rw.invalid = ((unsigned)pos.y >= (unsigned)rw.cellYCnt || pos.x >= rw.cellXCnt); |
| 476 | |
| 477 | return true; |
| 478 | } |
| 479 | |
| 480 | |
| 481 | static bool _startCell(RleWorker& rw, SwPoint pos) |
| 482 | { |
| 483 | if (pos.x > rw.cellMax.x) pos.x = rw.cellMax.x; |
| 484 | if (pos.x < rw.cellMin.x) pos.x = rw.cellMin.x - 1; |
| 485 | |
| 486 | rw.area = 0; |
| 487 | rw.cover = 0; |
| 488 | rw.cellPos = pos - rw.cellMin; |
| 489 | rw.invalid = false; |
| 490 | |
| 491 | return _setCell(rw, pos); |
| 492 | } |
| 493 | |
| 494 | |
| 495 | static bool _moveTo(RleWorker& rw, const SwPoint& to) |
| 496 | { |
| 497 | //record current cell, if any */ |
| 498 | if (!rw.invalid) { |
| 499 | if (!_recordCell(rw)) return false; |
| 500 | } |
| 501 | |
| 502 | //start to a new position |
| 503 | if (!_startCell(rw, TRUNC(to))) return false; |
| 504 | |
| 505 | rw.pos = to; |
| 506 | |
| 507 | return true; |
| 508 | } |
| 509 | |
| 510 | |
| 511 | static bool _lineTo(RleWorker& rw, const SwPoint& to) |
| 512 | { |
| 513 | #define SW_UDIV(a, b) \ |
| 514 | static_cast<SwCoord>(((unsigned long)(a) * (unsigned long)(b)) >> \ |
| 515 | (sizeof(long) * CHAR_BIT - PIXEL_BITS)) |
| 516 | |
| 517 | auto e1 = TRUNC(rw.pos); |
| 518 | auto e2 = TRUNC(to); |
| 519 | |
| 520 | //vertical clipping |
| 521 | if ((e1.y >= rw.cellMax.y && e2.y >= rw.cellMax.y) || (e1.y < rw.cellMin.y && e2.y < rw.cellMin.y)) { |
| 522 | rw.pos = to; |
| 523 | return true; |
| 524 | } |
| 525 | |
| 526 | auto line = rw.lineStack; |
| 527 | line[0] = to; |
| 528 | line[1] = rw.pos; |
| 529 | |
| 530 | while (true) { |
| 531 | if (SAFE_HYPOT(line[0], line[1]) > SHRT_MAX) { |
| 532 | mathSplitLine(line); |
| 533 | ++line; |
| 534 | continue; |
| 535 | } |
| 536 | auto diff = line[0] - line[1]; |
| 537 | e1 = TRUNC(line[1]); |
| 538 | e2 = TRUNC(line[0]); |
| 539 | |
| 540 | auto f1 = line[1] - SUBPIXELS(e1); |
| 541 | SwPoint f2; |
| 542 | |
| 543 | //inside one cell |
| 544 | if (e1 == e2) { |
| 545 | ; |
| 546 | //any horizontal line |
| 547 | } else if (diff.y == 0) { |
| 548 | e1.x = e2.x; |
| 549 | if (!_setCell(rw, e1)) return false; |
| 550 | } else if (diff.x == 0) { |
| 551 | //vertical line up |
| 552 | if (diff.y > 0) { |
| 553 | do { |
| 554 | f2.y = ONE_PIXEL; |
| 555 | rw.cover += (f2.y - f1.y); |
| 556 | rw.area += (f2.y - f1.y) * f1.x * 2; |
| 557 | f1.y = 0; |
| 558 | ++e1.y; |
| 559 | if (!_setCell(rw, e1)) return false; |
| 560 | } while(e1.y != e2.y); |
| 561 | //vertical line down |
| 562 | } else { |
| 563 | do { |
| 564 | f2.y = 0; |
| 565 | rw.cover += (f2.y - f1.y); |
| 566 | rw.area += (f2.y - f1.y) * f1.x * 2; |
| 567 | f1.y = ONE_PIXEL; |
| 568 | --e1.y; |
| 569 | if (!_setCell(rw, e1)) return false; |
| 570 | } while(e1.y != e2.y); |
| 571 | } |
| 572 | //any other line |
| 573 | } else { |
| 574 | Area prod = diff.x * f1.y - diff.y * f1.x; |
| 575 | |
| 576 | /* These macros speed up repetitive divisions by replacing them |
| 577 | with multiplications and right shifts. */ |
| 578 | auto dx_r = static_cast<long>(ULONG_MAX >> PIXEL_BITS) / (diff.x); |
| 579 | auto dy_r = static_cast<long>(ULONG_MAX >> PIXEL_BITS) / (diff.y); |
| 580 | |
| 581 | /* The fundamental value `prod' determines which side and the */ |
| 582 | /* exact coordinate where the line exits current cell. It is */ |
| 583 | /* also easily updated when moving from one cell to the next. */ |
| 584 | do { |
| 585 | auto px = diff.x * ONE_PIXEL; |
| 586 | auto py = diff.y * ONE_PIXEL; |
| 587 | |
| 588 | //left |
| 589 | if (prod <= 0 && prod - px > 0) { |
| 590 | f2 = {0, SW_UDIV(-prod, -dx_r)}; |
| 591 | prod -= py; |
| 592 | rw.cover += (f2.y - f1.y); |
| 593 | rw.area += (f2.y - f1.y) * (f1.x + f2.x); |
| 594 | f1 = {ONE_PIXEL, f2.y}; |
| 595 | --e1.x; |
| 596 | //up |
| 597 | } else if (prod - px <= 0 && prod - px + py > 0) { |
| 598 | prod -= px; |
| 599 | f2 = {SW_UDIV(-prod, dy_r), ONE_PIXEL}; |
| 600 | rw.cover += (f2.y - f1.y); |
| 601 | rw.area += (f2.y - f1.y) * (f1.x + f2.x); |
| 602 | f1 = {f2.x, 0}; |
| 603 | ++e1.y; |
| 604 | //right |
| 605 | } else if (prod - px + py <= 0 && prod + py >= 0) { |
| 606 | prod += py; |
| 607 | f2 = {ONE_PIXEL, SW_UDIV(prod, dx_r)}; |
| 608 | rw.cover += (f2.y - f1.y); |
| 609 | rw.area += (f2.y - f1.y) * (f1.x + f2.x); |
| 610 | f1 = {0, f2.y}; |
| 611 | ++e1.x; |
| 612 | //down |
| 613 | } else { |
| 614 | f2 = {SW_UDIV(prod, -dy_r), 0}; |
| 615 | prod += px; |
| 616 | rw.cover += (f2.y - f1.y); |
| 617 | rw.area += (f2.y - f1.y) * (f1.x + f2.x); |
| 618 | f1 = {f2.x, ONE_PIXEL}; |
| 619 | --e1.y; |
| 620 | } |
| 621 | |
| 622 | if (!_setCell(rw, e1)) return false; |
| 623 | |
| 624 | } while(e1 != e2); |
| 625 | } |
| 626 | |
| 627 | f2 = {line[0].x - SUBPIXELS(e2.x), line[0].y - SUBPIXELS(e2.y)}; |
| 628 | rw.cover += (f2.y - f1.y); |
| 629 | rw.area += (f2.y - f1.y) * (f1.x + f2.x); |
| 630 | rw.pos = line[0]; |
| 631 | |
| 632 | if (line-- == rw.lineStack) return true; |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | |
| 637 | static bool _cubicTo(RleWorker& rw, const SwPoint& ctrl1, const SwPoint& ctrl2, const SwPoint& to) |
| 638 | { |
| 639 | auto arc = rw.bezStack; |
| 640 | arc[0] = to; |
| 641 | arc[1] = ctrl2; |
| 642 | arc[2] = ctrl1; |
| 643 | arc[3] = rw.pos; |
| 644 | |
| 645 | //Short-cut the arc that crosses the current band |
| 646 | auto min = arc[0].y; |
| 647 | auto max = arc[0].y; |
| 648 | |
| 649 | SwCoord y; |
| 650 | for (auto i = 1; i < 4; ++i) { |
| 651 | y = arc[i].y; |
| 652 | if (y < min) min = y; |
| 653 | if (y > max) max = y; |
| 654 | } |
| 655 | |
| 656 | if (TRUNC(min) >= rw.cellMax.y || TRUNC(max) < rw.cellMin.y) goto draw; |
| 657 | |
| 658 | /* Decide whether to split or draw. See `Rapid Termination */ |
| 659 | /* Evaluation for Recursive Subdivision of Bezier Curves' by Thomas */ |
| 660 | /* F. Hain, at */ |
| 661 | /* http://www.cis.southalabama.edu/~hain/general/Publications/Bezier/Camera-ready%20CISST02%202.pdf */ |
| 662 | while (true) { |
| 663 | { |
| 664 | //diff is the P0 - P3 chord vector |
| 665 | auto diff = arc[3] - arc[0]; |
| 666 | auto L = HYPOT(diff); |
| 667 | |
| 668 | //avoid possible arithmetic overflow below by splitting |
| 669 | if (L > SHRT_MAX) goto split; |
| 670 | |
| 671 | //max deviation may be as much as (s/L) * 3/4 (if Hain's v = 1) |
| 672 | auto sLimit = L * (ONE_PIXEL / 6); |
| 673 | |
| 674 | auto diff1 = arc[1] - arc[0]; |
| 675 | auto s = diff.y * diff1.x - diff.x * diff1.y; |
| 676 | if (s < 0) s = -s; |
| 677 | if (s > sLimit) goto split; |
| 678 | |
| 679 | //s is L * the perpendicular distance from P2 to the line P0 - P3 |
| 680 | auto diff2 = arc[2] - arc[0]; |
| 681 | s = diff.y * diff2.x - diff.x * diff2.y; |
| 682 | if (s < 0) s = -s; |
| 683 | if (s > sLimit) goto split; |
| 684 | |
| 685 | /* Split super curvy segments where the off points are so far |
| 686 | from the chord that the angles P0-P1-P3 or P0-P2-P3 become |
| 687 | acute as detected by appropriate dot products */ |
| 688 | if (diff1.x * (diff1.x - diff.x) + diff1.y * (diff1.y - diff.y) > 0 || |
| 689 | diff2.x * (diff2.x - diff.x) + diff2.y * (diff2.y - diff.y) > 0) |
| 690 | goto split; |
| 691 | |
| 692 | //no reason to split |
| 693 | goto draw; |
| 694 | } |
| 695 | split: |
| 696 | mathSplitCubic(arc); |
| 697 | arc += 3; |
| 698 | continue; |
| 699 | |
| 700 | draw: |
| 701 | if (!_lineTo(rw, arc[0])) return false; |
| 702 | if (arc == rw.bezStack) return true; |
| 703 | arc -= 3; |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | |
| 708 | static bool _decomposeOutline(RleWorker& rw) |
| 709 | { |
| 710 | auto outline = rw.outline; |
| 711 | auto first = 0; //index of first point in contour |
| 712 | |
| 713 | for (auto cntr = outline->cntrs.begin(); cntr < outline->cntrs.end(); ++cntr) { |
| 714 | auto last = *cntr; |
| 715 | auto limit = outline->pts.data + last; |
| 716 | auto start = UPSCALE(outline->pts[first]); |
| 717 | auto pt = outline->pts.data + first; |
| 718 | auto types = outline->types.data + first; |
| 719 | ++types; |
| 720 | |
| 721 | if (!_moveTo(rw, UPSCALE(outline->pts[first]))) return false; |
| 722 | |
| 723 | while (pt < limit) { |
| 724 | //emit a single line_to |
| 725 | if (types[0] == SW_CURVE_TYPE_POINT) { |
| 726 | ++pt; |
| 727 | ++types; |
| 728 | if (!_lineTo(rw, UPSCALE(*pt))) return false; |
| 729 | //types cubic |
| 730 | } else { |
| 731 | pt += 3; |
| 732 | types += 3; |
| 733 | if (pt <= limit) { |
| 734 | if (!_cubicTo(rw, UPSCALE(pt[-2]), UPSCALE(pt[-1]), UPSCALE(pt[0]))) return false; |
| 735 | } |
| 736 | else if (pt - 1 == limit) { |
| 737 | if (!_cubicTo(rw, UPSCALE(pt[-2]), UPSCALE(pt[-1]), start)) return false; |
| 738 | } |
| 739 | else goto close; |
| 740 | } |
| 741 | } |
| 742 | close: |
| 743 | if (!_lineTo(rw, start)) return false; |
| 744 | first = last + 1; |
| 745 | } |
| 746 | |
| 747 | return true; |
| 748 | } |
| 749 | |
| 750 | |
| 751 | static int _genRle(RleWorker& rw) |
| 752 | { |
| 753 | if (!_decomposeOutline(rw)) return -1; |
| 754 | if (!rw.invalid) { |
| 755 | if (!_recordCell(rw)) return -1; |
| 756 | } |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | |
| 761 | static SwSpan* _intersectSpansRegion(const SwRle *clip, const SwRle *target, SwSpan *outSpans, uint32_t outSpansCnt) |
| 762 | { |
| 763 | auto out = outSpans; |
| 764 | auto spans = target->spans; |
| 765 | auto end = target->spans + target->size; |
| 766 | auto clipSpans = clip->spans; |
| 767 | auto clipEnd = clip->spans + clip->size; |
| 768 | |
| 769 | while (spans < end && clipSpans < clipEnd) { |
| 770 | //align y-coordinates. |
| 771 | if (clipSpans->y > spans->y) { |
| 772 | ++spans; |
| 773 | continue; |
| 774 | } |
| 775 | if (spans->y > clipSpans->y) { |
| 776 | ++clipSpans; |
| 777 | continue; |
| 778 | } |
| 779 | |
| 780 | //Try clipping with all clip spans which have a same y-coordinate. |
| 781 | auto temp = clipSpans; |
| 782 | while(temp < clipEnd && outSpansCnt > 0 && temp->y == clipSpans->y) { |
| 783 | auto sx1 = spans->x; |
| 784 | auto sx2 = sx1 + spans->len; |
| 785 | auto cx1 = temp->x; |
| 786 | auto cx2 = cx1 + temp->len; |
| 787 | |
| 788 | //The span must be left(x1) to right(x2) direction. Not intersected. |
| 789 | if (cx2 < sx1 || sx2 < cx1) { |
| 790 | ++temp; |
| 791 | continue; |
| 792 | } |
| 793 | |
| 794 | //clip span region. |
| 795 | auto x = sx1 > cx1 ? sx1 : cx1; |
| 796 | auto len = (sx2 < cx2 ? sx2 : cx2) - x; |
| 797 | if (len > 0) { |
| 798 | out->x = x; |
| 799 | out->y = temp->y; |
| 800 | out->len = len; |
| 801 | out->coverage = (uint8_t)(((spans->coverage * temp->coverage) + 0xff) >> 8); |
| 802 | ++out; |
| 803 | --outSpansCnt; |
| 804 | } |
| 805 | ++temp; |
| 806 | } |
| 807 | ++spans; |
| 808 | } |
| 809 | return out; |
| 810 | } |
| 811 | |
| 812 | |
| 813 | static SwSpan* _intersectSpansRect(const SwBBox *bbox, const SwRle *targetRle, SwSpan *outSpans, uint32_t outSpansCnt) |
| 814 | { |
| 815 | auto out = outSpans; |
| 816 | auto spans = targetRle->spans; |
| 817 | auto end = targetRle->spans + targetRle->size; |
| 818 | auto minx = static_cast<int16_t>(bbox->min.x); |
| 819 | auto miny = static_cast<int16_t>(bbox->min.y); |
| 820 | auto maxx = minx + static_cast<int16_t>(bbox->max.x - bbox->min.x) - 1; |
| 821 | auto maxy = miny + static_cast<int16_t>(bbox->max.y - bbox->min.y) - 1; |
| 822 | |
| 823 | while (outSpansCnt > 0 && spans < end) { |
| 824 | if (spans->y > maxy) { |
| 825 | spans = end; |
| 826 | break; |
| 827 | } |
| 828 | if (spans->y < miny || spans->x > maxx || spans->x + spans->len <= minx) { |
| 829 | ++spans; |
| 830 | continue; |
| 831 | } |
| 832 | if (spans->x < minx) { |
| 833 | out->len = (spans->len - (minx - spans->x)) < (maxx - minx + 1) ? (spans->len - (minx - spans->x)) : (maxx - minx + 1); |
| 834 | out->x = minx; |
| 835 | } |
| 836 | else { |
| 837 | out->x = spans->x; |
| 838 | out->len = spans->len < (maxx - spans->x + 1) ? spans->len : (maxx - spans->x + 1); |
| 839 | } |
| 840 | if (out->len > 0) { |
| 841 | out->y = spans->y; |
| 842 | out->coverage = spans->coverage; |
| 843 | ++out; |
| 844 | --outSpansCnt; |
| 845 | } |
| 846 | ++spans; |
| 847 | } |
| 848 | return out; |
| 849 | } |
| 850 | |
| 851 | |
| 852 | void _replaceClipSpan(SwRle *rle, SwSpan* clippedSpans, uint32_t size) |
| 853 | { |
| 854 | free(rle->spans); |
| 855 | rle->spans = clippedSpans; |
| 856 | rle->size = rle->alloc = size; |
| 857 | } |
| 858 | |
| 859 | |
| 860 | /************************************************************************/ |
| 861 | /* External Class Implementation */ |
| 862 | /************************************************************************/ |
| 863 | |
| 864 | SwRle* rleRender(SwRle* rle, const SwOutline* outline, const SwBBox& renderRegion, SwMpool* mpool, unsigned tid, bool antiAlias) |
| 865 | { |
| 866 | if (!outline) return nullptr; |
| 867 | |
| 868 | RleWorker rw; |
| 869 | auto cellPool = mpoolReqCellPool(mpool, tid); |
| 870 | auto reqSize = uint32_t(std::max(renderRegion.w(), renderRegion.h()) * 0.75f) * sizeof(SwCell); //experimental decision |
| 871 | |
| 872 | // grow by 1.25x and align to multiple of sizeof(SwCell) |
| 873 | if (reqSize > cellPool->size) { |
| 874 | cellPool->size = ((reqSize + (reqSize >> 2)) / sizeof(SwCell)) * sizeof(SwCell); |
| 875 | free(cellPool->buffer); |
| 876 | cellPool->buffer = (SwCell*)malloc(cellPool->size); |
| 877 | } |
| 878 | |
| 879 | //Init Cells |
| 880 | rw.buffer = cellPool->buffer; |
| 881 | rw.bufferSize = cellPool->size; |
| 882 | rw.yCells = reinterpret_cast<SwCell**>(cellPool->buffer); |
| 883 | rw.cells = nullptr; |
| 884 | rw.maxCells = 0; |
| 885 | rw.cellsCnt = 0; |
| 886 | rw.area = 0; |
| 887 | rw.cover = 0; |
| 888 | rw.invalid = true; |
| 889 | rw.cellMin = renderRegion.min; |
| 890 | rw.cellMax = renderRegion.max; |
| 891 | rw.cellXCnt = rw.cellMax.x - rw.cellMin.x; |
| 892 | rw.cellYCnt = rw.cellMax.y - rw.cellMin.y; |
| 893 | rw.outline = const_cast<SwOutline*>(outline); |
| 894 | rw.bandSize = rw.bufferSize / (sizeof(SwCell) * 2); |
| 895 | rw.bandShoot = 0; |
| 896 | rw.antiAlias = antiAlias; |
| 897 | |
| 898 | if (!rle) rw.rle = reinterpret_cast<SwRle*>(calloc(1, sizeof(SwRle))); |
| 899 | else rw.rle = rle; |
| 900 | |
| 901 | //Generate RLE |
| 902 | constexpr auto BAND_SIZE = 40; |
| 903 | |
| 904 | Band bands[BAND_SIZE]; |
| 905 | Band* band; |
| 906 | |
| 907 | /* set up vertical bands */ |
| 908 | auto bandCnt = static_cast<int>((rw.cellMax.y - rw.cellMin.y) / rw.bandSize); |
| 909 | if (bandCnt == 0) bandCnt = 1; |
| 910 | else if (bandCnt >= BAND_SIZE) bandCnt = (BAND_SIZE - 1); |
| 911 | |
| 912 | auto min = rw.cellMin.y; |
| 913 | auto yMax = rw.cellMax.y; |
| 914 | SwCoord max; |
| 915 | int ret; |
| 916 | |
| 917 | for (int n = 0; n < bandCnt; ++n, min = max) { |
| 918 | max = min + rw.bandSize; |
| 919 | if (n == bandCnt -1 || max > yMax) max = yMax; |
| 920 | |
| 921 | bands[0].min = min; |
| 922 | bands[0].max = max; |
| 923 | band = bands; |
| 924 | |
| 925 | while (band >= bands) { |
| 926 | rw.yCells = reinterpret_cast<SwCell**>(rw.buffer); |
| 927 | rw.yCnt = band->max - band->min; |
| 928 | |
| 929 | int cellStart = sizeof(SwCell*) * (int)rw.yCnt; |
| 930 | int cellMod = cellStart % sizeof(SwCell); |
| 931 | |
| 932 | if (cellMod > 0) cellStart += sizeof(SwCell) - cellMod; |
| 933 | |
| 934 | auto cellsMax = reinterpret_cast<SwCell*>((char*)rw.buffer + rw.bufferSize); |
| 935 | rw.cells = reinterpret_cast<SwCell*>((char*)rw.buffer + cellStart); |
| 936 | |
| 937 | if (rw.cells >= cellsMax) goto reduce_bands; |
| 938 | |
| 939 | rw.maxCells = cellsMax - rw.cells; |
| 940 | if (rw.maxCells < 2) goto reduce_bands; |
| 941 | |
| 942 | for (int y = 0; y < rw.yCnt; ++y) |
| 943 | rw.yCells[y] = nullptr; |
| 944 | |
| 945 | rw.cellsCnt = 0; |
| 946 | rw.invalid = true; |
| 947 | rw.cellMin.y = band->min; |
| 948 | rw.cellMax.y = band->max; |
| 949 | rw.cellYCnt = band->max - band->min; |
| 950 | |
| 951 | ret = _genRle(rw); |
| 952 | if (ret == 0) { |
| 953 | _sweep(rw); |
| 954 | --band; |
| 955 | continue; |
| 956 | } else if (ret == 1) { |
| 957 | goto error; |
| 958 | } |
| 959 | |
| 960 | reduce_bands: |
| 961 | /* render pool overflow: we will reduce the render band by half */ |
| 962 | auto bottom = band->min; |
| 963 | auto top = band->max; |
| 964 | auto middle = bottom + ((top - bottom) >> 1); |
| 965 | |
| 966 | /* This is too complex for a single scanline; there must |
| 967 | be some problems */ |
| 968 | if (middle == bottom) goto error; |
| 969 | |
| 970 | if (bottom - top >= rw.bandSize) ++rw.bandShoot; |
| 971 | |
| 972 | band[1].min = bottom; |
| 973 | band[1].max = middle; |
| 974 | band[0].min = middle; |
| 975 | band[0].max = top; |
| 976 | ++band; |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | if (rw.bandShoot > 8 && rw.bandSize > 16) |
| 981 | rw.bandSize = (rw.bandSize >> 1); |
| 982 | |
| 983 | return rw.rle; |
| 984 | |
| 985 | error: |
| 986 | free(rw.rle); |
| 987 | return nullptr; |
| 988 | } |
| 989 | |
| 990 | |
| 991 | SwRle* rleRender(const SwBBox* bbox) |
| 992 | { |
| 993 | auto width = static_cast<uint16_t>(bbox->max.x - bbox->min.x); |
| 994 | auto height = static_cast<uint16_t>(bbox->max.y - bbox->min.y); |
| 995 | |
| 996 | auto rle = static_cast<SwRle*>(malloc(sizeof(SwRle))); |
| 997 | rle->spans = static_cast<SwSpan*>(malloc(sizeof(SwSpan) * height)); |
| 998 | rle->size = height; |
| 999 | rle->alloc = height; |
| 1000 | |
| 1001 | auto span = rle->spans; |
| 1002 | for (uint16_t i = 0; i < height; ++i, ++span) { |
| 1003 | span->x = bbox->min.x; |
| 1004 | span->y = bbox->min.y + i; |
| 1005 | span->len = width; |
| 1006 | span->coverage = 255; |
| 1007 | } |
| 1008 | |
| 1009 | return rle; |
| 1010 | } |
| 1011 | |
| 1012 | |
| 1013 | void rleReset(SwRle* rle) |
| 1014 | { |
| 1015 | if (!rle) return; |
| 1016 | rle->size = 0; |
| 1017 | } |
| 1018 | |
| 1019 | |
| 1020 | void rleFree(SwRle* rle) |
| 1021 | { |
| 1022 | if (!rle) return; |
| 1023 | if (rle->spans) free(rle->spans); |
| 1024 | free(rle); |
| 1025 | } |
| 1026 | |
| 1027 | |
| 1028 | bool rleClip(SwRle *rle, const SwRle *clip) |
| 1029 | { |
| 1030 | if (rle->size == 0 || clip->size == 0) return false; |
| 1031 | auto spanCnt = 2 * (rle->size > clip->size ? rle->size : clip->size); //factor 2 added for safety (no real cases observed where the factor exceeded 1.4) |
| 1032 | auto spans = static_cast<SwSpan*>(malloc(sizeof(SwSpan) * (spanCnt))); |
| 1033 | auto spansEnd = _intersectSpansRegion(clip, rle, spans, spanCnt); |
| 1034 | |
| 1035 | _replaceClipSpan(rle, spans, spansEnd - spans); |
| 1036 | |
| 1037 | TVGLOG("SW_ENGINE", "Using Path Clipping!"); |
| 1038 | |
| 1039 | return true; |
| 1040 | } |
| 1041 | |
| 1042 | |
| 1043 | bool rleClip(SwRle *rle, const SwBBox* clip) |
| 1044 | { |
| 1045 | if (rle->size == 0) return false; |
| 1046 | auto spans = static_cast<SwSpan*>(malloc(sizeof(SwSpan) * (rle->size))); |
| 1047 | auto spansEnd = _intersectSpansRect(clip, rle, spans, rle->size); |
| 1048 | |
| 1049 | _replaceClipSpan(rle, spans, spansEnd - spans); |
| 1050 | |
| 1051 | TVGLOG("SW_ENGINE", "Using Box Clipping!"); |
| 1052 | |
| 1053 | return true; |
| 1054 | } |
| 1055 |