Android -- FBReader 阅读笔记 (二)

给简书写没纸了,在弄一个新页吧


十二、绘制逻辑

估计了解阅读器原理的人会说, 怎么先说绘制,为什么fbreader 怎么生成的Model,并且怎么计算的排版的为什么不写在前面?

之前不了解阅读器,我还以为是在做绘制的时候进行的排版处理呢,蜜汁尴尬啊!

fbreader的主要绘制流程实在ZLTextView里,这个类叫view,但是,他其实不是继承自Android的view类。只是叫这个名字而已。

首先,我们还是看FBReader 的 openBook 方法。并一路跟下去在FBReaderApp的openBookInternal方法中会调用 setView(BookTextView);

调用了widget 的 repaint 方法,这个weight是ZLAndroidWidget

1
ZLAndroidWidget::repaint()

1
2
3
4
@Override
public void repaint() {
postInvalidate();
}

就这一句,那就看onDraw吧

1
ZLAndroidWidget::onDraw(final Canvas canvas)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Override
protected void onDraw(final Canvas canvas) {
final Context context = getContext();
if (context instanceof FBReader) {
((FBReader) context).createWakeLock();// 开启不断重绘自身的定时器
} else {
System.err.println("A surprise: view's context is not an FBReader");
}
super.onDraw(canvas);

if (getAnimationProvider().inProgress()) {
onDrawInScrolling(canvas);
} else {
onDrawStatic(canvas);
ZLApplication.Instance().onRepaintFinished();
}
}

最重要的也就是onDrawStatic(canvas);这个了。

我们看选中的这一句

这里执行了myWidget的drawOnBitmap方法

调用了paint方法
这个view是ZLView的一个子类,直接看ZLTextView的paint

1
ZLTextView::paint(ZLPaintContext context, PageIndex pageIndex)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* 绘制阅读界面
*/
@Override
public synchronized void paint(ZLPaintContext context, PageIndex pageIndex) {
setContext(context);
// 壁纸
final ZLFile wallpaper = getWallpaperFile();
if (wallpaper != null) {
context.clear(wallpaper, getFillMode());
} else {
context.clear(getBackgroundColor());
}

// 判断文章为空的情况
if (myModel == null || myModel.getParagraphsNumber() == 0) {
return;
}

ZLTextPage page;
LogUtils.d("ZLTextView -> paint pageIndex: " + pageIndex);
switch (pageIndex) {
default:
case current:
page = myCurrentPage;
break;
case previous:
page = myPreviousPage;
if (myPreviousPage.PaintState == PaintStateEnum.NOTHING_TO_PAINT) {
preparePaintInfo(myCurrentPage);
myPreviousPage.EndCursor.setCursor(myCurrentPage.StartCursor);
myPreviousPage.PaintState = PaintStateEnum.END_IS_KNOWN;
}
break;
case next:
page = myNextPage;
if (myNextPage.PaintState == PaintStateEnum.NOTHING_TO_PAINT) {
preparePaintInfo(myCurrentPage);
myNextPage.StartCursor.setCursor(myCurrentPage.EndCursor);
myNextPage.PaintState = PaintStateEnum.START_IS_KNOWN;
}
}

page.TextElementMap.clear();
// 进行绘制之前的准备
preparePaintInfo(page);

if (page.StartCursor.isNull() || page.EndCursor.isNull()) {
return;
}

final ArrayList<ZLTextLineInfo> lineInfos = page.LineInfos;
final int[] labels = new int[lineInfos.size() + 1];
int x = getLeftMargin();
int y = getTopMargin();
int index = 0;
ZLTextLineInfo previousInfo = null;
for (ZLTextLineInfo info : lineInfos) {
info.adjust(previousInfo);
prepareTextLine(page, info, x, y);
y += info.Height + info.Descent + info.VSpaceAfter;
labels[++index] = page.TextElementMap.size();
if (index == page.Column0Height) {
y = getTopMargin();
x += page.getTextWidth() + getSpaceBetweenColumns();
}
previousInfo = info;
}

x = getLeftMargin();
y = getTopMargin();
index = 0;
for (ZLTextLineInfo info : lineInfos) {
drawHighlightings(page, info, labels[index], labels[index + 1], x, y);
y += info.Height + info.Descent + info.VSpaceAfter;
++index;
if (index == page.Column0Height) {
y = getTopMargin();
x += page.getTextWidth() + getSpaceBetweenColumns();
}
}
// 绘制这行文字
x = getLeftMargin();
y = getTopMargin();
index = 0;
for (ZLTextLineInfo info : lineInfos) {
drawTextLine(page, info, labels[index], labels[index + 1]);
y += info.Height + info.Descent + info.VSpaceAfter;
++index;
if (index == page.Column0Height) {
y = getTopMargin();
x += page.getTextWidth() + getSpaceBetweenColumns();
}
}

// 绘制被选择的文字的区域
final ZLTextRegion selectedElementRegion = getSelectedRegion(page);
if (selectedElementRegion != null && myHighlightSelectedRegion) {
selectedElementRegion.draw(context);
}

// 绘制选择文字时左右两个拖动柄
drawSelectionCursor(context, getSelectionCursorPoint(page, ZLTextSelectionCursor.Left));
drawSelectionCursor(context, getSelectionCursorPoint(page, ZLTextSelectionCursor.Right));
}

