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