Skip to content

Commit

Permalink
transcoder: improve the @supports logic in CSSTranscodingHelper
Browse files Browse the repository at this point in the history
- Accept the `color-mix()` function as supported.
- em, ex units are natively supported by EchoSVG.
- The column combinator (||) selector is not supported.
  • Loading branch information
carlosame committed Oct 16, 2023
1 parent af100fa commit d01cf66
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1351,28 +1351,28 @@ public void testBug19363() throws TranscoderException, IOException {
*/
@Test
public void testCSS3_All() throws TranscoderException, IOException {
testAllInputSources("samples/tests/spec2/styling/css3.html", null, false, null, false, 5);
testAllInputSources("samples/tests/spec2/styling/css3.html", null, false, null, false, 4);
}

@Test
public void testCSS3Print() throws TranscoderException, IOException {
testPrint("samples/tests/spec2/styling/css3.html", 5);
testPrint("samples/tests/spec2/styling/css3.html", 4);
}

@Test
public void testCSS3AlternateStylesheet() throws TranscoderException, IOException {
testAlternate("samples/tests/spec2/styling/css3.html", "Gray", false, 5);
testAlternate("samples/tests/spec2/styling/css3.html", "Gray", false, 4);
}

@Test
public void testCSS3Dark() throws TranscoderException, IOException {
testDark("samples/tests/spec2/styling/css3.html", 5);
testDark("samples/tests/spec2/styling/css3.html", 4);
}

@Test
public void testCSS3_Selector() throws TranscoderException, IOException {
test("samples/tests/spec2/styling/css3.html", SVGRenderingAccuracyTest.DEFAULT_MEDIUM,
false, "#theSVG", true, 5);
false, "#theSVG", true, 4);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@
import io.sf.carte.doc.style.css.CSSValue.CssType;
import io.sf.carte.doc.style.css.StyleDatabase;
import io.sf.carte.doc.style.css.awt.AWTHelper;
import io.sf.carte.doc.style.css.nsac.ArgumentCondition;
import io.sf.carte.doc.style.css.nsac.CombinatorCondition;
import io.sf.carte.doc.style.css.nsac.CombinatorSelector;
import io.sf.carte.doc.style.css.nsac.Condition;
import io.sf.carte.doc.style.css.nsac.ConditionalSelector;
import io.sf.carte.doc.style.css.nsac.Selector;
import io.sf.carte.doc.style.css.nsac.SelectorList;
import io.sf.carte.doc.style.css.om.AbstractCSSCanvas;
import io.sf.carte.doc.style.css.om.AbstractStyleDatabase;
import io.sf.carte.doc.style.css.om.ComputedCSSStyle;
Expand Down Expand Up @@ -1101,21 +1108,68 @@ public boolean supports(String property, CSSValue value) {
private boolean supportsColor(CSSValue value) {
switch (value.getPrimitiveType()) {
case COLOR:
case COLOR_MIX:
return true;
case IDENT:
return ColorIdentifiers.getInstance()
.isColorIdentifier(value.getCssText().toLowerCase(Locale.ROOT));
default:
return false;
}
return false;
}

private boolean isSupportedType(CSSTypedValue value) {
short unit = value.getUnitType();
return (CSSUnit.isLengthUnitType(unit) && !CSSUnit.isRelativeLengthUnitType(unit))
/*
* Supported units are: absolute lengths, em, ex, angles and times
*/
return (unit >= CSSUnit.CSS_PX && unit <= CSSUnit.CSS_EX)
|| CSSUnit.isAngleUnitType(unit) || CSSUnit.isTimeUnitType(unit);
}

@Override
public boolean supports(SelectorList selectors) {
for (Selector selector : selectors) {
if (!supports(selector)) {
return false;
}
}
return true;
}

private boolean supports(Selector selector) {
switch (selector.getSelectorType()) {
case CHILD:
case DESCENDANT:
case DIRECT_ADJACENT:
case SUBSEQUENT_SIBLING:
CombinatorSelector combSel = (CombinatorSelector) selector;
return supports(combSel.getSelector()) && supports(combSel.getSecondSelector());
case CONDITIONAL:
ConditionalSelector condSel = (ConditionalSelector) selector;
return supports(condSel.getSimpleSelector())
&& supports(condSel.getCondition());
case COLUMN_COMBINATOR:
return false;
default:
return true;
}
}

private boolean supports(Condition condition) {
switch (condition.getConditionType()) {
case AND:
CombinatorCondition combCond = (CombinatorCondition) condition;
return supports(combCond.getFirstCondition())
&& supports(combCond.getSecondCondition());
case SELECTOR_ARGUMENT:
ArgumentCondition argCond = (ArgumentCondition) condition;
return supports(argCond.getSelectors());
default:
return true;
}
}

}

private class MyCanvas extends AbstractCSSCanvas {
Expand Down
6 changes: 4 additions & 2 deletions samples/tests/spec2/styling/css3.html
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,10 @@
}
}

text.itemdesc {
fill: var(--textColor);
@supports (not selector(div col.foo||td)) {
text.itemdesc {
fill: var(--textColor);
}
}

/*]]>*/
Expand Down

0 comments on commit d01cf66

Please sign in to comment.