Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove setters #434

Merged
merged 32 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
df536f9
#429 [refactor] make class PagePosition immutable
asolntsev Oct 29, 2024
2805e2c
#429 [refactor] make class `FontDescription` immutable
asolntsev Oct 29, 2024
6eab044
#429 [refactor] make class `ITextFSFontMetrics` immutable
asolntsev Oct 29, 2024
2189ed4
#429 [refactor] return Optional instead of null from `ITextOutputDevi…
asolntsev Oct 29, 2024
6d9adeb
#429 [refactor] make class `PDFAsImage` immutable
asolntsev Oct 29, 2024
1b2835d
#429 [refactor] make class `PDFEncryption` immutable
asolntsev Oct 29, 2024
f8bf190
#429 [refactor] make class `PageRule` almost immutable :)
asolntsev Oct 30, 2024
0ce4d71
#429 [refactor] make method `resetNegativeValues()` return new instance
asolntsev Oct 30, 2024
2ed9922
#429 [refactor] make class `FloatLayoutResult` immutable
asolntsev Oct 30, 2024
c185740
#429 [refactor] make class `FloatManager` immutable
asolntsev Oct 30, 2024
ea82480
#429 [refactor] convert constants `FloatManager.{LEFT,RIGHT}` to enum
asolntsev Oct 30, 2024
03b652b
#429 [refactor] make class `FunctionData` immutable
asolntsev Oct 30, 2024
0ab7acc
#429 [refactor] make class `InlineBox` more immutable :)
asolntsev Oct 30, 2024
1aa5a3f
#429 [refactor] make few fields in `LineBreakContext` immutable or no…
asolntsev Oct 30, 2024
bbc588c
#429 [refactor] make class `InlineBoxMeasurements` immutable
asolntsev Oct 30, 2024
8e31d9d
#429 [refactor] make field `Layer._stackingContext` immutable
asolntsev Oct 30, 2024
f4e656c
#429 [refactor] convert constants Layer.{AUTO|NEGATIVE|POSITIVE|ZERO}…
asolntsev Oct 30, 2024
b8aa47a
#429 [refactor] make class `PaintingInfo` immutable
asolntsev Oct 30, 2024
99761e0
#429 [refactor] replace few setters in `InlineBoxMeasurements` by con…
asolntsev Oct 30, 2024
3223974
#429 [refactor] replace `setElement()` and `setStyle()` by constructo…
asolntsev Oct 30, 2024
ad1643b
#429 [refactor] remove unused setter `TableSectionBox.setGrid()`
asolntsev Oct 30, 2024
6839559
#429 [refactor] replace `Box.setAnonymous()` by a constructor parameter
asolntsev Oct 30, 2024
c13416d
#429 [refactor] make class `BoxDimensions` immutable
asolntsev Oct 30, 2024
6313a0e
#429 [refactor] make class `FloatDistances` (and its subclasses) immu…
asolntsev Oct 30, 2024
00b4cc4
#429 [refactor] make field `InlineLayoutBox._containingBlockWidth` im…
asolntsev Oct 30, 2024
89ca3d1
#429 [refactor] make class `JustificationInfo` immutable
asolntsev Oct 30, 2024
8f91572
#429 [refactor] make fields `PageBox.{_top,_bottom}` immutable
asolntsev Oct 30, 2024
d401a10
#429 [refactor] make field `PageBox._pageNo` immutable
asolntsev Oct 30, 2024
c323447
#429 [refactor] remove unused setters from `StrutMetrics`
asolntsev Oct 30, 2024
f871178
#429 [refactor] remove unused package `org.xhtmlrenderer.simple.xhtml`
asolntsev Oct 30, 2024
b46fc2d
#429 [refactor] make class `ScalingOptions` immutable
asolntsev Oct 30, 2024
d159980
#429 [refactor] make class `DocumentSplitter.NamespaceScope` immutable
asolntsev Oct 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
*/
package org.xhtmlrenderer.css.constants;

import com.google.errorprone.annotations.CheckReturnValue;
import org.jspecify.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -73,6 +76,8 @@ public String toString() {
return _ident;
}

