Changeset 630
- Timestamp:
- 12/17/13 15:31:24 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/io/WebParser.java
r628 r630 159 159 .invoke(webEngine, 160 160 "function isContent(el) { " 161 + " if (el.tagName === 'li' && window.getComputedStyle(el).getPropertyValue('list-style-type') !== 'none') { 162 + " return true; 163 + " } 164 + " var text = el.textContent.replace(/(^\\s*)|(\\s*$)/, ''); 165 + " var w = text.split(/\\S\\s\\S/).length; var punct = text.split(/\\.|\\,|\\!/).length; 166 + " if (punct < 2 || w < 10) { 161 + " if (el.tagName === 'li' && window.getComputedStyle(el).getPropertyValue('list-style-type') !== 'none') {" 162 + " return true;" 163 + " }" 164 + " var text = el.textContent.replace(/(^\\s*)|(\\s*$)/, '');" 165 + " var w = text.split(/\\S\\s\\S/).length; var punct = text.split(/\\.|\\,|\\!/).length;" 166 + " if (punct < 2 || w < 10) {" 167 167 + " return false;" 168 168 + " } else { " 169 169 + " return true;" 170 + " } " 171 + "} " 170 + " }" 171 + "}" 172 172 173 + "function addToSpan(text) {" 173 174 + " span = document.createElement('wordSpan');" 174 + " span.textContent = text + ' '; 175 + " para.appendChild(span); 176 + " if (prevSpan !== null && span.getBoundingClientRect().top > prevSpan.getBoundingClientRect().top) { 175 + " span.textContent = text + ' ';" 176 + " para.appendChild(span);" 177 + " if (prevSpan !== null && span.getBoundingClientRect().top > prevSpan.getBoundingClientRect().top) {" 177 178 + " span.textContent = '\\n' + span.textContent;" 178 179 + " if (prevPrevSpan !== null && prevPrevSpan.getBoundingClientRect().left == prevSpan.getBoundingClientRect().left) {" … … 183 184 + " }" 184 185 + " prevSpan = span;" 185 186 186 + " } else {" 187 187 + " prevSpan.textContent = prevSpan.textContent + span.textContent;" … … 196 196 for (int i = 0; i < contentElementsLength; i++) { 197 197 Element currentNode = (Element) JavaFX.JSObjectGetSlot.invoke(contentElements, i); 198 // JavaFX.JSObjectCall.invoke(window, "addBreaks", new Object[] { currentNode }); 198 199 199 JavaFX.JSObjectSetMember.invoke(window, "para", currentNode); 200 200 201 201 JavaFX.WebEngineExecuteScript.invoke(webEngine, "" 202 + "var exp = {};"203 202 + "var span = document.createElement('wordSpan');" 204 203 + "var prevSpan = span;" … … 293 292 // Returns comma-separated list of typefaces 294 293 String typeface = (String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "font-family" }); 294 295 295 String[] typefaces = typeface.split(", |,"); 296 296 … … 302 302 String letterSpacing = (String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "letter-spacing" }); 303 303 304 String textTransform = (String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "text-transform" }); 305 304 306 Boolean fontFound = false; 305 307 Font font = new Font(null); … … 308 310 // found, or the end of the list is reached, in which case the default font is used 309 311 for (int j = 0; j < typefaces.length && !fontFound; j++) { 310 312 if (typefaces[j].toLowerCase().equals("sans-serif")) { 313 typefaces[j] = "Arial"; 314 } else if (typefaces[j].toLowerCase().equals("serif")) { 315 typefaces[j] = "Times New Roman"; 316 } 317 311 318 // Regex will remove any inverted commas surrounding multi-word typeface names 312 319 font = new Font(typefaces[j].replaceAll("^'|'$", ""), Font.PLAIN, 12); 313 320 321 // If the font isn't found, Java just uses Font.DIALOG, so this check checks whether the font was found 314 322 if (!(font.getFamily().toLowerCase().equals(Font.DIALOG.toLowerCase()))) { 315 323 fontFound = true; 316 324 } 325 } 326 327 if (font.getFamily().toLowerCase().equals(Font.DIALOG.toLowerCase())) { 328 font = new Font("Times New Roman", Font.PLAIN, 12); 317 329 } 318 330 … … 344 356 } 345 357 346 Node siblingNode;347 NodeList siblings = currentNode.getParentNode().getChildNodes();348 349 String combined = currentNode.getTextContent();350 351 for (int j = 0; j < siblings.getLength(); j++) {352 siblingNode = siblings.item(j);353 if (siblingNode.getNodeType() == Node.TEXT_NODE) {354 System.out.println("sibling is text");355 combined = combined + siblingNode.getTextContent();356 }357 }358 359 System.out.println("Combined: " + combined);360 361 362 358 Text t; 363 364 t = frame.addText((int) x, (int) y, currentNode.getTextContent().replaceAll("[^\\S\\n]+", " ").trim(), null); 359 String textContent = currentNode.getTextContent().replaceAll("[^\\S\\n]+", " ").trim(); 360 361 if (textTransform.equals("uppercase")) { 362 textContent = textContent.toUpperCase(); 363 } else if (textTransform.equals("lowercase")) { 364 textContent = textContent.toUpperCase(); 365 } 366 367 t = frame.addText((int) x, (int) y, textContent, null); 365 368 366 369 t.setColor(rgbStringToColor(color)); … … 396 399 397 400 String bgSize = ((String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "background-size" })).toLowerCase(); 401 String bgRepeat = ((String) JavaFX.JSObjectCall.invoke(style, "getPropertyValue", new Object[] { "background-repeat" })).toLowerCase(); 398 402 399 403 // Returns "[x]px [y]px", "[x]% [y]%", "[x]px [y]%" or "[x]% [y]px" … … 446 450 447 451 try { 448 WebParser.addImageFromUrl(bgImage, frame, x, y, bgWidth, cropStartX, cropStartY, cropEndX, cropEndY); 452 System.out.println("START-Y: " + cropStartY + " END-Y: " + cropEndY); 453 WebParser.addImageFromUrl(bgImage, frame, x, y, bgWidth, cropStartX, cropStartY, cropEndX, cropEndY, bgRepeat); 449 454 } catch (MalformedURLException mue) { 450 455 // probably a 'data:' url, not supported yet … … 457 462 if (currentNode.getNodeName().toLowerCase().equals("img") && (imgSrc = JavaFX.JSObjectGetMember.invoke(currentNode, "src").toString()) != null) { 458 463 try { 459 WebParser.addImageFromUrl(imgSrc, frame, x, y, (int) width, null, null, null, null );464 WebParser.addImageFromUrl(imgSrc, frame, x, y, (int) width, null, null, null, null, null); 460 465 } catch (MalformedURLException mue) { 461 466 // probably a 'data:' url, not supported yet … … 547 552 * @throws IOException 548 553 */ 549 private static void addImageFromUrl(String imgSrc, final Frame frame, float x, float y, int width, Integer cropStartX, Integer cropStartY, Integer cropEndX, Integer cropEndY )554 private static void addImageFromUrl(String imgSrc, final Frame frame, float x, float y, int width, Integer cropStartX, Integer cropStartY, Integer cropEndX, Integer cropEndY, String repeat) 550 555 throws MalformedURLException, 551 556 IOException { … … 564 569 out.mkdirs(); 565 570 ImageIO.write(img, "png", out); 566 567 571 568 572 if (cropEndX == null || cropStartX == null || cropEndY == null || cropStartY == null) { 569 573 cropStartX = 0; … … 571 575 cropEndX = img.getWidth(); 572 576 cropEndY = img.getHeight(); 577 } else if (cropStartX < 0) { 578 cropEndX = cropEndX - cropStartX; 579 x = x + Math.abs(cropStartX); 580 cropStartX = 0; 581 } 582 583 if (cropStartY < 0) { 584 cropEndY = cropEndY - cropStartY; 585 y = y + Math.abs(cropStartY); 586 cropStartY = 0; 587 573 588 } 574 589 … … 576 591 width = img.getWidth(); 577 592 } 578 593 594 if (repeat != null) { 595 if (repeat.equals("no-repeat")) { 596 int tmpCropEndY = (int) (cropStartY + ((float) width / img.getWidth()) * img.getHeight()); 597 int tmpCropEndX = cropStartX + width; 598 599 cropEndX = (cropEndX < tmpCropEndX) ? cropEndX : tmpCropEndX; 600 cropEndY = (cropEndY < tmpCropEndY) ? cropEndY : tmpCropEndY; 601 } else if (repeat.equals("repeat-x")) { 602 int tmpCropEndY = (int) (cropStartY + ((float) width / img.getWidth()) * img.getHeight()); 603 cropEndY = (cropEndY < tmpCropEndY) ? cropEndY : tmpCropEndY; 604 } else if (repeat.equals("repeat-y")) { 605 int tmpCropEndX = cropStartX + width; 606 cropEndX = (cropEndX < tmpCropEndX) ? cropEndX : tmpCropEndX; 607 } 608 } 609 579 610 Text text = new Text("@i: " + out.getName() + " " + width); 580 611 text.setPosition(x, y); … … 583 614 584 615 float invScale = 1 / pic.getScale(); 585 586 // System.out.println("unscaled crop: " + cropStartX + " " + cropStartY + " " + cropEndX + " " + cropEndY); 587 // System.out.println("crop: " + cropStartX * invScale + " " + cropStartY * invScale + " " + cropEndX * invScale + " " + cropEndY * invScale); 588 616 589 617 pic.setCrop((int)(cropStartX * invScale), (int)(cropStartY * invScale), (int)(cropEndX * invScale), (int)(cropEndY * invScale)); 590 591 // System.out.println("source = " + pic.getSource().getText()); 592 618 593 619 frame.addItem(pic); 594 620 pic.anchor();
Note:
See TracChangeset
for help on using the changeset viewer.