Swing/SurrogatePair のバックアップ差分(No.31)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/SurrogatePair へ行く。
- 1 (2012-05-14 (月) 11:54:26)
- 2 (2012-05-14 (月) 15:01:46)
- 3 (2012-05-15 (火) 11:17:30)
- 4 (2012-06-06 (水) 18:42:16)
- 5 (2012-06-12 (火) 19:09:04)
- 6 (2012-06-25 (月) 18:37:16)
- 7 (2012-06-25 (月) 20:42:16)
- 8 (2012-09-26 (水) 17:11:57)
- 9 (2012-11-15 (木) 16:55:12)
- 10 (2012-11-16 (金) 18:26:03)
- 11 (2012-12-11 (火) 20:55:41)
- 12 (2013-01-29 (火) 23:48:27)
- 13 (2013-05-27 (月) 17:03:23)
- 14 (2013-05-27 (月) 21:55:45)
- 15 (2014-09-18 (木) 02:36:05)
- 16 (2014-10-18 (土) 13:03:17)
- 17 (2014-11-01 (土) 00:46:09)
- 18 (2014-12-20 (土) 13:29:12)
- 19 (2014-12-22 (月) 16:47:22)
- 20 (2015-11-26 (木) 04:20:20)
- 21 (2017-01-05 (木) 14:19:26)
- 22 (2017-04-18 (火) 21:07:09)
- 23 (2017-04-19 (水) 15:39:47)
- 24 (2017-11-02 (木) 15:34:40)
- 25 (2017-12-07 (木) 11:48:46)
- 26 (2017-12-07 (木) 14:09:21)
- 27 (2018-01-15 (月) 18:56:18)
- 28 (2018-04-04 (水) 15:27:26)
- 29 (2018-10-12 (金) 17:44:22)
- 30 (2018-12-17 (月) 16:24:00)
- 31 (2020-11-12 (木) 09:44:04)
- 32 (2022-08-05 (金) 16:04:11)
- 33 (2022-08-20 (土) 22:15:25)
- 34 (2022-10-04 (火) 15:57:26)
- 追加された行はこの色です。
- 削除された行はこの色です。
---
category: swing
folder: SurrogatePair
title: JTextComponentでサロゲートペアのテスト
tags: [JEditorPane, JTextComponent, Unicode, Fixed]
author: aterai
pubdate: 2012-05-14T11:54:26+09:00
description: JEditorPaneなどで数値文字参照やUnicodeエスケープを使ってサロゲートペアのテストをします。
image: https://lh5.googleusercontent.com/-BY6N3kDDUG8/T7ByWIn0mgI/AAAAAAAABMo/4dpU-rm8zAQ/s800/SurrogatePair.png
---
* 概要 [#summary]
`JEditorPane`などで数値文字参照や`Unicode`エスケープを使ってサロゲートペアのテストをします。
#download(https://lh5.googleusercontent.com/-BY6N3kDDUG8/T7ByWIn0mgI/AAAAAAAABMo/4dpU-rm8zAQ/s800/SurrogatePair.png)
* サンプルコード [#sourcecode]
#code(link){{
final URL url = getClass().getResource("SurrogatePair.html");
try (Reader reader = new InputStreamReader(url.openStream(), StandardCharsets.UTF_8)) {
editor1.read(reader , "html");
} catch (Exception ex) {
editor1.setText(
"<html><p>(��) (𦹀)<br />(��) (𠮟)</p></html>");
}
JEditorPane editor2 = new JEditorPane();
//editor2.setFont(new Font("IPAexGothic", Font.PLAIN, 24));
editor2.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
editor2.setText("(\uD85B\uDE40) (\u26E40)\n(\uD842\uDF9F) (\u20B9F)");
}}
* 解説 [#explanation]
以下、サロゲートペア対応フォントを使えるようにしてテストしています。`Java Web Start`で起動した場合、このサンプルの`browse`ボタンで`jar`ファイル内の`SurrogatePair.html`を表示することはできません。
- 上: 数値文字参照(`Numeric character reference`)
-- [https://bugs.openjdk.java.net/browse/JDK-6836089 Bug ID: 6836089 Swing HTML parser can't properly decode codepoints outside the Unicode Plane 0 into a surrogate pair]で修正されたので、`𦹀`などでも正常に文字が表示される(上記のスクリーンショットのように文字化けしない)
-- `JEditorPane(HTMLEditorKit)`の場合
--- `JEditorPane` + `��`: `OK`
--- `JEditorPane` + `𦹀`: `%%NG%%` `OK`
-- ブラウザ(試したのは`IE`, `FireFox`, `Chrome`, `Opera`)の場合
--- `Browser` + `��`: `NG`
--- `Browser` + `𦹀`: `OK`
- 下: `Unicode`エスケープ(`Unicode escapes`)
--- `JEditorPane` + `\uD85B\uDE40`: `OK`
--- `JEditorPane` + `\u26E40`: `NG`
----
- `JTextComponent`とブラウザでサロゲートペアの表現が異なるため、これらの文字をどちらの環境でも正しく表示したい場合は、数値文字参照や`Unicode`エスケープは使用せず、ソースコードなどを`UTF-8`にしてそのまま𦹀や𠮟と書く(メモ帳などの対応済みエディタで)ほうが良さそう
- `Windows 7`, `JDK 1.7.0_02`の環境では、`JTextComponent`からメモ帳などにサロゲートペアの文字をコピーペーストは可能だが、逆にメモ帳やブラウザから`JTextComponent`にサロゲートペアの文字をコピーペーストは不可
- `JTextComponent`(`Java 1.7.0`)は、異体字セレクタに対応してない
-- フォントを`IVS`に対応している`IPAmj明朝`などに変更し、異体字セレクタ付き文字列のある`UTF-8`のテキストを`JTextArea`などに読み込んでも異体字セレクタが〓になる
-- 数値文字参照、`Unicode`エスケープを使う方法でも〓になる
-- [http://d.hatena.ne.jp/terazzo/20110115/1295047469 Java IVSの異体字を元の字と同一視して比較する - terazzoの日記]のように、`VS`を`UTF-16`に(例えば`U+E0101`を`\uDB40\uDD01`に)しても、`JTextComponent`では駄目
----
- 参考メモ: [http://d.hatena.ne.jp/akane_neko/20121115/1352932112 MS IVSアドインでDTPにはどんな影響があるのか - ちくちく日記]
-- `Adobe-Japan1 collection`: `小塚明朝 Pr6N`など
-- `Hanyo-Denshi collection`: `IPAmj明朝`など
// %%`小塚明朝 Pr6N`(メモ: `Pr6N`の`N`は`JIS2004`対応の意味で、`IVS`対応とは関係ないらしい)%%
----
- %%`Windows 10 64bit`、`JDK 1.8.0_112`、`小塚明朝 Pr6N R`(`KozMinPr6N-Regular.otf`)で、`Font.createFont(...)`を使って`Font`を作成すると`IllegalArgumentException`が発生する場合がある%%
-- %%例外が発生しなくても、文字は正常に表示されない%%
-- [https://bugs.openjdk.java.net/browse/JDK-5092191 JDK-5092191 RFE: CFF/Type2 embedded fonts not supported with Font.createFont() - Java Bug System]
-- [https://bugs.openjdk.java.net/browse/JDK-8074562 JDK-8074562 CID keyed OpenType fonts are not supported by T2K - Java Bug System]
-- [https://bugs.openjdk.java.net/browse/JDK-8168288 JDK-8168288 Dubious FontMetrics values from NullFontScaler - Java Bug System]
--- `Java 9`、`8u152`で修正された
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Length must be >= 0.
at javax.swing.text.GlyphPainter2.getBoundedPosition(GlyphPainter2.java: 205)
#code{{
import java.awt.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class OTFTest {
public JComponent makeUI() {
JTextArea textArea = new JTextArea("1234567890\n \uD85B\uDE40");
String str = "file:///C:/Program Files (x86)/Adobe/Acrobat 2015/Resource/CIDFont/KozMinPr6N-Regular.otf";
//String str = "file:///C:/Program Files (x86)/Adobe/Reader 11.0/Resource/CIDFont/KozMinPr6N-Regular.otf";
//String str = "file:///C:/Windows/Fonts/meiryo.ttc";
//String str = "file:///C:/Windows/Fonts/ipaexg.ttf";
//String str = "file:///C:/Windows/Fonts/A-OTF-ShinGoPro-Regular.otf";
try (InputStream is = new URL(str).openStream()) {
//textArea.setFont(Font.createFont(Font.TRUETYPE_FONT, is));
textArea.setFont(Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(32f));
} catch (IOException | FontFormatException ex) {
ex.printStackTrace();
}
JPanel p = new JPanel(new BorderLayout());
p.add(new JScrollPane(textArea));
return p;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new OTFTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
}}
* 参考リンク [#reference]
- [http://mindprod.com/jgloss/opentype.html#JAVASUPPORT Java Font Support - OpenType : Java Glossary]
- [https://docs.oracle.com/javase/jp/8/docs/api/java/awt/font/OpenType.html OpenType (Java Platform SE 8)]
- [https://stackoverflow.com/questions/22605666/java-access-files-in-jar-causes-java-nio-file-filesystemnotfoundexception Java access files in jar causes java.nio.file.FileSystemNotFoundException - Stack Overflow]
* コメント [#comment]
#comment
- 結合文字(`À`、`が`)も`JTextComponent`は未対応。%%ブラウザだと`Chrome`は対応されているが、他は部分的な対応になっている?%% -- &user(aterai); &new{2012-05-15 (火) 11:17:30};
- `Windows 7` + `JDK 1.7.0`で`OTF`フォントは使えない??? -- &user(aterai); &new{2012-06-06 (水) 19:07:01};
- メモ: [https://bugs.openjdk.java.net/browse/JDK-6836089 Bug ID: 6836089 Swing HTML parser can't properly decode codepoints outside the Unicode Plane 0 into a surrogate pair] -- &user(aterai); &new{2012-06-25 (月) 18:37:16};
-- via: [http://mail.openjdk.java.net/pipermail/swing-dev/2012-June/002145.html <Swing Dev> <Swind Dev> <7u6> Review request for 6836089: Swing HTML parser can't properly decode codepoints outside the Unicode Plane 0 into a surrogate pair]
-- 確かに`JDK 1.6.0_33`では、`𦹀`などの数値文字参照が`JEditorPane`で正常に表示されている。 -- &user(aterai); &new{2012-06-25 (月) 20:42:16};
#comment