Changeset 1102 for trunk/src/org/expeditee/io/WebParser.java
- Timestamp:
- 05/10/18 16:04:51 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/io/WebParser.java
r919 r1102 19 19 package org.expeditee.io; 20 20 21 import java.awt.Color;22 import java.awt.Font;23 import java.awt.image.BufferedImage;24 21 import java.io.File; 25 22 import java.io.IOException; … … 45 42 import javafx.beans.value.ObservableValue; 46 43 import javafx.concurrent.Worker.State; 47 import javafx.embed.swing.SwingFXUtils;48 44 import javafx.scene.SnapshotParameters; 49 45 import javafx.scene.image.WritableImage; … … 51 47 import javafx.scene.web.WebView; 52 48 53 import javax.imageio.ImageIO;54 55 49 import netscape.javascript.JSObject; 56 50 57 import org.expeditee.gui.DisplayIO; 51 import org.expeditee.core.Colour; 52 import org.expeditee.core.Font; 53 import org.expeditee.core.Image; 54 import org.expeditee.core.InOutReference; 55 import org.expeditee.gio.EcosystemManager; 56 import org.expeditee.gio.gesture.StandardGestureActions; 57 import org.expeditee.gio.swing.SwingMiscManager; 58 import org.expeditee.gui.DisplayController; 58 59 import org.expeditee.gui.Frame; 59 import org.expeditee.gui.FrameGraphics;60 60 import org.expeditee.gui.FrameIO; 61 import org.expeditee.gui.FrameMouseActions;62 61 import org.expeditee.gui.FrameUtils; 63 62 import org.expeditee.gui.MessageBay; … … 303 302 try { 304 303 305 final int verticalScrollPerPage = (int) ( FrameGraphics.getMaxFrameSize().getHeight() * 0.85);306 final int horizontalScrollPerPage = (int) ( FrameGraphics.getMaxFrameSize().getWidth() * 0.85);304 final int verticalScrollPerPage = (int) (DisplayController.getFramePaintArea().getHeight() * 0.85); 305 final int horizontalScrollPerPage = (int) (DisplayController.getFramePaintArea().getWidth() * 0.85); 307 306 308 307 Platform.runLater(new Runnable() { … … 320 319 final Object notifier = new Object(); 321 320 322 final MutableInt verticalCount = new MutableInt(0);323 final MutableInt horizontalCount = new MutableInt(0);321 final InOutReference<Integer> verticalCount = new InOutReference<Integer>(0); 322 final InOutReference<Integer> horizontalCount = new InOutReference<Integer>(0); 324 323 325 final MutableInt pagesVertical = new MutableInt(1);326 final MutableInt pagesHorizontal = new MutableInt(1);324 final InOutReference<Integer> pagesVertical = new InOutReference<Integer>(1); 325 final InOutReference<Integer> pagesHorizontal = new InOutReference<Integer>(1); 327 326 328 327 final String pageTitle; … … 341 340 frameset.getTitleItem().setSize(14); 342 341 343 WebParser.addButton("Return to original frame", frame.getName(), null, 200, frameset, null, 0 f, 10f, null);344 345 Text link = Display IO.getCurrentFrame().addText(FrameMouseActions.getX(), FrameMouseActions.getY(), pageTitle, null);342 WebParser.addButton("Return to original frame", frame.getName(), null, 200, frameset, null, 0, 10, null); 343 344 Text link = DisplayController.getCurrentFrame().addText(DisplayController.getMouseX(), DisplayController.getMouseY(), pageTitle, null); 346 345 link.setLink(frameset.getName()); 347 346 348 FrameMouseActions.pickup(link);347 StandardGestureActions.pickup(link); 349 348 350 349 // Timer that fires every time JFX is redrawn. After a few redraws, the handle method of this takes a screenshot of the page, … … 364 363 this.stop(); 365 364 366 verticalCount.set Value(verticalCount.getValue() + 1);365 verticalCount.set(verticalCount.get() + 1); 367 366 368 367 frameToAddTo = FrameIO.CreateFrame(frameToAddTo.getFramesetName(), pageTitle, null); … … 395 394 396 395 // Getting a BufferedImage from the JavaFX image 397 BufferedImage image = SwingFXUtils.fromFXImage(img, null); 396 //BufferedImage image = SwingFXUtils.fromFXImage(img, null); 397 Image image = SwingMiscManager.getImageForJavaFXImage(img); 398 398 399 399 try { 400 int hashcode = Arrays.hashCode(image.getData().getPixels(0, 0, image.getWidth(), image.getHeight(), (int[]) null)); 401 400 // TODO: tidy. cts16 401 //int hashcode = Arrays.hashCode(image.getData().getPixels(0, 0, image.getWidth(), image.getHeight(), (int[]) null)); 402 int hashcode = image.hashCode(); 403 402 404 File out = new File(FrameIO.IMAGES_PATH + "webpage-" + Integer.toHexString(hashcode) + ".png"); 403 405 out.mkdirs(); 404 ImageIO.write(image,"png", out);406 image.writeToDisk("png", out); 405 407 406 408 // Adding the image to the frame … … 408 410 409 411 // Adding thumbnail to the overview page 410 Text thumb = frameset.addText((int) (thumbWidth * 1.1 * horizontalCount.get Value()) + 10,411 (int) ((((float) thumbWidth / image.getWidth()) * image.getHeight()) * 1.1 * verticalCount.get Value()),412 Text thumb = frameset.addText((int) (thumbWidth * 1.1 * horizontalCount.get()) + 10, 413 (int) ((((float) thumbWidth / image.getWidth()) * image.getHeight()) * 1.1 * verticalCount.get()), 412 414 "@i: " + out.getName() + " " + thumbWidth, 413 415 null); 414 416 415 417 thumb.setLink(frameToAddTo.getName()); 416 thumb.setBorderColor(Colo r.lightGray);418 thumb.setBorderColor(Colour.LIGHT_GREY); 417 419 thumb.setThickness(1); 418 420 419 421 // Button to go to the next frame/page 420 WebParser.addButton("Next", null, "next", 70, frameToAddTo, null, 0 f, 10f, null);422 WebParser.addButton("Next", null, "next", 70, frameToAddTo, null, 0, 10, null); 421 423 422 424 // Button to go to the previous frame/page 423 if (verticalCount.get Value() > 1 || horizontalCount.getValue() > 0) {424 WebParser.addButton("Previous", null, "previous", 70, frameToAddTo, null, 85 f, 10f, null);425 if (verticalCount.get() > 1 || horizontalCount.get() > 0) { 426 WebParser.addButton("Previous", null, "previous", 70, frameToAddTo, null, 85, 10, null); 425 427 } 426 428 427 429 // Button to return to the index/overview page 428 WebParser.addButton("Index", frameset.getName(), null, 70, frameToAddTo, null, null, 10 f, 5f);430 WebParser.addButton("Index", frameset.getName(), null, 70, frameToAddTo, null, null, 10, 5); 429 431 430 432 FrameIO.SaveFrame(frameToAddTo); … … 435 437 } 436 438 437 image. flush();439 image.releaseImage(); 438 440 439 441 synchronized (notifier) { … … 468 470 // but if the webpage fits in a single converted page, there's no need for any overlap, so just use 1 as the number of pages 469 471 if((Boolean) webEngine.executeScript("document.documentElement.scrollHeight > window.innerHeight")) { 470 pagesVertical.set Value((int) Math.ceil((Integer) webEngine.executeScript("document.documentElement.scrollHeight") / (float) verticalScrollPerPage));472 pagesVertical.set((int) Math.ceil((Integer) webEngine.executeScript("document.documentElement.scrollHeight") / (float) verticalScrollPerPage)); 471 473 } 472 474 473 475 if((Boolean) webEngine.executeScript("document.documentElement.scrollWidth > window.innerWidth")) { 474 pagesHorizontal.set Value((int) Math.ceil((Integer) webEngine.executeScript("document.documentElement.scrollWidth") / (float) horizontalScrollPerPage));476 pagesHorizontal.set((int) Math.ceil((Integer) webEngine.executeScript("document.documentElement.scrollWidth") / (float) horizontalScrollPerPage)); 475 477 } 476 478 477 479 System.out.println(webEngine.executeScript("document.documentElement.scrollWidth") + "/" + horizontalScrollPerPage); 478 System.out.println(pagesVertical.get Value() + "x" + pagesHorizontal.getValue());480 System.out.println(pagesVertical.get() + "x" + pagesHorizontal.get()); 479 481 480 482 // Setting up the element that contains the CSS to hide all text. Also hiding readability mode buttons. … … 628 630 629 631 // Loop that scrolls the page horizontally 630 for(int i = 0; i < pagesHorizontal.get Value() && browserWidget.isParserRunning(); i++) {632 for(int i = 0; i < pagesHorizontal.get() && browserWidget.isParserRunning(); i++) { 631 633 632 634 Platform.runLater(new Runnable() { … … 647 649 648 650 // Loop that scrolls the page vertically (for each horizontal scroll position) 649 for(int j = 0; j < pagesVertical.get Value() && browserWidget.isParserRunning(); j++) {651 for(int j = 0; j < pagesVertical.get() && browserWidget.isParserRunning(); j++) { 650 652 651 653 try { 652 progressBar.set((int) (50 + ((float)(j+1)/(pagesVertical.get Value() * pagesHorizontal.getValue()) + ((float)(i) / pagesHorizontal.getValue())) * 50));654 progressBar.set((int) (50 + ((float)(j+1)/(pagesVertical.get() * pagesHorizontal.get()) + ((float)(i) / pagesHorizontal.get())) * 50)); 653 655 } catch (Exception e) { 654 656 e.printStackTrace(); … … 698 700 } 699 701 700 horizontalCount.set Value(horizontalCount.getValue() + 1);701 verticalCount.set Value(0);702 horizontalCount.set(horizontalCount.get() + 1); 703 verticalCount.set(0); 702 704 } 703 705 … … 742 744 * @return A Color object that should match the rgb string passed int. Returns null if alpha is 0 743 745 */ 744 private static Colo r rgbStringToColor(String rgbString) {746 private static Colour rgbStringToColor(String rgbString) { 745 747 746 748 if (rgbString == null) { … … 764 766 765 767 if (components[3] > 0) { 766 return new Color(components[0], components[1], components[2], components[3]);768 return Colour.FromRGBA255(components[0], components[1], components[2], components[3]); 767 769 } else { 768 770 return null; … … 854 856 855 857 Boolean fontFound = false; 856 Font font = n ew Font(null);858 Font font = null; 857 859 858 860 // Looping through all font-families listed in the element's CSS until one that is installed is … … 869 871 870 872 // Regex will remove any inverted commas surrounding multi-word typeface names 871 font = new Font(typefaces[j].replaceAll("^'|'$", ""), Font.PLAIN, 12); 872 873 // If the font isn't found, Java just uses Font.DIALOG, so this check checks whether the font was found 874 if (!(font.getFamily().toLowerCase().equals(Font.DIALOG.toLowerCase()))) { 873 String familyName = typefaces[j].replaceAll("^'|'$", ""); 874 font = new Font(familyName); 875 font.setStyle(Font.Style.PLAIN); 876 font.setSize(12); 877 878 // Check whether the font was found 879 if (EcosystemManager.getFontManager().getActualFont(font).getFamilyName().toLowerCase().equals(familyName.toLowerCase())) { 875 880 fontFound = true; 876 881 } 877 882 } 878 883 879 if (font.getFamily().toLowerCase().equals(Font.DIALOG.toLowerCase())) { 880 font = new Font("Times New Roman", Font.PLAIN, 12); 884 if (!fontFound) { 885 font = new Font("Times New Roman"); 886 font.setStyle(Font.Style.PLAIN); 887 font.setSize(12); 881 888 } 882 889 … … 975 982 String bgColorString = (String) style.call("getPropertyValue", new Object[] { "background-color" }); 976 983 977 Colo r bgColor = rgbStringToColor(bgColorString);984 Colour bgColor = rgbStringToColor(bgColorString); 978 985 979 986 // If the element has a background color then add it (to Expeditee) as a rectangle with that background color … … 1077 1084 HttpURLConnection connection = (HttpURLConnection) (imgUrl.openConnection()); 1078 1085 1079 // Spoofing a widely accepted User Agent, since some sites refuse to serve non-webbrowser clients 1080 connection.setRequestProperty("User-Agent", "Mozilla/5.0"); 1081 1082 BufferedImage img = ImageIO.read(connection.getInputStream()); 1083 1084 int hashcode = Arrays.hashCode(img.getData().getPixels(0, 0, img.getWidth(), img.getHeight(), (int[]) null)); 1086 Image img = Image.getImage(connection); 1087 1088 int hashcode = img.hashCode(); 1089 1085 1090 File out = new File(FrameIO.IMAGES_PATH + Integer.toHexString(hashcode) + ".png"); 1086 1091 out.mkdirs(); 1087 ImageIO.write(img,"png", out);1092 img.writeToDisk("png", out); 1088 1093 1089 1094 if (repeat == null && cropEndX == null && cropStartX == null && cropEndY == null && cropStartY == null) { … … 1153 1158 text.setPosition(x, y); 1154 1159 1155 Picture pic = ItemUtils.CreatePicture(text , frame);1160 Picture pic = ItemUtils.CreatePicture(text); 1156 1161 1157 1162 float invScale = 1 / pic.getScale(); … … 1324 1329 1325 1330 Boolean fontFound = false; 1326 Font font = n ew Font(null);1331 Font font = null; 1327 1332 1328 1333 // Looping through all font-families listed in the element's CSS until one that is installed is … … 1342 1347 1343 1348 // Regex will remove any inverted commas surrounding multi-word typeface names 1344 font = new Font(typefaces[j].replaceAll("^'|'$", ""), Font.PLAIN, 12); 1349 String familyName = typefaces[j].replaceAll("^'|'$", ""); 1350 font = new Font(familyName); 1351 font.setStyle(Font.Style.PLAIN); 1352 font.setSize(12); 1345 1353 1346 1354 // If the font isn't found, Java just uses Font.DIALOG, so this check checks whether the font was found 1347 if ( !(font.getFamily().toLowerCase().equals(Font.DIALOG.toLowerCase()))) {1355 if (EcosystemManager.getFontManager().getActualFont(font).getFamilyName().toLowerCase().equals(familyName.toLowerCase())) { 1348 1356 fontFound = true; 1349 1357 } 1350 1358 } 1351 1359 1352 if (font.getFamily().toLowerCase().equals(Font.DIALOG.toLowerCase())) { 1353 font = new Font("Times New Roman", Font.PLAIN, 12); 1360 if (!fontFound) { 1361 font = new Font("Times New Roman"); 1362 font.setStyle(Font.Style.PLAIN); 1363 font.setSize(12); 1354 1364 } 1355 1365 … … 1464 1474 * @param anchorLeft 1465 1475 */ 1466 private static void addButton(String text, String link, String action, int width, Frame toAddTo, Float anchorTop, Float anchorRight, Float anchorBottom, FloatanchorLeft) {1476 private static void addButton(String text, String link, String action, int width, Frame toAddTo, Integer anchorTop, Integer anchorRight, Integer anchorBottom, Integer anchorLeft) { 1467 1477 // Button to go to the next frame/page 1468 1478 Text button = new Text(text); … … 1470 1480 button.setLink(link); 1471 1481 button.addAction(action); 1472 button.setBorderColor(new Colo r(0.7f, 0.7f, 0.7f));1473 button.setBackgroundColor(new Colo r(0.9f, 0.9f, 0.9f));1482 button.setBorderColor(new Colour(0.7f, 0.7f, 0.7f)); 1483 button.setBackgroundColor(new Colour(0.9f, 0.9f, 0.9f)); 1474 1484 button.setThickness(1); 1475 1485 button.setLinkMark(false); … … 1499 1509 1500 1510 } 1501 1502 1503 private static class MutableBool {1504 private boolean value;1505 1506 public MutableBool(boolean value) {1507 this.value = value;1508 }1509 1510 public boolean getValue() {1511 return value;1512 }1513 1514 public void setValue(boolean value) {1515 this.value = value;1516 }1517 }1518 1519 private static class MutableInt {1520 private int value;1521 1522 public MutableInt(int value) {1523 this.value = value;1524 }1525 1526 public int getValue() {1527 return value;1528 }1529 1530 public void setValue(int value) {1531 this.value = value;1532 }1533 }1534 1511 }
Note:
See TracChangeset
for help on using the changeset viewer.