-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy pathCSSTheme.java
More file actions
7229 lines (6212 loc) · 304 KB
/
CSSTheme.java
File metadata and controls
7229 lines (6212 loc) · 304 KB
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
/*
* Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codename1.designer.css;
import com.codename1.io.JSONParser;
import com.codename1.io.Util;
import com.codename1.processing.Result;
import com.codename1.ui.Component;
import com.codename1.ui.Display;
import com.codename1.ui.EditorTTFFont;
import com.codename1.ui.EncodedImage;
import com.codename1.ui.Font;
import com.codename1.ui.Image;
import com.codename1.ui.animations.AnimationAccessor;
import com.codename1.ui.plaf.Accessor;
import com.codename1.ui.plaf.CSSBorder;
import com.codename1.ui.plaf.RoundBorder;
import com.codename1.ui.plaf.RoundRectBorder;
import com.codename1.ui.plaf.Style;
import com.codename1.ui.util.EditableResourcesForCSS;
import com.codename1.ui.util.EditableResources;
import com.codename1.ui.util.Resources;
import java.io.ByteArrayInputStream;
import java.io.CharArrayReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.metadata.IIOMetadataNode;
import javax.imageio.stream.ImageInputStream;
import org.w3c.css.sac.AttributeCondition;
import org.w3c.css.sac.CSSException;
import org.w3c.css.sac.CSSParseException;
import org.w3c.css.sac.Condition;
import org.w3c.css.sac.ConditionalSelector;
import org.w3c.css.sac.DocumentHandler;
import org.w3c.css.sac.ElementSelector;
import org.w3c.css.sac.ErrorHandler;
import org.w3c.css.sac.InputSource;
import org.w3c.css.sac.LexicalUnit;
import org.w3c.css.sac.Parser;
import org.w3c.css.sac.SACMediaList;
import org.w3c.css.sac.Selector;
import org.w3c.css.sac.SelectorList;
import org.w3c.css.sac.SimpleSelector;
import org.w3c.css.sac.helpers.ParserFactory;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.flute.parser.ParseException;
/**
*
* @author shannah
*/
public class CSSTheme {
private boolean refreshImages;
URL baseURL;
File cssFile = new File("test.css");
File resourceFile = new File("test.css.res");
Element anyNodeStyle = new Element();
Map<String,Element> elements = new LinkedHashMap<String,Element>();
Map<String,LexicalUnit> constants = new LinkedHashMap<String,LexicalUnit>();
EditableResources res;
private String themeName = "Theme";
private ImagesMetadata imagesMetadata = new ImagesMetadata();
private List<FontFace> fontFaces = new ArrayList<FontFace>();
public static final int DEFAULT_TARGET_DENSITY = com.codename1.ui.Display.DENSITY_HD;
public static final String[] supportedNativeBorderTypes = new String[]{
"none",
"line",
"bevel",
"etched",
"solid"
};
public static boolean isBorderTypeNativelySupported(String type) {
for (String str : supportedNativeBorderTypes) {
if (str.equals(type)) {
return true;
}
}
return false;
}
private int targetDensity = DEFAULT_TARGET_DENSITY;
private double minDpi = 120;
private double maxDpi = 480;
private double currentDpi = 480;
private static class XYVal {
double x;
double y;
String axis;
boolean valid;
}
static boolean isGradient(LexicalUnit background) {
if (background != null) {
if (background.getLexicalUnitType() == LexicalUnit.SAC_FUNCTION && "linear-gradient".equals(background.getFunctionName())) {
return true;
} else if (background.getLexicalUnitType() == LexicalUnit.SAC_FUNCTION && "radial-gradient".equals(background.getFunctionName())) {
return true;
}
}
return false;
}
class ImageMetadata {
private String imageName;
int sourceDpi;
public ImageMetadata(String imageName, int sourceDpi) {
this.imageName = imageName;
this.sourceDpi = sourceDpi;
}
}
class ImagesMetadata {
private Map<String,ImageMetadata> images = new LinkedHashMap<>();
public void addImageMetadata(ImageMetadata data) {
images.put(data.imageName, data);
}
public ImageMetadata get(String name) {
return images.get(name);
}
void load(Resources res) throws IOException {
images.clear();
String metadataStr = (String)res.getTheme(themeName).getOrDefault("@imageMetadata", "{}");
JSONParser parser = new JSONParser();
Map metadataMap = parser.parseJSON(new StringReader(metadataStr));
for (String key : (Set<String>)metadataMap.keySet()) {
Map data = (Map)metadataMap.get(key);
int sourceDpi = (int)Math.round((data.containsKey("sourceDpi") ? ((Number)data.get("sourceDpi")).intValue() :
currentDpi));
String name = (String)data.get("imageName");
ImageMetadata md = new ImageMetadata(name, sourceDpi);
addImageMetadata(md);
}
}
void store(EditableResources res) {
Map map = new LinkedHashMap();
for (String key : images.keySet()) {
ImageMetadata md = images.get(key);
Map data = new LinkedHashMap();
data.put("imageName", md.imageName);
data.put("sourceDpi", md.sourceDpi);
map.put(md.imageName, data);
}
res.setThemeProperty(themeName, "@imageMetadata", Result.fromContent(map).toString());
}
}
static class CN1Gradient {
/**
* One of {@link Style#BACKGROUND_GRADIENT_LINEAR_HORIZONTAL}, {@link Style#BACKGROUND_GRADIENT_LINEAR_VERTICAL}, or
* {@link Style#BACKGROUND_GRADIENT_RADIAL}.
*/
int type;
int startColor;
int endColor;
float gradientX;
float gradientY;
float size;
byte bgTransparency;
String reason;
/**
* Flag to indicate whether this gradient is valid
* Only gradients that can be completely reproduced using CN1
* are valid. E.g. if the opacity of the start color is different than
* the end color, or there are multiple stages, or other parameters
* that can't be expressed as a CN1 gradient style, then this gradient won't
* be used and a 9-piece border will be generated as a fallback.
*/
boolean valid;
void parse(ScaledUnit background) {
if (background != null) {
if (background.getLexicalUnitType() == LexicalUnit.SAC_FUNCTION && "linear-gradient".equals(background.getFunctionName())) {
parseLinearGradient(background);
} else if (background.getLexicalUnitType() == LexicalUnit.SAC_FUNCTION && "radial-gradient".equals(background.getFunctionName())) {
parseRadialGradient(background);
}
}
}
private XYVal parseTransformCoordVal(String val) {
XYVal out = new XYVal();
switch (val) {
case "center" : out.x = 0.5; out.y = 0.5; break;
case "left" : out.x = 0; out.y = -1; out.axis = "x"; break;
case "right" : out.x = 1; out.y = -1; out.axis = "x"; break;
case "bottom" : out.x = -1;; out.y = 1; out.axis = "y"; break;
case "top" : out.x = -1; out.y = 0; out.axis = "y"; break;
default: return out;
}
out.valid = true;
return out;
}
private XYVal parseTransformCoordVal(double val, boolean x) {
XYVal out = new XYVal();
if (x) {
out.y = 0.5;
out.x = val / 100;
out.axis = "x";
} else {
out.y = val / 100;
out.x = 0.5;
out.axis = "y";
}
out.valid = true;
return out;
}
private void parseRadialGradient(ScaledUnit background) {
if (background == null || background.getLexicalUnitType() != LexicalUnit.SAC_FUNCTION || !"radial-gradient".equals(background.getFunctionName())) {
return;
}
ScaledUnit params = (ScaledUnit)background.getParameters();
if (params == null) {
reason = "No parameters found in radial-gradient() function [1]";
return;
}
ScaledUnit param1 = params;
ScaledUnit param2 = null;
double relX = 0.5;
double relY = 0.5;
double relSize = 1.0;
int step = 0;
while (param1 != null) {
// Parse the first parameter of radial-gradient
switch (param1.getLexicalUnitType()) {
case LexicalUnit.SAC_IDENT: {
switch (param1.getStringValue()) {
case "circle": {
if (step != 0) {
reason = "Unrecognized syntax for radial gradient [2]";
return;
}
step++;
ScaledUnit nex = (ScaledUnit)param1.getNextLexicalUnit();
if (nex == null) {
reason = "Unrecognized syntax for radial gradient [3]";
return;
}
if (nex.getLexicalUnitType() == LexicalUnit.SAC_OPERATOR_COMMA) {
// It's a circle at center
param1 = null;
param2 = (ScaledUnit)nex.getNextLexicalUnit();
break;
}
param1 = nex;
//reason = "Unsupported parameter following 'circle' in radial gradient.";
//return;
break;
}
case "closest-side" : {
// This is the only extent value that is supported.
step++;
param1 = (ScaledUnit)param1.getNextLexicalUnit();
if (param1 != null && param1.getLexicalUnitType() == LexicalUnit.SAC_OPERATOR_COMMA) {
param1 = null;
}
break;
}
case "at" : {
ScaledUnit nex = (ScaledUnit)param1.getNextLexicalUnit();
if (nex == null) {
reason = "Invalid syntax for radial-gradient position. 'at' followed by nothing. [4]";
return;
}
if (nex.getLexicalUnitType() == LexicalUnit.SAC_IDENT || nex.getLexicalUnitType() == LexicalUnit.SAC_PERCENTAGE) {
XYVal val, val2;
if (nex.getLexicalUnitType() == LexicalUnit.SAC_PERCENTAGE) {
val = parseTransformCoordVal(nex.getNumericValue(), true);
} else {
val = parseTransformCoordVal(nex.getStringValue());
}
if (!val.valid) {
reason = "Invalid position coordinate for radial-gradient. [5]";
return;
}
if ("x".equals(val.axis)) {
relX = val.x;
} else if ("y".equals(val.axis)) {
relY = val.y;
} else {
relX = val.x;
relY = val.y;
}
nex = (ScaledUnit)nex.getNextLexicalUnit();
if (nex == null) {
reason = "Invalid radial-gradient syntax. No color information provided. [6]";
return;
}
if (nex.getLexicalUnitType() == LexicalUnit.SAC_OPERATOR_COMMA) {
// we have our position.
param2 = (ScaledUnit)nex.getNextLexicalUnit();
param1 = null;
break;
}
if (nex.getLexicalUnitType() == LexicalUnit.SAC_IDENT || nex.getLexicalUnitType() == LexicalUnit.SAC_PERCENTAGE) {
if (nex.getLexicalUnitType() == LexicalUnit.SAC_PERCENTAGE) {
val2 = parseTransformCoordVal(nex.getNumericValue(), "y".equals(val.axis));
} else {
val2 = parseTransformCoordVal(nex.getStringValue());
}
if ("x".equals(val2.axis)) {
relX = val2.x;
} else if ("y".equals(val2.axis)) {
relY = val2.y;
} else if ("x".equals(val.axis)) {
relY = val2.y;
} else if ("y".equals(val.axis)) {
relX = val2.x;
} else {
relY = val2.y;
}
}
nex = (ScaledUnit)nex.getNextLexicalUnit();
if (nex == null || nex.getLexicalUnitType() != LexicalUnit.SAC_OPERATOR_COMMA) {
reason = "Invalid radial-gradient syntax. No color information provided. [7]";
return;
}
param1 = null;
param2 = (ScaledUnit) nex.getNextLexicalUnit();
break;
}
break;
}
default:
reason = "Unsupported syntax for radial-gradient. ("+param1.getStringValue()+") [8]";
return;
}
}
}
}
if (param2 == null) {
reason = "No color information provided (param2==null) [9]";
return;
}
Integer color1 = null;
Integer color2 = null;
int alpha = 0;
while (param2 != null) {
// parse 2nd param of radial-gradient
if (color1 == null) {
color1 = getColorInt(param2);
alpha = getColorAlphaInt(param2);
ScaledUnit nex = (ScaledUnit)param2.getNextLexicalUnit();
if (nex == null) {
reason = "Invalid radial gradient syntax. Missing 2nd color [11]";
return;
}
if (nex.getLexicalUnitType() == LexicalUnit.SAC_PERCENTAGE) {
if (Math.abs(nex.getNumericValue()) > 0.0001) {
reason = "CN1 native gradients must have start color at 0%. Falling back to use image background. [12]";
return;
}
nex = (ScaledUnit)nex.getNextLexicalUnit();
}
if (nex == null || nex.getLexicalUnitType()!= LexicalUnit.SAC_OPERATOR_COMMA) {
reason = "Invalid radial gradient syntax. Missing 2nd color";
return;
}
param2 = (ScaledUnit)nex.getNextLexicalUnit();
} else if (color2 == null) {
color2 = getColorInt(param2);
int alpha2 = getColorAlphaInt(param2);
if (alpha2 != alpha) {
reason = "Codename One only native supports gradients with the same alpha for start and end colors [13]";
return;
}
ScaledUnit nex = (ScaledUnit)param2.getNextLexicalUnit();
if (nex == null) {
break;
} else if (nex.getLexicalUnitType() == LexicalUnit.SAC_PERCENTAGE) {
relSize = nex.getNumericValue() / 100;
}
nex = (ScaledUnit)nex.getNextLexicalUnit();
// This should be the last parameter
if (nex != null) {
reason = "Only radial gradients with two color stops are supported natively in CN1. Falling back to image background. [14]";
return;
}
param2 = null;
}
}
if (color1 == null || color2 == null) {
reason = "Failed to set either start or end color when parsing radial-gradient [15]";
return;
}
type = Style.BACKGROUND_GRADIENT_RADIAL;
gradientX = (float)relX;
gradientY = (float)relY;
this.bgTransparency = (byte)alpha;
this.startColor = color1;
this.endColor = color2;
this.size = (float)relSize;
this.valid = true;
}
private void parseLinearGradient(ScaledUnit background) {
ScaledUnit linearGradientFunc = null;
int gradientType = 0;
boolean reverse = false;
Integer startColor = null;
Integer endColor = null;
int alpha = -1;
loop: while (background != null) {
switch (background.getLexicalUnitType()) {
case LexicalUnit.SAC_FUNCTION:
if ("linear-gradient".equals(background.getFunctionName())) {
linearGradientFunc = background;
break loop;
}
}
background = (ScaledUnit)background.getNextLexicalUnit();
}
if (linearGradientFunc == null) {
return;
}
ScaledUnit params = (ScaledUnit)linearGradientFunc.getParameters();
if (params == null) {
return;
}
ScaledUnit param1 = params;
switch (param1.getLexicalUnitType()) {
case LexicalUnit.SAC_IDENT: {
String identVal = param1.getStringValue();
if ("to".equals(identVal)) {
param1 = (ScaledUnit)param1.getNextLexicalUnit();
if (param1 == null) {
reason = "Invalid linear-gradient syntax. 'to' must be followed by a side.";
return;
}
if (param1.getLexicalUnitType() == LexicalUnit.SAC_IDENT) {
String sideStr = param1.getStringValue();
switch (sideStr) {
case "top" : {
gradientType = Style.BACKGROUND_GRADIENT_LINEAR_VERTICAL;
reverse = true;
break;
}
case "bottom" : {
gradientType = Style.BACKGROUND_GRADIENT_LINEAR_VERTICAL;
break;
}
case "left" : {
gradientType = Style.BACKGROUND_GRADIENT_LINEAR_HORIZONTAL;
reverse = true;
break;
}
case "right": {
gradientType = Style.BACKGROUND_GRADIENT_LINEAR_HORIZONTAL;
break;
}
default :
reason = "Unrecognized side identifier '"+sideStr+"'. Expected top, left, bottom, or right";
return;
}
} else {
reason = "Unsupported syntax following to ident in linear-gradient.";
return;
}
} else {
reason = "Unsupported syntax for first parameter of linear-gradient. Only 'to' and 'degree' values are supported";
return;
}
break;
}
case LexicalUnit.SAC_RADIAN:
case LexicalUnit.SAC_DEGREE: {
double degreeVal = params.getNumericValue();
degreeVal = (params.getLexicalUnitType() == LexicalUnit.SAC_RADIAN) ?
(degreeVal * 180.0/Math.PI) : degreeVal;
if (Math.abs(degreeVal) < 0.0001) {
gradientType = Style.BACKGROUND_GRADIENT_LINEAR_VERTICAL;
reverse = true; //bottom to top
} else if (Math.abs(degreeVal - 90) < 0.0001 ) {
gradientType = Style.BACKGROUND_GRADIENT_LINEAR_HORIZONTAL;
} else if (Math.abs(degreeVal - 180) < 0.0001) {
gradientType = Style.BACKGROUND_GRADIENT_LINEAR_VERTICAL;
} else if (Math.abs(degreeVal - 270) < 0.0001) {
gradientType = Style.BACKGROUND_GRADIENT_LINEAR_HORIZONTAL;
reverse = true;
} else {
reason = "Only 0, 90, 180, and 270 degrees supported.";
return;
}
break;
}
default:
System.err.println("Expected degree. Found lexical type" +param1.getLexicalUnitType());
System.err.println("Currently only degree and radian parameters supported for first arg of linear-gradient");
// We only support degree and radian first param right now.
return;
}
ScaledUnit param2 = (ScaledUnit)param1.getNextLexicalUnit();
if (param2 == null) {
return;
}
if (param2.getLexicalUnitType() == LexicalUnit.SAC_OPERATOR_COMMA) {
param2 = (ScaledUnit)param2.getNextLexicalUnit();
}
if (param2 == null) {
return;
}
switch (param2.getLexicalUnitType()) {
case LexicalUnit.SAC_IDENT:
case LexicalUnit.SAC_FUNCTION:
case LexicalUnit.SAC_RGBCOLOR:
case LexicalUnit.SAC_ATTR:
if (reverse) {
endColor = getColorInt(param2);
} else {
startColor = getColorInt(param2);
}
alpha = getColorAlphaInt(param2);
break;
default:
System.err.println("Expected color for 2nd parameter of linear-gradient");
return;
}
ScaledUnit param3 = (ScaledUnit)param2.getNextLexicalUnit();
if (param3 == null) {
return;
}
if (param3.getLexicalUnitType() == LexicalUnit.SAC_OPERATOR_COMMA) {
param3 = (ScaledUnit)param3.getNextLexicalUnit();
}
if (param3 == null) {
return;
}
switch (param3.getLexicalUnitType()) {
case LexicalUnit.SAC_IDENT:
case LexicalUnit.SAC_FUNCTION:
case LexicalUnit.SAC_RGBCOLOR:
case LexicalUnit.SAC_ATTR:
if (reverse) {
startColor = getColorInt(param3);
} else {
endColor = getColorInt(param3);
}
int alpha2 = getColorAlphaInt(param3);
if (alpha2 != alpha) {
// Alphas don't match so the gradient can't be expressed using CN1 gradients.
reason = "alphas of start and end colors don't match";
return;
}
break;
default:
System.err.println("Error processing background rule "+background+" for selector "+currentId);
System.err.println("Expecting color for param 3 of linear-gradient but found "+param3+" of type "+param3.getLexicalUnitType());
System.err.println("This gradient will need to be generated as an image border, which will slow down CSS compilation.");
//new RuntimeException("Error processing background rule "+background).printStackTrace(System.err);
return;
}
this.startColor = startColor;
this.endColor = endColor;
this.type = gradientType;
this.bgTransparency = (byte)alpha;
this.valid = true;
/*
Object[] out = new Object[] {
startColor,
endColor,
0f,
0f,
0f
};
*/
}
Object[] getThemeBgGradient() {
if (!valid) {
return null;
}
return new Object[]{
startColor,
endColor,
gradientX,
gradientY,
size
};
}
byte getBgTransparency() {
if (valid) {
return 0;
}
return bgTransparency;
}
}
//private int currentScreenWidth=1080;
//private int currentScreenHeight=1920;
/*
LexicalUnit px(LexicalUnit val) {
switch (val.getLexicalUnitType()) {
case LexicalUnit.SAC_CENTIMETER:
case LexicalUnit.SAC_MILLIMETER:
case LexicalUnit.SAC_PIXEL:
}
return val;
}
*/
/*
double px(double val) {
switch (targetDensity) {
case com.codename1.ui.Display.DENSITY_MEDIUM:
return val;
// Generate High Resolution MultiImage
case com.codename1.ui.Display.DENSITY_HIGH:
return val*480.0/320.0;
// Generate Very High Resolution MultiImage
case com.codename1.ui.Display.DENSITY_VERY_HIGH:
return val*2.0;
// Generate HD Resolution MultiImage
case com.codename1.ui.Display.DENSITY_HD:
return val*1080.0/320.0;
// Generate HD560 Resolution MultiImage
case com.codename1.ui.Display.DENSITY_560:
return val*1500.0/320.0;
// Generate HD2 Resolution MultiImage
case com.codename1.ui.Display.DENSITY_2HD:
return val*2000.0/320.0;
// Generate 4k Resolution MultiImage
case com.codename1.ui.Display.DENSITY_4K:
return val*2500.0/320.0;
}
throw new RuntimeException("Unsupported density");
}
*/
public class FontFace {
File fontFile;
LexicalUnit fontFamily;
LexicalUnit src;
LexicalUnit fontWeight;
LexicalUnit fontStretch;
LexicalUnit fontStyle;
URL getURL() {
try {
if (src == null) {
return null;
}
switch (src.getLexicalUnitType()) {
case LexicalUnit.SAC_URI:
String url = src.getStringValue();
if (url.startsWith("github://")) {
//url(https://raw.githubusercontent.com/google/fonts/master/ofl/sourcesanspro/SourceSansPro-Light.ttf);
url = "https://raw.githubusercontent.com/" + url.substring(9).replace("/blob/master/", "/master/");
return new URL(url);
}
if (url.startsWith("http://") || url.startsWith("https://")) {
return new URL(url);
} else {
return new URL(baseURL, url);
}
}
return null;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
File getFontFile() {
if (fontFile == null) {
try {
URL url = getURL();
if (url == null) {
return null;
}
File parentDir = cssFile.getParentFile();
if (url.getProtocol().startsWith("http")) {
// If it is remote, check so see if we've already downloaded
// the font to the current directory.
String fontName = java.net.URLDecoder.decode(url.getPath(), "UTF-8");
if (fontName.indexOf("/") != -1) {
fontName = fontName.substring(fontName.lastIndexOf("/")+1);
}
File tmpFontFile = new File(parentDir, fontName);
if (tmpFontFile.exists()) {
fontFile = tmpFontFile;
} else {
InputStream is = url.openStream();
FileOutputStream fos = new FileOutputStream(tmpFontFile);
Util.copy(is, fos);
Util.cleanup(is);
Util.cleanup(fos);
fontFile = tmpFontFile;
}
} else if ("file".equals(url.getProtocol())){
fontFile = new File(url.toURI());
}
} catch (Exception ex) {
Logger.getLogger(CSSTheme.class.getName()).log(Level.SEVERE, null, ex);
throw new RuntimeException(ex);
}
}
if (fontFile != null && resourceFile != null) {
File srcDir = resourceFile.getParentFile();
File deployFontFile = new File(srcDir, fontFile.getName());
if (!deployFontFile.exists()) {
try (FileInputStream in = new FileInputStream(fontFile); FileOutputStream out = new FileOutputStream(deployFontFile)) {
Util.copy(in, out);
Util.cleanup(out);
} catch (IOException ioe) {
ioe.printStackTrace();
throw new RuntimeException(ioe);
}
}
}
return fontFile;
}
}
public FontFace createFontFace() {
FontFace f = new FontFace();
fontFaces.add(f);
return f;
}
private static class ScaledUnit implements LexicalUnit {
LexicalUnit src;
double dpi=144;
int screenWidth=640;
int screenHeight=960;
CN1Gradient gradient;
LexicalUnit next, prev, param;
ScaledUnit(LexicalUnit src, double dpi, int screenWidth, int screenHeight) {
this.src = src;
this.dpi = dpi;
this.screenWidth = screenWidth;
this.screenHeight = screenHeight;
}
public void setParameters(LexicalUnit params) {
this.param = params;
}
private boolean nextLexicalUnitNull;
public void setNextLexicalUnit(LexicalUnit next) {
this.next = next;
nextLexicalUnitNull = (next == null);
}
private boolean prevLexicalUnitNull;
public void setPrevLexicalUnit(LexicalUnit prev) {
this.prev = prev;
prevLexicalUnitNull = (prev == null);
}
/**
* If this lexical unit is a gradient function and can be expressed as a CN1 gradient
* this will return that gradient. Otherwise it will return null.
* @return
*/
public CN1Gradient getCN1Gradient() {
if (gradient == null && isGradient(src)) {
gradient = new CN1Gradient();
gradient.parse(this);
if (!gradient.valid && gradient.reason != null) {
System.err.println("Selector with id "+currentId+" Gradient not valid: "+gradient.reason);
}
}
if (gradient != null && gradient.valid) {
return gradient;
}
return null;
}
public boolean isCN1Gradient() {
CN1Gradient g = getCN1Gradient();
return (g != null && g.valid);
}
@Override
public short getLexicalUnitType() {
return src.getLexicalUnitType();
}
@Override
public LexicalUnit getNextLexicalUnit() {
if (next != null || nextLexicalUnitNull) return next;
LexicalUnit nex = src.getNextLexicalUnit();
LexicalUnit out = nex == null ? null : new ScaledUnit(nex, dpi, screenWidth, screenHeight);
if (out != null) next = out;
return out;
}
private static boolean hasCycle(LexicalUnit lu, ScaledUnit su) {
if (lu == su) return true;
if (su.src instanceof ScaledUnit) {
return hasCycle(lu, (ScaledUnit)su.src);
}
return false;
}
public ScaledUnit getNextNumericUnit() {
ScaledUnit nex = (ScaledUnit)getNextLexicalUnit();
while (nex != null) {
switch (nex.getLexicalUnitType()) {
case LexicalUnit.SAC_INTEGER:
case LexicalUnit.SAC_REAL:
return nex;
}
nex = (ScaledUnit)nex.getNextLexicalUnit();
}
return null;
}
@Override
public LexicalUnit getPreviousLexicalUnit() {
if (this.prev != null || prevLexicalUnitNull) return this.prev;
LexicalUnit prev = src.getPreviousLexicalUnit();
LexicalUnit out = prev == null ? null : new ScaledUnit(prev, dpi, screenWidth, screenHeight);
if (out != null) this.prev = out;
return out;
}
@Override
public int getIntegerValue() {
return src.getIntegerValue();
}
@Override
public float getFloatValue() {
return src.getFloatValue();
}
public double getNumericValue() {
switch (src.getLexicalUnitType()) {
case LexicalUnit.SAC_INTEGER:
return src.getIntegerValue();
default:
return src.getFloatValue();
}
}