Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
306 andreas 1
/*
2
 * Copyright (C) 2023 by Andreas Theofilu <andreas@theosys.at>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
#include <string>
19
#include <vector>
20
 
21
#include <include/core/SkImage.h>
22
#include <include/core/SkCanvas.h>
23
#include <include/core/SkPixmap.h>
24
#include <include/core/SkBitmap.h>
25
#include <include/core/SkSize.h>
26
#include <include/core/SkColor.h>
27
#include <include/core/SkSurfaceProps.h>
28
#include <include/core/SkRRect.h>
29
 
30
#include "tintborder.h"
31
#include "tcolor.h"
32
#include "tresources.h"
33
#include "terror.h"
34
 
35
using namespace Border;
36
using std::string;
37
using std::vector;
38
 
39
/**
40
 * The following table defines some of the system borders. It is mostly a
41
 * fallback table but defines whether a border should be calculated internaly
42
 * or constructed out of the images in the system border folder. The later is
43
 * only possible if this folder exist and contains the system images from AMX.
44
 * This images could be retrieved by sending a full surface with the system
45
 * files included from TPDesign4.
46
 * All borders not listed in this table are constructed out of the system
47
 * border images, if they exist.
48
 */
49
SYSBORDER_t sysBorders[] = {
50
//   ID   Name                  AMX number  Style          width radius calc
51
    {  1, (char *)"Single Line",         0, (char *)"solid",   1,   0, true },
52
    {  2, (char *)"Double Line",         0, (char *)"solid",   2,   0, true },
53
    {  3, (char *)"Quad Line",           0, (char *)"solid",   4,   0, true },
54
    {  4, (char *)"Picture Frame",       0, (char *)"double",  0,   0, false },
55
    {  5, (char *)"Circle 15",           8, (char *)"solid",   2,   7, true },
56
    {  6, (char *)"Circle 25",           9, (char *)"solid",   2,  14, true },
57
    {  7, (char *)"Circle 35",          10, (char *)"solid",   2,  21, true },
58
    {  8, (char *)"Circle 45",          11, (char *)"solid",   2,  28, true },
59
    {  9, (char *)"Circle 55",          12, (char *)"solid",   2,  35, true },
60
    { 10, (char *)"Circle 65",          13, (char *)"solid",   2,  42, true },
61
    { 11, (char *)"Circle 75",          14, (char *)"solid",   2,  49, true },
62
    { 12, (char *)"Circle 85",          15, (char *)"solid",   2,  56, true },
63
    { 13, (char *)"Circle 95",          16, (char *)"solid",   2,  63, true },
64
    { 14, (char *)"Circle 105",         17, (char *)"solid",   2,  70, true },
65
    { 15, (char *)"Circle 115",         18, (char *)"solid",   2,  77, true },
66
    { 16, (char *)"Circle 125",         19, (char *)"solid",   2,  84, true },
67
    { 17, (char *)"Circle 135",         20, (char *)"solid",   2,  91, true },
68
    { 18, (char *)"Circle 145",         21, (char *)"solid",   2,  98, true },
69
    { 19, (char *)"Circle 155",         22, (char *)"solid",   2, 105, true },
70
    { 20, (char *)"Circle 165",         23, (char *)"solid",   2, 112, true },
71
    { 21, (char *)"Circle 175",         24, (char *)"solid",   2, 119, true },
72
    { 22, (char *)"Circle 185",         25, (char *)"solid",   2, 126, true },
73
    { 23, (char *)"Circle 195",         26, (char *)"solid",   2, 133, true },
74
    { 24, (char *)"AMX Elite Inset -L",  0, (char *)"groove", 20,   0, false },
75
    { 25, (char *)"AMX Elite Raised -L", 0, (char *)"ridge",  20,   0, false },
76
    { 26, (char *)"AMX Elite Inset -M",  0, (char *)"groove", 10,   0, false },
77
    { 27, (char *)"AMX Elite Raised -M", 0, (char *)"ridge",  10,   0, false },
78
    { 28, (char *)"AMX Elite Inset -S",  0, (char *)"groove",  4,   0, false },
79
    { 29, (char *)"AMX Elite Raised -S", 0, (char *)"ridge",   4,   0, false },
80
    { 30, (char *)"Bevel Inset -L",      0, (char *)"inset",  20,   0, false },
81
    { 31, (char *)"Bevel Raised -L",     0, (char *)"outset", 20,   0, false },
82
    { 32, (char *)"Bevel Inset -M",      0, (char *)"inset",  10,   0, false },
83
    { 33, (char *)"Bevel Raised -M",     0, (char *)"outset", 10,   0, false },
84
    { 34, (char *)"Bevel Inset -S",      0, (char *)"inset",   4,   0, false },
85
    { 35, (char *)"Bevel Raised -S",     0, (char *)"outset",  4,   0, false },
86
    {  0, nullptr, 0, nullptr, 0, 0 }
87
};
88
 
