高橋メソッド画像メーカー

http://konbu.s13.xrea.com/lib/presen/TakImg.jar
なんか経緯を憶えてないのですが、テキストを高橋メソッドに最適なサイズの画像に変換するプログラムをJavaで書きました。
以下のようなテキストファイルを渡すと、3つの画像ファイルができます。

高橋画像
めーかー
----
たぶん、画面に
ぴったりに
なって
ます?
----
おなか
すきました
----

できる画像はそのまんまこんなの。

CUIプログラムです。以下のように実行。

> java -jar TakImg.jar hoge.txt

テキストだけ渡すとdataって名前のディレクトリ掘ってそこに画像ファイルを保存します。

> java -jar TakImg.jar hoge.txt fuga

とかやるとfugaディレクトリに。

つーか、考えてみれば来週締切のレポートやる予定だった気がしたんだが。何やってんだ俺。
以下コード。jarアーカイブの中にも入ってますけど。

import javax.imageio.ImageIO;
import java.awt.GraphicsEnvironment;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.DisplayMode;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Font;
import java.awt.font.TextLayout;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.util.Vector;

public class TakImg {
  private Vector<String> takpages = new Vector<String>();
  private Dimension screensize = null;
  private static final GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();
  private static final GraphicsDevice gdevice = genv.getDefaultScreenDevice();
  private static final DisplayMode dmode = gdevice.getDisplayMode();
  private static final GraphicsConfiguration gconf = gdevice.getDefaultConfiguration();
  private static final Graphics2D localGraphics =
    genv.createGraphics(gconf.createCompatibleImage(dmode.getWidth(), dmode.getHeight()));
  private static final FontRenderContext localFRC = localGraphics.getFontRenderContext();

  public TakImg()
  {
    screensize = new Dimension(dmode.getWidth(), dmode.getHeight());
  }
  public BufferedImage createImage(int width, int height)
  {
    return gconf.createCompatibleImage(dmode.getWidth(), dmode.getHeight());
  }
  public Font getFitFont(Graphics2D g2, String page)
  {
    Font fitFont = Font.decode("default");
    float fontsize = (float)1.0;
    Rectangle2D areasize = null;
    String[] lines = page.split("\n", 1000);
    int lineCount = lines.length - 1;
    String limitWidthLine = null;
    Rectangle2D maxRect = null;
    for(String line : lines){
      if(line.intern()=="") break;
      TextLayout text = new TextLayout(line, fitFont, localFRC);
      Rectangle2D lineRect = text.getBounds();
      if(maxRect == null || lineRect.getWidth()>maxRect.getWidth()){
        limitWidthLine = line;
        maxRect = lineRect;
      }
    }

    float width, height;
    do{
      fontsize += 10.0;
      fitFont = fitFont.deriveFont(fontsize);
      TextLayout text = new TextLayout(limitWidthLine, fitFont, localFRC);
      width = text.getAdvance();
      height = text.getAscent() + text.getDescent();
    } while(width < screensize.width && height*lineCount < screensize.height);
    while(width > screensize.width || height*lineCount > screensize.height){
      fontsize -= 1.0;
      fitFont = fitFont.deriveFont(fontsize);
      TextLayout text = new TextLayout(limitWidthLine, fitFont, localFRC);
      width = text.getAdvance();
      height = text.getAscent() + text.getDescent();
    }
    return fitFont;
  }
  public void addPage(String page)
  {
    takpages.addElement(page);
  }
  public void paintPages(Graphics2D g2)
  {
    for(String page : takpages){
      paintPage(g2, page);
    }
  }
  public void paintPage(Graphics2D g2, String page)
  {
    Font fitFont = getFitFont(g2, page);
    float y = (float)0.0;
    String[] lines = page.split("\n", 1000);
    g2.setColor(Color.white);
    g2.fillRect(0, 0, screensize.width, screensize.height);
    g2.setColor(Color.black);
    for(String line : lines){
      if(line.intern()=="") break;
      TextLayout text = new TextLayout(line, fitFont, localFRC);
      y += text.getAscent();
      text.draw(g2, (float)0.0, y);
      y += text.getDescent();
    }
  }
  public void savePages(String savepath) throws IOException
  {
      int i = 0;
      for(String page : takpages){
        System.out.println(page);
        BufferedImage pageimg = createImage(screensize.width, screensize.height);
        Graphics2D g2 = pageimg.createGraphics();
        paintPage(g2, page);
        String pagepath = String.format(savepath+"/data%05d.png", i++);
        File pagefile = new File(pagepath);
        ImageIO.write(pageimg, "png", pagefile);
      }
  }
  private static boolean isDirectory(String path)
  {
    File file = new File(path);
    if(!file.exists()){
      if(file.mkdir()){
        return true;
      }
    }
    else if(file.isDirectory()){
      return true;
    }
    return false;
  }
  public void run(String[] args)
  {
    String savepath = null;
    if(args.length == 1){
      savepath = "data";
    }
    else if(args.length == 2){
      savepath = args[1];
    }

    if(savepath == null || !isDirectory(savepath)){
      return;
    }

    String page = "";
    String line = null;
    try{
      BufferedReader takfile =
        new BufferedReader(new FileReader(args[0]));
      while((line = takfile.readLine())!=null){
        if(line.intern()=="----"){
          addPage(page);
          page = "";
        }
        else{
          page += (line + "\r\n");
        }
      }
    }
    catch(FileNotFoundException fex){
      System.err.println(args[0]+"は存在しません");
    }
    catch(IOException ex){
      System.err.println(args[0]+"の読み込みに失敗しました");
    }
    finally{
      if(page != ""){
        addPage(page);
      }
    }
    try{
      savePages(savepath);
    }
    catch(IOException ex){
      System.err.println("画像書き込み失敗");
    }
  }

  public static void main(String[] args)
  {
    TakImg takImg = new TakImg();
    takImg.run(args);
    System.exit(0);
  }
}

えーと、なんだろう。今ざっと見かえした感じだと、これ全部staticいんじゃね、みたいな。

うーん、なんかこう脳味噌がテロンテロンになってる気分で、自分のコードですらよくわかんなくなってきた。っていうかそうだ、俺は今きっと眠いんだな。

test