Skip to content

Commit

Permalink
#429 [refactor] make class JustificationInfo immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Oct 30, 2024
1 parent 00b4cc4 commit 89ca3d1
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,9 @@ public float calcTotalAdjustment(JustificationInfo info) {
for (int i = 0; i < len; i++) {
char c = s.charAt(i);
if (c == ' ' || c == '\u00a0' || c == '\u3000') {
result += info.getSpaceAdjust();
result += info.spaceAdjust();
} else {
result += info.getNonSpaceAdjust();
result += info.nonSpaceAdjust();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,5 @@
*/
package org.xhtmlrenderer.render;

public class JustificationInfo {
private float _nonSpaceAdjust;
private float _spaceAdjust;

public float getNonSpaceAdjust() {
return _nonSpaceAdjust;
}

public void setNonSpaceAdjust(float nonSpaceAdjust) {
_nonSpaceAdjust = nonSpaceAdjust;
}

public float getSpaceAdjust() {
return _spaceAdjust;
}

public void setSpaceAdjust(float spaceAdjust) {
_spaceAdjust = spaceAdjust;
}
public record JustificationInfo(float nonSpaceAdjust, float spaceAdjust) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

import static java.lang.System.lineSeparator;
import static java.util.Collections.emptyList;
import static org.xhtmlrenderer.css.constants.CSSName.LETTER_SPACING;
import static org.xhtmlrenderer.css.constants.IdentValue.NORMAL;
import static org.xhtmlrenderer.render.Box.Dump.RENDER;

/**
Expand Down Expand Up @@ -228,31 +230,28 @@ public void justify() {

CharCounts counts = countJustifiableChars();

JustificationInfo info = new JustificationInfo();
if (! getParent().getStyle().isIdent(CSSName.LETTER_SPACING, IdentValue.NORMAL)) {
info.setNonSpaceAdjust(0.0f);
info.setSpaceAdjust((float)toAdd / counts.getSpaceCount());
} else {
if (counts.getNonSpaceCount() > 1) {
info.setNonSpaceAdjust(toAdd * JUSTIFY_NON_SPACE_SHARE / (counts.getNonSpaceCount()-1));
} else {
info.setNonSpaceAdjust(0.0f);
}

if (counts.getSpaceCount() > 0) {
info.setSpaceAdjust(toAdd * JUSTIFY_SPACE_SHARE / counts.getSpaceCount());
} else {
info.setSpaceAdjust(0.0f);
}
}
JustificationInfo info = !getParent().getStyle().isIdent(LETTER_SPACING, NORMAL) ?
new JustificationInfo(0.0f, (float) toAdd / counts.getSpaceCount()) :
justificationInfo(counts, toAdd);

adjustChildren(info);

setJustificationInfo(info);
}
}
}

private static JustificationInfo justificationInfo(CharCounts counts, int toAdd) {
float nonSpaceAdjust = counts.getNonSpaceCount() > 1 ?
toAdd * JUSTIFY_NON_SPACE_SHARE / (counts.getNonSpaceCount() - 1) :
0.0f;

float spaceAdjust = counts.getSpaceCount() > 0 ?
toAdd * JUSTIFY_SPACE_SHARE / counts.getSpaceCount() :
0.0f;

return new JustificationInfo(nonSpaceAdjust, spaceAdjust);
}

private void adjustChildren(JustificationInfo info) {
float adjust = 0.0f;
for (Box b : getChildren()) {
Expand Down Expand Up @@ -499,7 +498,7 @@ public void trimTrailingSpace(LayoutContext c) {
if (text != null) {
InlineLayoutBox iB = text.getParent();
IdentValue whitespace = iB.getStyle().getWhitespace();
if (whitespace == IdentValue.NORMAL || whitespace == IdentValue.NOWRAP) {
if (whitespace == NORMAL || whitespace == IdentValue.NOWRAP) {
text.trimTrailingSpace(c);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ private void drawSelectedText(RenderingContext c, InlineText inlineText, InlineL
i, new Point2D.Double(point.getX() + adjust, point.getY()));
}
if (ch == ' ' || ch == '\u00a0' || ch == '\u3000') {
adjust += info.getSpaceAdjust();
adjust += info.spaceAdjust();
} else {
adjust += info.getNonSpaceAdjust();
adjust += info.nonSpaceAdjust();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ private void adjustGlyphPositions(
i, new Point2D.Double(point.getX() + adjust, point.getY()));
}
if (c == ' ' || c == '\u00a0' || c == '\u3000') {
adjust += info.getSpaceAdjust();
adjust += info.spaceAdjust();
} else {
adjust += info.getNonSpaceAdjust();
adjust += info.nonSpaceAdjust();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -588,9 +588,9 @@ private PdfTextArray makeJustificationArray(String s, JustificationInfo info) {
if (i != len - 1) {
float offset;
if (c == ' ' || c == '\u00a0' || c == '\u3000') {
offset = info.getSpaceAdjust();
offset = info.spaceAdjust();
} else {
offset = info.getNonSpaceAdjust();
offset = info.nonSpaceAdjust();
}
array.add((-offset / _dotsPerPoint) * 1000 / (_font.getSize2D() / _dotsPerPoint));
}
Expand Down

0 comments on commit 89ca3d1

Please sign in to comment.