看到了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

我也是这个时候才发现,原来,阅读器是先解析文件,然后进行排版(姑且叫他排版吧),然后进行绘制,是一条线下来的

## 十三、排版资源

fbreader解析epub的部分看了没啥意思,也就是按照epub格式进行解析。网上有很多想这样的库。所以,忘记这个, 直接看Model是怎么生成的吧!

在绘制的时候,绘制文字调用了ZLTextView这个类的drawTextLine这个方法
从代码上来看,绘制的时候取得了paragraph.getElement(wordIndex)进行绘制
paragraph是通过info.ParagraphCursor 得到的 , info是上面的lineInfos循环得到的子项的对象, 而lineInfos是page.LineInfos 得到的,而page是构造的时候new 出来的, 知道paint调用的时候经过了preparePaintInfo()方法的调用,才会赋值。

有点乱, 想直接分析```paint```方法,在preparePaintInfo(page);方法之前,参数page还是一个初始化的状态,看没有看到相关的书的信息。那么给page复制时一定就是这个方法起到了作用。

最重要的方法就是```ZLTextView::preparePaintInfo(ZLTextPage page)```其中的核心方法是```ZLTextView::buildInfos(ZLTextPage page, ZLTextWordCursor start, ZLTextWordCursor result)```代码比较负责,我理解的不是很透彻。只能说个大概:
* 先绘制当前页
* 绘制下一页的时候,将当前页的最后的指针给下一页
* 绘制上一页的时候,将上一页的尾指针给当前页的头指针
* 排版满一行获取下一行的信息
* 排版满一页获取下一页的信息

这里会存在一个问题,但我在绘制的时候,其实我是只知道当前也的文字,全本书是看不到的,fbreader是当每次翻页的时候才会排版绘制。并且,排版完之后就进行绘制。

## 十四、绘制当前页所在的章节数

正如上面一段说的, 在绘制的时候。其实是拿不到整章的数据的。看了一个下午。发现fbreader中提供了一个这样的方法

![](http://upload-images.jianshu.io/upload_images/1285832-6e3656c319f29696.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

这段代码在ZLTextView::buildInfos中。可以看到它但排版完一页之后,会判断了一下这不是这一章的结束。如果他们判断出是不是这一样的结束,那一定就有办法判断当前页属于那一章。

这个方法存在与ZLTextParagraphCursor类中,那么我在绘制的时候可以拿到当前页,我们可以看一下它的bean。

![](http://upload-images.jianshu.io/upload_images/1285832-ddbef323be1ff5ec.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

EndCursor这个指针就代表当前页的之后尾指针,我们要判断它是不是这一章的末尾。

ZLTextParagraphCursor::isEndOfSection

1
2
3
4
```
public boolean isEndOfSection() {
return Model.getParagraph(Index).getKind() == ZLTextParagraph.Kind.END_OF_SECTION_PARAGRAPH;
}

主要是判断Model 的 getParagraph的之的kind,再继续跟getParagraph方法

1
ZLTextPlainModel::getParagraph(int index)
1
2
3
4
5
6
public final ZLTextParagraph getParagraph(int index) {
final byte kind = myParagraphKinds[index];
return (kind == ZLTextParagraph.Kind.TEXT_PARAGRAPH) ?
new ZLTextParagraphImpl(this, index) :
new ZLTextSpecialParagraphImpl(kind, this, index);
}

查找了myParagraphKinds的index个元素, 然后构建了一个ZLTextParagraph 。
虽然我现在还不知道myParagraphKinds和index的具体意义。但我要是将当前页之前的所有Index之前的元素一一判断是不是isEndOfSection,然后手动计数,我就可以知道他是第几章的。问题就是我要遍历myParagraphKinds,而且越到后面,代价越大。但是这也是我相出来的唯一办法。先有在好,满足需求吧!(我从网上下了一本书,大概有一百多章,一万两千多页,这个Index会达到六十多万,手动滑稽)

思路有了开始动手,在ZLTextParagraphCursor类中增加一段

在绘制的时候,拿到当前页page

1
int chapterIndex = page.EndCursor.getParagraphCursor().getChapterIndex();