NightWhistler / HtmlSpanner

Android HTML rendering library with CSS support
http://nightwhistler.github.io/HtmlSpanner/
877 stars 209 forks source link

RTL ( Text Alignment bug ) #28

Open MohHeader opened 10 years ago

MohHeader commented 10 years ago

When Using RTL epub Books, Many Text are Miss-Aligned. ( Not all ) Tested on Page Turner too, the same issue exist.

I am not sure if that could be the case Because TextView be Default Auto-Adapt the Alignment. So some Alignments are inverted.

Thanks in Advance

MohHeader commented 10 years ago

A hack can be applied : [ CSSCompiler.java ]

if ( "align".equals(key) || "text-align".equals(key)) {
            try {
                String value2 = value;
                if (value.equals("right"))
                        value2 = "left";
                else if( value.equals("left"))
                    value2 = "right";
                final Style.TextAlignment alignment = Style.TextAlignment.valueOf(value2.toUpperCase());

But it is very Dirty :(, & will Solve only the p tag, img tags will be reversed ( it was correct at first ) ! Also added gravity="right"

This could be Considered a Dirty Temp Solution,

looknear commented 10 years ago

page-turner not supporting justify aligned (not on RTL books). only LEFT and RIGHT. if that answer your question...

On Tue, May 13, 2014 at 2:12 PM, Mohammed Header notifications@github.comwrote:

A hack can be applied :

if ( "align".equals(key) || "text-align".equals(key)) { try { String value2 = value; if (value.equals("right")) value2 = "left"; else if( value.equals("left")) value2 = "right"; final Style.TextAlignment alignment = Style.TextAlignment.valueOf(value2.toUpperCase());

But it is very Dirty :(, & will Solve only the p tag, img tags will be reversed ( it was correct at first ) !

— Reply to this email directly or view it on GitHubhttps://github.com/NightWhistler/HtmlSpanner/issues/28#issuecomment-42942504 .

MohHeader commented 10 years ago

No, I don't mean justify alignment, But text is verse aligned.

i.e. if it has text-align:right it will go to left, & so on.

NightWhistler commented 10 years ago

OK, this makes sense actually... if you look at the code of

src/main/java/net/nightwhistler/htmlspanner/style/StyleCallback.java

You'll see this:

        if ( useStyle.getTextAlignment() != null ) {

            AlignmentSpan alignSpan = null;

            switch ( useStyle.getTextAlignment() ) {
                case LEFT:
                    alignSpan = new AlignNormalSpan();
                    break;
                case CENTER:
                    alignSpan = new CenterSpan();
                    break;
                case RIGHT:
                    alignSpan = new AlignOppositeSpan();
                    break;
            }

            //Log.d("StyleCallback", "Applying AlignmentSpan from " + start + " to " + end + " on text " + builder.subSequence(start, end) );
            builder.setSpan(alignSpan, start, end,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        }

As you can see it's a bit naive, mapping "right" to "opposite"... but if the alignment is already RTL, this will in fact switch the alignment back to left.