Swing/W3CLengthUnits のバックアップ差分(No.1)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/W3CLengthUnits へ行く。
- 1 (2020-10-12 (月) 03:22:46)
- 2 (2020-12-07 (月) 14:47:09)
- 3 (2022-08-20 (土) 22:15:25)
- 4 (2023-04-14 (金) 15:01:56)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- category: swing folder: W3CLengthUnits title: JEditorPaneのHTMLレンダリングでw3c準拠の長さ単位を使用する tags: [JEditorPane, Html] 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`を継承しているため #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] * コメント [#comment] #comment #comment