#!/usr/bin/pike /* * ttfwriter.pike * * Create a png with some text using the specified ttf font. * * History: * 27/10/2020 Creation from cropper.pike * * Author: Dario Rodriguez dario@softhome.net * This program is distributed under the terms of the GNU GPL v2.1+ */ int main(int argc, array(string) argv) { string fontname; int fontsize; string htmlcolor; string text; string destfilename; Image.Image textimage; Image.Image colorimage; mapping(string:int) color; if(argc<5 || (argc>1 && argv[1]=="--help")) { Stdio.werror("Syntax: "+argv[0]+" <font.ttf> <fontsize> <htmlcolor> <text> <outfile.png>\n"); Stdio.werror("Example: "+argv[0]+" font.ttf 16 \"#000000\" \"Text\" dest.png\n"); Stdio.werror("Example with alpha: "+argv[0]+" font.ttf 16 \"#000000a0\" \"Text\" dest.png\n"); return(1); } fontname=argv[1]; fontsize=(int)argv[2]; htmlcolor=argv[3]; text=argv[4]; destfilename=argv[5]; write("* Loading font "+fontname+"...\n"); Image.Fonts.set_font_dirs( ({"."}) ); Image.Fonts.Font font=Image.Fonts.Font(fontname,fontsize); write("* Parsing color...\n"); int r,g,b,a=0; htmlcolor-="#"; sscanf(htmlcolor[0..1],"%x",r); sscanf(htmlcolor[2..3],"%x",g); sscanf(htmlcolor[4..5],"%x",b); if(sizeof(htmlcolor)>6) sscanf(htmlcolor[6..7],"%x",a); color=(["r":r,"g":g,"b":b,"a":a]); write("* Rendering text...\n"); textimage=font->write(text)*((255.0-color["a"])/255.0); colorimage=Image.Image(textimage->xsize(),textimage->ysize())->clear(color["r"],color["g"],color["b"]); write("* Writing "+destfilename+"...\n"); Stdio.write_file(destfilename,Image.PNG.encode(colorimage,(["alpha":textimage]))); write("* Finished successfully\n"); return(0); }