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

java.util.regex.PatternSyntaxException on android #394

Closed
michaelhugi opened this issue Jul 14, 2020 · 6 comments
Closed

java.util.regex.PatternSyntaxException on android #394

michaelhugi opened this issue Jul 14, 2020 · 6 comments
Labels

Comments

@michaelhugi
Copy link

librepdf 1.3.20 is almost working on android. There is only one issue with custom fonts:

  • Registering font (create local file of ttf from resources and register it with alias works)
  • FontFactory.getFont(...) calls
public class FontFactoryImp implements FontProvider {
    ...
    public Font getFont(@Nullable String fontname, String encoding, boolean embedded, float size, int style, @Nullable Color color, boolean cached) {

        ...Line 195
        try {
                // the font is a type 1 font or CJK font
                basefont = BaseFont.createFont(fontname, encoding, embedded, cached, null, null, true);
            } catch (DocumentException ignored) {
            }
        ...
    }
...
}

Used in Play! Framework DocumentException is thrown.
On Android java.util.regex.PatternSyntaxException is thrown, therefore using custom fonts is not working. The error is caused by the following regexes that can't be resolved for some reason.

public final class MessageLocalization {
    ... Line 172
    public static String getComposedMessage(String key, Object p1, Object p2, Object p3, Object p4) {
        String msg = getMessage(key);
        if (p1 != null) {
            msg = msg.replaceAll("\\{1}", p1.toString());
        }
        if (p2 != null) {
            msg = msg.replaceAll("\\{2}", p2.toString());
        }
        if (p3 != null) {
            msg = msg.replaceAll("\\{3}", p3.toString());
        }
        if (p4 != null) {
            msg = msg.replaceAll("\\{4}", p4.toString());
        }
        return msg;
    }
    ...
}

On android build.gradle

dependencies {
    implementation 'com.github.librepdf:openpdf:1.3.20'
    implementation 'com.github.andob:android-awt:1.0.0' // Used by OpenPDF
}

/* necessary for OpenPDF */
repositories {
    maven { url "https://jitpack.io" }
}
@michaelhugi
Copy link
Author

For anyone having this issue. Workaround is to 1. register font and 2. getFont with:

FontFactory.getFont(FontFactory.getFontImp().getFontPath("font-alias").toString(), font-size);

@joseluu
Copy link

joseluu commented Jan 15, 2021

The proper syntax would be: msg = msg.replaceAll("\\{1\\}", p1.toString());
The second bracket should be escaped like the first because it is a special char in the regex language
This happens only on android because they have substituted the java pattern matcher by a native library: ICU which is less tolerant

The call is in openpdf/src/main/java/com/lowagie/text/error_messages/MessageLocalization.java

@asturio
Copy link
Member

asturio commented Jan 15, 2021

This code seems to have been changed by some IDE-Optimisations. It was originally:

if (p1 != null) {
    msg = msg.replaceAll("\\{1\\}", p1.toString());
}

@asturio
Copy link
Member

asturio commented Jan 15, 2021

I'll do some refactoring on this, this weekend. Stay tuned.

@asturio
Copy link
Member

asturio commented Jan 16, 2021

Hi @joseluu , @michaelhugi ,

Could you try with the SNAPSHOT-Version, to see if the issue is fixed?

Thanks

@asturio asturio added bug and removed question labels Jan 16, 2021
@joseluu
Copy link

joseluu commented Jan 19, 2021

Yes, I confirm that the issue is fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants