概要

JEditorPaneHTMLレンダリングでフォントサイズなどに指定された絶対単位をw3c準拠の長さで表示します。

サンプルコード

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()));
View in GitHub: Java, Kotlin

解説

@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));
  }
  // ...
}

参考リンク

コメント