summaryrefslogtreecommitdiff
path: root/graphics/metapost/contrib/macros/wordcloud/tex/wordcloud.sty
blob: 378a8da3156c51738c8b8282a57a5db00359ac78 (plain)
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                        wordcloud                           %%
%%                    drawing wordclouds                      %%
%%                   with METAPOST and Lua                    %%
%%                chupin@ceremade.dauphine.fr                 %%
%%                Version 0.2 (septembre    2023)             %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% This work may be distributed and/or modified under the conditions of
% the LaTeX Project Public License, either version 1.3c of this license
% or (at your option) any later version.  The latest version of this
% license is in http://www.latex-project.org/lppl.txt
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\NeedsTeXFormat{LaTeX2e}%
\def\PackageName{wordcloud}%
\def\fileversion{v0.2}%
\def\filedate{2023/07/26}%
\ProvidesPackage{wordcloud}[\filedate\space\fileversion]%
%
% package to load
\RequirePackage{luamplib}%
\mplibtextextlabel{enable} % to allow LaTeX in list of words inside LaTeX document
%
% load the lua code
\directlua{require("wordcloud.lua")}%

\ExplSyntaxOn

% variable globale
\tl_new:N \g__wc_wordcloud_scale_tl
\tl_new:N \g__wc_wordcloud_rotate_tl
\tl_new:N \g__wc_wordcloud_margin_tl

% definitions des clés/valeurs pour la commande \wordcloud
\keys_define:nn{wc/wordcloud}{%
  scale .tl_gset:N = \g__wc_wordcloud_scale_tl,
  scale .value_required:n = true,
  rotate .tl_gset:N = \g__wc_wordcloud_rotate_tl,
  rotate .value_required:n = true,
  margin .tl_gset:N = \g__wc_wordcloud_margin_tl,
  margin .value_required:n = true,
  usecolor .bool_set:N = \g__wc_wordcloud_usecolor_bool,
  colors .clist_set:N = \g__wc_wordcloud_colors_clist, % list of LaTeX colors
  colors .value_required:n = true,
%  usecolor .default:n = true,
}

% local variable to stock metapost colors list
\tl_new:N \l_colors_list_tl%

% function to build a wordcloud from a list of the form :
% (word1,weight1);(word2,weight2);etc.
\NewDocumentCommand\wordcloud{ o m }{%
  % mandatory argument : list (word1,weight1);(word2,weight2);etc.
  % optional arguments :
  % scale=<value> (default 1)
  % rotate=<value> (default 0)
  % margin=<value> (default 0.3pt)
  % usecolor=true/false (default false)
  % colors={color1,color2,etc}
  % pour les clés à valeur par défaut
  \keys_set:nn { wc/wordcloud } { scale=1 } %
  \keys_set:nn { wc/wordcloud } { rotate=0 } %
  \keys_set:nn { wc/wordcloud } { margin=0.3pt } %
  \keys_set:nn { wc/wordcloud } { colors={} } %
  \keys_set:nn { wc/wordcloud } { usecolor=false } %

  \tl_clear:N \l_colors_list_tl%
  \IfNoValueF {#1}
  {
      \keys_set:nn { wc/wordcloud } { #1 }
  }

  % parse the list to build a list of rgb colors for metapost code

  \clist_map_variable:NNn \g__wc_wordcloud_colors_clist \l_onecolor_tl {% 
    \extractcolorspecs{\tl_use:N
    \l_onecolor_tl}{\wc_onecolor_type}{\wc_onecolor_values}
    % get the type and values of color (see xcolor doc)
    \convertcolorspec{\wc_onecolor_type}{\wc_onecolor_values}{rgb}\wc_rgb_values
    % convert to rgb (see xcolor loc)
    \tl_gput_right:Nx \l_colors_list_tl {(\wc_rgb_values);} % add value with
    %parenthesis 
  }%


  %\show {coucous}
  \directlua{%
    %tex.sprint("test")
    wc_build_wordcloud("\luaescapestring{\unexpanded{#2}}",\tl_use:N \g__wc_wordcloud_rotate_tl,\tl_use:N \g__wc_wordcloud_scale_tl,"\tl_use:N \g__wc_wordcloud_margin_tl",\bool_if:NTF \g__wc_wordcloud_usecolor_bool{"true"}{"false"},"\tl_use:N \l_colors_list_tl")
  }%
}


% function to build a wordcloud from a text file :
\NewDocumentCommand\wordcloudFile{ o m m}{%
  % two mandatory arguments : name of the file and number of words for the
  % wordcloud 
  % optional arguments :
  % scale=<value> (default 1)
  % rotate=<value> (default 0)
  % margin=<value> (default 0.3pt)
  % usecolor=true/false (default false)
  % colors={color1,color2,etc}

  % pour les clés à valeur par défaut
  \keys_set:nn { wc/wordcloud } { scale=1 } %
  \keys_set:nn { wc/wordcloud } { rotate=0 } %
  \keys_set:nn { wc/wordcloud } { margin=0.3pt } %
  \keys_set:nn { wc/wordcloud } { colors={} } %
  \keys_set:nn { wc/wordcloud } { usecolor=false } %
  \tl_clear:N \l_colors_list_tl%

  \IfNoValueF {#1}
  {
      \keys_set:nn { wc/wordcloud } { #1 }
  }

  % parse the list to build a list of rgb colors for metapost code
  
  \clist_map_variable:NNn \g__wc_wordcloud_colors_clist \l_onecolor_tl {% 
    \extractcolorspecs{\tl_use:N
    \l_onecolor_tl}{\wc_onecolor_type}{\wc_onecolor_values}
    % get the type and values of color (see xcolor doc)
    \convertcolorspec{\wc_onecolor_type}{\wc_onecolor_values}{rgb}\wc_rgb_values
    % convert to rgb (see xcolor loc)
    \tl_gput_right:Nx \l_colors_list_tl {(\wc_rgb_values);} % add value with
    %parenthesis 
  }%

  %\show {coucous}
  \directlua{%
    wc_build_wordcloud_file("#2",#3,\tl_use:N \g__wc_wordcloud_rotate_tl,\tl_use:N \g__wc_wordcloud_scale_tl,"\tl_use:N \g__wc_wordcloud_margin_tl",\bool_if:NTF \g__wc_wordcloud_usecolor_bool{"true"}{"false"},"\tl_use:N \l_colors_list_tl")
  }%
}

% function to add words to ignor when a wordcloud is build from a text file
\NewDocumentCommand\wordcloudIgnoreWords{m}{%
  % mandatory argument : list of words separated by commas 
  % word1, word2, word3, etc.
  \directlua{%
    tab=wc_string_to_tab("#1")
    wc_add_ignored_words(tab)
  }%
}


\ExplSyntaxOff