89
TIntBorder::TIntBorder()
90
{
91
    DECL_TRACER("TIntBorder::TIntBorder()");
92
}
93
 
94
bool TIntBorder::drawBorder(SkBitmap* bm, const string& bname, int wt, int ht, const string& cb, bool force)
95
{
96
    DECL_TRACER("TIntBorder::drawBorder(SkBitmap* bm, string& bname, int wt, int ht, string& cb, bool force)");
97
 
98
    if (!bm || wt <= 0 || ht <= 0)
99
        return false;
100
 
101
    if (bname.empty())
102
    {
103
        MSG_DEBUG("No border name defined.");
104
        return false;
105
    }
106
 
107
    // Try to find the border in the system table
108
    int borderIndex = -1;
109
    int i = 0;
110
 
111
    while (sysBorders[i].id)
112
    {
308 andreas 113
        if (strCaseCompare(bname, sysBorders[i].name) == 0)
306 andreas 114
        {
115
            if (!force && !sysBorders[i].calc)
307 andreas 116
            {
117
                MSG_DEBUG("Ignoring border " << bname << " because it was not forced.");
306 andreas 118
                return false;
307 andreas 119
            }
306 andreas 120
 
121
            borderIndex = i;
122
            MSG_DEBUG("Found internal system border [" << i << "]: " << sysBorders[i].name);
123
            break;
124
        }
125
 
126
        i++;
127
    }
128
 
129
    if (borderIndex < 0)
307 andreas 130
    {
131
        MSG_DEBUG(bname << " is not an internal border.");
306 andreas 132
        return false;
307 andreas 133
    }
306 andreas 134
 
135
    MSG_DEBUG("Border " << sysBorders[borderIndex].name << " found.");
136
    SkCanvas canvas(*bm, SkSurfaceProps());
137
    SkPaint paint;
138
    SkColor color = TColor::getSkiaColor(cb);
139
 
140
    paint.setColor(color);
317 andreas 141
    paint.setBlendMode(SkBlendMode::kSrc);
306 andreas 142
    paint.setStyle(SkPaint::kStroke_Style);
143
    SkRRect outher, inner;
144
    SkScalar radius = (SkScalar)sysBorders[borderIndex].radius;
145
    int red, green, blue;
146
    SkColor borderColor, bcLight, bcDark;
147
    int lineWidth = 0;
148
 
149
    switch (sysBorders[borderIndex].id)
150
    {
151
        case 1: // Single Frame
152
        case 2: // Double Frame
153
        case 3: // Quad Frame
154
            paint.setStrokeWidth(sysBorders[borderIndex].width);
155
            canvas.drawRect(calcRect(wt, ht, sysBorders[borderIndex].width), paint);
156
        break;
157
 
158
        case 4: // Picture Frame
159
            {
160
                paint.setStrokeWidth(2);
161
                SkRect rect = SkRect::MakeXYWH(0, 0, wt, ht);
162
                canvas.drawRect(rect, paint);
163
                rect = SkRect::MakeXYWH(4, 4, wt - 4, ht - 4);
164
                canvas.drawRect(rect, paint);
165
            }
166
        break;
167
 
168
        case 5: // Circle 15
169
        case 6: // Circle 25
170
        case 7: // Circle 35
171
        case 8: // Circle 45
172
        case 9: // Circle 55
173
        case 10: // Circle 65
174
        case 11: // Circle 75
175
        case 12: // Circle 85
176
        case 13: // Circle 95
177
        case 14: // Circle 105
178
        case 15: // Circle 115
179
        case 16: // Circle 125
180
        case 17: // Circle 135
181
        case 18: // Circle 145
182
        case 19: // Circle 155
183
        case 20: // Circle 165
184
        case 21: // Circle 175
185
        case 22: // Circle 185
186
        case 23: // Circle 195
187
            lineWidth = sysBorders[borderIndex].width;
188
            paint.setStrokeWidth(1.0);
189
            paint.setStyle(SkPaint::kFill_Style);
190
            MSG_DEBUG("Line width: " << lineWidth << ", radius: " << radius);
191
            // We draw a rounded rectangle to "clip" the corners. To do this
192
            // in a way to not miss any pixel, we draw a rectangle followed
193
            // by a rounded rectangle as an inner one. The space between
194
            // them will be filled transparent.
195
            outher = SkRRect::MakeRect({0, 0, (SkScalar)wt, (SkScalar)ht});
196
            inner = SkRRect::MakeRectXY(calcRect(wt, ht, 1), radius, radius);
197
            paint.setColor(SK_ColorTRANSPARENT);
198
            canvas.drawDRRect(outher, inner, paint);
199
            // Here we draw the rounded rectangle.
200
            paint.setStyle(SkPaint::kStroke_Style);
201
            paint.setStrokeWidth(lineWidth);
202
            paint.setColor(color);
203
            paint.setStrokeJoin(SkPaint::kRound_Join);
204
            canvas.drawRoundRect(calcRect(wt, ht, lineWidth), radius, radius, paint);
205
        break;
206
 
207
        case 24:    // AMX Elite Inset -L
208
        case 26:    // AMX Elite Inset -M
209
        case 28:    // AMX Elite Inset -S
210
            {
211
                borderColor = TColor::getSkiaColor(cb);
212
                vector<SkColor> cols = TColor::colorRange(borderColor, sysBorders[borderIndex].width, 40, TColor::DIR_LIGHT_DARK_LIGHT);
213
                vector<SkColor>::iterator iter;
214
                i = 0;
215
 
216
                for (iter = cols.begin(); iter != cols.end(); ++iter)
217
                {
218
                    paint.setStrokeWidth(1);
219
                    paint.setColor(*iter);
220
                    SkRect rect = SkRect::MakeXYWH(i, i, wt - i, ht - i);
221
                    canvas.drawRect(rect, paint);
222
                    i++;
223
                }
224
            }
225
        break;
226
 
227
        case 25:    // AMX Elite Raised -L
228
        case 27:    // AMX Elite Raised -M
229
        case 29:    // AMX Elite Raised -S
230
        {
231
            borderColor = TColor::getSkiaColor(cb);
232
            vector<SkColor> cols = TColor::colorRange(borderColor, sysBorders[borderIndex].width, 40, TColor::DIR_DARK_LIGHT_DARK);
233
            vector<SkColor>::iterator iter;
234
            i = 0;
235
 
236
            for (iter = cols.begin(); iter != cols.end(); ++iter)
237
            {
238
                paint.setStrokeWidth(1);
239
                paint.setColor(*iter);
240
                SkRect rect = SkRect::MakeXYWH(i, i, wt - i, ht - i);
241
                canvas.drawRect(rect, paint);
242
                i++;
243
            }
244
        }
245
        break;
246
 
247
        case 30:    // BevelInset -L
248
        case 32:    // Bevel Inset -M
249
        case 34:    // Bevel Inset -S
250
            borderColor = TColor::getSkiaColor(cb);
251
            red = std::min((int)SkColorGetR(borderColor) + 20, 255);
252
            green = std::min((int)SkColorGetG(borderColor) + 20, 255);
253
            blue = std::min((int)SkColorGetB(borderColor) + 20, 255);
254
            bcLight = SkColorSetARGB(SkColorGetA(borderColor), red, green, blue);
255
            red = std::max((int)SkColorGetR(borderColor) - 20, 0);
256
            green = std::max((int)SkColorGetG(borderColor) - 20, 0);
257
            blue = std::max((int)SkColorGetB(borderColor) - 20, 0);
258
            bcDark = SkColorSetARGB(SkColorGetA(borderColor), red, green, blue);
259
            paint.setStrokeWidth(1);
260
            paint.setColor(bcDark);
261
            // Lines on the left
262
            for (i = 0; i < sysBorders[borderIndex].width; i++)
263
            {
264
                int yt = i;
265
                int yb = ht - i;
266
                canvas.drawLine(i, yt, i, yb, paint);
267
            }
268
            // Lines on the top
269
            for (i = 0; i < sysBorders[borderIndex].width; i++)
270
            {
271
                int xl = i;
272
                int xr = wt - i;
273
                canvas.drawLine(xl, i, xr, i, paint);
274
            }
275
            // Lines on right side
276
            paint.setColor(bcLight);
277
 
278
            for (i = 0; i < sysBorders[borderIndex].width; i++)
279
            {
280
                int yt = i;
281
                int yb = ht - i;
282
                canvas.drawLine(wt - i, yt, wt - i, yb, paint);
283
            }
284
            // Lines on bottom
285
            for (i = 0; i < sysBorders[borderIndex].width; i++)
286
            {
287
                int xl = i;
288
                int xr = wt - i;
289
                canvas.drawLine(xl, ht - i, xr, ht - i, paint);
290
            }
291
        break;
292
 
293
        case 31:    // Bevel Raised _L
294
        case 33:    // Bevel Raised _M
295
        case 35:    // Bevel Raised _S
296
            borderColor = TColor::getSkiaColor(cb);
297
            red = std::min((int)SkColorGetR(borderColor) + 10, 255);
298
            green = std::min((int)SkColorGetG(borderColor) + 10, 255);
299
            blue = std::min((int)SkColorGetB(borderColor) + 10, 255);
300
            bcLight = SkColorSetARGB(SkColorGetA(borderColor), red, green, blue);
301
            red = std::max((int)SkColorGetR(borderColor) - 10, 0);
302
            green = std::max((int)SkColorGetG(borderColor) - 10, 0);
303
            blue = std::max((int)SkColorGetB(borderColor) - 10, 0);
304
            bcDark = SkColorSetARGB(SkColorGetA(borderColor), red, green, blue);
305
            paint.setStrokeWidth(1);
306
            paint.setColor(bcLight);
307
            // Lines on the left
308
            for (i = 0; i < sysBorders[borderIndex].width; i++)
309
            {
310
                int yt = i;
311
                int yb = ht - i;
312
                canvas.drawLine(i, yt, i, yb, paint);
313
            }
314
            // Lines on the top
315
            for (i = 0; i < sysBorders[borderIndex].width; i++)
316
            {
317
                int xl = i;
318
                int xr = wt - i;
319
                canvas.drawLine(xl, i, xr, i, paint);
320
            }
321
            // Lines on right side
322
            paint.setColor(bcDark);
323
 
324
            for (i = 0; i < sysBorders[borderIndex].width; i++)
325
            {
326
                int yt = i;
327
                int yb = ht - i;
328
                canvas.drawLine(wt - i, yt, wt - i, yb, paint);
329
            }
330
            // Lines on bottom
331
            for (i = 0; i < sysBorders[borderIndex].width; i++)
332
            {
333
                int xl = i;
334
                int xr = wt - borderIndex;
335
                canvas.drawLine(xl, ht - i, xr, ht - i, paint);
336
            }
337
        break;
338
 
339
        default:
340
            return false;
341
    }
342
 
343
    return true;
344
}
345
 
