-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
EbookController.cpp
954 lines (854 loc) · 31.4 KB
/
EbookController.cpp
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
/* Copyright 2014 the SumatraPDF project authors (see AUTHORS file).
License: GPLv3 */
// utils
#include "BaseUtil.h"
#include "ArchUtil.h"
#include "GdiPlusUtil.h"
#include "HtmlParserLookup.h"
#include "HtmlPullParser.h"
#include "Mui.h"
#include "ThreadUtil.h"
#include "Timer.h"
#include "TrivialHtmlParser.h"
// rendering engines
#include "BaseEngine.h"
#include "EbookBase.h"
#include "EbookDoc.h"
#include "MobiDoc.h"
#include "HtmlFormatter.h"
#include "Doc.h"
// layout controllers
#include "SettingsStructs.h"
#include "Controller.h"
#include "EbookController.h"
#include "GlobalPrefs.h"
// ui
#include "EbookControls.h"
#include "Translations.h"
//#define NOLOG 0
#include "DebugLog.h"
static const WCHAR *GetFontName()
{
// TODO: validate the name?
return gGlobalPrefs->ebookUI.fontName;
}
static float GetFontSize()
{
float fontSize = gGlobalPrefs->ebookUI.fontSize;
if (fontSize < 7.f || fontSize > 32.f)
fontSize = 12.5;
return fontSize;
}
HtmlFormatterArgs *CreateFormatterArgsDoc(Doc doc, int dx, int dy, Allocator *textAllocator)
{
HtmlFormatterArgs *args = CreateFormatterDefaultArgs(dx, dy, textAllocator);
args->htmlStr = doc.GetHtmlData(args->htmlStrLen);
args->SetFontName(GetFontName());
args->fontSize = GetFontSize();
return args;
}
class EbookTocDest : public DocTocItem, public PageDestination {
ScopedMem<WCHAR> url;
public:
EbookTocDest(const WCHAR *title, int reparseIdx) :
DocTocItem(str::Dup(title), reparseIdx), url(nullptr) { }
EbookTocDest(const WCHAR *title, const WCHAR *url) :
DocTocItem(str::Dup(title)), url(str::Dup(url)) { }
virtual PageDestination *GetLink() { return this; }
virtual PageDestType GetDestType() const { return url ? Dest_LaunchURL : Dest_ScrollTo; }
virtual int GetDestPageNo() const { return pageNo; }
virtual RectD GetDestRect() const { return RectD(); }
virtual WCHAR *GetDestValue() const { return str::Dup(url); }
};
struct EbookFormattingData {
enum { MAX_PAGES = 256 };
HtmlPage * pages[MAX_PAGES];
size_t pageCount;
bool finished;
LONG threadNo;
EbookFormattingData(HtmlPage **pages, size_t pageCount, bool finished, LONG threadNo) :
pageCount(pageCount), finished(finished), threadNo(threadNo) {
CrashIf(pageCount > MAX_PAGES);
memcpy(this->pages, pages, pageCount * sizeof(*pages));
}
};
class EbookFormattingThread : public ThreadBase {
HtmlFormatterArgs * formatterArgs; // we own it
Doc doc;
EbookController * controller;
ControllerCallback *cb;
// state used during layout process
HtmlPage * pages[EbookFormattingData::MAX_PAGES];
int pageCount;
// we want to send 2 pages after reparseIdx as soon as we have them,
// so that we can show them to the user as quickly as possible
// We want 2 to accomodate possible 2 page view
int reparseIdx;
int pagesAfterReparseIdx;
public:
void SendPagesIfNecessary(bool force, bool finished);
bool Format();
EbookFormattingThread(Doc doc, HtmlFormatterArgs *args,
EbookController *ctrl, int reparseIdx, ControllerCallback *cb);
virtual ~EbookFormattingThread();
// ThreadBase
virtual void Run();
};
EbookFormattingThread::EbookFormattingThread(Doc doc, HtmlFormatterArgs *args, EbookController *ctrl, int reparseIdx, ControllerCallback *cb) :
doc(doc), formatterArgs(args), cb(cb), controller(ctrl), pageCount(0), reparseIdx(reparseIdx), pagesAfterReparseIdx(0)
{
CrashIf(reparseIdx < 0);
AssertCrash(doc.IsDocLoaded() || (doc.IsNone() && (nullptr != args->htmlStr)));
}
EbookFormattingThread::~EbookFormattingThread()
{
//lf("ThreadLayoutEbook::~ThreadLayoutEbook()");
delete formatterArgs;
}
// send accumulated pages if we filled the buffer or the caller forces us
void EbookFormattingThread::SendPagesIfNecessary(bool force, bool finished)
{
if (finished)
force = true;
if (!force && (pageCount < dimof(pages)))
return;
EbookFormattingData *msg = new EbookFormattingData(pages, pageCount, finished, GetNo());
//lf("ThreadLayoutEbook::SendPagesIfNecessary() sending %d pages, finished=%d", pageCount, (int)finished);
pageCount = 0;
memset(pages, 0, sizeof(pages));
cb->HandleLayoutedPages(controller, msg);
}
// layout pages from a given reparse point (beginning if nullptr)
// returns true if layout thread was cancelled
bool EbookFormattingThread::Format()
{
//lf("Started laying out ebook, reparseIdx=%d", reparseIdx);
int totalPageCount = 0;
formatterArgs->reparseIdx = 0;
pagesAfterReparseIdx = 0;
HtmlFormatter *formatter = doc.CreateFormatter(formatterArgs);
for (HtmlPage *pd = formatter->Next(); pd; pd = formatter->Next()) {
if (WasCancelRequested()) {
//lf("layout cancelled");
for (int i = 0; i < pageCount; i++) {
delete pages[i];
}
pageCount = 0;
delete pd;
// send a 'finished' message so that the thread object gets deleted
SendPagesIfNecessary(true, true /* finished */);
delete formatter;
return true;
}
pages[pageCount++] = pd;
++totalPageCount;
if (pd->reparseIdx >= reparseIdx) {
++pagesAfterReparseIdx;
}
// force sending accumulated pages
bool force = false;
if (2 == pagesAfterReparseIdx) {
force = true;
//lf("EbookFormattingThread::Format: sending pages because pagesAfterReparseIdx == %d", pagesAfterReparseIdx);
}
SendPagesIfNecessary(force, false);
CrashIf(pageCount >= dimof(pages));
}
SendPagesIfNecessary(true, true /* finished */);
delete formatter;
return false;
}
void EbookFormattingThread::Run()
{
Timer t;
Format();
//lf("Formatting time: %.2f ms", t.Stop());
}
static void DeletePages(Vec<HtmlPage*>** toDeletePtr)
{
if (!*toDeletePtr)
return;
DeleteVecMembers(**toDeletePtr);
delete *toDeletePtr;
*toDeletePtr = nullptr;
}
EbookController::EbookController(EbookControls *ctrls, ControllerCallback *cb) :
Controller(cb), ctrls(ctrls), pages(nullptr), incomingPages(nullptr),
currPageNo(0), pageSize(0, 0), formattingThread(nullptr), formattingThreadNo(-1),
currPageReparseIdx(0), handleMsgs(true), pageAnchorIds(nullptr), pageAnchorIdxs(nullptr),
navHistoryIx(0)
{
EventMgr *em = ctrls->mainWnd->evtMgr;
// TODO: do I need lambada here, can I just pass EbookController::ClickedNext directly?
em->EventsForName("next")->Clicked = [=](Control *c, int x, int y) {
this->ClickedNext(c, x, y);
};
em->EventsForName("prev")->Clicked = [=](Control *c, int x, int y) {
this->ClickedPrev(c, x, y);
};
em->EventsForControl(ctrls->progress)->Clicked = [=](Control *c, int x, int y) {
this->ClickedProgress(c, x, y);
};
PageControl *page1 = ctrls->pagesLayout->GetPage1();
PageControl *page2 = ctrls->pagesLayout->GetPage2();
em->EventsForControl(page1)->SizeChanged = [=](Control *c, int dx, int dy) {
this->SizeChangedPage(c, dx, dy);
};
em->EventsForControl(page2)->SizeChanged = [=](Control *c, int dx, int dy) {
this->SizeChangedPage(c, dx, dy);
};
em->EventsForControl(page1)->Clicked = [=](Control *c, int x, int y) {
this->ClickedPage1(c, x, y);
};
em->EventsForControl(page2)->Clicked = [=](Control *c, int x, int y) {
this->ClickedPage2(c, x, y);
};
}
EbookController::~EbookController()
{
StopFormattingThread();
EventMgr *evtMgr = ctrls->mainWnd->evtMgr;
// we must manually disconnect all events becuase evtMgr is
// destroyed after EbookController, and EbookController destructor
// will disconnect slots without deleting them, causing leaks
// TODO: this seems fragile
EnableMessageHandling(false);
CloseCurrentDocument();
DestroyEbookControls(ctrls);
delete pageAnchorIds;
delete pageAnchorIdxs;
}
// stop layout thread (if we're closing a document we'll delete
// the ebook data, so we can't have the thread keep using it)
void EbookController::StopFormattingThread()
{
if (!formattingThread)
return;
formattingThread->RequestCancel();
bool ok = formattingThread->Join();
CrashIf(!ok);
delete formattingThread;
formattingThread = nullptr;
formattingThreadNo = -1;
DeletePages(&incomingPages);
}
void EbookController::CloseCurrentDocument()
{
ctrls->pagesLayout->GetPage1()->SetPage(nullptr);
ctrls->pagesLayout->GetPage2()->SetPage(nullptr);
StopFormattingThread();
DeletePages(&pages);
doc.Delete();
pageSize = SizeI(0, 0);
}
// returns page whose content contains reparseIdx
// page is in 1..$pageCount range to match currPageNo
// returns 0 if not found (or maybe on the last page)
// returns -1 if no pages are available
static int PageForReparsePoint(Vec<HtmlPage*> *pages, int reparseIdx)
{
if (!pages || pages->Count() == 0) {
return -1;
}
// sometimes reparseIdx of first page is > 0 and the code below
// doesn't handle that, so do that case first
if (reparseIdx < pages->At(0)->reparseIdx) {
return 1;
}
for (size_t i = 0; i < pages->Count(); i++) {
HtmlPage *pd = pages->At(i);
if (pd->reparseIdx == reparseIdx) {
return (int)i + 1;
}
// this is the first page whose content is after reparseIdx, so
// the page contining reparseIdx must be the one before
if (pd->reparseIdx > reparseIdx) {
CrashIf(0 == i);
return (int)i;
}
}
return 0;
}
// gets pages as formatted from beginning, either from a temporary state
// when layout is in progress or final formatted pages
Vec<HtmlPage*> *EbookController::GetPages()
{
return pages;
}
void EbookController::HandlePagesFromEbookLayout(EbookFormattingData *ft)
{
if (formattingThreadNo != ft->threadNo) {
// this is a message from cancelled thread, we can disregard
lf("EbookController::HandlePagesFromEbookLayout() thread msg discarded, curr thread: %d, sending thread: %d", formattingThreadNo, ft->threadNo);
delete ft;
return;
}
//lf("EbookController::HandlePagesFromEbookLayout() %d pages, ft=0x%x", ft->pageCount, (int)ft);
if (incomingPages) {
for (size_t i = 0; i < ft->pageCount; i++) {
incomingPages->Append(ft->pages[i]);
}
int pageNo = PageForReparsePoint(incomingPages, currPageReparseIdx);
if (pageNo > 0) {
Vec<HtmlPage*> *toDelete = pages;
pages = incomingPages;
incomingPages = nullptr;
DeletePages(&toDelete);
GoToPage(pageNo, false);
}
} else {
CrashIf(!pages);
for (size_t i = 0; i < ft->pageCount; i++) {
pages->Append(ft->pages[i]);
}
}
if (ft->finished) {
CrashIf(!pages);
StopFormattingThread();
}
UpdateStatus();
delete ft;
}
void EbookController::TriggerLayout()
{
Size s = ctrls->pagesLayout->GetPage1()->GetDrawableSize();
SizeI size(s.Width, s.Height);
if (size.IsEmpty()) {
// we haven't been sized yet
return;
}
// CrashIf(size.dx < 100 || size.dy < 40);
if (!doc.IsDocLoaded())
return;
if (pageSize == size) {
//lf("EbookController::TriggerLayout() - skipping layout because same as last size");
return;
}
//lf("(%3d,%3d) EbookController::TriggerLayout",size.dx, size.dy);
pageSize = size; // set it early to prevent re-doing layout at the same size
StopFormattingThread();
CrashIf(incomingPages);
incomingPages = new Vec<HtmlPage*>(1024);
HtmlFormatterArgs *args = CreateFormatterArgsDoc(doc, size.dx, size.dy, &textAllocator);
formattingThread = new EbookFormattingThread(doc, args, this, currPageReparseIdx, cb);
formattingThreadNo = formattingThread->GetNo();
formattingThread->Start();
UpdateStatus();
}
void EbookController::SizeChangedPage(Control *c, int dx, int dy)
{
CrashIf(!(c == ctrls->pagesLayout->GetPage1() || c==ctrls->pagesLayout->GetPage2()));
// delay re-layout so that we don't unnecessarily do the
// work as long as the user is still resizing the window
// TODO: previously, the delay was 100 while inSizeMove and 600 else
// (to delay a bit if the user resizes but not when e.g. switching to fullscreen)
cb->RequestDelayedLayout(200);
}
void EbookController::ClickedNext(Control *c, int x, int y)
{
//CrashIf(c != ctrls->next);
GoToNextPage();
}
void EbookController::ClickedPrev(Control *c, int x, int y)
{
//CrashIf(c != ctrls->prev);
GoToPrevPage();
}
// (x, y) is in the coordinates of w
void EbookController::ClickedProgress(Control *c, int x, int y)
{
CrashIf(c != ctrls->progress);
float perc = ctrls->progress->GetPercAt(x);
int pageCount = (int)GetPages()->Count();
int newPageNo = IntFromPerc(pageCount, perc) + 1;
GoToPage(newPageNo, true);
}
void EbookController::OnClickedLink(int pageNo, DrawInstr *link)
{
ScopedMem<WCHAR> url(str::conv::FromHtmlUtf8(link->str.s, link->str.len));
if (url::IsAbsolute(url)) {
EbookTocDest dest(nullptr, url);
cb->GotoLink(&dest);
return;
}
if (Doc_Epub == doc.Type() && pages && (size_t)pageNo <= pages->Count()) {
// normalize the URL by combining it with the chapter's base path
for (int j = pageNo; j > 0; j--) {
HtmlPage *p = pages->At(j - 1);
// <pagebreak src="..." page_marker /> is usually the second instruction on a page
for (size_t k = 0; k < std::min((size_t)2, p->instructions.Count()); k++) {
DrawInstr& di = p->instructions.At(k);
if (InstrAnchor == di.type && str::StartsWith(di.str.s + di.str.len, "\" page_marker />")) {
ScopedMem<char> basePath(str::DupN(di.str.s, di.str.len));
ScopedMem<char> relPath(ResolveHtmlEntities(link->str.s, link->str.len));
ScopedMem<char> absPath(NormalizeURL(relPath, basePath));
url.Set(str::conv::FromUtf8(absPath));
j = 0; // done
break;
}
}
}
}
int idx = ResolvePageAnchor(url);
if (-1 == idx && str::FindChar(url, '%')) {
url::DecodeInPlace(url);
idx = ResolvePageAnchor(url);
}
if (idx != -1) {
EbookTocDest dest(nullptr, idx);
cb->GotoLink(&dest);
}
}
void EbookController::ClickedPage1(Control *c, int x, int y)
{
cb->FocusFrame(true);
CrashIf(c != ctrls->pagesLayout->GetPage1());
DrawInstr *link = ctrls->pagesLayout->GetPage1()->GetLinkAt(x, y);
if (link)
OnClickedLink(currPageNo, link);
}
void EbookController::ClickedPage2(Control *c, int x, int y)
{
cb->FocusFrame(true);
CrashIf(!IsDoublePage());
CrashIf(c != ctrls->pagesLayout->GetPage2());
DrawInstr *link = ctrls->pagesLayout->GetPage2()->GetLinkAt(x, y);
if (link)
OnClickedLink(currPageNo + 1, link);
}
int EbookController::GetMaxPageCount() const
{
Vec<HtmlPage *> *pagesTmp = pages;
if (incomingPages) {
CrashIf(!FormattingInProgress());
pagesTmp = incomingPages;
}
if (!pagesTmp)
return 0;
return (int)pagesTmp->Count();
}
// show the status text based on current state
void EbookController::UpdateStatus()
{
int pageCount = GetMaxPageCount();
if (FormattingInProgress()) {
ScopedMem<WCHAR> s(str::Format(_TR("Formatting the book... %d pages"), pageCount));
ctrls->status->SetText(s);
ctrls->progress->SetFilled(0.f);
return;
}
ScopedMem<WCHAR> s(str::Format(L"%s %d / %d", _TR("Page:"), currPageNo, pageCount));
ctrls->status->SetText(s);
#if 1
ctrls->progress->SetFilled(PercFromInt(pageCount, currPageNo));
#else
if (GetPages())
ctrls->progress->SetFilled(PercFromInt(pageCount, currPageNo));
else
ctrls->progress->SetFilled(0.f);
#endif
}
void EbookController::GoToPage(int pageNo, bool addNavPoint)
{
// we're still formatting, disable page movement
if (incomingPages) {
//lf("EbookController::GoToPage(%d): skipping because incomingPages != nullptr", pageNo);
return;
}
CrashIf(!pages);
// Hopefully prevent crashes like 55175
if (!pages) {
return;
}
if (addNavPoint)
AddNavPoint();
int pageCount = PageCount();
int n = IsDoublePage() ? 1 : 0;
if (pageNo + n > pageCount)
pageNo = pageCount - n;
// if have only 1 page and showing double, we could go below 1
if (pageNo < 1)
pageNo = 1;
HtmlPage *p = pages->At(pageNo - 1);
currPageNo = pageNo;
currPageReparseIdx = p->reparseIdx;
ctrls->pagesLayout->GetPage1()->SetPage(p);
if (IsDoublePage() && pages->Count() > 1) {
p = pages->At(pageNo);
ctrls->pagesLayout->GetPage2()->SetPage(p);
} else {
ctrls->pagesLayout->GetPage2()->SetPage(nullptr);
}
UpdateStatus();
// update the ToC selection
cb->PageNoChanged(pageNo);
}
bool EbookController::GoToNextPage()
{
int dist = IsDoublePage() ? 2 : 1;
if (currPageNo + dist > PageCount())
return false;
GoToPage(currPageNo + dist, false);
return true;
}
bool EbookController::GoToPrevPage(bool toBottom)
{
int dist = IsDoublePage() ? 2 : 1;
if (currPageNo - dist < 1)
return false;
GoToPage(currPageNo - dist, false);
return true;
}
void EbookController::SetDoc(Doc newDoc, int startReparseIdxArg, DisplayMode displayMode)
{
CrashIf(!newDoc.IsDocLoaded());
currPageReparseIdx = startReparseIdxArg;
if ((size_t)currPageReparseIdx >= newDoc.GetHtmlDataSize())
currPageReparseIdx = 0;
CloseCurrentDocument();
doc = newDoc;
// displayMode could be any value if alternate UI was used, we have to limit it to
// either DM_SINGLE_PAGE or DM_FACING
if (DM_AUTOMATIC == displayMode)
displayMode = gGlobalPrefs->defaultDisplayModeEnum;
SetDisplayMode(displayMode);
TriggerLayout();
UpdateStatus();
}
bool EbookController::IsDoublePage() const
{
return ctrls->pagesLayout->GetPage2()->IsVisible();
}
static RenderedBitmap *RenderFirstDocPageToBitmap(Doc doc, SizeI pageSize, SizeI bmpSize, int border)
{
PoolAllocator textAllocator;
HtmlFormatterArgs *args = CreateFormatterArgsDoc(doc, pageSize.dx - 2 * border, pageSize.dy - 2 * border, &textAllocator);
TextRenderMethod renderMethod = args->textRenderMethod;
HtmlFormatter *formatter = doc.CreateFormatter(args);
HtmlPage *pd = formatter->Next();
delete formatter;
delete args;
args = nullptr;
if (!pd)
return nullptr;
Bitmap pageBmp(pageSize.dx, pageSize.dy, PixelFormat24bppRGB);
Graphics g(&pageBmp);
Rect r(0, 0, pageSize.dx, pageSize.dy);
r.Inflate(1, 1);
SolidBrush br(Color(255, 255, 255));
g.FillRectangle(&br, r);
ITextRender *textRender = CreateTextRender(renderMethod, &g, pageSize.dx, pageSize.dy);
textRender->SetTextBgColor(Color(255,255,255));
DrawHtmlPage(&g, textRender, &pd->instructions, (REAL)border, (REAL)border, false, Color((ARGB)Color::Black));
delete pd;
delete textRender;
Bitmap res(bmpSize.dx, bmpSize.dy, PixelFormat24bppRGB);
Graphics g2(&res);
g2.SetInterpolationMode(InterpolationModeHighQualityBicubic);
g2.DrawImage(&pageBmp, Rect(0, 0, bmpSize.dx, bmpSize.dy),
0, 0, pageSize.dx, pageSize.dy, UnitPixel);
HBITMAP hbmp;
Status ok = res.GetHBITMAP((ARGB)Color::White, &hbmp);
if (ok != Ok)
return nullptr;
return new RenderedBitmap(hbmp, bmpSize);
}
static RenderedBitmap *ThumbFromCoverPage(Doc doc, SizeI size)
{
ImageData *coverImage = doc.GetCoverImage();
if (!coverImage)
return nullptr;
Bitmap *coverBmp = BitmapFromData(coverImage->data, coverImage->len);
if (!coverBmp)
return nullptr;
Bitmap res(size.dx, size.dy, PixelFormat24bppRGB);
float scale = (float)size.dx / (float)coverBmp->GetWidth();
int fromDy = size.dy;
if (scale < 1.f)
fromDy = (int)((float)coverBmp->GetHeight() * scale);
Graphics g(&res);
g.SetInterpolationMode(InterpolationModeHighQualityBicubic);
Status ok = g.DrawImage(coverBmp, Rect(0, 0, size.dx, size.dy),
0, 0, coverBmp->GetWidth(), fromDy, UnitPixel);
if (ok != Ok) {
delete coverBmp;
return nullptr;
}
HBITMAP hbmp;
ok = res.GetHBITMAP((ARGB)Color::White, &hbmp);
delete coverBmp;
if (ok == Ok)
return new RenderedBitmap(hbmp, SizeI(size.dx, size.dy));
return nullptr;
}
void EbookController::CreateThumbnail(SizeI size, const std::function<void(RenderedBitmap*)> &saveThumbnail)
{
// TODO: create thumbnail asynchronously
CrashIf(!doc.IsDocLoaded());
// if there is cover image, we use it to generate thumbnail by scaling
// image width to thumbnail dx, scaling height proportionally and using
// as much of it as fits in thumbnail dy
RenderedBitmap *bmp = ThumbFromCoverPage(doc, size);
if (!bmp) {
// no cover image so generate thumbnail from first page
SizeI pageSize(size.dx * 3, size.dy * 3);
bmp = RenderFirstDocPageToBitmap(doc, pageSize, size, 10);
}
saveThumbnail(bmp);
}
void EbookController::SetDisplayMode(DisplayMode mode, bool keepContinuous)
{
bool newDouble = !IsSingle(mode);
if (IsDoublePage() == newDouble)
return;
// showing/hiding a control will trigger re-layout which will
// trigger book re-formatting
if (newDouble)
ctrls->pagesLayout->GetPage2()->Show();
else
ctrls->pagesLayout->GetPage2()->Hide();
}
void EbookController::ExtractPageAnchors()
{
if (pageAnchorIds || pageAnchorIdxs) {
CrashIf(!pageAnchorIds || !pageAnchorIdxs);
return;
}
pageAnchorIds = new WStrVec();
pageAnchorIdxs = new Vec<int>();
ScopedMem<WCHAR> epubPagePath;
int fb2TitleCount = 0;
size_t len;
const char *data = doc.GetHtmlData(len);
HtmlPullParser parser(data, len);
HtmlToken *tok;
while ((tok = parser.Next()) != nullptr && !tok->IsError()) {
if (!tok->IsStartTag() && !tok->IsEmptyElementEndTag())
continue;
AttrInfo *attr = tok->GetAttrByName("id");
if (!attr && Tag_A == tok->tag && doc.Type() != Doc_Fb2)
attr = tok->GetAttrByName("name");
if (attr) {
ScopedMem<WCHAR> id(str::conv::FromUtf8(attr->val, attr->valLen));
pageAnchorIds->Append(str::Format(L"%s#%s", epubPagePath ? epubPagePath : L"", id.Get()));
pageAnchorIdxs->Append((int)(tok->GetReparsePoint() - parser.Start()));
}
// update EPUB page paths and create an anchor per chapter
if (Tag_Pagebreak == tok->tag &&
(attr = tok->GetAttrByName("page_path")) != nullptr &&
str::StartsWith(attr->val + attr->valLen, "\" page_marker />")) {
CrashIf(doc.Type() != Doc_Epub);
epubPagePath.Set(str::conv::FromUtf8(attr->val, attr->valLen));
pageAnchorIds->Append(str::Dup(epubPagePath));
pageAnchorIdxs->Append((int)(tok->GetReparsePoint() - parser.Start()));
}
// create FB2 title anchors (cf. Fb2Doc::ParseToc)
if (Tag_Title == tok->tag && tok->IsStartTag() && Doc_Fb2 == doc.Type()) {
ScopedMem<WCHAR> id(str::Format(TEXT(FB2_TOC_ENTRY_MARK) L"%d", ++fb2TitleCount));
pageAnchorIds->Append(id.StealData());
pageAnchorIdxs->Append((int)(tok->GetReparsePoint() - parser.Start()));
}
}
}
int EbookController::ResolvePageAnchor(const WCHAR *id)
{
ExtractPageAnchors();
int reparseIdx = -1;
if (Doc_Mobi == doc.Type() && str::Parse(id, L"%d%$", &reparseIdx) &&
0 <= reparseIdx && (size_t)reparseIdx <= doc.GetHtmlDataSize()) {
// Mobi uses filepos (reparseIdx) for in-document links
return reparseIdx;
}
int idx = pageAnchorIds->Find(id);
if (idx != -1)
return pageAnchorIdxs->At(idx);
if (doc.Type() != Doc_Epub || !str::FindChar(id, '#'))
return -1;
ScopedMem<WCHAR> chapterPath(str::DupN(id, str::FindChar(id, '#') - id));
idx = pageAnchorIds->Find(chapterPath);
if (idx != -1)
return pageAnchorIdxs->At(idx);
return -1;
}
class EbookTocCollector : public EbookTocVisitor {
EbookController *ctrl;
EbookTocDest *root;
int idCounter;
public:
explicit EbookTocCollector(EbookController *ctrl) :
ctrl(ctrl), root(nullptr), idCounter(0) { }
virtual void Visit(const WCHAR *name, const WCHAR *url, int level) {
EbookTocDest *item = nullptr;
if (!url)
item = new EbookTocDest(name, 0);
else if (url::IsAbsolute(url))
item = new EbookTocDest(name, url);
else {
int idx = ctrl->ResolvePageAnchor(url);
if (-1 == idx && str::FindChar(url, '%')) {
ScopedMem<WCHAR> decodedUrl(str::Dup(url));
url::DecodeInPlace(decodedUrl);
idx = ctrl->ResolvePageAnchor(decodedUrl);
}
item = new EbookTocDest(name, idx + 1);
}
item->id = ++idCounter;
// find the last child at each level, until finding the parent of the new item
if (!root) {
root = item;
}
else {
DocTocItem *r2 = root;
for (level--; level > 0; level--) {
for (; r2->next; r2 = r2->next);
if (!r2->child)
break;
r2 = r2->child;
}
if (level <= 0)
r2->AddSibling(item);
else
r2->child = item;
}
}
EbookTocDest *GetRoot() { return root; }
};
DocTocItem *EbookController::GetTocTree()
{
EbookTocCollector visitor(this);
doc.ParseToc(&visitor);
EbookTocDest *root = visitor.GetRoot();
if (root)
root->OpenSingleNode();
return root;
}
void EbookController::ScrollToLink(PageDestination *dest)
{
int reparseIdx = dest->GetDestPageNo() - 1;
int pageNo = PageForReparsePoint(pages, reparseIdx);
if (pageNo > 0)
GoToPage(pageNo, true);
else if (0 == pageNo)
GoToLastPage();
}
PageDestination *EbookController::GetNamedDest(const WCHAR *name)
{
int reparseIdx = -1;
if (Doc_Mobi == doc.Type() && str::Parse(name, L"%d%$", &reparseIdx) &&
0 <= reparseIdx && (size_t)reparseIdx <= doc.GetHtmlDataSize()) {
// Mobi uses filepos (reparseIdx) for in-document links
}
else if (!str::FindChar(name, '#')) {
ScopedMem<WCHAR> id(str::Format(L"#%s", name));
reparseIdx = ResolvePageAnchor(id);
}
else {
reparseIdx = ResolvePageAnchor(name);
}
if (reparseIdx < 0)
return nullptr;
CrashIf((size_t)reparseIdx > doc.GetHtmlDataSize());
return new EbookTocDest(nullptr, reparseIdx + 1);
}
int EbookController::CurrentTocPageNo() const
{
return currPageReparseIdx + 1;
}
void EbookController::UpdateDisplayState(DisplayState *ds)
{
if (!ds->filePath || !str::EqI(ds->filePath, doc.GetFilePath()))
str::ReplacePtr(&ds->filePath, doc.GetFilePath());
ds->useDefaultState = !gGlobalPrefs->rememberStatePerDocument;
// don't modify any of the other DisplayState values
// as long as they're not used, so that the same
// DisplayState settings can also be used for EbookEngine;
// we get reasonable defaults from DisplayState's constructor anyway
ds->reparseIdx = currPageReparseIdx;
str::ReplacePtr(&ds->displayMode, prefs::conv::FromDisplayMode(GetDisplayMode()));
}
void EbookController::SetViewPortSize(SizeI size)
{
// relayouting gets the size from the canvas hwnd
ctrls->mainWnd->RequestLayout();
}
LRESULT EbookController::HandleMessage(UINT msg, WPARAM wParam, LPARAM lParam, bool& wasHandled)
{
if (!handleMsgs) {
wasHandled = false;
return 0;
}
return ctrls->mainWnd->evtMgr->OnMessage(msg, wParam, lParam, wasHandled);
}
// TODO: also needs to update for font name/size changes, but it's more complicated
// because requires re-layout
void EbookController::UpdateDocumentColors()
{
SetMainWndBgCol(ctrls);
// changing background will repaint mainWnd control but changing
// of text color will not, so we request uncoditional repaint
// TODO: in PageControl::Paint() use a property for text color, instead of
// taking it directly from prefs
::RequestRepaint(ctrls->mainWnd);
}
void EbookController::RequestRepaint()
{
ctrls->mainWnd->MarkForRepaint();
}
// cf. DisplayModel.cpp
#define MAX_NAV_HISTORY_LEN 50
void EbookController::AddNavPoint()
{
int idx = currPageReparseIdx;
// remove the current and all Forward history entries
if (navHistoryIx < navHistory.Count())
navHistory.RemoveAt(navHistoryIx, navHistory.Count() - navHistoryIx);
// don't add another entry for the exact same position
if (navHistoryIx > 0 && idx == navHistory.At(navHistoryIx - 1))
return;
// make sure that the history doesn't grow overly large
if (navHistoryIx >= MAX_NAV_HISTORY_LEN) {
CrashIf(navHistoryIx > MAX_NAV_HISTORY_LEN);
navHistory.RemoveAt(0, navHistoryIx - MAX_NAV_HISTORY_LEN + 1);
navHistoryIx = MAX_NAV_HISTORY_LEN - 1;
}
// add a new Back history entry
navHistory.Append(idx);
navHistoryIx++;
}
bool EbookController::CanNavigate(int dir) const
{
CrashIf(navHistoryIx > navHistory.Count());
if (dir < 0)
return navHistoryIx >= (size_t)-dir;
return navHistoryIx + dir < navHistory.Count();
}
void EbookController::Navigate(int dir)
{
if (!CanNavigate(dir))
return;
// update the current history entry
int idx = currPageReparseIdx;
if (navHistoryIx < navHistory.Count())
navHistory.At(navHistoryIx) = idx;
else
navHistory.Append(idx);
navHistoryIx += dir;
idx = navHistory.At(navHistoryIx);
int pageNo = PageForReparsePoint(pages, idx);
if (0 == pageNo)
pageNo = GetMaxPageCount();
if (pageNo > 0)
GoToPage(pageNo, false);
}
void EbookController::CopyNavHistory(EbookController& orig)
{
navHistory = orig.navHistory;
navHistoryIx = orig.navHistoryIx;
}
EbookController *EbookController::Create(HWND hwnd, ControllerCallback *cb, FrameRateWnd *frameRateWnd)
{
EbookControls *ctrls = CreateEbookControls(hwnd, frameRateWnd);
if (!ctrls)
return nullptr;
return new EbookController(ctrls, cb);
}