Skip to content

Commit

Permalink
Merge pull request #655 from johnnyaug/open-dev-v1
Browse files Browse the repository at this point in the history
fix rtl numeric list
  • Loading branch information
danfickle authored Feb 23, 2021
2 parents 421a892 + e4fd98d commit 76b86d9
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 4 deletions.
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

0 comments on commit 76b86d9

Please sign in to comment.