Crossfire JXClient, Trunk
TextSegment.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff
19  * Copyright (C) 2006-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.gui.log;
24 
25 import java.awt.Color;
26 import java.awt.Font;
27 import java.awt.Graphics;
28 import java.awt.Graphics2D;
29 import java.awt.font.FontRenderContext;
30 import java.awt.font.TextLayout;
31 import java.awt.geom.RectangularShape;
32 import java.util.Locale;
33 import org.jetbrains.annotations.NotNull;
34 
40 public class TextSegment {
41 
45  @NotNull
46  private final String text;
47 
51  @NotNull
52  private final Font font;
53 
57  private final boolean bold;
58 
62  private final boolean italic;
63 
67  private final boolean underline;
68 
72  @NotNull
73  private final FontID fontID;
74 
78  @NotNull
79  private final Color color;
80 
85  @NotNull
86  private final Color selectionColor;
87 
91  @NotNull
92  private final RectangularShape size;
93 
97  private final int ascentOffset;
98 
102  private final int underlineOffset;
103 
107  private int x;
108 
112  private int y;
113 
117  private int position;
118 
135  public TextSegment(@NotNull final String text, @NotNull final Font font, final boolean bold, final boolean italic, final boolean underline, @NotNull final FontID fontID, @NotNull final Color color, @NotNull final RectangularShape size, final int ascentOffset, final int underlineOffset, @NotNull final Color selectionColor) {
136  this.text = text;
137  this.font = font;
138  this.bold = bold;
139  this.italic = italic;
140  this.underline = underline;
141  this.fontID = fontID;
142  this.color = color;
143  this.size = size;
144  this.ascentOffset = ascentOffset;
145  this.underlineOffset = underlineOffset;
146  this.selectionColor = selectionColor;
147  }
148 
153  @NotNull
154  public String getText() {
155  return text;
156  }
157 
162  public int getPosition() {
163  return position;
164  }
165 
172  public void setExtends(final int x, final int y, final int position) {
173  this.x = x;
174  this.y = y+ascentOffset;
175  this.position = position;
176  }
177 
182  public void updatePosition(final int positions) {
183  if (positions < 0 && position+positions < 0) {
184  throw new IllegalArgumentException("cannot shift segment position "+position+" by "+positions);
185  }
186  position += positions;
187  }
188 
193  public int getX() {
194  return x;
195  }
196 
201  public int getY() {
202  return y+underlineOffset;
203  }
204 
209  public int getWidth() {
210  return (int)Math.round(size.getWidth());
211  }
212 
219  public int getWidth(final int chars, @NotNull final FontRenderContext context) {
220  return (int)font.getStringBounds(text.substring(0, chars), context).getWidth();
221  }
222 
227  public int getHeight() {
228  return (int)Math.round(size.getHeight());
229  }
230 
238  public void draw(@NotNull final Graphics2D g, final int y, final int beginSelection, final int endSelection) {
239  if (beginSelection < position+text.length() && position < endSelection) {
240  final int beginIndex = Math.max(0, beginSelection-position);
241  final int endIndex = Math.min(text.length(), endSelection-position);
242  final int beginX = beginIndex == 0 ? 0 : (int)Math.round(new TextLayout(text.substring(0, beginIndex), font, g.getFontRenderContext()).getBounds().getWidth());
243  final int endX = endIndex >= text.length() ? getWidth() : (int)Math.round(new TextLayout(text.substring(0, endIndex), font, g.getFontRenderContext()).getBounds().getWidth());
244  g.setColor(selectionColor);
245  g.fillRect(x+beginX, y+this.y-ascentOffset, endX-beginX, getHeight());
246  }
247 
248  g.setColor(color);
249  g.setFont(font);
250  g.drawString(text, x, y+this.y);
251  if (underline) {
252  g.drawLine(x, y+this.y+underlineOffset, x+(int)Math.round(size.getWidth())-1, y+this.y+underlineOffset);
253  }
254  }
255 
260  @NotNull
261  public RectangularShape getSize() {
262  return size;
263  }
264 
275  public boolean matches(final boolean bold, final boolean italic, final boolean underline, @NotNull final FontID fontID, @NotNull final Color color, @NotNull final Color selectionColor) {
276  return this.bold == bold && this.italic == italic && this.underline == underline && this.fontID == fontID && this.color == color && this.selectionColor == selectionColor;
277  }
278 
279  @NotNull
280  @Override
281  public String toString() {
282  final StringBuilder sb = new StringBuilder();
283  sb.append("segment:");
284  if (bold) {
285  sb.append("(bold)");
286  }
287  if (italic) {
288  sb.append("(italic)");
289  }
290  if (underline) {
291  sb.append("(underline)");
292  }
293  if (fontID != FontID.PRINT) {
294  sb.append('(').append(fontID.toString().toLowerCase(Locale.ENGLISH)).append(')');
295  }
296  sb.append('(').append(Parser.toString(color)).append('/').append(Parser.toString(selectionColor)).append(')');
297  sb.append(text);
298  sb.append('\n');
299  return sb.toString();
300  }
301 
306  @NotNull
307  public String format() {
308  return "["+x+","+y+","+text+"]";
309  }
310 
317  public void appendSelection(@NotNull final StringBuilder sb, final int beginSelection, final int endSelection) {
318  final int beginIndex = Math.min(Math.max(0, beginSelection-position), text.length());
319  final int endIndex = Math.min(Math.max(0, endSelection-position), text.length());
320  sb.append(text, beginIndex, endIndex);
321  }
322 
323 }
com.realtime.crossfire.jxclient.gui.log.TextSegment.color
final Color color
Definition: TextSegment.java:79
com.realtime.crossfire.jxclient.gui.log.TextSegment.y
int y
Definition: TextSegment.java:112
com.realtime.crossfire.jxclient.gui.log.TextSegment.size
final RectangularShape size
Definition: TextSegment.java:92
com.realtime.crossfire.jxclient.gui.log.TextSegment.draw
void draw(@NotNull final Graphics2D g, final int y, final int beginSelection, final int endSelection)
Definition: TextSegment.java:238
com.realtime.crossfire.jxclient.gui.log.TextSegment.x
int x
Definition: TextSegment.java:107
com.realtime.crossfire.jxclient.gui.log.TextSegment
Definition: TextSegment.java:40
com.realtime.crossfire.jxclient.gui.log.TextSegment.getText
String getText()
Definition: TextSegment.java:154
com.realtime.crossfire.jxclient.gui.log.TextSegment.toString
String toString()
Definition: TextSegment.java:281
com.realtime.crossfire.jxclient.gui.log.TextSegment.underlineOffset
final int underlineOffset
Definition: TextSegment.java:102
com.realtime.crossfire.jxclient.gui.log.TextSegment.setExtends
void setExtends(final int x, final int y, final int position)
Definition: TextSegment.java:172
com.realtime.crossfire.jxclient.gui.log.TextSegment.underline
final boolean underline
Definition: TextSegment.java:67
com.realtime.crossfire.jxclient.gui.log.TextSegment.getHeight
int getHeight()
Definition: TextSegment.java:227
com.realtime.crossfire.jxclient.gui.log.TextSegment.getWidth
int getWidth(final int chars, @NotNull final FontRenderContext context)
Definition: TextSegment.java:219
com.realtime.crossfire.jxclient.gui.log.TextSegment.ascentOffset
final int ascentOffset
Definition: TextSegment.java:97
com.realtime.crossfire.jxclient.gui.log.TextSegment.selectionColor
final Color selectionColor
Definition: TextSegment.java:86
com.realtime.crossfire.jxclient.gui.log.TextSegment.appendSelection
void appendSelection(@NotNull final StringBuilder sb, final int beginSelection, final int endSelection)
Definition: TextSegment.java:317
com.realtime.crossfire.jxclient.gui.log.FontID
Definition: FontID.java:29
com.realtime.crossfire.jxclient.gui.log.TextSegment.font
final Font font
Definition: TextSegment.java:52
com.realtime.crossfire.jxclient.gui.log.TextSegment.getX
int getX()
Definition: TextSegment.java:193
com.realtime.crossfire.jxclient.gui.log.TextSegment.text
final String text
Definition: TextSegment.java:46
com.realtime.crossfire.jxclient.gui.log.TextSegment.fontID
final FontID fontID
Definition: TextSegment.java:73
com.realtime.crossfire.jxclient.gui.log.TextSegment.position
int position
Definition: TextSegment.java:117
com.realtime.crossfire.jxclient.gui.log.TextSegment.getPosition
int getPosition()
Definition: TextSegment.java:162
com.realtime.crossfire.jxclient.gui.log.FontID.PRINT
PRINT
Definition: FontID.java:34
com.realtime.crossfire.jxclient.gui.log.TextSegment.TextSegment
TextSegment(@NotNull final String text, @NotNull final Font font, final boolean bold, final boolean italic, final boolean underline, @NotNull final FontID fontID, @NotNull final Color color, @NotNull final RectangularShape size, final int ascentOffset, final int underlineOffset, @NotNull final Color selectionColor)
Definition: TextSegment.java:135
com.realtime.crossfire.jxclient.gui.log.TextSegment.italic
final boolean italic
Definition: TextSegment.java:62
com.realtime.crossfire.jxclient.gui.log.TextSegment.getY
int getY()
Definition: TextSegment.java:201
com.realtime.crossfire.jxclient.gui.log.TextSegment.format
String format()
Definition: TextSegment.java:307
com.realtime.crossfire.jxclient.gui.log.TextSegment.getSize
RectangularShape getSize()
Definition: TextSegment.java:261
com.realtime.crossfire.jxclient.gui.log.Parser.toString
static String toString(@NotNull final Color color)
Definition: Parser.java:341
com.realtime.crossfire.jxclient.gui.log.TextSegment.updatePosition
void updatePosition(final int positions)
Definition: TextSegment.java:182
com.realtime.crossfire.jxclient.gui.log.TextSegment.bold
final boolean bold
Definition: TextSegment.java:57
com.realtime.crossfire.jxclient.gui.log.Parser
Definition: Parser.java:40
com.realtime.crossfire.jxclient.gui.log.TextSegment.matches
boolean matches(final boolean bold, final boolean italic, final boolean underline, @NotNull final FontID fontID, @NotNull final Color color, @NotNull final Color selectionColor)
Definition: TextSegment.java:275
com.realtime.crossfire.jxclient.gui.log.TextSegment.getWidth
int getWidth()
Definition: TextSegment.java:209