@Nullable
@CheckReturnValue
public static MarginBoxName valueOf(String ident) {
return ALL.get(ident);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,21 +519,23 @@ private void page(Stylesheet stylesheet) throws IOException {
//System.out.println("page()");
Token t = next();
try {
PageRule pageRule = new PageRule(stylesheet.getOrigin());
if (t == Token.TK_PAGE_SYM) {
String pageName = null;
String pseudoPage = null;
Map<MarginBoxName, List<PropertyDeclaration>> margins = new HashMap<>();

skip_whitespace();
t = la();
if (t == Token.TK_IDENT) {
String pageName = getTokenValue(t);
pageName = getTokenValue(t);
if (pageName.equals("auto")) {
throw new CSSParseException("page name may not be auto", getCurrentLine());
}
next();
pageRule.setName(pageName);
t = la();
}
if (t == Token.TK_COLON) {
pageRule.setPseudoPage(pseudo_page());
pseudoPage = pseudo_page();
}
Ruleset ruleset = new Ruleset(stylesheet.getOrigin());

Expand All @@ -548,7 +550,7 @@ private void page(Stylesheet stylesheet) throws IOException {
skip_whitespace();
break;
} else if (t == Token.TK_AT_RULE) {
margin(stylesheet, pageRule);
margins = margin(stylesheet);
} else {
declaration_list(ruleset, false, true, false);
}
Expand All @@ -558,7 +560,7 @@ private void page(Stylesheet stylesheet) throws IOException {
throw new CSSParseException(t, Token.TK_LBRACE, getCurrentLine());
}

pageRule.addContent(ruleset);
PageRule pageRule = new PageRule(stylesheet.getOrigin(), pageName, pseudoPage, margins, ruleset);
stylesheet.addContent(pageRule);
} else {
push(t);
Expand All @@ -573,19 +575,22 @@ private void page(Stylesheet stylesheet) throws IOException {
// margin :
// margin_sym S* '{' declaration [ ';' S* declaration? ]* '}' S*
// ;
private void margin(Stylesheet stylesheet, PageRule pageRule) throws IOException {
private Map<MarginBoxName, List<PropertyDeclaration>> margin(Stylesheet stylesheet) throws IOException {
Map<MarginBoxName, List<PropertyDeclaration>> margins = new HashMap<>();

Token t = next();
if (t != Token.TK_AT_RULE) {
error(new CSSParseException(t, Token.TK_AT_RULE, getCurrentLine()), "at rule", true);
recover(true, false);
return;
return margins;
}

String name = getTokenValue(t);
MarginBoxName marginBoxName = MarginBoxName.valueOf(name);
if (marginBoxName == null) {
error(new CSSParseException(name + " is not a valid margin box name", getCurrentLine()), "at rule", true);
recover(true, false);
return;
return margins;
}

skip_whitespace();
Expand All @@ -600,7 +605,7 @@ private void margin(Stylesheet stylesheet, PageRule pageRule) throws IOException
push(t);
throw new CSSParseException(t, Token.TK_RBRACE, getCurrentLine());
}
pageRule.addMarginBoxProperties(marginBoxName, ruleset.getPropertyDeclarations());
margins.put(marginBoxName, ruleset.getPropertyDeclarations());
} else {
push(t);
throw new CSSParseException(t, Token.TK_LBRACE, getCurrentLine());
Expand All @@ -609,6 +614,7 @@ private void margin(Stylesheet stylesheet, PageRule pageRule) throws IOException
error(e, "margin box", true);
recover(false, false);
}
return margins;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,50 +28,41 @@
import java.util.Map;

public class PageRule implements RulesetContainer {
private String _name;
private String _pseudoPage;
private Ruleset _ruleset;
private final String _name;
private final String _pseudoPage;
private final Ruleset _ruleset;
private final Origin _origin;

private final Map<MarginBoxName, List<PropertyDeclaration>> _marginBoxes = new HashMap<>();

private int _pos;

private int _specificityF;
private int _specificityG;
private int _specificityH;
private final int _specificityF;
private final int _specificityG;
private final int _specificityH;

public PageRule(Origin origin) {
public PageRule(Origin origin, @Nullable String name, @Nullable String pseudoPage, Map<MarginBoxName, List<PropertyDeclaration>> marginBoxes, Ruleset ruleset) {
_origin = origin;
_name = name;
_specificityF = name == null ? 0 : 1;
_ruleset = ruleset;
_pseudoPage = pseudoPage;
_specificityG = "first".equals(pseudoPage) ? 1 : 0;
_specificityH = "first".equals(pseudoPage) ? 0 : 1;
_marginBoxes.putAll(marginBoxes);
}

public String getPseudoPage() {
return _pseudoPage;
}

public void setPseudoPage(String pseudoPage) {
_pseudoPage = pseudoPage;
if (pseudoPage.equals("first")) {
_specificityG = 1;
} else {
_specificityH = 1;
}
}

public Ruleset getRuleset() {
return _ruleset;
}

public void setRuleset(Ruleset ruleset) {
_ruleset = ruleset;
}

@Override
public void addContent(Ruleset ruleset) {
if (_ruleset != null) {
throw new IllegalStateException("Ruleset has already been set");
}
_ruleset = ruleset;
throw new IllegalStateException("Ruleset has already been set");
}

@Override
Expand All @@ -83,19 +74,10 @@ public String getName() {
return _name;
}

public void setName(String name) {
_name = name;
_specificityF = 1;
}

public List<PropertyDeclaration> getMarginBoxProperties(MarginBoxName name) {
return _marginBoxes.get(name);
}

public void addMarginBoxProperties(MarginBoxName name, List<PropertyDeclaration> props) {
_marginBoxes.put(name, props);
}

public Map<MarginBoxName, List<PropertyDeclaration>> getMarginBoxes() {
return _marginBoxes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;

import static org.xhtmlrenderer.css.constants.CSSName.PADDING_SIDE_PROPERTIES;
import static org.xhtmlrenderer.css.style.CssKnowledge.BLOCK_EQUIVALENTS;
import static org.xhtmlrenderer.css.style.CssKnowledge.BORDERS_NOT_ALLOWED;
import static org.xhtmlrenderer.css.style.CssKnowledge.LAID_OUT_IN_INLINE_CONTEXT;
Expand Down Expand Up @@ -474,18 +475,14 @@ public RectPropertySet getMarginRect(float cbWidth, CssContext ctx, boolean useC
*
* @return The paddingWidth value
*/
public RectPropertySet getPaddingRect(float cbWidth, CssContext ctx, boolean useCache) {
public RectPropertySet getPaddingRect(float cbWidth, CssContext ctx) {
if (! _paddingAllowed) {
return RectPropertySet.ALL_ZEROS;
} else {
return getPaddingProperty(this, cbWidth, ctx, useCache);
return getPaddingProperty(this, cbWidth, ctx);
}
}

public RectPropertySet getPaddingRect(float cbWidth, CssContext ctx) {
return getPaddingRect(cbWidth, ctx, true);
}

public String getStringProperty(CSSName cssName) {
return valueByName(cssName).asString();
}
Expand Down Expand Up @@ -593,28 +590,13 @@ public RectPropertySet getCachedPadding() {

private static RectPropertySet getPaddingProperty(CalculatedStyle style,
float cbWidth,
CssContext ctx,
boolean useCache) {
if (! useCache) {
return newRectInstance(style, CSSName.PADDING_SIDE_PROPERTIES, cbWidth, ctx);
} else {
if (style._padding == null) {
RectPropertySet result = newRectInstance(style, CSSName.PADDING_SIDE_PROPERTIES, cbWidth, ctx);
boolean allZeros = result.isAllZeros();

if (allZeros) {
result = RectPropertySet.ALL_ZEROS;
}

style._padding = result;

if (! allZeros && style._padding.hasNegativeValues()) {
style._padding.resetNegativeValues();
}
}

return style._padding;
CssContext ctx) {
if (style._padding == null) {
style._padding = newRectInstance(style, PADDING_SIDE_PROPERTIES, cbWidth, ctx)
.resetNegativeValues();
}

return style._padding;
}

private static RectPropertySet getMarginProperty(CalculatedStyle style,
Expand All @@ -625,11 +607,7 @@ private static RectPropertySet getMarginProperty(CalculatedStyle style,
return newRectInstance(style, CSSName.MARGIN_SIDE_PROPERTIES, cbWidth, ctx);
} else {
if (style._margin == null) {
RectPropertySet result = newRectInstance(style, CSSName.MARGIN_SIDE_PROPERTIES, cbWidth, ctx);
if (result.isAllZeros()) {
result = RectPropertySet.ALL_ZEROS;
}
style._margin = result;
style._margin = newRectInstance(style, CSSName.MARGIN_SIDE_PROPERTIES, cbWidth, ctx);
}

return style._margin;
Expand All @@ -640,29 +618,18 @@ private static RectPropertySet newRectInstance(CalculatedStyle style,
CSSName.CSSSideProperties sides,
float cbWidth,
CssContext ctx) {
RectPropertySet rect;
rect = RectPropertySet.newInstance(style,
sides,
cbWidth,
ctx);
RectPropertySet rect = RectPropertySet.newInstance(style, sides, cbWidth, ctx);

if (rect.isAllZeros()) {
rect = RectPropertySet.ALL_ZEROS;
}
return rect;
}

private static BorderPropertySet getBorderProperty(CalculatedStyle style,
@Nullable CssContext ctx) {
if (style._border == null) {
BorderPropertySet result = BorderPropertySet.newInstance(style, ctx);

boolean allZeros = result.isAllZeros();
if (allZeros && ! result.hasHidden() && !result.hasBorderRadius()) {
result = BorderPropertySet.EMPTY_BORDER;
}

style._border = result;

if (! allZeros && style._border.hasNegativeValues()) {
style._border.resetNegativeValues();
}
style._border = BorderPropertySet.newInstance(style, ctx).resetNegativeValues();
}
return style._border;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.awt.*;

import static java.lang.Math.max;
import static org.xhtmlrenderer.css.constants.CSSName.BORDER_BOTTOM_COLOR;
import static org.xhtmlrenderer.css.constants.CSSName.BORDER_BOTTOM_LEFT_RADIUS;
import static org.xhtmlrenderer.css.constants.CSSName.BORDER_BOTTOM_RIGHT_RADIUS;
Expand Down Expand Up @@ -188,7 +189,11 @@ public static BorderPropertySet newInstance(
CalculatedStyle style,
@Nullable CssContext ctx
) {
return new BorderPropertySet(style, ctx);
BorderPropertySet result = new BorderPropertySet(style, ctx);
return result.isAllZeros() && !result.hasHidden() && !result.hasBorderRadius() ?
BorderPropertySet.EMPTY_BORDER :
result;

}

@Override
Expand Down Expand Up @@ -305,8 +310,13 @@ public BorderPropertySet normalizedInstance(Rectangle bounds) {
* helper function for normalizeBorderRadius. Gets the max side width for each of the corners or the side width whichever is larger
*/
private float getSideLength(BorderRadiusCorner left, BorderRadiusCorner right, float sideLength) {
return Math.max(sideLength, left.getMaxRight(sideLength) + right.getMaxLeft(sideLength));
return max(sideLength, left.getMaxRight(sideLength) + right.getMaxLeft(sideLength));
}

@CheckReturnValue
@Override
public BorderPropertySet resetNegativeValues() {
return new BorderPropertySet(max(0, top()), max(0, right()), max(0, bottom()), max(0, left()), styles, corners, colors);
}
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.xhtmlrenderer.css.style.derived;

import com.google.errorprone.annotations.CheckReturnValue;
import org.xhtmlrenderer.css.constants.CSSName;
import org.xhtmlrenderer.css.style.CalculatedStyle;
import org.xhtmlrenderer.css.style.CssContext;
Expand Down Expand Up @@ -28,6 +29,7 @@ public RectPropertySet(
this._left = left;
}

@SuppressWarnings("SuspiciousNameCombination")
public static RectPropertySet newInstance(
CalculatedStyle style,
CSSName.CSSSideProperties sideProperties,
Expand Down Expand Up @@ -104,18 +106,8 @@ public boolean hasNegativeValues() {
return _top < 0 || _right < 0 || _bottom < 0 || _left < 0;
}

public void resetNegativeValues() {
if (top() < 0) {
setTop(0);
}
if (right() < 0) {
setRight(0);
}
if (bottom() < 0) {
setBottom(0);
}
if (left() < 0) {
setLeft(0);
}
@CheckReturnValue
public RectPropertySet resetNegativeValues() {
return new RectPropertySet(Math.max(0, top()), Math.max(0, right()), Math.max(0, bottom()), Math.max(0, left()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
public interface FSImage {
int getWidth();
int getHeight();
void scale(int width, int height);
FSImage scale(int width, int height);

default boolean hasSize(int width, int height) {
return getWidth() == width && getHeight() == height;
Expand Down
Loading