Skip to content
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

fix issue: 534 #539

Merged
merged 1 commit into from
May 13, 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 @@ -100,6 +100,10 @@ public static boolean isEnabled() {
return enabled;
}

public static int getFlags() {
return flags;
}

public static boolean supportsFont(BaseFont baseFont) {
boolean supports = enabled && (awtFontMap.get(baseFont) != null);
return supports;
Expand Down
5 changes: 4 additions & 1 deletion openpdf/src/main/java/com/lowagie/text/pdf/PdfDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -1700,7 +1700,10 @@ else if (isJustified && numberOfSpaces > 0 && chunk.isSpecialEncoding()) {
text.showText(s);
else {
float spaceCorrection = - baseWordSpacing * 1000f / chunk.font.size() / hScale;
PdfTextArray textArray = new PdfTextArray(s.substring(0, idx));
PdfTextArray textArray = new PdfTextArray();
if (LayoutProcessor.isEnabled()&&LayoutProcessor.getFlags() == java.awt.Font.LAYOUT_RIGHT_TO_LEFT)
textArray.setRTL(true);
textArray.add(s.substring(0, idx));
int lastIdx = idx;
while ((idx = s.indexOf(' ', lastIdx + 1)) >= 0) {
textArray.add(spaceCorrection);
Expand Down
38 changes: 31 additions & 7 deletions openpdf/src/main/java/com/lowagie/text/pdf/PdfTextArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

package com.lowagie.text.pdf;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
Expand All @@ -63,7 +63,7 @@
*/

public class PdfTextArray{
private List<Object> arrayList = new ArrayList<>();
private List<Object> arrayList = new LinkedList<>();

// To emit a more efficient array, we consolidate
// repeated numbers or strings into single array entries.
Expand All @@ -72,6 +72,7 @@ public class PdfTextArray{
// --Mark Storer, May 12, 2008
private String lastStr;
private Float lastNum;
private boolean isRTL = false;

// constructors
public PdfTextArray(String str) {
Expand All @@ -97,11 +98,14 @@ public void add(float number) {
if (lastNum != 0) {
replaceLast(lastNum);
} else {
arrayList.remove(arrayList.size() - 1);
arrayList.remove(isRTL ? 0 : arrayList.size() - 1);
}
} else {
lastNum = number;
arrayList.add(lastNum);
if (isRTL)
arrayList.add(0, lastNum);
else
arrayList.add(lastNum);
}

lastStr = null;
Expand All @@ -110,13 +114,22 @@ public void add(float number) {
}

public void add(String str) {
if (isRTL)
str = new StringBuffer(str).reverse().toString();
if (str.length() > 0) {
if (lastStr != null) {
lastStr = lastStr + str;
if (isRTL)
lastStr = str + lastStr;
else
lastStr = lastStr + str;
replaceLast(lastStr);
} else {
lastStr = str;
arrayList.add(lastStr);
if (isRTL) {
arrayList.add(0, lastStr);
}else {
arrayList.add(lastStr);
}
}
lastNum = null;
}
Expand All @@ -129,6 +142,17 @@ List getArrayList() {

private void replaceLast(Object obj) {
// deliberately throw the IndexOutOfBoundsException if we screw up.
arrayList.set(arrayList.size() - 1, obj);
if (isRTL)
arrayList.set(0, obj);
else
arrayList.set(arrayList.size() - 1, obj);
}

public void setRTL(boolean isRTL){
this.isRTL = isRTL;
}

public boolean getRTL(){
return this.isRTL;
}
}