EPSGraphics.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
00003  * All rights reserved. This program is made available under the terms of the
00004  * Eclipse Public License v1.0 which accompanies this distribution, and is
00005  * available at http://www.eclipse.org/legal/epl-v10.html
00006  * Contributors:
00007  * General Robotix Inc.
00008  * National Institute of Advanced Industrial Science and Technology (AIST) 
00009  */
00010 package com.generalrobotix.ui.view.graph;
00011 
00012 import java.awt.*;
00013 import java.io.*;
00014 import java.util.*;
00015 import java.awt.image.ImageObserver;
00016 import java.text.AttributedCharacterIterator;
00017 
00024 public class EPSGraphics extends Graphics {
00025 
00026     // -----------------------------------------------------------------
00027     // 定数
00028     // ヘッダ等
00029     private static final String HEADER = "%!PS-Adobe-3.0 EPSF-3.0";
00030     private static final String BOUNDING_BOX = "%%BoundingBox:";
00031     private static final String DEF = "/";
00032     private static final String BIND_DEF = " bind def";
00033     private static final String EOF = "%%EOF";
00034     // 色オペレータ
00035     private static final String SET_COLOR_WHITE     = "setColorWhite";
00036     private static final String SET_COLOR_LIGHTGRAY = "setColorLightGray";
00037     private static final String SET_COLOR_GRAY      = "setColorGray";
00038     private static final String SET_COLOR_DARKGRAY  = "setColorDarkGray";
00039     private static final String SET_COLOR_BLACK     = "setColorBlack";
00040     private static final String SET_COLOR_OTHERS    = "setColorOthers";
00041     private static final String SET_COLOR_GREEN     = "setColorGreen";
00042     private static final String SET_COLOR_YELLOW    = "setColorYellow";
00043     private static final String SET_COLOR_PINK      = "setColorPink";
00044     private static final String SET_COLOR_CYAN      = "setColorCyan";
00045     private static final String SET_COLOR_MAGENTA   = "setColorMagenta";
00046     private static final String SET_COLOR_RED       = "setColorRed";
00047     private static final String SET_COLOR_ORANGE    = "setColorOrange";
00048     private static final String SET_COLOR_BLUE      = "setColorBlue";
00049     // 色オペレータ定義(白黒用)
00050     private static final String DEF_COLOR_WHITE
00051         = DEF + SET_COLOR_WHITE     + " {0.3 setlinewidth 0 setgray [] 0 setdash}" + BIND_DEF;
00052     private static final String DEF_COLOR_LIGHTGRAY
00053         = DEF + SET_COLOR_LIGHTGRAY + " {0.3 setlinewidth 0.2 setgray [] 0 setdash}" + BIND_DEF;
00054     private static final String DEF_COLOR_GRAY
00055         = DEF + SET_COLOR_GRAY      + " {0.3 setlinewidth 0.5 setgray [] 0 setdash}" + BIND_DEF;
00056     private static final String DEF_COLOR_DARKGRAY
00057         = DEF + SET_COLOR_DARKGRAY  + " {0.3 setlinewidth 0.7 setgray [] 0 setdash}" + BIND_DEF;
00058     private static final String DEF_COLOR_BLACK
00059         = DEF + SET_COLOR_BLACK     + " {0.3 setlinewidth 1 setgray [] 0 setdash}" + BIND_DEF;
00060     private static final String DEF_COLOR_OTHERS
00061         = DEF + SET_COLOR_OTHERS    + " {0.3 setlinewidth 0 setgray [] 0 setdash}" + BIND_DEF;
00062     private static final String DEF_COLOR_GREEN
00063         = DEF + SET_COLOR_GREEN     + " {0.3 setlinewidth 0 setgray [] 0 setdash}" + BIND_DEF;
00064     private static final String DEF_COLOR_YELLOW
00065         = DEF + SET_COLOR_YELLOW    + " {0.3 setlinewidth 0.4 setgray [] 0 setdash}" + BIND_DEF;
00066     private static final String DEF_COLOR_PINK
00067         = DEF + SET_COLOR_PINK      + " {0.3 setlinewidth 0.7 setgray [] 0 setdash}" + BIND_DEF;
00068     private static final String DEF_COLOR_CYAN
00069         = DEF + SET_COLOR_CYAN      + " {0.6 setlinewidth 0.7 setgray [] 0 setdash}" + BIND_DEF;
00070     private static final String DEF_COLOR_MAGENTA
00071         = DEF + SET_COLOR_MAGENTA   + " {0.9 setlinewidth 0 setgray [] 0 setdash}" + BIND_DEF;
00072     private static final String DEF_COLOR_RED
00073         = DEF + SET_COLOR_RED       + " {0.9 setlinewidth 0.7 setgray [] 0 setdash}" + BIND_DEF;
00074     private static final String DEF_COLOR_ORANGE
00075         = DEF + SET_COLOR_ORANGE    + " {0.9 setlinewidth 0 setgray [6 2 2 2] 0 setdash}" + BIND_DEF;
00076     private static final String DEF_COLOR_BLUE
00077         = DEF + SET_COLOR_BLUE      + " {0.9 setlinewidth 0.7 setgray [6 2 2 2] 0 setdash}" + BIND_DEF;
00078 
00079     // 描画オペレータ
00080     private static final String DRAW_LINE     = "drawLine";
00081     private static final String SET_FONT      = "setFont";
00082     private static final String DRAW_STRING   = "drawString";
00083     private static final String SET_CLIP      = "setClip";
00084     private static final String SET_CLIP_NULL = "setClipNull";
00085     private static final String NEWPATH = "N";
00086     private static final String MOVETO = "M";
00087     private static final String LINETO = "L";
00088     private static final String STROKE = "S";
00089     // 描画オペレータ定義
00090     private static final String DEF_DRAW_LINE
00091         = DEF + DRAW_LINE     + " {newpath moveto lineto stroke}" + BIND_DEF;
00092     private static final String DEF_SET_FONT
00093         = DEF + SET_FONT      + " {exch findfont exch scalefont setfont}" + BIND_DEF;
00094     private static final String DEF_DRAW_STRING
00095         = DEF + DRAW_STRING   + " {moveto show}" + BIND_DEF;
00096     private static final String DEF_SET_CLIP
00097         = DEF + SET_CLIP
00098         + " {gsave newpath 3 index 3 index moveto dup 0 exch"
00099         + " rlineto exch 0 rlineto 0 exch sub 0 exch"
00100         + " rlineto pop pop closepath clip}"
00101         + BIND_DEF;
00102     private static final String DEF_SET_CLIP_NULL
00103         = DEF + SET_CLIP_NULL + " {grestore}" + BIND_DEF;
00104     private static final String DEF_NEWPATH = DEF + NEWPATH + " {newpath}" + BIND_DEF;
00105     private static final String DEF_MOVETO  = DEF + MOVETO  + " {moveto}"  + BIND_DEF;
00106     private static final String DEF_LINETO  = DEF + LINETO  + " {lineto}"  + BIND_DEF;
00107     private static final String DEF_STROKE  = DEF + STROKE  + " {stroke}"  + BIND_DEF;
00108     // 紙サイズ
00109     private static final int PAGE_HEIGHT = 792;
00110     //private static final int PAGE_WIDTH = 612;
00111 
00112     // -----------------------------------------------------------------
00113     // インスタンス変数
00114     private PrintWriter pw;         // プリントライタ
00115     private ArrayList<Color> colorList_;   // 色一覧
00116     private ArrayList<String> colorOps_;    // 色オペレータ一覧
00117     private double scale_;          // スケール
00118     private boolean inPath_;        // パス継続中フラグ
00119     private int prevX_;             // 前回X座標
00120     private int prevY_;             // 前回Y座標
00121     private int xOfs_;              // X座標オフセット
00122     private int yOfs_;              // Y座標オフセット
00123 
00124     // -----------------------------------------------------------------
00125     // コンストラクタ
00136     public EPSGraphics(
00137         Writer writer,
00138         int top,
00139         int left,
00140         int width,
00141         int height,
00142         boolean color
00143     ) {
00144         super();
00145         // 色設定
00146         colorList_ = new ArrayList<Color>();
00147         colorList_.add(Color.white);
00148         colorList_.add(Color.lightGray);
00149         colorList_.add(Color.gray);
00150         colorList_.add(Color.darkGray);
00151         colorList_.add(Color.black);
00152         colorList_.add(Color.green);
00153         colorList_.add(Color.yellow);
00154         colorList_.add(Color.pink);
00155         colorList_.add(Color.cyan);
00156         colorList_.add(Color.magenta);
00157         colorList_.add(Color.red);
00158         colorList_.add(Color.orange);
00159         colorList_.add(Color.blue);
00160         colorOps_ = new ArrayList<String>();
00161         colorOps_.add(SET_COLOR_WHITE);
00162         colorOps_.add(SET_COLOR_LIGHTGRAY);
00163         colorOps_.add(SET_COLOR_GRAY);
00164         colorOps_.add(SET_COLOR_DARKGRAY);
00165         colorOps_.add(SET_COLOR_BLACK);
00166         colorOps_.add(SET_COLOR_GREEN);
00167         colorOps_.add(SET_COLOR_YELLOW);
00168         colorOps_.add(SET_COLOR_PINK);
00169         colorOps_.add(SET_COLOR_CYAN);
00170         colorOps_.add(SET_COLOR_MAGENTA);
00171         colorOps_.add(SET_COLOR_RED);
00172         colorOps_.add(SET_COLOR_ORANGE);
00173         colorOps_.add(SET_COLOR_BLUE);
00174         // その他の設定
00175         scale_ = 1;         // スケールクリア
00176         inPath_ = false;    // パス継続中でない
00177         xOfs_ = 0;          // Xオフセットクリア
00178         yOfs_ = 0;          // Yオフセットクリア
00179         // ヘッダ出力
00180         pw = new PrintWriter(writer);   // プリントライタオープン
00181         _writeHeader(top, left, width, height, color);   // ヘッダ出力
00182     }
00183 
00184     // -----------------------------------------------------------------
00185     // メソッド
00191     public void setXOffset(int xofs) {
00192         xOfs_ = xofs;
00193     }
00194 
00200     public void setYOffset(int yofs) {
00201         yOfs_ = yofs;
00202     }
00203 
00209     public void setScale(double scale) {
00210         _stroke();
00211         scale_ = scale;
00212     }
00213 
00219     public void setLineWidth(double width) {
00220         _stroke();
00221         pw.println("" + width + " setlinewidth");
00222     }
00223 
00228     public void finishOutput() {
00229         _stroke();           // パスを描画
00230         pw.println(EOF);    // EOFマーク出力
00231         pw.close();         // プリントライタクローズ
00232     }
00233 
00234     // -----------------------------------------------------------------
00235     // Graphicsのメソッドオーバーライド
00241     public void setColor(Color c) {
00242         _stroke();
00243         int ind = colorList_.indexOf(c);
00244         String col;
00245         if (ind >= 0) {
00246             col = (String)colorOps_.get(ind);
00247         } else {
00248             col = SET_COLOR_OTHERS;
00249         }
00250         pw.println(col);
00251     }
00252 
00261     public void fillRect(int x, int y, int width, int height) {
00262         // 無処理
00263     }
00264 
00273     public void drawLine(int x1, int y1, int x2, int y2) {
00274         StringBuffer sb;
00275         if (inPath_) {   // パス継続中?
00276             if (prevX_ == x1 && prevY_ == y1) { // 始点が前回の終点と一致?
00277                 // x2 y2 lineto
00278                 sb = new StringBuffer();
00279                 sb.append(x2 / scale_ + xOfs_);
00280                 sb.append(' '); sb.append(PAGE_HEIGHT - y2 / scale_ - yOfs_);
00281                 sb.append(' '); sb.append(LINETO);
00282                 pw.println(sb.toString());
00283             } else {    // 始点が前回の終点と異なる?
00284                 // stroke
00285                 // newpath
00286                 // x1 y1 moveto
00287                 // x2 y2 lineto
00288                 sb = new StringBuffer(STROKE);
00289                 sb.append(' '); sb.append(NEWPATH);
00290                 sb.append(' '); sb.append(x1 / scale_ + xOfs_);
00291                 sb.append(' '); sb.append(PAGE_HEIGHT - y1 / scale_ - yOfs_);
00292                 sb.append(' '); sb.append(MOVETO);
00293                 sb.append('\n'); sb.append(x2 / scale_ + xOfs_);
00294                 sb.append(' '); sb.append(PAGE_HEIGHT - y2 / scale_ - yOfs_);
00295                 sb.append(' '); sb.append(LINETO);
00296                 pw.println(sb.toString());
00297             }
00298         } else {    // パス継続中でない?
00299             // newpath
00300             // x1 y1 moveto
00301             // x2 y2 lineto
00302             sb = new StringBuffer(NEWPATH);
00303             sb.append(' '); sb.append(x1 / scale_ + xOfs_);
00304             sb.append(' '); sb.append(PAGE_HEIGHT - y1 / scale_ - yOfs_);
00305             sb.append(' '); sb.append(MOVETO);
00306             sb.append('\n'); sb.append(x2 / scale_ + xOfs_);
00307             sb.append(' '); sb.append(PAGE_HEIGHT - y2 / scale_ - yOfs_);
00308             sb.append(' '); sb.append(LINETO);
00309             pw.println(sb.toString());
00310             inPath_ = true;
00311         }
00312         prevX_ = x2; prevY_ = y2;   // 終点を更新
00313 
00314         /* ★この実装は無駄が多いので取りやめ
00315         StringBuffer sb = new StringBuffer();
00316         sb.append(x1 / scale_ + xOfs_);
00317         sb.append(' '); sb.append(PAGE_HEIGHT - y1 / scale_ - yOfs_);
00318         sb.append(' '); sb.append(x2 / scale_ + xOfs_);
00319         sb.append(' '); sb.append(PAGE_HEIGHT - y2 / scale_ - yOfs_);
00320         sb.append(' '); sb.append(DRAW_LINE);
00321         pw.println(sb.toString());
00322         */
00323     }
00324 
00330     public void setFont(Font font) {
00331         _stroke();  // 引きかけの線を引く
00332         StringBuffer sb = new StringBuffer("/");
00333 
00334         // フォント決定
00335         //sb.append(font.getPSName()); <--- ★本来はこれで良いはずだが...
00336         String fname = font.getName();
00337         String psf;
00338         if (fname.equals("dialog")) {
00339             psf = "Helvetica";
00340         } else if (fname.equals("monospaced")) {
00341             psf = "Courier";
00342         } else {
00343             psf = "Times-Roman";
00344         }
00345         sb.append(psf);
00346 
00347         sb.append(' '); sb.append(font.getSize());
00348         sb.append(' '); sb.append(SET_FONT);
00349         pw.println(sb.toString());
00350     }
00351 
00359     public void drawString(String str, int x, int y) {
00360         _stroke();  // 引きかけの線を引く
00361         StringBuffer sb = new StringBuffer("(");
00362         int len = str.length();
00363         for (int i = 0; i < len; i++) {
00364             char c = str.charAt(i);
00365             if (c == '(' || c == ')') {
00366                 sb.append('\\');
00367             }
00368             sb.append(c);
00369         }
00370         sb.append(") "); sb.append(x / scale_ + xOfs_);
00371         sb.append(' '); sb.append(PAGE_HEIGHT - y / scale_ - yOfs_);
00372         sb.append(' '); sb.append(DRAW_STRING);
00373         pw.println(sb.toString());
00374     }
00375 
00381     public void setClip(Shape clip) {
00382         _stroke();  // 引きかけの線を引く
00383         if (clip == null) { // クリップ解除?
00384             pw.println(SET_CLIP_NULL);  // クリップ解除
00385         }
00386     }
00387 
00396     public void setClip(int x, int y, int width, int height) {
00397         _stroke();  // 引きかけの線を引く
00398         StringBuffer sb = new StringBuffer();
00399         sb.append(x / scale_ + xOfs_);
00400         sb.append(' '); sb.append(PAGE_HEIGHT - (y + height) / scale_ - yOfs_);
00401         sb.append(' '); sb.append(width / scale_);
00402         sb.append(' '); sb.append(height / scale_);
00403         sb.append(' '); sb.append(SET_CLIP);
00404         pw.println(sb.toString());  // クリップ
00405     }
00406 
00412     public void setXORMode(Color c) {
00413         // 無処理
00414     }
00415 
00420     public void setPaintMode() {
00421         // 無処理
00422     }
00423 
00424     // -----------------------------------------------------------------
00425     // Graphicsのメソッドオーバーライド(未使用部)
00426     public Graphics create() {
00427         return null;
00428     }
00429     public void translate(int x, int y) { }
00430     public Color getColor() {
00431         return null;
00432     }
00433     public Font getFont() {
00434         return null;
00435     }
00436     public FontMetrics getFontMetrics(Font f) {
00437         return null;
00438     }
00439     public Rectangle getClipBounds() {
00440         return null;
00441     }
00442     public void clipRect(int x, int y, int width, int height) { }
00443     public Shape getClip() {
00444         return null;
00445     }
00446     public void copyArea(
00447         int x, int y, int width, int height, int dx, int dy
00448     ) { }
00449     public void clearRect(int x, int y, int width, int height) { }
00450     public void drawRoundRect(
00451         int x, int y, int width, int height, int arcWidth, int arcHeight
00452     ) { }
00453     public void fillRoundRect(
00454         int x, int y, int width, int height, int arcWidth, int arcHeight
00455     ) { }
00456     public void drawOval(int x, int y, int width, int height) { }
00457     public void fillOval(int x, int y, int width, int height) { }
00458     public void drawArc(
00459         int x, int y, int width, int height, int startAngle, int arcAngle
00460     ) { }
00461     public void fillArc(
00462         int x, int y, int width, int height, int startAngle, int arcAngle
00463     ) { }
00464     public void drawPolyline(int xPoints[], int yPoints[], int nPoints) { }
00465     public void drawPolygon(int xPoints[], int yPoints[], int nPoints) { }
00466     public void fillPolygon(int xPoints[], int yPoints[], int nPoints) { }
00467     public void drawString(
00468         AttributedCharacterIterator iterator, int x, int y
00469     ) { }
00470     public boolean drawImage(
00471         Image img, int x, int y, ImageObserver observer
00472     ) {
00473         return false;
00474     }
00475     public boolean drawImage(
00476         Image img, int x, int y, int width, int height, ImageObserver observer
00477     ) {
00478         return false;
00479     }
00480     public boolean drawImage(
00481         Image img, int x, int y, Color bgcolor, ImageObserver observer
00482     ) {
00483         return false;
00484     }
00485     public boolean drawImage(
00486         Image img, int x, int y, int width, int height,
00487         Color bgcolor, ImageObserver observer
00488     ) {
00489         return false;
00490     }
00491     public boolean drawImage(
00492         Image img, int dx1, int dy1, int dx2, int dy2,
00493         int sx1, int sy1, int sx2, int sy2, ImageObserver observer
00494     ) {
00495         return false;
00496     }
00497     public boolean drawImage(
00498         Image img, int dx1, int dy1, int dx2, int dy2,
00499         int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer
00500     ) {
00501         return false;
00502     }
00503     public void dispose() { }
00504 
00505 
00506     // -----------------------------------------------------------------
00507     // プライベートメソッド
00517     private void _writeHeader(
00518         int top,
00519         int left,
00520         int width,
00521         int height,
00522         boolean color
00523     ) {
00524         // BoundingBox計算
00525         int bl = left;
00526         int bb = PAGE_HEIGHT - (top + height);
00527         int br = left + width;
00528         int bt = PAGE_HEIGHT - top;
00529 
00530         // Header
00531         pw.println(HEADER);
00532         StringBuffer sb = new StringBuffer(BOUNDING_BOX);
00533         sb.append(' '); sb.append(bl);
00534         sb.append(' '); sb.append(bb);
00535         sb.append(' '); sb.append(br);
00536         sb.append(' '); sb.append(bt);
00537         pw.println(sb.toString());
00538         pw.println();
00539 
00540         // 色オペレータ宣言
00541         pw.println("% Color definition");
00542         if (color) {
00543             for (int i = 0; i < colorList_.size(); i++) {
00544                 Color col = (Color)colorList_.get(i);
00545                 StringBuffer sbuf = new StringBuffer(DEF);
00546                 sbuf.append((String)colorOps_.get(i));
00547                 sbuf.append(" {0.3 setlinewidth 0 setgray [] 0 setdash ");
00548                 sbuf.append((255 - col.getRed()) / 255.0f);
00549                 sbuf.append(' ');
00550                 sbuf.append((255 - col.getGreen()) / 255.0f);
00551                 sbuf.append(' ');
00552                 sbuf.append((255 - col.getBlue()) / 255.0f);
00553                 sbuf.append(" setrgbcolor}");
00554                 sbuf.append(BIND_DEF);
00555                 pw.println(sbuf.toString());
00556             }
00557             pw.println(DEF_COLOR_OTHERS);
00558         } else {
00559             pw.println(DEF_COLOR_WHITE);
00560             pw.println(DEF_COLOR_LIGHTGRAY);
00561             pw.println(DEF_COLOR_GRAY);
00562             pw.println(DEF_COLOR_DARKGRAY);
00563             pw.println(DEF_COLOR_BLACK);
00564             pw.println(DEF_COLOR_OTHERS);
00565             pw.println(DEF_COLOR_GREEN);
00566             pw.println(DEF_COLOR_YELLOW);
00567             pw.println(DEF_COLOR_PINK);
00568             pw.println(DEF_COLOR_CYAN);
00569             pw.println(DEF_COLOR_MAGENTA);
00570             pw.println(DEF_COLOR_RED);
00571             pw.println(DEF_COLOR_ORANGE);
00572             pw.println(DEF_COLOR_BLUE);
00573         }
00574         pw.println();
00575 
00576         // 描画オペレータ宣言
00577         pw.println("% Method definition");
00578         pw.println(DEF_DRAW_LINE);
00579         pw.println(DEF_SET_FONT);
00580         pw.println(DEF_DRAW_STRING);
00581         pw.println(DEF_SET_CLIP);
00582         pw.println(DEF_SET_CLIP_NULL);
00583         pw.println(DEF_NEWPATH);
00584         pw.println(DEF_MOVETO);
00585         pw.println(DEF_LINETO);
00586         pw.println(DEF_STROKE);
00587         pw.println();
00588 
00589         // ヘッダ終了
00590         pw.println("% end of header");
00591         pw.println();
00592 
00593         // ★デバグ用(バウンディングボックス描画)
00594         pw.println("newpath");
00595         pw.println("" + bl + " " + bb + " moveto");
00596         pw.println("" + br + " " + bb + " lineto");
00597         pw.println("" + br + " " + bt + " lineto");
00598         pw.println("" + bl + " " + bt + " lineto");
00599         pw.println("closepath");
00600         pw.println("stroke");
00601     }
00602 
00607     private void _stroke() {
00608         if (inPath_) {  // パス継続中?
00609             pw.println(STROKE); // ストローク
00610             inPath_ = false;    // パス終了
00611         }
00612     }
00613 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Apr 11 2019 03:30:16