1 /*****************************************************/
2 /* This java file is a part of the */
4 /* - Plouf's Java IRC Client - */
6 /* Copyright (C) 2002 - 2004 Philippe Detournay */
8 /* All contacts : theplouf@yahoo.com */
10 /* PJIRC is free software; you can redistribute */
11 /* it and/or modify it under the terms of the GNU */
12 /* General Public License as published by the */
13 /* Free Software Foundation; version 2 or later of */
16 /* PJIRC is distributed in the hope that it will */
17 /* be useful, but WITHOUT ANY WARRANTY; without */
18 /* even the implied warranty of MERCHANTABILITY or */
19 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
20 /* General Public License for more details. */
22 /* You should have received a copy of the GNU */
23 /* General Public License along with PJIRC; if */
24 /* not, write to the Free Software Foundation, */
25 /* Inc., 59 Temple Place, Suite 330, Boston, */
26 /* MA 02111-1307 USA */
28 /*****************************************************/
32 import irc.IRCConfiguration;
33 import irc.SmileyTable;
35 import java.awt.Dimension;
36 import java.awt.FontMetrics;
37 import java.awt.Graphics;
38 import java.awt.Image;
39 import java.awt.image.ImageObserver;
40 import java.util.Vector;
43 * Atomic characters drawer. The CharactersDrawer handles graphical smileys.
45 public class CharactersDrawer {
46 private IRCConfiguration _ircConfiguration;
47 private char[] _current;
48 private int _lineSpacing;
51 * Create a new CharactersDrawer.
54 * the irc configuration.
56 public CharactersDrawer(IRCConfiguration config) {
57 _ircConfiguration = config;
58 _current = new char[256];
59 _lineSpacing = config.getI("style:linespacing");
62 private int getBitmapSmileyWidth(int c, ImageObserver obs) {
63 Image img = _ircConfiguration.getSmileyTable().getImage(c);
66 return img.getWidth(obs);
69 private int getBitmapSmileyHeight(int c, ImageObserver obs) {
70 Image img = _ircConfiguration.getSmileyTable().getImage(c);
73 return img.getHeight(obs);
76 private Object drawBitmapSmiley(Graphics g, FontMetrics fm, int smiley, int x, int y, ImageObserver obs) {
77 Image img = _ircConfiguration.getSmileyTable().getImage(smiley);
80 int h = getBitmapSmileyHeight(smiley, obs);
84 g.drawImage(img, x, y, obs);
88 private String handleSmiley(String line, String ascii, char code) {
89 int pos = line.indexOf(ascii);
93 String previous = line.substring(0, pos);
94 String after = line.substring(pos + ascii.length());
95 char toAdd = (char) (code + 0xE000);
96 line = previous + toAdd + after;
97 return handleSmiley(line, ascii, code);
101 * Prepare and decode the given line for smileys.
104 * source line, before smileys replacement.
105 * @return prepared line with special characters.
107 public String decodeLine(String line) {
108 SmileyTable table = _ircConfiguration.getSmileyTable();
109 int s = table.getSize();
110 for (int i = 0; i < s; i++) {
111 String m = table.getMatch(i);
112 line = handleSmiley(line, m, (char) (i));
118 * Get the given string width, in pixel.
123 * the FontMetrics that will be used on display.
125 * the image observer in case of bitmap handling.
126 * @return the string width, in pixel.
128 public int getWidth(String str, FontMetrics fm, ImageObserver obs) {
129 if (_current.length < str.length())
130 _current = new char[str.length() * 2];
133 for (int i = 0; i < str.length(); i++) {
134 char c = str.charAt(i);
135 if ((c >= 0xE000) && (c <= 0xF8FF)) {
137 w += fm.charsWidth(_current, 0, size);
139 w += getBitmapSmileyWidth(c, obs);
141 _current[size++] = c;
144 w += fm.charsWidth(_current, 0, size);
149 * Get the given string height, in pixel.
154 * the FontMetrics that will be used on display.
156 * the image observer in case of bitmap handling.
157 * @return the string height, in pixel.
159 public int getHeight(String str, FontMetrics fm, ImageObserver obs) {
162 for (int i = 0; i < str.length(); i++) {
163 char c = str.charAt(i);
164 if ((c >= 0xE000) && (c <= 0xF8FF)) {
166 h = getBitmapSmileyHeight(c, obs);
171 h = fm.getFont().getSize() + 1;
174 return mh + _lineSpacing;
178 * Get the given string width and height, in pixel.
183 * the FontMetrics that will be used on display.
185 * Dimension to be used for result.
187 * the image observer in case of bitmap handling.
189 public void getWidthHeight(String str, FontMetrics fm, Dimension res, ImageObserver obs) {
190 if (_current.length < str.length())
191 _current = new char[str.length() * 2];
196 for (int i = 0; i < str.length(); i++) {
197 char c = str.charAt(i);
198 if ((c >= 0xE000) && (c <= 0xF8FF)) {
200 w += fm.charsWidth(_current, 0, size);
201 h = getBitmapSmileyHeight(c, obs);
204 w += getBitmapSmileyWidth(c, obs);
207 _current[size++] = c;
210 w += fm.charsWidth(_current, 0, size);
211 h = fm.getFont().getSize() + 1;
215 res.height = mh + _lineSpacing;
219 * Draw the given prepared line.
222 * prepared line to draw.
224 * graphics to draw onto.
226 * fontmetrics to use.
232 * the image observer in case of bitmap display.
234 * target vector, where all update handles will be put.
236 public void draw(String str, Graphics g, FontMetrics fm, int x, int y, ImageObserver obs, Vector handles) {
237 if (_current.length < str.length())
238 _current = new char[str.length() * 2];
240 for (int i = 0; i < str.length(); i++) {
241 char c = str.charAt(i);
242 if ((c >= 0xE000) && (c <= 0xF8FF)) {
244 g.drawChars(_current, 0, size, x, y);
245 x += fm.charsWidth(_current, 0, size);
247 Object handle = drawBitmapSmiley(g, fm, c, x, y, obs);
249 handles = new Vector();
250 handles.insertElementAt(handle, handles.size());
251 x += getBitmapSmileyWidth(c, obs);
253 _current[size++] = c;
256 g.drawChars(_current, 0, size, x, y);