FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
codegen/codegen/style/generator.cpp at master-codegen · AyuGram/codegen · GitHub
AyuGram
/
codegen
Public
forked from
desktop-app/codegen
Notifications
You must be signed in to change notification settings
Fork
20
Star
2
Code
Pull requests
2
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
codegen
/
codegen
/
style
/
generator.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
1338 lines (1230 loc) · 37.8 KB
Breadcrumbs
codegen
/
codegen
/
style
/
generator.cpp
Copy path
File metadata and controls
1338 lines (1230 loc) · 37.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
This file is part of Desktop App Toolkit,
//
a set of libraries for developing nice desktop applications.
//
//
For license and copyright information please follow this link:
//
https://github.com/desktop-app/legal/blob/master/LEGAL
//
#
include
"
codegen/style/generator.h
"
#
include
"
base/crc32hash.h
"
#
include
<
algorithm
>
#
include
<
set
>
#
include
<
memory
>
#
include
<
functional
>
#
include
<
QtCore/QDir
>
#
include
<
QtCore/QSet
>
#
include
<
QtCore/QBuffer
>
#
include
<
QtGui/QImage
>
#
include
<
QtGui/QPainter
>
#
include
<
QtSvg/QSvgRenderer
>
#
include
"
codegen/style/parsed_file.h
"
using
Module = codegen::style::structure::Module;
using
Struct = codegen::style::structure::Struct;
using
Variable = codegen::style::structure::Variable;
using
Tag = codegen::style::structure::TypeTag;
namespace
codegen
{
namespace
style
{
namespace
{
constexpr
int
kErrorBadIconSize
=
861
;
const
auto
kMustBeContrast
= std::map<QString, QString>{
{
"
dialogsMenuIconFg
"
,
"
dialogsBg
"
},
{
"
windowBoldFg
"
,
"
windowBg
"
},
};
const
auto
kWideMultiplicable
= std::map<QString, QString>{
{
"
msgMaxWidth
"
,
"
1.0
"
},
{
"
historyGroupWidthMax
"
,
"
1.0
"
},
{
"
msgServicePhotoWidth
"
,
"
1.0
"
},
//
tied to `msgMaxWidth` (should have the same mult, or previews would look bad)
{
"
maxMediaSize
"
,
"
1.0
"
},
{
"
maxGifSize
"
,
"
0.6
"
},
{
"
maxStickerSize
"
,
"
0.6
"
},
{
"
maxVideoMessageSize
"
,
"
0.75
"
},
{
"
maxAnimatedEmojiSize
"
,
"
0.6
"
},
{
"
maxWallPaperWidth
"
,
"
1.0
"
},
{
"
maxWallPaperHeight
"
,
"
1.0
"
},
};
char
hexChar
(uchar ch) {
if
(ch <
10
) {
return
'
0
'
+ ch;
}
else
if
(ch <
16
) {
return
'
a
'
+ (ch -
10
);
}
return
'
0
'
;
}
char
hexSecondChar
(
char
ch) {
return
hexChar
((*
reinterpret_cast
<uchar*>(&ch)) &
0x0F
);
}
char
hexFirstChar
(
char
ch) {
return
hexChar
((*
reinterpret_cast
<uchar*>(&ch)) >>
4
);
}
QString
stringToEncodedString
(
const
QString &str) {
QString result, lineBreak =
"
\\\n
"
;
result.
reserve
(str.
size
() *
8
);
bool
writingHexEscapedCharacters =
false
, startOnNewLine =
false
;
int
lastCutSize =
0
;
auto
utf = str.
toUtf8
();
for
(
auto
ch : utf) {
if
(result.
size
() - lastCutSize >
80
) {
startOnNewLine =
true
;
result.
append
(lineBreak);
lastCutSize = result.
size
();
}
if
(ch ==
'
\n
'
) {
writingHexEscapedCharacters =
false
;
result.
append
(
"
\\
n
"
);
}
else
if
(ch ==
'
\t
'
) {
writingHexEscapedCharacters =
false
;
result.
append
(
"
\\
t
"
);
}
else
if
(ch ==
'
"
'
|| ch ==
'
\\
'
) {
writingHexEscapedCharacters =
false
;
result.
append
(
'
\\
'
).
append
(ch);
}
else
if
(ch <
32
||
static_cast
<uchar>(ch) >
127
) {
writingHexEscapedCharacters =
true
;
result.
append
(
"
\\
x
"
).
append
(
hexFirstChar
(ch)).
append
(
hexSecondChar
(ch));
}
else
{
if
(writingHexEscapedCharacters) {
writingHexEscapedCharacters =
false
;
result.
append
(
"
\"\"
"
);
}
result.
append
(ch);
}
}
return
'
"
'
+ (startOnNewLine ? lineBreak :
QString
()) + result +
'
"
'
;
}
QString
stringToEncodedString
(
const
std::string &str) {
return
stringToEncodedString
(
QString::fromStdString
(str));
}
QString
stringToBinaryArray
(
const
std::string &str) {
QStringList rows, chars;
chars.
reserve
(
13
);
rows.
reserve
(
1
+ (str.
size
() /
13
));
for
(uchar ch : str) {
if
(chars.
size
() >
12
) {
rows.
push_back
(chars.
join
(
"
,
"
));
chars.
clear
();
}
chars.
push_back
(
QString
(
"
0x
"
) +
hexFirstChar
(ch) +
hexSecondChar
(ch));
}
if
(!chars.
isEmpty
()) {
rows.
push_back
(chars.
join
(
"
,
"
));
}
return
QString
(
"
{
"
) + ((rows.
size
() >
1
) ?
'
\n
'
:
'
'
) + rows.
join
(
"
,
\n
"
) +
"
}
"
;
}
QString
pxValueName
(
int
value) {
QString result =
"
px
"
;
if
(value <
0
) {
value = -value;
result +=
'
m
'
;
}
return
result +
QString::number
(value);
}
QString
moduleBaseName
(
const
structure::Module &
module
) {
auto
moduleInfo =
QFileInfo
(
module
.
filepath
());
auto
moduleIsPalette = (moduleInfo.
suffix
() ==
"
palette
"
);
return
moduleIsPalette ?
"
palette
"
:
"
style_
"
+ moduleInfo.
baseName
();
}
QString
colorFallbackName
(structure::Value value) {
auto
copy = value.
copyOf
();
if
(!copy.
isEmpty
()) {
return
copy.
back
();
}
return
value.
Color
().
fallback
;
}
QChar
paletteColorPart
(uchar part) {
part = (part &
0x0F
);
if
(part >=
10
) {
return
QChar
(
'
a
'
+ (part -
10
));
}
return
QChar
(
'
0
'
+ part);
}
QString
paletteColorComponent
(uchar value) {
return
QString
() +
paletteColorPart
(value >>
4
) +
paletteColorPart
(value);
}
QString
paletteColorValue
(
const
structure::data::color &value) {
auto
result =
paletteColorComponent
(value.
red
) +
paletteColorComponent
(value.
green
) +
paletteColorComponent
(value.
blue
);
if
(value.
alpha
!=
255
) result +=
paletteColorComponent
(value.
alpha
);
return
result;
}
[[nodiscard]]
bool
IsValueInHeader
(structure::Type type) {
switch
(type.
tag
) {
case
Tag::Int:
case
Tag::Double:
return
true
;
default
:
return
false
;
}
}
}
//
namespace
Generator::Generator
(
const
structure::Module &
module
,
const
QString &destBasePath,
const
common::ProjectInfo &project,
bool
isPalette)
: module_(
module
)
, basePath_(destBasePath)
, baseName_(QFileInfo(basePath_).baseName())
, project_(project)
, isPalette_(isPalette) {
}
bool
Generator::writeHeader
() {
header_ = std::make_unique<common::CppFile>(basePath_ +
"
.h
"
, project_);
header_->
include
(
"
ui/style/style_core.h
"
).
newline
();
if
(!
writeHeaderRequiredIncludes
()) {
return
false
;
}
if
(!
writeHeaderStyleNamespace
()) {
return
false
;
}
if
(!
writeRefsDeclarations
()) {
return
false
;
}
return
header_->
finalize
();
}
bool
Generator::writeSource
() {
source_ = std::make_unique<common::CppFile>(basePath_ +
"
.cpp
"
, project_);
writeIncludesInSource
();
if
(module_.
hasVariables
()) {
source_->
pushNamespace
().
newline
();
source_->
stream
() <<
"
\
bool inited = false;
\n
\
\n
\
class Module_
"
<< baseName_ <<
"
: public style::internal::ModuleBase {
\n
\
public:
\n
\
Module_
"
<< baseName_ <<
"
() { style::internal::registerModule(this); }
\n
\
\n
\
void start(int scale) override {
\n
\
style::internal::init_
"
<< baseName_ <<
"
(scale);
\n
\
}
\n
\
};
\n
\
Module_
"
<< baseName_ <<
"
registrator;
\n
"
;
if
(isPalette_) {
source_->
newline
();
source_->
stream
() <<
"
style::palette _palette;
\n
"
;
}
else
{
if
(!
writeVariableDefinitions
()) {
return
false
;
}
}
source_->
newline
().
popNamespace
();
source_->
newline
().
pushNamespace
(
"
st
"
);
if
(!
writeRefsDefinition
()) {
return
false
;
}
source_->
popNamespace
().
newline
().
pushNamespace
(
"
style
"
);
if
(isPalette_) {
writeSetPaletteColor
();
}
source_->
pushNamespace
(
"
internal
"
).
newline
();
if
(!
writeVariableInit
()) {
return
false
;
}
}
return
source_->
finalize
();
}
//
Empty result means an error.
QString
Generator::typeToString
(structure::Type type)
const
{
switch
(type.
tag
) {
case
Tag::Invalid:
return
QString
();
case
Tag::Int:
return
"
int
"
;
case
Tag::Bool:
return
"
bool
"
;
case
Tag::Double:
return
"
double
"
;
case
Tag::Pixels:
return
"
int
"
;
case
Tag::String:
return
"
QString
"
;
case
Tag::Color:
return
"
style::color
"
;
case
Tag::Point:
return
"
style::point
"
;
case
Tag::Size:
return
"
style::size
"
;
case
Tag::Align:
return
"
style::align
"
;
case
Tag::Margins:
return
"
style::margins
"
;
case
Tag::Font:
return
"
style::font
"
;
case
Tag::Icon:
return
"
style::icon
"
;
case
Tag::Struct:
return
"
style::
"
+ type.
name
.
back
();
}
return
QString
();
}
//
Empty result means an error.
QString
Generator::typeToDefaultValue
(structure::Type type)
const
{
switch
(type.
tag
) {
case
Tag::Invalid:
return
QString
();
case
Tag::Int:
return
"
0
"
;
case
Tag::Bool:
return
"
false
"
;
case
Tag::Double:
return
"
0.
"
;
case
Tag::Pixels:
return
"
0
"
;
case
Tag::String:
return
"
QString()
"
;
case
Tag::Color:
return
"
{ Qt::Uninitialized }
"
;
case
Tag::Point:
return
"
{ 0, 0 }
"
;
case
Tag::Size:
return
"
{ 0, 0 }
"
;
case
Tag::Align:
return
"
style::al_topleft
"
;
case
Tag::Margins:
return
"
{ 0, 0, 0, 0 }
"
;
case
Tag::Font:
return
"
{ Qt::Uninitialized }
"
;
case
Tag::Icon:
return
"
{ Qt::Uninitialized }
"
;
case
Tag::Struct: {
if
(
auto
realType = module_.
findStruct
(type.
name
)) {
QStringList fields;
for
(
const
auto
&field : realType->
fields
) {
fields.
push_back
(
typeToDefaultValue
(field.
type
));
}
return
"
{
"
+ fields.
join
(
"
,
"
) +
"
}
"
;
}
return
QString
();
}
break
;
}
return
QString
();
}
//
Empty result means an error.
QString
Generator::valueAssignmentCode
(
structure::Value value,
bool
ignoreCopy,
const
QString& name)
const
{
auto
copy = value.
copyOf
();
if
(!ignoreCopy && !copy.
isEmpty
()) {
return
"
st::
"
+ copy.
join
(
'
.
'
);
}
switch
(value.
type
().
tag
) {
case
Tag::Invalid:
return
QString
();
case
Tag::Int:
return
QString
(
"
%1
"
).
arg
(value.
Int
());
case
Tag::Bool:
return
QString
(value.
Bool
() ?
"
true
"
:
"
false
"
);
case
Tag::Double:
return
QString
(
"
%1
"
).
arg
(value.
Double
());
case
Tag::Pixels: {
if
(
kWideMultiplicable
.
contains
(name)) {
return
QString
(
"
AyuUiSettings::getWideMultiplied(%1, %2)
"
).
arg
(
pxValueName
(value.
Int
())).
arg
(
kWideMultiplicable
.
at
(name));
}
return
pxValueName
(value.
Int
());
}
break
;
case
Tag::String:
return
QString
(
"
QString::fromUtf8(%1)
"
).
arg
(
stringToEncodedString
(value.
String
()));
case
Tag::Color: {
auto
v
(value.
Color
());
if
(v.
red
== v.
green
&& v.
red
== v.
blue
&& v.
red
==
0
&& v.
alpha
==
255
) {
return
QString
(
"
st::windowFg
"
);
}
else
if
(v.
red
== v.
green
&& v.
red
== v.
blue
&& v.
red
==
255
&& v.
alpha
==
0
) {
return
QString
(
"
st::transparent
"
);
}
else
{
common::logError
(common::
kErrorInternal
,
"
"
) <<
"
bad color value
"
;
return
QString
();
}
}
break
;
case
Tag::Point: {
auto
v
(value.
Point
());
return
QString
(
"
{ %1, %2 }
"
).
arg
(
pxValueName
(v.
x
),
pxValueName
(v.
y
));
}
break
;
case
Tag::Size: {
auto
v
(value.
Size
());
if
(
kWideMultiplicable
.
contains
(name)) {
return
QString
(
"
{ AyuUiSettings::getWideMultiplied(%1, %3), AyuUiSettings::getWideMultiplied(%2, %3) }
"
).
arg
(
pxValueName
(v.
width
),
pxValueName
(v.
height
)).
arg
(
kWideMultiplicable
.
at
(name));
}
return
QString
(
"
{ %1, %2 }
"
).
arg
(
pxValueName
(v.
width
),
pxValueName
(v.
height
));
}
break
;
case
Tag::Align:
return
QString
(
"
style::al_%1
"
).
arg
(value.
String
().
c_str
());
case
Tag::Margins: {
auto
v
(value.
Margins
());
return
QString
(
"
{ %1, %2, %3, %4 }
"
).
arg
(
pxValueName
(v.
left
),
pxValueName
(v.
top
),
pxValueName
(v.
right
),
pxValueName
(v.
bottom
));
}
break
;
case
Tag::Font: {
auto
v
(value.
Font
());
QString family =
"
0
"
;
if
(!v.
family
.
empty
()) {
auto
familyIndex = fontFamilies_.
value
(v.
family
, -
1
);
if
(familyIndex <
0
) {
return
QString
();
}
family =
QString
(
"
font%1index
"
).
arg
(familyIndex);
}
return
QString
(
"
{ %1, FontFlags::from_raw(%2), %3 }
"
).
arg
(
pxValueName
(v.
size
)).
arg
(v.
flags
).
arg
(family);
}
break
;
case
Tag::Icon: {
auto
v
(value.
Icon
());
QStringList parts = {
"
std::in_place
"
};
for
(
const
auto
&part : v.
parts
) {
auto
maskIndex = iconMasks_.
value
(part.
filename
, -
1
);
if
(maskIndex <
0
) {
return
QString
();
}
auto
color =
valueAssignmentCode
(part.
color
);
auto
padding =
valueAssignmentCode
(part.
padding
);
parts.
push_back
(
QString
(
"
MonoIcon{ &iconMask%1, %2, %3 }
"
).
arg
(
QString::number
(maskIndex),
color,
padding));
}
return
QString
(
"
{ %1 }
"
).
arg
(parts.
join
(
"
,
"
));
}
break
;
case
Tag::Struct: {
if
(!value.
Fields
())
return
QString
();
QStringList fields;
for
(
const
auto
&field : *value.
Fields
()) {
fields.
push_back
(
valueAssignmentCode
(field.
variable
.
value
));
}
return
"
{
"
+ fields.
join
(
"
,
"
) +
"
}
"
;
}
break
;
}
return
QString
();
}
bool
Generator::writeHeaderRequiredIncludes
() {
std::function<
QString
(
const
Module&, structure::FullName)> findInIncludes = [&](
const
Module &
module
,
const
structure::FullName &name) {
auto
result =
QString
();
module
.
enumIncludes
([&](
const
Module &included) {
if
(
Module::findStructInModule
(name, included)) {
result =
moduleBaseName
(included);
return
false
;
}
result =
findInIncludes
(included, name);
return
true
;
});
return
result;
};
auto
includes =
QStringList
();
const
auto
written = module_.
enumStructs
([&](
const
Struct &value) ->
bool
{
for
(
const
auto
&field : value.
fields
) {
if
(field.
type
.
tag
== structure::TypeTag::Struct) {
const
auto
name = field.
type
.
name
;
if
(!module_.
findStructInModule
(name, module_)) {
const
auto
base =
findInIncludes
(module_, name);
if
(base.
isEmpty
()) {
return
false
;
}
if
(!includes.
contains
(base)) {
includes.
push_back
(base);
}
}
}
}
return
true
;
});
if
(!written) {
return
false
;
}
else
if
(includes.
isEmpty
()) {
return
true
;
}
for
(
const
auto
&base : includes) {
header_->
include
(
"
styles/
"
+ base +
"
.h
"
);
}
header_->
newline
();
return
true
;
}
bool
Generator::writeHeaderStyleNamespace
() {
if
(!module_.
hasStructs
() && !module_.
hasVariables
()) {
return
true
;
}
header_->
pushNamespace
(
"
style
"
);
if
(module_.
hasVariables
()) {
header_->
pushNamespace
(
"
internal
"
).
newline
();
header_->
stream
() <<
"
void init_
"
<< baseName_ <<
"
(int scale);
\n\n
"
;
header_->
popNamespace
();
}
bool
wroteForwardDeclarations =
writeStructsForwardDeclarations
();
if
(module_.
hasStructs
()) {
if
(!wroteForwardDeclarations) {
header_->
newline
();
}
if
(!
writeStructsDefinitions
()) {
return
false
;
}
}
else
if
(isPalette_) {
if
(!wroteForwardDeclarations) {
header_->
newline
();
}
if
(!
writePaletteDefinition
()) {
return
false
;
}
}
header_->
popNamespace
().
newline
();
return
true
;
}
bool
Generator::writePaletteDefinition
() {
header_->
stream
() <<
"
\
class palette;
\n
\
class palette_data {
\n
\
public:
\n
\
static constexpr auto kCount =
"
<< (
2
+ module_.
variablesCount
()) <<
"
;
\n
\
static int32 Checksum();
\n
\
\n
\
inline constexpr const color &transparent() const { return _colors[0]; }; // special color
\n
\
inline constexpr const color &white() const { return _colors[1]; }; // special color
\n
"
;
auto
indexInPalette =
2
;
if
(!module_.
enumVariables
([&](
const
Variable &variable) ->
bool
{
auto
name = variable.
name
.
back
();
if
(variable.
value
.
type
().
tag
!= structure::TypeTag::Color) {
return
false
;
}
auto
index = (indexInPalette++);
header_->
stream
() <<
"
\t
inline constexpr const color &
"
<< name <<
"
() const { return _colors[
"
<< index <<
"
]; };
\n
"
;
return
true
;
}))
return
false
;
const
auto
count = indexInPalette;
header_->
stream
() <<
"
\
\n
\
protected:
\n
\
void finalize(palette &that);
\n
\
\n
\
internal::ColorData *data(int index) {
\n
\
return reinterpret_cast<internal::ColorData*>(_data) + index;
\n
\
}
\n
\
\n
\
const internal::ColorData *data(int index) const {
\n
\
return reinterpret_cast<const internal::ColorData*>(_data) + index;
\n
\
}
\n
\
\n
\
enum class Status {
\n
\
Initial,
\n
\
Created,
\n
\
Loaded,
\n
\
};
\n
\
\n
\
alignas(alignof(internal::ColorData)) char _data[sizeof(internal::ColorData) * kCount];
\n
\
\n
\
color _colors[kCount] = {
\n
"
;
for
(
int
i =
0
; i != count; ++i) {
header_->
stream
() <<
"
\t\t
data(
"
<< i <<
"
),
\n
"
;
}
header_->
stream
() <<
"
\
};
\n
\
Status _status[kCount] = { Status::Initial };
\n
\
bool _ready = false;
\n
\
\n
\
};
\n
\
\n
\
namespace main_palette {
\n
\
\n
\
not_null<const palette*> get();
\n
\
\n
\
struct row {
\n
\
QLatin1String name;
\n
\
QLatin1String value;
\n
\
QLatin1String fallback;
\n
\
QLatin1String description;
\n
\
};
\n
\
QList<row> data();
\n
\
\n
\
} // namespace main_palette
\n
\
\n
\
namespace internal {
\n
\
\n
\
int GetPaletteIndex(QLatin1String name);
\n
\
\n
\
} // namespace internal
\n
"
;
return
true
;
}
bool
Generator::writeStructsForwardDeclarations
() {
bool
hasNoExternalStructs = module_.
enumVariables
([&](
const
Variable &value) ->
bool
{
if
(value.
value
.
type
().
tag
== structure::TypeTag::Struct) {
if
(!module_.
findStructInModule
(value.
value
.
type
().
name
, module_)) {
return
false
;
}
}
return
true
;
});
if
(hasNoExternalStructs) {
return
false
;
}
header_->
newline
();
std::set<QString> alreadyDeclaredTypes;
bool
result = module_.
enumVariables
([&](
const
Variable &value) ->
bool
{
if
(value.
value
.
type
().
tag
== structure::TypeTag::Struct) {
if
(!module_.
findStructInModule
(value.
value
.
type
().
name
, module_)) {
if
(alreadyDeclaredTypes.
find
(value.
value
.
type
().
name
.
back
()) == alreadyDeclaredTypes.
end
()) {
header_->
stream
() <<
"
struct
"
<< value.
value
.
type
().
name
.
back
() <<
"
;
\n
"
;
alreadyDeclaredTypes.
emplace
(value.
value
.
type
().
name
.
back
());
}
}
}
return
true
;
});
header_->
newline
();
return
result;
}
bool
Generator::writeStructsDefinitions
() {
if
(!module_.
hasStructs
()) {
return
true
;
}
bool
result = module_.
enumStructs
([&](
const
Struct &value) ->
bool
{
header_->
stream
() <<
"
\
struct
"
<< value.
name
.
back
() <<
"
{
\n
"
;
for
(
auto
&field : value.
fields
) {
auto
type =
typeToString
(field.
type
);
if
(type.
isEmpty
()) {
return
false
;
}
header_->
stream
() <<
"
\t
"
<< type <<
"
"
<< field.
name
.
back
() <<
"
;
\n
"
;
}
header_->
stream
() <<
"
\
};
\n\n
"
;
return
true
;
});
return
result;
}
bool
Generator::writeRefsDeclarations
() {
if
(!module_.
hasVariables
()) {
return
true
;
}
header_->
pushNamespace
(
"
st
"
);
if
(isPalette_) {
header_->
stream
() <<
"
extern const style::color &transparent; // special color
\n
"
;
header_->
stream
() <<
"
extern const style::color &white; // special color
\n
"
;
}
bool
result = module_.
enumVariables
([&](
const
Variable &value) ->
bool
{
auto
name = value.
name
.
back
();
auto
type =
typeToString
(value.
value
.
type
());
if
(type.
isEmpty
()) {
return
false
;
}
if
(
IsValueInHeader
(value.
value
.
type
())) {
header_->
stream
()
<<
"
constexpr
"
<< type
<<
"
"
<< name
<<
"
=
"
<<
valueAssignmentCode
(
value.
value
,
IsValueInHeader
(value.
value
.
type
()))
<<
"
;
\n
"
;
}
else
{
header_->
stream
()
<<
"
extern const
"
<< type
<<
"
&
"
<< name
<<
"
;
\n
"
;
}
return
true
;
});
header_->
popNamespace
();
return
result;
}
bool
Generator::writeIncludesInSource
() {
if
(isPalette_) {
source_->
include
(
"
ui/style/style_core_palette.h
"
);
source_->
newline
();
}
if
(!module_.
hasIncludes
()) {
return
true
;
}
auto
includes =
QStringList
();
std::function<
bool
(
const
Module&)> collector = [&](
const
Module &
module
) {
module
.
enumIncludes
(collector);
auto
base =
moduleBaseName
(
module
);
if
(!includes.
contains
(base)) {
includes.
push_back
(base);
}
return
true
;
};
auto
result = module_.
enumIncludes
(collector);
for
(
const
auto
&base : includes) {
source_->
include
(
"
styles/
"
+ base +
"
.h
"
);
}
const
auto
ayuIncluded = !module_.
enumVariables
([=](
const
Variable &value) ->
bool
{
for
(
const
auto
&name : value.
name
) {
if
(
kWideMultiplicable
.
contains
(name)) {
return
false
;
}
}
return
true
;
});
if
(ayuIncluded) {
source_->
include
(
"
ayu/ayu_ui_settings.h
"
);
}
source_->
newline
();
return
result;
}
bool
Generator::writeVariableDefinitions
() {
if
(!module_.
hasVariables
()) {
return
true
;
}
source_->
newline
();
bool
result = module_.
enumVariables
([&](
const
Variable &variable) ->
bool
{
auto
name = variable.
name
.
back
();
auto
type =
typeToString
(variable.
value
.
type
());
if
(type.
isEmpty
()) {
return
false
;
}
if
(!
IsValueInHeader
(variable.
value
.
type
())) {
source_->
stream
()
<< type
<<
"
_
"
<< name
<<
"
=
"
<<
typeToDefaultValue
(variable.
value
.
type
())
<<
"
;
\n
"
;
}
return
true
;
});
return
result;
}
bool
Generator::writeRefsDefinition
() {
if
(!module_.
hasVariables
()) {
return
true
;
}
if
(isPalette_) {
source_->
stream
() <<
"
const style::color &transparent(_palette.transparent()); // special color
\n
"
;
source_->
stream
() <<
"
const style::color &white(_palette.white()); // special color
\n
"
;
}
bool
result = module_.
enumVariables
([&](
const
Variable &variable) ->
bool
{
auto
name = variable.
name
.
back
();
auto
type =
typeToString
(variable.
value
.
type
());
if
(type.
isEmpty
()) {
return
false
;
}
if
(
IsValueInHeader
(variable.
value
.
type
())) {
return
true
;
}
source_->
stream
() <<
"
const
"
<< type <<
"
&
"
<< name <<
"
(
"
;
if
(isPalette_) {
source_->
stream
() <<
"
_palette.
"
<< name <<
"
()
"
;
}
else
{
source_->
stream
() <<
"
_
"
<< name;
}
source_->
stream
() <<
"
);
\n
"
;
return
true
;
});
return
result;
}
bool
Generator::writeSetPaletteColor
() {
source_->
stream
() <<
"
\n
\
void palette_data::finalize(palette &that) {
\n
\
that.compute(0, -1, { 255, 255, 255, 0}); // special color transparent
\n
\
that.compute(1, -1, { 255, 255, 255, 255}); // special color white
\n
"
;
QList<structure::FullName> names;
module_.
enumVariables
([&](
const
Variable &variable) ->
bool
{
names.
push_back
(variable.
name
);
return
true
;
});
QString dataRows;
int
indexInPalette =
2
;
QByteArray checksumString;
checksumString.
append
(
"
&transparent:{ 255, 255, 255, 0 }
"
);
checksumString.
append
(
"
&white:{ 255, 255, 255, 255 }
"
);
auto
result = module_.
enumVariables
([&](
const
Variable &variable) ->
bool
{
auto
name = variable.
name
.
back
();
auto
index = indexInPalette++;
paletteIndices_.
emplace
(name, index);
if
(variable.
value
.
type
().
tag
!= structure::TypeTag::Color) {
return
false
;
}
auto
color = variable.
value
.
Color
();
auto
fallbackIterator = paletteIndices_.
find
(
colorFallbackName
(variable.
value
));
auto
fallbackIndex = (fallbackIterator == paletteIndices_.
end
()) ? -
1
: fallbackIterator->
second
;
auto
assignment =
QString
(
"
{ %1, %2, %3, %4 }
"
).
arg
(color.
red
).
arg
(color.
green
).
arg
(color.
blue
).
arg
(color.
alpha
);
source_->
stream
() <<
"
\t
that.compute(
"
<< index <<
"
,
"
<< fallbackIndex <<
"
,
"
<< assignment <<
"
);
\n
"
;
checksumString.
append
((
'
&
'
+ name +
'
:
'
+ assignment).
toUtf8
());
auto
isCopy = !variable.
value
.
copyOf
().
isEmpty
();
auto
colorString =
paletteColorValue
(color);
auto
fallbackName =
QString
();
if
(fallbackIndex >
1
) {
auto
fallbackVariable = module_.
findVariableInModule
(names[fallbackIndex -
2
], module_);
if
(fallbackVariable && fallbackVariable->
value
.
type
().
tag
== structure::TypeTag::Color) {
fallbackName = fallbackVariable->
name
.
back
();
}
}
auto
value = isCopy ? fallbackName :
'
#
'
+ colorString;
if
(value.
isEmpty
()) {
return
false
;
}
dataRows.
append
(
"
\t
result.push_back({ qstr(
\"
"
+ name +
"
\"
), qstr(
\"
"
+ value +
"
\"
), qstr(
\"
"
+ (isCopy ?
QString
() : fallbackName) +
"
\"
), qstr(
"
+
stringToEncodedString
(variable.
description
) +
"
) });
\n
"
);
return
true
;
});
if
(!result) {
return
false
;
}
auto
count = indexInPalette;
auto
checksum =
base::crc32
(checksumString.
constData
(), checksumString.
size
());
source_->
stream
() <<
"
\n\n
"
;
for
(
const
auto
&[over, under] :
kMustBeContrast
) {
const
auto
overIndex = paletteIndices_.
find
(over);
const
auto
underIndex = paletteIndices_.
find
(under);
if
(overIndex == paletteIndices_.
end
() || underIndex == paletteIndices_.
end
()) {
return
false
;
}
source_->
stream
() <<
"
\t
internal::EnsureContrast(*data(
"
<< overIndex->
second
<<
"
), *data(
"
<< underIndex->
second
<<
"
));
\n
"
;
}
source_->
stream
() <<
"
\
}
\n
\
\n
\
int32 palette_data::Checksum() {
\n
\
return
"
<< checksum <<
"
;
\n
\
}
\n
"
;
source_->
newline
().
pushNamespace
(
"
internal
"
).
newline
();
source_->
stream
() <<
"
\
int GetPaletteIndex(QLatin1String name) {
\n
\
auto size = name.size();
\n
\
auto data = name.data();
\n
"
;
auto
tabs = [](
int
size) {
return
QString
(size,
'
\t
'
);
};
enum
class
UsedCheckType
{
Switch,
If,
UpcomingIf,
};
auto
checkTypes = QVector<UsedCheckType>();
auto
checkLengthHistory = QVector<
int
>(
1
,
0
);
auto
chars =
QString
();
auto
tabsUsed =
1
;
//
Returns true if at least one check was finished.
auto
finishChecksTillKey = [&](
const
QString &key) {
auto
result =
false
;
while
(!chars.
isEmpty
() && !key.
startsWith
(chars)) {
result =
true
;
auto
wasType = checkTypes.
back
();
chars.
resize
(chars.
size
() -
1
);
checkTypes.
pop_back
();
checkLengthHistory.
pop_back
();
if
(wasType == UsedCheckType::Switch || wasType == UsedCheckType::If) {
--tabsUsed;
if
(wasType == UsedCheckType::Switch) {
source_->
stream
() <<
tabs
(tabsUsed) <<
"
break;
\n
"
;
}
if
((!chars.
isEmpty
() && !key.
startsWith
(chars)) || key == chars) {
source_->
stream
() <<
tabs
(tabsUsed) <<
"
}
\n
"
;
}
}
}
return
result;
};
//
Check if we can use "if" for a check on "charIndex" in "it" (otherwise only "switch")
auto
canUseIfForCheck = [](
auto
it,
auto
end,
int
charIndex) {
auto
key = it->
first
;
auto
i = it;
auto
keyStart = key.
mid
(
0
, charIndex);
for
(++i; i != end; ++i) {
auto
nextKey = i->
first
;
if
(nextKey.
mid
(
0
, charIndex) != keyStart) {
return
true
;
}
else
if
(nextKey.
size
() > charIndex && nextKey[charIndex] != key[charIndex]) {
return
false
;
}
}
return
true
;
};
auto
countMinimalLength = [](
auto
it,
auto
end,
int
charIndex) {
auto
key = it->
first
;
auto
i = it;
auto
keyStart = key.
mid
(
0
, charIndex);
auto
result = key.
size
();
for
(++i; i != end; ++i) {
auto
nextKey = i->
first
;
if
(nextKey.
mid
(
0
, charIndex) != keyStart) {
break
;
}
else
if
(nextKey.
size
() > charIndex && result > nextKey.
size
()) {
result = nextKey.
size
();
}
}
return
result;
};
for
(
auto
i = paletteIndices_.
begin
(), e = paletteIndices_.
end
(); i != e; ++i) {
auto
name = i->
first
;
auto
index = i->
second
;
auto
weContinueOldSwitch =
finishChecksTillKey
(name);
while
(chars.
size
() != name.
size
()) {
auto
checking = chars.
size
();
auto
keyChar = name[checking];
auto
usedIfForCheckCount =
0
;
auto
minimalLengthCheck =
int
(
countMinimalLength
(i, e, checking));
for
(; checking + usedIfForCheckCount != name.
size
(); ++usedIfForCheckCount) {
if
(!
canUseIfForCheck
(i, e, checking + usedIfForCheckCount)
||
countMinimalLength
(i, e, checking + usedIfForCheckCount) != minimalLengthCheck) {
break
;
}
}
auto
usedIfForCheck = !weContinueOldSwitch && (usedIfForCheckCount >
0
);
auto
checkLengthCondition =
QString
();
if
(weContinueOldSwitch) {
weContinueOldSwitch =
false
;
}
else
{
checkLengthCondition = (minimalLengthCheck > checkLengthHistory.
back
()) ? (
"
size >=
"
+
QString::number
(minimalLengthCheck)) :
QString
();
if
(!usedIfForCheck) {
source_->
stream
() <<
tabs
(tabsUsed) << (checkLengthCondition.
isEmpty
() ?
QString
() : (
"
if (
"
+ checkLengthCondition +
"
)
"
)) <<
"
switch (data[
"
<< checking <<
"
]) {
\n
"
;
}
}
if
(usedIfForCheck) {
auto
conditions =
QStringList
();
if
(usedIfForCheckCount >
1
) {
conditions.
push_back
(
"
!memcmp(data +
"
+
QString::number
(checking) +
"
,
\"
"
+ name.
mid
(checking, usedIfForCheckCount) +
"
\"
,
"
+
QString::number
(usedIfForCheckCount) +
"
)
"
);
}
else
{
conditions.
push_back
(
"
data[
"
+
QString::number
(checking) +
"
] == '
"
+ keyChar +
"
'
"
);
}
if
(!checkLengthCondition.
isEmpty
()) {
conditions.
push_front
(checkLengthCondition);
}
source_->
stream
() <<
tabs
(tabsUsed) <<
"
if (
"
<< conditions.
join
(
"
&&
"
) <<
"
) {
\n
"
;
checkTypes.
push_back
(UsedCheckType::If);
for
(
auto
i =
1
; i != usedIfForCheckCount; ++i) {
checkTypes.
push_back
(UsedCheckType::UpcomingIf);
chars.
push_back
(keyChar);
checkLengthHistory.
push_back
(
std::max
(
minimalLengthCheck,
checkLengthHistory.
back
()));
keyChar = name[checking + i];
}
}
else
{
source_->
stream
() <<
tabs
(tabsUsed) <<
"
case '
"
<< keyChar <<
"
':
\n
"
;
checkTypes.
push_back
(UsedCheckType::Switch);
}
++tabsUsed;
chars.
push_back
(keyChar);
checkLengthHistory.
push_back
(
std::max
(
minimalLengthCheck,
checkLengthHistory.
back
()));
}
source_->
stream
() <<
tabs
(tabsUsed) <<
"
return (size ==
"
<< chars.
size
() <<
"
) ?
"
<< index <<
"
: -1;
\n
"
;
}
finishChecksTillKey
(
QString
());
source_->
stream
() <<
"
\
\n
\
return -1;
\n
\
}
\n
"
;
source_->
newline
().
popNamespace
().
newline
();
source_->
stream
() <<
"
\
namespace main_palette {
\n
\
\n
\
not_null<const palette*> get() {
\n
\
return &_palette;
\n
\
}
\n
\
\n
\
QList<row> data() {
\n
\
auto result = QList<row>();
\n
\
result.reserve(
"
<< count <<
"
);
\n
\
\n
\
"
<< dataRows <<
"
\n
\
return result;
\n
\
}
\n
\
\n
\
} // namespace main_palette
\n
\
\n
"
;
return
result;
}
bool
Generator::writeVariableInit
() {
if
(!module_.
hasVariables
()) {
return
true
;
}
if
(!
collectUniqueValues
()) {
return
false
;
}
bool
hasUniqueValues = (!fontFamilies_.
isEmpty
() || !iconMasks_.
isEmpty
());
if
(hasUniqueValues) {
source_->
pushNamespace
();
if
(!
writeFontFamiliesInit
()) {
return
false
;
}
if
(!
writeIconValues
()) {
return
false
;
}
source_->
popNamespace
().
newline
();
}
source_->
stream
() <<
"
\
void init_
"
<< baseName_ <<
"
(int scale) {
\n
\
if (inited) return;
\n
\
inited = true;
\n\n
"
;
if
(module_.
hasIncludes
()) {
bool
writtenAtLeastOne =
false
;
bool
result = module_.
enumIncludes
([&](
const
Module &
module
) ->
bool
{
if
(
module
.
hasVariables
()) {
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL