Skip to content

Commit 06babfa

Browse files
committed
Support direction SVG property
DEVSIX-8779
1 parent 6ed0578 commit 06babfa

File tree

6 files changed

+33
-8
lines changed

6 files changed

+33
-8
lines changed

layout/src/main/java/com/itextpdf/layout/renderer/ParagraphRenderer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ public LayoutResult layout(LayoutContext layoutContext) {
8484
if (orphansControl != null || widowsControl != null) {
8585
return OrphansWidowsLayoutHelper.orphansWidowsAwareLayout(this, layoutContext, orphansControl, widowsControl);
8686
}
87+
if (RenderingMode.SVG_MODE == this.<RenderingMode>getProperty(Property.RENDERING_MODE) &&
88+
!TypographyUtils.isPdfCalligraphAvailable()) {
89+
// BASE_DIRECTION property is always set to the SVG text since we can't easily check whether typography is
90+
// available at svg module level, but it makes no sense without typography, so it is removed here.
91+
this.deleteProperty(Property.BASE_DIRECTION);
92+
}
8793
final LayoutResult layoutResult = directLayout(layoutContext);
8894
updateParentLines(this);
8995
updateParentLines((ParagraphRenderer) layoutResult.getSplitRenderer());

svg/src/main/java/com/itextpdf/svg/SvgConstants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,11 @@ public static final class Attributes extends CommonAttributeConstants {
507507
*/
508508
public static final String D = "d";
509509

510+
/**
511+
* Attribute defining the direction used by the text
512+
*/
513+
public static final String DIRECTION = "direction";
514+
510515
/**
511516
* Attribute defining the relative x-translation of a text-element
512517
*/

svg/src/main/java/com/itextpdf/svg/renderers/impl/TextSvgBranchRenderer.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ This file is part of the iText (R) project.
3434
import com.itextpdf.layout.font.FontProvider;
3535
import com.itextpdf.layout.font.FontSet;
3636
import com.itextpdf.layout.layout.LayoutPosition;
37+
import com.itextpdf.layout.properties.BaseDirection;
3738
import com.itextpdf.layout.properties.IBeforeTextRestoreExecutor;
3839
import com.itextpdf.layout.properties.Property;
3940
import com.itextpdf.layout.properties.RenderingMode;
@@ -246,6 +247,13 @@ protected void doDraw(SvgDrawContext context) {
246247
new ClippedElementDrawer(getParentClipPath(), context));
247248
}
248249
this.paragraph.setMargin(0);
250+
String direction = this.attributesAndStyles.get(SvgConstants.Attributes.DIRECTION);
251+
boolean isRtl = "rtl".equals(direction);
252+
if (isRtl) {
253+
paragraph.setProperty(Property.BASE_DIRECTION, BaseDirection.RIGHT_TO_LEFT);
254+
} else {
255+
paragraph.setProperty(Property.BASE_DIRECTION, BaseDirection.LEFT_TO_RIGHT);
256+
}
249257
applyTextRenderingMode(paragraph);
250258
applyFontProperties(paragraph, context);
251259
// We resolve and draw absolutely positioned text chunks similar to getTextRectangle method. We are interested
@@ -473,22 +481,27 @@ private void deepCopyChildren(TextSvgBranchRenderer deepCopy) {
473481

474482
private void applyTextAnchor() {
475483
if (this.attributesAndStyles != null &&
476-
this.attributesAndStyles.containsKey(SvgConstants.Attributes.TEXT_ANCHOR)) {
477-
String textAnchorValue = this.getAttribute(SvgConstants.Attributes.TEXT_ANCHOR);
478-
applyTextAnchor(textAnchorValue);
484+
(this.attributesAndStyles.containsKey(SvgConstants.Attributes.TEXT_ANCHOR) ||
485+
this.attributesAndStyles.containsKey(SvgConstants.Attributes.DIRECTION))) {
486+
String textAnchorValue = getAttributeOrDefault(SvgConstants.Attributes.TEXT_ANCHOR,
487+
SvgConstants.Values.TEXT_ANCHOR_START);
488+
String direction = this.attributesAndStyles.get(SvgConstants.Attributes.DIRECTION);
489+
boolean isRtl = "rtl".equals(direction);
490+
applyTextAnchor(textAnchorValue, isRtl);
479491
}
480492
}
481493

482-
private void applyTextAnchor(String textAnchorValue) {
494+
private void applyTextAnchor(String textAnchorValue, boolean isRtl) {
483495
if (getParent() instanceof TextSvgBranchRenderer) {
484-
((TextSvgBranchRenderer) getParent()).applyTextAnchor(textAnchorValue);
496+
((TextSvgBranchRenderer) getParent()).applyTextAnchor(textAnchorValue, isRtl);
485497
return;
486498
}
487499
if (SvgConstants.Values.TEXT_ANCHOR_MIDDLE.equals(textAnchorValue)) {
488500
paragraph.setProperty(Property.TEXT_ANCHOR, TextAnchor.MIDDLE);
489501
return;
490502
}
491-
if (SvgConstants.Values.TEXT_ANCHOR_END.equals(textAnchorValue)) {
503+
if (SvgConstants.Values.TEXT_ANCHOR_END.equals(textAnchorValue) && !isRtl ||
504+
!SvgConstants.Values.TEXT_ANCHOR_END.equals(textAnchorValue) && isRtl) {
492505
paragraph.setProperty(Property.TEXT_ANCHOR, TextAnchor.END);
493506
return;
494507
}

svg/src/test/java/com/itextpdf/svg/css/DirectionTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ This file is part of the iText (R) project.
2222
*/
2323
package com.itextpdf.svg.css;
2424

25+
import com.itextpdf.svg.SvgConstants;
2526
import com.itextpdf.svg.renderers.SvgIntegrationTest;
2627
import com.itextpdf.test.ITextTest;
2728

2829
import java.io.IOException;
30+
2931
import org.junit.jupiter.api.BeforeAll;
3032
import org.junit.jupiter.api.Tag;
3133
import org.junit.jupiter.api.Test;
@@ -41,8 +43,7 @@ public static void beforeClass() {
4143
}
4244

4345
@Test
44-
//TODO DEVSIX-8779: update cmp file after supporting
4546
public void directionAttributeTest() throws IOException, InterruptedException {
46-
convertAndCompareSinglePage(SOURCE_FOLDER, DESTINATION_FOLDER,"direction");
47+
convertAndCompareSinglePage(SOURCE_FOLDER, DESTINATION_FOLDER, SvgConstants.Attributes.DIRECTION);
4748
}
4849
}
Binary file not shown.

0 commit comments

Comments
 (0)