Java HTML 渲染“pt"对比“px"尺寸 [英] Java HTML rendering "pt" vs. "px" sizes

查看:27
本文介绍了Java HTML 渲染“pt"对比“px"尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 JLabel 中指定 html 元素的宽度时遇到了一个问题,我想我会把它运行到这里的旗杆上,看看是否有人有任何建议.

I ran into an issue with specifying a html element's width in a JLabel and thought I would run it up the flag pole here and see if anyone had any advice.

当我使用px"单位值指定元素的宽度时,结果大小实际上是 ~133%,而如果我不使用单位或使用pt",我会得到确切的大小指定.

When I specify the width of an element using a "px" unit value, the resulting size is actually ~133%, whereas if I don't use a unit, or use "pt", I get the exact size I specified.

在下面的示例中,如果您将width: 100px"更改为width: 100pt",您将获得正确的尺寸.

In the below example, if you change the "width: 100px" to "width: 100pt", you will get the right size.

这个答案 https://stackoverflow.com/a/6257861/131795 似乎是相关的,72dpi 调整似乎与我在示例中看到的不匹配相匹配.

This answer https://stackoverflow.com/a/6257861/131795 seems to be related, and the 72 dpi adjustment seems to match with the mismatch that I'm seeing in in my example.

我可能在这里对一段古老的代码感到愤怒,但为什么绝对 px 值被转换而 pt 值被视为绝对值?

I might be raging against an ancient piece of code here, but why is an absolute px value being converted and a pt value being treated as absolute?

public class test {
    public static void main(String args[]) {
        JFrame frame = new JFrame();

        JLabel label = new JLabel("<html><div style='width: 100px; background-color: red;'>test</div>");
        frame.getContentPane().add(label);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 200);
        frame.setVisible(true);
    }
}

推荐答案

好吧,我想我已经找到了 Java 缩放像素值的原因"(或罪魁祸首).

Ok, I think I've found the 'reason' (or culprit) for java scaling pixel values.

在 HTMLEditorKit 实现中,有一个处理 css 内容的 CSS 类.它有一个表格,其中包含每个受支持单位的缩放值.(从 CSS.class 中的第 2843 行开始)

In the HTMLEditorKit implementation, there is a CSS class that handles css stuff. It has a table with scaling values for each supported unit. (starting on line 2843 in CSS.class)

对于pt",它是一对一的映射.对于px",它是 1 到 1.3 的映射.甚至有评论说:

For "pt", it is 1-to-1 mapping. For "px", its 1-to-1.3 mapping. There is even a comment that says:

//1.3 不确定,通过实验确定.

// Not sure about 1.3, determined by experiementation.

实际上有两个映射表,其中第二个是在w3c模式开启的情况下使用的.它具有从第一个表向后的缩放模式:

There are actually two mapping tables, where the second one is used if the w3c mode is turned on. It has scaling modes that are backwards from the first table:

对于pt",它是 1-to-screenres/72dpi.对于px",它是一对一的映射.

For "pt", it is 1-to-screenres/72dpi. For "px", its 1-to-1 mapping.

我不确定是什么激活了 w3c 模式,但要注意这个问题.

I'm not sure what activates the w3c mode, but beware of the issue.

这篇关于Java HTML 渲染“pt"对比“px"尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