summaryrefslogtreecommitdiff
path: root/src/fl_draw.cxx
blob: 652f46058b1db770e72951eb1130b6020cd03fbe (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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
//
// Label drawing code for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2023 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file.  If this
// file is missing or damaged, see the license at:
//
//     https://www.fltk.org/COPYING.php
//
// Please see the following page on how to report bugs and issues:
//
//     https://www.fltk.org/bugs.php
//

// Implementation of fl_draw(const char*,int,int,int,int,Fl_Align)
// Used to draw all the labels and text, this routine:
// Word wraps the labels to fit into their bounding box.
// Breaks them into lines at the newlines.
// Expands all unprintable characters to ^X or \nnn notation
// Aligns them against the inside of the box.

#include <FL/fl_utf8.h>
#include <FL/Fl.H>
#include <FL/fl_draw.H>
#include <FL/Fl_Image.H>
#include <FL/platform.H>        // fl_open_display()

#include "flstring.h"
#include "fl_oxy.h"

#include <ctype.h>
#include <math.h>
#include <stdlib.h>

char fl_draw_shortcut;  // set by fl_labeltypes.cxx

static char* underline_at;

/*
 Extract the part of text that fits into the given maximum width.

 If called with maxbuf==0, use an internally allocated buffer and enlarge it as needed.
 Otherwise, use buf as buffer but don't go beyond its length of maxbuf.

 \param[in] from input text, can contain '\n' and single or double '\@' characters
 \param[out] buf buffer for the output text segment
 \param[in] maxbuf size of the buffer, or 0 to use the internal buffer allocated in this function
 \param[in] maxw maximum width in pixels of the output text
 \param[out] n number of characters in the output text segment
 \param[out] width actual width in pixels of the output text
 \param[in] wrap if true, wrap at maxw, else wrap at newlines
 \param[in] draw_symbols if true, double '\@' characters are escaped into a single '@'

 \return pointer to the next character in the input text
 */
static const char* expand_text_(const char* from, char*& buf, int maxbuf, double maxw, int& n,
               double &width, int wrap, int draw_symbols) {
  // Reset underline_at to null
  underline_at = NULL;

  // Initialize the total width to 0
  double w = 0;

  // Check if the caller wants to use the internal buffer
  static int l_local_buff = 500;
  static char *local_buf = (char*)malloc(l_local_buff); // initial buffer allocation

  // Calculate the end pointer of the buffer
  char* e;
  if (maxbuf == 0) {
    buf = local_buf;
    e = buf + l_local_buff - 4;
  } else {
    e = buf+(maxbuf-4);
  }

  // Initialize the output pointer to the buffer
  char* o = buf;

  // Initialize the word end pointer that points into the `out` buffer
  char* word_end = o;

  // Initialize the word start pointer that points into the `from` buffer
  const char* word_start = from;

  // Iterate over the input text
  const char* p = from;
  for (;; p++) {
    int c = *p & 255;

    // Check for end of line, space, or '\n'
    if (!c || c == ' ' || c == '\n') {
      // Check for word wrap
      if (word_start < p && wrap) {
        // Calculate the new width
        double newwidth = w + fl_width(word_end, (int) (o-word_end) );

        // Check if the new width exceeds the maximum width
        if (word_end > buf && int(newwidth) > maxw) { // break before this word
          o = word_end;
          p = word_start;
          break;
        }
        // Update the word end pointer
        word_end = o;
        w = newwidth;
      }

      // Check for end of line
      if (!c) break;
      else if (c == '\n') {p++; break;}

      // Update the word start pointer
      word_start = p+1;
    }

    // Check if the buffer needs to be enlarged
    if (o > e) {
      if (maxbuf) break; // don't overflow buffer
      l_local_buff += int(o - e) + 200; // enlarge buffer
      size_t delta_o = (o - local_buf);
      size_t delta_end = (word_end - local_buf);
      local_buf = (char*)realloc(local_buf, l_local_buff);
      buf = local_buf;
      e = local_buf + l_local_buff - 4; // update pointers to buffer content
      o = local_buf + delta_o;
      word_end = local_buf + delta_end;
    }

    // Process the character based on its type
    if (c == '\t') {
      // Process tab character
      for (c = fl_utf_nb_char((uchar*)buf, (int) (o-buf) )%8; c<8 && o<e; c++)
           *o++ = ' ';
    } else if (c == '&' && fl_draw_shortcut && *(p+1)) {
      // Process '&' character
      if (*(p+1) == '&') {p++; *o++ = '&';}
      else if (fl_draw_shortcut != 2) underline_at = o;
    } else if (c < ' ' || c == 127) { // ^X
      // Process control characters
      *o++ = '^';
      *o++ = c ^ 0x40;
    } else if (c == '@' && draw_symbols) { // Symbol???
      // Process '@' character
      if (p[1] && p[1] != '@')  break;
      *o++ = c;
      if (p[1]) p++;
    } else {
      // Process regular characters
      *o++ = c;
    }
  }

  // Calculate the final width
  width = w + fl_width(word_end, (int) (o-word_end));

  // Add the null terminator and set the number of characters
  *o = 0;
  n = (int)(o-buf);

  return p;
}

/**
 Copy \p from to \p buf, replacing control characters with ^X.

 Stop at a newline or if \p maxbuf characters written to buffer.
 Also word-wrap if width exceeds maxw.
 Returns a pointer to the start of the next line of characters.
 Sets n to the number of characters put into the buffer.
 Sets width to the width of the string in the \ref drawing_fl_font "current font".

 \param[in] from input text, can contain '\n' and single or double '\@' characters
 \param[out] buf buffer for the output text segment
 \param[in] maxbuf size of the buffer, or 0 to use the internal buffer allocated in this function
 \param[in] maxw maximum width in pixels of the output text
 \param[out] n number of characters in the output text segment
 \param[out] width actual width in pixels of the output text
 \param[in] wrap if true, wrap at maxw, else wrap at newlines
 \param[in] draw_symbols if true, double '\@' characters are escaped into a single '@'

 \return pointer to the next character in the input text
 */
const char*
fl_expand_text(const char* from, char* buf, int maxbuf, double maxw, int& n,
               double &width, int wrap, int draw_symbols) {
  return expand_text_(from,  buf, maxbuf, maxw,  n, width,  wrap,  draw_symbols);
}

// Caution: put the documentation next to the function's declaration in fl_draw.H for Doxygen
// to see default argument values.
void fl_draw(
    const char* str,    // the (multi-line) string
    int x, int y, int w, int h, // bounding box
    Fl_Align align,
    void (*callthis)(const char*,int,int,int),
    Fl_Image* img, int draw_symbols, int spacing)
{
  char *linebuf = NULL;       // Pointer to a buffer managed by expand_text_
  const char* p;              // Scratch pointer into text, multiple use
  const char* e;              // Scratch pointer into text, multiple use
  int buflen;                 // Number of bytes copied into linebuf
                              // by expand_text_
  char symbol[2][255];        // Copy of symbol text at start and end of str
  int symwidth[2];            // Width and height of symbols (always square)
  int symoffset;
  int symtotal;               // Combined width of both sybols
  int imgtotal;               // Width of image if image is to the left or right
  int imgvert = ((align&FL_ALIGN_IMAGE_NEXT_TO_TEXT)==0); // True if image is
                              // above or below text
  int lines;                  // Number of text lines including '\n' and wrapping
  double width;               // width of the longest text line
  int height = fl_height();   // Height of a line of text


  // If the image is set as a backdrop, ignore it in this function
  if (img && (align & FL_ALIGN_IMAGE_BACKDROP)) img = 0;

  symbol[0][0] = '\0';
  symwidth[0]  = 0;
  symbol[1][0] = '\0';
  symwidth[1]  = 0;

  // Find the symbol at the start and end of the string
  if (draw_symbols) {
    if (str && str[0] == '@' && str[1] && str[1] != '@') {
      char *symptr;
      // Start with a symbol...
      for (symptr = symbol[0];
           *str && !isspace(*str) && symptr < (symbol[0] + sizeof(symbol[0]) - 1);
           *symptr++ = *str++) {/*empty*/}
      *symptr = '\0';
      if (isspace(*str)) str++;
      symwidth[0] = (w < h ? w : h);
    }

    if (str && (p = strrchr(str, '@')) != NULL && p > (str + 1) && p[-1] != '@') {
      strlcpy(symbol[1], p, sizeof(symbol[1]));
      symwidth[1] = (w < h ? w : h);
    }
  }

  // Width and height of both symbols combined
  symtotal = symwidth[0] + symwidth[1];
  // Image width if image is to the left or right of the text, else 0
  imgtotal = (img && (align&FL_ALIGN_IMAGE_NEXT_TO_TEXT)) ? img->w() + spacing : 0;

  int strw = 0;               // Width of text only without symbols
  int strh;                   // Height of text only without symbols

  // Count how many lines and put the last one into the buffer:
  if (str) {
    for (p = str, lines=0; p;) {
      e = expand_text_(p, linebuf, 0, w - symtotal - imgtotal, buflen, width,
                         align&FL_ALIGN_WRAP, draw_symbols);
      if (strw<width) strw = (int)width;
      lines++;
      if (!*e || (*e == '@' && e[1] != '@' && draw_symbols)) break;
      p = e;
    }
  } else lines = 0;

  // Fix the size of the symbols if there is at least one line of text to print
  if (lines) {
    if (symwidth[0]) symwidth[0] = lines * height;
    if (symwidth[1]) symwidth[1] = lines * height;
  }

  // Width and height of both symbols combined
  symtotal = symwidth[0] + symwidth[1];
  // Height of text only
  strh = lines * fl_height();

  // Figure out vertical position of the first element
  int xpos;                   // Position of image or text
  int ypos;                   // Position of image or text
  int imgh = img && imgvert ? img->h() + spacing : 0; // Height of image if image is above or below text
  int imgw[2] = {0, 0};       // Width of image on the left and right side of the text

  symoffset = 0;

  // Figure out vertical position of the first line of text or top image
  if (align & FL_ALIGN_BOTTOM) {
    ypos = y+h-(lines-1)*height-imgh;
  } else if (align & FL_ALIGN_TOP) {
    ypos = y+height;
  } else {
    ypos = y+(h-lines*height-imgh)/2+height;
  }

  // Draw the image if located *above* the text
  if (img && imgvert && !(align & FL_ALIGN_TEXT_OVER_IMAGE)) {
    if (img->w() > symoffset) symoffset = img->w();

    if (align & FL_ALIGN_LEFT) {
      xpos = x + symwidth[0];
    } else if (align & FL_ALIGN_RIGHT) {
      xpos = x + w - img->w() - symwidth[1];
    } else {
      xpos = x + (w - img->w() - symtotal) / 2 + symwidth[0];
    }

    img->draw(xpos, ypos - height);
    ypos += img->h() + spacing;
  }

  // Draw the image if either on the *left* or *right* side of the text
  if (img && !imgvert) {
    if (align & FL_ALIGN_TEXT_OVER_IMAGE) {
      // Image is to the right of the text
      imgw[1] = img->w() + spacing;
      // Find the horizontal position of the image
      if (align & FL_ALIGN_LEFT) {
        xpos = x + symwidth[0] + strw + 1;
      } else if (align & FL_ALIGN_RIGHT) {
        xpos = x + w - symwidth[1] - imgw[1] + 1;
      } else {
        xpos = x + (w - strw - symtotal - imgw[1]) / 2 + symwidth[0] + strw + 1;
      }
      xpos += spacing;
    } else {
      // Image is to the left of the text
      imgw[0] = img->w() + spacing;
      // Find the horizontal position of the image
      if (align & FL_ALIGN_LEFT) {
        xpos = x + symwidth[0] - 1;
      } else if (align & FL_ALIGN_RIGHT) {
        xpos = x + w - symwidth[1] - strw - imgw[0] - 1;
      } else {
        xpos = x + (w - strw - symtotal - imgw[0]) / 2 - 1;
      }
    }
    // Find the vertical position of the image
    int yimg;
    if (align & FL_ALIGN_TOP) {
      yimg = ypos - height;
    } else if (align & FL_ALIGN_BOTTOM) {
      yimg = ypos - height + strh - img->h() - 1;
    } else {
      yimg = ypos - height + (strh - img->h() - 1) / 2;
    }
    // Draw the image
    img->draw(xpos, yimg);
  }

  // Now draw all the text lines
  if (str) {
    int desc = fl_descent();
    for (p=str; ; ypos += height) {
      if (lines>1) {
        e = expand_text_(p, linebuf, 0, w - symtotal - imgtotal, buflen,
                         width, align&FL_ALIGN_WRAP, draw_symbols);
      } else {
        e = "";
      }

      if (width > symoffset) symoffset = (int)(width + 0.5);

      if (align & FL_ALIGN_LEFT) {
        xpos = x + symwidth[0] + imgw[0];
      } else if (align & FL_ALIGN_RIGHT) {
        xpos = x + w - (int)(width + .5) - symwidth[1] - imgw[1];
      } else {
        xpos = x + (w - (int)(width + .5) - symtotal - imgw[0] - imgw[1]) / 2 + symwidth[0] + imgw[0];
      }

      callthis(linebuf,buflen,xpos,ypos-desc);

      if (underline_at && underline_at >= linebuf && underline_at < (linebuf + buflen))
        callthis("_",1,xpos+int(fl_width(linebuf,(int) (underline_at-linebuf))),ypos-desc);

      if (!*e || (*e == '@' && e[1] != '@')) break;
      p = e;
    }
  }

  // Draw the image if the image is *below* the text
  if (img && imgvert && (align & FL_ALIGN_TEXT_OVER_IMAGE)) {
    if (img->w() > symoffset) symoffset = img->w();

    if (align & FL_ALIGN_LEFT) {
      xpos = x + symwidth[0];
    } else if (align & FL_ALIGN_RIGHT) {
      xpos = x + w - img->w() - symwidth[1];
    } else {
      xpos = x + (w - img->w() - symtotal) / 2 + symwidth[0];
    }

    img->draw(xpos, ypos + spacing);
  }

  // Draw the symbols, if any...
  if (symwidth[0]) {
    // Draw the leading symbol to the left of the text
    if (align & FL_ALIGN_LEFT) {
      xpos = x;
    } else if (align & FL_ALIGN_RIGHT) {
      xpos = x + w - symtotal - symoffset;
    } else {
      xpos = x + (w - symoffset - symtotal) / 2;
    }

    if (align & FL_ALIGN_BOTTOM) {
      ypos = y + h - symwidth[0];
    } else if (align & FL_ALIGN_TOP) {
      ypos = y;
    } else {
      ypos = y + (h - symwidth[0]) / 2;
    }

    fl_draw_symbol(symbol[0], xpos, ypos, symwidth[0], symwidth[0], fl_color());
  }

  if (symwidth[1]) {
    // Draw the trailing symbol to the right of the text
    if (align & FL_ALIGN_LEFT) {
      xpos = x + symoffset + symwidth[0];
    } else if (align & FL_ALIGN_RIGHT) {
      xpos = x + w - symwidth[1];
    } else {
      xpos = x + (w - symoffset - symtotal) / 2 + symoffset + symwidth[0];
    }

    if (align & FL_ALIGN_BOTTOM) {
      ypos = y + h - symwidth[1];
    } else if (align & FL_ALIGN_TOP) {
      ypos = y;
    } else {
      ypos = y + (h - symwidth[1]) / 2;
    }

    fl_draw_symbol(symbol[1], xpos, ypos, symwidth[1], symwidth[1], fl_color());
  }
}

// Caution: put the documentation next to the function's declaration in fl_draw.H for Doxygen
// to see default argument values.
void fl_draw(
  const char* str,
  int x, int y, int w, int h,
  Fl_Align align,
  Fl_Image* img,
  int draw_symbols,
  int spacing)
{
  if ((!str || !*str) && !img) return;
  if (w && h && !fl_not_clipped(x, y, w, h) && (align & FL_ALIGN_INSIDE)) return;
  if (align & FL_ALIGN_CLIP)
    fl_push_clip(x, y, w, h);
  fl_draw(str, x, y, w, h, align, fl_draw, img, draw_symbols, spacing);
  if (align & FL_ALIGN_CLIP)
    fl_pop_clip();
}

/**
  Measure how wide and tall the string will be when printed by the
  fl_draw() function with \p align parameter. If the incoming \p w
  is non-zero it will wrap to that width.

  The \ref drawing_fl_font "current font" is used to do the width/height
  calculations, so unless its value is known at the time fl_measure() is
  called, it is advised to first set the current font with fl_font().
  With event-driven GUI programming you can never be sure which
  widget was exposed and redrawn last, nor which font it used.
  If you have not called fl_font() explicitly in your own code,
  the width and height may be set to unexpected values, even zero!

  \b Note: In the general use case, it's a common error to forget to set
  \p w to 0 before calling fl_measure() when wrap behavior isn't needed.

  \param[in] str nul-terminated string
  \param[in,out] w call with w=0, or with the prefered width for word wrapping,
        returns with the width of the string in current font
  \param[out] h height of string in current font
  \param[in] draw_symbols non-zero to enable @@symbol handling [default=1]

  \code
  // Example: Common use case for fl_measure()
  const char *s = "This is a test";
  int wi=0, hi=0;              // initialize to zero before calling fl_measure()
  fl_font(FL_HELVETICA, 14);   // set current font face/size to be used for measuring
  fl_measure(s, wi, hi);       // returns pixel width/height of string in current font
  \endcode
*/
void fl_measure(const char* str, int& w, int& h, int draw_symbols) {
  if (!str || !*str) {w = 0; h = 0; return;}
  h = fl_height();
  char *linebuf = NULL;
  const char* p;
  const char* e;
  int buflen;
  int lines;
  double width=0;
  int W = 0;
  int symwidth[2], symtotal;

  symwidth[0] = 0;      // size of symbol at beginning of string (if any)
  symwidth[1] = 0;      // size of symbol at end of string (if any)

  if (draw_symbols) {
    // Symbol at beginning of string?
    const char *sym2 = (str[0]=='@' && str[1]=='@') ? str+2 : str;      // sym2 check will skip leading @@
    if (str[0] == '@' && str[1] != '@') {
      while (*str && !isspace(*str)) { ++str; }         // skip over symbol
      if (isspace(*str)) ++str;                         // skip over trailing space
      sym2 = str;                                       // sym2 check will skip leading symbol
      symwidth[0] = h;
    }
    // Symbol at end of string?
    if ((p=strchr(sym2,'@')) != NULL && p[1] != '@') {
      symwidth[1] = h;
    }
  }

  symtotal = symwidth[0] + symwidth[1];

  for (p = str, lines=0; p;) {
//    e = expand(p, linebuf, w - symtotal, buflen, width, w != 0, draw_symbols);
    e = expand_text_(p, linebuf, 0, w - symtotal, buflen, width,
                        w != 0, draw_symbols);
    if ((int)ceil(width) > W) W = (int)ceil(width);
    lines++;
    if (!*e || (*e == '@' && e[1] != '@' && draw_symbols)) break;
    p = e;
  }

  if ((symwidth[0] || symwidth[1]) && lines) {
    if (symwidth[0]) symwidth[0] = lines * fl_height();
    if (symwidth[1]) symwidth[1] = lines * fl_height();
  }

  symtotal = symwidth[0] + symwidth[1];

  w = W + symtotal;
  h = lines*h;
}

/**
  Sets the current font, which is then used in various drawing routines.
  You may call this outside a draw context if necessary to measure text,
  for instance by calling fl_width(), fl_measure(), or fl_text_extents(),
  but on X this will open the display.

  The font is identified by a \p face and a \p size.
  The size of the font is measured in pixels and not "points".
  Lines should be spaced \p size pixels apart or more.
*/
void fl_font(Fl_Font face, Fl_Fontsize fsize) {
  if (!fl_graphics_driver)
    fl_open_display();
  fl_graphics_driver->font(face, fsize);
}

/**
  This function returns the actual height of the specified \p font
  and \p size. Normally the font height should always be 'size',
  but with the advent of XFT, there are (currently) complexities
  that seem to only be solved by asking the font what its actual
  font height is. (See STR#2115)

  This function was originally undocumented in 1.1.x, and was used
  only by Fl_Text_Display. We're now documenting it in 1.3.x so that
  apps that need precise height info can get it with this function.

  \returns the height of the font in pixels.

  \todo  In the future, when the XFT issues are resolved, this function
         should simply return the 'size' value.
*/
int fl_height(int font, int size) {
    if ( font == fl_font() && size == fl_size() ) return(fl_height());
    int tf = fl_font(), ts = fl_size();   // save
    fl_font(font,size);
    int height = fl_height();
    fl_font(tf,ts);                       // restore
    return(height);
}

/** Removes any GUI scaling factor in subsequent drawing operations.
 This must be matched by a later call to fl_restore_scale().
 This function can be used to transiently perform drawing operations
 that are not rescaled by the current value of the GUI scaling factor.
 It's not supported to call fl_push_clip() while transiently removing scaling.
 \return The GUI scaling factor value that was in place when the function started.
 */
float fl_override_scale() {
  return fl_graphics_driver->override_scale();
}

/** Restores the GUI scaling factor in subsequent drawing operations.
 \param s Value returned by a previous call to fl_override_scale(). */
void fl_restore_scale(float s) {
  fl_graphics_driver->restore_scale(s);
}

/**
  Draw a check mark inside the given bounding box.

  The check mark is allowed to fill the entire box but the algorithm used
  makes sure that a 1-pixel border is kept free if the box is large enough.
  You need to calculate margins for box borders etc. yourself.

  The check mark size is limited (minimum and maximum size) and the check
  mark is always centered in the given box.

  \note If the box is too small (bad GUI design) the check mark will be drawn
    over the box borders. This is intentional for better user experience.
    Otherwise users might not be able to recognize if a box is checked.

  The size limits are implementation details and may be changed at any time.

  \param[in]  bb   rectangle that defines the bounding box
  \param[in]  col  Fl_Color to draw the check mark

  \since 1.4.0
*/

void fl_draw_check(Fl_Rect bb, Fl_Color col) {

  const int md = 6; // max. d1 value: 3 * md + 1 pixels wide
  int tx = bb.x();
  int ty = bb.y();
  int tw = bb.w();
  int th = bb.h();
  int lh = 3;       // line height 3 means 4 pixels
  int d1, d2;

  Fl_Color saved_color = fl_color();

  // make sure there's a free 1-pixel border if the area is large enough
  if (tw > 10) {
    tx++;
    tw -= 2;
  }
  if (th > 10) {
    ty++;
    th -= 2;
  }
  // calculate d1, the width and height of the left part of the check mark
  d1 = tw / 3;
  d2 = 2 * d1;
  if (d1 > md) {
    d1 = md;
    d2 = 2 * d1;
  }
  // make sure the height fits
  if (d2 + lh + 1 > th) {
    d2 = th - lh - 1;
    d1 = (d2+1) / 2;
  }
  // check minimal size (box too small)
  if (d1 < 2) {
    d1 = 2;
    d2 = 4;
  }
  // reduce line height (width) for small sizes
  if (d1 < 3)
    lh = 2;

  tw = d1 + d2 + 1; // total width
  th = d2 + lh + 1; // total height

  tx = bb.x() + (bb.w() - tw + 1) / 2;  // x position (centered)
  ty = bb.y() + (bb.h() - th + 1) / 2;  // y position (centered)

  // Set DEBUG_FRAME to 1 - 3 for debugging (0 otherwise)
  // Bit 1 set: draws a green background (the entire given box)
  // Bit 2 set: draws a red frame around the check mark (the bounding box)
  // The background (1) can be used to test correct positioning by the widget code

#define DEBUG_FRAME (0)
#if (DEBUG_FRAME)
  if (DEBUG_FRAME & 1) {    // 1 = background
    fl_color(0x88dd8800);
    fl_rectf(bb.x(), bb.y(), bb.w(), bb.h());
  }
  if (DEBUG_FRAME & 2) {    // 2 = bounding box
    fl_color(0xff000000);
    fl_rect(tx, ty, tw, th);
  }
#endif

  // draw the check mark

  fl_color(col);

  fl_begin_complex_polygon();

    ty += d2 - d1;            // upper border of check mark: left to right
    fl_vertex(tx, ty);
    fl_vertex(tx + d1, ty + d1);
    fl_vertex(tx + d1 + d2, ty + d1 - d2);
    ty += lh;                 // lower border of check mark: right to left
    fl_vertex(tx + d1 + d2, ty + d1 - d2);
    fl_vertex(tx + d1, ty + d1);
    fl_vertex(tx, ty);

  fl_end_complex_polygon();

  fl_color(saved_color);

} // fl_draw_check()

/**
  Draw a potentially small, filled circle using a given color.

  This function draws a filled circle bounded by rectangle
  <tt>(x, y, d, d)</tt> using color \p color

  This function is the same as <tt>fl_pie(x, y, d, d, 0, 360)</tt> except
  with some systems that don't draw small circles well. In that situation,
  the circle diameter \p d is converted from FLTK units to pixels and this
  function approximates a filled circle by drawing several filled
  rectangles if the converted diameter is ≤ 6 pixels.

  The current drawing color fl_color() is preserved across the call.

  \param[in]  x,y     coordinates of top left of the bounding box
  \param[in]  d       diameter == width and height of the bounding box in FLTK units
  \param[in]  color   the color used to draw the circle

  \since 1.4.0
 */
void fl_draw_circle(int x, int y, int d, Fl_Color color) {
#define DEBUG_DRAW_CIRCLE (0) // bit 1 = draw bounding box (green)
#if (DEBUG_DRAW_CIRCLE & 0)
  Fl_Color current = fl_color();
  fl_rectf(x, y, d, d, FL_GREEN);
  fl_color(current);
#endif
  fl_graphics_driver->draw_circle(x, y, d, color);
}

/**
  Draw a round check mark (circle) of a radio button.

  This draws only the round "radio button mark", it does not draw the
  (also typically round) box of the radio button.

  Call this only if the radio button is \c ON.

  This method draws a scheme specific "circle" with a particular light effect
  if the scheme is gtk+. For all other schemes this function draws a simple,
  small circle.

  The \c color must be chosen by the caller so it has enough contrast with
  the background.

  The bounding box of the circle is the rectangle <tt>(x, y, d, d)</tt>.

  The current drawing color fl_color() is preserved across the call.

  \param[in]  x,y     coordinates of top left of the bounding box
  \param[in]  d       diameter == width and height of the bounding box in FLTK units
  \param[in]  color   the base color used to draw the circle

  \since 1.4.0
*/
void fl_draw_radio(int x, int y, int d, Fl_Color color) {

  Fl_Color current = fl_color();

#if (0) // DEBUG: draw bounding box
  fl_color(fl_lighter(FL_RED));
  fl_rectf(x, y, d, d);
#endif

  if (Fl::is_scheme("gtk+")) {
    fl_color(color);
    fl_pie(x, y, d, d, 0.0, 360.0);
    Fl_Color icol = fl_color_average(FL_WHITE, color, 0.2f);
    fl_draw_circle(x + 2, y + 2, d - 4, icol);
    fl_color(fl_color_average(FL_WHITE, color, 0.5));
    fl_arc(x + 1, y + 1, d - 1, d - 1, 60.0, 180.0);
  } else {
    fl_draw_circle(x + 1, y + 1, d - 2, color);
  }
  fl_color(current);
}