11/*
2- * Copyright (c) 1998, 2018 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 1998, 2025 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
@@ -146,6 +146,8 @@ private AreaOp() {
146146 public static final int RSTAG_INSIDE = 1 ;
147147 public static final int RSTAG_OUTSIDE = -1 ;
148148
149+ public static final int MAX_LINK_COUNT = 1024 ;
150+
149151 public abstract void newRow ();
150152
151153 public abstract int classify (Edge e );
@@ -195,6 +197,25 @@ public int compare(Edge o1, Edge o2) {
195197 }
196198 };
197199
200+ private void consumeSubCurves (Vector <CurveLink > subcurves ,
201+ Vector <ChainEnd > chains ,
202+ Vector <Curve > curve ) {
203+ finalizeSubCurves (subcurves , chains );
204+ Enumeration <CurveLink > enum_ = subcurves .elements ();
205+ while (enum_ .hasMoreElements ()) {
206+ CurveLink link = enum_ .nextElement ();
207+ curve .add (link .getMoveto ());
208+ CurveLink nextlink = link ;
209+ while ((nextlink = nextlink .getNext ()) != null ) {
210+ if (!link .absorb (nextlink )) {
211+ curve .add (link .getSubCurve ());
212+ link = nextlink ;
213+ }
214+ }
215+ curve .add (link .getSubCurve ());
216+ }
217+ }
218+
198219 private Vector <Curve > pruneEdges (Vector <Edge > edges ) {
199220 int numedges = edges .size ();
200221 if (numedges < 2 ) {
@@ -218,6 +239,8 @@ private Vector<Curve> pruneEdges(Vector<Edge> edges) {
218239 Vector <CurveLink > subcurves = new Vector <>();
219240 Vector <ChainEnd > chains = new Vector <>();
220241 Vector <CurveLink > links = new Vector <>();
242+ Vector <Curve > ret = new Vector <>();
243+ int linkCount = 0 ;
221244 // Active edges are between left (inclusive) and right (exclusive)
222245 while (left < numedges ) {
223246 double y = yrange [0 ];
@@ -390,27 +413,22 @@ private Vector<Curve> pruneEdges(Vector<Edge> edges) {
390413 System .out .println (" " +link .getSubCurve ());
391414 }
392415 }
416+ // If we have complex area calculation, we should consume the
417+ // intermediate subcurves to optimize memory footprint
418+ if (linkCount >= MAX_LINK_COUNT ) {
419+ consumeSubCurves (subcurves , chains , ret );
420+ linkCount = 0 ;
421+ chains .clear ();
422+ subcurves .clear ();
423+ }
424+ linkCount += links .size ();
393425 resolveLinks (subcurves , chains , links );
394426 links .clear ();
395427 // Finally capture the bottom of the valid Y range as the top
396428 // of the next Y range.
397429 yrange [0 ] = yend ;
398430 }
399- finalizeSubCurves (subcurves , chains );
400- Vector <Curve > ret = new Vector <>();
401- Enumeration <CurveLink > enum_ = subcurves .elements ();
402- while (enum_ .hasMoreElements ()) {
403- CurveLink link = enum_ .nextElement ();
404- ret .add (link .getMoveto ());
405- CurveLink nextlink = link ;
406- while ((nextlink = nextlink .getNext ()) != null ) {
407- if (!link .absorb (nextlink )) {
408- ret .add (link .getSubCurve ());
409- link = nextlink ;
410- }
411- }
412- ret .add (link .getSubCurve ());
413- }
431+ consumeSubCurves (subcurves , chains , ret );
414432 return ret ;
415433 }
416434
0 commit comments