summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/tonevalue/tonevalue.sty
blob: cfc01a7a00b05ca67cf860d7ca2662a0dc16b8ae (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
150
151
152
153
154
155
156
157
158
159
160
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{tonevalue}
  [2021/07/25 v1.0 LaTeX Package (Tone value: visualising tone value patterns)]

\RequirePackage{etoolbox}
\RequirePackage{listofitems}
\RequirePackage{xstring}
\RequirePackage{xkeyval}
\RequirePackage{xcolor}
\RequirePackage{tikz}
  \usetikzlibrary{positioning,decorations.markings,arrows}
\RequirePackage{contour}

% package options
  % font command
  \DeclareOptionX{fontcmd}[\sffamily]{\def\toneVisualisationFontCmd{#1}}
  % colors
  \DeclareOptionX{defaultcolors}{
    \definecolor{1}{HTML}{7E2639}
    \definecolor{2}{HTML}{FF98AF}
    \definecolor{3}{HTML}{AD724A}
    \definecolor{4}{HTML}{DE6A1C}
    \definecolor{5}{HTML}{426579}
    \definecolor{6}{HTML}{86B8F3}
    \definecolor{7}{HTML}{769164}
    \definecolor{8}{HTML}{ACC551}
  }
  % contour around numbers
  \DeclareOptionX{draft}{\contournumber{50}}
  \DeclareOptionX{contourlength}[0.075em]{\contourlength{#1}}
  \DeclareOptionX{contournumber}[1000]{\contournumber{#1}}
\ExecuteOptionsX{
  contourlength=0.075em,
  contournumber=1000,
  fontcmd=\sffamily,
}
\ProcessOptionsX*\relax
\ProcessOptionsX\relax

\def\xjoinbycomma<#1#2>{%
  \ifx\relax#1
  \else
    #1,\xjoinbycomma<#2>%
  \fi
}
\def\sendiauToListStr#1{\xjoinbycomma<#1\relax>}

% environment for unt's visualisation approach
\define@key{untVisualisation}{minmax}{\def\untVisualisation@minmax{#1}}
\define@key{untVisualisation}{scale}{\def\untVisualisation@scale{#1}}
\define@key{untVisualisation}{showlabels}{\def\untVisualisation@showlabels{#1}}
\newenvironment{untVisualisation}[1][]{
  \setkeys{untVisualisation}{minmax={1,5}, scale=1, showlabels=false}
  \setkeys{untVisualisation}{#1}
  \toneVisualisationFontCmd
  % mxn minmax
  \pgfmathparse{{\untVisualisation@minmax}[0]}
  \edef\xstart{\pgfmathresult}
  \pgfmathparse{{\untVisualisation@minmax}[1]}
  \edef\xend{\pgfmathresult}
  \pgfmathparse{{\untVisualisation@minmax}[0]}
  \edef\ystart{\pgfmathresult}
  \pgfmathparse{{\untVisualisation@minmax}[1]}
  \edef\yend{\pgfmathresult}
  \tikzpicture[scale=\untVisualisation@scale]
    \begin{scope}[rotate=45, scale=1.4142, line width=0.1em, gray]
      \foreach \x in {\xstart,...,\xend}
        \draw (\x,\ystart) -- (\x,\yend);
      \foreach \y in {\ystart,...,\yend}
        \draw (\xstart,\y) -- (\xend,\y);
    \end{scope}
    \begin{scope}[gray]
      \expandafter\ifstrequal\expandafter{\untVisualisation@showlabels}{true}{
        \node at (0,\ystart*2-0.5) {\large\bfseries\xstart};
        \node at (-\xend+0.6767+\xstart-1,\yend+0.6767+\ystart-1) {\large\bfseries\xend};
        \node at (\xend-0.6767-\xstart+1,\yend+0.6767+\ystart-1) {\large\bfseries\xend};
        \node (xAxisArrowTip) at (-\xend+0.6767+\xstart-1+\xend/2-\xstart/2,\yend+0.6767+\ystart-1-\yend/2+\ystart/2) {};
        \node[below left=1.25em of xAxisArrowTip] (xAxisArrowTail) {};
        \draw[line width=0.125em, -angle 60] (xAxisArrowTail) -- (xAxisArrowTip);
        \node (yAxisArrowTail) at (\xend-0.6767-\xstart+1-\xend/2+\xstart/2,\yend+0.6767+\ystart-1-\yend/2+\ystart/2) {};
        \node[below right=1.25em of yAxisArrowTail] (yAxisArrowTip) {};
        \draw[line width=0.125em, -angle 60] (yAxisArrowTail) -- (yAxisArrowTip);
      }{}
    \end{scope}
}{
  \endtikzpicture
}

% draw a point
\newcommand{\drawuntpoint}[9][]{
  % override tikz options, background color, coordinates, tone name, tone value in numbers, label position
    \node[draw, shape=circle, scale=#9*0.75, inner sep=0.1em, fill, #2, text=white, #1, xshift=#7, yshift=#8]
    ({#5}{#4})
    at ({#3}[0],{#3}[1])
    {#4};
  \node[#2, #6 = 0pt of {#5}{#4}] {\contour{white}{#5}};
}

\newcounter{sumOfPitchHeights}
% keys of options
\define@key{untpoint}{label}{\def\untpoint@label{#1}}
\define@key{untpoint}{tikzoptions}{\def\untpoint@tikzoptions{#1}}
\define@key{untpoint}{bgcolor}{\def\untpoint@bgcolor{#1}}
\define@key{untpoint}{xshift}{\def\untpoint@xshift{#1}}
\define@key{untpoint}{yshift}{\def\untpoint@yshift{#1}}
\define@key{untpoint}{scale}{\def\untpoint@scale{#1}}
\define@key{untpoint}{stem}{\def\untpoint@stem{#1}}
% drawing interface
\newcommand{\untpoint}[3][]{
  % options, tone value in numbers, tone name
  \setkeys{untpoint}{label=above, tikzoptions={}, bgcolor=black, xshift=0pt, yshift=0pt, scale=1, stem=false}
  \setkeys{untpoint}{#1}

  \StrGobbleRight{\sendiauToListStr{#2}}{2}[\sendiaulistStr] % readlist cannot parse trailing comma
  \readlist\sendiaulist{\sendiaulistStr}
  \edef\len{\listlen\sendiaulist[]}

  \ifnum0\len=1
    \drawuntpoint[{\untpoint@tikzoptions}]{\untpoint@bgcolor}{0,{#2}[0]*2}{#3}{#2}
      {\untpoint@label}{\untpoint@xshift}{\untpoint@yshift}{\untpoint@scale}
  \else
    \setcounter{sumOfPitchHeights}{0}
    \pgfmathparse{\len-2}
    % calculate the sum of pitch heights
    \foreach \pitchHeightIndex in {0,...,{\pgfmathresult}} {
      \pgfmathparse{{#2}[\pitchHeightIndex]}
      \addtocounter{sumOfPitchHeights}{\pgfmathresult}
      \pgfmathparse{{#2}[\pitchHeightIndex+1]}
      \addtocounter{sumOfPitchHeights}{\pgfmathresult}
    }
    % draw the point
    \drawuntpoint[\untpoint@tikzoptions]
      {\untpoint@bgcolor}
      {{-({#2}[0])+{#2}[\len-1]},{\thesumOfPitchHeights/(\len-1)}}
      {#3}{#2}
      {\untpoint@label}{\untpoint@xshift}{\untpoint@yshift}{\untpoint@scale}
    % draw the stem
    \expandafter\ifstrequal\expandafter{\untpoint@stem}{true}{
      \draw[\untpoint@bgcolor, line width=0.1em] ({-({#2}[0])+{#2}[\len-1]},{#2}[0]+{#2}[\len-1]) -- ({#2}{#3});
    }{}
  \fi
}

% link points
\define@key{linkuntpoints}{color}{\def\linkuntpoints@color{#1}}
\define@key{linkuntpoints}{bend}{\def\linkuntpoints@bend{#1}}
\newcommand{\linkuntpoints}[3][]{
  \setkeys{linkuntpoints}{color=black, bend={}}
  \setkeys{linkuntpoints}{#1}

  \begin{scope}[
      decoration={
        markings,
        mark=at position 0.5 with {\arrow[scale=0.875]{angle 60}}}
    ]
    \draw[postaction={decorate}, line width=0.15em, \linkuntpoints@color] (#2) to [\linkuntpoints@bend] (#3);
  \end{scope}
}

\endinput