--[[ SVGModule v1.0 - Alexander "Apickx" Pickering Entered into the public domain Febuary 4, 2017 You are not required to, but consider putting a link to the source in your file's comments! ]] local svg = {} local file_cache_max_size = 100 local file_cache_size = 0 local file_cache = {} local function readsvg(path) if file_cache[path] == nil then if file_cache_size > file_cache_max_size then local k,_ = next(file_cache) --Random replacement file_cache[k] = nil end file_cache[path] = file.Read(path,"GAME") end return file_cache[path] end local function updatesvg(self) if self.path ~= self.svgpath then self.svgdata = readsvg(self.path) assert(self.svgdata ~= nil,"Could not open file:" .. self.path) self.svgpath = self.path end local bgf = "" local fgf = "" if self.bg ~= nil then bgf = string.format("svg{background:%s}",self.bg) end if self.fg ~= nil then fgf = string.format("svg path{fill:%s}",self.fg) end self.html:SetHTML(string.format([[
%s ]],bgf,fgf,self.svgdata)) end local function updatesvgimg(self,newpath) self.path = newpath self:Update() end local function updatesvgfg(self,fore) self.fg = fore self:Update() end local function updatesvgbg(self,back) self.bg = back self:Update() end local toprocess = {} function svg.MaterialFromSVG(spath,background,foreground) local html = vgui.Create("DHTML") local svgdata = readsvg(spath) local bgf = "" local fgf = "" if background ~= nil then bgf = string.format("svg{background:%s}",background) end if foreground ~= nil then fgf = string.format("svg path{fill:%s}",foreground) end html:SetHTML(string.format([[ %s ]],background and bgf or "",fgf and foreground or "",svgdata)) local mat = {} toprocess[#toprocess + 1] = {mat,html} return mat end hook.Add("Think","process_svg_materials",function() for k,v in ipairs(toprocess) do local hm = v[2]:GetHTMLMaterial() if hm then v[1].material = hm end for i = k,#toprocess do toprocess[i] = toprocess[i + 1] end end end) function svg.SvgOnDpanel(spath,background,foreground,dpanel) local ret = {} ret.html = vgui.Create("DHTML",dpanel) ret.path = spath ret.fg = foreground or Color(0,0,0,255) ret.bg = background or Color(255,255,255,0) ret.UpdateForeground = updatesvgfg ret.UpdateBackground = updatesvgbg ret.UpdateImage = updatesvgimg ret.Update = updatesvg ret:Update() return ret end return svg