Skip to content

Commit

Permalink
Cleaned up text rectangle code + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DougLau committed Jan 3, 2025
1 parent 19904ff commit 7173958
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/us/mn/state/dot/tms/SignConfigHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static public TextRect textRect(SignConfig sc) {
int c_height = sc.getCharHeight();
int fn = sc.getDefaultFont();
return new TextRect(1, 1, 1, width, height, c_height,
fn);
fn, true);
} else
return null;
}
Expand Down
35 changes: 17 additions & 18 deletions src/us/mn/state/dot/tms/utils/TextRect.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
*
* @author Douglas Lau
* @author John L. Stanley - SRF Consulting
*/
public class TextRect {
public final int page_number;
Expand All @@ -42,21 +41,29 @@ public class TextRect {
public final int height;
public final int c_height;
public final int font_num;
public boolean implied;
public final boolean implied;

/** Glyph width cache */
private HashMap<Integer, Integer> glyph_widths;

/** Create a new text rectangle */
public TextRect(int pn, int x, int y, int w, int h, int ch, int fn) {
public TextRect(int pn, int x, int y, int w, int h, int ch, int fn,
boolean imp)
{
page_number = pn;
rx = x;
ry = y;
width = w;
height = h;
c_height = ch;
font_num = fn;
implied = false;
implied = imp;
}

/** Create an implied full-page TextRect */
private TextRect pageRect(int page, int font_cur) {
return new TextRect(page, rx, ry, width, height, c_height,
font_cur, true);
}

/** Compare with another text rectangle for equality.
Expand Down Expand Up @@ -97,7 +104,7 @@ abstract private class Scanner extends MultiAdapter {

private Scanner() {
font_cur = font_num;
page_rect = genImpliedPageRect(page, font_cur);
page_rect = pageRect(page, font_cur);
rect = page_rect;
fillable = true;
}
Expand All @@ -120,7 +127,7 @@ void startRect(TextRect tr) {
}
@Override public void addPage() {
page++;
page_rect = genImpliedPageRect(page, font_cur);
page_rect = pageRect(page, font_cur);
startRect(page_rect);
}
@Override public void setTextRectangle(int x, int y,
Expand All @@ -133,7 +140,7 @@ void startRect(TextRect tr) {
if (rect.equals(page_rect))
fillable = false;
startRect(new TextRect(page, x, y, w, h, c_height,
font_cur));
font_cur, false));
}
}

Expand Down Expand Up @@ -163,7 +170,7 @@ private Filler(List<TextRect> trs, List<String> lns) {
rects = trs;
lines = lns.iterator();
font_cur = font_num;
fillRect(genImpliedPageRect(page, font_cur));
fillRect(pageRect(page, font_cur));
}

private void fillRect(TextRect tr) {
Expand All @@ -189,7 +196,7 @@ private void fillRect(TextRect tr) {
@Override public void addPage() {
super.addPage();
page++;
fillRect(genImpliedPageRect(page, font_cur));
fillRect(pageRect(page, font_cur));
}
@Override public void setTextRectangle(int x, int y,
int w, int h)
Expand All @@ -200,7 +207,7 @@ private void fillRect(TextRect tr) {
if (h == 0)
h = height - (y - 1);
fillRect(new TextRect(page, x, y, w, h, c_height,
font_cur));
font_cur, false));
}
@Override public void addFeed(String fid) {
// strip feed tags
Expand Down Expand Up @@ -393,12 +400,4 @@ public String checkLine(String ms, boolean abbrev) {
}
return null;
}

/** Generate an implied full-page TextRect */
private TextRect genImpliedPageRect(int page, int font_cur) {
TextRect tr = new TextRect(page, rx, ry, width, height,
c_height, font_cur);
tr.implied = true;
return tr;
}
}
32 changes: 16 additions & 16 deletions test/us/mn/state/dot/tms/utils/TextRectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class TextRectTest extends TestCase {

// page size for 3 lines of text
final TextRect tr3 = new TextRect(1, 1, 1, 50, 26, 0, 1);
final TextRect tr3 = new TextRect(1, 1, 1, 50, 26, 0, 1, true);

public TextRectTest(String name) {
super(name);
Expand All @@ -45,34 +45,34 @@ public void testFind2() {
public void testFind3() {
List<TextRect> rects = tr3.find("[np]");
assertTrue(rects.size() == 2);
assertTrue(rects.get(0).equals(new TextRect(1, 1, 1, 50, 26, 0, 1)));
assertTrue(rects.get(1).equals(new TextRect(2, 1, 1, 50, 26, 0, 1)));
assertTrue(rects.get(0).equals(new TextRect(1, 1, 1, 50, 26, 0, 1, true)));
assertTrue(rects.get(1).equals(new TextRect(2, 1, 1, 50, 26, 0, 1, true)));
}

public void testFind4() {
List<TextRect> rects = tr3.find("FIRST[np]");
assertTrue(rects.size() == 1);
assertTrue(rects.get(0).equals(new TextRect(2, 1, 1, 50, 26, 0, 1)));
assertTrue(rects.get(0).equals(new TextRect(2, 1, 1, 50, 26, 0, 1, true)));
}

public void testFind5() {
List<TextRect> rects = tr3.find("[np]SECOND");
assertTrue(rects.size() == 1);
assertTrue(rects.get(0).equals(new TextRect(1, 1, 1, 50, 26, 0, 1)));
assertTrue(rects.get(0).equals(new TextRect(1, 1, 1, 50, 26, 0, 1, true)));
}

public void testFind6() {
List<TextRect> rects = tr3.find("[np][np]");
assertTrue(rects.size() == 3);
assertTrue(rects.get(0).equals(new TextRect(1, 1, 1, 50, 26, 0, 1)));
assertTrue(rects.get(1).equals(new TextRect(2, 1, 1, 50, 26, 0, 1)));
assertTrue(rects.get(2).equals(new TextRect(3, 1, 1, 50, 26, 0, 1)));
assertTrue(rects.get(0).equals(new TextRect(1, 1, 1, 50, 26, 0, 1, true)));
assertTrue(rects.get(1).equals(new TextRect(2, 1, 1, 50, 26, 0, 1, true)));
assertTrue(rects.get(2).equals(new TextRect(3, 1, 1, 50, 26, 0, 1, true)));
}

public void testFind7() {
List<TextRect> rects = tr3.find("[tr1,1,50,24]");
assertTrue(rects.size() == 1);
assertTrue(rects.get(0).equals(new TextRect(1, 1, 1, 50, 24, 0, 1)));
assertTrue(rects.get(0).equals(new TextRect(1, 1, 1, 50, 24, 0, 1, false)));
}

public void testFind8() {
Expand All @@ -83,25 +83,25 @@ public void testFind8() {
public void testFind9() {
List<TextRect> rects = tr3.find("[tr1,1,50,24][tr1,25,50,24]");
assertTrue(rects.size() == 2);
assertTrue(rects.get(0).equals(new TextRect(1, 1, 1, 50, 24, 0, 1)));
assertTrue(rects.get(1).equals(new TextRect(1, 1, 25, 50, 24, 0, 1)));
assertTrue(rects.get(0).equals(new TextRect(1, 1, 1, 50, 24, 0, 1, false)));
assertTrue(rects.get(1).equals(new TextRect(1, 1, 25, 50, 24, 0, 1, false)));
}

public void testFind10() {
List<TextRect> rects = tr3.find(
"[tr1,1,50,24][fo2][tr1,25,50,24]");
assertTrue(rects.size() == 2);
assertTrue(rects.get(0).equals(new TextRect(1, 1, 1, 50, 24, 0, 1)));
assertTrue(rects.get(1).equals(new TextRect(1, 1, 25, 50, 24, 0, 2)));
assertTrue(rects.get(0).equals(new TextRect(1, 1, 1, 50, 24, 0, 1, false)));
assertTrue(rects.get(1).equals(new TextRect(1, 1, 25, 50, 24, 0, 2, false)));
}

public void testFind11() {
List<TextRect> rects = tr3.find(
"[tr1,1,50,24][fo2][tr1,25,50,24][fo3][np]");
assertTrue(rects.size() == 3);
assertTrue(rects.get(0).equals(new TextRect(1, 1, 1, 50, 24, 0, 1)));
assertTrue(rects.get(1).equals(new TextRect(1, 1, 25, 50, 24, 0, 2)));
assertTrue(rects.get(2).equals(new TextRect(2, 1, 1, 50, 26, 0, 3)));
assertTrue(rects.get(0).equals(new TextRect(1, 1, 1, 50, 24, 0, 1, false)));
assertTrue(rects.get(1).equals(new TextRect(1, 1, 25, 50, 24, 0, 2, false)));
assertTrue(rects.get(2).equals(new TextRect(2, 1, 1, 50, 26, 0, 3, true)));
}

public void testFillFail() {
Expand Down

0 comments on commit 7173958

Please sign in to comment.