org.fit.cssbox.layout.Viewport Java Examples

The following examples show how to use org.fit.cssbox.layout.Viewport. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GraphicsRenderer.java    From CSSBox with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void renderElementBackground(ElementBox elem)
{
    AffineTransform origAt = null;
    AffineTransform at = Transform.createTransform(elem);
    if (at != null)
    {
        origAt = g.getTransform();
        g.transform(at);
    }
    
    if (elem instanceof Viewport)
        clearViewport((Viewport) elem);
    drawBackground(elem, g);
    
    if (origAt != null)
        g.setTransform(origAt);
}
 
Example #2
Source File: ElementBackground.java    From CSSBox with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Computes the target position of an image within the background according to its CSS
 * background-position and background-size values. Specifics of the viewport background
 * are considered as well.
 * @param img The image to be positioned
 * @return the target position and size of the image
 */
protected Rectangle computeTargetImagePosition(BackgroundImage img)
{
    final Rectangle pos;
    if (isViewportOwner() && ((Viewport) getOwner()).getRootBox() != null)
    {
        // compute the image position within the root box
        final ElementBox root = ((Viewport) getOwner()).getRootBox();
        pos = img.getComputedPosition(root);
        // position the image within the viewport
        Dimension ofs = ((Viewport) getOwner()).getBackgroundOffset();
        pos.x += ofs.width;
        pos.y += ofs.height;
    }
    else
    {
        pos = img.getComputedPosition();
    }
    return pos;
}
 
Example #3
Source File: ContentReader.java    From SwingBox with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Updates the layout. It is designed to do a re-layout only, not to process
 * input data again.
 * 
 * @param root
 *            the root
 * @param newDimension
 *            the new dimension
 * @param cba
 *            the CSSBoxAnalyzer
 * @return the list
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public List<ElementSpec> update(Viewport root, Dimension newDimension, CSSBoxAnalyzer cba) throws IOException
{
    if (cba == null)
        throw new IllegalArgumentException("CSSBoxAnalyzer can not be NULL !!!\nProvide your custom implementation or check instantiation of DefaultAnalyzer object...");

    elements = new LinkedList<ElementSpec>();
    elements.add(new ElementSpec(SimpleAttributeSet.EMPTY, ElementSpec.EndTagType));
    order = 0;

    Viewport vp;
    try
    {
        vp = cba.update(newDimension);
    } catch (Exception e)
    {
        throw new IOException(e);
    }

    vp.draw(this);
    
    return elements;
}
 
Example #4
Source File: ImageRenderer.java    From WebVector with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Renders the viewport using an SVGRenderer to the given output writer.
 *
 * @param vp
 * @param out
 * @throws IOException
 */
protected void writeSVG(Viewport vp, Writer out) throws IOException
{

    //obtain the viewport bounds depending on whether we are clipping to viewport size or using the whole page
    float w = vp.getClippedContentBounds().width;
    float h = vp.getClippedContentBounds().height;

    SVGDOMRenderer render = new SVGDOMRenderer(w, h, out);
    vp.draw(render);
    render.close();
}
 
Example #5
Source File: GraphicsEngine.java    From CSSBox with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected void renderViewport(Viewport viewport)
{
    // adds clearCanvas before rendering
    GraphicsRenderer r = (GraphicsRenderer) getRenderer();
    r.init(viewport);
    r.clearCanvas();
    viewport.draw(r);
    r.close();
}
 
Example #6
Source File: DefaultAnalyzer.java    From SwingBox with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Viewport update(Dimension dim)
        throws Exception
{
    canvas.createLayout(dim);
    return canvas.getViewport();
}
 
