-
Notifications
You must be signed in to change notification settings - Fork 5
/
textstyle4ht.sty
170 lines (143 loc) · 3.8 KB
/
textstyle4ht.sty
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
161
162
163
164
165
166
167
168
169
170
\ProvidesPackage{textstyle4ht}
\RequirePackage{xcolor}
\RequirePackage{cssframework}
\RequirePackage{xparse}
\ExplSyntaxOn
% some helper macros using LaTeX3
% I know this style mixing is ugly, but I am too used to old school :)
\NewDocumentCommand {\RoundingUpFunction} { m }
{
\fp_eval:n { ceil(#1) }
}
\seq_new:N \ts_tag_stack
\tl_new:N \ts_current_tag
\tl_new:N \ts_current_params
\tl_new:N \ts_default_inline
\tl_new:N \ts_default_block
\NewDocumentCommand {\PushTag}{ m }
{
\seq_gpush:Nx \ts_tag_stack {#1}
}
\NewDocumentCommand {\PopTag}{ }
{
\seq_gpop:NN \ts_tag_stack \ts_current_params
}
\NewDocumentCommand{\DefaultInlineTag}{ m }
{
\tl_set:Nn \ts_default_inline {#1}
}
\NewDocumentCommand{\DefaultBlockTag}{ m }
{
\tl_set:Nn \ts_default_block {#1}
}
\DefaultInlineTag{span}
\DefaultBlockTag{div}
% print an element
\cs_new:Npn\ts_print_tag#1#2#3{
\tl_gset:Nn\ts_current_params{{\ts_current_tag}{#1}{#3}}
\PushTag{\ts_current_params}
#3\HCode{<\ts_current_tag}\HCode{~ class="#1" #2>}
}
\cs_new:Npn\ts_set_tag#1#2{
\IfNoValueTF{#1}{
\tl_gset:Nn \ts_current_tag {#2}
}{
\tl_gset:Nn \ts_current_tag {#1}
}
}
\NewDocumentCommand\TSFontSeries{m m}{
\cs_new:cpn{ts-font-series#1}{#2}
}
\NewDocumentCommand\TSFontShape{m m}{
\cs_new:cpn{ts-font-shape#1}{#2}
}
\cs_new:Npn\tsf@getseries{
\cs_if_exist_use:c{ts-font-series\f@series}
}
\cs_new:Npn\tsf@getshape{
\cs_if_exist_use:c{ts-font-shape\f@shape}
}
\cs_new:Npn\ts_get_tag#1#2#3{#1}
\cs_new:Npn\ts_get_class#1#2#3{#2}
\cs_new:Npn\ts_get_end#1#2#3{#3}
\cs_new:Npn\ts_save_style#1#2#3{
\cs_if_exist_use:cTF{ts_tag_#1_#2}{}
{
\cs_new:cpn{ts_tag_#1_#2}{\relax}
\FontAttr{#1.#2}
}
}
\NewDocumentCommand{\SaveStyle}{}{
\expandafter\ts_save_style\ts_current_params
}
% trick to grab the style inside command which can't be modified
% #1 \InlineTag or \BlockTag with all parameters
% #2 command which we want to grab
\NewDocumentCommand{\GrabStyle}{ m m }{
\setbox0=\hbox{#1#2{\SaveStyle}\EndTag}
\expandafter\Css\expandafter{\css@body}
\typeout{\css@body}
}
% optional element name, class, attributes
\NewDocumentCommand{\InlineTag}{ o m m }
{
\ts_set_tag{#1}{\ts_default_inline}
\ts_print_tag{#2}{#3}{}
}
\NewDocumentCommand{\BlockTag}{ o m m }
{
\ts_set_tag{#1}{\ts_default_block}
\ts_print_tag{#2}{#3}{\ifvmode\IgnorePar\fi\EndP}
\par
}
\NewDocumentCommand{\EndTag}{ }
{
\seq_gpop:NNTF \ts_tag_stack \ts_current_params {
%\PopTag
\SaveStyle
\expandafter\ts_get_end\ts_current_params
\HCode{</\expandafter\ts_get_tag\ts_current_params>}
}{\typeout{textstyle4ht warning: tag stack is empty}}
}
\ExplSyntaxOff
% extract current color as hexadecimal value
\newcommand\tsf@getColor[1][.]{
% colorname `.` holds current color
\extractcolorspec{#1}{\tsf@color}
\expandafter\convertcolorspec\tsf@color{HTML}\tsf@color
%\tmpcolor
}
% write css color for given css selector
\newcommand\CssColor[1]{%
% save current color
\tsf@getColor%
\Css{#1{color:\#\tsf@color;}}%
}
\TSFontShape{n}{font-style:normal;}
\TSFontShape{it}{font-style:italic;}
\TSFontShape{sl}{font-style:oblique;}
\TSFontShape{sc}{font-variant:small-caps;}
\TSFontSeries{m}{font-weight:normal;}
\TSFontSeries{b}{font-weight:bold;}
\TSFontSeries{bx}{font-weight:600;}
\TSFontSeries{sb}{font-weight:900;}
\TSFontSeries{c}{font-stretch: condensed;}
% write css font information of current font face
\newcommand\FontAttr[1]{%
\bgroup\normalsize
\xdef\tsf@normalsize{\RoundingUpFunction{\f@size}}
\egroup
\edef\tsf@fontsize{\strip@pt\dimexpr\f@size pt/\tsf@normalsize\relax rem}
\tsf@getColor
\xdef\css@body{#1{
font-size:\tsf@fontsize;
color: \#\tsf@color;
\tsf@getseries
\tsf@getshape
}}
\expandafter\Css\expandafter{\css@body}
}
\newcommand\DeclareHColor[1]{%
\tsf@getColor[#1]%
\Configure{HColor}{#1}{\#\tsf@color}%
}