ecere/gfx/newFonts: Initial integration of new fonts system with FontResource
[sdk] / ecere / src / gfx / FontResource.ec
1 namespace gfx;
2
3 import "Window"
4 #if !defined(ECERE_VANILLA)
5 import "fmFontManager"
6 #endif
7
8 public class FontResource : Resource
9 {
10 public:
11    property const char * faceName { set { delete faceName; faceName = CopyString(value); } get { return this ? faceName : null; } };
12    property float size { set { size = value; } get { return this ? size : 0; } };
13    property bool bold { set { flags.bold = value; } get { return this ? flags.bold : false; } };
14    property bool italic { set { flags.italic = value; } get { return this ? flags.italic : false; } };
15    property bool underline { set { flags.underline = value; } get { return this ? flags.underline : false; } };
16    property Font font { get { return this ? font : null; } };
17    property Window window { set { if(value) { value.RemoveResource(this); value.AddResource(this); } }  };
18    property float outlineSize { set { outlineSize = value; } get { return this ? outlineSize : 0; } };
19    property float outlineFade { set { outlineFade = value; } get { return this ? outlineFade : 0; } };
20 #if !defined(ECERE_VANILLA)
21    property FMFont fmFont { get { return this ? fmFont : null; } };
22 #endif
23
24 private:
25    char * faceName;
26    Font font;
27    float size;
28    FontFlags flags;
29    DisplaySystem displaySystem;
30    float outlineSize, outlineFade;
31 #if !defined(ECERE_VANILLA)
32    FontManager fm;
33    FMFont fmFont;
34 #endif
35
36    void Load(FontResource copy, DisplaySystem displaySystem)
37    {
38       delete faceName;
39       faceName = *&CopyString(copy.faceName);
40       *&size = *&copy.size;
41       *&flags = *&copy.flags;
42       *&outlineSize = *&copy.outlineSize;
43       *&outlineFade = *&copy.outlineFade;
44       if(faceName && displaySystem)
45       {
46          this.displaySystem = displaySystem;
47          font = displaySystem.LoadOutlineFont(faceName, size, flags, outlineSize, outlineFade);
48       }
49    }
50
51 #if !defined(ECERE_VANILLA)
52    void LoadFM(FontResource copy, DisplaySystem displaySystem, FontManager fm)
53    {
54       Load(copy, displaySystem);
55       if(fm)
56       {
57          this.fm = fm;
58          fmFont = fm.getFont(this);
59       }
60    }
61 #endif
62
63    void Reference(FontResource reference)
64    {
65       delete faceName;
66       faceName = *&CopyString(reference.faceName);
67       *&size = *&reference.size;
68       *&flags = *&reference.flags;
69       *&outlineSize = *&reference.outlineSize;
70       *&outlineFade = *&reference.outlineFade;
71       font = reference.font;
72 #if !defined(ECERE_VANILLA)
73       fmFont = reference.fmFont;
74 #endif
75    }
76
77    void Dereference()
78    {
79       font = null;
80 #if !defined(ECERE_VANILLA)
81       fmFont = null;
82 #endif
83    }
84
85    ~FontResource()
86    {
87       if(font && displaySystem)
88          displaySystem.UnloadFont(font);
89 #if !defined(ECERE_VANILLA)
90       if(fmFont && fm)
91          fm.removeFont(fmFont);
92 #endif
93       delete faceName;
94    }
95
96    void OnCopy(FontResource newData)
97    {
98       property::size = newData.size;
99       property::faceName = newData.faceName;
100       property::bold = newData.bold;
101       property::outlineSize = newData.outlineSize;
102       property::outlineFade = newData.outlineFade;
103    }
104
105 /*
106    Window OnEdit(Window window, Window master, int x, int y, int w, int h, void * userData)
107    {
108       Window editData = class::OnEdit(window, master, x + 24,y,w - 48,h, userData);
109       Button browse
110       {
111          window, master = editData, inactive = true, text = "...", hotKey = F2,
112          position = { Max(x + 24, x + w - 24), y }, size = { 24, h }
113       };
114       browse.Create();
115       return editData;
116    }
117
118    void OnDisplay(Surface surface, int x, int y, int width, void * fieldData, Alignment alignment, DataDisplayFlags flags)
119    {
120       char * string = this ? faceName : null;
121       Font font = this ? font : null;
122       if(!string) string = "(none)";
123       surface.WriteTextDots(alignment, x + 24, y + 1, width - 24, string, strlen(string));
124       surface.SetBackground(White);
125       surface.Area(x - 4, y, x + 20, y + 15);
126
127       surface.SetForeground(Black);
128       surface.Rectangle(x-1, y + 1, x + 18, y + 14);
129    }
130
131    int OnCompare(FontResource font2)
132    {
133       int result = 0;
134       if(this && font2)
135       {
136          char * string1 = faceName;
137          char * string2 = font2.faceName;
138          if(string1 && string2)
139             result = strcmpi(string1, string2);
140       }
141       return result;
142    }
143
144    const char * OnGetString(char * string, void * fieldDat, bool * needClass)
145    {
146       if(this)
147       {
148          char * fileName = faceName;
149          if(fileName)
150             strcpy(string, fileName);
151          else
152             string[0] = '\0';
153          return string;
154       }
155       return null;
156    }
157
158    bool OnGetDataFromString(const char * string)
159    {
160       this = (string && string[0]) ? FontResource { } : null;
161       if(this)
162          faceName = string;
163    }
164 */
165 };