Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix layout with italic fonts #18

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ private static void doBreakText(LayoutContext c,
int lastGraphicsLength = 0;

while (right > 0 && graphicsLength <= avail) {
lastGraphicsLength = graphicsLength;
graphicsLength += c.getTextRenderer().getWidth(
c.getFontContext(), font, currentString.substring(left, right));
lastGraphicsLength = graphicsLength;
graphicsLength = c.getTextRenderer().getWidth(
c.getFontContext(), font, currentString.substring(0, right));
lastWrap = left;
left = right;
if ( tryToBreakAnywhere ) {
Expand All @@ -135,8 +135,8 @@ private static void doBreakText(LayoutContext c,
//try for the last bit too!
lastWrap = left;
lastGraphicsLength = graphicsLength;
graphicsLength += c.getTextRenderer().getWidth(
c.getFontContext(), font, currentString.substring(left));
graphicsLength = c.getTextRenderer().getWidth(
c.getFontContext(), font, currentString);
}

if (graphicsLength <= avail) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;

import org.w3c.dom.Element;
import org.xhtmlrenderer.css.constants.CSSName;
import org.xhtmlrenderer.css.constants.IdentValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.xhtmlrenderer.extend.*;
import org.xhtmlrenderer.layout.Layer;
import org.xhtmlrenderer.layout.SharedContext;
import org.xhtmlrenderer.swing.RootPanel;

/**
* Supplies information about the context in which rendering will take place
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.xhtmlrenderer.simple;

import org.xhtmlrenderer.extend.TextRenderer;
import org.xhtmlrenderer.util.Uu;

import java.awt.Graphics;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
package org.xhtmlrenderer.swing;

import java.awt.Cursor;
import java.awt.event.MouseEvent;

import org.xhtmlrenderer.render.Box;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.awt.image.BufferedImage;
import java.util.logging.Level;

import org.xhtmlrenderer.extend.ReplacedElement;
import org.xhtmlrenderer.layout.LayoutContext;
import org.xhtmlrenderer.util.ImageUtil;
import org.xhtmlrenderer.util.Configuration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/
package org.xhtmlrenderer.swing;

import org.xhtmlrenderer.extend.TextRenderer;
import org.xhtmlrenderer.simple.XHTMLPanel;
import org.xhtmlrenderer.util.Uu;
import org.xhtmlrenderer.util.XRLog;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
package org.xhtmlrenderer.swing;

import java.awt.Rectangle;
import java.awt.event.MouseEvent;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xhtmlrenderer.context.StyleReference;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
package org.xhtmlrenderer.swing;

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.font.GlyphVector;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.Map;

import org.xhtmlrenderer.extend.FSGlyphVector;
Expand Down Expand Up @@ -198,13 +200,19 @@ public int getWidth(FontContext fc, FSFont font, String string) {
graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, fractionalFontMetricsHint);
Font awtFont = ((AWTFSFont)font).getAWTFont();
int width = 0;
if(fractionalFontMetricsHint == RenderingHints.VALUE_FRACTIONALMETRICS_ON) {
width = (int)Math.round(
graphics.getFontMetrics(awtFont).getStringBounds(string, graphics).getWidth());
} else {
width = (int)Math.ceil(
graphics.getFontMetrics(awtFont).getStringBounds(string, graphics).getWidth());
}
FontMetrics fontMetrics = graphics.getFontMetrics(awtFont);
Rectangle2D stringBounds = fontMetrics.getStringBounds(string, graphics);
GlyphVector vector = awtFont.createGlyphVector(graphics.getFontRenderContext(), string);
Rectangle2D visualBounds = vector.getVisualBounds();
double minX = Math.min(visualBounds.getMinX(), stringBounds.getMinX());
double maxX = Math.max(visualBounds.getMaxX(), stringBounds.getMaxX());
double fullWidth = maxX - minX;
if (fractionalFontMetricsHint == RenderingHints.VALUE_FRACTIONALMETRICS_ON) {
width = (int) Math.round(fullWidth);
} else {
width = (int) Math.ceil(fullWidth);
}

graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, fracHint);
return width;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
*/
package org.xhtmlrenderer.swing;

import java.awt.event.MouseEvent;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xhtmlrenderer.render.Box;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.xhtmlrenderer.util.ImageUtil;
import org.xhtmlrenderer.util.XRLog;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
import org.xhtmlrenderer.swing.BoxRenderer;
import org.xhtmlrenderer.swing.Java2DRenderer;
import org.xhtmlrenderer.util.FSImageWriter;
import org.xhtmlrenderer.util.IOUtil;
import org.xhtmlrenderer.util.Zipper;

import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.*;
import java.util.List;

/**
* @author patrick
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,9 @@
*/
package org.xhtmlrenderer.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import java.util.logging.Handler;
import java.util.logging.Formatter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.lowagie.text.pdf.PdfAcroForm;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfFormField;
import com.lowagie.text.pdf.PdfWriter;
import org.w3c.dom.Element;
import org.xhtmlrenderer.layout.LayoutContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import java.util.HashMap;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
Expand Down