Changeset 815
- Timestamp:
- 02/03/14 16:03:18 (10 years ago)
- Location:
- trunk/src/org/expeditee
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/io/WebParser.java
r811 r815 882 882 frame.addRectangle(Math.round(x), Math.round(y), Math.round(width), Math.round(height), 0, null, bgColor); 883 883 } 884 885 String linkUrl = (String) ((JSObject) currentNode).getMember("href"); 884 886 885 887 // background image, returns in format "url(protocol://absolute/path/to/img.extension)" for images, 886 888 // may also return gradients, data, etc. (not handled yet). Only need to add bg image on 887 889 // 'ELEMENT_NODE' (and not 'TEXT_NODE' otherwise there would be double-ups 888 String bgImage = (String) style.call("getPropertyValue", new Object[] { "background-image" }); 889 890 String linkUrl = (String) ((JSObject) currentNode).getMember("href"); 891 892 if (bgImage.startsWith("url(")) { 893 bgImage = bgImage.substring(4, bgImage.length() - 1); 894 895 String bgSize = ((String) style.call("getPropertyValue", new Object[] { "background-size" })).toLowerCase(); 896 String bgRepeat = ((String) style.call("getPropertyValue", new Object[] { "background-repeat" })).toLowerCase(); 897 898 // Returns "[x]px [y]px", "[x]% [y]%", "[x]px [y]%" or "[x]% [y]px" 899 String bgPosition = ((String) style.call("getPropertyValue", new Object[] { "background-position" })).toLowerCase(); 900 901 String[] bgOffsetCoords = bgPosition.split(" "); 902 903 int bgOffsetX = 0, bgOffsetY = 0; 904 905 float originXPercent = 0, originYPercent = 0; 906 907 int cropStartX, cropStartY, cropEndX, cropEndY; 908 909 // Converting the x and y offset values to integers (and from % to px if needed) 910 if (bgOffsetCoords[0].endsWith("%")) { 911 bgOffsetX = (int) ((Integer.valueOf(bgOffsetCoords[0].substring(0, bgOffsetCoords[0].length() - 1)) / 100.0) * width); 912 originXPercent = (Integer.valueOf(bgOffsetCoords[0].substring(0, bgOffsetCoords[0].length() - 1))) / 100f; 913 } else if (bgOffsetCoords[0].endsWith("px")) { 914 bgOffsetX = (int) (Integer.valueOf(bgOffsetCoords[0].substring(0, bgOffsetCoords[0].length() - 2))); 915 } 916 917 if (bgOffsetCoords[1].endsWith("%")) { 918 bgOffsetY = (int) ((Integer.valueOf(bgOffsetCoords[1].substring(0, bgOffsetCoords[1].length() - 1)) / 100.0) * height); 919 originYPercent = (Integer.valueOf(bgOffsetCoords[1].substring(0, bgOffsetCoords[1].length() - 1))) / 100f; 920 } else if (bgOffsetCoords[1].endsWith("px")) { 921 bgOffsetY = (int) (Integer.valueOf(bgOffsetCoords[1].substring(0, bgOffsetCoords[1].length() - 2))); 922 } 923 924 // Converting from an offset to crop coords 925 cropStartX = -1 * bgOffsetX; 926 cropEndX = (int) (cropStartX + width); 927 928 cropStartY = -1 * bgOffsetY; 929 cropEndY = (int) (cropStartY + height); 930 931 int bgWidth = -1; 932 933 if (bgSize.equals("cover")) { 934 bgWidth = (int) width; 935 } else if (bgSize.equals("contain")) { 936 // TODO: actually compute the appropriate width 937 bgWidth = (int) width; 938 } else if (bgSize.equals("auto")) { 939 bgWidth = -1; 940 } else { 941 bgSize = bgSize.split(" ")[0]; 942 943 if (bgSize.endsWith("%")) { 944 bgWidth = (int) ((Integer.parseInt(bgSize.replaceAll("\\D", "")) / 100.0) * width); 945 } else if (bgSize.endsWith("px")) { 946 bgWidth = Integer.parseInt(bgSize.replaceAll("\\D", "")); 947 } 948 } 890 if (((String) style.call("getPropertyValue", new Object[] { "background-image" })).startsWith("url(")) { 949 891 950 892 try { 951 WebParser.add ImageFromUrl(bgImage, linkUrl, frame, x, y, bgWidth, cropStartX, cropStartY, cropEndX, cropEndY, bgRepeat, originXPercent, originYPercent);893 WebParser.addBackgroundImageFromNode(currentNode, style, frame, linkUrl, x, y, width, height); 952 894 } catch (MalformedURLException mue) { 953 895 // probably a 'data:' url, not supported yet … … 1028 970 * @throws IOException 1029 971 */ 1030 private static void addImageFromUrl(String imgSrc, String linkUrl, final Frame frame, float x, float y, int width, Integer cropStartX, Integer cropStartY, Integer cropEndX, Integer cropEndY, String repeat, 1031 float originXPercent, float originYPercent) 1032 throws MalformedURLException, 1033 IOException { 972 public static Picture getImageFromUrl(String imgSrc, String linkUrl, final Frame frame, float x, float y, int width, 973 Integer cropStartX, Integer cropStartY, Integer cropEndX, Integer cropEndY, String repeat, float originXPercent, float originYPercent) 974 throws IOException { 1034 975 1035 976 URL imgUrl = new URL(imgSrc); … … 1120 1061 } 1121 1062 1063 return pic; 1064 } 1065 1066 private static void addImageFromUrl(String imgSrc, String linkUrl, final Frame frame, float x, float y, int width, Integer cropStartX, Integer cropStartY, Integer cropEndX, Integer cropEndY, String repeat, 1067 float originXPercent, float originYPercent) 1068 throws IOException { 1069 Picture pic = getImageFromUrl(imgSrc, linkUrl, frame, x, y, width, cropStartX, cropStartY, cropEndX, cropEndY, repeat, originXPercent, originYPercent); 1070 frame.addItem(pic); 1071 pic.anchor(); 1072 pic.getSource().anchor(); 1073 } 1074 1075 public static Picture getBackgroundImageFromNode(Node node, JSObject style, final Frame frame, String linkUrl, float x, float y, float width, float height) throws IOException { 1076 1077 1078 String bgImage = (String) style.call("getPropertyValue", new Object[] { "background-image" }); 1079 bgImage = bgImage.substring(4, bgImage.length() - 1); 1080 1081 String bgSize = ((String) style.call("getPropertyValue", new Object[] { "background-size" })).toLowerCase(); 1082 String bgRepeat = ((String) style.call("getPropertyValue", new Object[] { "background-repeat" })).toLowerCase(); 1083 1084 // Returns "[x]px [y]px", "[x]% [y]%", "[x]px [y]%" or "[x]% [y]px" 1085 String bgPosition = ((String) style.call("getPropertyValue", new Object[] { "background-position" })).toLowerCase(); 1086 1087 String[] bgOffsetCoords = bgPosition.split(" "); 1088 1089 int bgOffsetX = 0, bgOffsetY = 0; 1090 1091 float originXPercent = 0, originYPercent = 0; 1092 1093 int cropStartX, cropStartY, cropEndX, cropEndY; 1094 1095 // Converting the x and y offset values to integers (and from % to px if needed) 1096 if (bgOffsetCoords[0].endsWith("%")) { 1097 bgOffsetX = (int) ((Integer.valueOf(bgOffsetCoords[0].substring(0, bgOffsetCoords[0].length() - 1)) / 100.0) * width); 1098 originXPercent = (Integer.valueOf(bgOffsetCoords[0].substring(0, bgOffsetCoords[0].length() - 1))) / 100f; 1099 } else if (bgOffsetCoords[0].endsWith("px")) { 1100 bgOffsetX = (int) (Integer.valueOf(bgOffsetCoords[0].substring(0, bgOffsetCoords[0].length() - 2))); 1101 } 1102 1103 if (bgOffsetCoords[1].endsWith("%")) { 1104 bgOffsetY = (int) ((Integer.valueOf(bgOffsetCoords[1].substring(0, bgOffsetCoords[1].length() - 1)) / 100.0) * height); 1105 originYPercent = (Integer.valueOf(bgOffsetCoords[1].substring(0, bgOffsetCoords[1].length() - 1))) / 100f; 1106 } else if (bgOffsetCoords[1].endsWith("px")) { 1107 bgOffsetY = (int) (Integer.valueOf(bgOffsetCoords[1].substring(0, bgOffsetCoords[1].length() - 2))); 1108 } 1109 1110 // Converting from an offset to crop coords 1111 cropStartX = -1 * bgOffsetX; 1112 cropEndX = (int) (cropStartX + width); 1113 1114 cropStartY = -1 * bgOffsetY; 1115 cropEndY = (int) (cropStartY + height); 1116 1117 int bgWidth = -1; 1118 1119 if (bgSize.equals("cover")) { 1120 bgWidth = (int) width; 1121 } else if (bgSize.equals("contain")) { 1122 // TODO: actually compute the appropriate width 1123 bgWidth = (int) width; 1124 } else if (bgSize.equals("auto")) { 1125 bgWidth = -1; 1126 } else { 1127 bgSize = bgSize.split(" ")[0]; 1128 1129 if (bgSize.endsWith("%")) { 1130 bgWidth = (int) ((Integer.parseInt(bgSize.replaceAll("\\D", "")) / 100.0) * width); 1131 } else if (bgSize.endsWith("px")) { 1132 bgWidth = Integer.parseInt(bgSize.replaceAll("\\D", "")); 1133 } 1134 } 1135 1136 return getImageFromUrl(bgImage, linkUrl, frame, x, y, bgWidth, cropStartX, cropStartY, cropEndX, cropEndY, bgRepeat, originXPercent, originYPercent); 1137 } 1138 1139 private static void addBackgroundImageFromNode(Node node, JSObject style, final Frame frame, String linkUrl, float x, float y, float width, float height) throws IOException { 1140 Picture pic = getBackgroundImageFromNode(node, style, frame, linkUrl, x, y, width, height); 1122 1141 frame.addItem(pic); 1123 1142 pic.anchor(); -
trunk/src/org/expeditee/items/widgets/JfxBrowser.java
r813 r815 36 36 import javafx.scene.web.WebEngine; 37 37 import javafx.scene.web.WebView; 38 import netscape.javascript.JSObject; 38 39 39 40 import org.expeditee.gui.DisplayIO; … … 43 44 import org.expeditee.io.WebParser; 44 45 import org.expeditee.items.Item; 46 import org.expeditee.items.Picture; 45 47 import org.expeditee.items.Text; 46 48 import org.expeditee.settings.network.NetworkSettings; … … 382 384 String selection = (String) JfxBrowser.this._webEngine.executeScript("window.getSelection().toString()"); 383 385 384 // Do nothing if no text is selected 385 if (!selection.equals("")) { 386 // If no text is selected, see if an image is under the cursor 387 if (selection.length() == 0) { 388 JSObject window = (JSObject) JfxBrowser.this._webEngine.executeScript("window"); 389 Object image = JfxBrowser.this._webEngine.executeScript("" 390 + "function isImage(o) {" 391 + " if(o.tagName == \"IMG\" && o.src != null) {" 392 + " return true;" 393 + " }" 394 + " if(window.getComputedStyle(o).getPropertyValue(\"background-image\").indexOf(\"url\") == 0) {" 395 + " return true;" 396 + " }" 397 + " return false;" 398 + "};" 399 + "var clicked = document.elementFromPoint(" + e.getX() + "," + e.getY() + ");" 400 + "if(isImage(clicked)) {" 401 + " clicked;" 402 + "} else {" 403 // TODO: look in child elements and other elements behind the current one 404 // For now just return nothing 405 + " null;" 406 + "}" 407 + ""); 408 if(image instanceof org.w3c.dom.Node) { 409 System.out.println(image); 410 try { 411 JSObject style = (JSObject) window.call("getComputedStyle", image); 412 413 JSObject bounds = (JSObject) ((JSObject) image).call("getBoundingClientRect", new Object[] {}); 414 float width = Float.valueOf(bounds.getMember("width").toString()); 415 float height = Float.valueOf(bounds.getMember("height").toString()); 416 417 Picture pic; 418 419 org.w3c.dom.Node node = ((org.w3c.dom.Node) image); 420 421 if (((String) style.call("getPropertyValue", new Object[] { "background-image" })).startsWith("url(")) { 422 pic = WebParser.getBackgroundImageFromNode(node, style, DisplayIO.getCurrentFrame(), null, 423 (float) FrameMouseActions.getX(), (float) FrameMouseActions.getY(), width, height); 424 } else { 425 String imgSrc; 426 if(node.getNodeName().toLowerCase().equals("img") && 427 (imgSrc = ((JSObject) node).getMember("src").toString()) != null) { 428 pic = WebParser.getImageFromUrl(imgSrc, null, DisplayIO.getCurrentFrame(), 429 (float) FrameMouseActions.getX(), (float) FrameMouseActions.getY(), (int) width, null, null, null, null, null, 0, 0); 430 } else { 431 return; 432 } 433 } 434 pic.setXY(FrameMouseActions.getX(), FrameMouseActions.getY()); 435 FrameMouseActions.pickup(pic); 436 } catch (Exception e1) { 437 // TODO Auto-generated catch block 438 e1.printStackTrace(); 439 } 440 } 441 } else { 386 442 // Copy text and attach to cursor 387 443 Text t = new Text(selection);
Note:
See TracChangeset
for help on using the changeset viewer.