Example #7
Source File: DefaultAnalyzer.java    From SwingBox with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Viewport analyze(DocumentSource docSource, Dimension dim)
        throws Exception
{
    DOMSource parser = new DefaultDOMSource(docSource);
    w3cdoc = parser.parse();

    // Create the CSS analyzer
    DOMAnalyzer da = new DOMAnalyzer(w3cdoc, docSource.getURL());
    da.attributesToStyles();
    da.addStyleSheet(null, CSSNorm.stdStyleSheet(), DOMAnalyzer.Origin.AGENT);
    da.addStyleSheet(null, CSSNorm.userStyleSheet(), DOMAnalyzer.Origin.AGENT);
    da.getStyleSheets();
    
    BufferedImage tmpImg = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
    canvas = new BrowserCanvas(da.getRoot(), da, docSource.getURL());
    canvas.setImage(tmpImg);
    canvas.getConfig().setLoadImages(true);
    canvas.getConfig().setLoadBackgroundImages(true);
    canvas.createLayout(dim);

    return canvas.getViewport();
}
 
Example #8
Source File: ElementBackground.java    From CSSBox with GNU Lesser General Public License v3.0 5 votes vote down vote up
public ElementBackground(ElementBox owner)
{
    this.owner = owner;
    this.viewportOwner = (owner instanceof Viewport);
    bounds = owner.getAbsoluteBorderBounds();
    clipped = owner.getClippedBounds();
    if (viewportOwner)
        bounds = clipped;  //for the root box (Viewport), use the whole clipped content (not only the visible part)
    clipped = new Rectangle(clipped.x - bounds.x, clipped.y - bounds.y, clipped.width, clipped.height); //make the clip relative to the background bounds
}
 
Example #9
Source File: GraphicsRenderer.java    From CSSBox with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected void clearViewport(Viewport vp)
{
    Color bgcolor = convertColor(vp.getConfig().getViewportBackgroundColor());
    if (getViewportBackground() != null && getViewportBackground().getBgcolor() != null)
        bgcolor = convertColor(getViewportBackground().getBgcolor());
    
    Color color = g.getColor();
    g.setColor(bgcolor);
    g.fillRect(0, 0, Math.round(vp.getCanvasWidth()), Math.round(vp.getCanvasHeight()));
    g.setColor(color);
}
 
Example #10
Source File: StructuredRenderer.java    From CSSBox with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Returns the viewport for which the renderer was initialized.
 * @return The viewport box.
 */
public Viewport getViewport()
{
    return vp;
}
 
Example #11
Source File: ContentReader.java    From SwingBox with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Reads input data and converts them to "elements"
 * 
 * @param docSource
 *            the document source
 * @param cba
 *            the instance of {@link CSSBoxAnalyzer}
 * @param dim
 *            the dimension
 * @return the list of elements. Note that, this method returns instance of
 *         LinkedList.
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public List<ElementSpec> read(DocumentSource docSource, CSSBoxAnalyzer cba, Dimension dim)
        throws IOException
{
    // ale ked sa pouzije setText() neviem nic o url, nic sa nenastavuje ,
    // moze byt null
    // (URL) doc.getProperty(Document.StreamDescriptionProperty)

    if (cba == null)
        throw new IllegalArgumentException("CSSBoxAnalyzer can not be NULL !!!\nProvide your custom implementation or check instantiation of DefaultAnalyzer object...");

    elements = new Vector<ElementSpec>();// ArrayList<ElementSpec>(1024);
    elements.add(new ElementSpec(SimpleAttributeSet.EMPTY, ElementSpec.EndTagType));
    order = 0;

    // System.err.print("used Reader and encoding ? " +
    // is.getCharacterStream() + "  ,  ");
    // InputStreamReader r = (InputStreamReader)is.getCharacterStream();
    // System.err.println(r.getEncoding());

    Viewport vp;
    try
    {
        // System.err.println("analyzing...");
        vp = cba.analyze(docSource, dim);
        // System.err.println("analyzing finished...");
    } catch (Exception e)
    {
        throw new IOException(e);
    }

    //Use this for "drawing" the boxes. This constructs the element list.
    vp.draw(this);

    // System.err.println("num. of elements : " + elements.size());
    // System.err.println("Root min width : " + root.getMinimalWidth() +
    // " ,normal width : " + root.getWidth() + " ,maximal width : " +
    // root.getMaximalWidth());

    // TODO po skonceni nacitavania aj nejake info spravit
    // >> Document.TitleProperty - observer, metainfo
    return elements;
}
 
