| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| tsukuba_bunko.peko.canvas.text.Line |
|
|
| 1 | /* |
|
| 2 | * All Rights Reserved. |
|
| 3 | * Copyright (C) 1999-2005 Tsukuba Bunko. |
|
| 4 | * |
|
| 5 | * Licensed under the BSD License ("the License"); you may not use |
|
| 6 | * this file except in compliance with the License. |
|
| 7 | * You may obtain a copy of the License at |
|
| 8 | * |
|
| 9 | * http://www.tsukuba-bunko.org/licenses/LICENSE.txt |
|
| 10 | * |
|
| 11 | * Unless required by applicable law or agreed to in writing, software |
|
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 14 | * See the License for the specific language governing permissions and |
|
| 15 | * limitations under the License. |
|
| 16 | * |
|
| 17 | * $Id: Line.java,v 1.2 2005/07/23 18:54:35 ppoi Exp $ |
|
| 18 | */ |
|
| 19 | package tsukuba_bunko.peko.canvas.text; |
|
| 20 | ||
| 21 | import java.io.Serializable; |
|
| 22 | ||
| 23 | import java.awt.Color; |
|
| 24 | import java.awt.Graphics2D; |
|
| 25 | import java.awt.Shape; |
|
| 26 | ||
| 27 | import java.awt.font.TextAttribute; |
|
| 28 | import java.awt.font.TextLayout; |
|
| 29 | ||
| 30 | import java.text.AttributedString; |
|
| 31 | ||
| 32 | ||
| 33 | /** |
|
| 34 | * ページに描画される行です。 |
|
| 35 | * @author $Author: ppoi $ |
|
| 36 | * @version $Revision: 1.2 $ |
|
| 37 | */ |
|
| 38 | public class Line implements Serializable { |
|
| 39 | ||
| 40 | /** |
|
| 41 | * serial version UID |
|
| 42 | */ |
|
| 43 | private static final long serialVersionUID = 6658248129363617887L; |
|
| 44 | ||
| 45 | /** |
|
| 46 | * 描画する TextLayout |
|
| 47 | */ |
|
| 48 | 0 | transient private TextLayout _textLayout = null; |
| 49 | ||
| 50 | /** |
|
| 51 | * shadow |
|
| 52 | */ |
|
| 53 | 0 | transient private Shape _shape = null; |
| 54 | ||
| 55 | /** |
|
| 56 | * line span |
|
| 57 | */ |
|
| 58 | 0 | private float _lineSpan = 0f; |
| 59 | ||
| 60 | /** |
|
| 61 | */ |
|
| 62 | 0 | private Color _foreground = null; |
| 63 | ||
| 64 | /** |
|
| 65 | */ |
|
| 66 | 0 | private Color _shadowColor = Color.black; |
| 67 | ||
| 68 | /** |
|
| 69 | * この行に描画される文字列 |
|
| 70 | */ |
|
| 71 | 0 | private String _text = null; |
| 72 | ||
| 73 | ||
| 74 | /** |
|
| 75 | * <code>Line</code> のインスタンスを作成します。 |
|
| 76 | */ |
|
| 77 | public Line() |
|
| 78 | { |
|
| 79 | 0 | super(); |
| 80 | 0 | } |
| 81 | ||
| 82 | /** |
|
| 83 | * 描画に使用する前景色を指定します。 |
|
| 84 | * @param color 前景色 |
|
| 85 | */ |
|
| 86 | public void setForeground( Color color ) |
|
| 87 | { |
|
| 88 | 0 | _foreground = color; |
| 89 | 0 | } |
| 90 | ||
| 91 | /** |
|
| 92 | * 描画に使用する前景色を取得します。 |
|
| 93 | * @return 前景色 |
|
| 94 | */ |
|
| 95 | public Color getForeground() |
|
| 96 | { |
|
| 97 | 0 | return _foreground; |
| 98 | } |
|
| 99 | ||
| 100 | /** |
|
| 101 | * 描画に使用する影の色を指定します。 |
|
| 102 | * @param color 影の色 |
|
| 103 | */ |
|
| 104 | public void setShadowColor( Color color ) |
|
| 105 | { |
|
| 106 | 0 | _shadowColor = color; |
| 107 | 0 | } |
| 108 | ||
| 109 | /** |
|
| 110 | * 描画に使用する影の色を取得します。 |
|
| 111 | * @return 描画に使用する影の色 |
|
| 112 | */ |
|
| 113 | public Color getShadowColor() |
|
| 114 | { |
|
| 115 | 0 | return _shadowColor; |
| 116 | } |
|
| 117 | ||
| 118 | /** |
|
| 119 | * 描画する文字列を設定します。 |
|
| 120 | * @param text 描画する文字列 |
|
| 121 | */ |
|
| 122 | public void setText( String text ) |
|
| 123 | { |
|
| 124 | 0 | _text = text; |
| 125 | 0 | } |
| 126 | ||
| 127 | /** |
|
| 128 | * 描画する文字列を取得します。 |
|
| 129 | * @return 描画する文字列 |
|
| 130 | */ |
|
| 131 | public String getText() |
|
| 132 | { |
|
| 133 | 0 | return _text; |
| 134 | } |
|
| 135 | ||
| 136 | /** |
|
| 137 | * 描画する TextLayout を設定します。 |
|
| 138 | * @param layout 描画する TextLayout |
|
| 139 | */ |
|
| 140 | public void setTextLayout( TextLayout layout ) |
|
| 141 | { |
|
| 142 | 0 | _textLayout = layout; |
| 143 | 0 | _shape = layout.getOutline( null ); |
| 144 | 0 | } |
| 145 | ||
| 146 | /** |
|
| 147 | * 描画する TextLayout を取得します。 |
|
| 148 | * @return 描画する TextLayout |
|
| 149 | */ |
|
| 150 | public TextLayout getTextLayout() |
|
| 151 | { |
|
| 152 | 0 | return _textLayout; |
| 153 | } |
|
| 154 | ||
| 155 | /** |
|
| 156 | * 行間隔を設定します。 |
|
| 157 | * @param lineSpan 行間隔 |
|
| 158 | */ |
|
| 159 | public void setLineSpan( float lineSpan ) |
|
| 160 | { |
|
| 161 | 0 | _lineSpan = lineSpan; |
| 162 | 0 | } |
| 163 | ||
| 164 | /** |
|
| 165 | * 行間隔を取得します。 |
|
| 166 | * @return 行間隔 |
|
| 167 | */ |
|
| 168 | public float getLineSpan() |
|
| 169 | { |
|
| 170 | 0 | return _lineSpan; |
| 171 | } |
|
| 172 | ||
| 173 | /** |
|
| 174 | * ベースラインより上の部分の長さ(ascent)を取得します。ascent には、行間隔部分も含まれます。 |
|
| 175 | * @return ascent |
|
| 176 | */ |
|
| 177 | public float getAscent() |
|
| 178 | { |
|
| 179 | 0 | return _textLayout.getAscent() + _lineSpan; |
| 180 | } |
|
| 181 | ||
| 182 | /** |
|
| 183 | * ベースラインより下の部分の長さ(descent)を取得します。 |
|
| 184 | * @return descent |
|
| 185 | */ |
|
| 186 | public float getDescent() |
|
| 187 | { |
|
| 188 | 0 | return _textLayout.getDescent(); |
| 189 | } |
|
| 190 | ||
| 191 | /** |
|
| 192 | * 行の長さ(advance)を取得します。 |
|
| 193 | * @return advance |
|
| 194 | */ |
|
| 195 | public float getAdavance() |
|
| 196 | { |
|
| 197 | 0 | return _textLayout.getAdvance(); |
| 198 | } |
|
| 199 | ||
| 200 | /** |
|
| 201 | * 行を描画します。 |
|
| 202 | * @param g2 グラフィックコンテクスト |
|
| 203 | * @param x 描画開始位置の x 座標 |
|
| 204 | * @param y ベースラインの y 座標 |
|
| 205 | */ |
|
| 206 | public void draw( Graphics2D g2, float x, class="keyword">float y ) |
|
| 207 | { |
|
| 208 | 0 | Graphics2D g = (Graphics2D)g2.create(); |
| 209 | 0 | g.translate( (int)x + 2, (class="keyword">int)y + 2 ); |
| 210 | 0 | if( _shadowColor == null ) { |
| 211 | 0 | g.setColor( Color.black ); |
| 212 | 0 | } |
| 213 | else { |
|
| 214 | 0 | g.setColor( _shadowColor ); |
| 215 | } |
|
| 216 | 0 | g.setRenderingHint( java.awt.RenderingHints.KEY_ANTIALIASING, java.awt.RenderingHints.VALUE_ANTIALIAS_ON ); |
| 217 | 0 | g.fill( _shape ); |
| 218 | ||
| 219 | 0 | Color original = g2.getColor(); |
| 220 | 0 | if( _foreground != null ) { |
| 221 | 0 | g2.setColor( _foreground ); |
| 222 | } |
|
| 223 | 0 | _textLayout.draw( g2, x, y ); |
| 224 | ||
| 225 | 0 | g2.setColor( original ); |
| 226 | 0 | } |
| 227 | ||
| 228 | ||
| 229 | /** |
|
| 230 | * 描画の準備を行います。 |
|
| 231 | */ |
|
| 232 | public void prepare( Page page ) |
|
| 233 | { |
|
| 234 | 0 | if( _textLayout == null ) { |
| 235 | 0 | AttributedString astring = new AttributedString( _text ); |
| 236 | 0 | astring.addAttribute( TextAttribute.FONT, page.getDefaultFont() ); |
| 237 | 0 | astring.addAttribute( TextAttribute.FOREGROUND, page.getForeground() ); |
| 238 | ||
| 239 | 0 | setTextLayout( new TextLayout(astring.getIterator(), page.getFontRenderContext()) ); |
| 240 | } |
|
| 241 | 0 | } |
| 242 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |