diff --git a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/context/ContentFunctionFactory.java b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/context/ContentFunctionFactory.java index c79b2c6f6..4418673a6 100644 --- a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/context/ContentFunctionFactory.java +++ b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/context/ContentFunctionFactory.java @@ -44,6 +44,7 @@ public ContentFunctionFactory() { _functions.add(new PageCounterFunction()); _functions.add(new PagesCounterFunction()); _functions.add(new TargetCounterFunction()); + _functions.add(new TargetTextFunction()); _functions.add(new LeaderFunction()); _functions.add(new FsIfCutOffFunction()); } @@ -232,6 +233,56 @@ public boolean canHandle(LayoutContext c, FSFunction function) { } } + + private static class TargetTextFunction implements ContentFunction { + @Override + public boolean isStatic() { + return false; + } + + @Override + public String calculate(RenderingContext c, FSFunction function, InlineText text) { + String uri = text.getParent().getElement().getAttribute("href"); + if (uri != null && uri.startsWith("#")) { + String anchor = uri.substring(1); + Box target = c.getBoxById(anchor); + if (target != null) { + StringBuilder strBuilder = new StringBuilder(); + target.collectText(c, strBuilder); + return strBuilder.toString(); + } + } + return ""; + } + + @Override + public String calculate(LayoutContext c, FSFunction function) { + return null; + } + + @Override + public String getLayoutReplacementText() { + return "ABCABC"; + } + + @Override + public boolean canHandle(LayoutContext c, FSFunction function) { + if (c.isPrint() && function.getName().equals("target-text")) { + List parameters = function.getParameters(); + if(parameters.size() == 1) { + FSFunction f = parameters.get(0).getFunction(); + if (f == null || + f.getParameters().size() != 1 || + ! f.getParameters().get(0).getStringValue().equals("href")) { + return false; + } + return true; + } + } + return false; + } + } + /** * Partially implements leaders as specified here: * http://www.w3.org/TR/2007/WD-css3-gcpm-20070504/#leaders diff --git a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/parser/property/ContentPropertyBuilder.java b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/parser/property/ContentPropertyBuilder.java index 2628490be..061d831de 100644 --- a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/parser/property/ContentPropertyBuilder.java +++ b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/parser/property/ContentPropertyBuilder.java @@ -99,6 +99,6 @@ private boolean isFunctionAllowed(FSFunction function) { String name = function.getName(); return name.equals("attr") || name.equals("counter") || name.equals("counters") || name.equals("element") || name.startsWith("-fs") || name.equals("target-counter") || - name.equals("leader"); + name.equals("leader") || name.startsWith("target-text"); } } diff --git a/openhtmltopdf-examples/src/main/resources/visualtest/expected/content-property-capabilities.pdf b/openhtmltopdf-examples/src/main/resources/visualtest/expected/content-property-capabilities.pdf index df6fd3845..fe2158000 100644 Binary files a/openhtmltopdf-examples/src/main/resources/visualtest/expected/content-property-capabilities.pdf and b/openhtmltopdf-examples/src/main/resources/visualtest/expected/content-property-capabilities.pdf differ diff --git a/openhtmltopdf-examples/src/main/resources/visualtest/html/content-property-capabilities.html b/openhtmltopdf-examples/src/main/resources/visualtest/html/content-property-capabilities.html index bb94cc79b..5289bbbba 100644 --- a/openhtmltopdf-examples/src/main/resources/visualtest/html/content-property-capabilities.html +++ b/openhtmltopdf-examples/src/main/resources/visualtest/html/content-property-capabilities.html @@ -17,6 +17,12 @@ content: url(../../demos/images/flyingsaucer.png) "..." url(../../demos/images/flyingsaucer.png); } } +table { + line-height: 100%; +} +table tr { + font-size: small; +} body { margin: 10px; max-width: 340px; @@ -37,12 +43,28 @@ background-color: orange; content: url(../../demos/images/flyingsaucer.png) "..." url(../../demos/images/flyingsaucer.png); } +#four::after { + content: "Table: " target-text(attr(href)) leader(dotted) target-counter(attr(href), page); +} +#five::after { + content: target-text(attr(href)) leader(dotted) target-counter(attr(href), page); +} + +
ONE
WORLD
With images:
+

Title 1

+ + + + + + +
Population by country
CountryWorld share
China18.47%
India17.70%
United States4.25%