• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:Fontのアウトラインから輪郭を取得する
#navi(../)
#tags(Font, PathIterator, Icon, AffineTransform, Shape, JLabel)
RIGHT:Posted by &author(aterai); at 2013-09-16
* Fontのアウトラインから輪郭を取得する [#g07a2f18]
---
title: Fontのアウトラインから輪郭を取得する
tags: [Font, PathIterator, Icon, AffineTransform, Shape, JLabel]
author: aterai
pubdate: 2013-09-16T00:01:42+09:00
description: Fontから取得した字形の輪郭を抽出し、縁取りや内部の塗り潰しなどを行います。
---
* 概要 [#g07a2f18]
`Font`から取得した字形の輪郭を抽出し、縁取りや内部の塗り潰しなどを行います。このサンプルは、[http://stackoverflow.com/questions/18686199/fill-unicode-characters-in-labels java - 'Fill' Unicode characters in labels - Stack Overflow]に投稿されているコードを参考にしています。

- &jnlp;
- &jar;
- &zip;
#download(https://lh5.googleusercontent.com/-kzMG9iEHFz4/UjWgNdHCh1I/AAAAAAAAB2A/0gpKBcNqz44/s800/FontSilhouette.png)

#ref(https://lh5.googleusercontent.com/-kzMG9iEHFz4/UjWgNdHCh1I/AAAAAAAAB2A/0gpKBcNqz44/s800/FontSilhouette.png)

** サンプルコード [#u80cc8b3]
* サンプルコード [#u80cc8b3]
#code(link){{
public static Area getOuterShape(Shape shape) {
  Area area = new Area();
  double[] coords = new double[6];
  PathIterator pi = shape.getPathIterator(null);
  Path2D.Double path = null;
  while(!pi.isDone()) {
    int pathSegmentType = pi.currentSegment(coords);
    if(pathSegmentType == PathIterator.SEG_MOVETO) {
      if(area.isEmpty() || !area.contains(coords[0], coords[1])) {
        path = new Path2D.Double();
        path.moveTo(coords[0], coords[1]);
      }
    }else if(path==null) {
      pi.next();
      continue;
    }else if(pathSegmentType == PathIterator.SEG_LINETO) {
      path.lineTo(coords[0], coords[1]);
    }else if(pathSegmentType == PathIterator.SEG_QUADTO) {
      path.quadTo(coords[0], coords[1], coords[2], coords[3]);
    }else if(pathSegmentType == PathIterator.SEG_CUBICTO) {
      path.curveTo(coords[0], coords[1], coords[2],
                   coords[3], coords[4], coords[5]);
    }else if(pathSegmentType == PathIterator.SEG_CLOSE) {
      path.closePath();
      area.add(new Area(path));
      path = null;
    }else{
      System.err.println("Unexpected value! " + pathSegmentType);
    }
    pi.next();
  }
  return area;
}
}}

** 解説 [#k5b39527]
* 解説 [#k5b39527]
上記のサンプルの下二行は、チェスの駒の字形から輪郭を取得し、それを使って縁取り、内部の塗り潰しを行う`Icon`を`JLabel`に配置して表示しています。

字形(`Shape`)の輪郭は、`Shape#getPathIterator(...)`で字形からパスを取得し、開始点が一番外側にあるパスの集合を`Path2D`に変換、`Area`に追加することで作成しています。

** 参考リンク [#l4dba66c]
* 参考リンク [#l4dba66c]
- [http://stackoverflow.com/questions/18686199/fill-unicode-characters-in-labels java - 'Fill' Unicode characters in labels - Stack Overflow]

** コメント [#f643624a]
* コメント [#f643624a]
#comment
- `src.zip`などがダウンロードできない状態(上げ忘れ?)だったのを修正。 -- &user(aterai); &new{2013-12-06 (金) 16:37:48};

#comment