radkovo / CSSBox

CSSBox is an (X)HTML/CSS rendering engine written in pure Java. Its primary purpose is to provide a complete information about the rendered page suitable for further processing. However, it also allows displaying the rendered document.
http://cssbox.sourceforge.net/
GNU Lesser General Public License v3.0
234 stars 76 forks source link

Cannot wrap according to the displayed size #64

Open yunhaok opened 3 years ago

yunhaok commented 3 years ago

Can not automatically wrap according to the displayed size, and the style of automatic wrapping for the p tag or div setting does not take effect

yunhaok commented 3 years ago

Output picture: image

Case code:

import cz.vutbr.web.css.MediaSpec; import org.fit.cssbox.awt.BrowserCanvas; import org.fit.cssbox.css.CSSNorm; import org.fit.cssbox.css.DOMAnalyzer; import org.jsoup.Jsoup; import org.jsoup.helper.W3CDom; import org.jsoup.nodes.Document;

import javax.imageio.ImageIO; import java.awt.*; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.net.URL;

public class FileUtils {

private static String mediaType = "screen";
private static Dimension windowSize = new Dimension(1050, 768);
private static boolean cropWindow = false;
private static boolean loadImages = true;
private static boolean loadBackgroundImages = true;

public static void turnImage() throws Exception {
    int width, heigth;
    width = 80;
    heigth = 60;
    Document document = Jsoup.parse(template);
    W3CDom w3CDom = new W3CDom();
    org.w3c.dom.Document w3cDoc = w3CDom.fromJsoup(document);
    DOMAnalyzer da = new DOMAnalyzer(w3cDoc, null);
    MediaSpec media = new MediaSpec(mediaType);
    media.setDimensions((float) windowSize.width, (float) windowSize.height);
    media.setDeviceDimensions((float) windowSize.width, (float) windowSize.height);
    da.setMediaSpec(media);
    da.attributesToStyles();
    da.addStyleSheet((URL) null, CSSNorm.stdStyleSheet(), DOMAnalyzer.Origin.AGENT);
    da.addStyleSheet((URL) null, CSSNorm.userStyleSheet(), DOMAnalyzer.Origin.AGENT);
    da.addStyleSheet((URL) null, CSSNorm.formsStyleSheet(), DOMAnalyzer.Origin.AGENT);
    da.getStyleSheets();
    BrowserCanvas contentCanvas = new BrowserCanvas(w3cDoc.getDocumentElement(), da, null);
    contentCanvas.getConfig().setClipViewport(cropWindow);
    contentCanvas.getConfig().setLoadImages(loadImages);
    contentCanvas.getConfig().setLoadBackgroundImages(loadBackgroundImages);
    contentCanvas.createLayout(new org.fit.cssbox.layout.Dimension(width, heigth));
    File file = new File("E:\\temp\\aaa.png");
    if (!file.exists()) {
        file.mkdirs();
        file.delete();
        file.createNewFile();
    }
    OutputStream out = new FileOutputStream(file);
    ImageIO.write(contentCanvas.getEngine().getImage(), "png", out);
    out.close();
}

public static void main(String[] args) throws Exception {
    System.out.println(template);
    turnImage();
}

static String template= "<div style=\"width: 80px;border: 1px solid\">\n" +
        "    <table width=\"80px\" border=\"1px\">\n" +
        "        <tr>\n" +
        "            <td>\n" +
        "                <div style=\"word-break:break-all\">\n" +
        "                    99999999999\n" +
        "                </div>\n" +
        "            </td>\n" +
        "            <td>99</td>\n" +
        "        </tr>\n" +
        "    </table>\n" +
        "</div>";

}

yunhaok commented 3 years ago

The effect of html code in the browser image

radkovo commented 3 years ago

Unfortunately, the word-break property is not supported yet. We plan to add it in the future but at the moment, I am not able to provide a time scope. Till then, the text must contain a white space if you want a line break inside.

yunhaok commented 3 years ago

Received, thank you♪ (・ω・)ノ