java - 是否可以在 JEditorPane 中缩放/缩放字体大小(和图像大小)

标签 java

有没有可能在JEditorPane中以某种方式缩放文本和图片。

我不喜欢遍历所有 HTML 页面来使字体变大。

最佳答案

我定制了 HTMLEditorKit,它可以放大/缩小 JEditorPane 的 HTML 内容,它具有更好的渲染性能,我称之为 LargeHTMLEditorKit:

/**
 * An extended {@link HTMLEditorKit} that allow faster
 * rendering of large html files and allow zooming of content.
 * @author Alessio Pollero
 * @version 1.0
 */
public class LargeHTMLEditorKit extends HTMLEditorKit {

     ViewFactory factory = new MyViewFactory();

        @Override
        public ViewFactory getViewFactory() {
            return factory;
        }

        class MyViewFactory extends HTMLFactory {
            @Override
            public View create(Element elem) {
                AttributeSet attrs = elem.getAttributes();
                Object elementName = attrs.getAttribute(AbstractDocument.ElementNameAttribute);
                Object o = (elementName != null) ? null : attrs.getAttribute(StyleConstants.NameAttribute);
                if (o instanceof HTML.Tag) {
                    HTML.Tag kind = (HTML.Tag) o;
                    if (kind == HTML.Tag.HTML) {
                        return new HTMLBlockView(elem);
                    }
                    else if (kind == HTML.Tag.IMPLIED) {
                        String ws = (String) elem.getAttributes().getAttribute(CSS.Attribute.WHITE_SPACE);
                        if ((ws != null) && ws.equals("pre")) {
                            return super.create(elem);
                        }
                        return new HTMLParagraphView(elem);
                    } else if ((kind == HTML.Tag.P) ||
                            (kind == HTML.Tag.H1) ||
                            (kind == HTML.Tag.H2) ||
                            (kind == HTML.Tag.H3) ||
                            (kind == HTML.Tag.H4) ||
                            (kind == HTML.Tag.H5) ||
                            (kind == HTML.Tag.H6) ||
                            (kind == HTML.Tag.DT)) {
                        // paragraph
                        return new HTMLParagraphView(elem);
                    }
                }
                return super.create(elem);
            }

        }


        private class HTMLBlockView extends BlockView {

            public HTMLBlockView(Element elem) {
                super(elem,  View.Y_AXIS);
            }

            @Override
            protected void layout(int width, int height) {
                if (width<Integer.MAX_VALUE) {
                    super.layout(new Double(width / getZoomFactor()).intValue(),
                             new Double(height *
                                        getZoomFactor()).intValue());
                }
            }

            public double getZoomFactor() {
                Double scale = (Double) getDocument().getProperty("ZOOM_FACTOR");
                if (scale != null) {
                    return scale.doubleValue();
                }

                return 1;
            }

            @Override
            public void paint(Graphics g, Shape allocation) {
                Graphics2D g2d = (Graphics2D) g;
                double zoomFactor = getZoomFactor();
                AffineTransform old = g2d.getTransform();
                g2d.scale(zoomFactor, zoomFactor);
                super.paint(g2d, allocation);
                g2d.setTransform(old);
            }

            @Override
            public float getMinimumSpan(int axis) {
                float f = super.getMinimumSpan(axis);
                f *= getZoomFactor();
                return f;
            }

            @Override
            public float getMaximumSpan(int axis) {
                float f = super.getMaximumSpan(axis);
                f *= getZoomFactor();
                return f;
            }

            @Override
            public float getPreferredSpan(int axis) {
                float f = super.getPreferredSpan(axis);
                f *= getZoomFactor();
                return f;
            }

            @Override
            public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
                double zoomFactor = getZoomFactor();
                Rectangle alloc;
                alloc = a.getBounds();
                Shape s = super.modelToView(pos, alloc, b);
                alloc = s.getBounds();
                alloc.x *= zoomFactor;
                alloc.y *= zoomFactor;
                alloc.width *= zoomFactor;
                alloc.height *= zoomFactor;

                return alloc;
            }

            @Override
            public int viewToModel(float x, float y, Shape a,
                                   Position.Bias[] bias) {
                double zoomFactor = getZoomFactor();
                Rectangle alloc = a.getBounds();
                x /= zoomFactor;
                y /= zoomFactor;
                alloc.x /= zoomFactor;
                alloc.y /= zoomFactor;
                alloc.width /= zoomFactor;
                alloc.height /= zoomFactor;

                return super.viewToModel(x, y, alloc, bias);
            }

        }
}

这是必需的 HTMLParagraphView :

class HTMLParagraphView extends ParagraphView {

     public static int MAX_VIEW_SIZE=100;

        public HTMLParagraphView(Element elem) {
            super(elem);
            strategy = new HTMLParagraphView.HTMLFlowStrategy();
        }

        public static class HTMLFlowStrategy extends FlowStrategy {
            protected View createView(FlowView fv, int startOffset, int spanLeft, int rowIndex) {
                View res=super.createView(fv, startOffset, spanLeft, rowIndex);
                if (res.getEndOffset()-res.getStartOffset()> MAX_VIEW_SIZE) {
                    res = res.createFragment(startOffset, startOffset+ MAX_VIEW_SIZE);
                }
                return res;
            }

        }
        public int getResizeWeight(int axis) {
            return 0;
        }
}

然后你可以这样使用它:

//Create a new JEditorPane
JEditorPane yourPane = new JEditorPane();
//Set the custom HTMLEditorKit
yourPane.setEditorKit(new LargeHTMLEditorKit());
//Set the zoom to 150%
yourPane.getDocument().putProperty("ZOOM_FACTOR", new Double(1.5));

关于java - 是否可以在 JEditorPane 中缩放/缩放字体大小(和图像大小),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/680817/

相关文章:

java - 如何捕获从 JOptionPane 按下的确定按钮

java - 如何在“取消”按钮上清除时间选择器的共享首选项?

JavaFX 需要多个 ImageView 节点而不是对一个节点的引用?

java - 如何设计基于微服务的应用程序,每个微服务都有自己的数据源?

java - Proguard 将重写的方法保留在扩展另一个接口(interface)的接口(interface)内

java - Twitch Api - 不允许授予密码

java - 这是 O(N) 表示法中需要常量的情况吗?

java - 无需安装即可使用旧版 JRE 在 Eclipse 中编译 Java 程序

java - Eclipse启动时出现"Could not create the JVM"错误

java - 检查对象是否来自某个包