Browse code

Initial commit

Dario Rodriguez authored on 28/10/2020 22:49:01
Showing 1 changed files
1 1
new file mode 100755
... ...
@@ -0,0 +1,56 @@
1
+#!/usr/bin/pike
2
+/*
3
+ * ttfwriter.pike
4
+ *
5
+ * Create a png with some text using the specified ttf font.
6
+ *
7
+ * History:
8
+ *      27/10/2020 Creation from cropper.pike
9
+ *
10
+ * Author: Dario Rodriguez dario@softhome.net
11
+ * This program is distributed under the terms of the GNU GPL v2.1+
12
+ */
13
+
14
+int
15
+main(int argc, array(string) argv)
16
+{
17
+	string fontname;
18
+	int fontsize;
19
+	string htmlcolor;
20
+        string text;
21
+        string destfilename;
22
+        Image.Image textimage;
23
+	Image.Image colorimage;
24
+	mapping(string:int) color;
25
+        if(argc<5 || (argc>1 && argv[1]=="--help")) {
26
+                Stdio.werror("Syntax:  "+argv[0]+" <font.ttf> <fontsize> <htmlcolor> <text> <outfile.png>\n");
27
+                Stdio.werror("Example: "+argv[0]+" font.ttf 16 \"#000000\" \"Text\" dest.png\n");
28
+                Stdio.werror("Example with alpha: "+argv[0]+" font.ttf 16 \"#000000a0\" \"Text\" dest.png\n");
29
+                return(1);
30
+        }
31
+	fontname=argv[1];
32
+	fontsize=(int)argv[2];
33
+	htmlcolor=argv[3];
34
+	text=argv[4];
35
+	destfilename=argv[5];
36
+        write("* Loading font "+fontname+"...\n");
37
+	Image.Fonts.set_font_dirs( ({"."}) );
38
+	Image.Fonts.Font font=Image.Fonts.Font(fontname,fontsize);
39
+	write("* Parsing color...\n");
40
+	int r,g,b,a=0;
41
+	htmlcolor-="#";
42
+	sscanf(htmlcolor[0..1],"%x",r);
43
+	sscanf(htmlcolor[2..3],"%x",g);
44
+	sscanf(htmlcolor[4..5],"%x",b);
45
+	if(sizeof(htmlcolor)>6)
46
+		sscanf(htmlcolor[6..7],"%x",a);
47
+	color=(["r":r,"g":g,"b":b,"a":a]);
48
+	write("* Rendering text...\n");
49
+	textimage=font->write(text)*((255.0-color["a"])/255.0);
50
+	colorimage=Image.Image(textimage->xsize(),textimage->ysize())->clear(color["r"],color["g"],color["b"]);
51
+	write("* Writing "+destfilename+"...\n");
52
+        Stdio.write_file(destfilename,Image.PNG.encode(colorimage,(["alpha":textimage])));
53
+	write("* Finished successfully\n");
54
+        return(0);
55
+}
56
+