• 追加された行はこの色です。
  • 削除された行はこの色です。
---
category: swing
folder: W3CLengthUnits
title: JEditorPaneのHTMLレンダリングでw3c準拠の長さ単位を使用する
tags: [JEditorPane, Html, StyleSheet, CSS]
author: aterai
pubdate: 2020-10-12T03:21:41+09:00
description: JEditorPaneのHTMLレンダリングでフォントサイズなどに指定された絶対単位をw3c準拠の長さで表示します。
image: https://drive.google.com/uc?id=1j_CUQjXi2KjzpbM-AlCQME1ZInedQArw
---
* 概要 [#summary]
`JEditorPane`の`HTML`レンダリングでフォントサイズなどに指定された絶対単位を`w3c`準拠の長さで表示します。

#download(https://drive.google.com/uc?id=1j_CUQjXi2KjzpbM-AlCQME1ZInedQArw)

* サンプルコード [#sourcecode]
#code(link){{
JEditorPane editor = new JEditorPane("text/html", "");

HTMLEditorKit htmlEditorKit = (HTMLEditorKit) editor.getEditorKit();
StyleSheet styles = htmlEditorKit.getStyleSheet();
styles.addRule(".number {font-size: 14}");
styles.addRule(".pt {font-size: 14pt}");
styles.addRule(".em {font-size: 1.2em}");
styles.addRule(".percentage {font-size: 120%}");

String html = "<html><h3>h3 {font-size: medium}</h3>"
    + "<h3 class='number'>h3 {font-size: 14}</h3>"
    + "<h3 class='pt'>h3 {font-size: 14pt}</h3>"
    + "<h3 class='em'>h3 {font-size: 1.2em}</h3>"
    + "<h3 class='percentage'>h3 {font-size: 120%}</h3>";
editor.setText(html);

JCheckBox check = new JCheckBox("JEditorPane.W3C_LENGTH_UNITS");
check.addActionListener(e -> editor.putClientProperty(
    JEditorPane.W3C_LENGTH_UNITS, ((JCheckBox) e.getSource()).isSelected()));
}}

* 解説 [#explanation]
- `JEditorPane.W3C_LENGTH_UNITS`: `false`
-- デフォルト
- `JEditorPane.W3C_LENGTH_UNITS`: `true`
-- `w3c`準拠の長さ単位を使用する
-- `CSS.LengthUnit`クラスで以下のようにマッピングされているため、例えばポイントで`font-size`が指定されている場合、`JEditorPane.W3C_LENGTH_UNITS`を`true`に設定すると一般的な`Windows`環境では`96/72`倍文字サイズが拡大する
-- 単位指定なしや相対単位の場合は変化しない
-- このサンプルの`h3`要素で相対単位の`em`や`%`指定の`font-size`が変化するのは親の`body`要素の`14pt`を継承しているため
-- 以下のコードでは高`DPI`で文字サイズが大きくなりすぎるので[https://github.com/openjdk/jdk/pull/1628 8231286: HTML font size too large with high-DPI scaling and W3C_UNIT_LENGTHS by prsadhuk · Pull Request #1628 · openjdk/jdk]で修正される
-- [https://bugs.openjdk.org/browse/JDK-8231286 &#91;JDK-8231286&#93; HTML font size too large with high-DPI scaling and W3C_LENGTH_UNITS - Java Bug System]

#code{{
@see javax/swing/text/html/CSS.java
static class LengthUnit implements Serializable {
  static Hashtable<String, Float> lengthMapping = new Hashtable<String, Float>(6);
  static Hashtable<String, Float> w3cLengthMapping = new Hashtable<String, Float>(6);
  static {
    lengthMapping.put("pt", Float.valueOf(1f));
    // Not sure about 1.3, determined by experiementation.
    lengthMapping.put("px", Float.valueOf(1.3f));
    lengthMapping.put("mm", Float.valueOf(2.83464f));
    lengthMapping.put("cm", Float.valueOf(28.3464f));
    lengthMapping.put("pc", Float.valueOf(12f));
    lengthMapping.put("in", Float.valueOf(72f));
    int res = 72;
    try {
      res = Toolkit.getDefaultToolkit().getScreenResolution();
    } catch (HeadlessException e) {
    }
    // mapping according to the CSS2 spec
    w3cLengthMapping.put("pt", Float.valueOf(res / 72f));
    w3cLengthMapping.put("px", Float.valueOf(1f));
    w3cLengthMapping.put("mm", Float.valueOf(res / 25.4f));
    w3cLengthMapping.put("cm", Float.valueOf(res / 2.54f));
    w3cLengthMapping.put("pc", Float.valueOf(res / 6f));
    w3cLengthMapping.put("in", Float.valueOf((float) res));
  }
  // ...
}
}}

* 参考リンク [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JEditorPane.html#W3C_LENGTH_UNITS JEditorPane.W3C_LENGTH_UNITS (Java Platform SE 8)]
- [https://www.w3.org/TR/CSS21/syndata.html#length-units 4.3.2 Lengths - Syntax and basic data types - Cascading Style Sheets Level 2 Revision 1 (CSS 2.1) Specification]
- [https://github.com/openjdk/jdk/pull/1628 8231286: HTML font size too large with high-DPI scaling and W3C_UNIT_LENGTHS by prsadhuk · Pull Request #1628 · openjdk/jdk]
-- [https://bugs.openjdk.org/browse/JDK-8231286 &#91;JDK-8231286&#93; HTML font size too large with high-DPI scaling and W3C_UNIT_LENGTHS - Java Bug System]

* コメント [#comment]
#comment
#comment