File: css.cpp

package info (click to toggle)
bibledit-cloud 5.1.036-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 250,636 kB
  • sloc: xml: 915,934; ansic: 261,349; cpp: 92,628; javascript: 32,542; sh: 4,915; makefile: 586; php: 69
file content (315 lines) | stat: -rw-r--r-- 7,799 bytes parent folder | download | duplicates (5)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 Copyright (©) 2003-2025 Teus Benschop.
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */


#include <filter/css.h>
#include <filter/url.h>
#include <filter/string.h>
#include <filter/md5.h>
#include <config/globals.h>
#include <webserver/request.h>
#include <styles/logic.h>


std::string Filter_Css::directionUnspecified (int value)
{
  value = value % 10;
  if (value == 0) return "checked";
  else return std::string();
}


std::string Filter_Css::directionLeftToRight (int value)
{
  value = value % 10;
  if (value == 1) return "checked";
  else return std::string();
}


std::string Filter_Css::directionRightToLeft (int value)
{
  value = value % 10;
  if (value == 2) return "checked";
  else return std::string();
}


std::string Filter_Css::ltr ()
{
  return "ltr";
}


std::string Filter_Css::rtl ()
{
  return "rtl";
}


int Filter_Css::directionValue (std::string direction)
{
  if (direction == ltr ()) return 1;
  if (direction == rtl ()) return 2;
  return 0;
}


std::string Filter_Css::writingModeUnspecified (int value)
{
  value = value / 10;
  value = value % 10;
  if (value == 0) return "checked";
  else return std::string();
}


std::string Filter_Css::writingModeTopBottomLeftRight (int value)
{
  value = value / 10;
  value = value % 10;
  if (value == 1) return "checked";
  else return std::string();
}


std::string Filter_Css::writingModeTopBottomRightLeft (int value)
{
  value = value / 10;
  value = value % 10;
  if (value == 2) return "checked";
  else return std::string();
}


std::string Filter_Css::writingModeBottomTopLeftRight (int value)
{
  value = value / 10;
  value = value % 10;
  if (value == 3) return "checked";
  else return std::string();
}


std::string Filter_Css::writingModeBottomTopRightLeft (int value)
{
  value = (value / 10);
  value = value % 10;
  if (value == 4) return "checked";
  else return std::string();
}


std::string Filter_Css::tb_lr ()
{
  return "tb-lr";
}


std::string Filter_Css::tb_rl ()
{
  return "tb-rl";
}


std::string Filter_Css::bt_lr ()
{
  return "bt-lr";
}


std::string Filter_Css::bt_rl ()
{
  return "bt-rl";
}


int Filter_Css::writingModeValue (std::string mode)
{
  if (mode == tb_lr ()) return 1;
  if (mode == tb_rl ()) return 2;
  if (mode == bt_lr ()) return 3;
  if (mode == bt_rl ()) return 4;
  return 0;
}


// The purpose of the function is to convert the name of the bible into a string
// that is acceptable as a class identifier in HTML.
// Since a bible can contain any Unicode character,
// just using the bible as the class identifier will not work.
// The function solves that.
std::string Filter_Css::getClass (std::string bible)
{
  std::string classs = md5 (bible);
  classs = classs.substr (0, 6);
  classs = "custom" + classs;
  return classs;
}


// This function produces CSS based on input.
// class: The class for the CSS.
// font: The name or URL of the font to use. It may be empty.
// directionvalue: The value for the text direction.
// $lineheigh: Value in percents.
// $letterspacing: Value multiplied by 10, in pixels.
std::string Filter_Css::get_css (std::string class_, std::string font, int directionvalue, int lineheight, int letterspacing)
{
  std::vector <std::string> css;
  
  // If the font has a URL, then it is a web font.
  if ((font != filter_url_basename_web (font)) && !font.empty()) {
    css.push_back ("@font-face");
    css.push_back ("{");
    css.push_back ("font-family: " + class_ + ";");
    css.push_back ("src: url(" + font + ");");
    css.push_back ("}");
    // Below, properly reference the above web font as the class.
    font = class_;
  }
  
  css.push_back ("." + class_);
  css.push_back ("{");
  
  if (font != "") {
    css.push_back ("font-family: " + font + ";");
  }
  
  int direction = directionvalue % 10;
  
  if (direction > 0) {
    std::string line = "direction: ";
    if (direction == 2) line += rtl ();
    else line += ltr ();
    line += ";";
    css.push_back (line);
  }
  
  int mode = directionvalue / 10;
  mode = mode % 10;
  
  if (mode > 0) {
    std::string line = "writing-mode: ";
    switch (mode) {
      case 1: line += tb_lr (); break;
      case 2: line += tb_rl (); break;
      case 3: line += bt_lr (); break;
      case 4: line += bt_rl (); break;
      default: line += tb_lr (); break;
    }
    line += ";";
    css.push_back (line);
  }
  
  if (lineheight != 100) {
    std::string line = "line-height: " + std::to_string(lineheight) + "%;";
    css.push_back (line);
  }
  
  if (letterspacing != 0) {
    float value = static_cast <float> (letterspacing / 10);
    std::string line = "letter-spacing: " + filter::strings::convert_to_string (value) + "px;";
    css.push_back (line);
  }
  
  css.push_back ("}");
  
  return filter::strings::implode (css, "\n");
}


void Filter_Css::distinction_set_basic ()
{
  return;
}


std::string Filter_Css::distinction_set_light (int itemstyleindex)
{
  if (itemstyleindex == 0) return "light-background";
  if (itemstyleindex == 1) return "light-menu-tabs";
  if (itemstyleindex == 2) return "light-editor";
  if (itemstyleindex == 3) return "light-active-editor";
  if (itemstyleindex == 4) return "light-workspacewrapper";
  return std::string();
}


std::string Filter_Css::distinction_set_dark (int itemstyleindex)
{
  if (itemstyleindex == 0) return "dark-background";
  if (itemstyleindex == 1) return "dark-menu-tabs";
  if (itemstyleindex == 2) return "dark-editor";
  if (itemstyleindex == 3) return "dark-active-editor";
  if (itemstyleindex == 4) return "dark-workspacewrapper";
  if (itemstyleindex == 5) return "dark-versebeam";
  return std::string();
}


std::string Filter_Css::distinction_set_redblue_light (int itemstyleindex)
{
  std::string standard_light = distinction_set_light (itemstyleindex);
  if (itemstyleindex == 1) {
    return standard_light = "redblue-menu-tabs";
  } else {
    return standard_light;
  }
}


std::string Filter_Css::distinction_set_redblue_dark (int itemstyleindex)
{
  std::string standard_dark = distinction_set_dark (itemstyleindex);
  if (itemstyleindex == 1) {
    return standard_dark = "redblue-menu-tabs";
  } else {
    return standard_dark;
  }
}


std::string Filter_Css::distinction_set_notes (int itemstyleindex)
{
  if (itemstyleindex == 0) return "note-status-new";
  if (itemstyleindex == 1) return "note-status-pending";
  if (itemstyleindex == 2) return "note-status-inprogress";
  if (itemstyleindex == 3) return "note-status-done";
  if (itemstyleindex == 4) return "note-status-reopened";
  if (itemstyleindex == 5) return "note-status-unset";
  return std::string();
}


std::string Filter_Css::theme_picker (int indexnumber, int itemstyleindex)
{
  if (indexnumber == 0) distinction_set_basic ();
  if (indexnumber == 1) return distinction_set_light (itemstyleindex);
  if (indexnumber == 2) return distinction_set_dark (itemstyleindex);
  if (indexnumber == 3) return distinction_set_redblue_light (itemstyleindex);
  if (indexnumber == 4) return distinction_set_redblue_dark (itemstyleindex);
  return std::string();
}


std::string filter_css_grey_background ()
{
  return R"(style="background-color: #CCCCCC")";
}