mathew-kurian/TextJustify-Android

How can I get the number of lines of text?

Opened this issue · 23 comments

I want to do pagination

@object1984 The number of lines is stored in there but I have not made it accessible yet. Will do that in the next 2 days. Hold tight.

How much can I get the page number of rows to display it

2015-01-07 13:47 GMT+08:00 Mathew Kurian notifications@github.com:

@object1984 https://github.com/object1984 The number of lines is stored
in there but I have not made it accessible yet. Will do that in the next 2
days. Hold tight.


Reply to this email directly or view it on GitHub
#50 (comment)
.

To get the line count:

DocumentView documentView;
Log.d("LineCount", Integer.toString(documentView.getLineCount());

To draw a certain area of text.

// Page dimensions
float pageHeight = 600;
float pageWidth = 400;

// Create and setup layout
IDocumentLayout documentLayout = new StringDocumentLayout(this, new TextPaint());
documentLayout.setText("Large text input");
documentLayout.getLayoutParams().setParentWidth(pageWidth);
documentLayout.measure(new IDocumentLayout.ISet<Float>() {
    @Override
    public void set(Float val) {
        Log.d("progress", val.toString());
    }
}, new IDocumentLayout.IGet<Boolean>() {
    @Override
    public Boolean get() {
        return true; // don't ever cancel the process
    }
});

// Create bitmap
Bitmap bitmap = Bitmap.createBitmap((int) pageWidth, (int) pageHeight, Bitmap.Config.ARGB_8888);

// If you only want text between 30 and 100
int drawStartY = 30;
int drawEndY = 100;
documentLayout.draw(new Canvas(bitmap), drawStartY, drawEndY);

so i should devide large text and i should paginate.
how can i measure text height or how i know filled with text?

Paginating is up to you. Depends on how you use it. If you the provided DocumentView class which manages the DocumentLayout then it will do smart caching and automatically draw the content for you. But if you have a large amount of data like a chapter in a book, you can give each DocumentView a chapter of the book and it will manage it for you.

Let me expand. You can give DocumentLayout a very large text. Imagine it is Ypx tall after documentLayout.measure() is called. You can then do:

// To get Y
Y = documentLayout.getMeasuredHeight();
// Page 1
documentLayout.draw(canvas, 0, Apx);  // A <  Y 
// Page 2
documentLayout.draw(canvas, Apx, Bpx);  // A < B <  Y 
// Page 3
documentLayout.draw(canvas, Bpx, Ypx);  // B < Y

The important thing to note is that DocumentLayout#draw(...) will only draw the text that falls in between the (vertical) offsets you specify.

if i create new documentLayout for each page, i should define how many text fill the documentLaoyut (for current screen size).

for example: i give 90 words to documentLayout, i check, it's not filled, i give 95 words and i check, i give 100 words and it's filled, second page will begin from 101'st word

@jamshid88 You don't need to do that in this case because you can give DocumentLayout all the text at once. You tell documentlayout how wide your screen is. Then it will fill it with text. When it finishes, you will get how tall the text is. That means you know how many pages you need.

documentLayout.setText(myLongText);
documentLayout.getLayoutParams().setParentWidth(getScreenWidth());
documentLayout.measure(progress, cancel);
float measuredHeight = documentLayout.getMeasuredHeight();

// At least 1 page
Log.d("I need this many pages", Math.max(1, measuredHeight / getScreenHeight())); 

PS: I recommend updating to 2.0.4. Make sure to migrate the code as necessary. Minor changes which improve readability and one major bug fix.

if i give 0 to drawStartY (startTop) and drawEndY(startBottom), nothing changed. (build: 2.0.4)

// If you only want text between 30 and 100
int drawStartY = 0;
int drawEndY = 0;
documentLayout.draw(new Canvas(bitmap), drawStartY, drawEndY);

What do you mean nothing changed?

@bluejamesbond
Oh, it's uninmportant. (if i increase the values nothing changed)

mesaureHeight is normally worked.
Thank you!

@bluejamesbond
now i debugging documentLayout, i set the text, Justify working normally, but last symbol of the text disappeared. sometime, first symbol disappeared on documentLayout

Interesting. Can you provide me with some code to debug.

/**
     * set the text with justify style
     *
     * @param text
     * @param canvas
     */
    private void setTextAndJustify(CharSequence text, Canvas canvas) {
        documentLayout.setText(text);
//        documentLayout.getLayoutParams().setReverse(true);
        documentLayout.getLayoutParams().setTextAlignment(TextAlignment.JUSTIFIED);
        documentLayout.measure(new IDocumentLayout.IProgress<Float>() {
            @Override
            public void onUpdate(Float val) {
                Log.d("progress", val.toString());
            }

        }, new IDocumentLayout.ICancel<Boolean>() {
            @Override
            public Boolean isCancelled() {
                return false;
            }
        });

        documentLayout.draw(canvas);
    }

Can you provide me the full code?

if curl the page, loadBitmap method will work.
i debugged set the text to documentLayout, but sometimes (not always) first or (both) last symbol disappeared.

/**
* creates bitmap
*
* @param width width of bitmap
* @param height height of bitmap
* @param index index of page
* @return returns ready bitmap
*/
private Bitmap loadBitmap(int width, int height, int index) {

        Bitmap b = Bitmap.createBitmap(bitmapWidth, bitmapMinusStatusB, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);

        // Create and setup layout
        documentLayout = new SpannableDocumentLayout(ContentReaderController.this, getTextPaint());
        documentLayout.getLayoutParams().setParentWidth(displayWidth);
        documentLayout.getLayoutParams().setInsetPaddingLeft(padding);
        documentLayout.getLayoutParams().setInsetPaddingRight(padding);
        documentLayout.getLayoutParams().setTextAlignment(TextAlignment.JUSTIFIED);

        if (currentPageIndex > curlViewIndex) {
            if (mCurlView.isLeft()) {
                drawLeftPage(c, getStart(), getEnd());                
           }          
        } else {
                drawRightPage(c, getStart(), getEnd());                
        }

        return b;
    }

/**
 * sets text to the text view
 *
 * @param canvas canvas to be drawn to the text view
 */
private void drawRightPage(Canvas canvas, int start, int end) {
    CharSequence text = buffer.getTextBuffer().subSequence(start, end);
    if (text.length() > 0 && (text.charAt(0) == '\n' || text.charAt(0) == ' ')) {
        text = text.subSequence(1, text.length());
    }
    setTextAndJustify(text, canvas);
}

@jamshid88 Can you provide getStart and getEnd

we have large text, if we paginate, save start and end of page index on the text. so they saved numbers.

@jamshid88 I need to see where you invoke documentLayout.draw and documentLayout.measure to debug the issue

you can see, documentLayout.draw and documentLayout.measure in setTextAndJustify()

if it's complicated, i can try to give you project

/**
* set the text with justify style
*
* @param text
* @param canvas
*/
private void setTextAndJustify(CharSequence text, Canvas canvas) {
documentLayout.setText(text);
// documentLayout.getLayoutParams().setReverse(true);
documentLayout.getLayoutParams().setTextAlignment(TextAlignment.JUSTIFIED);
documentLayout.measure(new IDocumentLayout.IProgress() {
@OverRide
public void onUpdate(Float val) {
Log.d("progress", val.toString());
}

      }, new IDocumentLayout.ICancel<Boolean>() {
        @Override
        public Boolean isCancelled() {
            return false;
        }
     });

    documentLayout.draw(canvas);
}

Can you provide me the project?

if text start with the space, 2 nd symbol deleted, if i cut the space(symbol) everythinng ok.
if the text ended with space, last symbol deleted and everything is ok.

Now, everything working.

Thank you for the project and for your job and for your time!

Your most welcome. But that is interesting. Can you provide me with some screenshots? I would like to fix this issue for the future. Thank you!