From ec872fa592c4795140f3b7fc804af8e3bdae8bb3 Mon Sep 17 00:00:00 2001 From: ScientificWare Date: Sat, 10 Sep 2022 14:52:47 +0200 Subject: [PATCH 01/26] Adds new color hex formats. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CSS level 4 - defines color hex code as #[2 digits Red][2 digits Green][2 digits Blue][2 digits Alpha]. With digit 0 ... f. - allows, webpage passes 3, 4, 6 or 8 digit color code. - 3 digits #[R][G][B] ........ represents #[RR][GG][BB]FF - 4 digits #[R][G][B][A] ..... represents #[RR][GG][BB][AA] - 6 digits #[RR][GG][BB] ..... represents #[RR][GG][BB]FF - 8 digits #[RR][GG][BB][AA] . represents #[RR][GG][BB][AA] Becareful ! In java.awt.Color hex #[2 digits Alpha][2 digits Red][2 digits Green][2 digits Blue] Comme cette méthode est définie dans CSS, elle doit traiter uniquement le format CSS Level 4. According notes below the current OpenJDK implementation is - 3 digits #[R][G][B] represents #[RR][GG][BB]FF - 6 digits #[R][G][B] represents #[RR][GG][BB]FF --- .../classes/javax/swing/text/html/CSS.java | 65 +++++++++++++------ 1 file changed, 44 insertions(+), 21 deletions(-) diff --git a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java index 22fa96b615337..50661da6c83c2 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java @@ -1350,19 +1350,32 @@ else if (str.length() < 2) return colorstr; } - /** - * Convert a "#FFFFFF" hex string to a Color. - * If the color specification is bad, an attempt - * will be made to fix it up. - */ - static final Color hexToColor(String value) { - String digits; - int n = value.length(); - if (value.startsWith("#")) { - digits = value.substring(1, Math.min(value.length(), 7)); - } else { - digits = value; - } + /** + * Convert a "#FFF", "#FFFF", "#FFFFFF" or "#FFFFFFFF" hex string to a Color. + * If the color specification is bad, an attempt + * will be made to fix it up. + */ + static final Color hexToColor(String digits) { + int n = digits.length(); + if (digits.startsWith("#")) { + digits = digits.substring(1, Math.min(n, 9)); + n--; + } + // CSS level 4 + // - defines color hex code as #[2 digits Red][2 digits Green][2 digits Blue][2 digits Alpha]. With digit 0 ... f. + // - allows, webpage passes 3, 4, 6 or 8 digit color code. + // - 3 digits #[R][G][B] ........ represents #[RR][GG][BB]FF + // - 4 digits #[R][G][B][A] ..... represents #[RR][GG][BB][AA] + // - 6 digits #[RR][GG][BB] ..... represents #[RR][GG][BB]FF + // - 8 digits #[RR][GG][BB][AA] . represents #[RR][GG][BB][AA] + // + // Becareful ! In java.awt.Color hex #[2 digits Alpha][2 digits Red][2 digits Green][2 digits Blue] + // Since this method is defined in CSS.java, it's must only take in charge CSS Level 4 color format. + // + // According notes below the current OpenJDK implementation is + // - 3 digits #[R][G][B] represents #[RR][GG][BB]FF + // - 6 digits #[R][G][B] represents #[RR][GG][BB]FF + // // Some webpage passes 3 digit color code as in #fff which is // decoded as #000FFF resulting in blue background. // As per https://www.w3.org/TR/CSS1/#color-units, @@ -1370,21 +1383,31 @@ static final Color hexToColor(String value) { // (#rrggbb) by replicating digits, not by adding zeros. // This makes sure that white (#ffffff) can be specified with the short notation // (#fff) and removes any dependencies on the color depth of the display. - if (digits.length() == 3) { + if (n == 3 && hex.matcher(digits).matches()) { final String r = digits.substring(0, 1); final String g = digits.substring(1, 2); final String b = digits.substring(2, 3); - digits = String.format("%s%s%s%s%s%s", r, r, g, g, b, b); + digits = String.format("%s%s%s%s%s%sff", r, r, g, g, b, b); + } else if (n==4 && hex.matcher(digits).matches()) { + final String r = digits.substring(0, 1); + final String g = digits.substring(1, 2); + final String b = digits.substring(2, 3); + final String a = digits.substring(3, 4); + digits = String.format("%s%s%s%s%s%s%s%s", r, r, g, g, b, b, a, a); + } else if (n == 6 && hex.matcher(digits).matches()) { + digits = String.format("%sff", digits); + } else if (n != 8 || !hex.matcher(digits).matches()) { + return null; } - String hstr = "0x" + digits; - Color c; try { - c = Color.decode(hstr); + Integer intValue = Integer.parseUnsignedInt(digits, 16); + int l = intValue.intValue(); + return new Color((int)((l >> 24) & 0xFF),(int)((l >> 16) & 0xFF), (int)((l >> 8) & 0xFF), (int)(l & 0xFF)); } catch (NumberFormatException nfe) { - c = null; + System.out.println(nfe); + return null; } - return c; - } + } /** * Convert a color string such as "RED" or "#NNNNNN" or "rgb(r, g, b)" From 04f2d689e3bd1c4b2968e0a25e8105d56af3d8ff Mon Sep 17 00:00:00 2001 From: ScientificWare Date: Sun, 11 Sep 2022 01:19:29 +0200 Subject: [PATCH 02/26] Typo. Typo in comment. --- src/java.desktop/share/classes/javax/swing/text/html/CSS.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java index 50661da6c83c2..49d566d9a89f0 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java @@ -1370,7 +1370,7 @@ static final Color hexToColor(String digits) { // - 8 digits #[RR][GG][BB][AA] . represents #[RR][GG][BB][AA] // // Becareful ! In java.awt.Color hex #[2 digits Alpha][2 digits Red][2 digits Green][2 digits Blue] - // Since this method is defined in CSS.java, it's must only take in charge CSS Level 4 color format. + // Since this method is defined in CSS.java, it must only take in charge CSS Level 4 color format. // // According notes below the current OpenJDK implementation is // - 3 digits #[R][G][B] represents #[RR][GG][BB]FF From fe75e26ebc3b4d27a37cd1f6e25766157800dcc8 Mon Sep 17 00:00:00 2001 From: ScientificWare Date: Sun, 11 Sep 2022 11:24:09 +0200 Subject: [PATCH 03/26] Adds missing import and declaration. This implementation uses a Pattern instance and parses the string argument as an unsigned integer with radix 16. - The pattern declaration and import are missing. - The parseUnsignedInt method can send a NumberFormatException, its import is missing. --- .../share/classes/javax/swing/text/html/CSS.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java index 49d566d9a89f0..87dedee90fad0 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java @@ -33,11 +33,13 @@ import java.io.ObjectInputStream; import java.io.Serial; import java.io.Serializable; +import java.lang.NumberFormatException; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.Enumeration; import java.util.Hashtable; +import java.util.regex.Pattern; import javax.swing.ImageIcon; import javax.swing.SizeRequirements; @@ -1350,6 +1352,8 @@ else if (str.length() < 2) return colorstr; } + private static Pattern hex = Pattern.compile("\\p{XDigit}+"); + /** * Convert a "#FFF", "#FFFF", "#FFFFFF" or "#FFFFFFFF" hex string to a Color. * If the color specification is bad, an attempt @@ -1360,7 +1364,7 @@ static final Color hexToColor(String digits) { if (digits.startsWith("#")) { digits = digits.substring(1, Math.min(n, 9)); n--; - } + } // CSS level 4 // - defines color hex code as #[2 digits Red][2 digits Green][2 digits Blue][2 digits Alpha]. With digit 0 ... f. // - allows, webpage passes 3, 4, 6 or 8 digit color code. @@ -1404,7 +1408,6 @@ static final Color hexToColor(String digits) { int l = intValue.intValue(); return new Color((int)((l >> 24) & 0xFF),(int)((l >> 16) & 0xFF), (int)((l >> 8) & 0xFF), (int)(l & 0xFF)); } catch (NumberFormatException nfe) { - System.out.println(nfe); return null; } } From 146bba4e7f4deeef4ce9e84f158f4ce8b519d007 Mon Sep 17 00:00:00 2001 From: ScientificWare Date: Sun, 11 Sep 2022 14:00:57 +0200 Subject: [PATCH 04/26] Remove NumberFormatException java.lang.NumberFormatException is imported by default. --- src/java.desktop/share/classes/javax/swing/text/html/CSS.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java index 87dedee90fad0..bc15a638cdc0e 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java @@ -33,7 +33,6 @@ import java.io.ObjectInputStream; import java.io.Serial; import java.io.Serializable; -import java.lang.NumberFormatException; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; From d1301f3f19f7fba7c83d6d26e7e460fe91861bd9 Mon Sep 17 00:00:00 2001 From: ScientificWare Date: Sun, 11 Sep 2022 14:17:40 +0200 Subject: [PATCH 05/26] Update Copyrigth. Updatre Copyrigth date. --- src/java.desktop/share/classes/javax/swing/text/html/CSS.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java index bc15a638cdc0e..c7fca71f6ff99 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2022, Oracle 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 From 7d7f49fe8a3f88e7fba8dcb68bda30ef891c6598 Mon Sep 17 00:00:00 2001 From: ScientificWare Date: Sun, 11 Sep 2022 19:38:18 +0200 Subject: [PATCH 06/26] Removes int cast. Removes unnecessary integer casts. --- src/java.desktop/share/classes/javax/swing/text/html/CSS.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java index c7fca71f6ff99..e093c062d6ce5 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java @@ -1405,7 +1405,7 @@ static final Color hexToColor(String digits) { try { Integer intValue = Integer.parseUnsignedInt(digits, 16); int l = intValue.intValue(); - return new Color((int)((l >> 24) & 0xFF),(int)((l >> 16) & 0xFF), (int)((l >> 8) & 0xFF), (int)(l & 0xFF)); + return new Color((l >> 24) & 0xFF,(l >> 16) & 0xFF, (l >> 8) & 0xFF, l & 0xFF); } catch (NumberFormatException nfe) { return null; } From dd0281323fba1670ec0d065ceb14e8f3256e9a3b Mon Sep 17 00:00:00 2001 From: Guy Abossolo Foh - ScientificWare Date: Tue, 13 Sep 2022 21:02:54 +0200 Subject: [PATCH 07/26] - This modifies the CSS.java Copyright date. - This modifies CSS.java hexToColor method, - by adding two hex code formats, - by treating only 2, 3, 4 and 8 digits well formated hex code, all other string returns null. --- .../classes/javax/swing/text/html/CSS.java | 71 +++++++++++++------ 1 file changed, 48 insertions(+), 23 deletions(-) diff --git a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java index 22fa96b615337..78ffc5a302c12 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2022, Oracle 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 @@ -38,6 +38,7 @@ import java.util.ArrayList; import java.util.Enumeration; import java.util.Hashtable; +import java.util.regex.Pattern; import javax.swing.ImageIcon; import javax.swing.SizeRequirements; @@ -1350,19 +1351,34 @@ else if (str.length() < 2) return colorstr; } - /** - * Convert a "#FFFFFF" hex string to a Color. - * If the color specification is bad, an attempt - * will be made to fix it up. - */ - static final Color hexToColor(String value) { - String digits; - int n = value.length(); - if (value.startsWith("#")) { - digits = value.substring(1, Math.min(value.length(), 7)); - } else { - digits = value; - } + private static Pattern hex = Pattern.compile("\\p{XDigit}+"); + + /** + * Convert a "#FFF", "#FFFF", "#FFFFFF" or "#FFFFFFFF" hex string to a Color. + * If the color specification is bad, an attempt + * will be made to fix it up. + */ + static final Color hexToColor(String digits) { + int n = digits.length(); + if (digits.startsWith("#")) { + digits = digits.substring(1, Math.min(n, 9)); + n--; + } + // CSS level 4 + // - defines color hex code as #[2 digits Red][2 digits Green][2 digits Blue][2 digits Alpha]. With digit 0 ... f. + // - allows, webpage passes 3, 4, 6 or 8 digit color code. + // - 3 digits #[R][G][B] ........ represents #[RR][GG][BB]FF + // - 4 digits #[R][G][B][A] ..... represents #[RR][GG][BB][AA] + // - 6 digits #[RR][GG][BB] ..... represents #[RR][GG][BB]FF + // - 8 digits #[RR][GG][BB][AA] . represents #[RR][GG][BB][AA] + // + // Becareful ! In java.awt.Color hex #[2 digits Alpha][2 digits Red][2 digits Green][2 digits Blue] + // Since this method is defined in CSS.java, it must only take in charge CSS Level 4 color format. + // + // According notes below the current OpenJDK implementation is + // - 3 digits #[R][G][B] represents #[RR][GG][BB]FF + // - 6 digits #[R][G][B] represents #[RR][GG][BB]FF + // // Some webpage passes 3 digit color code as in #fff which is // decoded as #000FFF resulting in blue background. // As per https://www.w3.org/TR/CSS1/#color-units, @@ -1370,21 +1386,30 @@ static final Color hexToColor(String value) { // (#rrggbb) by replicating digits, not by adding zeros. // This makes sure that white (#ffffff) can be specified with the short notation // (#fff) and removes any dependencies on the color depth of the display. - if (digits.length() == 3) { + if (n == 3 && hex.matcher(digits).matches()) { final String r = digits.substring(0, 1); final String g = digits.substring(1, 2); final String b = digits.substring(2, 3); - digits = String.format("%s%s%s%s%s%s", r, r, g, g, b, b); + digits = String.format("%s%s%s%s%s%sff", r, r, g, g, b, b); + } else if (n==4 && hex.matcher(digits).matches()) { + final String r = digits.substring(0, 1); + final String g = digits.substring(1, 2); + final String b = digits.substring(2, 3); + final String a = digits.substring(3, 4); + digits = String.format("%s%s%s%s%s%s%s%s", r, r, g, g, b, b, a, a); + } else if (n == 6 && hex.matcher(digits).matches()) { + digits = String.format("%sff", digits); + } else if (n != 8 || !hex.matcher(digits).matches()) { + return null; } - String hstr = "0x" + digits; - Color c; try { - c = Color.decode(hstr); + Integer intValue = Integer.parseUnsignedInt(digits, 16); + int l = intValue.intValue(); + return new Color((l >> 24) & 0xFF,(l >> 16) & 0xFF, (l >> 8) & 0xFF, l & 0xFF); } catch (NumberFormatException nfe) { - c = null; + return null; } - return c; - } + } /** * Convert a color string such as "RED" or "#NNNNNN" or "rgb(r, g, b)" @@ -3651,4 +3676,4 @@ private StyleSheet getStyleSheet(StyleSheet ss) { private transient StyleSheet styleSheet = null; static int baseFontSizeIndex = 3; -} +} \ No newline at end of file From 0ebf1747649119c795590fb6abb47b820824c4fa Mon Sep 17 00:00:00 2001 From: ScientificWare Date: Fri, 16 Sep 2022 20:49:07 +0200 Subject: [PATCH 08/26] Adds or removes spaces. Some spaces are missing or extra. --- .../share/classes/javax/swing/text/html/CSS.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java index e093c062d6ce5..3489df14c3344 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java @@ -1351,7 +1351,7 @@ else if (str.length() < 2) return colorstr; } - private static Pattern hex = Pattern.compile("\\p{XDigit}+"); + private static Pattern hex = Pattern.compile("\\p{XDigit}+"); /** * Convert a "#FFF", "#FFFF", "#FFFFFF" or "#FFFFFFFF" hex string to a Color. @@ -1391,7 +1391,7 @@ static final Color hexToColor(String digits) { final String g = digits.substring(1, 2); final String b = digits.substring(2, 3); digits = String.format("%s%s%s%s%s%sff", r, r, g, g, b, b); - } else if (n==4 && hex.matcher(digits).matches()) { + } else if (n == 4 && hex.matcher(digits).matches()) { final String r = digits.substring(0, 1); final String g = digits.substring(1, 2); final String b = digits.substring(2, 3); @@ -1405,7 +1405,7 @@ static final Color hexToColor(String digits) { try { Integer intValue = Integer.parseUnsignedInt(digits, 16); int l = intValue.intValue(); - return new Color((l >> 24) & 0xFF,(l >> 16) & 0xFF, (l >> 8) & 0xFF, l & 0xFF); + return new Color((l >> 24) & 0xFF, (l >> 16) & 0xFF, (l >> 8) & 0xFF, l & 0xFF); } catch (NumberFormatException nfe) { return null; } From 7a99e7ca9af454ed6130da72ba3549f7f1be32a6 Mon Sep 17 00:00:00 2001 From: ScientificWare Date: Sat, 17 Sep 2022 00:50:10 +0200 Subject: [PATCH 09/26] Adds final modifier. Adds a final modifier to hex Pattern. --- src/java.desktop/share/classes/javax/swing/text/html/CSS.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java index 3489df14c3344..87e0381e6863f 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java @@ -1351,7 +1351,7 @@ else if (str.length() < 2) return colorstr; } - private static Pattern hex = Pattern.compile("\\p{XDigit}+"); + private static final Pattern hex = Pattern.compile("\\p{XDigit}+"); /** * Convert a "#FFF", "#FFFF", "#FFFFFF" or "#FFFFFFFF" hex string to a Color. From 5b365172a82f20021fd9641763539aea4b0ff9d3 Mon Sep 17 00:00:00 2001 From: ScientificWare Date: Sat, 17 Sep 2022 02:18:00 +0200 Subject: [PATCH 10/26] Improper Name in comments Modifies the improper name of the recommendation with CSS Color Level 4. CSS Level 4 doesn't exist. CSS is a set of Modules, one of them is Color. --- src/java.desktop/share/classes/javax/swing/text/html/CSS.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java index 87e0381e6863f..fbc2401da480f 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java @@ -1364,7 +1364,7 @@ static final Color hexToColor(String digits) { digits = digits.substring(1, Math.min(n, 9)); n--; } - // CSS level 4 + // CSS Color level 4 // - defines color hex code as #[2 digits Red][2 digits Green][2 digits Blue][2 digits Alpha]. With digit 0 ... f. // - allows, webpage passes 3, 4, 6 or 8 digit color code. // - 3 digits #[R][G][B] ........ represents #[RR][GG][BB]FF @@ -1373,7 +1373,7 @@ static final Color hexToColor(String digits) { // - 8 digits #[RR][GG][BB][AA] . represents #[RR][GG][BB][AA] // // Becareful ! In java.awt.Color hex #[2 digits Alpha][2 digits Red][2 digits Green][2 digits Blue] - // Since this method is defined in CSS.java, it must only take in charge CSS Level 4 color format. + // Since this method is defined in CSS.java, it must only take in charge CSS Color Level 4 notations. // // According notes below the current OpenJDK implementation is // - 3 digits #[R][G][B] represents #[RR][GG][BB]FF From 1724cdf8ef3cb45732a23bd98484c90800a9b4e8 Mon Sep 17 00:00:00 2001 From: Guy Abossolo Foh - ScientificWare Date: Sat, 17 Sep 2022 02:51:05 +0200 Subject: [PATCH 11/26] CSS.java - Adds final modifier to hex Pattern. - corrects recommandation names CSS Level 4. - Adds missing spaces. --- .../share/classes/javax/swing/text/html/CSS.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java index 78ffc5a302c12..d613575015fa1 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java @@ -1351,7 +1351,7 @@ else if (str.length() < 2) return colorstr; } - private static Pattern hex = Pattern.compile("\\p{XDigit}+"); + private static final Pattern hex = Pattern.compile("\\p{XDigit}+"); /** * Convert a "#FFF", "#FFFF", "#FFFFFF" or "#FFFFFFFF" hex string to a Color. @@ -1364,7 +1364,7 @@ static final Color hexToColor(String digits) { digits = digits.substring(1, Math.min(n, 9)); n--; } - // CSS level 4 + // CSS Color level 4 // - defines color hex code as #[2 digits Red][2 digits Green][2 digits Blue][2 digits Alpha]. With digit 0 ... f. // - allows, webpage passes 3, 4, 6 or 8 digit color code. // - 3 digits #[R][G][B] ........ represents #[RR][GG][BB]FF @@ -1373,7 +1373,7 @@ static final Color hexToColor(String digits) { // - 8 digits #[RR][GG][BB][AA] . represents #[RR][GG][BB][AA] // // Becareful ! In java.awt.Color hex #[2 digits Alpha][2 digits Red][2 digits Green][2 digits Blue] - // Since this method is defined in CSS.java, it must only take in charge CSS Level 4 color format. + // Since this method is defined in CSS.java, it must only take in charge CSS Color Level 4 notations. // // According notes below the current OpenJDK implementation is // - 3 digits #[R][G][B] represents #[RR][GG][BB]FF @@ -1391,7 +1391,7 @@ static final Color hexToColor(String digits) { final String g = digits.substring(1, 2); final String b = digits.substring(2, 3); digits = String.format("%s%s%s%s%s%sff", r, r, g, g, b, b); - } else if (n==4 && hex.matcher(digits).matches()) { + } else if (n == 4 && hex.matcher(digits).matches()) { final String r = digits.substring(0, 1); final String g = digits.substring(1, 2); final String b = digits.substring(2, 3); @@ -1405,7 +1405,7 @@ static final Color hexToColor(String digits) { try { Integer intValue = Integer.parseUnsignedInt(digits, 16); int l = intValue.intValue(); - return new Color((l >> 24) & 0xFF,(l >> 16) & 0xFF, (l >> 8) & 0xFF, l & 0xFF); + return new Color((l >> 24) & 0xFF, (l >> 16) & 0xFF, (l >> 8) & 0xFF, l & 0xFF); } catch (NumberFormatException nfe) { return null; } From 93dc0bbdf6574823bf7e2542ce1b09c89de25daa Mon Sep 17 00:00:00 2001 From: ScientificWare Date: Sat, 17 Sep 2022 18:23:41 +0200 Subject: [PATCH 12/26] Adds regression test. Adds a regression for DK-8293776 : Adds CSS 4 and 8 digits hex coded Color #13 --- .../text/html/CSS/Hex3468DigitsColor.java | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java diff --git a/test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java b/test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java new file mode 100644 index 0000000000000..6ebdefc48df6a --- /dev/null +++ b/test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2022, Oracle 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. Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8293776 + * @summary Adds CSS 4 and 8 digits hex coded Color + * @run main Hex3468DigitsColor + * @author Guy Abossolo Foh - ScientificWare + */ + +import java.awt.Color; +import javax.swing.text.html.StyleSheet; + +public class Hex3468DigitsColor { + + public static void main(String[] args) { + StringBuilder result = new StringBuilder(); + boolean passed = true; + StyleSheet styleSheet = new StyleSheet(); + // #rgba Should be interpreted as #rrggbbaa according CSS Color Level 4. + // Then expecting r=255 g=17 b=34 a=170 + Color color = styleSheet.stringToColor("#f12a"); + int red = color.getRed(); + int green = color.getGreen(); + int blue = color.getBlue(); + int alpha = color.getAlpha(); + result.append(" Test for #f00a"); + if (red != 255) { + result.append(", expected r=255 but r=%s found".formatted(red)); + passed = false; + } + if (green != 17) { + result.append(", expected g=17 but g=%s found".formatted(green)); + passed = false; + } + if (blue != 34) { + result.append(", expected b=34 but b=%s found".formatted(blue)); + passed = false; + } + if (alpha != 170) { + result.append(", expected a=170 but a=%s found".formatted(alpha)); + passed = false; + } + // In #rrggbbaa last two digits should be interpreted as Alpha value according CSS Color Level 4. + // Then expecting r=255 g=17 b=34 a=170 + color = styleSheet.stringToColor("#ff1122aa"); + alpha = color.getAlpha(); + result.append("\n Test for #ff1122aa"); + if (red != 255) { + result.append(", expected r=255 but r=%s found".formatted(red)); + passed = false; + } + if (green != 17) { + result.append(", expected g=17 but g=%s found".formatted(green)); + passed = false; + } + if (blue != 34) { + result.append(", expected b=34 but b=%s found".formatted(blue)); + passed = false; + } + if (alpha != 170) { + result.append(", expected a=170 but a=%s found".formatted(alpha)); + passed = false; + } + if (!passed) { + result.insert(0, "Failed :"); + throw new RuntimeException(result.toString()); + } + } +} \ No newline at end of file From 24040cf664ecdeb2678796761331edfc60a6b897 Mon Sep 17 00:00:00 2001 From: ScientificWare Date: Sat, 17 Sep 2022 18:28:49 +0200 Subject: [PATCH 13/26] Removes author name. Removes author name for JDK main-line development repository. --- test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java b/test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java index 6ebdefc48df6a..007dc689730b3 100644 --- a/test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java +++ b/test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java @@ -28,7 +28,6 @@ * @bug 8293776 * @summary Adds CSS 4 and 8 digits hex coded Color * @run main Hex3468DigitsColor - * @author Guy Abossolo Foh - ScientificWare */ import java.awt.Color; @@ -90,4 +89,4 @@ public static void main(String[] args) { throw new RuntimeException(result.toString()); } } -} \ No newline at end of file +} From 59709803601bb73bf717caea515c83d67a5ad916 Mon Sep 17 00:00:00 2001 From: ScientificWare Date: Sun, 18 Sep 2022 09:08:24 +0200 Subject: [PATCH 14/26] Update src/java.desktop/share/classes/javax/swing/text/html/CSS.java Sorry. Thanks. Co-authored-by: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> --- src/java.desktop/share/classes/javax/swing/text/html/CSS.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java index fbc2401da480f..85f17b8930243 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java @@ -1403,8 +1403,7 @@ static final Color hexToColor(String digits) { return null; } try { - Integer intValue = Integer.parseUnsignedInt(digits, 16); - int l = intValue.intValue(); + int l = Integer.parseUnsignedInt(digits, 16); return new Color((l >> 24) & 0xFF, (l >> 16) & 0xFF, (l >> 8) & 0xFF, l & 0xFF); } catch (NumberFormatException nfe) { return null; From e6454c11b53c5efb56a7ed8d60c3eefdfc8fcb70 Mon Sep 17 00:00:00 2001 From: ScientificWare Date: Sun, 18 Sep 2022 17:17:13 +0200 Subject: [PATCH 15/26] Corrects typos and digit extraction. Corrects typos. And applies a better digit extraction as suggested in main-line review thread. --- .../classes/javax/swing/text/html/CSS.java | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java index 85f17b8930243..fe442978847b2 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java @@ -1372,14 +1372,14 @@ static final Color hexToColor(String digits) { // - 6 digits #[RR][GG][BB] ..... represents #[RR][GG][BB]FF // - 8 digits #[RR][GG][BB][AA] . represents #[RR][GG][BB][AA] // - // Becareful ! In java.awt.Color hex #[2 digits Alpha][2 digits Red][2 digits Green][2 digits Blue] + // Be careful ! In java.awt.Color hex #[2 digits Alpha][2 digits Red][2 digits Green][2 digits Blue] // Since this method is defined in CSS.java, it must only take in charge CSS Color Level 4 notations. // // According notes below the current OpenJDK implementation is - // - 3 digits #[R][G][B] represents #[RR][GG][BB]FF - // - 6 digits #[R][G][B] represents #[RR][GG][BB]FF + // - 3 digits #[R][G][B] .......... represents #[RR][GG][BB]FF + // - 6 digits #[R][G][B] .......... represents #[RR][GG][BB]FF // - // Some webpage passes 3 digit color code as in #fff which is + // Some webpages pass 3 digit color code as in #fff which is // decoded as #000FFF resulting in blue background. // As per https://www.w3.org/TR/CSS1/#color-units, // The three-digit RGB notation (#rgb) is converted into six-digit form @@ -1387,18 +1387,18 @@ static final Color hexToColor(String digits) { // This makes sure that white (#ffffff) can be specified with the short notation // (#fff) and removes any dependencies on the color depth of the display. if (n == 3 && hex.matcher(digits).matches()) { - final String r = digits.substring(0, 1); - final String g = digits.substring(1, 2); - final String b = digits.substring(2, 3); - digits = String.format("%s%s%s%s%s%sff", r, r, g, g, b, b); + final char r = digits.charAt(0); + final char g = digits.charAt(1); + final char b = digits.charAt(2); + digits = String.format("%1$s%1$s%2$s%2$s%3$s%3$sff", r, g, b); } else if (n == 4 && hex.matcher(digits).matches()) { - final String r = digits.substring(0, 1); - final String g = digits.substring(1, 2); - final String b = digits.substring(2, 3); - final String a = digits.substring(3, 4); - digits = String.format("%s%s%s%s%s%s%s%s", r, r, g, g, b, b, a, a); + final char r = digits.charAt(0); + final char g = digits.charAt(1); + final char b = digits.charAt(2); + final char a = digits.charAt(3); + digits = String.format("%1$s%1$s%2$s%2$s%3$s%3$s%4$s%4$s", r, g, b, a); } else if (n == 6 && hex.matcher(digits).matches()) { - digits = String.format("%sff", digits); + digits += "ff"; } else if (n != 8 || !hex.matcher(digits).matches()) { return null; } From 6ee25cd576b745d78a8d2ef08ea1f6c38b378b8e Mon Sep 17 00:00:00 2001 From: Guy Abossolo Foh - ScientificWare Date: Sun, 18 Sep 2022 18:45:09 +0200 Subject: [PATCH 16/26] CSS.java : - Corrects typos. - Applies a better digit extraction as suggested in main-line review thread. - Using charAt avoids creating temporary strings of length 1. - Also, using the %[argument_index$]conversion format specifier form allows halving the lengths of the vararg arrays. - Corrects mistake about variable declaration for parseUnsignedInt(...) method returned value. Must be int and not Integer. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Hex3468DigitsColor.java : - Ajout d'un test de régression pour JDK-8293776. --- .../classes/javax/swing/text/html/CSS.java | 32 +++---- .../text/html/CSS/Hex3468DigitsColor.java | 93 +++++++++++++++++++ 2 files changed, 109 insertions(+), 16 deletions(-) create mode 100644 test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java diff --git a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java index d613575015fa1..4566dd706016c 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java @@ -1351,6 +1351,7 @@ else if (str.length() < 2) return colorstr; } + private static final Pattern hex = Pattern.compile("\\p{XDigit}+"); /** @@ -1372,14 +1373,14 @@ static final Color hexToColor(String digits) { // - 6 digits #[RR][GG][BB] ..... represents #[RR][GG][BB]FF // - 8 digits #[RR][GG][BB][AA] . represents #[RR][GG][BB][AA] // - // Becareful ! In java.awt.Color hex #[2 digits Alpha][2 digits Red][2 digits Green][2 digits Blue] + // Be careful ! In java.awt.Color hex #[2 digits Alpha][2 digits Red][2 digits Green][2 digits Blue] // Since this method is defined in CSS.java, it must only take in charge CSS Color Level 4 notations. // // According notes below the current OpenJDK implementation is - // - 3 digits #[R][G][B] represents #[RR][GG][BB]FF - // - 6 digits #[R][G][B] represents #[RR][GG][BB]FF + // - 3 digits #[R][G][B] .......... represents #[RR][GG][BB]FF + // - 6 digits #[R][G][B] .......... represents #[RR][GG][BB]FF // - // Some webpage passes 3 digit color code as in #fff which is + // Some webpages pass 3 digit color code as in #fff which is // decoded as #000FFF resulting in blue background. // As per https://www.w3.org/TR/CSS1/#color-units, // The three-digit RGB notation (#rgb) is converted into six-digit form @@ -1387,24 +1388,23 @@ static final Color hexToColor(String digits) { // This makes sure that white (#ffffff) can be specified with the short notation // (#fff) and removes any dependencies on the color depth of the display. if (n == 3 && hex.matcher(digits).matches()) { - final String r = digits.substring(0, 1); - final String g = digits.substring(1, 2); - final String b = digits.substring(2, 3); - digits = String.format("%s%s%s%s%s%sff", r, r, g, g, b, b); + final char r = digits.charAt(0); + final char g = digits.charAt(1); + final char b = digits.charAt(2); + digits = String.format("%1$s%1$s%2$s%2$s%3$s%3$sff", r, g, b); } else if (n == 4 && hex.matcher(digits).matches()) { - final String r = digits.substring(0, 1); - final String g = digits.substring(1, 2); - final String b = digits.substring(2, 3); - final String a = digits.substring(3, 4); - digits = String.format("%s%s%s%s%s%s%s%s", r, r, g, g, b, b, a, a); + final char r = digits.charAt(0); + final char g = digits.charAt(1); + final char b = digits.charAt(2); + final char a = digits.charAt(3); + digits = String.format("%1$s%1$s%2$s%2$s%3$s%3$s%4$s%4$s", r, g, b, a); } else if (n == 6 && hex.matcher(digits).matches()) { - digits = String.format("%sff", digits); + digits += "ff"; } else if (n != 8 || !hex.matcher(digits).matches()) { return null; } try { - Integer intValue = Integer.parseUnsignedInt(digits, 16); - int l = intValue.intValue(); + int l = Integer.parseUnsignedInt(digits, 16); return new Color((l >> 24) & 0xFF, (l >> 16) & 0xFF, (l >> 8) & 0xFF, l & 0xFF); } catch (NumberFormatException nfe) { return null; diff --git a/test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java b/test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java new file mode 100644 index 0000000000000..6ebdefc48df6a --- /dev/null +++ b/test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2022, Oracle 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. Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8293776 + * @summary Adds CSS 4 and 8 digits hex coded Color + * @run main Hex3468DigitsColor + * @author Guy Abossolo Foh - ScientificWare + */ + +import java.awt.Color; +import javax.swing.text.html.StyleSheet; + +public class Hex3468DigitsColor { + + public static void main(String[] args) { + StringBuilder result = new StringBuilder(); + boolean passed = true; + StyleSheet styleSheet = new StyleSheet(); + // #rgba Should be interpreted as #rrggbbaa according CSS Color Level 4. + // Then expecting r=255 g=17 b=34 a=170 + Color color = styleSheet.stringToColor("#f12a"); + int red = color.getRed(); + int green = color.getGreen(); + int blue = color.getBlue(); + int alpha = color.getAlpha(); + result.append(" Test for #f00a"); + if (red != 255) { + result.append(", expected r=255 but r=%s found".formatted(red)); + passed = false; + } + if (green != 17) { + result.append(", expected g=17 but g=%s found".formatted(green)); + passed = false; + } + if (blue != 34) { + result.append(", expected b=34 but b=%s found".formatted(blue)); + passed = false; + } + if (alpha != 170) { + result.append(", expected a=170 but a=%s found".formatted(alpha)); + passed = false; + } + // In #rrggbbaa last two digits should be interpreted as Alpha value according CSS Color Level 4. + // Then expecting r=255 g=17 b=34 a=170 + color = styleSheet.stringToColor("#ff1122aa"); + alpha = color.getAlpha(); + result.append("\n Test for #ff1122aa"); + if (red != 255) { + result.append(", expected r=255 but r=%s found".formatted(red)); + passed = false; + } + if (green != 17) { + result.append(", expected g=17 but g=%s found".formatted(green)); + passed = false; + } + if (blue != 34) { + result.append(", expected b=34 but b=%s found".formatted(blue)); + passed = false; + } + if (alpha != 170) { + result.append(", expected a=170 but a=%s found".formatted(alpha)); + passed = false; + } + if (!passed) { + result.insert(0, "Failed :"); + throw new RuntimeException(result.toString()); + } + } +} \ No newline at end of file From 478f285e128036291db1678e4044f5732bd505e3 Mon Sep 17 00:00:00 2001 From: ScientificWare Date: Wed, 21 Sep 2022 05:33:03 +0200 Subject: [PATCH 17/26] Corrects Licence Header. Removes Classpath exception. --- test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java b/test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java index 007dc689730b3..6140eb7003cae 100644 --- a/test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java +++ b/test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java @@ -4,9 +4,7 @@ * * 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. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. + * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or From 767c87ce2449513cbe91992b3c3446a7aeaab950 Mon Sep 17 00:00:00 2001 From: ScientificWare Date: Fri, 23 Sep 2022 05:09:22 +0200 Subject: [PATCH 18/26] Renames an identifier. Suggested change, not to use `l` as an identifier because it could be confused with `1`. This part of code could change and be replaced by bits right rotation after performance tests. --- src/java.desktop/share/classes/javax/swing/text/html/CSS.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java index fe442978847b2..3098f6c60afa4 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java @@ -1403,8 +1403,8 @@ static final Color hexToColor(String digits) { return null; } try { - int l = Integer.parseUnsignedInt(digits, 16); - return new Color((l >> 24) & 0xFF, (l >> 16) & 0xFF, (l >> 8) & 0xFF, l & 0xFF); + int i = Integer.parseUnsignedInt(digits, 16); + return new Color((i >> 24) & 0xFF, (i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF); } catch (NumberFormatException nfe) { return null; } From a95d2824b8fa9b545c808740bc335c4c6bd46550 Mon Sep 17 00:00:00 2001 From: ScientificWare Date: Sat, 24 Sep 2022 17:57:21 +0200 Subject: [PATCH 19/26] Simplifications of the test. Removes individual color tests and only compares the RGB value. --- .../text/html/CSS/Hex3468DigitsColor.java | 48 ++++--------------- 1 file changed, 10 insertions(+), 38 deletions(-) diff --git a/test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java b/test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java index 6140eb7003cae..726e0ea1fc56a 100644 --- a/test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java +++ b/test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java @@ -37,51 +37,23 @@ public static void main(String[] args) { StringBuilder result = new StringBuilder(); boolean passed = true; StyleSheet styleSheet = new StyleSheet(); - // #rgba Should be interpreted as #rrggbbaa according CSS Color Level 4. - // Then expecting r=255 g=17 b=34 a=170 + + // #rgba should be interpreted as #rrggbbaa according CSS Color Level 4. + // Then expecting 0xaaff1122 from Color. Color color = styleSheet.stringToColor("#f12a"); - int red = color.getRed(); - int green = color.getGreen(); - int blue = color.getBlue(); - int alpha = color.getAlpha(); result.append(" Test for #f00a"); - if (red != 255) { - result.append(", expected r=255 but r=%s found".formatted(red)); - passed = false; - } - if (green != 17) { - result.append(", expected g=17 but g=%s found".formatted(green)); - passed = false; - } - if (blue != 34) { - result.append(", expected b=34 but b=%s found".formatted(blue)); - passed = false; - } - if (alpha != 170) { - result.append(", expected a=170 but a=%s found".formatted(alpha)); + if (0xaaff1122 != color.getRGB()) { passed = false; } - // In #rrggbbaa last two digits should be interpreted as Alpha value according CSS Color Level 4. - // Then expecting r=255 g=17 b=34 a=170 + + // In #rrggbbaa, last two digits should be interpreted as Alpha value according CSS Color Level 4. + // Then expecting 0xaaff1122 from Color. color = styleSheet.stringToColor("#ff1122aa"); - alpha = color.getAlpha(); - result.append("\n Test for #ff1122aa"); - if (red != 255) { - result.append(", expected r=255 but r=%s found".formatted(red)); - passed = false; - } - if (green != 17) { - result.append(", expected g=17 but g=%s found".formatted(green)); - passed = false; - } - if (blue != 34) { - result.append(", expected b=34 but b=%s found".formatted(blue)); - passed = false; - } - if (alpha != 170) { - result.append(", expected a=170 but a=%s found".formatted(alpha)); + result.append(" and Test for #ff1122aa"); + if (0xaaff1122 != color.getRGB()) { passed = false; } + if (!passed) { result.insert(0, "Failed :"); throw new RuntimeException(result.toString()); From b73bec53cae487d88d7072cb42b72046e54c03a2 Mon Sep 17 00:00:00 2001 From: ScientificWare Date: Tue, 27 Sep 2022 05:14:08 +0200 Subject: [PATCH 20/26] Corrects a value in a message. A message is added to the result in case of failure only. The updated code does not output the actual value. The tested color is #f12a instead of #f00a. --- test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java b/test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java index 726e0ea1fc56a..086de62a21755 100644 --- a/test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java +++ b/test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java @@ -41,7 +41,7 @@ public static void main(String[] args) { // #rgba should be interpreted as #rrggbbaa according CSS Color Level 4. // Then expecting 0xaaff1122 from Color. Color color = styleSheet.stringToColor("#f12a"); - result.append(" Test for #f00a"); + result.append(" Test for #f12a"); if (0xaaff1122 != color.getRGB()) { passed = false; } From 45c2c180928ddf82baaf74df56a2c47cdcbbcac6 Mon Sep 17 00:00:00 2001 From: Guy Abossolo Foh - ScientificWare Date: Fri, 11 Nov 2022 14:13:26 +0100 Subject: [PATCH 21/26] CSS.java : MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Méthode hexToColor : - Simplication des commentaires pour le dépôt principal. - Modification de l'analyse des valeurs hexadécimales. - La valeur de la couleur est formée directement lors de la lecture de chaque caractère. - La position des valeurs à utiliser est désormais donnée par un objet Map. Ce qui accélère le traitement dans un facteur de 6 à 7. --- .../classes/javax/swing/text/html/CSS.java | 85 ++++++++----------- 1 file changed, 36 insertions(+), 49 deletions(-) diff --git a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java index 4566dd706016c..3d52e09b453e8 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java @@ -38,6 +38,7 @@ import java.util.ArrayList; import java.util.Enumeration; import java.util.Hashtable; +import java.util.Map; import java.util.regex.Pattern; import javax.swing.ImageIcon; @@ -1351,65 +1352,51 @@ else if (str.length() < 2) return colorstr; } - - private static final Pattern hex = Pattern.compile("\\p{XDigit}+"); - /** * Convert a "#FFF", "#FFFF", "#FFFFFF" or "#FFFFFFFF" hex string to a Color. - * If the color specification is bad, an attempt - * will be made to fix it up. + * If the color specification is bad, an attempt will be made to fix it up. */ static final Color hexToColor(String digits) { - int n = digits.length(); - if (digits.startsWith("#")) { - digits = digits.substring(1, Math.min(n, 9)); - n--; - } - // CSS Color level 4 - // - defines color hex code as #[2 digits Red][2 digits Green][2 digits Blue][2 digits Alpha]. With digit 0 ... f. - // - allows, webpage passes 3, 4, 6 or 8 digit color code. + // CSS Color level 4 allows webpage passes 3, 4, 6 or 8 digit color codes. // - 3 digits #[R][G][B] ........ represents #[RR][GG][BB]FF // - 4 digits #[R][G][B][A] ..... represents #[RR][GG][BB][AA] // - 6 digits #[RR][GG][BB] ..... represents #[RR][GG][BB]FF // - 8 digits #[RR][GG][BB][AA] . represents #[RR][GG][BB][AA] - // - // Be careful ! In java.awt.Color hex #[2 digits Alpha][2 digits Red][2 digits Green][2 digits Blue] - // Since this method is defined in CSS.java, it must only take in charge CSS Color Level 4 notations. - // - // According notes below the current OpenJDK implementation is - // - 3 digits #[R][G][B] .......... represents #[RR][GG][BB]FF - // - 6 digits #[R][G][B] .......... represents #[RR][GG][BB]FF - // - // Some webpages pass 3 digit color code as in #fff which is - // decoded as #000FFF resulting in blue background. - // As per https://www.w3.org/TR/CSS1/#color-units, - // The three-digit RGB notation (#rgb) is converted into six-digit form - // (#rrggbb) by replicating digits, not by adding zeros. - // This makes sure that white (#ffffff) can be specified with the short notation - // (#fff) and removes any dependencies on the color depth of the display. - if (n == 3 && hex.matcher(digits).matches()) { - final char r = digits.charAt(0); - final char g = digits.charAt(1); - final char b = digits.charAt(2); - digits = String.format("%1$s%1$s%2$s%2$s%3$s%3$sff", r, g, b); - } else if (n == 4 && hex.matcher(digits).matches()) { - final char r = digits.charAt(0); - final char g = digits.charAt(1); - final char b = digits.charAt(2); - final char a = digits.charAt(3); - digits = String.format("%1$s%1$s%2$s%2$s%3$s%3$s%4$s%4$s", r, g, b, a); - } else if (n == 6 && hex.matcher(digits).matches()) { - digits += "ff"; - } else if (n != 8 || !hex.matcher(digits).matches()) { + final byte[] iseq = digits.startsWith("#") ? + iseqmap.get(Integer.valueOf(digits.length())): + iseqmap.get(Integer.valueOf(-digits.length())); + if (iseq == null) { + // Rejects string argument with a wrong number length. return null; } - try { - int l = Integer.parseUnsignedInt(digits, 16); - return new Color((l >> 24) & 0xFF, (l >> 16) & 0xFF, (l >> 8) & 0xFF, l & 0xFF); - } catch (NumberFormatException nfe) { - return null; - } - } + // Only 3, 4, 6 and 8 digits notations. + // Parses the string argument and build color value. + int dv; + int value = 0; + for (byte i : iseq) { + if ((dv = -i) != 15 && (dv = Character.digit(digits.charAt(i), 16)) < 0) { + // Rejects string argument with not a valid digit in the radix-16. + return null; + } + value = dv | value << 4; + } + return new Color(value, true); + } + + // Map of Index Sequences. Index -15 means, use the default value 15. + private static final Map iseqmap = + Map.ofEntries( + // Positive key, for # prefixed string, is associated with index from 1 to 8. + // Negative key, for not # prefixed string, is associated with index from 0 to 7. + Map.entry(Integer.valueOf(4), new byte[]{-15, -15, 1, 1, 2, 2, 3, 3}), + Map.entry(Integer.valueOf(5), new byte[]{4, 4, 1, 1, 2, 2, 3, 3}), + Map.entry(Integer.valueOf(7), new byte[]{-15, -15, 1, 2, 3, 4, 5, 6}), + Map.entry(Integer.valueOf(9), new byte[]{7, 8, 1, 2, 3, 4, 6, 6}), + Map.entry(Integer.valueOf(-3), new byte[]{-15, -15, 0, 0, 1, 1, 2, 2}), + Map.entry(Integer.valueOf(-4), new byte[]{3, 3, 0, 0, 1, 1, 2, 2}), + Map.entry(Integer.valueOf(-6), new byte[]{-15, -15, 0, 1, 2, 3, 4, 5}), + Map.entry(Integer.valueOf(-8), new byte[]{6, 7, 0, 1, 2, 3, 4, 5}) + ); /** * Convert a color string such as "RED" or "#NNNNNN" or "rgb(r, g, b)" From 8f65b852b7d93084792ed8ce7a5cd03c23aac012 Mon Sep 17 00:00:00 2001 From: ScientificWare Date: Wed, 14 Dec 2022 19:28:30 +0100 Subject: [PATCH 22/26] Performance improvement Performance results came from my repository I mentioned in the header. The code before this PR ran in 230ms. Our previous codes ran in 1 200ms to 1800 ms with String + formatted + %n$s usage. They ran in 350ms to 380ms with String + formatted + %s usage. And in 100ms to 110ms if we replace String + format with a string concatenation. Now the code below gives the same results in 36ms and with all our expected behaviors. Since we control notation length we can bypass some controls, directly generate the color value, without generate a new string, and reject a wrong number format without generate any exception. --- .../classes/javax/swing/text/html/CSS.java | 83 ++++++++----------- 1 file changed, 35 insertions(+), 48 deletions(-) diff --git a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java index 3098f6c60afa4..5fcff7e703984 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java @@ -1351,64 +1351,51 @@ else if (str.length() < 2) return colorstr; } - private static final Pattern hex = Pattern.compile("\\p{XDigit}+"); - /** * Convert a "#FFF", "#FFFF", "#FFFFFF" or "#FFFFFFFF" hex string to a Color. - * If the color specification is bad, an attempt - * will be made to fix it up. + * If the color specification is bad, an attempt will be made to fix it up. */ static final Color hexToColor(String digits) { - int n = digits.length(); - if (digits.startsWith("#")) { - digits = digits.substring(1, Math.min(n, 9)); - n--; - } - // CSS Color level 4 - // - defines color hex code as #[2 digits Red][2 digits Green][2 digits Blue][2 digits Alpha]. With digit 0 ... f. - // - allows, webpage passes 3, 4, 6 or 8 digit color code. + // CSS Color level 4 allows webpage passes 3, 4, 6 or 8 digit color codes. // - 3 digits #[R][G][B] ........ represents #[RR][GG][BB]FF // - 4 digits #[R][G][B][A] ..... represents #[RR][GG][BB][AA] // - 6 digits #[RR][GG][BB] ..... represents #[RR][GG][BB]FF // - 8 digits #[RR][GG][BB][AA] . represents #[RR][GG][BB][AA] - // - // Be careful ! In java.awt.Color hex #[2 digits Alpha][2 digits Red][2 digits Green][2 digits Blue] - // Since this method is defined in CSS.java, it must only take in charge CSS Color Level 4 notations. - // - // According notes below the current OpenJDK implementation is - // - 3 digits #[R][G][B] .......... represents #[RR][GG][BB]FF - // - 6 digits #[R][G][B] .......... represents #[RR][GG][BB]FF - // - // Some webpages pass 3 digit color code as in #fff which is - // decoded as #000FFF resulting in blue background. - // As per https://www.w3.org/TR/CSS1/#color-units, - // The three-digit RGB notation (#rgb) is converted into six-digit form - // (#rrggbb) by replicating digits, not by adding zeros. - // This makes sure that white (#ffffff) can be specified with the short notation - // (#fff) and removes any dependencies on the color depth of the display. - if (n == 3 && hex.matcher(digits).matches()) { - final char r = digits.charAt(0); - final char g = digits.charAt(1); - final char b = digits.charAt(2); - digits = String.format("%1$s%1$s%2$s%2$s%3$s%3$sff", r, g, b); - } else if (n == 4 && hex.matcher(digits).matches()) { - final char r = digits.charAt(0); - final char g = digits.charAt(1); - final char b = digits.charAt(2); - final char a = digits.charAt(3); - digits = String.format("%1$s%1$s%2$s%2$s%3$s%3$s%4$s%4$s", r, g, b, a); - } else if (n == 6 && hex.matcher(digits).matches()) { - digits += "ff"; - } else if (n != 8 || !hex.matcher(digits).matches()) { - return null; - } - try { - int i = Integer.parseUnsignedInt(digits, 16); - return new Color((i >> 24) & 0xFF, (i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF); - } catch (NumberFormatException nfe) { + final byte[] iseq = digits.startsWith("#") ? + iseqmap.get(Integer.valueOf(digits.length())): + iseqmap.get(Integer.valueOf(-digits.length())); + if (iseq == null) { + // Rejects string argument with a wrong number length. return null; } - } + // Only 3, 4, 6 and 8 digits notations. + // Parses the string argument and build color value. + int dv; + int value = 0; + for (byte i : iseq) { + if ((dv = -i) != 15 && (dv = Character.digit(digits.charAt(i), 16)) < 0) { + // Rejects string argument with not a valid digit in the radix-16. + return null; + } + value = dv | value << 4; + } + return new Color(value, true); + } + + // Map of Index Sequences. Index -15 means, use the default value 15. + private static final Map iseqmap = + Map.ofEntries( + // Positive key, for # prefixed string, is associated with index from 1 to 8. + // Negative key, for not # prefixed string, is associated with index from 0 to 7. + Map.entry(Integer.valueOf(4), new byte[]{-15, -15, 1, 1, 2, 2, 3, 3}), + Map.entry(Integer.valueOf(5), new byte[]{4, 4, 1, 1, 2, 2, 3, 3}), + Map.entry(Integer.valueOf(7), new byte[]{-15, -15, 1, 2, 3, 4, 5, 6}), + Map.entry(Integer.valueOf(9), new byte[]{7, 8, 1, 2, 3, 4, 6, 6}), + Map.entry(Integer.valueOf(-3), new byte[]{-15, -15, 0, 0, 1, 1, 2, 2}), + Map.entry(Integer.valueOf(-4), new byte[]{3, 3, 0, 0, 1, 1, 2, 2}), + Map.entry(Integer.valueOf(-6), new byte[]{-15, -15, 0, 1, 2, 3, 4, 5}), + Map.entry(Integer.valueOf(-8), new byte[]{6, 7, 0, 1, 2, 3, 4, 5}) + ); /** * Convert a color string such as "RED" or "#NNNNNN" or "rgb(r, g, b)" From 433e76f7a46aeef27f85d3501b19d086488bce4f Mon Sep 17 00:00:00 2001 From: ScientificWare Date: Wed, 14 Dec 2022 21:08:55 +0100 Subject: [PATCH 23/26] Update imports Remove java.util.Pattern and add java.util.Map imports --- src/java.desktop/share/classes/javax/swing/text/html/CSS.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java index 5fcff7e703984..cb4d0e937cea9 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java @@ -38,7 +38,7 @@ import java.util.ArrayList; import java.util.Enumeration; import java.util.Hashtable; -import java.util.regex.Pattern; +import java.util.Map; import javax.swing.ImageIcon; import javax.swing.SizeRequirements; From eba37e96d5cf69ff2eaa57100b8862f17862e967 Mon Sep 17 00:00:00 2001 From: Guy Abossolo Foh - ScientificWare Date: Sun, 5 Feb 2023 08:12:13 +0100 Subject: [PATCH 24/26] =?UTF-8?q?Suppression=20d'un=20import=20non=20utili?= =?UTF-8?q?s=C3=A9.=20L'utilisation=20de=20la=20classe=20Patter=20n'est=20?= =?UTF-8?q?plus=20d'actualit=C3=A9.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java.desktop/share/classes/javax/swing/text/html/CSS.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java index 3d52e09b453e8..a8328dd8ae530 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java @@ -39,7 +39,6 @@ import java.util.Enumeration; import java.util.Hashtable; import java.util.Map; -import java.util.regex.Pattern; import javax.swing.ImageIcon; import javax.swing.SizeRequirements; From b9304ef906e6ccf0330c239f7ef345b3e1f329d9 Mon Sep 17 00:00:00 2001 From: ScientificWare Date: Sun, 5 Feb 2023 10:50:22 +0100 Subject: [PATCH 25/26] Updates copyright date. Updates copyright date to 2023. --- src/java.desktop/share/classes/javax/swing/text/html/CSS.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java index cb4d0e937cea9..24ee9c5d42c53 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/CSS.java +++ b/src/java.desktop/share/classes/javax/swing/text/html/CSS.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2023, Oracle 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 From 82946cce634d6f490eb44e5979f8221bbcb14985 Mon Sep 17 00:00:00 2001 From: ScientificWare Date: Sun, 5 Feb 2023 10:51:04 +0100 Subject: [PATCH 26/26] Updates copyright date. Updates copyright date to 2023. --- test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java b/test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java index 086de62a21755..17edce8d86a1d 100644 --- a/test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java +++ b/test/jdk/javax/swing/text/html/CSS/Hex3468DigitsColor.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2023, Oracle 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