Skip to content

fix rtl numeric list #655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import com.openhtmltopdf.bidi.BidiReorderer;
import com.openhtmltopdf.bidi.BidiSplitter;
import org.w3c.dom.Element;

import com.openhtmltopdf.css.constants.CSSName;
Expand Down Expand Up @@ -375,16 +378,24 @@ private MarkerData.TextMarker makeTextMarker(LayoutContext c, IdentValue listSty
int listCounter = getListCounter();
text = CounterFunction.createCounterText(listStyle, listCounter);

text += ". ";
IdentValue listDirection = getParent().getStyle().getDirection();

if (listDirection == IdentValue.RTL) {
text = " .".concat(text);
} else {
assert listDirection == IdentValue.LTR || listDirection == IdentValue.AUTO;
text = text.concat(". ");
}

int w = c.getTextRenderer().getWidth(
c.getFontContext(),
getStyle().getFSFont(c),
text);

MarkerData.TextMarker result = new MarkerData.TextMarker();
result.setText(text);

result.setLayoutWidth(w);
result.setText(text);

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.openhtmltopdf.css.constants.CSSName;
import com.openhtmltopdf.css.constants.IdentValue;
import com.openhtmltopdf.css.style.CalculatedStyle;
import com.openhtmltopdf.css.style.CssContext;
import com.openhtmltopdf.extend.FSImage;

/**
Expand Down Expand Up @@ -152,11 +153,24 @@ private static boolean isInVisiblePageArea(RenderingContext c, PageBox page, Str

private static void drawText(RenderingContext c, BlockBox box, IdentValue listStyle) {
MarkerData.TextMarker text = box.getMarkerData().getTextMarker();

int x = getReferenceX(c, box);
x += -text.getLayoutWidth();
int y = getReferenceBaseline(c, box);

// calculations for numbered lists
MarkerData markerData = box.getMarkerData();

// Chrome uses the direction determined for the ol for all list-items.
IdentValue direction = box.getParent().getStyle().getDirection();

if (direction == IdentValue.RTL){
x = markerData.getReferenceLine() != null ?
x + markerData.getReferenceLine().getWidth() :
box.getParent().getAbsX() + box.getParent().getWidth() - (int) box.getParent().getPadding(c).right();
} else {
assert direction == IdentValue.LTR || direction == IdentValue.AUTO;
x += -text.getLayoutWidth();
}

c.getOutputDevice().setColor(box.getStyle().getColor());
c.getOutputDevice().setFont(box.getStyle().getFSFont(c));
if (c.getOutputDevice() instanceof AbstractOutputDevice) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ small { font-size: .83em }
s, strike, del { text-decoration: line-through }
ol, ul, dir,
menu { padding-left: 40px }
ol[dir="rtl"] {
/* Use padding-inline-start when available. */
padding-right: 40px;
padding-left: 0;
}
dd { margin-left: 40px }
ul { list-style-type: disc }
ol { list-style-type: decimal }
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<html dir="rtl">
<head>
<style>
@page {
size: 300px 1400px;
}
html * {
font-family: 'TestFont', 'arabic', sans-serif;
}
body {
border: 1px solid red;
}
</style>
</head>
<body dir="auto">
<!-- forced rtl -->
<ol dir="rtl" class="rtl">
<li>السيطرة</li>
<li>السيطرة</li>
<li>three</li>
<li>four</li>
<li>five</li>
<li>six</li>
<li>seven</li>
<li>eight</li>
<li>nine</li>
<li>ten</li>
<li>eleven</li>
<li>twelve</li>
</ol>

<!-- forced ltr -->
<ol dir="ltr">
<li>السيطرة</li>
<li>السيطرة</li>
<li>three</li>
<li>four</li>
<li>five</li>
<li>six</li>
<li>seven</li>
<li>eight</li>
<li>nine</li>
<li>ten</li>
<li>eleven</li>
<li>twelve</li>
</ol>

<!-- auto ltr -->
<ol>
<li>one</li>
<li>two</li>
<li>three</li>
<li>four</li>
<li>five</li>
<li>six</li>
<li>seven</li>
<li>eight</li>
<li>nine</li>
<li>ten</li>
<li>eleven</li>
<li>twelve</li>
</ol>

</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,15 @@ public void testArabicBiDi() throws IOException {
assertTrue(vtester.runTest("arabic-bidi", TestSupport.WITH_ARABIC));
}

/**
* Tests that list numbers are placed correctly for RTL ol elements.
* https://github.com/danfickle/openhtmltopdf/pull/655
*/
@Test
public void testPr655RtlNumericList() throws IOException {
assertTrue(vtester.runTest("pr-655-rtl-numeric-list", TestSupport.WITH_ARABIC));
}

/**
* Tests that rtl and bidi with text-align: justify works correctly.
*/
Expand Down