Ignore:
Timestamp:
12/10/13 14:23:00 (10 years ago)
Author:
ngw8
Message:

Added support for the CSS 'background-size' property to WebParser

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/io/WebParser.java

    r594 r600  
    33import java.awt.Color;
    44import java.awt.Font;
     5import java.awt.Point;
    56import java.awt.image.BufferedImage;
    67import java.io.File;
     
    179180                                                                // Checking if the element is actually visible on the page
    180181                                                                if (WebParser.elementVisible(x, y, width, height, style)) {
    181 
    182                                                                         // background image, returns in format "url(protocol://absolute/path/to/img.extension)" for images, may
    183                                                                         // also return gradients, data, etc. (not handled yet)
    184                                                                         String bgImage = (String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "background-image" });
    185 
    186                                                                         if (bgImage.startsWith("url(")) {
    187 
    188                                                                                 bgImage = bgImage.substring(4, bgImage.length() - 1);
    189 
    190                                                                                 String bgSize = ((String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "background-size" })).toLowerCase();
    191 
    192                                                                                 // Returns "[x]px [y]px", "[x]% [y]%", "[x]px [y]%" or "[x]% [y]px"
    193                                                                                 String bgPosition = ((String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "background-position" })).toLowerCase();
    194 
    195                                                                                 String[] bgOffsetCoords = bgPosition.split(" ");
    196 
    197                                                                                 int bgOffsetX = 0, bgOffsetY = 0;
    198 
    199                                                                                 int cropStartX, cropStartY, cropEndX, cropEndY;
    200 
    201                                                                                 // Converting the x and y offset values to integers (and from % to px if needed)
    202                                                                                 if (bgOffsetCoords[0].endsWith("%")) {
    203                                                                                         bgOffsetX = (int) ((Integer.valueOf(bgOffsetCoords[0].substring(0, bgOffsetCoords[0].length() - 1)) / 100.0) * width);
    204                                                                                 } else if (bgOffsetCoords[0].endsWith("px")) {
    205                                                                                         bgOffsetX = (int) (Integer.valueOf(bgOffsetCoords[0].substring(0, bgOffsetCoords[0].length() - 2)));
    206                                                                                 }
    207 
    208                                                                                 if (bgOffsetCoords[1].endsWith("%")) {
    209                                                                                         bgOffsetY = (int) ((Integer.valueOf(bgOffsetCoords[1].substring(0, bgOffsetCoords[1].length() - 1)) / 100.0) * height);
    210                                                                                 } else if (bgOffsetCoords[1].endsWith("px")) {
    211                                                                                         bgOffsetY = (int) (Integer.valueOf(bgOffsetCoords[1].substring(0, bgOffsetCoords[1].length() - 2)));
    212                                                                                 }
    213 
    214                                                                                 // Converting from an offset to crop coords
    215                                                                                 cropStartX = -1 * bgOffsetX;
    216                                                                                 cropEndX = (int) (cropStartX + width);
    217 
    218                                                                                 cropStartY = -1 * bgOffsetY;
    219                                                                                 cropEndY = (int) (cropStartY + height);
    220 
    221                                                                                 int bgWidth = -1;
    222 
    223                                                                                 if (bgSize.equals("cover")) {
    224                                                                                         bgWidth = (int) width;
    225                                                                                 } else if (bgSize.equals("contain")) {
    226                                                                                         // TODO: actually compute the appropriate width
    227                                                                                         bgWidth = (int) width;
    228                                                                                 } else if (bgSize.equals("auto")) {
    229                                                                                         bgWidth = -1;
    230                                                                                 } else {
    231                                                                                         bgWidth = Integer.parseInt(bgSize.split(" ")[0].replaceAll("\\D", ""));
    232                                                                                 }
    233 
    234                                                                                 try {
    235                                                                                         WebParser.addImageFromUrl(bgImage, frame, x, y, bgWidth, cropStartX, cropStartY, cropEndX, cropEndY);
    236                                                                                 } catch (MalformedURLException mue) {
    237                                                                                         // probably a 'data:' url, not supported yet
    238                                                                                         mue.printStackTrace();
    239                                                                                 }
    240                                                                         }
    241182       
    242183                                                                        // Filtering the node type, starting with text nodes
     
    333274
    334275                                                                        } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
     276
     277                                                                                // background image, returns in format "url(protocol://absolute/path/to/img.extension)" for images,
     278                                                                                // may also return gradients, data, etc. (not handled yet). Only need to add bg image on
     279                                                                                // 'ELEMENT_NODE' (and not 'TEXT_NODE' otherwise there would be double-ups
     280                                                                                String bgImage = (String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "background-image" });
     281
     282                                                                                if (bgImage.startsWith("url(")) {
     283
     284                                                                                        bgImage = bgImage.substring(4, bgImage.length() - 1);
     285
     286                                                                                        String bgSize = ((String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "background-size" })).toLowerCase();
     287
     288                                                                                        // Returns "[x]px [y]px", "[x]% [y]%", "[x]px [y]%" or "[x]% [y]px"
     289                                                                                        String bgPosition = ((String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "background-position" })).toLowerCase();
     290
     291                                                                                        String[] bgOffsetCoords = bgPosition.split(" ");
     292
     293                                                                                        int bgOffsetX = 0, bgOffsetY = 0;
     294
     295                                                                                        int cropStartX, cropStartY, cropEndX, cropEndY;
     296
     297                                                                                        // Converting the x and y offset values to integers (and from % to px if needed)
     298                                                                                        if (bgOffsetCoords[0].endsWith("%")) {
     299                                                                                                bgOffsetX = (int) ((Integer.valueOf(bgOffsetCoords[0].substring(0, bgOffsetCoords[0].length() - 1)) / 100.0) * width);
     300                                                                                        } else if (bgOffsetCoords[0].endsWith("px")) {
     301                                                                                                bgOffsetX = (int) (Integer.valueOf(bgOffsetCoords[0].substring(0, bgOffsetCoords[0].length() - 2)));
     302                                                                                        }
     303
     304                                                                                        if (bgOffsetCoords[1].endsWith("%")) {
     305                                                                                                bgOffsetY = (int) ((Integer.valueOf(bgOffsetCoords[1].substring(0, bgOffsetCoords[1].length() - 1)) / 100.0) * height);
     306                                                                                        } else if (bgOffsetCoords[1].endsWith("px")) {
     307                                                                                                bgOffsetY = (int) (Integer.valueOf(bgOffsetCoords[1].substring(0, bgOffsetCoords[1].length() - 2)));
     308                                                                                        }
     309
     310                                                                                        // Converting from an offset to crop coords
     311                                                                                        cropStartX = -1 * bgOffsetX;
     312                                                                                        cropEndX = (int) (cropStartX + width);
     313
     314                                                                                        cropStartY = -1 * bgOffsetY;
     315                                                                                        cropEndY = (int) (cropStartY + height);
     316
     317                                                                                        int bgWidth = -1;
     318
     319                                                                                        if (bgSize.equals("cover")) {
     320                                                                                                bgWidth = (int) width;
     321                                                                                        } else if (bgSize.equals("contain")) {
     322                                                                                                // TODO: actually compute the appropriate width
     323                                                                                                bgWidth = (int) width;
     324                                                                                        } else if (bgSize.equals("auto")) {
     325                                                                                                bgWidth = -1;
     326                                                                                        } else {
     327                                                                                                bgSize = bgSize.split(" ")[0];
     328
     329                                                                                                if (bgSize.endsWith("%")) {
     330                                                                                                        bgWidth = (int) ((Integer.parseInt(bgSize.replaceAll("\\D", "")) / 100.0) * width);
     331                                                                                                } else if (bgSize.endsWith("px")) {
     332                                                                                                        bgWidth = Integer.parseInt(bgSize.replaceAll("\\D", ""));
     333                                                                                                }
     334                                                                                        }
     335
     336                                                                                        try {
     337                                                                                                WebParser.addImageFromUrl(bgImage, frame, x, y, bgWidth, cropStartX, cropStartY, cropEndX, cropEndY);
     338                                                                                        } catch (MalformedURLException mue) {
     339                                                                                                // probably a 'data:' url, not supported yet
     340                                                                                                mue.printStackTrace();
     341                                                                                        }
     342                                                                                }
    335343
    336344                                                                                String imgSrc;
     
    413421         *            Y-coordinate at which the image should be placed on the frame
    414422         * @param width
    415          *            Width of the image once added to the frame, negative 1 (-1) will cause the actual width of the image file to be used
     423         *            Width of the image once added to the frame. Negative 1 (-1) will cause the actual width of the image file to be used
    416424         *
    417425         * @param cropStartX
     426         *            X-coordinate at which to start crop, or null for no crop
    418427         * @param cropStartY
     428         *            Y-coordinate at which to start crop, or null for no crop
    419429         * @param cropEndX
     430         *            X-coordinate at which to end the crop, or null for no crop
    420431         * @param cropEndY
     432         *            Y-coordinate at which to end the crop, or null for no crop
    421433         * @throws MalformedURLException
    422434         * @throws IOException
     
    440452                ImageIO.write(img, "png", out);
    441453
    442                 if (cropEndX != null && cropStartX != null && cropEndY != null && cropStartY != null) {
    443                         width = cropEndX - cropStartX;
    444                 } else {
     454                if (cropEndX == null || cropStartX == null || cropEndY == null || cropStartY == null) {
    445455                        cropStartX = 0;
    446456                        cropStartY = 0;
    447457                        cropEndX = img.getWidth();
    448458                        cropEndY = img.getHeight();
    449 
    450                         if (width < 0) {
    451                                 width = img.getWidth();
    452                         }
    453                 }
    454 
    455                 Text item = new Text(ItemUtils.GetTag(ItemUtils.TAG_IMAGE) + ":" + out.getPath() + " " + ((width >= 0) ? width : img.getWidth()) + " " + cropStartX + " " + cropStartY
    456                                 + " " + cropEndX + " " + cropEndY);
    457 
    458                 item.setPosition(x, y);
    459 
    460                 Picture pic = ItemUtils.CreatePicture(item, frame);
     459                }
     460
     461                if (width < 0) {
     462                        width = img.getWidth();
     463                }
     464
     465                Picture pic = new Picture(out.getName(), frame);
     466                pic.setWidth(width);
     467
     468                // Have to divide the crop coords by the image scale, since Expeditee seems to always crop then scale
     469                pic.setCropStart(new Point((int) (cropStartX / pic.getScale()), (int) (cropStartY / pic.getScale())));
     470                pic.setCropEnd(new Point((int) (cropEndX / pic.getScale()), (int) (cropEndY / pic.getScale())));
    461471                pic.setPosition(x, y);
    462472                frame.addItem(pic);
Note: See TracChangeset for help on using the changeset viewer.