Example #12
Source File: StructuredRenderer.java    From CSSBox with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void init(Viewport vp)
{
    this.vp = vp;
    findViewportBackgroundSource();
}
 
Example #13
Source File: ContentReader.java    From SwingBox with GNU Lesser General Public License v3.0 4 votes vote down vote up
private SimpleAttributeSet buildElement(ElementBox box)
{
    if (box instanceof InlineBox)
    {
        return buildInlineBox((InlineBox) box);
    }
    else if (box instanceof Viewport)
    { // -- the boxes
        return buildViewport((Viewport) box);
    }
    else if (box instanceof TableBox)
    { // -- tables
        return buildTableBox((TableBox) box);
    }
    else if (box instanceof TableCaptionBox)
    {
        return buildTableCaptionBox((TableCaptionBox) box);
    }
    else if (box instanceof TableBodyBox)
    {
        return buildTableBodyBox((TableBodyBox) box);
    }
    else if (box instanceof TableRowBox)
    {
        return buildTableRowBox((TableRowBox) box);
    }
    else if (box instanceof TableCellBox)
    {
        return buildTableCellBox((TableCellBox) box);
    }
    else if (box instanceof TableColumnGroup)
    {
        return buildTableColumnGroup((TableColumnGroup) box);
    }
    else if (box instanceof TableColumn)
    {
        return buildTableColumn((TableColumn) box);
    }
    else if (box instanceof BlockTableBox)
    {
        return buildBlockTableBox((BlockTableBox) box);
    }
    else if (box instanceof ListItemBox)
    {
        return buildListItemBox((ListItemBox) box);
    }
    else if (box instanceof BlockBox)
    {
        return buildBlockBox((BlockBox) box);
    }
    else
    {
        System.err.println("Unknown BOX : " + box.getClass().getName());
        return null;
    }
}
 
Example #14
Source File: ContentReader.java    From SwingBox with GNU Lesser General Public License v3.0 4 votes vote down vote up
private SimpleAttributeSet buildViewport(Viewport box)
{
    return commonBuild(box, Constants.VIEWPORT);
}
 
Example #15
Source File: SwingBoxEditorKit.java    From SwingBox with GNU Lesser General Public License v3.0 3 votes vote down vote up
/**
 * Updates layout, using new dimensions.
 * 
 * @param doc
 *            the document
 * @param root
 *            the root box
 * @param dim
 *            new dimension
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public void update(SwingBoxDocument doc, Viewport root, Dimension dim)
        throws IOException
{
    ContentReader rdr = new ContentReader();
    List<ElementSpec> elements = rdr.update(root, dim, getCSSBoxAnalyzer());
    ElementSpec elementsArray[] = elements.toArray(new ElementSpec[0]);
    doc.create(elementsArray);
}
 
Example #16
Source File: CSSBoxAnalyzer.java    From SwingBox with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Analyzes content from InputSource and constructs a tree of Boxes, which
 * is further processed.
 * 
 * @param docSource
 *            the document source implementation.
 * @param dim
 *            the dimension of rendering area.
 * @return the tree of boxes.
 * @throws Exception
 *             some exception may be thrown during processing.
 */
Viewport analyze(DocumentSource docSource, Dimension dim) throws Exception;
 
Example #17
Source File: CSSBoxAnalyzer.java    From SwingBox with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Updates the layout according to the new dimension (the tree structure of
 * view objects has to be modified).
 * 
 * @param dim
 *            the new dimension of rendering area.
 * @return the box
 * @throws Exception
 *             some exception may be thrown during processing.
 */
Viewport update(Dimension dim) throws Exception;
 
Example #18
Source File: BoxRenderer.java    From CSSBox with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Initializes the renderer for rendering a ready-to-render viewport.
 * @param vp The viewport that will be rendered. The viewport must be fully initialized
 * and ready to draw.
 */
public void init(Viewport vp);