--image=fontsheet --seperationColor=the color to use for seperating the colors on the image used for love2d --rows=# of rows --columns=# of columns --box_borderw=you can seperate the characters by having them boxed, if you have a regular ASCII font sheet then set this to 0 --box_borderh=same as above --start_right=whether or not the image starts at the right and goes to the left --start_bottom=whether or not the image starts at the bottom and goes to the top --upDown_sheet=whether or not to go Row by Row or Column by Column, defaults to Row to Row --NOTE: the function returns the love2d font and the image used for the font function asciiFont(image, separationColor, rows, columns, box_borderw, box_borderh, start_right, start_bottom, upDown_sheet) --returns a new love image font from an ascii fontsheet and returns the image created if type(fontsheet)~=nil then upDown_sheet=upDown_sheet or nil rows=rows or 16 columns=columns or 16 start_right=start_right or nil start_bottom=start_bottom or nil box_borderw=box_borderw or 0 box_borderh=box_borderh or 0 if type(seperationColor)==nil then seperationColor={r=255,g=0,b=255} end local char_width=(image:getWidth()-(box_borderw*(columns+1)))/columns --store the width and height so it knows when to skip to the next character local char_height=(image:getHeight()-(box_borderh*(rows+1)))/rows local temp_rects={} local start_row=0 --row and column that started being used local start_column=0 local add_row=0 --tells whether to add -1, +1, or 0 to the row or column local add_column=0 local current_row=0 local current_column=0 local image_data=image:getData() --imageData of fontsheet, so the function can use getPixel local cc_counter=0 --used for shifting to the next row/column local retimage_height=0 --the height needed to make the image local setheights={} --stores how far to push each character; NOTE: subtracted by retimage_height when used -- Check how the columns work if start_right~=nil then --check if the characters start to the right; if true then.. start_column=columns-1 --then start at the right add_column=-1 --and move to left every time else --they start to the left start_column=0 --then start at the left add_column=1 --and move to the right every time end if columns==0 then --the fontsheet only goes up and down or down and up start_column=0 add_column=0 end -- Check how the columns work if start_bottom~=nil then --check if the characters start at the bottom; if true then.. start_row=rows-1 --then start at the bottom add_row=-1 --and move up every time else --they start at the the top start_row=0 --then start at the top add_row=1 --and move down every time end if columns==0 then --the fontsheet only goes left and right or right and left start_column=0 add_column=0 end current_row=start_row current_column=start_column for current_character=1,256,1 do --find the values for all ASCII characters -- Find pixel starting and stopping positions local loopx=char_width*current_column+(box_borderw*current_column)+box_borderw --start pixel for checking where the character starts local loopy=char_height*current_row+(box_borderh*current_row)+box_borderh local loopx_end=loopx+char_width-1 --end pixel for checking where the pixel ends local loopy_end=loopy+char_height-1 local found_pixel=false --tells if a pixel has been found local no_pixel=false --used to skip loops if no pixel exists for the character local r,g,b,a = 0 --used for getPixel local retrect = {x=0,y=0,w=0,h=0} local retquad=false --local setheight=0 --used for _charheights --local setwidth=0 --used for _charwidths cc_counter=cc_counter+1 -- FIND THE LEFT SIDE for tempx=0,char_width-1,1 do --go up and down moving to the right until a pixel is found for tempy=0,char_height-1,1 do r,g,b,a=image_data:getPixel(loopx+tempx,loopy+tempy) if a~=0 and found_pixel==false then --a pixel has been found! retrect.x=loopx+tempx found_pixel=true --image_data:setPixel(loopx+tempx,loopy+tempy,255,0,0,255) --RED break end end if found_pixel then break end end if found_pixel==false then no_pixel=true end found_pixel=false if no_pixel==false then --if a pixel exists then continue, if not stop checking -- FIND THE RIGHT SIDE for tempx_end=0,char_width-1,1 do --same as left side except go to the right for tempy=0,char_height-1,1 do r,g,b,a=image_data:getPixel(loopx_end-tempx_end,loopy+tempy) if a~=0 and found_pixel==false then --a pixel has been found! retrect.w=loopx_end-tempx_end-retrect.x+1 found_pixel=true --setwidth=retrect.w --image_data:setPixel(loopx_end-tempx_end,loopy+tempy,0,255,0,255) --GREEN break end end if found_pixel then break end end found_pixel=false -- FIND THE TOP for tempy=0,char_height-1,1 do --go left and right moving down until a pixel is found for tempx=0,char_width-1,1 do r,g,b,a=image_data:getPixel(loopx+tempx,loopy+tempy) if a~=0 and found_pixel==false then --a pixel has been found! retrect.y=loopy+tempy found_pixel=true setheights[current_character]=tempy --image_data:setPixel(loopx+tempx,loopy+tempy,0,0,255,255) --BLUE break end end end found_pixel=false -- FIND THE BOTTOM for tempy_end=0,char_height-1,1 do --same as top except move up for tempx=0,char_width-1,1 do r,g,b,a=image_data:getPixel(loopx+tempx,loopy_end-tempy_end) if a~=0 and found_pixel==false then --a pixel has been found! retrect.h=loopy_end-tempy_end-retrect.y+1 found_pixel=true retimage_height=math.max(retimage_height,retrect.h) --image_data:setPixel(loopx+tempx,loopy_end-tempy_end,255,255,0,255) --YELLOW break end end end end --table.insert(self._charheights,current_character,setheight) --table.insert(self._charwidths,current_character,setwidth) if upDown_sheet~=nil then --if the fontsheet goes up and down then current_row=current_row+add_row --go to the next row if(cc_counter>=rows) then --row is at the last row cc_counter=0 current_row=start_row --restart the column current_column=current_column+add_column --go to the next column end else --if the fontsheet goes left and right current_column=current_column+add_column --go to the next column if(cc_counter>=columns) then --column is at the last column cc_counter=0 current_column=start_column --restart the column current_row=current_row+add_row --go to the next row end end if no_pixel==true then retrect=0 else print("ASCII " .. current_character-1 .. " found!") end table.insert(temp_rects,current_character,retrect) end --end of loop if box_borderw==0 then box_borderw=nil end --destroy border variables if not used if box_borderh==0 then box_borderh=nil end local temp_string="" --a string with all used ascii characters local retimage_width=0 for i=0,255,1 do if temp_rects[i+1]~=0 then temp_string=temp_string .. string.char(i) retimage_width=retimage_width+1+temp_rects[i+1].w --adds the separation color space and the character width print("ASCII " .. i .. " refound!") end end retimage_width=retimage_width+1 --plus 1 since it the image ends with the separation color local retimage_data if retimage_width~=0 or retimage_height~=0 then retimage_data=love.image.newImageData(retimage_width,retimage_height) else return "size" end --returns that the size of the characters was too small print("retimage_width: " .. retimage_width .. "\nretimage_height: " .. retimage_height) local ascii=0 local current_x=0 for i=1,string.len(temp_string),1 do --create the image ascii=string.byte(temp_string,i,i) for v=0,retimage_height-1,1 do --draw the separation line print("i: " .. i .. " v: " .. v .. " ascii: " .. ascii) retimage_data:setPixel(current_x,v,separationColor.r,separationColor.g,separationColor.b,separationColor.a) end current_x=current_x+1 print("sX: " .. current_x-1 .. " cX: " .. current_x .. " nX: " .. current_x+temp_rects[ascii+1].w .. "\n") retimage_data:paste(image_data,current_x,setheights[ascii+1],temp_rects[ascii+1].x,temp_rects[ascii+1].y,temp_rects[ascii+1].w,temp_rects[ascii+1].h) --put the character on the return image current_x=current_x+temp_rects[ascii+1].w end for v=0,retimage_height-1,1 do --draw the final separation line retimage_data:setPixel(retimage_width-1,v,separationColor.r,separationColor.g,separationColor.b,separationColor.a) end print(love.filesystem.getSaveDirectory()) return love.graphics.newImageFont(image_data,temp_string),love.graphics.newImage(retimage_data) --return the font and the image created from the font else return "image" end --returns that the image could not be loaded end