ecere/gfx: Font outline support
[sdk] / ecere / src / gfx / FontResource.ec
1 namespace gfx;
2
3 import "Window"
4
5 public class FontResource : Resource
6 {
7 public:
8    property const char * faceName { set { delete faceName; faceName = CopyString(value); } get { return this ? faceName : null; } };
9    property float size { set { size = value; } get { return this ? size : 0; } };
10    property bool bold { set { flags.bold = value; } get { return this ? flags.bold : false; } };
11    property bool italic { set { flags.italic = value; } get { return this ? flags.italic : false; } };
12    property bool underline { set { flags.underline = value; } get { return this ? flags.underline : false; } };
13    property Font font { get { return this ? font : null; } };
14    property Window window { set { if(value) { value.RemoveResource(this); value.AddResource(this); } }  };
15    property float outlineSize { set { outlineSize = value; } get { return this ? outlineSize : 0; } };
16    property float outlineFade { set { outlineFade = value; } get { return this ? outlineFade : 0; } };
17
18 private:
19    char * faceName;
20    Font font;
21    float size;
22    FontFlags flags;
23    DisplaySystem displaySystem;
24    float outlineSize, outlineFade;
25
26    void Load(FontResource copy, DisplaySystem displaySystem)
27    {
28       delete faceName;
29       faceName = *&CopyString(copy.faceName);
30       *&size = *&copy.size;
31       *&flags = *&copy.flags;
32       *&outlineSize = *&copy.outlineSize;
33       *&outlineFade = *&copy.outlineFade;
34       if(faceName && displaySystem)
35       {
36          this.displaySystem = displaySystem;
37          font = displaySystem.LoadOutlineFont(faceName, size, flags, outlineSize, outlineFade);
38       }
39    }
40
41    void Reference(FontResource reference)
42    {
43       font = reference.font;
44    }
45
46    void Dereference()
47    {
48       font = null;
49    }
50
51    ~FontResource()
52    {
53       if(font && displaySystem)
54          displaySystem.UnloadFont(font);
55       delete faceName;
56    }
57
58 /*
59    Window OnEdit(Window window, Window master, int x, int y, int w, int h, void * userData)
60    {
61       Window editData = class::OnEdit(window, master, x + 24,y,w - 48,h, userData);
62       Button browse
63       {
64          window, master = editData, inactive = true, text = "...", hotKey = F2,
65          position = { Max(x + 24, x + w - 24), y }, size = { 24, h }
66       };
67       browse.Create();
68       return editData;
69    }
70
71    void OnDisplay(Surface surface, int x, int y, int width, void * fieldData, Alignment alignment, DataDisplayFlags flags)
72    {
73       char * string = this ? faceName : null;
74       Font font = this ? font : null;
75       if(!string) string = "(none)";
76       surface.WriteTextDots(alignment, x + 24, y + 1, width - 24, string, strlen(string));
77       surface.SetBackground(White);
78       surface.Area(x - 4, y, x + 20, y + 15);
79
80       surface.SetForeground(Black);
81       surface.Rectangle(x-1, y + 1, x + 18, y + 14);
82    }
83
84    int OnCompare(FontResource font2)
85    {
86       int result = 0;
87       if(this && font2)
88       {
89          char * string1 = faceName;
90          char * string2 = font2.faceName;
91          if(string1 && string2)
92             result = strcmpi(string1, string2);
93       }
94       return result;
95    }
96
97    const char * OnGetString(char * string, void * fieldDat, bool * needClass)
98    {
99       if(this)
100       {
101          char * fileName = faceName;
102          if(fileName)
103             strcpy(string, fileName);
104          else
105             string[0] = '\0';
106          return string;
107       }
108       return null;
109    }
110
111    bool OnGetDataFromString(const char * string)
112    {
113       this = (string && string[0]) ? FontResource { } : null;
114       if(this)
115          faceName = string;
116    }
117 */
118 };