Skip to content

Commit

Permalink
Fixes #459 - Implements the rem CSS unit.
Browse files Browse the repository at this point in the history
With test proof.
  • Loading branch information
danfickle committed Mar 16, 2020
1 parent 1d9ff98 commit 1a6d2c9
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.openhtmltopdf.css.sheet.PropertyDeclaration;
import com.openhtmltopdf.css.sheet.Stylesheet;
import com.openhtmltopdf.css.sheet.StylesheetInfo;
import com.openhtmltopdf.css.style.CalculatedStyle;
import com.openhtmltopdf.extend.NamespaceHandler;
import com.openhtmltopdf.extend.UserAgentCallback;
import com.openhtmltopdf.extend.UserInterface;
Expand Down Expand Up @@ -72,6 +73,17 @@ public StyleReference(UserAgentCallback userAgent) {
_stylesheetFactory = new StylesheetFactoryImpl(userAgent);
}

/**
* Gets the style of the root element, should be html tag.
*/
public CalculatedStyle getRootElementStyle() {
if (_context != null && _doc != null) {
return _context.getStyle(_doc.getDocumentElement());
} else {
return null;
}
}

/**
* Sets the documentContext attribute of the StyleReference object
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public static boolean isAbsoluteUnit(short type) {
return false;
// refer to values known to the DerivedValue instance (tobe)
case CSSPrimitiveValue.CSS_EMS:
case CSSPrimitiveValue.CSS_REMS:
case CSSPrimitiveValue.CSS_EXS:
// length
case CSSPrimitiveValue.CSS_IN:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,7 @@ private List<PropertyValue> expr(boolean literal) throws IOException {
case Token.URI:
case Token.HASH:
case Token.FUNCTION:
case Token.DIMENSION:
PropertyValue term = term(literal);
if (operatorToken != null) {
term.setOperator(operatorToken);
Expand Down Expand Up @@ -1462,7 +1463,19 @@ private PropertyValue term(boolean literal) throws IOException {
case Token.TIME:
case Token.FREQ:
case Token.DIMENSION:
throw new CSSParseException("Unsupported CSS unit " + extractUnit(t), getCurrentLine());
String unitType = extractUnit(t);

if ("rem".equals(unitType)) {
result = new PropertyValue(
CSSPrimitiveValue.CSS_REMS,
sign*Float.parseFloat(extractNumber(t)),
sign(sign) + getTokenValue(t));
next();
skip_whitespace();
} else {
throw new CSSParseException("Unsupported CSS unit " + unitType, getCurrentLine());
}
break;
case Token.NUMBER:
result = new PropertyValue(
CSSPrimitiveValue.CSS_NUMBER,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ public interface CSSPrimitiveValue extends CSSValue {
public static final short CSS_COUNTER = 23;
public static final short CSS_RECT = 24;
public static final short CSS_RGBCOLOR = 25;


/** rems unit, not official, added by danfickle. */
public static final short CSS_REMS = 26;

public short getPrimitiveType();
public float getFloatValue(short unitType);
public String getStringValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ public static boolean isLengthHelper(CSSPrimitiveValue value) {
|| unit == CSSPrimitiveValue.CSS_PX || unit == CSSPrimitiveValue.CSS_IN
|| unit == CSSPrimitiveValue.CSS_CM || unit == CSSPrimitiveValue.CSS_MM
|| unit == CSSPrimitiveValue.CSS_PT || unit == CSSPrimitiveValue.CSS_PC
|| unit == CSSPrimitiveValue.CSS_REMS
|| (unit == CSSPrimitiveValue.CSS_NUMBER && value.getFloatValue(CSSPrimitiveValue.CSS_IN) == 0.0f);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,19 @@ public static float calcFloatProportionalValue(CalculatedStyle style,
}

break;
case CSSPrimitiveValue.CSS_REMS: {
// The rem unit is the same as em except uses the font-size of the html element
// rather than the font-size of current/parent element.
CalculatedStyle htmlTagStyle = ctx.getCss().getRootElementStyle();
if (htmlTagStyle == null) {
// The default font-size for the html tag is 16px.
absVal = relVal * 16f * ctx.getDotsPerPixel();
} else {
FontSpecification htmlTagFontSpecification = htmlTagStyle.getFont(ctx);
absVal = relVal * htmlTagFontSpecification.size;
}
break;
}
case CSSPrimitiveValue.CSS_EXS:
// To convert EMS to pixels, we need the height of the lowercase 'Xx' character in the current
// element...
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<html>
<head>
<style>
@page {
size: 15rem 240px;
margin: 0.5rem;
}
body {
border: 1px solid orange;
margin: 1rem;
max-width: 12rem;
}
</style>
</head>
<body>
<!-- Should look the same if rem default amount is 16px -->
<div style="margin: 16px; padding: 0.1rem 32px; border: 1.6px solid red; background-color: blue; height: 24px;">ONE</div>
<div style="margin: 1rem; padding: 1.6px 2rem; border: 0.1rem solid red; background-color: blue; height: 1.5rem;">TWO</div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,15 @@ public void testIssue446LangSelector() throws IOException {
assertTrue(vt.runTest("issue-446-lang-selector"));
}

/**
* Tests that the rem unit work with a document that has no author
* font-size set on the html element (defaults to 16px).
*/
@Test
public void testIssue459RemUnitDefault() throws IOException {
assertTrue(vt.runTest("issue-459-rem-unit-default"));
}

/**
* Tests what the CSS content property is capable of.
*/
Expand Down

0 comments on commit 1a6d2c9

Please sign in to comment.