FINAL CUT
foptiattr.h
1 /***********************************************************************
2 * foptiattr.h - Sets video attributes in optimized order *
3 * *
4 * This file is part of the FINAL CUT widget toolkit *
5 * *
6 * Copyright 2016-2023 Markus Gans *
7 * *
8 * FINAL CUT is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as *
10 * published by the Free Software Foundation; either version 3 of *
11 * the License, or (at your option) any later version. *
12 * *
13 * FINAL CUT is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with this program. If not, see *
20 * <http://www.gnu.org/licenses/>. *
21 ***********************************************************************/
22 
23 /* Standalone class
24  * ════════════════
25  *
26  * ▕▔▔▔▔▔▔▔▔▔▔▔▏1 1▕▔▔▔▔▔▔▔▔▔▔▔▔▔▔▏
27  * ▕ FOptiAttr ▏- - - -▕ SGRoptimizer ▏
28  * ▕▁▁▁▁▁▁▁▁▁▁▁▏ ▕▁▁▁▁▁▁▁▁▁▁▁▁▁▁▏
29  */
30 
31 #ifndef FOPTIATTR_H
32 #define FOPTIATTR_H
33 
34 #if !defined (USE_FINAL_H) && !defined (COMPILE_FINAL_CUT)
35  #error "Only <final/final.h> can be included directly."
36 #endif
37 
38 #include <cassert>
39 #include <algorithm> // need for std::swap
40 #include <array>
41 #include <string>
42 
43 #include "final/ftypes.h"
44 #include "final/output/tty/sgr_optimizer.h"
45 #include "final/util/fstring.h"
46 
47 namespace finalcut
48 {
49 
50 //----------------------------------------------------------------------
51 // class FOptiAttr
52 //----------------------------------------------------------------------
53 
54 class FOptiAttr final
55 {
56  public:
58  {
59  const char* on;
60  const char* off;
61  };
62 
64  {
65  const char* a_foreground;
66  const char* a_background;
67  const char* foreground;
68  const char* background;
69  const char* color_pair;
70  const char* orig_pair;
71  const char* orig_colors;
72  int max_color;
73  int attr_without_color;
74  bool ansi_default_color;
75  };
76 
77  struct TermEnv
78  {
79  TermEnvTextStyle t_set_bold;
80  TermEnvTextStyle t_set_dim;
81  TermEnvTextStyle t_set_italics;
82  TermEnvTextStyle t_set_underline;
83  TermEnvTextStyle t_set_blink;
84  TermEnvTextStyle t_set_reverse;
85  TermEnvTextStyle t_set_standout;
86  TermEnvTextStyle t_set_secure;
87  TermEnvTextStyle t_set_protected;
88  TermEnvTextStyle t_set_crossed_out;
89  TermEnvTextStyle t_set_dbl_underline;
90  TermEnvTextStyle t_set_attributes;
91  TermEnvTextStyle t_set_alt_charset;
92  TermEnvTextStyle t_set_pc_charset;
93  TermEnvColorStyle t_set_color;
94  };
95 
96  // Constructor
97  FOptiAttr();
98 
99  // Accessors
100  auto getClassName() const -> FString;
101  static auto getInstance() -> FOptiAttr&;
102 
103  // Mutators
104  void setTermEnvironment (const TermEnv&);
105  void setMaxColor (const int&) noexcept;
106  void setNoColorVideo (int) noexcept;
107  void setDefaultColorSupport() noexcept;
108  void unsetDefaultColorSupport() noexcept;
109  void set_enter_bold_mode (const char[]);
110  void set_exit_bold_mode (const char[]);
111  void set_enter_dim_mode (const char[]);
112  void set_exit_dim_mode (const char[]);
113  void set_enter_italics_mode (const char[]);
114  void set_exit_italics_mode (const char[]);
115  void set_enter_underline_mode (const char[]);
116  void set_exit_underline_mode (const char[]);
117  void set_enter_blink_mode (const char[]);
118  void set_exit_blink_mode (const char[]);
119  void set_enter_reverse_mode (const char[]);
120  void set_exit_reverse_mode (const char[]);
121  void set_enter_secure_mode (const char[]);
122  void set_exit_secure_mode (const char[]);
123  void set_enter_protected_mode (const char[]);
124  void set_exit_protected_mode (const char[]);
125  void set_enter_crossed_out_mode (const char[]);
126  void set_exit_crossed_out_mode (const char[]);
127  void set_enter_dbl_underline_mode (const char[]);
128  void set_exit_dbl_underline_mode (const char[]);
129  void set_enter_standout_mode (const char[]);
130  void set_exit_standout_mode (const char[]);
131  void set_set_attributes (const char[]);
132  void set_exit_attribute_mode (const char[]);
133  void set_enter_alt_charset_mode (const char[]);
134  void set_exit_alt_charset_mode (const char[]);
135  void set_enter_pc_charset_mode (const char[]);
136  void set_exit_pc_charset_mode (const char[]);
137  void set_a_foreground_color (const char[]);
138  void set_a_background_color (const char[]);
139  void set_foreground_color (const char[]);
140  void set_background_color (const char[]);
141  void set_term_color_pair (const char[]);
142  void set_orig_pair (const char[]);
143  void set_orig_colors (const char[]);
144 
145  // Inquiry
146  static auto isNormal (const FChar&) -> bool;
147 
148  // Methods
149  void initialize();
150  static auto vga2ansi (FColor) -> FColor;
151  auto changeAttribute (FChar&, FChar&) -> std::string;
152 
153  private:
154  struct Capability
155  {
156  const char* cap{};
157  bool caused_reset{};
158  };
159 
160  struct TextStyle
161  {
162  Capability on{};
163  Capability off{};
164  };
165 
166  struct ColorStyle
167  {
168  Capability a_foreground{};
169  Capability a_background{};
170  Capability foreground{};
171  Capability background{};
172  Capability color_pair{};
173  Capability orig_pair{};
174  Capability orig_colors{};
175  int attr_without_color{0};
176  int max_color{1};
177  bool monochron{true};
178  bool ansi_default_color{false};
179  };
180 
181  struct AttributeChanges
182  {
183  FChar on{};
184  FChar off{};
185  };
186 
187  // Using-declarations
188  using SetFunctionCall = std::function<bool(FOptiAttr*, FChar&)>;
189 
190  struct AttributeHandlerEntry
191  {
192  FAttribute mask;
193  SetFunctionCall function;
194  };
195 
196  using AttributeHandlers = std::array<AttributeHandlerEntry, 13>;
197  using NoColorVideoHandler = std::function<void(FOptiAttr*, FChar&)>;
198  using NoColorVideoHandlerTable = std::array<NoColorVideoHandler, 18>;
199 
200  // Enumerations
201  enum init_reset_tests
202  {
203  no_test = 0x00,
204  test_ansi_reset = 0x01, // ANSI X3.64 terminal
205  test_adm3_reset = 0x02, // Lear Siegler ADM-3 terminal
206  same_like_ue = 0x04,
207  same_like_se = 0x08,
208  same_like_me = 0x10,
209  all_tests = 0x1f
210  };
211 
212  // Mutators
213  auto setTermBold (FChar&) -> bool;
214  auto unsetTermBold (FChar&) -> bool;
215  auto setTermDim (FChar&) -> bool;
216  auto unsetTermDim (FChar&) -> bool;
217  auto setTermItalic (FChar&) -> bool;
218  auto unsetTermItalic (FChar&) -> bool;
219  auto setTermUnderline (FChar&) -> bool;
220  auto unsetTermUnderline (FChar&) -> bool;
221  auto setTermBlink (FChar&) -> bool;
222  auto unsetTermBlink (FChar&) -> bool;
223  auto setTermReverse (FChar&) -> bool;
224  auto unsetTermReverse (FChar&) -> bool;
225  auto setTermStandout (FChar&) -> bool;
226  auto unsetTermStandout (FChar&) -> bool;
227  auto setTermInvisible (FChar&) -> bool;
228  auto unsetTermInvisible (FChar&) -> bool;
229  auto setTermProtected (FChar&) -> bool;
230  auto unsetTermProtected (FChar&) -> bool;
231  auto setTermCrossedOut (FChar&) -> bool;
232  auto unsetTermCrossedOut (FChar&) -> bool;
233  auto setTermDoubleUnderline (FChar&) -> bool;
234  auto unsetTermDoubleUnderline (FChar&) -> bool;
235  auto setTermAttributes (FChar&, const TCapAttributes&) -> bool;
236  auto unsetTermAttributes (FChar&) -> bool;
237  auto setTermAltCharset (FChar&) -> bool;
238  auto unsetTermAltCharset (FChar&) -> bool;
239  auto setTermPCcharset (FChar&) -> bool;
240  auto unsetTermPCcharset (FChar&) -> bool;
241  auto setTermDefaultColor (FChar&) -> bool;
242  void setAttributesOn (FChar&);
243  void setAttributesOff (FChar&);
244  void setAttributes ( const FAttribute&
245  , const AttributeHandlers&, FChar& );
246 
247  // Inquiries
248  static auto hasColor (const FChar&) -> bool;
249  static auto hasAttribute (const FChar&) -> bool;
250  static auto hasNoAttribute (const FChar&) -> bool;
251  auto isItalicsUsed (const FChar&, const FChar&) const -> bool;
252  auto isCrossedOutUsed (const FChar&, const FChar&) const -> bool;
253  auto isDoubleUnderlineUsed (const FChar&, const FChar&) const -> bool;
254  auto isPCcharsetUsed (const FChar&, const FChar&) const -> bool;
255  auto isPCcharsetUsable (FChar&, const FChar&) -> bool;
256  auto hasColorChanged (const FChar&, const FChar&) const -> bool;
257 
258  // Methods
259  void resetColor (FChar&) const;
260  void prevent_no_color_video_attributes (FChar&, bool = false);
261  void deactivateAttributes (FChar&, FChar&);
262  void changeAttributeSGR (FChar&, FChar&);
263  void changeAttributeSeparately (FChar&, FChar&);
264  void change_color (FChar&, FChar&);
265  void normalizeColor (FColor&) const noexcept;
266  void handleDefaultColors (FChar&, FChar&, FColor&, FColor&);
267  void change_to_default_color (FChar&, FChar&, FColor&, FColor&);
268  void setDefaultForeground (FChar&);
269  void setDefaultBackground (FChar&);
270  void change_current_color (const FChar&, const FColor, const FColor);
271  void resetAttribute (FChar&) const;
272  void reset (FChar&) const;
273  auto caused_reset_attributes (const char[], uChar = all_tests) const -> bool;
274  void init_reset_attribute (Capability&, uChar = all_tests) const;
275  auto fake_reverse_color_change (const FChar& term) const -> bool;
276  auto hasCharsetEquivalence() const -> bool;
277  static auto getNoColorVideoHandlerTable() -> const NoColorVideoHandlerTable&;
278  static auto getAttributeOnHandlers() -> const AttributeHandlers&;
279  static auto getAttributeOffHandlers() -> const AttributeHandlers&;
280  static auto getByte0ReverseMask() -> uInt8;
281  static auto getByte1Mask() -> uInt8;
282  static auto getByte1ResetMask() -> uInt8;
283  static auto getByte2ResetMask() -> uInt8;
284  void detectSwitchOn (const FChar&, const FChar&);
285  void detectSwitchOff (const FChar&, const FChar&);
286  auto switchOn() const -> bool;
287  auto switchOff() const -> bool;
288  template <typename CharT
289  , enable_if_char_ptr_t<CharT> = nullptr>
290  auto append_sequence (CharT) -> bool;
291  template <typename CharT
292  , enable_if_char_array_t<CharT> = nullptr>
293  auto append_sequence (CharT) -> bool;
294  auto append_sequence (const std::string&) -> bool;
295 
296  // Data members
297  TextStyle F_bold{};
298  TextStyle F_dim{};
299  TextStyle F_italics{};
300  TextStyle F_underline{};
301  TextStyle F_blink{};
302  TextStyle F_reverse{};
303  TextStyle F_standout{};
304  TextStyle F_secure{};
305  TextStyle F_protected{};
306  TextStyle F_crossed_out{};
307  TextStyle F_dbl_underline{};
308  TextStyle F_attributes{};
309  TextStyle F_alt_charset{};
310  TextStyle F_pc_charset{};
311  ColorStyle F_color{};
312 
313  AttributeChanges changes{};
314  std::string attr_buf{};
315  SGRoptimizer sgr_optimizer{attr_buf};
316  bool alt_equal_pc_charset{false};
317  bool fake_reverse{false};
318 };
319 
320 
321 // FOptiAttr inline functions
322 //----------------------------------------------------------------------
323 inline auto FOptiAttr::getClassName() const -> FString
324 { return "FOptiAttr"; }
325 
326 //----------------------------------------------------------------------
327 inline void FOptiAttr::setMaxColor (const int& c) noexcept
328 { F_color.max_color = c; }
329 
330 //----------------------------------------------------------------------
331 inline void FOptiAttr::setNoColorVideo (int attr) noexcept
332 { F_color.attr_without_color = attr; }
333 
334 //----------------------------------------------------------------------
335 inline void FOptiAttr::setDefaultColorSupport() noexcept
336 { F_color.ansi_default_color = true; }
337 
338 //----------------------------------------------------------------------
339 inline void FOptiAttr::unsetDefaultColorSupport() noexcept
340 { F_color.ansi_default_color = false; }
341 
342 //----------------------------------------------------------------------
343 template <typename CharT
344  , enable_if_char_ptr_t<CharT>>
345 inline auto FOptiAttr::append_sequence (CharT seq) -> bool
346 {
347  // for char* and const char*
348  return seq
349  ? append_sequence(std::string(seq))
350  : false;
351 }
352 
353 //----------------------------------------------------------------------
354 template <typename CharT
355  , enable_if_char_array_t<CharT>>
356 inline auto FOptiAttr::append_sequence (CharT seq) -> bool
357 {
358  // for char[] and const char[]
359  return append_sequence(std::string(seq));
360 }
361 
362 } // namespace finalcut
363 
364 #endif // FOPTIATTR_H
Definition: class_template.cpp:25
Definition: foptiattr.h:57
Definition: ftypes.h:423
Definition: foptiattr.h:54
Definition: ftypes.h:260
Definition: fstring.h:79
Definition: foptiattr.h:77
Definition: ftypes.h:370
Definition: sgr_optimizer.h:49
Definition: foptiattr.h:63