Changeset 600
- Timestamp:
- 12/10/13 14:23:00 (11 years ago)
- Location:
- trunk/src/org/expeditee
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/io/WebParser.java
r594 r600 3 3 import java.awt.Color; 4 4 import java.awt.Font; 5 import java.awt.Point; 5 6 import java.awt.image.BufferedImage; 6 7 import java.io.File; … … 179 180 // Checking if the element is actually visible on the page 180 181 if (WebParser.elementVisible(x, y, width, height, style)) { 181 182 // background image, returns in format "url(protocol://absolute/path/to/img.extension)" for images, may183 // 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 coords215 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 width227 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 yet238 mue.printStackTrace();239 }240 }241 182 242 183 // Filtering the node type, starting with text nodes … … 333 274 334 275 } 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 } 335 343 336 344 String imgSrc; … … 413 421 * Y-coordinate at which the image should be placed on the frame 414 422 * @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 used423 * Width of the image once added to the frame. Negative 1 (-1) will cause the actual width of the image file to be used 416 424 * 417 425 * @param cropStartX 426 * X-coordinate at which to start crop, or null for no crop 418 427 * @param cropStartY 428 * Y-coordinate at which to start crop, or null for no crop 419 429 * @param cropEndX 430 * X-coordinate at which to end the crop, or null for no crop 420 431 * @param cropEndY 432 * Y-coordinate at which to end the crop, or null for no crop 421 433 * @throws MalformedURLException 422 434 * @throws IOException … … 440 452 ImageIO.write(img, "png", out); 441 453 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) { 445 455 cropStartX = 0; 446 456 cropStartY = 0; 447 457 cropEndX = img.getWidth(); 448 458 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 + " " + cropStartY456 + " " + 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()))); 461 471 pic.setPosition(x, y); 462 472 frame.addItem(pic); -
trunk/src/org/expeditee/items/ItemUtils.java
r592 r600 367 367 } 368 368 369 // try images subdirectory370 File file = null;371 372 for (String dir : org.expeditee.settings.UserSettings.ImageDirs) {373 file = new File(dir + path);374 if (file.exists() && !file.isDirectory())375 break;376 }377 378 if (file == null || !file.exists() || file.isDirectory())379 file = new File(path);380 381 // try relative path382 if (!file.exists() || file.isDirectory()) {383 URL picture = new Object().getClass().getResource(path);384 385 // decode to remove %20 in windows folder names386 if (picture != null) {387 try {388 path = URLDecoder.decode(picture.getFile(), "UTF-8");389 } catch (UnsupportedEncodingException e) {390 // TODO Auto-generated catch block391 e.printStackTrace();392 }393 }394 395 } else396 path = file.getPath();397 398 // if the image isn't found by now, give up.399 file = new File(path);400 if (!file.exists() || file.isDirectory()) {401 return null;402 }403 404 369 } catch (Exception e) { 405 370 return null; -
trunk/src/org/expeditee/items/Picture.java
r598 r600 17 17 import java.awt.image.ImageObserver; 18 18 import java.io.File; 19 import java.io.UnsupportedEncodingException; 19 20 import java.net.URL; 21 import java.net.URLDecoder; 20 22 21 23 import javax.imageio.ImageIO; … … 138 140 */ 139 141 protected void parseSize(String size) { 140 141 System.out.println("Size: " + size);142 143 142 // don't check this anymore since it's only called from constructors now 144 143 // if (_end.x != 0 || _end.y != 0) … … 582 581 583 582 public boolean refresh() { 583 // try images subdirectory 584 File file = null; 585 586 for (String dir : org.expeditee.settings.UserSettings.ImageDirs) { 587 file = new File(dir + _path); 588 if (file.exists() && !file.isDirectory()) 589 break; 590 } 591 592 if (file == null || !file.exists() || file.isDirectory()) 593 file = new File(_path); 594 595 // try relative path 596 if (!file.exists() || file.isDirectory()) { 597 URL picture = new Object().getClass().getResource(_path); 598 599 // decode to remove %20 in windows folder names 600 if (picture != null) { 601 try { 602 _path = URLDecoder.decode(picture.getFile(), "UTF-8"); 603 } catch (UnsupportedEncodingException e) { 604 // TODO Auto-generated catch block 605 e.printStackTrace(); 606 } 607 } 608 609 } else 610 _path = file.getPath(); 611 612 // if the image isn't found by now, give up. 613 file = new File(_path); 614 if (!file.exists() || file.isDirectory()) { 615 // If the image could not be read 616 _image = imageNotFound; 617 return false; 618 } 619 584 620 // ImageIcon is faster, but cannot handle some formats 585 621 // (notably.bmp) hence, we try this first, then if it fails we try … … 676 712 public Integer getWidthToSave() { 677 713 if(_scaleType == WIDTH) { 678 System.out.println(this.getWidth());679 714 return this.getWidth(); 680 715 }
Note:
See TracChangeset
for help on using the changeset viewer.