From aa5bab0409f4a92826100df1e6c15f936b5e82cf Mon Sep 17 00:00:00 2001 From: Benjamin Vega Date: Sun, 21 Feb 2021 15:45:36 +0100 Subject: [PATCH] Add feature: target-text() for content property capabilities --- .../context/ContentFunctionFactory.java | 51 ++++++++++++++++++ .../property/ContentPropertyBuilder.java | 2 +- .../content-property-capabilities.pdf | Bin 16842 -> 19323 bytes .../html/content-property-capabilities.html | 22 ++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) 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 df6fd38451d4d96ef2764311db674c36f5c8346e..fe2158000325de06652af3f6a7456e1bd4c85061 100644 GIT binary patch delta 2602 zcma)7&u<$=6qYx!V=W0vh|(6dq$Ao;Le=&Bp4rH86sJi^q^PJd;(#h}l0|WG?8pH;X3M!YrZqcx=v*G#qSO^Dt!JfR~Y$B?Pr7CaYqR=H$m;PHSzk4 zA6BMRatj;HMh8)BzMAkIO3c%VTMngWD!*h!Z01FVn{NJ!SPFd{Hq z(v#5O@Wz-&&+gS24Mn~OMIYgzkVer3I_+UdU&yo8>+*9fHTJNbu$bvv%~pLMwGXPT zAZAWZcZ>+O;=4BNwS!utdXCY+XJ}@vKsQ$l=a_{-A`Kl9#<$tnuf;rdA-~DQM6L!_ zM0pDTKTj&E$eWE?C)h_jooXj&pDps`u%wa4fi?7!h#Z<{(-&v$c-XNh7W-4kn0YdJ zac(f1FrLxmsFU9(Z;sE}8Hk(kGfjMRXXf!V>(A`YcD4OJV)Eqgnf7bfsBOCe5kags zl0%1Z6MS6Uv^T-ZGRm;thTDz#vGjuZqImrR;RH2~>h=5Xh?xwJd{mUDf7T(L4|HOV zD3-EkrnHP4W}S-*ryj6^uHI$#_mkFN*r14o_r!=M#!o#v6wQSBa(UZf(mY%K)nUr~ zRN8fzhDD8+oArH@S-Isf-{6&cmtz=N`=G{@8@^yH2;?|{NqaxVF)AcfJb_7j8744g zqf20#hv&MZj;SvowD>`WCB&b=>`yR>QJoUVeVnr8zJgX6@PU7LB(_Pm&oB#;z%Gsv zMffQgls$Ql@+2QoR3|eH{m>Q36atuvgeWePnE}jCrW*31;6As{AP}M?PG$=*-3z2U zdQg#cBub|v(KH>2mT4#2Puuz`Ci)6UC7Pro)=*DVSY-!XuHN?9y5MC+${UK^=8z1n t)?^tgDVQjs;<-oQzqwO84iNM07_~dqR_B9O0DmjZw00L3*6(h({{bp_OJe{4 delta 1016 zcmah{O=}ZT6eSaFGp|W)nVJL<`=l16YUX`UMncehOjTNx)}>NXoi=_nfh3}IQ;GZ4!Ij;#bL|BtG?Z36A4POSv z;F)MSqC#O;hEcd+WfFVhusxLRKD6*5s3((yVI#(R8JkHA z-!e}}*lsBEv$gg;%tUuR+n$26)^LJiJ+BTD7OX7%E>FQsc`R_aSso7@Zk44*j>2da zS6GnvKSO(kNKffEZU;E)aeNSg!}sbeyf3G~^WFvCpa(H75E~)mS72D)VWnpF?jGMU(@dukboA z5$-uv9Umo?Dv`n|5pq#g-BQJsu9q+3!hb0@kv)HB)z{k&9!|ScYjwi?Q$>}7CMT;` GJ@f~mKJU2z 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%