forked from ThornyFFXI/gdifonts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
include.lua
224 lines (193 loc) · 6.29 KB
/
include.lua
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
--[[
Copyright 2023 Thorny
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]--
local function GetLibPath()
return debug.getinfo(2, "S").source:sub(2);
end
local libPath = GetLibPath();
local d3d = require('d3d8');
local ffi = require('ffi');
local fontobject = dofile(string.gsub(libPath, 'include.lua', 'fontobject.lua'));
local rectobject = dofile(string.gsub(libPath, 'include.lua', 'rectobject.lua'));
local renderer = ffi.load(string.gsub(libPath, 'include.lua', 'gdifonttexture.dll'));
ffi.cdef[[
typedef struct {
int32_t BoxHeight;
int32_t BoxWidth;
float FontHeight;
float OutlineWidth;
uint32_t FontFlags;
uint32_t FontColor;
uint32_t OutlineColor;
uint32_t GradientStyle;
uint32_t GradientColor;
char FontFamily[256];
char FontText[4096];
} GdiFontData_t;
typedef struct {
int32_t Width;
int32_t Height;
int32_t Diameter;
uint32_t OutlineColor;
uint32_t OutlineWidth;
uint32_t FillColor;
uint32_t GradientStyle;
uint32_t GradientColor;
} GdiRectData_t;
typedef struct {
int32_t Width;
int32_t Height;
IDirect3DTexture8* Texture;
} GdiFontReturn_t;
uint32_t* CreateFontManager(IDirect3DDevice8* pDevice);
void DestroyFontManager(uint32_t* pManager);
GdiFontReturn_t CreateTexture(uint32_t* pManager, GdiFontData_t* data);
GdiFontReturn_t CreateRectTexture(uint32_t* pManager, GdiRectData_t* data);
bool GetFontAvailable(const char* font);
void EnableTextureDump(uint32_t* pManager, const char* folder);
void DisableTextureDump(uint32_t* pManager);
]]
local interface = renderer.CreateFontManager(d3d.get_device());
local objects = T{};
-- Render stuff..
local sprite = ffi.new('ID3DXSprite*[1]');
if (ffi.C.D3DXCreateSprite(d3d.get_device(), sprite) == ffi.C.S_OK) then
sprite = d3d.gc_safe_release(ffi.cast('ID3DXSprite*', sprite[0]));
else
sprite = nil;
end
local autoRender = false;
local frameDumpEnabled = false;
local function render_objects()
if (frameDumpEnabled) then
for _,obj in ipairs(objects) do
obj.is_dirty = true;
end
end
if (sprite ~= nil) then
sprite:Begin();
for _,obj in ipairs(objects) do
obj:render(sprite);
end
sprite:End();
if (frameDumpEnabled) then
frameDumpEnabled = false;
renderer.DisableTextureDump(interface);
end
end
end
local function sort_objects()
table.sort(objects, function(a,b) return (a.settings.z_order < b.settings.z_order) end);
end
local args = {
Interface = interface,
Renderer = renderer,
Rect = rectobject,
Sort = sort_objects,
};
-- Library exports..
local exports = {};
exports.FontFlags = {
None = 0,
Bold = 1,
Italic = 2,
Underline = 4,
Strikeout = 8
};
exports.Alignment = {
Left = 0,
Center = 1,
Right = 2
};
exports.Gradient = {
None = 0,
LeftToRight = 1,
TopLeftToBottomRight = 2,
TopToBottom = 3,
TopRightToBottomLeft = 4,
RightToLeft = 5,
BottomRightToTopLeft = 6,
BottomToTop = 7,
BottomLeftToTopRight = 8
};
function exports:create_object(settings, manual)
if (interface == nil) then
error('Interface doesn\'t exist.');
return;
end
local obj = fontobject:new(args, settings);
if (manual ~= true) then
objects:append(obj);
sort_objects();
end
return obj;
end
function exports:create_rect(settings, manual)
if (interface == nil) then
error('Interface doesn\'t exist.');
return;
end
local obj = rectobject:new(args, settings);
if (manual ~= true) then
objects:append(obj);
sort_objects();
end
return obj;
end
function exports:destroy_interface()
objects = T{};
renderer.DestroyFontManager(interface);
end
function exports:destroy_object(fontObject)
local newTable = T{};
for _,object in ipairs(objects) do
if (object ~= fontObject) then
newTable:append(object);
end
end
objects = newTable;
sort_objects();
end
function exports:disable_texture_dump()
renderer.DisableTextureDump(interface);
end
function exports:dump_frame(path)
if (ashita.fs.exists(path)) then
renderer.EnableTextureDump(interface, path);
frameDumpEnabled = true;
end
end
function exports:enable_texture_dump(path)
if (ashita.fs.exists(path)) then
renderer.EnableTextureDump(interface, path);
end
end
local checkedFonts = T{};
function exports:get_font_available(fontName)
local result = checkedFonts[fontName];
if result ~= nil then
return result;
end
result = renderer.GetFontAvailable(fontName);
checkedFonts[fontName] = result;
return result;
end
function exports:render()
render_objects();
end
function exports:set_auto_render(enabled)
local newSetting = (enabled == true);
if (autoRender ~= newSetting) then
autoRender = newSetting;
if autoRender then
ashita.events.register('d3d_present', 'gdifonts_render_tick', render_objects);
else
ashita.events.unregister('d3d_present', 'gdifonts_render_tick');
end
end
end
exports:set_auto_render(true);
return exports;