1package com.jme3.font;
2
3import com.jme3.math.ColorRGBA;
4import java.util.LinkedList;
5import java.util.regex.Matcher;
6import java.util.regex.Pattern;
7
8/**
9 * Contains the color information tagged in a text string
10 * Format: \#rgb#
11 *         \#rgba#
12 *         \#rrggbb#
13 *         \#rrggbbaa#
14 * @author YongHoon
15 */
16class ColorTags {
17    private static final Pattern colorPattern = Pattern.compile("\\\\#([0-9a-fA-F]{8})#|\\\\#([0-9a-fA-F]{6})#|" +
18    		                                                    "\\\\#([0-9a-fA-F]{4})#|\\\\#([0-9a-fA-F]{3})#");
19    private LinkedList<Range> colors = new LinkedList<Range>();
20    private String text;
21
22    ColorTags() { }
23
24    ColorTags(String seq) {
25        setText(seq);
26    }
27
28    /**
29     * @return text without color tags
30     */
31    String getPlainText() {
32        return text;
33    }
34
35    LinkedList<Range> getTags() {
36        return colors;
37    }
38
39    void setText(final String charSeq) {
40        colors.clear();
41        if (charSeq == null) {
42            return;
43        }
44        Matcher m = colorPattern.matcher(charSeq);
45        if (m.find()) {
46            StringBuilder builder = new StringBuilder(charSeq.length()-7);
47            int startIndex = 0;
48            do {
49                String colorStr = null;
50                for (int i = 1; i <= 4 && colorStr==null; i++) {
51                    colorStr = m.group(i);
52                }
53                builder.append(charSeq.subSequence(startIndex, m.start()));
54                Range range = new Range(builder.length(), colorStr);
55                startIndex = m.end();
56                colors.add(range);
57            } while (m.find());
58            builder.append(charSeq.subSequence(startIndex, charSeq.length()));
59            text = builder.toString();
60        } else {
61            text = charSeq;
62        }
63    }
64
65    class Range {
66        int start;
67        ColorRGBA color;
68        Range(int start, String colorStr) {
69            this.start = start;
70            this.color = new ColorRGBA();
71            if (colorStr.length() >= 6) {
72                color.set(Integer.parseInt(colorStr.subSequence(0,2).toString(), 16) / 255f,
73                          Integer.parseInt(colorStr.subSequence(2,4).toString(), 16) / 255f,
74                          Integer.parseInt(colorStr.subSequence(4,6).toString(), 16) / 255f,
75                          1);
76                if (colorStr.length() == 8) {
77                    color.a = Integer.parseInt(colorStr.subSequence(6,8).toString(), 16) / 255f;
78                }
79            } else {
80                color.set(Integer.parseInt(Character.toString(colorStr.charAt(0)), 16) / 15f,
81                          Integer.parseInt(Character.toString(colorStr.charAt(1)), 16) / 15f,
82                          Integer.parseInt(Character.toString(colorStr.charAt(2)), 16) / 15f,
83                          1);
84                if (colorStr.length() == 4) {
85                    color.a = Integer.parseInt(Character.toString(colorStr.charAt(3)), 16) / 15f;
86                }
87            }
88
89        }
90    }
91}
92