346
int TIntBorder::getBorderWidth(const string& bname, bool force)
347
{
348
    DECL_TRACER("TIntBorder::getBorderWidth(const string& bname, bool force)");
349
 
350
    for (int i = 0; sysBorders[i].id > 0; ++i)
351
    {
352
        if (strCaseCompare(bname, sysBorders[i].name) == 0)
353
        {
354
            if (!force && !sysBorders[i].calc)
355
                return 0;
356
 
357
            return sysBorders[i].width;
358
        }
359
    }
360
 
361
    return 0;
362
}
363
 
364
bool TIntBorder::borderExist(const string& name)
365
{
366
    DECL_TRACER("TIntBorder::borderExist(const string& name)");
367
 
368
    for (int i = 0; sysBorders[i].id > 0; ++i)
369
    {
370
        if (strCaseCompare(name, sysBorders[i].name) == 0)
371
            return true;
372
    }
373
 
374
    return false;
375
}
376
 
377
string TIntBorder::getCorrectName(const string& name)
378
{
379
    DECL_TRACER("TIntBorder::getCorrectName(const string& name)");
380
 
381
    for (int i = 0; sysBorders[i].id > 0; ++i)
382
    {
383
        if (strCaseCompare(name, sysBorders[i].name) == 0)
384
            return sysBorders[i].name;
385
    }
386
 
387
    return string();
388
}
389
 
