amarikp / nehan

Automatically exported from code.google.com/p/nehan
Other
0 stars 1 forks source link

How to get current page number instead of percentage? like page [currentpage] of [total no. of pages] #3

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
How to get current page number instead of percentage? like page 
[currentpage] of [total no. of pages]

Original issue reported on code.google.com by mannch...@gmail.com on 13 Apr 2010 at 1:54

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
You must manage them yourself.
StreamParser doesn't calculate total page bacause of performance issue.
If StreamParser calculate it, it will take long time when text is too long.
So StreamParser only manage whether parser has next page or not.
In my case(in tategakibunko), I calculate total page before making the first 
page 
visible.

If you want total page when startup, sample code is like this:

{{{

var text = "??????";
var streamParser = new Nehan.StreamParser(new Nehan.Layout({
 width:300,
 height:200
}), new Nehan.TextStream(text, text.length, true));

var totalPage = 0;
var pageCount = 0;

while(streamParser.hasNextPage()){
  var output = streamParser.parsePage(pageCount);
  var percentage = streamParser.getSeekPercent(pageCount);
  pageCount++;
}

totalPage = pageCount;

}}}

But above code will block if source text is too long.
you can use setTimeout and make seek percentage visible while calculating.

Original comment by lambda.w...@gmail.com on 13 Apr 2010 at 2:44

GoogleCodeExporter commented 8 years ago
Hi Lambda 

I cannot use this.parser.outputPage(0) to return first page after apply the 
pagecount
you advised to simplereader.js. 

To fix the issue.  I have to add   
this.parser.reset();
 then everything returns normal.

This behavior is strange but I don't know whether it's bug or not ..

//=================
var SimpleReader = {
  start : function(opt){
    if(typeof opt == "undefined"){
      opt = {};
    }
    var defopt = {
      direction: "vertical",
      width: 640,
      height: 480,
      fontSize: 16,
      fontFamily: "IPA明朝, MS 明朝, Hiragino Mincho Pro",
      charImgRoot:"./img"
    };
    for(var prop in defopt){
      this[prop] = (opt[prop])? opt[prop] : defopt[prop];
    }
    var self = this;
    var text = document.getElementById("source-text").innerHTML.replace(/<br \/>/gi,
"\n").replace(/<br>/gi, "\n");
    this.pageNo = 0;
    this.writing = false;
    this.parser = new Nehan.StreamParser(new Nehan.Layout({
      direction: this.direction,
      width: this.width,
      height: this.height,
      fontSize: this.fontSize,
      fontFamily: this.fontFamily,
      charImgRoot: this.charImgRoot
    }), new Nehan.TextStream(text, text.length, true));

    document.getElementById("pager-next").onclick = function(){ self.next(); return
false; };
    document.getElementById("pager-prev").onclick = function(){ self.prev(); return
false; };

    this.write(0);
    pageCount=0
    while(this.parser.hasNextPage()){
            var output = this.parser.parsePage(pageCount);
            //var percentage = this.parser.getSeekPercent(pageCount);
            pageCount++;
        }

        this.totalPage = pageCount;
      this.parser.reset();
    //alert(totalPage);
    alert(this.totalPage);
  },
  totalPage:0,
  write : function(pageNo){
    this.writing = true;
    var output = this.parser.outputPage(pageNo);
    if(output != ""){
      document.getElementById("result").innerHTML = output;
    }
    this.writing = false;
  },
  next : function(){
    if(!this.writing && this.parser.hasNextPage()){
      this.pageNo++;
      this.write(this.pageNo);
    }
  },
  prev : function(){
    if(!this.writing && this.pageNo > 0){
      this.pageNo--;
      this.write(this.pageNo);
    }
  }
}

//==============

Original comment by mannch...@gmail.com on 15 Apr 2010 at 6:08

GoogleCodeExporter commented 8 years ago
Maybe position of "this.write(0)" is wrong.
You call this before calculating total page.
The function("this.write(0)") will call this.parser.outputPage(0) inside,
and will change the stream position of "this.parser".
So you can rewrite your program like this.

//==========================
// comment out
//this.write(0);
pageCount=0
while(this.parser.hasNextPage()){
  this.parser.parsePage(pageCount);
  //var percentage = this.parser.getSeekPercent(pageCount);
  pageCount++;
}
this.totalPage = pageCount;

// comment out
//this.parser.reset();

//alert(totalPage);
alert(this.totalPage);

// now you can get the total page, so let's write first page.
this.write(0);

//==========================

Original comment by lambda.w...@gmail.com on 15 Apr 2010 at 9:08