Skip to content

Commit

Permalink
make most fields in SharedContext non-nullable
Browse files Browse the repository at this point in the history
I wish them to be final as well, but `SharedContext` has many setters, and we need to keep them for backward compatibility. :(
  • Loading branch information
asolntsev committed Oct 14, 2024
1 parent 9846e47 commit b521bd9
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 86 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.xhtmlrenderer.css.style;

import com.google.errorprone.annotations.CheckReturnValue;
import org.jspecify.annotations.Nullable;
import org.xhtmlrenderer.context.StyleReference;
import org.xhtmlrenderer.css.value.FontSpecification;
import org.xhtmlrenderer.render.FSFont;
Expand All @@ -18,6 +20,8 @@ public interface CssContext {

float getXHeight(FontSpecification parentFont);

@Nullable
@CheckReturnValue
FSFont getFont(FontSpecification font);

// FIXME Doesn't really belong here, but this is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,14 @@ public float getXHeight(FontSpecification parentFont) {
return _sharedContext.getXHeight(getFontContext(), parentFont);
}

@Nullable
@CheckReturnValue
@Override
public FSFont getFont(FontSpecification font) {
return _sharedContext.getFont(font);
}

@CheckReturnValue
public UserAgentCallback getUac() {
return _sharedContext.getUac();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.xhtmlrenderer.layout;

import com.google.errorprone.annotations.CheckReturnValue;
import org.jspecify.annotations.Nullable;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
Expand All @@ -45,6 +46,7 @@
import org.xhtmlrenderer.render.RenderingContext;
import org.xhtmlrenderer.simple.extend.FormSubmissionListener;
import org.xhtmlrenderer.swing.Java2DTextRenderer;
import org.xhtmlrenderer.swing.NaiveUserAgent;
import org.xhtmlrenderer.swing.SwingReplacedElementFactory;
import org.xhtmlrenderer.util.XRLog;

Expand All @@ -55,6 +57,7 @@
import java.util.Set;

import static java.util.Arrays.asList;
import static java.util.Objects.requireNonNull;

/**
* The SharedContext is that which is kept between successive layout and render runs.
Expand Down Expand Up @@ -87,24 +90,41 @@ public final class SharedContext {

private int dotsPerPixel = 1;

@Nullable
private Map<Element, CalculatedStyle> styleMap;

private ReplacedElementFactory replacedElementFactory;
@Nullable
private Rectangle temporaryCanvas;

private LineBreakingStrategy lineBreakingStrategy = new DefaultLineBreakingStrategy();

public SharedContext() {
this(new NaiveUserAgent());
}

public SharedContext(UserAgentCallback userAgent, FontResolver fontResolver,
ReplacedElementFactory replacedElementFactory,
TextRenderer textRenderer,
float dpi, int dotsPerPixel) {
uac = requireNonNull(userAgent);
this.css = new StyleReference(userAgent);
this.fontResolver = requireNonNull(fontResolver);
this.replacedElementFactory = replacedElementFactory;
this.textRenderer = requireNonNull(textRenderer);
media = "screen";
setDPI(dpi);
setDotsPerPixel(dotsPerPixel);
}

public SharedContext(UserAgentCallback uac) {
fontResolver = new AWTFontResolver();
replacedElementFactory = new SwingReplacedElementFactory();
setMedia("screen");
this.uac = uac;
setCss(new StyleReference(uac));
this.media = "screen";
this.uac = requireNonNull(uac);
this.css = new StyleReference(uac);
XRLog.render("Using CSS implementation from: " + getCss().getClass().getName());
setTextRenderer(new Java2DTextRenderer());
this.textRenderer = new Java2DTextRenderer();
try {
setDPI(Toolkit.getDefaultToolkit().getScreenResolution());
} catch (HeadlessException e) {
Expand All @@ -114,13 +134,13 @@ public SharedContext(UserAgentCallback uac) {


public SharedContext(UserAgentCallback uac, FontResolver fr, ReplacedElementFactory ref, TextRenderer tr, float dpi) {
fontResolver = fr;
fontResolver = requireNonNull(fr);
replacedElementFactory = ref;
setMedia("screen");
this.media = "screen";
this.uac = uac;
setCss(new StyleReference(uac));
this.css = new StyleReference(uac);
XRLog.render("Using CSS implementation from: " + getCss().getClass().getName());
setTextRenderer(tr);
this.textRenderer = requireNonNull(tr);
setDPI(dpi);
}

Expand Down Expand Up @@ -148,6 +168,7 @@ public RenderingContext newRenderingContextInstance(OutputDevice outputDevice, F
*
* @return The fontResolver value
*/
@CheckReturnValue
public FontResolver getFontResolver() {
return fontResolver;
}
Expand All @@ -161,6 +182,7 @@ public void flushFonts() {
/**
* The media for this context
*/
@CheckReturnValue
public String getMedia() {
return media;
}
Expand All @@ -171,8 +193,10 @@ public String getMedia() {
private boolean debug_draw_inline_boxes;
private boolean debug_draw_font_metrics;

@Nullable
private FSCanvas canvas;

@CheckReturnValue
public TextRenderer getTextRenderer() {
return textRenderer;
}
Expand Down Expand Up @@ -222,6 +246,8 @@ public void setCss(StyleReference css) {
this.css = css;
}

@Nullable
@CheckReturnValue
public FSCanvas getCanvas() {
return canvas;
}
Expand All @@ -235,6 +261,7 @@ public void setTemporaryCanvas(Rectangle rect) {
}


@Nullable
public Rectangle getFixedRectangle() {
if (getCanvas() == null) {
return temporaryCanvas;
Expand All @@ -245,12 +272,14 @@ public Rectangle getFixedRectangle() {
}
}

@Nullable
private NamespaceHandler namespaceHandler;

public void setNamespaceHandler(NamespaceHandler nh) {
namespaceHandler = nh;
}

@Nullable
public NamespaceHandler getNamespaceHandler() {
return namespaceHandler;
}
Expand All @@ -259,6 +288,8 @@ public void addBoxId(String id, Box box) {
idMap.put(id, box);
}

@Nullable
@CheckReturnValue
public Box getBoxById(String id) {
return idMap.get(id);
}
Expand All @@ -278,7 +309,7 @@ public Map<String, Box> getIdMap()
* @param textRenderer The new textRenderer value
*/
public void setTextRenderer(TextRenderer textRenderer) {
this.textRenderer = textRenderer;
this.textRenderer = requireNonNull(textRenderer);
}

/**
Expand All @@ -291,14 +322,15 @@ public void setTextRenderer(TextRenderer textRenderer) {
* @param media The new media value
*/
public void setMedia(String media) {
this.media = media;
this.media = requireNonNull(media);
}

/**
* Gets the uac attribute of the RenderingContext object
*
* @return The uac value
*/
@CheckReturnValue
public UserAgentCallback getUac() {
return uac;
}
Expand All @@ -308,11 +340,8 @@ public UserAgentCallback getUserAgentCallback() {
}

public void setUserAgentCallback(UserAgentCallback userAgentCallback) {
StyleReference styleReference = getCss();
if (styleReference != null) {
styleReference.setUserAgentCallback(userAgentCallback);
}
uac = userAgentCallback;
getCss().setUserAgentCallback(userAgentCallback);
uac = requireNonNull(userAgentCallback);
}

/**
Expand Down Expand Up @@ -348,14 +377,16 @@ public float getMmPerPx() {
return mm_per_dot;
}

@Nullable
@CheckReturnValue
public FSFont getFont(FontSpecification spec) {
return getFontResolver().resolveFont(this, spec);
return fontResolver.resolveFont(this, spec);
}

//strike-through offset should always be half of the height of lowercase x...
//and it is defined even for fonts without 'x'!
public float getXHeight(FontContext fontContext, FontSpecification fs) {
FSFont font = getFontResolver().resolveFont(this, fs);
FSFont font = fontResolver.resolveFont(this, fs);
FSFontMetrics fm = getTextRenderer().getFSFontMetrics(fontContext, font, " ");
float sto = fm.getStrikethroughOffset();
return fm.getAscent() - 2 * Math.abs(sto) + fm.getStrikethroughThickness();
Expand Down Expand Up @@ -406,11 +437,7 @@ public boolean isPrint() {

public void setPrint(boolean print) {
this.print = print;
if (print) {
setMedia("print");
} else {
setMedia("screen");
}
setMedia(print ? "print" : "screen");
}

/**
Expand Down Expand Up @@ -440,14 +467,13 @@ public void setPrint(boolean print) {
* add a new font mapping, or replace an existing one
*/
public void setFontMapping(String name, Font font) {
FontResolver resolver = getFontResolver();
if (resolver instanceof AWTFontResolver) {
((AWTFontResolver)resolver).setFontMapping(name, font);
if (fontResolver instanceof AWTFontResolver) {
((AWTFontResolver) fontResolver).setFontMapping(name, font);
}
}

public void setFontResolver(FontResolver resolver) {
fontResolver = resolver;
fontResolver = requireNonNull(resolver);
}

public int getDotsPerPixel() {
Expand Down Expand Up @@ -507,9 +533,7 @@ public void setReplacedElementFactory(ReplacedElementFactory ref) {
throw new NullPointerException("replacedElementFactory may not be null");
}

if (this.replacedElementFactory != null) {
this.replacedElementFactory.reset();
}
this.replacedElementFactory.reset();
this.replacedElementFactory = ref;
}

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

import com.google.errorprone.annotations.CheckReturnValue;
import org.jspecify.annotations.Nullable;
import org.xhtmlrenderer.context.StyleReference;
import org.xhtmlrenderer.css.style.CssContext;
Expand Down Expand Up @@ -116,10 +117,14 @@ public FontResolver getFontResolver() {
return sharedContext.getFontResolver();
}

@Nullable
@CheckReturnValue
public FSFont getFont(FontSpecification font) {
return sharedContext.getFont(font);
}

@Nullable
@CheckReturnValue
public FSCanvas getCanvas() {
return sharedContext.getCanvas();
}
Expand Down Expand Up @@ -213,6 +218,8 @@ public int getInitialPageNo() {
return initialPageNo;
}

@Nullable
@CheckReturnValue
public Box getBoxById(String id) {
return sharedContext.getBoxById(id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.xhtmlrenderer.simple;

import org.jspecify.annotations.Nullable;
import org.w3c.dom.Document;
import org.xhtmlrenderer.layout.SharedContext;

Expand Down Expand Up @@ -55,6 +56,7 @@ public class Graphics2DRenderer {
/**
* Dimensions of the image to render, in pixels.
*/
@Nullable
protected Dimension dim;

/**
Expand All @@ -73,7 +75,7 @@ public Graphics2DRenderer() {
* @param g2 the canvas to layout on.
* @param dim dimensions of the container for the document
*/
public void layout(Graphics2D g2, Dimension dim) {
public void layout(Graphics2D g2, @Nullable Dimension dim) {
this.dim = dim;
if (dim != null) {
panel.setSize(dim);
Expand Down Expand Up @@ -115,15 +117,6 @@ public void setDocument(Document doc, String base_url) {
panel.setDocument(doc, base_url);
}

/**
* Sets the SharedContext for rendering.
*
* @param ctx The new renderingContext value
*/
public void setSharedContext(SharedContext ctx) {
panel.setSharedContext(ctx);
}

/**
* Returns the size image needed to render the document without anything
* going off the side. Could be different from the dimensions passed into
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ public class Java2DRenderer {
* Whether we've completed rendering; image will only be rendered once.
*/
private boolean rendered;
@Nullable
private String sourceDocument;
@Nullable
private String sourceDocumentBase;
private final int width;
private final int height;
Expand Down Expand Up @@ -324,14 +326,6 @@ private Java2DRenderer(int width, int height, int bufferedImageType) {

UserAgentCallback userAgent = new NaiveUserAgent();
sharedContext = new SharedContext(userAgent);

AWTFontResolver fontResolver = new AWTFontResolver();
sharedContext.setFontResolver(fontResolver);

SwingReplacedElementFactory replacedElementFactory = new SwingReplacedElementFactory();
sharedContext.setReplacedElementFactory(replacedElementFactory);

sharedContext.setTextRenderer(new Java2DTextRenderer());
sharedContext.setDPI(72 * (float) Java2DRenderer.DEFAULT_DOTS_PER_POINT);
sharedContext.setDotsPerPixel(Java2DRenderer.DEFAULT_DOTS_PER_PIXEL);
sharedContext.setPrint(false);
Expand Down
Loading

0 comments on commit b521bd9

Please sign in to comment.