@@ -117,6 +117,20 @@ public void setEnd(float v) {
117117 this .setRight (v );
118118 }
119119 }
120+
121+ private void setStartEnd (float start , float end ) {
122+ if (this .oblique ()) {
123+ throw new UnsupportedOperationException ();
124+ }
125+ if (this .vertical ()) {
126+ this .setTop (start );
127+ this .setBottom (end );
128+ }
129+ else {
130+ this .setLeft (start );
131+ this .setRight (end );
132+ }
133+ }
120134
121135 // -----
122136
@@ -396,23 +410,32 @@ public static List<Ruling> collapseOrientedRulings(List<Ruling> lines) {
396410
397411 public static List <Ruling > collapseOrientedRulings (List <Ruling > lines , int expandAmount ) {
398412 ArrayList <Ruling > rv = new ArrayList <Ruling >();
399- if (lines .size () == 0 ) {
400- return rv ;
401- }
402413 Collections .sort (lines , new Comparator <Ruling >() {
403414 @ Override
404415 public int compare (Ruling a , Ruling b ) {
405- return (int ) (!Utils .feq (a .getPosition (), b .getPosition ()) ? a .getPosition () - b .getPosition () : a .getStart () - b .getStart ());
416+ final float diff = a .getPosition () - b .getPosition ();
417+ return java .lang .Float .compare (diff == 0 ? a .getStart () - b .getStart () : diff , 0f );
406418 }
407419 });
408-
409- rv .add (lines .remove (0 ));
420+
410421 for (Ruling next_line : lines ) {
411- Ruling last = rv .get (rv .size () - 1 );
422+ Ruling last = rv .isEmpty () ? null : rv . get (rv .size () - 1 );
412423 // if current line colinear with next, and are "close enough": expand current line
413- if (Utils .feq (next_line .getPosition (), last .getPosition ()) && last .nearlyIntersects (next_line , expandAmount )) {
414- last .setStart (next_line .getStart () < last .getStart () ? next_line .getStart () : last .getStart ());
415- last .setEnd (next_line .getEnd () < last .getEnd () ? last .getEnd () : next_line .getEnd ());
424+ if (last != null && Utils .feq (next_line .getPosition (), last .getPosition ()) && last .nearlyIntersects (next_line , expandAmount )) {
425+ final float lastStart = last .getStart ();
426+ final float lastEnd = last .getEnd ();
427+
428+ final boolean lastFlipped = lastStart > lastEnd ;
429+ final boolean nextFlipped = next_line .getStart () > next_line .getEnd ();
430+
431+ boolean differentDirections = nextFlipped != lastFlipped ;
432+ float nextS = differentDirections ? next_line .getEnd () : next_line .getStart ();
433+ float nextE = differentDirections ? next_line .getStart () : next_line .getEnd ();
434+
435+ final float newStart = lastFlipped ? Math .max (nextS , lastStart ) : Math .min (nextS , lastStart );
436+ final float newEnd = lastFlipped ? Math .min (nextE , lastEnd ) : Math .max (nextE , lastEnd );
437+ last .setStartEnd (newStart , newEnd );
438+ assert !last .oblique ();
416439 }
417440 else if (next_line .length () == 0 ) {
418441 continue ;
0 commit comments