2 * Copyright (c) 2020 - 2023 the ThorVG project. All rights reserved.
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:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
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
24 * The FreeType Project LICENSE
25 * ----------------------------
29 * Copyright 1996-2002, 2006 by
30 * David Turner, Robert Wilhelm, and Werner Lemberg
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
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.
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:
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)
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)
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')
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.
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:
74 * Portions of this software are copyright � <year> The FreeType
75 * Project (www.freetype.org). All rights reserved.
78 * Please replace <year> with the value from the FreeType version you
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.
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
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.
106 * The FreeType Project is copyright (C) 1996-2000 by David Turner,
107 * Robert Wilhelm, and Werner Lemberg. All rights reserved except as
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.
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:
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
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.
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
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.
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'.
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
172 * There are two mailing lists related to FreeType:
174 * o freetype@nongnu.org
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.
181 * o freetype-devel@nongnu.org
183 * Discusses bugs, as well as engine internals, design issues,
184 * specific licenses, porting, etc.
186 * Our home page can be found at
188 * http://www.freetype.org
194 #include "tvgSwCommon.h"
196 /************************************************************************/
197 /* Internal Class Implementation */
198 /************************************************************************/
200 constexpr auto MAX_SPANS = 256;
201 constexpr auto PIXEL_BITS = 8; //must be at least 6 bits!
202 constexpr auto ONE_PIXEL = (1L << PIXEL_BITS);
238 SwPoint bezStack[32 * 3 + 1];
243 SwSpan spans[MAX_SPANS];
263 static inline SwPoint UPSCALE(const SwPoint& pt)
265 return {SwCoord(((unsigned long) pt.x) << (PIXEL_BITS - 6)), SwCoord(((unsigned long) pt.y) << (PIXEL_BITS - 6))};
269 static inline SwPoint TRUNC(const SwPoint& pt)
271 return {pt.x >> PIXEL_BITS, pt.y >> PIXEL_BITS};
275 static inline SwCoord TRUNC(const SwCoord x)
277 return x >> PIXEL_BITS;
281 static inline SwPoint SUBPIXELS(const SwPoint& pt)
283 return {SwCoord(((unsigned long) pt.x) << PIXEL_BITS), SwCoord(((unsigned long) pt.y) << PIXEL_BITS)};
287 static inline SwCoord SUBPIXELS(const SwCoord x)
289 return SwCoord(((unsigned long) x) << PIXEL_BITS);
293 * Approximate sqrt(x*x+y*y) using the `alpha max plus beta min'
294 * algorithm. We use alpha = 1, beta = 3/8, giving us results with a
295 * largest error less than 7% compared to the exact value.
297 static inline SwCoord HYPOT(SwPoint pt)
299 if (pt.x < 0) pt.x = -pt.x;
300 if (pt.y < 0) pt.y = -pt.y;
301 return ((pt.x > pt.y) ? (pt.x + (3 * pt.y >> 3)) : (pt.y + (3 * pt.x >> 3)));
304 static void _genSpan(SwRleData* rle, const SwSpan* spans, uint32_t count)
306 auto newSize = rle->size + count;
308 /* allocate enough memory for new spans */
309 /* alloc is required to prevent free and reallocation */
310 /* when the rle needs to be regenerated because of attribute change. */
311 if (rle->alloc < newSize) {
312 rle->alloc = (newSize * 2);
313 //OPTIMIZE: use mempool!
314 rle->spans = static_cast<SwSpan*>(realloc(rle->spans, rle->alloc * sizeof(SwSpan)));
317 //copy the new spans to the allocated memory
318 SwSpan* lastSpan = rle->spans + rle->size;
319 memcpy(lastSpan, spans, count * sizeof(SwSpan));
325 static void _horizLine(RleWorker& rw, SwCoord x, SwCoord y, SwCoord area, SwCoord acount)
331 if (y < rw.cellMin.y || y >= rw.cellMax.y) return;
333 /* compute the coverage line's coverage, depending on the outline fill rule */
334 /* the coverage percentage is area/(PIXEL_BITS*PIXEL_BITS*2) */
335 auto coverage = static_cast<int>(area >> (PIXEL_BITS * 2 + 1 - 8)); //range 0 - 255
337 if (coverage < 0) coverage = -coverage;
339 if (rw.outline->fillRule == FillRule::EvenOdd) {
341 if (coverage > 255) coverage = 511 - coverage;
343 //normal non-zero winding rule
344 if (coverage > 255) coverage = 255;
347 //span has ushort coordinates. check limit overflow
349 TVGERR("SW_ENGINE", "X-coordiante overflow!");
353 TVGERR("SW_ENGINE", "Y Coordiante overflow!");
358 if (!rw.antiAlias) coverage = 255;
359 auto count = rw.spansCnt;
360 auto span = rw.spans + count - 1;
362 //see whether we can add this span to the current list
363 if ((count > 0) && (rw.ySpan == y) &&
364 (span->x + span->len == x) && (span->coverage == coverage)) {
368 if (x + acount >= rw.cellMax.x) xOver -= (x + acount - rw.cellMax.x);
369 if (x < rw.cellMin.x) xOver -= (rw.cellMin.x - x);
371 //span->len += (acount + xOver) - 1;
372 span->len += (acount + xOver);
376 if (count >= MAX_SPANS) {
377 _genSpan(rw.rle, rw.spans, count);
387 if (x + acount >= rw.cellMax.x) xOver -= (x + acount - rw.cellMax.x);
388 if (x < rw.cellMin.x) {
389 xOver -= (rw.cellMin.x - x);
394 if (acount + xOver <= 0) return;
396 //add a span to the current list
399 span->len = (acount + xOver);
400 span->coverage = coverage;
407 static void _sweep(RleWorker& rw)
409 if (rw.cellsCnt == 0) return;
414 for (int y = 0; y < rw.yCnt; ++y) {
417 auto cell = rw.yCells[y];
420 if (cell->x > x && cover != 0) _horizLine(rw, x, y, cover * (ONE_PIXEL * 2), cell->x - x);
421 cover += cell->cover;
422 auto area = cover * (ONE_PIXEL * 2) - cell->area;
423 if (area != 0 && cell->x >= 0) _horizLine(rw, cell->x, y, area, 1);
428 if (cover != 0) _horizLine(rw, x, y, cover * (ONE_PIXEL * 2), rw.cellXCnt - x);
431 if (rw.spansCnt > 0) _genSpan(rw.rle, rw.spans, rw.spansCnt);
435 static Cell* _findCell(RleWorker& rw)
437 auto x = rw.cellPos.x;
438 if (x > rw.cellXCnt) x = rw.cellXCnt;
440 auto pcell = &rw.yCells[rw.cellPos.y];
444 if (!cell || cell->x > x) break;
445 if (cell->x == x) return cell;
449 if (rw.cellsCnt >= rw.maxCells) longjmp(rw.jmpBuf, 1);
451 auto cell = rw.cells + rw.cellsCnt++;
462 static void _recordCell(RleWorker& rw)
464 if (rw.area | rw.cover) {
465 auto cell = _findCell(rw);
466 cell->area += rw.area;
467 cell->cover += rw.cover;
472 static void _setCell(RleWorker& rw, SwPoint pos)
474 /* Move the cell pointer to a new position. We set the `invalid' */
475 /* flag to indicate that the cell isn't part of those we're interested */
476 /* in during the render phase. This means that: */
478 /* . the new vertical position must be within min_ey..max_ey-1. */
479 /* . the new horizontal position must be strictly less than max_ex */
481 /* Note that if a cell is to the left of the clipping region, it is */
482 /* actually set to the (min_ex-1) horizontal position. */
484 /* All cells that are on the left of the clipping region go to the
485 min_ex - 1 horizontal position. */
486 pos.x -= rw.cellMin.x;
487 pos.y -= rw.cellMin.y;
489 if (pos.x > rw.cellMax.x) pos.x = rw.cellMax.x;
491 //Are we moving to a different cell?
492 if (pos != rw.cellPos) {
493 //Record the current one if it is valid
494 if (!rw.invalid) _recordCell(rw);
500 rw.invalid = ((unsigned)pos.y >= (unsigned)rw.cellYCnt || pos.x >= rw.cellXCnt);
504 static void _startCell(RleWorker& rw, SwPoint pos)
506 if (pos.x > rw.cellMax.x) pos.x = rw.cellMax.x;
507 if (pos.x < rw.cellMin.x) pos.x = rw.cellMin.x;
511 rw.cellPos = pos - rw.cellMin;
518 static void _moveTo(RleWorker& rw, const SwPoint& to)
520 //record current cell, if any */
521 if (!rw.invalid) _recordCell(rw);
523 //start to a new position
524 _startCell(rw, TRUNC(to));
530 static void _lineTo(RleWorker& rw, const SwPoint& to)
532 #define SW_UDIV(a, b) \
533 static_cast<SwCoord>(((unsigned long)(a) * (unsigned long)(b)) >> \
534 (sizeof(long) * CHAR_BIT - PIXEL_BITS))
536 auto e1 = TRUNC(rw.pos);
540 if ((e1.y >= rw.cellMax.y && e2.y >= rw.cellMax.y) || (e1.y < rw.cellMin.y && e2.y < rw.cellMin.y)) {
545 auto diff = to - rw.pos;
546 auto f1 = rw.pos - SUBPIXELS(e1);
552 //any horizontal line
553 } else if (diff.y == 0) {
556 } else if (diff.x == 0) {
561 rw.cover += (f2.y - f1.y);
562 rw.area += (f2.y - f1.y) * f1.x * 2;
566 } while(e1.y != e2.y);
571 rw.cover += (f2.y - f1.y);
572 rw.area += (f2.y - f1.y) * f1.x * 2;
576 } while(e1.y != e2.y);
580 Area prod = diff.x * f1.y - diff.y * f1.x;
582 /* These macros speed up repetitive divisions by replacing them
583 with multiplications and right shifts. */
584 auto dx_r = static_cast<long>(ULONG_MAX >> PIXEL_BITS) / (diff.x);
585 auto dy_r = static_cast<long>(ULONG_MAX >> PIXEL_BITS) / (diff.y);
587 /* The fundamental value `prod' determines which side and the */
588 /* exact coordinate where the line exits current cell. It is */
589 /* also easily updated when moving from one cell to the next. */
591 auto px = diff.x * ONE_PIXEL;
592 auto py = diff.y * ONE_PIXEL;
595 if (prod <= 0 && prod - px > 0) {
596 f2 = {0, SW_UDIV(-prod, -dx_r)};
598 rw.cover += (f2.y - f1.y);
599 rw.area += (f2.y - f1.y) * (f1.x + f2.x);
600 f1 = {ONE_PIXEL, f2.y};
603 } else if (prod - px <= 0 && prod - px + py > 0) {
605 f2 = {SW_UDIV(-prod, dy_r), ONE_PIXEL};
606 rw.cover += (f2.y - f1.y);
607 rw.area += (f2.y - f1.y) * (f1.x + f2.x);
611 } else if (prod - px + py <= 0 && prod + py >= 0) {
613 f2 = {ONE_PIXEL, SW_UDIV(prod, dx_r)};
614 rw.cover += (f2.y - f1.y);
615 rw.area += (f2.y - f1.y) * (f1.x + f2.x);
620 f2 = {SW_UDIV(prod, -dy_r), 0};
622 rw.cover += (f2.y - f1.y);
623 rw.area += (f2.y - f1.y) * (f1.x + f2.x);
624 f1 = {f2.x, ONE_PIXEL};
633 f2 = {to.x - SUBPIXELS(e2.x), to.y - SUBPIXELS(e2.y)};
634 rw.cover += (f2.y - f1.y);
635 rw.area += (f2.y - f1.y) * (f1.x + f2.x);
640 static void _cubicTo(RleWorker& rw, const SwPoint& ctrl1, const SwPoint& ctrl2, const SwPoint& to)
642 auto arc = rw.bezStack;
648 //Short-cut the arc that crosses the current band
653 for (auto i = 1; i < 4; ++i) {
655 if (y < min) min = y;
656 if (y > max) max = y;
659 if (TRUNC(min) >= rw.cellMax.y || TRUNC(max) < rw.cellMin.y) goto draw;
661 /* Decide whether to split or draw. See `Rapid Termination */
662 /* Evaluation for Recursive Subdivision of Bezier Curves' by Thomas */
664 /* http://www.cis.southalabama.edu/~hain/general/Publications/Bezier/Camera-ready%20CISST02%202.pdf */
667 //diff is the P0 - P3 chord vector
668 auto diff = arc[3] - arc[0];
669 auto L = HYPOT(diff);
671 //avoid possible arithmetic overflow below by splitting
672 if (L > SHRT_MAX) goto split;
674 //max deviation may be as much as (s/L) * 3/4 (if Hain's v = 1)
675 auto sLimit = L * (ONE_PIXEL / 6);
677 auto diff1 = arc[1] - arc[0];
678 auto s = diff.y * diff1.x - diff.x * diff1.y;
680 if (s > sLimit) goto split;
682 //s is L * the perpendicular distance from P2 to the line P0 - P3
683 auto diff2 = arc[2] - arc[0];
684 s = diff.y * diff2.x - diff.x * diff2.y;
686 if (s > sLimit) goto split;
688 /* Split super curvy segments where the off points are so far
689 from the chord that the angles P0-P1-P3 or P0-P2-P3 become
690 acute as detected by appropriate dot products */
691 if (diff1.x * (diff1.x - diff.x) + diff1.y * (diff1.y - diff.y) > 0 ||
692 diff2.x * (diff2.x - diff.x) + diff2.y * (diff2.y - diff.y) > 0)
705 if (arc == rw.bezStack) return;
711 static bool _decomposeOutline(RleWorker& rw)
713 auto outline = rw.outline;
714 auto first = 0; //index of first point in contour
716 for (uint32_t n = 0; n < outline->cntrsCnt; ++n) {
717 auto last = outline->cntrs[n];
718 auto limit = outline->pts + last;
719 auto start = UPSCALE(outline->pts[first]);
720 auto pt = outline->pts + first;
721 auto types = outline->types + first;
723 /* A contour cannot start with a cubic control point! */
724 if (types[0] == SW_CURVE_TYPE_CUBIC) goto invalid_outline;
726 _moveTo(rw, UPSCALE(outline->pts[first]));
732 //emit a single line_to
733 if (types[0] == SW_CURVE_TYPE_POINT) {
734 _lineTo(rw, UPSCALE(*pt));
737 if (pt + 1 > limit || types[1] != SW_CURVE_TYPE_CUBIC)
738 goto invalid_outline;
744 _cubicTo(rw, UPSCALE(pt[-2]), UPSCALE(pt[-1]), UPSCALE(pt[0]));
747 _cubicTo(rw, UPSCALE(pt[-2]), UPSCALE(pt[-1]), start);
759 TVGERR("SW_ENGINE", "Invalid Outline!");
764 static int _genRle(RleWorker& rw)
766 if (setjmp(rw.jmpBuf) == 0) {
767 auto ret = _decomposeOutline(rw);
768 if (!rw.invalid) _recordCell(rw);
769 if (ret) return 0; //success
770 else return 1; //fail
772 return -1; //lack of cell memory
776 SwSpan* _intersectSpansRegion(const SwRleData *clip, const SwRleData *targetRle, SwSpan *outSpans, uint32_t spanCnt)
779 auto spans = targetRle->spans;
780 auto end = targetRle->spans + targetRle->size;
781 auto clipSpans = clip->spans;
782 auto clipEnd = clip->spans + clip->size;
784 while (spanCnt > 0 && spans < end) {
785 if (clipSpans == clipEnd) {
789 if (clipSpans->y > spans->y) {
793 if (spans->y != clipSpans->y) {
798 auto sx2 = sx1 + spans->len;
799 auto cx1 = clipSpans->x;
800 auto cx2 = cx1 + clipSpans->len;
802 if (cx1 < sx1 && cx2 < sx1) {
806 else if (sx1 < cx1 && sx2 < cx1) {
810 auto x = sx1 > cx1 ? sx1 : cx1;
811 auto len = (sx2 < cx2 ? sx2 : cx2) - x;
813 auto spansCorverage = spans->coverage;
814 auto clipSpansCoverage = clipSpans->coverage;
815 out->x = sx1 > cx1 ? sx1 : cx1;
816 out->len = (sx2 < cx2 ? sx2 : cx2) - out->x;
818 out->coverage = (uint8_t)(((spansCorverage * clipSpansCoverage) + 0xff) >> 8);
822 if (sx2 < cx2) ++spans;
829 SwSpan* _intersectSpansRect(const SwBBox *bbox, const SwRleData *targetRle, SwSpan *outSpans, uint32_t spanCnt)
832 auto spans = targetRle->spans;
833 auto end = targetRle->spans + targetRle->size;
834 auto minx = static_cast<int16_t>(bbox->min.x);
835 auto miny = static_cast<int16_t>(bbox->min.y);
836 auto maxx = minx + static_cast<int16_t>(bbox->max.x - bbox->min.x) - 1;
837 auto maxy = miny + static_cast<int16_t>(bbox->max.y - bbox->min.y) - 1;
839 while (spanCnt && spans < end) {
840 if (spans->y > maxy) {
844 if (spans->y < miny || spans->x > maxx || spans->x + spans->len <= minx) {
848 if (spans->x < minx) {
849 out->len = (spans->len - (minx - spans->x)) < (maxx - minx + 1) ? (spans->len - (minx - spans->x)) : (maxx - minx + 1);
854 out->len = spans->len < (maxx - spans->x + 1) ? spans->len : (maxx - spans->x + 1);
858 out->coverage = spans->coverage;
868 void _replaceClipSpan(SwRleData *rle, SwSpan* clippedSpans, uint32_t size)
871 rle->spans = clippedSpans;
872 rle->size = rle->alloc = size;
876 /************************************************************************/
877 /* External Class Implementation */
878 /************************************************************************/
880 SwRleData* rleRender(SwRleData* rle, const SwOutline* outline, const SwBBox& renderRegion, bool antiAlias)
882 constexpr auto RENDER_POOL_SIZE = 16384L;
883 constexpr auto BAND_SIZE = 40;
885 //TODO: We can preserve several static workers in advance
887 Cell buffer[RENDER_POOL_SIZE / sizeof(Cell)];
891 rw.bufferSize = sizeof(buffer);
892 rw.yCells = reinterpret_cast<Cell**>(buffer);
899 rw.cellMin = renderRegion.min;
900 rw.cellMax = renderRegion.max;
901 rw.cellXCnt = rw.cellMax.x - rw.cellMin.x;
902 rw.cellYCnt = rw.cellMax.y - rw.cellMin.y;
904 rw.outline = const_cast<SwOutline*>(outline);
905 rw.bandSize = rw.bufferSize / (sizeof(Cell) * 8); //bandSize: 64
907 rw.antiAlias = antiAlias;
909 if (!rle) rw.rle = reinterpret_cast<SwRleData*>(calloc(1, sizeof(SwRleData)));
913 Band bands[BAND_SIZE];
916 /* set up vertical bands */
917 auto bandCnt = static_cast<int>((rw.cellMax.y - rw.cellMin.y) / rw.bandSize);
918 if (bandCnt == 0) bandCnt = 1;
919 else if (bandCnt >= BAND_SIZE) bandCnt = (BAND_SIZE - 1);
921 auto min = rw.cellMin.y;
922 auto yMax = rw.cellMax.y;
926 for (int n = 0; n < bandCnt; ++n, min = max) {
927 max = min + rw.bandSize;
928 if (n == bandCnt -1 || max > yMax) max = yMax;
934 while (band >= bands) {
935 rw.yCells = static_cast<Cell**>(rw.buffer);
936 rw.yCnt = band->max - band->min;
938 int cellStart = sizeof(Cell*) * (int)rw.yCnt;
939 int cellMod = cellStart % sizeof(Cell);
941 if (cellMod > 0) cellStart += sizeof(Cell) - cellMod;
943 auto cellEnd = rw.bufferSize;
944 cellEnd -= cellEnd % sizeof(Cell);
946 auto cellsMax = reinterpret_cast<Cell*>((char*)rw.buffer + cellEnd);
947 rw.cells = reinterpret_cast<Cell*>((char*)rw.buffer + cellStart);
949 if (rw.cells >= cellsMax) goto reduce_bands;
951 rw.maxCells = cellsMax - rw.cells;
952 if (rw.maxCells < 2) goto reduce_bands;
954 for (int y = 0; y < rw.yCnt; ++y)
955 rw.yCells[y] = nullptr;
959 rw.cellMin.y = band->min;
960 rw.cellMax.y = band->max;
961 rw.cellYCnt = band->max - band->min;
968 } else if (ret == 1) {
973 /* render pool overflow: we will reduce the render band by half */
974 auto bottom = band->min;
975 auto top = band->max;
976 auto middle = bottom + ((top - bottom) >> 1);
978 /* This is too complex for a single scanline; there must
980 if (middle == bottom) goto error;
982 if (bottom - top >= rw.bandSize) ++rw.bandShoot;
984 band[1].min = bottom;
985 band[1].max = middle;
986 band[0].min = middle;
992 if (rw.bandShoot > 8 && rw.bandSize > 16)
993 rw.bandSize = (rw.bandSize >> 1);
1004 void rleReset(SwRleData* rle)
1011 void rleFree(SwRleData* rle)
1014 if (rle->spans) free(rle->spans);
1019 void rleClipPath(SwRleData *rle, const SwRleData *clip)
1021 if (rle->size == 0 || clip->size == 0) return;
1022 auto spanCnt = rle->size > clip->size ? rle->size : clip->size;
1023 auto spans = static_cast<SwSpan*>(malloc(sizeof(SwSpan) * (spanCnt)));
1025 auto spansEnd = _intersectSpansRegion(clip, rle, spans, spanCnt);
1027 _replaceClipSpan(rle, spans, spansEnd - spans);
1029 TVGLOG("SW_ENGINE", "Using ClipPath!");
1033 void rleClipRect(SwRleData *rle, const SwBBox* clip)
1035 if (rle->size == 0) return;
1036 auto spans = static_cast<SwSpan*>(malloc(sizeof(SwSpan) * (rle->size)));
1038 auto spansEnd = _intersectSpansRect(clip, rle, spans, rle->size);
1040 _replaceClipSpan(rle, spans, spansEnd - spans);
1042 TVGLOG("SW_ENGINE", "Using ClipRect!");