390
bool TIntBorder::isForcedBorder(const string& name)
391
{
392
    DECL_TRACER("TIntBorder::isForcedBorder(const string& name)");
393
 
394
    for (int i = 0; sysBorders[i].id > 0; ++i)
395
    {
396
        if (strCaseCompare(name, sysBorders[i].name) == 0)
397
            return sysBorders[i].calc;
398
    }
399
 
400
    return false;
401
}
402
 
307 andreas 403
void TIntBorder::erasePart(SkBitmap *bm, const SkBitmap& mask, ERASE_PART_t ep)
404
{
405
    DECL_TRACER("TIntBorder::erasePart(SkBitmap *bm, const SkBitmap& mask, ERASE_PART_t ep)");
406
 
407
    if (!bm || bm->empty() || ep == ERASE_NONE)
408
        return;
409
 
410
    int x, y;
411
    int width = bm->info().width();
412
    int height = bm->info().height();
413
 
414
    switch(ep)
415
    {
416
        case ERASE_LEFT_RIGHT:
417
            for (y = 0; y < height; ++y)
418
            {
419
                bool barrier = false;
420
 
421
                for (x = 0; x < width; ++x)
422
                {
423
                    uint32_t *wpix = bm->getAddr32(x, y);
424
                    uint32_t color = mask.getColor(x, y);
425
                    barrier = setPixel(wpix, color, barrier);
426
                }
427
            }
428
        break;
429
 
430
        case ERASE_RIGHT_LEFT:
431
            for (y = 0; y < height; ++y)
432
            {
433
                bool barrier = false;
434
 
435
                for (x = width - 1; x > 0; --x)
436
                {
437
                    uint32_t *wpix = bm->getAddr32(x, y);
438
                    uint32_t color = mask.getColor(x, y);
439
                    barrier = setPixel(wpix, color, barrier);
440
                }
441
            }
442
        break;
443
 
444
        case ERASE_TOP_DOWN:
445
            for (x = 0; x < width; ++x)
446
            {
447
                bool barrier = false;
448
 
449
                for (y = 0; y < height; ++y)
450
                {
451
                    uint32_t *wpix = bm->getAddr32(x, y);
452
                    uint32_t color = mask.getColor(x, y);
453
                    barrier = setPixel(wpix, color, barrier);
454
                }
455
            }
456
        break;
457
 
458
        case ERASE_BOTTOM_UP:
459
            for (x = 0; x < width; ++x)
460
            {
461
                bool barrier = false;
462
 
463
                for (y = height - 1; y > 0; --y)
464
                {
465
                    uint32_t *wpix = bm->getAddr32(x, y);
466
                    uint32_t color = mask.getColor(x, y);
467
                    barrier = setPixel(wpix, color, barrier);
468
                }
469
            }
470
        break;
471
 
472
        case ERASE_OUTSIDE:
473
            for (y = 0; y < height; ++y)
474
            {
308 andreas 475
                uint32_t color = 0;
307 andreas 476
 
477
                for (x = 0; x < width; ++x)
478
                {
479
                    uint32_t *wpix = bm->getAddr32(x, y);
308 andreas 480
                    uint32_t alpha = SkColorGetA(mask.getColor(x, y));
307 andreas 481
 
308 andreas 482
                    if (alpha == 0)
307 andreas 483
                        color = SK_ColorTRANSPARENT;
308 andreas 484
                    else
485
                        break;
486
 
487
                    *wpix = color;
488
                }
489
 
490
                for (x = width - 1; x > 0; --x)
491
                {
492
                    uint32_t *wpix = bm->getAddr32(x, y);
493
                    uint32_t alpha = SkColorGetA(mask.getColor(x, y));
494
 
495
                    if (alpha == 0)
307 andreas 496
                        color = SK_ColorTRANSPARENT;
308 andreas 497
                    else
498
                        break;
307 andreas 499
 
500
                    *wpix = color;
501
                }
502
            }
503
        break;
504
 
505
        default:
506
            return;
507
    }
508
}
509
 
