1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
|
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* Copyright (C) 2013-2014 IBM Corporation and Others. All Rights Reserved.
*/
import java.awt.Color;
import java.awt.Composite;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.Image;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.RenderingHints.Key;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ImageObserver;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.RenderableImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.AttributedCharacterIterator;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.TreeMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* This test runs against a test XML file. It opens the fonts and attempts
* to shape and layout glyphs.
* Note that the test is highly environment dependent- you must have
* the same versions of the same fonts available or the test will fail.
*
* It is similar to letest which is part of ICU.
* For reference, here are some reference items:
* ICU's test file:
* http://source.icu-project.org/repos/icu/icu/trunk/source/test/testdata/letest.xml
* ICU's readme for the similar test:
* http://source.icu-project.org/repos/icu/icu/trunk/source/test/letest/readme.html
*
* @bug 8054203
* @test
* @summary manual test of layout engine behavior. Takes an XML control file.
* @compile TestLayoutVsICU.java
* @author srl
* @run main/manual
*/
public class TestLayoutVsICU {
public static boolean OPT_DRAW = false;
public static boolean OPT_VERBOSE = false;
public static boolean OPT_FAILMISSING = false;
public static boolean OPT_NOTHROW= false; // if true - don't stop on failure
public static int docs = 0; // # docs processed
public static int skipped = 0; // cases skipped due to bad font
public static int total = 0; // cases processed
public static int bad = 0; // cases with errs
public static final String XML_LAYOUT_TESTS = "layout-tests"; // top level
public static final String XML_TEST_CASE = "test-case";
public static final String XML_TEST_FONT = "test-font";
public static final String XML_TEST_TEXT = "test-text";
public static final String XML_RESULT_GLYPHS = "result-glyphs";
public static final String XML_ID = "id";
public static final String XML_SCRIPT = "script";
public static final String XML_NAME = "name";
public static final String XML_VERSION = "version";
public static final String XML_CHECKSUM = "checksum";
public static final String XML_RESULT_INDICES = "result-indices";
public static final String XML_RESULT_POSITIONS = "result-positions";
/**
* @param args
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
*/
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
System.out.println("Java " + System.getProperty("java.version") + " from " + System.getProperty("java.vendor"));
TestLayoutVsICU tlvi = null;
for(String arg : args) {
if(arg.equals("-d")) {
OPT_DRAW = true;
} else if(arg.equals("-n")) {
OPT_NOTHROW = true;
} else if(arg.equals("-v")) {
OPT_VERBOSE = true;
} else if(arg.equals("-f")) {
OPT_FAILMISSING = true;
} else {
if(tlvi == null) {
tlvi = new TestLayoutVsICU();
}
try {
tlvi.show(arg);
} finally {
if(OPT_VERBOSE) {
System.out.println("# done with " + arg);
}
}
}
}
if(tlvi == null) {
throw new IllegalArgumentException("No XML input. Usage: " + TestLayoutVsICU.class.getSimpleName() + " [-d][-v][-f] letest.xml ...");
} else {
System.out.println("\n\nRESULTS:\n");
System.out.println(skipped+"\tskipped due to missing font");
System.out.println(total+"\ttested of which:");
System.out.println(bad+"\twere bad");
if(bad>0) {
throw new InternalError("One or more failure(s)");
}
}
}
String id;
private void show(String arg) throws ParserConfigurationException, SAXException, IOException {
id = "<none>";
File xmlFile = new File(arg);
if(!xmlFile.exists()) {
throw new FileNotFoundException("Can't open input XML file " + arg);
}
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
if(OPT_VERBOSE) {
System.out.println("# Parsing " + xmlFile.getAbsolutePath());
}
Document doc = db.parse(xmlFile);
Element e = doc.getDocumentElement();
if(!XML_LAYOUT_TESTS.equals(e.getNodeName())) {
throw new IllegalArgumentException("Document " + xmlFile.getAbsolutePath() + " does not have <layout-tests> as its base");
}
NodeList testCases = e.getElementsByTagName(XML_TEST_CASE);
for(int caseNo=0;caseNo<testCases.getLength();caseNo++) {
final Node testCase = testCases.item(caseNo);
final Map<String,String> testCaseAttrs = attrs(testCase);
id = testCaseAttrs.get(XML_ID);
final String script = testCaseAttrs.get(XML_SCRIPT);
String testText = null;
Integer[] expectGlyphs = null;
Integer[] expectIndices = null;
Map<String,String> fontAttrs = null;
if(OPT_VERBOSE) {
System.out.println("#"+caseNo+" id="+id + ", script="+script);
}
NodeList children = testCase.getChildNodes();
for(int sub=0;sub<children.getLength();sub++) {
Node n = children.item(sub);
if(n.getNodeType()!=Node.ELEMENT_NODE) continue;
String nn = n.getNodeName();
if(nn.equals(XML_TEST_FONT)) {
fontAttrs = attrs(n);
} else if(nn.equals(XML_TEST_TEXT)) {
testText = n.getTextContent();
} else if(nn.equals(XML_RESULT_GLYPHS)) {
String hex = n.getTextContent();
expectGlyphs = parseHexArray(hex);
} else if(nn.equals(XML_RESULT_INDICES)) {
String hex = n.getTextContent();
expectIndices = parseHexArray(hex);
} else if(OPT_VERBOSE) {
System.out.println("Ignoring node " + nn);
}
}
if(fontAttrs == null) {
throw new IllegalArgumentException(id + " Missing node " + XML_TEST_FONT);
}
if(testText == null) {
throw new IllegalArgumentException(id + " Missing node " + XML_TEST_TEXT);
}
String fontName = fontAttrs.get(XML_NAME);
Font f = getFont(fontName, fontAttrs);
if(f==null) {
if(OPT_FAILMISSING) {
throw new MissingResourceException("Missing font, abort test", Font.class.getName(), fontName);
}
System.out.println("Skipping " + id + " because font is missing: " + fontName);
skipped++;
continue;
}
FontRenderContext frc = new FontRenderContext(null, true, true);
TextLayout tl = new TextLayout(testText,f,frc);
final List<GlyphVector> glyphs = new ArrayList<GlyphVector>();
Graphics2D myg2 = new Graphics2D(){
@Override
public void draw(Shape s) {
// TODO Auto-generated method stub
}
@Override
public boolean drawImage(Image img, AffineTransform xform,
ImageObserver obs) {
// TODO Auto-generated method stub
return false;
}
@Override
public void drawImage(BufferedImage img,
BufferedImageOp op, int x, int y) {
// TODO Auto-generated method stub
}
@Override
public void drawRenderedImage(RenderedImage img,
AffineTransform xform) {
// TODO Auto-generated method stub
}
@Override
public void drawRenderableImage(RenderableImage img,
AffineTransform xform) {
// TODO Auto-generated method stub
}
@Override
public void drawString(String str, int x, int y) {
// TODO Auto-generated method stub
}
@Override
public void drawString(String str, float x, float y) {
// TODO Auto-generated method stub
}
@Override
public void drawString(
AttributedCharacterIterator iterator, int x, int y) {
// TODO Auto-generated method stub
}
@Override
public void drawString(
AttributedCharacterIterator iterator, float x,
float y) {
// TODO Auto-generated method stub
}
@Override
public void drawGlyphVector(GlyphVector g, float x, float y) {
if(x!=0.0 || y!=0.0) {
throw new InternalError("x,y should be 0 but got " + x+","+y);
}
//System.err.println("dGV : " + g.toString() + " @ "+x+","+y);
glyphs.add(g);
}
@Override
public void fill(Shape s) {
// TODO Auto-generated method stub
}
@Override
public boolean hit(Rectangle rect, Shape s, boolean onStroke) {
// TODO Auto-generated method stub
return false;
}
@Override
public GraphicsConfiguration getDeviceConfiguration() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setComposite(Composite comp) {
// TODO Auto-generated method stub
}
@Override
public void setPaint(Paint paint) {
// TODO Auto-generated method stub
}
@Override
public void setStroke(Stroke s) {
// TODO Auto-generated method stub
}
@Override
public void setRenderingHint(Key hintKey, Object hintValue) {
// TODO Auto-generated method stub
}
@Override
public Object getRenderingHint(Key hintKey) {
// TODO Auto-generated method stub
return null;
}
@Override
public void setRenderingHints(Map<?, ?> hints) {
// TODO Auto-generated method stub
}
@Override
public void addRenderingHints(Map<?, ?> hints) {
// TODO Auto-generated method stub
}
@Override
public RenderingHints getRenderingHints() {
// TODO Auto-generated method stub
return null;
}
@Override
public void translate(int x, int y) {
// TODO Auto-generated method stub
}
@Override
public void translate(double tx, double ty) {
// TODO Auto-generated method stub
}
@Override
public void rotate(double theta) {
// TODO Auto-generated method stub
}
@Override
public void rotate(double theta, double x, double y) {
// TODO Auto-generated method stub
}
@Override
public void scale(double sx, double sy) {
// TODO Auto-generated method stub
}
@Override
public void shear(double shx, double shy) {
// TODO Auto-generated method stub
}
@Override
public void transform(AffineTransform Tx) {
// TODO Auto-generated method stub
}
@Override
public void setTransform(AffineTransform Tx) {
// TODO Auto-generated method stub
}
@Override
public AffineTransform getTransform() {
// TODO Auto-generated method stub
return null;
}
@Override
public Paint getPaint() {
// TODO Auto-generated method stub
return null;
}
@Override
public Composite getComposite() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setBackground(Color color) {
// TODO Auto-generated method stub
}
@Override
public Color getBackground() {
// TODO Auto-generated method stub
return null;
}
@Override
public Stroke getStroke() {
// TODO Auto-generated method stub
return null;
}
@Override
public void clip(Shape s) {
// TODO Auto-generated method stub
}
@Override
public FontRenderContext getFontRenderContext() {
// TODO Auto-generated method stub
return null;
}
@Override
public Graphics create() {
// TODO Auto-generated method stub
return null;
}
@Override
public Color getColor() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setColor(Color c) {
// TODO Auto-generated method stub
}
@Override
public void setPaintMode() {
// TODO Auto-generated method stub
}
@Override
public void setXORMode(Color c1) {
// TODO Auto-generated method stub
}
@Override
public Font getFont() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setFont(Font font) {
// TODO Auto-generated method stub
}
@Override
public FontMetrics getFontMetrics(Font f) {
// TODO Auto-generated method stub
return null;
}
@Override
public Rectangle getClipBounds() {
// TODO Auto-generated method stub
return null;
}
@Override
public void clipRect(int x, int y, int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void setClip(int x, int y, int width, int height) {
// TODO Auto-generated method stub
}
@Override
public Shape getClip() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setClip(Shape clip) {
// TODO Auto-generated method stub
}
@Override
public void copyArea(int x, int y, int width, int height,
int dx, int dy) {
// TODO Auto-generated method stub
}
@Override
public void drawLine(int x1, int y1, int x2, int y2) {
// TODO Auto-generated method stub
}
@Override
public void fillRect(int x, int y, int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void clearRect(int x, int y, int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void drawRoundRect(int x, int y, int width,
int height, int arcWidth, int arcHeight) {
// TODO Auto-generated method stub
}
@Override
public void fillRoundRect(int x, int y, int width,
int height, int arcWidth, int arcHeight) {
// TODO Auto-generated method stub
}
@Override
public void drawOval(int x, int y, int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void fillOval(int x, int y, int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void drawArc(int x, int y, int width, int height,
int startAngle, int arcAngle) {
// TODO Auto-generated method stub
}
@Override
public void fillArc(int x, int y, int width, int height,
int startAngle, int arcAngle) {
// TODO Auto-generated method stub
}
@Override
public void drawPolyline(int[] xPoints, int[] yPoints,
int nPoints) {
// TODO Auto-generated method stub
}
@Override
public void drawPolygon(int[] xPoints, int[] yPoints,
int nPoints) {
// TODO Auto-generated method stub
}
@Override
public void fillPolygon(int[] xPoints, int[] yPoints,
int nPoints) {
// TODO Auto-generated method stub
}
@Override
public boolean drawImage(Image img, int x, int y,
ImageObserver observer) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean drawImage(Image img, int x, int y,
int width, int height, ImageObserver observer) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean drawImage(Image img, int x, int y,
Color bgcolor, ImageObserver observer) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean drawImage(Image img, int x, int y,
int width, int height, Color bgcolor,
ImageObserver observer) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean drawImage(Image img, int dx1, int dy1,
int dx2, int dy2, int sx1, int sy1, int sx2,
int sy2, ImageObserver observer) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean drawImage(Image img, int dx1, int dy1,
int dx2, int dy2, int sx1, int sy1, int sx2,
int sy2, Color bgcolor, ImageObserver observer) {
// TODO Auto-generated method stub
return false;
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
};
tl.draw(myg2, 0, 0);
if(glyphs.size() != 1) {
err("drew " + glyphs.size() + " times - expected 1");
total++;
bad++;
continue;
}
boolean isBad = false;
GlyphVector gv = glyphs.get(0);
// GLYPHS
int gotGlyphs[] = gv.getGlyphCodes(0, gv.getNumGlyphs(), new int[gv.getNumGlyphs()]);
int count = Math.min(gotGlyphs.length, expectGlyphs.length); // go up to this count
for(int i=0;i<count;i++) {
if(gotGlyphs[i]!=expectGlyphs[i]) {
err("@"+i+" - got \tglyph 0x" + Integer.toHexString(gotGlyphs[i]) + " wanted 0x" + Integer.toHexString(expectGlyphs[i]));
isBad=true;
break;
}
}
// INDICES
int gotIndices[] = gv.getGlyphCharIndices(0, gv.getNumGlyphs(), new int[gv.getNumGlyphs()]);
for(int i=0;i<count;i++) {
if(gotIndices[i]!=expectIndices[i]) {
err("@"+i+" - got \tindex 0x" + Integer.toHexString(gotGlyphs[i]) + " wanted 0x" + Integer.toHexString(expectGlyphs[i]));
isBad=true;
break;
}
}
// COUNT
if(gotGlyphs.length != expectGlyphs.length) {
System.out.println("Got " + gotGlyphs.length + " wanted " + expectGlyphs.length + " glyphs");
isBad=true;
} else {
if(OPT_VERBOSE) {
System.out.println(">> OK: " + gotGlyphs.length + " glyphs");
}
}
if(isBad) {
bad++;
System.out.println("* FAIL: " + id + " /\t" + fontName);
} else {
System.out.println("* OK : " + id + " /\t" + fontName);
}
total++;
}
}
private boolean verifyFont(File f, Map<String, String> fontAttrs) {
InputStream fis = null;
String fontName = fontAttrs.get(XML_NAME);
int count=0;
try {
fis = new BufferedInputStream(new FileInputStream(f));
int i = 0;
int r;
try {
while((r=fis.read())!=-1) {
i+=(int)r;
count++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
if(OPT_VERBOSE) {
System.out.println("for " + f.getAbsolutePath() + " chks = 0x" + Integer.toHexString(i) + " size=" + count);
}
String theirStr = fontAttrs.get("rchecksum");
String ourStr = Integer.toHexString(i).toLowerCase();
if(theirStr!=null) {
if(theirStr.startsWith("0x")) {
theirStr = theirStr.substring(2).toLowerCase();
} else {
theirStr = theirStr.toLowerCase();
}
long theirs = Integer.parseInt(theirStr, 16);
if(theirs != i) {
err("WARNING: rchecksum for " + fontName + " was " + i + " (0x"+ourStr+") "+ " but file said " + theirs +" (0x"+theirStr+") - perhaps a different font?");
return false;
} else {
if(OPT_VERBOSE) {
System.out.println(" rchecksum for " + fontName + " OK");
}
return true;
}
} else {
//if(OPT_VERBOSE) {
System.err.println("WARNING: rchecksum for " + fontName + " was " + i + " (0x"+ourStr+") "+ " but rchecksum was MISSING. Old ICU data?");
//}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} finally {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return true;
}
private Integer[] parseHexArray(String hex) {
List<Integer> ret = new ArrayList<Integer>();
String items[] = hex.split("[\\s,]");
for(String i : items) {
if(i.isEmpty()) continue;
if(i.startsWith("0x")) {
i = i.substring(2);
}
ret.add(Integer.parseInt(i, 16));
}
return ret.toArray(new Integer[0]);
}
private void err(String string) {
if(OPT_NOTHROW) {
System.out.println(id+" ERROR: " + string +" (continuing due to -n)");
} else {
throw new InternalError(id+ ": " + string);
}
}
private Font getFont(String fontName, Map<String, String> fontAttrs) {
Font f;
if(false)
try {
f = Font.getFont(fontName);
if(f!=null) {
if(OPT_VERBOSE) {
System.out.println("Loaded default path to " + fontName);
}
return f;
}
} catch(Throwable t) {
if(OPT_VERBOSE) {
t.printStackTrace();
System.out.println("problem loading font " + fontName + " - " + t.toString());
}
}
File homeDir = new File(System.getProperty("user.home"));
File fontDir = new File(homeDir, "fonts");
File fontFile = new File(fontDir, fontName);
//System.out.println("## trying " + fontFile.getAbsolutePath());
if(fontFile.canRead()) {
try {
if(!verifyFont(fontFile,fontAttrs)) {
System.out.println("Warning: failed to verify " + fontName);
}
f = Font.createFont(Font.TRUETYPE_FONT, fontFile);
if(f!=null & OPT_VERBOSE) {
System.out.println("> loaded from " + fontFile.getAbsolutePath() + " - " + f.toString());
}
return f;
} catch (FontFormatException e) {
if(OPT_VERBOSE) {
e.printStackTrace();
System.out.println("problem loading font " + fontName + " - " + e.toString());
}
} catch (IOException e) {
if(OPT_VERBOSE) {
e.printStackTrace();
System.out.println("problem loading font " + fontName + " - " + e.toString());
}
}
}
return null;
}
private static Map<String, String> attrs(Node testCase) {
Map<String,String> rv = new TreeMap<String,String>();
NamedNodeMap nnm = testCase.getAttributes();
for(int i=0;i<nnm.getLength();i++) {
Node n = nnm.item(i);
String k = n.getNodeName();
String v = n.getNodeValue();
rv.put(k, v);
}
return rv;
}
}
|