summaryrefslogtreecommitdiff
path: root/script-editor/FontFile.cs
blob: cc74763537514963f733f1d21fb38cbd48dee59e (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Xml;

namespace VPScriptEditor
{
    public class FontFile
    {
        private Dictionary<char, int> data;
        private Symbol[] glyphs;
        private Bitmap[] bitmaps;

        private class Symbol
        {
            private bool[,] data;
            private int width;
            private char text;

            public char getText()
            {
                return text;
            }

            public bool getXY(int x, int y)
            {
                return data[x, y];
            }

            public int getWidth()
            {
                return width;
            }

            public Symbol(XmlAttributeCollection attrs)
            {
                data = new bool[12, 12];

                for (int i = 0; i < attrs.Count; i++)
                {
                    switch (attrs[i].Name)
                    {
                        case "Width":
                            width = int.Parse(attrs[i].Value);
                            break;

                        case "Data":
                            byte[] bdata = Convert.FromBase64String(attrs[i].Value);

                            for (int y = 0; y < 12; y++)
                            {
                                int curr = bdata[y * 2 + 1] * 256 + bdata[y * 2];
                                for (int x = 0; x < 12; x++)
                                    if ((curr & (0x8000 >> x)) > 0)
                                        data[x, y] = true;
                                    else
                                        data[x, y] = false;
                            }

                            break;

                        case "Text":
                            if (attrs[i].Value.Length != 1)
                                throw new Exception("Invalid database, character " + attrs[i].Value + " not supported");

                            text = attrs[i].Value[0];
                            break;

                        default:
                            throw new Exception(("Wrong Font XML Format - Unknown attribute: " + attrs[i].Name));
                    }

                }
            }
        }

        public FontFile(System.IO.StreamReader file, Color color)
        {
            XmlDocument fontXml = new XmlDocument();
            fontXml.Load(file);
            XmlNodeList fontNode = fontXml.GetElementsByTagName("Symbol");
            glyphs = new Symbol[fontNode.Count];
            bitmaps = new Bitmap[fontNode.Count];
            data = new Dictionary<char, int>();
            for (int i = 0; i < fontNode.Count; i++)
            {
                glyphs[i] = new Symbol(fontNode[i].Attributes);
                data.Add(glyphs[i].getText(), i);
                bitmaps[i] = new Bitmap(glyphs[i].getWidth(), 12);
                for (int y = 0; y < 12; y++)
                    for (int x = 0; x < glyphs[i].getWidth(); x++)
                        if (glyphs[i].getXY(x, y))
                            bitmaps[i].SetPixel(x, y, color);
            }
        }

        public bool containsSymbol(char symbol)
        {
            return data.ContainsKey(symbol);
        }

        public int getSymbolWidth(char symbol)
        {
            return glyphs[data[symbol]].getWidth();
        }

        public bool getSymbolXY(char symbol, int x, int y)
        {
            return glyphs[data[symbol]].getXY(x, y);
        }

        public Bitmap getBitmap(char symbol)
        {
            return bitmaps[data[symbol]];
        }
    }
}