510
bool TIntBorder::setPixel(uint32_t *wpix, uint32_t col, bool bar)
511
{
512
    DECL_TRACER("TIntBorder::setPixel(uint32_t *wpix, uint32_t col, bool bar)");
513
 
514
    uint32_t alpha = SkColorGetA(col);
515
    uint32_t color = col;
516
    bool barrier = bar;
517
 
518
    if (alpha == 0 && !barrier)
519
        color = SK_ColorTRANSPARENT;
520
    else if (alpha > 0 && !barrier)
521
    {
522
        barrier = true;
523
        color = *wpix;
524
    }
525
    else if (barrier)
526
        color = *wpix;
527
 
528
    *wpix = color;
529
    return barrier;
530
}
531
 
306 andreas 532
SkRect TIntBorder::calcRect(int width, int height, int pen)
533
{
534
    DECL_TRACER("TIntBorder::calcRect(int width, int height, int pen)");
535
    SkRect rect;
536
 
537
    SkScalar left = (SkScalar)pen / 2.0;
538
    SkScalar top = (SkScalar)pen / 2.0;
539
    SkScalar w = (SkScalar)width - (SkScalar)pen;
540
    SkScalar h = (SkScalar)height - (SkScalar)pen;
541
    rect.setXYWH(left, top, w, h);
542
    return rect;
543
}
544