Crossfire JXClient, Trunk
InternalHTMLRenderer.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.label;
24 
25 import java.awt.Color;
26 import java.awt.Font;
27 import java.awt.FontMetrics;
28 import java.awt.Graphics;
29 import java.util.Stack;
30 import javax.swing.text.MutableAttributeSet;
31 import javax.swing.text.html.HTML.Tag;
32 import javax.swing.text.html.HTMLEditorKit.ParserCallback;
33 import org.jetbrains.annotations.NotNull;
34 
38 public class InternalHTMLRenderer extends ParserCallback {
39 
40  @NotNull
41  private final Stack<Font> fonts = new Stack<>();
42 
43  @NotNull
44  private final Stack<Color> colors = new Stack<>();
45 
46  @NotNull
47  private final Graphics gc;
48 
49  private int x;
50 
51  private int y;
52 
53  private final int origX;
54 
55  private final int borderSize;
56 
57  public InternalHTMLRenderer(@NotNull final Font font, @NotNull final Color color, @NotNull final Graphics gc, final int x, final int y, final int borderSize) {
58  fonts.push(font);
59  colors.push(color);
60  this.gc = gc;
61  this.x = x;
62  this.y = y;
63  origX = x;
64  this.borderSize = borderSize;
65  }
66 
67  @Override
68  @SuppressWarnings("MethodDoesntCallSuperMethod")
69  public void handleText(final char @NotNull [] data, final int pos) {
70  gc.setFont(fonts.peek());
71  gc.setColor(colors.peek());
72  final FontMetrics m = gc.getFontMetrics();
73  final String str = new String(data);
74  final int w = m.stringWidth(str);
75  gc.drawString(str, x+borderSize, y+borderSize);
76  x += w;
77  }
78 
79  @Override
80  @SuppressWarnings("MethodDoesntCallSuperMethod")
81  public void handleStartTag(@NotNull final Tag t, @NotNull final MutableAttributeSet a, final int pos) {
82  if (t.equals(Tag.A)) {
83  fonts.push(fonts.peek());
84  colors.push(Color.GRAY);
85  //y += defaultFont.getSize()+1;
86  } else if (t.equals(Tag.B)) {
87  fonts.push(fonts.peek().deriveFont(Font.BOLD));
88  colors.push(colors.peek());
89  } else if (t.equals(Tag.I)) {
90  fonts.push(fonts.peek().deriveFont(Font.ITALIC));
91  colors.push(colors.peek());
92  } else if (t.equals(Tag.LI)) {
93  fonts.push(fonts.peek());
94  colors.push(colors.peek());
95  gc.setFont(fonts.peek());
96  gc.setColor(colors.peek());
97  final FontMetrics m = gc.getFontMetrics();
98  x = origX;
99  y += fonts.peek().getSize()+1;
100  final String str = " - ";
101  final int w = m.stringWidth(str);
102  gc.drawString(str, x+borderSize, y+borderSize);
103  x += w;
104  } else {
105  fonts.push(fonts.peek());
106  colors.push(colors.peek());
107  }
108  }
109 
110  @Override
111  @SuppressWarnings("MethodDoesntCallSuperMethod")
112  public void handleSimpleTag(@NotNull final Tag t, @NotNull final MutableAttributeSet a, final int pos) {
113  if (t.equals(Tag.BR)) {
114  y += fonts.peek().getSize()+1;
115  x = origX;
116  }
117  }
118 
119  @Override
120  @SuppressWarnings("MethodDoesntCallSuperMethod")
121  public void handleEndTag(@NotNull final Tag t, final int pos) {
122  fonts.pop();
123  colors.pop();
124  }
125 
126 }
com.realtime.crossfire.jxclient.gui.label.InternalHTMLRenderer.gc
final Graphics gc
Definition: InternalHTMLRenderer.java:47
com.realtime.crossfire.jxclient.gui.label.InternalHTMLRenderer.borderSize
final int borderSize
Definition: InternalHTMLRenderer.java:55
com.realtime.crossfire.jxclient.gui.label.InternalHTMLRenderer.x
int x
Definition: InternalHTMLRenderer.java:49
com.realtime.crossfire.jxclient.gui.label.InternalHTMLRenderer.fonts
final Stack< Font > fonts
Definition: InternalHTMLRenderer.java:41
com.realtime.crossfire.jxclient.gui.label.InternalHTMLRenderer.InternalHTMLRenderer
InternalHTMLRenderer(@NotNull final Font font, @NotNull final Color color, @NotNull final Graphics gc, final int x, final int y, final int borderSize)
Definition: InternalHTMLRenderer.java:57
com.realtime.crossfire.jxclient.gui.label.InternalHTMLRenderer
Definition: InternalHTMLRenderer.java:38
com.realtime.crossfire.jxclient.gui.label.InternalHTMLRenderer.handleEndTag
void handleEndTag(@NotNull final Tag t, final int pos)
Definition: InternalHTMLRenderer.java:121
com.realtime.crossfire.jxclient.gui.label.InternalHTMLRenderer.y
int y
Definition: InternalHTMLRenderer.java:51
com.realtime.crossfire.jxclient.gui.label.InternalHTMLRenderer.origX
final int origX
Definition: InternalHTMLRenderer.java:53
com.realtime.crossfire.jxclient.gui.label.InternalHTMLRenderer.colors
final Stack< Color > colors
Definition: InternalHTMLRenderer.java:44
com.realtime.crossfire.jxclient.gui.label.InternalHTMLRenderer.handleSimpleTag
void handleSimpleTag(@NotNull final Tag t, @NotNull final MutableAttributeSet a, final int pos)
Definition: InternalHTMLRenderer.java:112
com.realtime.crossfire.jxclient.gui.label.InternalHTMLRenderer.handleStartTag
void handleStartTag(@NotNull final Tag t, @NotNull final MutableAttributeSet a, final int pos)
Definition: InternalHTMLRenderer.java:81
com.realtime.crossfire.jxclient.gui.label.InternalHTMLRenderer.handleText
void handleText(final char @NotNull[] data, final int pos)
Definition: InternalHTMLRenderer.java:69