Swing/SurrogatePair のバックアップ(No.12)
- バックアップ一覧
 - 差分 を表示
 - 現在との差分 を表示
 - 現在との差分 - 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)
 - 35 (2025-01-03 (金) 08:57:02)
 - 36 (2025-01-03 (金) 09:01:23)
 - 37 (2025-01-03 (金) 09:02:38)
 - 38 (2025-01-03 (金) 09:03:21)
 - 39 (2025-01-03 (金) 09:04:02)
 - 40 (2025-06-19 (木) 12:41:37)
 - 41 (2025-06-19 (木) 12:43:47)
 
 
TITLE:JTextComponentでサロゲートペアのテスト
Posted by aterai at 2012-05-14
JTextComponentでサロゲートペアのテスト
`JEditorPaneなどで数値文字参照やUnicode`エスケープを使ってサロゲートペアのテストをします。
- &jnlp;
 - &jar;
 - &zip;
 
サンプルコード
final URL url = getClass().getResource("SurrogatePair.html");
try{
  editor1.read(new InputStreamReader(url.openStream(), "UTF-8"), "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)");
View in GitHub: Java, Kotlin解説
以下、サロゲートペア対応フォントを使えるようにしてテストしています。`Java Web Startで起動した場合、このサンプルの"browse"ボタンでjarファイル内のSurrogatePair.html`を表示することはできません。
- 上: 数値文字参照(`
Numeric character reference`)- `
JEditorPane(HTMLEditorKit)`の場合JEditorPane OK: �� JEditorPane NG: 𦹀
 
 - `
 
- ブラウザ(試したのはIE, FireFox, Chrome, Opera)の場合
Browser NG: �� Browser OK: 𦹀
 
- 下: Unicodeエスケープ(`
Unicode escapes`)JEditorPane OK: \uD85B\uDE40 JEditorPane NG: \u26E40
 
`JTextComponentとブラウザでサロゲートペアの表現が異なるようなので、これらの文字をどちらの環境でも正しく表示したい場合は、数値文字参照やUnicodeエスケープは使用せず、ソースコードなどをUTF-8`にしてそのまま𦹀や𠮟と書く*1のがよさそうです。
`Windows 7, JDK 1.7.0_02の環境では、JTextComponentからメモ帳などにサロゲートペアの文字をコピーペーストは可能ですが、逆にメモ帳やブラウザからJTextComponent`にサロゲートペアの文字をコピーペーストすることができないようです。
`JTextComponent(Java 1.7.0`)は、異体字セレクタに対応していない?
- フォントを`
IVSなどに対応している小塚明朝 Pr6N*2 や、IPAmj明朝などに変更して、数値文字参照、Unicodeエスケープを使っても下駄になったり、ソースコードをUTF-8にしても、以下のようなIllegalArgumentException`が発生する場合があるようです。- `
IllegalArgumentExceptionと異体字セレクタは関係なくて、Windows環境のJavaと小塚明朝 Pr6N R(OTF`フォントだから?)と相性が悪いだけ? 
 - `
 
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Length must be >= 0. at javax.swing.text.GlyphPainter2.getBoundedPosition(GlyphPainter2.java: 205)
- 参考メモ: MS IVSアドインでDTPにはどんな影響があるのか - ちくちく日記
- `
Adobe-Japan1 collection:小塚明朝 Pr6N`など - `
Hanyo-Denshi collection:IPAmj明朝`など 
 - `
 
- `
Font.createFont(...)で、C:\Program Files (x86)\Adobe\Acrobat 9.0\Resource\CIDFont\KozMinPr6N-Regular.otf`からフォントを作ると??? 
import java.awt.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class OTFTest {
  public JComponent makeUI() {
    JTextArea textArea = new JTextArea("1234567890");
    String str = "file:///C:/Program Files (x86)/Adobe/Acrobat 9.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()) {
      Font font = (Font.createFont(Font.TRUETYPE_FONT, is)).deriveFont(32.0f);
      textArea.setFont(font);
      is.close();
    }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);
  }
}
参考リンク
コメント
- 結合文字(`
À、が)もJTextComponent`は未対応。ブラウザだとChromeは対応されているが、他は部分的な対応になっている?-- aterai - `
Windows 7+JDK 1.7.0でOTF`フォントは使えない??? -- aterai - メモ: Bug ID: 6836089 Swing HTML parser can't properly decode codepoints outside the Unicode Plane 0 into a surrogate pair -- aterai 
- via: <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`で正常に表示されている。 -- aterai 
 
