com.openhtmltopdf.layout.counter.RootCounterContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of openhtmltopdf-core Show documentation
Show all versions of openhtmltopdf-core Show documentation
Open HTML to PDF is a CSS 2.1 renderer written in Java. This artifact contains the core rendering and layout code.
package com.openhtmltopdf.layout.counter;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.openhtmltopdf.css.parser.CounterData;
import com.openhtmltopdf.css.style.CalculatedStyle;
public class RootCounterContext implements AbstractCounterContext {
private final Map counterMap = new HashMap<>();
public void resetCounterValue(CalculatedStyle style) {
List resets = style.getCounterReset();
if (resets != null) {
resets.forEach(cd -> counterMap.put(cd.getName(), cd.getValue()));
}
}
public void incrementCounterValue(CalculatedStyle style) {
List incs = style.getCounterIncrement();
if (incs != null) {
for (CounterData cd : incs) {
counterMap.merge(cd.getName(), cd.getValue(), (old, newVal) -> old + newVal);
}
}
}
@Override
public int getCurrentCounterValue(String name) {
Integer current = counterMap.get(name);
if (current != null) {
return current;
} else {
counterMap.put(name, 0);
return 0;
}
}
@Override
public List getCurrentCounterValues(String name) {
return Collections.singletonList(getCurrentCounterValue(name));
}
}