Debian Codegraph @

texmf-dist/metapost/wordcloud/wordcloud.mp

raw
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 %% wordcloud %%
3 %% drawing wordclouds %%
4 %% with METAPOST and Lua %%
5 %% [email protected] %%
6 %% Version 0.3 (novembre 2025) %%
7 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 %
9 % This work may be distributed and/or modified under the conditions of
10 % the LaTeX Project Public License, either version 1.3c of this license
11 % or (at your option) any later version. The latest version of this
12 % license is in http://www.latex-project.org/lppl.txt
13 %
14 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
15 if not known mplib: input latexmp ; show"wordcloud Infos: latexmp loaded";fi;
16 input colorbrewer-rgb
17
18
19 % function to get all the path of letters (too slow)
20 vardef build_pathpicture(expr pict)=
21 save tex_pct,glp_pct,glp_idx,glp_num,pth_num,glp_pth,fnt_str, txt_str, sub_str,txt_wd;
22 % text material and glyphs contours
23 picture tex_pct, glp_pct;
24 numeric glp_num, pth_num[];
25 path glp_pth[][];
26 % tex_pct btex .. etex material
27 % glp_num number of glyphs within tex_pct
28 % pth_num[i] number of paths defining the rank i (0 .. glp_num-1) glyph
29 % glp_pth[i][j] rank j (0 .. pth_num[i]-1) path of the rank i glyph
30 % glp_pct glyphs picture
31
32 tex_pct:=pict;
33
34 glp_pct:=nullpicture;
35 string fnt_str, txt_str, sub_str;
36 numeric txt_wd;
37 glp_num:=0;
38 for tkn within pict:
39 %show tkn;
40 if textual tkn:
41 %show "textual";
42 fnt_str:=fontpart tkn;
43 txt_str:=textpart tkn;
44 txt_wd:=0;
45 for glp_idx=0 upto (length txt_str-1):
46 sub_str:=substring (glp_idx, glp_idx+1) of txt_str;
47 for sub_tkn within glyph ASCII sub_str of fnt_str
48 scaled (fontsize fnt_str/1000)
49 xscaled xxpart tkn
50 yscaled yypart tkn
51 shifted (txt_wd+xpart tkn, ypart tkn):
52 addto glp_pct doublepath (pathpart sub_tkn);
53 endfor
54 txt_wd:=txt_wd+
55 (xxpart tkn)*xpart urcorner (sub_str infont fnt_str);
56 endfor
57 fi
58 endfor
59 glp_pct
60 enddef;
61
62
63 % path intersection (trop gourmant en temps, too slow...)
64 vardef word_intersection(expr tic, tac)=
65 save _p, _w, _v,_output;
66 boolean _output;
67 _output:=false;
68 path _p,_w,_v;
69 for _p within tic:
70 _w := pathpart _p;
71 for _q within tac:
72 _v := pathpart _q;
73 if((_w intersectiontimes _v)<>(-1,-1)): % intersection
74 _output:=true;
75 fi
76 endfor
77 endfor
78 _output
79 enddef;
80
81 % global variable
82 _wordcloud_box_margin:=0.3pt;
83
84 % setting the box margin
85 def set_box_margin(expr s)=
86 _wordcloud_box_margin:=s;
87 enddef;
88
89 % extrenal box intersection (horizontal or vertical)
90 vardef extbb_intersection(expr tic, tac)=
91 save xmax_, xmin_, ymax_, ymin_, xmax__, xmin__, ymax__, ymin__, _output;
92 boolean _output;
93 _output:=false;
94 %show _p;
95 xmin_:= (xpart llcorner tic) -_wordcloud_box_margin;
96 ymin_ := (ypart llcorner tic)-_wordcloud_box_margin;
97 xmax_:= (xpart urcorner tic)+_wordcloud_box_margin;
98 ymax_ := (ypart urcorner tic)+_wordcloud_box_margin;
99 xmin__:= (xpart llcorner tac)-_wordcloud_box_margin;
100 ymin__ := (ypart llcorner tac)-_wordcloud_box_margin;
101 xmax__:= (xpart urcorner tac)+_wordcloud_box_margin;
102 ymax__ := (ypart urcorner tac)+_wordcloud_box_margin;
103 if ((xmin_<= xmax__) and (xmax_ >= xmin__) and (ymin_<= ymax__) and (ymax_ >= ymin__)):
104 _output:=true;
105 fi
106 _output
107 enddef;
108
109 % subbb_entersection of words (from textext)
110 %%%!!!!!! not working !!!!!!!!!
111 vardef bb_intersection(expr tic, tac)=
112 save xmax_, xmin_, ymax_, ymin_, xmax__, xmin__, ymax__, ymin__,
113 _output;
114 save _p, _w, _v,_output,_q;
115 boolean _output;
116 _output:=false;
117 path _p,_w,_v;
118 i:=0;
119 for _v within tic:
120 for _p within _v:
121 i:=i+1;
122 %show "i"&decimal(i);
123 xmin_:= (xpart llcorner _p)-_wordcloud_box_margin;
124 ymin_ := (ypart llcorner _p)-_wordcloud_box_margin;
125 xmax_:= (xpart urcorner _p)+_wordcloud_box_margin;
126 ymax_ := (ypart urcorner _p)+_wordcloud_box_margin;
127 j:=0;
128 for _w within tac:
129 for _q within _w:
130 j:=j+1;
131 %show "j"&decimal(j);
132 xmin__:= (xpart llcorner _q)-_wordcloud_box_margin;
133 ymin__ := (ypart llcorner _q)-_wordcloud_box_margin;
134 xmax__:= (xpart urcorner _q)+_wordcloud_box_margin;
135 ymax__ := (ypart urcorner _q)+_wordcloud_box_margin;
136 if ((xmin_<= xmax__) and (xmax_ >= xmin__) and (ymin_<= ymax__) and (ymax_ >= ymin__)):
137 _output:=true;
138 fi
139 exitif(_output);
140 endfor
141 endfor
142 exitif(_output);
143 endfor
144 endfor
145 _output
146 enddef;
147
148
149 % build the spiral to put words
150 path _wordcloud_spiral;
151
152 _wordcloud_spiral:=(0,0);
153
154 _wordcloud_N:= 10*360;
155 _tours:=20;
156 _scale:=0.3;
157 for j:=1 upto _tours:
158 _rapj:=j/_tours;
159 for i:=1 step 10 until 360:
160 _rapi:=((i)+(j)*360)/360;
161 %show (_rapj*_rapi);
162 %show (6*_rapj*_rapi)*(cosd(i)*1cm,sind(i)*1cm);
163 _wordcloud_spiral:=_wordcloud_spiral--((_scale*_rapi)*(cosd(i)*1cm,sind(i)*1cm));
164 endfor;
165 endfor
166
167 picture _wordcloud_words[], _wordcloud_pathwords[];
168 path _wordcloud_words_oriented_bb[];
169 numeric _wordcloud_weights[];
170
171 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
172 %%%%%%%%%% FONCTIONS USUELLES %%%%%%%%%
173 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
174 % from repere.mp Olivier Péault
175
176 pi:=3.141592654;
177 e:=2.718281828;
178
179 vardef sin(expr x) = sind(180*x/pi) enddef;
180
181 vardef cos(expr x) = cosd(180*x/pi) enddef;
182
183 vardef tan(expr x) = sin(x)/cos(x) enddef;
184
185 vardef exp(expr x) = mexp(x*256) enddef;
186
187 vardef ln(expr x) = mlog(x)/256 enddef;
188
189 vardef ch(expr x)=(exp(x)+exp(-x))/2 enddef;
190
191 vardef sh(expr x)=(exp(x)-exp(-x))/2 enddef;
192
193 vardef Arctan(expr x)= pi*angle(1,x)/180 enddef;
194 vardef Arctand(expr x)= angle(1,x) enddef;
195
196 vardef Arcsin(expr x)= Arctan(x/sqrt(1-x**2)) enddef;
197 vardef Arcsind(expr x)= Arctand(x/sqrt(1-x**2)) enddef;
198
199 vardef Arccos(expr x)= pi/2-Arcsin(x) enddef;
200 vardef Arccosd(expr x)= Arctand((sqrt(1-x**2))/x)enddef;
201
202 % general scaling
203 _wordcloud_gen_scale:=1;
204
205 def set_wordcloud_scale(expr s)=
206 _wordcloud_gen_scale:=s;
207 enddef;
208
209 % fonction d'agrandissement en fonction du poids du mot
210 vardef _wordcloud_scale_fct(expr e,_max,_min)=
211 save _t,_N,_a,_b;
212 _a*_min+_b=0.7;
213 _a*_max+_b=2.5;
214 _t:=_a*e+_b;
215 _t:=ln(_t)-ln(0.7)+0.295;
216 _wordcloud_gen_scale*(1.1*_t)
217 enddef;
218
219 % fabrication des mots avec la taille requise
220 vardef _build_tabs(suffix _words, _weights)(expr _N)=
221 save _tmp_pict,_width,_height;
222 picture _tmp_pict;
223 for i=1 upto _N:
224 _tmp_pict:=textext(_words[i]) scaled (_wordcloud_scale_fct(_weights[i],_weights[1],_weights[_N]));
225 _width:=abs(ulcorner _tmp_pict - urcorner _tmp_pict);
226 _height:=abs(ulcorner _tmp_pict - llcorner _tmp_pict);
227 _wordcloud_words[i]:= _tmp_pict shifted (-0.5_width,-0.5_height);
228 _wordcloud_weights[i]:=_weights[i];
229 _bbox_old:=bboxmargin;
230 bboxmargin:=_wordcloud_box_margin;
231 _wordcloud_words_oriented_bb[i]:= bbox _wordcloud_words[i];
232 bboxmargin:=_bbox_old;
233 _wordcloud_pathwords[i]:=build_pathpicture(_wordcloud_words[i]) ;
234 endfor;
235 enddef;
236 % function to compute angle
237 def _wordcloud_rotation(expr a,b)=
238 % a: angle
239 % b: 0 or 1
240 if b=0:
241 a
242 else:
243 if(a=0):
244 90
245 else:
246 -a
247 fi
248 fi
249 enddef;
250
251 %%% colors
252 boolean _wordcloud_use_color;
253 _wordcloud_use_color:=false;
254
255 numeric wordcloud_colors_number;
256 color wordcloud_colors[];
257
258 % default colors
259 wordcloud_colors[1]:=Reds[3][3];
260 wordcloud_colors[2]:=Greens[3][3];
261 wordcloud_colors[3]:=Blues[3][3];
262 wordcloud_colors[4]:=Oranges[3][3];
263 wordcloud_colors[5]:=black;
264 wordcloud_colors_number:=5;
265
266 def wordcloud_use_color(expr b)=
267 _wordcloud_use_color:= b;
268 enddef;
269
270 % main function to draw the picture
271 vardef draw_wordcloud(suffix _words, _weights, rotation)(expr _N)=
272 save _rotation_bool,_position,_ok_draw,_tmp_word,_random_pt,_position_pt,_pt;
273 pair _random_pt,_position_pt,_pt;
274 path _tmp_word;
275 boolean _ok_draw;
276 _position:=0;
277 _build_tabs(_words,_weights,_N);
278 for i=1 upto _N:
279 %show i;
280 _position:=0;
281 if(i mod 2) = 0:
282 _rotation_bool:=1;
283 else:
284 _rotation_bool:=0;
285 fi
286 if(i=1):
287 draw _wordcloud_words[i] rotated (_wordcloud_rotation(rotation,_rotation_bool)) shifted (point _position
288 of _wordcloud_spiral) if(_wordcloud_use_color): withcolor wordcloud_colors[i] fi ;
289 _position_pt:=(0,0);
290 _random_pt:=(0,0);
291 else:
292 _count:=0;
293
294 forever:
295 _position_pt:=point _position of _wordcloud_spiral;
296 _random_pt:=(uniformdeviate(1)*0.01cm,uniformdeviate(1)*0.01cm);
297 _pt:=_position_pt+_random_pt;
298 _count:=_count+1;
299 _ok_draw:= true;
300 _tmp_word:=_wordcloud_words_oriented_bb[i] rotated (_wordcloud_rotation(rotation,_rotation_bool)) shifted (_position_pt + _random_pt);
301 %_tmp_word:=_wordcloud_words[i] rotated (_rotation*90) shifted (point %_position
302 % of _wordcloud_spiral);
303 for j=1 upto i-1:
304 if(rectangles_intersection(_tmp_word,_wordcloud_words_oriented_bb[j])):
305 %if(bb_intersection(_tmp_word,_wordcloud_words[j])):
306 _ok_draw:= false;
307 %fi
308 fi
309 endfor;
310 if(_ok_draw): % if there is no intersections
311 draw _wordcloud_words[i] rotated
312 (_wordcloud_rotation(rotation,_rotation_bool)) shifted (_position_pt +
313 _random_pt) if(_wordcloud_use_color): withcolor wordcloud_colors[((i-1)
314 mod wordcloud_colors_number)+1] fi;
315 else: % else, we move along the spiral
316 _position:=_position+1;
317 fi
318 exitif(_ok_draw);
319 %exitif(_count>30);
320 endfor;
321 fi
322 _wordcloud_words[i]:=_wordcloud_words[i] rotated (_wordcloud_rotation(rotation,_rotation_bool)) shifted
323 (_position_pt + _random_pt);
324 _wordcloud_words_oriented_bb[i]:=_wordcloud_words_oriented_bb[i] rotated (_wordcloud_rotation(rotation,_rotation_bool)) shifted
325 (_position_pt + _random_pt);
326 _wordcloud_pathwords[i]:=_wordcloud_pathwords[i] rotated (_wordcloud_rotation(rotation,_rotation_bool)) shifted
327 (_position_pt + _random_pt);
328 endfor;
329 enddef;
330
331
332 vardef dotproduct(expr a,b)=
333 save _output;
334 _output := (xpart a)*(xpart b)+(ypart a)*(ypart b);
335 _output
336 enddef;
337
338 vardef normvec(expr p)=
339 p/(sqrt((xpart p)**2+(ypart p)**2))
340 enddef;
341
342 vardef bb_projection(expr p,a)=
343 % p : edge of a rectangle (pair)
344 % a : axis of proj
345 dotproduct(p,a)
346 enddef;
347
348 % intersection of oriented rectangel
349 vardef rectangles_intersection(expr tic,tac)=
350 % tic, tac, two rectangular path (closed)
351 save x,y,a,b,normedge,edge,_min,_max,_dotprod,_output,_minEdge,_maxEdge;
352 pair normedge,edge;
353 boolean _output;
354 _output:=false;
355 numeric x[],y[],a[],b[];
356 (x1,y1) = point 1 of tic;
357 (x2,y2) = point 2 of tic;
358 (x3,y3) = point 3 of tic;
359 (x4,y4) = point 4 of tic;
360 (a1,b1) = point 1 of tac;
361 (a2,b2) = point 2 of tac;
362 (a3,b3) = point 3 of tac;
363 (a4,b4) = point 4 of tac;
364 for i=1 upto 4:
365 %show (x[i],y[i]);
366 endfor;
367 for i=1 upto 4:
368 %show (a[i],b[i]);
369 endfor;
370 %draw (a1,b1)--(a2,b2)--(a3,b3)--(a4,b4);
371 for i=1 upto 2: % on prend deux côtés perpendiculaire du rectangle 1
372 %show "premier";
373 %show i;
374 edge:=(x[i+1],y[i+1])-(x[i],y[i]);
375 %show edge;
376 normedge:=normvec(edge);
377 for j=1 upto 4: %pour chaque point de deuxième triangle on projette sur
378 % l’axe du côté du rectangle 1 qu’on considère
379 _dotprod:=dotproduct((x[j],y[j]),normedge);
380 if(j=1):
381 _minEdge:=_dotprod;
382 _maxEdge:=_dotprod;
383 fi
384 if(_dotprod<_minEdge):
385 _minEdge:=_dotprod;
386 else:
387 if(_dotprod>_maxEdge):
388 _maxEdge:=_dotprod;
389 fi
390 fi
391 %show _minEdge;
392 %show _maxEdge;
393 endfor;
394 %show _minEdge;
395 %show _maxEdge;
396
397 for j=1 upto 4: %pour chaque point de deuxième triangle on projette sur
398 % l’axe du côté du rectangle 1 qu’on considère
399 _dotprod:=dotproduct((a[j],b[j]),normedge);
400 if(j=1):
401 _min:=_dotprod;
402 _max:=_dotprod;
403 fi
404 if(_dotprod<_min):
405 _min:=_dotprod;
406 else:
407 if(_dotprod>_max):
408 _max:=_dotprod;
409 fi
410 fi
411 endfor;
412 %show _min;
413 %show _max;
414 if (_min>_maxEdge) or (_max<_minEdge):
415 %show "OK";
416 _output:=true;
417 fi
418 endfor; %the other way
419 for i=1 upto 2: % on prend deux côtés perpendiculaire du rectangle 1
420 %show "deuxième";
421 %show i;
422 edge:=(a[i+1],b[i+1])-(a[i],b[i]);
423 %show edge;
424 normedge:=normvec(edge);
425 for j=1 upto 4: %pour chaque point de deuxième triangle on projette sur
426 % l’axe du côté du rectangle 1 qu’on considère
427 _dotprod:=dotproduct((a[j],b[j]),normedge);
428 if(j=1):
429 _minEdge:=_dotprod;
430 _maxEdge:=_dotprod;
431 fi
432 if(_dotprod<_minEdge):
433 _minEdge:=_dotprod;
434 else:
435 if(_dotprod>_maxEdge):
436 _maxEdge:=_dotprod;
437 fi
438 fi
439 %show _minEdge;
440 %show _maxEdge;
441 endfor;
442 %show _minEdge;
443 %show _maxEdge;
444
445 for j=1 upto 4: %pour chaque point de deuxième triangle on projette sur
446 % l’axe du côté du rectangle 1 qu’on considère
447 _dotprod:=dotproduct((x[j],y[j]),normedge);
448 if(j=1):
449 _min:=_dotprod;
450 _max:=_dotprod;
451 fi
452 if(_dotprod<_min):
453 _min:=_dotprod;
454 else:
455 if(_dotprod>_max):
456 _max:=_dotprod;
457 fi
458 fi
459 endfor;
460 if (_min>_maxEdge) or (_max<_minEdge):
461 %show "OK";
462 _output:=true;
463 fi
464 endfor;
465 not _output
466 enddef;
467