Skip to content

Commit

Permalink
Adopt the new CSSBox image API
Browse files Browse the repository at this point in the history
  • Loading branch information
radkovo committed Apr 9, 2020
1 parent 2e77d85 commit 9a5ca9a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 49 deletions.
118 changes: 69 additions & 49 deletions src/main/java/org/fit/cssbox/pdf/PDFRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary;
import org.apache.pdfbox.util.Matrix;
import org.fit.cssbox.awt.BackgroundBitmap;
import org.fit.cssbox.awt.BitmapImage;
import org.fit.cssbox.css.CSSUnits;
import org.fit.cssbox.layout.BackgroundImage;
import org.fit.cssbox.layout.Box;
Expand All @@ -61,6 +63,7 @@
import org.fit.cssbox.layout.ReplacedContent;
import org.fit.cssbox.layout.ReplacedImage;
import org.fit.cssbox.layout.TextBox;
import org.fit.cssbox.render.BackgroundImageImage;
import org.fit.cssbox.render.BoxRenderer;
import org.w3c.dom.Element;

Expand Down Expand Up @@ -1632,30 +1635,34 @@ private void insertImg(ReplacedBox box, int i, float plusOffset, float plusHeigh
{
if (cont instanceof ReplacedImage)
{
BufferedImage img = ((ReplacedImage) cont).getBufferedImage();
float pageStart = i * pageFormat.getHeight();
float pageEnd = (i + 1) * pageFormat.getHeight();

Rectangle cb = ((Box) box).getAbsoluteContentBounds();
if (img != null && cb.y * resCoef < pageEnd
&& (cb.y + img.getHeight()) * resCoef + plusHeight + plusOffset > pageStart)
final ReplacedImage rimg = (ReplacedImage) cont;
if (rimg.getImage() != null && rimg.getImage() instanceof BitmapImage)
{
img = filter.filterImg(img);
// calculates resized coordinates in CSSBox form
float startX = cb.x * resCoef;
float startY = (cb.y * resCoef + plusOffset + plusHeight) - i * pageFormat.getHeight(); // y position in the page
float width = (float) cb.getWidth() * resCoef;
float height = (float) cb.getHeight() * resCoef + plusHeight;
if (isBorderRad)
{ // if border radius is set
float radiusX = Math.max(Math.max(borRad.topLeftX, borRad.topRightX),
Math.max(borRad.botLeftX, borRad.botRightX));
float radiusY = Math.max(Math.max(borRad.topLeftY, borRad.topRightY),
Math.max(borRad.botLeftY, borRad.botRightY));
img = makeImgRadiusCorner(img, radiusX, radiusY);
BufferedImage img = ((BitmapImage) rimg.getImage()).getBufferedImage();
float pageStart = i * pageFormat.getHeight();
float pageEnd = (i + 1) * pageFormat.getHeight();

Rectangle cb = ((Box) box).getAbsoluteContentBounds();
if (img != null && cb.y * resCoef < pageEnd
&& (cb.y + img.getHeight()) * resCoef + plusHeight + plusOffset > pageStart)
{
img = filter.filterImg(img);
// calculates resized coordinates in CSSBox form
float startX = cb.x * resCoef;
float startY = (cb.y * resCoef + plusOffset + plusHeight) - i * pageFormat.getHeight(); // y position in the page
float width = (float) cb.getWidth() * resCoef;
float height = (float) cb.getHeight() * resCoef + plusHeight;
if (isBorderRad)
{ // if border radius is set
float radiusX = Math.max(Math.max(borRad.topLeftX, borRad.topRightX),
Math.max(borRad.botLeftX, borRad.botRightX));
float radiusY = Math.max(Math.max(borRad.topLeftY, borRad.topRightY),
Math.max(borRad.botLeftY, borRad.botRightY));
img = makeImgRadiusCorner(img, radiusX, radiusY);
}
// inserts image
insertImagePDFBox(img, startX, startY, width, height);
}
// inserts image
insertImagePDFBox(img, startX, startY, width, height);
}
}
}
Expand All @@ -1668,37 +1675,50 @@ private void insertImg(ReplacedBox box, int i, float plusOffset, float plusHeigh
private void insertBgImg(ElementBox elem, int i, float plusOffset, float plusHeight, Filter filter,
boolean isBorderRad, BorderRadius borRad) throws IOException
{
for (BackgroundImage bimg : elem.getBackgroundImages())
if (elem.getBackgroundImages() != null)
{
BufferedImage img = bimg.getBufferedImage();
float pageStart = i * pageFormat.getHeight();
float pageEnd = (i + 1) * pageFormat.getHeight();
if (img != null && elem.getAbsoluteContentY() * resCoef + plusOffset < pageEnd
&& (elem.getAbsoluteContentY() + img.getHeight()) * resCoef + plusOffset + plusHeight > pageStart)
final BackgroundBitmap bitmap = new BackgroundBitmap(elem);
for (BackgroundImage img : elem.getBackgroundImages())
{
img = filter.filterImg(img);
// calculates resized coordinates in CSSBox form
float startX = (elem.getAbsoluteContentX() - elem.getPadding().left) * resCoef;
float startY = (elem.getAbsoluteContentY() - elem.getPadding().top) * resCoef + plusOffset
- i * pageFormat.getHeight();
float width = img.getWidth() * resCoef;
float height = img.getHeight() * resCoef;

// correction of long backgrounds
if (height > 5 * plusHeight) height += plusHeight;

// if corner radius is set
if (isBorderRad)
{ // if border radius is set
float radiusX = Math.max(Math.max(borRad.topLeftX, borRad.topRightX),
Math.max(borRad.botLeftX, borRad.botRightX));
float radiusY = Math.max(Math.max(borRad.topLeftY, borRad.topRightY),
Math.max(borRad.botLeftY, borRad.botRightY));
img = makeImgRadiusCorner(img, radiusX * 2, radiusY * 2);
if (img instanceof BackgroundImageImage)
{
bitmap.addBackgroundImage((BackgroundImageImage) img);
}
}
if (bitmap.getBufferedImage() != null)
{
//final Rectangle bg = elem.getAbsoluteBorderBounds();
//g.drawImage(bitmap.getBufferedImage(), Math.round(bg.x), Math.round(bg.y), null);
BufferedImage img = bitmap.getBufferedImage();
float pageStart = i * pageFormat.getHeight();
float pageEnd = (i + 1) * pageFormat.getHeight();
if (img != null && elem.getAbsoluteContentY() * resCoef + plusOffset < pageEnd
&& (elem.getAbsoluteContentY() + img.getHeight()) * resCoef + plusOffset + plusHeight > pageStart)
{
img = filter.filterImg(img);
// calculates resized coordinates in CSSBox form
Rectangle bb = elem.getAbsoluteBorderBounds();
float startX = bb.x * resCoef;
float startY = bb.y * resCoef + plusOffset - i * pageFormat.getHeight();
float width = img.getWidth() * resCoef;
float height = img.getHeight() * resCoef;

// correction of long backgrounds
if (height > 5 * plusHeight) height += plusHeight;

// if corner radius is set
if (isBorderRad)
{ // if border radius is set
float radiusX = Math.max(Math.max(borRad.topLeftX, borRad.topRightX),
Math.max(borRad.botLeftX, borRad.botRightX));
float radiusY = Math.max(Math.max(borRad.topLeftY, borRad.topRightY),
Math.max(borRad.botLeftY, borRad.botRightY));
img = makeImgRadiusCorner(img, radiusX * 2, radiusY * 2);
}

// inserts image
insertImagePDFBox(img, startX, startY, width, height);
// inserts image
insertImagePDFBox(img, startX, startY, width, height);
}
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/fit/cssbox/pdf/PDFVisualContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.fit.cssbox.awt.GraphicsImageLoader;
import org.fit.cssbox.css.CSSUnits;
import org.fit.cssbox.css.FontSpec;
import org.fit.cssbox.css.FontTable;
import org.fit.cssbox.layout.BrowserConfig;
import org.fit.cssbox.layout.FontInfo;
import org.fit.cssbox.layout.ImageLoader;
import org.fit.cssbox.layout.VisualContext;

import cz.vutbr.web.css.CSSProperty.FontStyle;
Expand All @@ -59,6 +61,8 @@ public class PDFVisualContext extends VisualContext
private float ch; // 1ch length in points
private FontCache fontCache; //font cache to store already created fonts

/** Used image loader instance */
private ImageLoader imageLoader;

public PDFVisualContext(PDDocument doc, VisualContext parent, BrowserConfig config, FontTable fontTable)
{
Expand Down Expand Up @@ -286,4 +290,13 @@ private void updateMetrics()
}
}

//=========================================================================

@Override
public ImageLoader getImageLoader()
{
if (imageLoader == null)
imageLoader = new GraphicsImageLoader(this);
return imageLoader;
}
}

0 comments on commit 9a5ca9a

Please sign in to comment.