forked from darktable-org/darktable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow_icons_in_paint_c.pl
executable file
·150 lines (126 loc) · 4.33 KB
/
show_icons_in_paint_c.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env perl
use File::Temp qw/ tempfile tempdir /;
$ICONWIDTH = 50;
$ICONHEIGHT = 30;
$BORDER = 4;
$FONTSIZE = 12;
$ALIGNMENT_CROSS = false;
my $srcdir = $ARGV[0];
my $argc = @ARGV;
if($argc != 1){
print "Usage: show_icons_in_paint_c.pl <src dir>\n";
exit 1;
}
# create a temporary directory
my $tempdir = tempdir(CLEANUP => 1);
# find all the icons listed in paint.h
open(PAINTH, "<$srcdir/dtgtk/paint.h");
my $comment;
my $lastlinewasacomment = false;
my $code;
my $numberoficons = 0;
my $longeststring;
my @codelines;
while(<PAINTH>){
chomp;
next if $_ eq '';
if($_ =~ /^\/\*\* (.*) \*\//){ # this should be the description of a paint function
$comment = $1;
$lastlinewasacomment = true;
}
else{
if($lastlinewasacomment eq true){ # this is the next non-empty line after a comment, so it should be a paint function
$_ =~ /(dtgtk_cairo_[^(]*)/;
$code = $1;
my $description = "$comment ($code)";
$longeststring = $description if length($longeststring) < length($description);
$codelines[$numberoficons][0] = $description;
$codelines[$numberoficons][1] = $code;
$numberoficons++;
}
$lastlinewasacomment = false;
}
}
close(PAINTH);
# write the .c file
open(TESTC, ">$tempdir/test.c");
print TESTC <<EOF;
#include <cairo.h>
#include "dtgtk/paint.h"
int main(){
cairo_surface_t *surface;
cairo_t *cr;
cairo_text_extents_t ext;
int width, height, textwidth;
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 10, 10);
cr = cairo_create(surface);
cairo_select_font_face (cr, "mono", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size (cr, $FONTSIZE);
cairo_text_extents (cr, "$longeststring", &ext);
textwidth = ext.width; width = textwidth+4*$ICONWIDTH+6*$BORDER; height = $numberoficons*($ICONHEIGHT+$BORDER)+$BORDER;
cairo_destroy(cr);
cairo_surface_destroy(surface);
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
cr = cairo_create(surface);
cairo_select_font_face (cr, "mono", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size (cr, $FONTSIZE);
cairo_rectangle(cr, 0.0, 0.0, width, height);
cairo_set_source_rgb(cr, .15, .15, .15);
cairo_fill(cr);
EOF
# add all the calls to the individual paint functions
my $row = 0;
foreach my $line (@codelines){
my $description = @$line[0];
$description =~ s/"/\\"/g;
my $code = @$line[1];
# add description text
print TESTC <<EOF;
cairo_save(cr);
cairo_set_source_rgb(cr, .79, .79, .79);
cairo_move_to (cr, $BORDER, $BORDER+$row*($ICONHEIGHT+$BORDER)+0.8*$ICONHEIGHT);
cairo_show_text(cr, "$description");
cairo_restore(cr);
EOF
# add icons for all four directions
my $column = 0;
foreach my $direction ("CPF_DIRECTION_UP", "CPF_DIRECTION_DOWN", "CPF_DIRECTION_LEFT", "CPF_DIRECTION_RIGHT"){
print TESTC <<EOF;
cairo_save(cr);
cairo_rectangle(cr, textwidth+2*$BORDER+$column*($ICONWIDTH+$BORDER), $BORDER+$row*($ICONHEIGHT+$BORDER), $ICONWIDTH, $ICONHEIGHT);
cairo_set_source_rgb(cr, .17, .17, .17);
cairo_fill(cr);
EOF
if($ALIGNMENT_CROSS eq true){
print TESTC <<EOF;
cairo_set_source_rgb(cr, .7, 0, 0);
cairo_set_line_width(cr, 1);
cairo_move_to(cr, textwidth+2*$BORDER+$column*($ICONWIDTH+$BORDER)+0.5*$ICONWIDTH, $BORDER+$row*($ICONHEIGHT+$BORDER));
cairo_line_to(cr, textwidth+2*$BORDER+$column*($ICONWIDTH+$BORDER)+0.5*$ICONWIDTH, $BORDER+$row*($ICONHEIGHT+$BORDER)+$ICONHEIGHT);
cairo_stroke(cr);
EOF
}
print TESTC <<EOF;
cairo_move_to(cr, textwidth+2*$BORDER+$column*($ICONWIDTH+$BORDER), $BORDER+$row*($ICONHEIGHT+$BORDER)+0.5*$ICONHEIGHT);
cairo_line_to(cr, textwidth+2*$BORDER+$column*($ICONWIDTH+$BORDER)+$ICONWIDTH, $BORDER+$row*($ICONHEIGHT+$BORDER)+0.5*$ICONHEIGHT);
cairo_stroke(cr);
cairo_set_source_rgb(cr, .79, .79, .79);
$code(cr, textwidth+3*$BORDER+$column*($ICONWIDTH+$BORDER), 2*$BORDER+$row*($ICONHEIGHT+$BORDER), $ICONWIDTH-2*$BORDER, $ICONHEIGHT-2*$BORDER, $direction);
cairo_restore(cr);
EOF
$column++;
}
$row++;
}
# rest of the .c file
print TESTC <<EOF;
cairo_destroy(cr);
cairo_surface_write_to_png(surface, "show_icons_in_paint_c.png");
cairo_surface_destroy(surface);
return 0;
}
EOF
# compile & run the .c file
system("gcc `pkg-config --cflags --libs gtk+-2.0` -lm -std=c99 -I$srcdir -o $tempdir/test $tempdir/test.c $srcdir/dtgtk/paint.c");
system("$tempdir/test");
print "show_icons_in_paint_c.png created in the current directory\n";