aboutsummaryrefslogtreecommitdiff
path: root/engine/src/core/com/jme3/font/ColorTags.java
blob: 01f15c31f88b05d4115a010e2441e40362c0f7ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.jme3.font;

import com.jme3.math.ColorRGBA;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Contains the color information tagged in a text string
 * Format: \#rgb#
 *         \#rgba#
 *         \#rrggbb#
 *         \#rrggbbaa#
 * @author YongHoon
 */
class ColorTags {
    private static final Pattern colorPattern = Pattern.compile("\\\\#([0-9a-fA-F]{8})#|\\\\#([0-9a-fA-F]{6})#|" +
    		                                                    "\\\\#([0-9a-fA-F]{4})#|\\\\#([0-9a-fA-F]{3})#");
    private LinkedList<Range> colors = new LinkedList<Range>();
    private String text;

    ColorTags() { }

    ColorTags(String seq) {
        setText(seq);
    }
    
    /**
     * @return text without color tags
     */
    String getPlainText() {
        return text;
    }
    
    LinkedList<Range> getTags() {
        return colors;
    }

    void setText(final String charSeq) {
        colors.clear();
        if (charSeq == null) {
            return;
        }
        Matcher m = colorPattern.matcher(charSeq);
        if (m.find()) {
            StringBuilder builder = new StringBuilder(charSeq.length()-7);
            int startIndex = 0;
            do {
                String colorStr = null;
                for (int i = 1; i <= 4 && colorStr==null; i++) {
                    colorStr = m.group(i);
                }
                builder.append(charSeq.subSequence(startIndex, m.start()));
                Range range = new Range(builder.length(), colorStr);
                startIndex = m.end();
                colors.add(range);
            } while (m.find());
            builder.append(charSeq.subSequence(startIndex, charSeq.length()));
            text = builder.toString();
        } else {
            text = charSeq;
        }
    }
    
    class Range {
        int start;
        ColorRGBA color;
        Range(int start, String colorStr) {
            this.start = start;
            this.color = new ColorRGBA();
            if (colorStr.length() >= 6) {
                color.set(Integer.parseInt(colorStr.subSequence(0,2).toString(), 16) / 255f,
                          Integer.parseInt(colorStr.subSequence(2,4).toString(), 16) / 255f,
                          Integer.parseInt(colorStr.subSequence(4,6).toString(), 16) / 255f,
                          1);
                if (colorStr.length() == 8) {
                    color.a = Integer.parseInt(colorStr.subSequence(6,8).toString(), 16) / 255f;
                }
            } else {
                color.set(Integer.parseInt(Character.toString(colorStr.charAt(0)), 16) / 15f,
                          Integer.parseInt(Character.toString(colorStr.charAt(1)), 16) / 15f,
                          Integer.parseInt(Character.toString(colorStr.charAt(2)), 16) / 15f,
                          1);
                if (colorStr.length() == 4) {
                    color.a = Integer.parseInt(Character.toString(colorStr.charAt(3)), 16) / 15f;
                }
            }
            
        }
    }
}