From f989f4922ee0e493105b3f9ec8adf125c3e71fd6 Mon Sep 17 00:00:00 2001 From: kerry Date: Mon, 16 Apr 2018 09:51:08 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-7481=20&&=20REPORT-7480=20html=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E4=B9=8BIMG=E6=A0=87=E7=AD=BE;html=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E4=B9=8B=E5=88=86=E9=A1=B5=E6=96=AD=E5=BC=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fr/third/v2/lowagie/text/Font.java | 48 ++++------ .../com/fr/third/v2/lowagie/text/Image.java | 4 +- .../html/simpleparser/FactoryProperties.java | 5 +- .../text/html/simpleparser/HTMLWorker.java | 3 + .../third/v2/lowagie/text/pdf/PdfChunk.java | 91 ++++++------------- .../fr/third/v2/lowagie/text/pdf/PdfFont.java | 43 ++++++--- 6 files changed, 80 insertions(+), 114 deletions(-) diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/Font.java b/fine-itext/src/com/fr/third/v2/lowagie/text/Font.java index a9fb4d267..f7269bb17 100644 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/Font.java +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/Font.java @@ -133,6 +133,14 @@ public class Font implements Comparable { /** the external font */ private BaseFont baseFont = null; + private String fontName; + + + public String getFontName(){ + return fontName; + } + + // constructors /** @@ -169,6 +177,13 @@ public class Font implements Comparable { this.color = color; } + public Font(String face, float size, int style, Color color) { + this.fontName = face; + this.size =size; + this.style = style; + this.color = color; + } + /** * Constructs a Font. * @@ -335,35 +350,7 @@ public class Font implements Comparable { * @return the familyname */ public String getFamilyname() { - String tmp = "unknown"; - switch (getFamily()) { - case Font.COURIER: - return FontFactory.COURIER; - case Font.HELVETICA: - return FontFactory.HELVETICA; - case Font.TIMES_ROMAN: - return FontFactory.TIMES_ROMAN; - case Font.SYMBOL: - return FontFactory.SYMBOL; - case Font.ZAPFDINGBATS: - return FontFactory.ZAPFDINGBATS; - default: - if (baseFont != null) { - String[][] names = baseFont.getFamilyFontName(); - for (int i = 0; i < names.length; i++) { - if ("0".equals(names[i][2])) { - return names[i][3]; - } - if ("1033".equals(names[i][2])) { - tmp = names[i][3]; - } - if ("".equals(names[i][2])) { - tmp = names[i][3]; - } - } - } - } - return tmp; + return getFontName(); } /** @@ -374,9 +361,10 @@ public class Font implements Comparable { * A String representing a certain font-family. */ public void setFamily(String family) { - this.family = getFamilyIndex(family); + this.fontName = family; } + /** * Translates a String -value of a certain family into the * index that is used for this family in this class. diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/Image.java b/fine-itext/src/com/fr/third/v2/lowagie/text/Image.java index 32f6a53fe..0f240d566 100644 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/Image.java +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/Image.java @@ -321,7 +321,9 @@ public abstract class Image extends Rectangle { } throw new IOException(url.toString() + " is not a recognized imageformat."); - } finally { + }catch (Exception e){ + return null; + }finally { if (is != null) { is.close(); } diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java b/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java index 862dfde8a..78508f064 100644 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java @@ -193,10 +193,7 @@ public class FactoryProperties { if (value != null) size = Float.parseFloat(value); Color color = Markup.decodeColor(props.getProperty("color")); - String encoding = props.getProperty("encoding"); - if (encoding == null) - encoding = BaseFont.WINANSI; - return fontImp.getFont(face, encoding, true, size, style, color); + return new Font(face, size, style, color); } /** diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/HTMLWorker.java b/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/HTMLWorker.java index 47b72368e..4b7b700a8 100644 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/HTMLWorker.java +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/HTMLWorker.java @@ -338,6 +338,9 @@ public class HTMLWorker implements SimpleXMLDocHandler, DocListener { } img = Image.getInstance(src); } + if(img == null){ + return; + } String align = (String) h.get("align"); String width = (String) h.get("width"); String height = (String) h.get("height"); diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java b/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java index 9087921b0..a45255cb7 100644 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java @@ -50,6 +50,7 @@ package com.fr.third.v2.lowagie.text.pdf; import java.awt.Color; +import java.awt.FontMetrics; import java.util.HashMap; import java.util.Iterator; import java.util.Map; @@ -59,6 +60,7 @@ import com.fr.third.v2.lowagie.text.Utilities; import com.fr.third.v2.lowagie.text.Chunk; import com.fr.third.v2.lowagie.text.Font; import com.fr.third.v2.lowagie.text.Image; +import sun.font.FontDesignMetrics; /** * A PdfChunk is the PDF translation of a Chunk. @@ -124,9 +126,15 @@ public class PdfChunk { /** The font for this PdfChunk. */ protected PdfFont font; - - protected BaseFont baseFont; - + + public PdfFont getFont() { + return font; + } + + public void setFont(PdfFont font) { + this.font = font; + } + protected SplitCharacter splitCharacter; /** * Metric attributes. @@ -174,7 +182,6 @@ public class PdfChunk { this.font = other.font; this.attributes = other.attributes; this.noStroke = other.noStroke; - this.baseFont = other.baseFont; Object obj[] = (Object[])attributes.get(Chunk.IMAGE); if (obj == null) image = null; @@ -184,7 +191,6 @@ public class PdfChunk { offsetY = ((Float)obj[2]).floatValue(); changeLeading = ((Boolean)obj[3]).booleanValue(); } - encoding = font.getFont().getEncoding(); splitCharacter = (SplitCharacter)noStroke.get(Chunk.SPLITCHARACTER); if (splitCharacter == null) splitCharacter = DefaultSplitCharacter.DEFAULT; @@ -206,28 +212,20 @@ public class PdfChunk { float size = f.getSize(); if (size == Font.UNDEFINED) size = 12; - baseFont = f.getBaseFont(); int style = f.getStyle(); if (style == Font.UNDEFINED) { style = Font.NORMAL; } - if (baseFont == null) { - // translation of the font-family to a PDF font-family - baseFont = f.getCalculatedBaseFont(false); - } - else { - // bold simulation - if ((style & Font.BOLD) != 0) - attributes.put(Chunk.TEXTRENDERMODE, new Object[]{new Integer(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE), new Float(size / 30f), null}); - // italic simulation - if ((style & Font.ITALIC) != 0) - attributes.put(Chunk.SKEW, new float[]{0, ITALIC_ANGLE}); - } - font = new PdfFont(baseFont, size); - float fontSize = font.size(); - float ascender = font.getFont().getFontDescriptor(BaseFont.ASCENT, fontSize); - float descender = font.getFont().getFontDescriptor(BaseFont.DESCENT, fontSize); - height = ascender - descender; + + // bold simulation + if ((style & Font.BOLD) != 0) + attributes.put(Chunk.TEXTRENDERMODE, new Object[]{new Integer(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE), new Float(size / 30f), null}); + // italic simulation + if ((style & Font.ITALIC) != 0) + attributes.put(Chunk.SKEW, new float[]{0, ITALIC_ANGLE}); + FontMetrics metrics = FontDesignMetrics.getMetrics(new java.awt.Font(f.getFontName(), f.getStyle(), (int)f.getSize())); + font = new PdfFont(f, f.getSize()); + height = metrics.getHeight(); // other style possibilities HashMap attr = chunk.getAttributes(); if (attr != null) { @@ -259,7 +257,7 @@ public class PdfChunk { attributes.put(Chunk.ACTION, action); // the color can't be stored in a PdfFont noStroke.put(Chunk.COLOR, f.getColor()); - noStroke.put(Chunk.ENCODING, font.getFont().getEncoding()); +// noStroke.put(Chunk.ENCODING, font.getFont().getEncoding()); Object obj[] = (Object[])attributes.get(Chunk.IMAGE); if (obj == null) { image = null; @@ -267,6 +265,7 @@ public class PdfChunk { else { attributes.remove(Chunk.HSCALE); // images are scaled in other ways image = (Image)obj[0]; + this.height = image.getHeight(); offsetX = ((Float)obj[1]).floatValue(); offsetY = ((Float)obj[2]).floatValue(); changeLeading = ((Boolean)obj[3]).booleanValue(); @@ -275,7 +274,6 @@ public class PdfChunk { Float hs = (Float)attributes.get(Chunk.HSCALE); if (hs != null) font.setHorizontalScaling(hs.floatValue()); - encoding = font.getFont().getEncoding(); splitCharacter = (SplitCharacter)noStroke.get(Chunk.SPLITCHARACTER); if (splitCharacter == null) splitCharacter = DefaultSplitCharacter.DEFAULT; @@ -290,7 +288,7 @@ public class PdfChunk { * @return the Unicode equivalent */ public int getUnicodeEquivalent(int c) { - return baseFont.getUnicodeEquivalent(c); + return 0; } protected int getWord(String text, int start) { @@ -338,38 +336,7 @@ public class PdfChunk { int length = value.length(); char valueArray[] = value.toCharArray(); char character = 0; - BaseFont ft = font.getFont(); boolean surrogate = false; - if (ft.getFontType() == BaseFont.FONT_TYPE_CJK && ft.getUnicodeEquivalent(' ') != ' ') { - while (currentPosition < length) { - // the width of every character is added to the currentWidth - char cidChar = valueArray[currentPosition]; - character = (char)ft.getUnicodeEquivalent(cidChar); - // if a newLine or carriageReturn is encountered - if (character == '\n') { - newlineSplit = true; - String returnValue = value.substring(currentPosition + 1); - value = value.substring(0, currentPosition); - if (value.length() < 1) { - value = "\u0001"; - } - PdfChunk pc = new PdfChunk(returnValue, this); - return pc; - } - currentWidth += font.width(cidChar); - if (character == ' ') { - lastSpace = currentPosition + 1; - lastSpaceWidth = currentWidth; - } - if (currentWidth > width) - break; - // if a split-character is encountered, the splitPosition is altered - if (splitCharacter.isSplitCharacter(0, currentPosition, length, valueArray, thisChunk)) - splitPosition = currentPosition + 1; - currentPosition++; - } - } - else { while (currentPosition < length) { // the width of every character is added to the currentWidth character = valueArray[currentPosition]; @@ -381,9 +348,6 @@ public class PdfChunk { inc = 2; String returnValue = value.substring(currentPosition + inc); value = value.substring(0, currentPosition); - if (value.length() < 1) { - value = " "; - } PdfChunk pc = new PdfChunk(returnValue, this); return pc; } @@ -405,8 +369,7 @@ public class PdfChunk { splitPosition = currentPosition + 1; currentPosition++; } - } - + // if all the characters fit in the total width, null is returned (there is no overflow) if (currentPosition == length) { return null; @@ -710,7 +673,7 @@ public class PdfChunk { * @return true if an image is present */ - boolean isImage() + public boolean isImage() { return image != null; } @@ -720,7 +683,7 @@ public class PdfChunk { * @return the image or null */ - Image getImage() + public Image getImage() { return image; } diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfFont.java b/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfFont.java index 571ae534a..7e65064e3 100644 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfFont.java +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfFont.java @@ -50,7 +50,11 @@ package com.fr.third.v2.lowagie.text.pdf; import com.fr.third.v2.lowagie.text.ExceptionConverter; +import com.fr.third.v2.lowagie.text.Font; import com.fr.third.v2.lowagie.text.Image; +import sun.font.FontDesignMetrics; + +import java.awt.FontMetrics; /** * PdfFont is the Pdf Font object. @@ -67,27 +71,28 @@ import com.fr.third.v2.lowagie.text.Image; * @see BadPdfFormatException */ -class PdfFont implements Comparable { - - +public class PdfFont implements Comparable { + + private Font oriFont; + /** the font metrics. */ - private BaseFont font; +// private BaseFont font; /** the size. */ private float size; /** an image. */ protected Image image; - + protected float hScale = 1; // constructors - PdfFont(BaseFont bf, float size) { + PdfFont(Font oriFont, float size) { this.size = size; - font = bf; + this.oriFont = oriFont; } - + // methods /** @@ -106,7 +111,7 @@ class PdfFont implements Comparable { PdfFont pdfFont; try { pdfFont = (PdfFont) object; - if (font != pdfFont.font) { + if (oriFont != pdfFont.oriFont) { return 1; } if (this.size() != pdfFont.size()) { @@ -118,6 +123,8 @@ class PdfFont implements Comparable { return -2; } } + + /** * Returns the size of this font. @@ -151,31 +158,37 @@ class PdfFont implements Comparable { */ float width(int character) { + FontMetrics metrics = FontDesignMetrics.getMetrics(getAwtFont()); if (image == null) - return font.getWidthPoint(character, size) * hScale; + return metrics.charWidth(character) * hScale; else return image.getScaledWidth(); } float width(String s) { + FontMetrics metrics = FontDesignMetrics.getMetrics(getAwtFont()); if (image == null) - return font.getWidthPoint(s, size) * hScale; + return metrics.stringWidth(s) * hScale; else return image.getScaledWidth(); } BaseFont getFont() { - return font; + return oriFont.getCalculatedBaseFont(false); } - + + public java.awt.Font getAwtFont() { + return new java.awt.Font(oriFont.getFontName(), oriFont.getStyle(), (int)oriFont.getSize()); + } + void setImage(Image image) { this.image = image; } static PdfFont getDefaultFont() { try { - BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false); - return new PdfFont(bf, 12); +// BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false); + return new PdfFont(new Font(), 12); } catch (Exception ee) { throw new ExceptionConverter(ee);