Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

Jump to content

Module:Sandbox/Wnt/Sprite

From Wikipedia, the free encyclopedia
 --- The purpose of this module is to show pieces of an image file instead of the whole image.
 --- One intended use is within other templates, such as a template to return flags of many
 --- countries, so that you need to load only ONE image to produce all the flags on the page
 --- This should produce - but is not actually known to - some increase in performance.
 --- It might also be useful for pulling out entries from very large tables of graphics, such
 --- as one character from a font.

 --- This module accepts a filename, and uses Template:Sprite/filename (or parameter spritedefs)
 --- to identify sprites within the image at filename.
 --- it locates a given name for a sprite within the file and scales it to the desired width


local p={}

function spritedraw(left,right,top,bottom,name,imagewidth,spritewidth,scalewidth,float)
    if (left==nil or right==nil or top==nil or bottom==nil or name==nil or imagewidth==nil or spritewidth==nil or scalewidth==nil) then return tostring(left) .. tostring(right)..tostring(top)..tostring(bottom)..tostring(name)..tostring(imagewidth)..tostring(spritewidth)..tostring(scalewidth) end
    if float then float="float:"..float..";" else float="" end
    local scale=scalewidth/spritewidth
    top=math.floor(top*scale)
    bottom=math.floor(bottom*scale)
    left=math.floor(left*scale)
    right=math.floor(right*scale)
    local scalestring=""
    if scalewidth~=spritewidth then scalestring=math.floor(imagewidth*scale)..'px|' end
    output='<div style="position:relative;'..float..'width:'..scalewidth..'px;height:'..(bottom-top)..'px;"><div style="position:absolute;'..float..'top:'..(-1*top)..'px;left:'..(-1*left)..'px;clip:rect('..top..'px,'..right..'px,'..bottom..'px,'..left..'px);">[[Image:Flag sprite demo.png|'..scalestring..'link=http://en.wikipedia.org/wiki/'..name..']]</div></div>'
          
    return output
end

function p.sprite(frame)
   local args=frame.args
   local parent=frame.getParent(frame)
   local pargs=parent.args
   local imagewidth=args.imagewidth or pargs.imagewidth or 360
   local spritewidth=args.spritewidth or pargs.spritewidth or 120
   local scalewidth=args.width or pargs.width or 120
   local leftcol=0;local midcol=120;local rightcol=240
   local filename=args.filename or pargs.filename or ""
   if filename=="" then return "error: Sprite needs filename= parameter" end
   local spritedefs=args.spritedefs or pargs.spritedefs or ("Module:Sandbox/Wnt/Sprite/"..filename)
   local sfile=mw.loadData(spritedefs)
   local name=args[1] or pargs[1] or "0"
   local top,bottom,left,right
   local count=-4;debuglog=""
   local float=args.float or pargs.float --string like "left", "right", or nil=no float
   repeat
      count=count+5
      local n=sfile[count];local l=sfile[count+1];local r=sfile[count+2];local t=sfile[count+3];local b=sfile[count+4]
      debuglog="n"..tostring(n).."l="..tostring(l).."r=" ..tostring(r).."t="..tostring(t).."b="..tostring(b)
      if not(n and l and r and t and b) then return "error: sprite not found"..debuglog end
      if n==name or count==tonumber(name) then top=tonumber(t);bottom=tonumber(b);left=tonumber(l);right=tonumber(r);break end
   until false
   if args.nowiki or pargs.nowiki then return frame:preprocess("<nowiki>"..spritedraw(left,right,top,bottom,name,imagewidth,spritewidth,scalewidth,float).."</nowiki>") end
   return spritedraw(left,right,top,bottom,name,imagewidth,spritewidth,scalewidth,float)
end

return p