-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDjVuView.cpp
7111 lines (5928 loc) · 183 KB
/
DjVuView.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
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// WinDjView
// Copyright (C) 2004-2015 Andrew Zhezherun
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// http://www.gnu.org/copyleft/gpl.html
#include "stdafx.h"
#include "WinDjView.h"
#include "Global.h"
#include "DjVuDoc.h"
#include "DjVuView.h"
#include "MainFrm.h"
#include "MDIChild.h"
#include "PrintDlg.h"
#include "Drawing.h"
#include "ProgressDlg.h"
#include "ZoomDlg.h"
#include "GotoPageDlg.h"
#include "FindDlg.h"
#include "BookmarksView.h"
#include "SearchResultsView.h"
#include "FullscreenWnd.h"
#include "ThumbnailsView.h"
#include "PageIndexWnd.h"
#include "MagnifyWnd.h"
#include "AnnotationDlg.h"
#include "BookmarkDlg.h"
#include "MyFileDialog.h"
#include "NavPane.h"
#include "MyGdiPlus.h"
#include "RenderThread.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// Segment
struct Segment
{
Segment(double x1_, double y1_, double x2_, double y2_)
: x1(x1_), y1(y1_), x2(x2_), y2(y2_)
{
a = y2 - y1;
b = x1 - x2;
c = -a*x1 - b*y1;
double mult = pow(a*a + b*b, 0.5);
if (mult > 1e-9)
{
a /= mult;
b /= mult;
c /= mult;
}
else
{
a = b = 0;
c = (c > 0 ? 1 : c < 0 ? -1 : 0);
}
}
bool Contains(double x, double y) const
{
if (fabs(a*x + b*y + c) > 1e-6)
return false;
return (x - x1)*(x - x2) <= 1e-9 && (y - y1)*(y - y2) <= 1e-9;
}
bool Intersects(const Segment& rhs) const
{
double x, y;
if (!IntersectionPoint(rhs, x, y))
return false;
return (x - x1)*(x - x2) <= 1e-9 && (y - y1)*(y - y2) <= 1e-9
&& (x - rhs.x1)*(x - rhs.x2) <= 1e-9 && (y - rhs.y1)*(y - rhs.y2) <= 1e-9;
}
bool IntersectsDisc(double x, double y, double r) const
{
if (pow(pow(x - x1, 2) + pow(y - y1, 2), 0.5) <= r + 1e-6
|| pow(pow(x - x2, 2) + pow(y - y2, 2), 0.5) <= r + 1e-6)
return true;
double xx, yy;
Segment perp(x, y, x + (y1 - y2), y + (x2 - x1));
if (!IntersectionPoint(perp, xx, yy))
return false;
return (xx - x1)*(xx - x2) <= 1e-9 && (yy - y1)*(yy - y2) <= 1e-9
&& pow(pow(x - xx, 2) + pow(y - yy, 2), 0.5) <= r + 1e-6;
}
private:
bool IntersectionPoint(const Segment& rhs, double& x, double& y) const
{
double d = a*rhs.b - b*rhs.a;
if (fabs(d) < 1e-9)
return false;
x = (b*rhs.c - c*rhs.b) / d;
y = (c*rhs.a - a*rhs.c) / d;
return true;
}
double x1, y1, x2, y2, a, b, c;
};
// CDjVuView
HCURSOR CDjVuView::hCursorHand = NULL;
HCURSOR CDjVuView::hCursorDrag = NULL;
HCURSOR CDjVuView::hCursorLink = NULL;
HCURSOR CDjVuView::hCursorText = NULL;
HCURSOR CDjVuView::hCursorMagnify = NULL;
HCURSOR CDjVuView::hCursorCross = NULL;
HCURSOR CDjVuView::hCursorZoomRect = NULL;
const int c_nDefaultMargin = 4;
const int c_nDefaultShadowMargin = 4; // 3
const int c_nDefaultPageGap = 4;
const int c_nDefaultFacingGap = 4;
const int c_nDefaultPageBorder = 0; // 1
const int c_nDefaultPageShadow = 0; // 3
const int c_nFullscreenMargin = 0;
const int c_nFullscreenShadowMargin = 0;
const int c_nFullscreenPageGap = 4;
const int c_nFullscreenFacingGap = 4;
const int c_nFullscreenPageBorder = 0;
const int c_nFullscreenPageShadow = 0;
const int c_nHourglassWidth = 15;
const int c_nHourglassHeight = 24;
const int s_nCursorHideDelay = 3500;
// IMPORTANT: There seems to be a problem with the way TranslateAccelerator
// interacts with MFC. When an accelerator corresponds to an item in a submenu
// (like "View"->"Go To"->"First Page", the OnInitMenuPopup function is called
// with the "View" menu as an argument, not with the "Go To" menu. As a result,
// the update handler for a menu item in the "Go To" submenu is not called,
// and if this item was disabled when the menu was last displayed, it will stay
// disabled, even if it is enabled on the toolbar. TranslateAccelerator doesn't
// send a WM_COMMAND message if a corresponding menu item is disabled, so the
// accelerator doesn't work as expected.
//
// To fix this, for every item on a submenu with an accelerator, a second
// command ID is introduced, and the accelerator table specifies this second ID
// (so that it is not attached to a menu). Note that there is no update handler
// for this second ID, so the command handler should take this into account.
//
// This applies to the following items: "First Page", "Previous Page",
// "Next Page", "Last Page", "Previous View" and "Next View"
// (because they might become disabled).
IMPLEMENT_DYNCREATE(CDjVuView, CMyScrollView)
BEGIN_MESSAGE_MAP(CDjVuView, CMyScrollView)
ON_COMMAND(ID_FILE_PRINT, OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
ON_WM_ERASEBKGND()
ON_COMMAND(ID_VIEW_NEXTPAGE, OnViewNextpage)
ON_COMMAND(ID_VIEW_PREVIOUSPAGE, OnViewPreviouspage)
ON_COMMAND(ID_VIEW_NEXTPAGE_SHORTCUT, OnViewNextpage)
ON_COMMAND(ID_VIEW_PREVIOUSPAGE_SHORTCUT, OnViewPreviouspage)
ON_UPDATE_COMMAND_UI(ID_VIEW_NEXTPAGE, OnUpdateViewNextpage)
ON_UPDATE_COMMAND_UI(ID_VIEW_PREVIOUSPAGE, OnUpdateViewPreviouspage)
ON_WM_WINDOWPOSCHANGED()
ON_WM_SIZE()
ON_WM_KEYDOWN()
ON_COMMAND_RANGE(ID_ZOOM_50, ID_ZOOM_CUSTOM, OnViewZoom)
ON_UPDATE_COMMAND_UI_RANGE(ID_ZOOM_50, ID_ZOOM_CUSTOM, OnUpdateViewZoom)
ON_COMMAND_RANGE(ID_ROTATE_LEFT, ID_ROTATE_180, OnViewRotate)
ON_COMMAND(ID_VIEW_FIRSTPAGE, OnViewFirstpage)
ON_COMMAND(ID_VIEW_LASTPAGE, OnViewLastpage)
ON_COMMAND(ID_VIEW_FIRSTPAGE_SHORTCUT, OnViewFirstpage)
ON_COMMAND(ID_VIEW_LASTPAGE_SHORTCUT, OnViewLastpage)
ON_UPDATE_COMMAND_UI(ID_VIEW_LASTPAGE, OnUpdateViewNextpage)
ON_UPDATE_COMMAND_UI(ID_VIEW_FIRSTPAGE, OnUpdateViewPreviouspage)
ON_WM_SETFOCUS()
ON_WM_KILLFOCUS()
ON_WM_CANCELMODE()
ON_WM_SETCURSOR()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_RBUTTONDOWN()
ON_WM_RBUTTONUP()
ON_COMMAND_RANGE(ID_LAYOUT_CONTINUOUS, ID_LAYOUT_FACING, OnViewLayout)
ON_UPDATE_COMMAND_UI_RANGE(ID_LAYOUT_CONTINUOUS, ID_LAYOUT_FACING, OnUpdateViewLayout)
ON_MESSAGE(WM_PAGE_RENDERED, OnPageRendered)
ON_MESSAGE(WM_PAGE_DECODED, OnPageDecoded)
ON_WM_DESTROY()
ON_WM_MOUSEWHEEL()
ON_COMMAND(ID_FIND_STRING, OnFindString)
ON_COMMAND(ID_FIND_ALL, OnFindAll)
ON_COMMAND(ID_ZOOM_IN, OnViewZoomIn)
ON_COMMAND(ID_ZOOM_OUT, OnViewZoomOut)
ON_UPDATE_COMMAND_UI(ID_ZOOM_IN, OnUpdateViewZoomIn)
ON_UPDATE_COMMAND_UI(ID_ZOOM_OUT, OnUpdateViewZoomOut)
ON_COMMAND(ID_VIEW_FULLSCREEN, OnViewFullscreen)
ON_WM_MOUSEACTIVATE()
ON_WM_LBUTTONDBLCLK()
ON_COMMAND(ID_PAGE_INFORMATION, OnPageInformation)
ON_COMMAND_RANGE(ID_EXPORT_PAGE, ID_EXPORT_SELECTION, OnExportPage)
ON_UPDATE_COMMAND_UI(ID_EXPORT_SELECTION, OnUpdateExportSelection)
ON_COMMAND_RANGE(ID_MODE_DRAG, ID_MODE_ZOOM_RECT, OnChangeMode)
ON_UPDATE_COMMAND_UI_RANGE(ID_MODE_DRAG, ID_MODE_ZOOM_RECT, OnUpdateMode)
ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
ON_COMMAND_RANGE(ID_DISPLAY_COLOR, ID_DISPLAY_FOREGROUND, OnViewDisplay)
ON_UPDATE_COMMAND_UI_RANGE(ID_DISPLAY_COLOR, ID_DISPLAY_FOREGROUND, OnUpdateViewDisplay)
ON_COMMAND(ID_VIEW_GOTO_PAGE, OnViewGotoPage)
ON_WM_TIMER()
ON_COMMAND(ID_LAYOUT_FIRSTPAGE_ALONE, OnFirstPageAlone)
ON_COMMAND(ID_LAYOUT_RTL_ORDER, OnRightToLeftOrder)
ON_UPDATE_COMMAND_UI(ID_LAYOUT_FIRSTPAGE_ALONE, OnUpdateFirstPageAlone)
ON_UPDATE_COMMAND_UI(ID_LAYOUT_RTL_ORDER, OnUpdateRightToLeftOrder)
ON_MESSAGE_VOID(WM_MOUSELEAVE, OnMouseLeave)
ON_COMMAND_RANGE(ID_HIGHLIGHT_SELECTION, ID_HIGHLIGHT_TEXT, OnHighlight)
ON_UPDATE_COMMAND_UI_RANGE(ID_HIGHLIGHT_SELECTION, ID_HIGHLIGHT_TEXT, OnUpdateHighlight)
ON_COMMAND(ID_ANNOTATION_DELETE, OnDeleteAnnotation)
ON_COMMAND(ID_ANNOTATION_EDIT, OnEditAnnotation)
ON_COMMAND(ID_BOOKMARK_ADD, OnAddBookmark)
ON_COMMAND(ID_ZOOM_TO_SELECTION, OnZoomToSelection)
ON_COMMAND_RANGE(ID_NEXT_PANE, ID_PREV_PANE, OnSwitchFocus)
ON_UPDATE_COMMAND_UI_RANGE(ID_NEXT_PANE, ID_PREV_PANE, OnUpdateSwitchFocus)
ON_WM_SHOWWINDOW()
ON_MESSAGE(WM_SHOWPARENT, OnShowParent)
ON_COMMAND(ID_VIEW_BACK, OnViewBack)
ON_COMMAND(ID_VIEW_FORWARD, OnViewForward)
ON_COMMAND(ID_VIEW_BACK_SHORTCUT, OnViewBack)
ON_COMMAND(ID_VIEW_FORWARD_SHORTCUT, OnViewForward)
ON_UPDATE_COMMAND_UI(ID_VIEW_BACK, OnUpdateViewBack)
ON_UPDATE_COMMAND_UI(ID_VIEW_FORWARD, OnUpdateViewForward)
END_MESSAGE_MAP()
// CDjVuView construction/destruction
CDjVuView::CDjVuView()
: m_nPage(-1), m_nPageCount(0), m_nZoomType(ZoomPercent), m_fZoom(100.0),
m_nLayout(SinglePage), m_nRotate(0), m_bDragging(false),
m_bDraggingRight(false), m_pSource(NULL), m_pRenderThread(NULL),
m_bInsideUpdateLayout(false), m_bInsideMouseMove(false), m_bClick(false),
m_nPendingPage(-1), m_nClickedPage(-1), m_nMode(Drag),
m_bHasSelection(false), m_nDisplayMode(Color), m_bShiftDown(false),
m_bNeedUpdate(false), m_bCursorHidden(false), m_bDraggingPage(false),
m_bDraggingText(false), m_bFirstPageAlone(false), m_bRightToLeft(false),
m_bDraggingMagnify(false), m_bControlDown(false), m_nType(Normal),
m_bHoverIsCustom(false), m_bDraggingRect(false), m_nSelectionPage(-1),
m_pHoverAnno(NULL), m_pClickedAnno(NULL), m_bDraggingLink(false),
m_bPopupMenu(false), m_bClickedCustom(false), m_bUpdateBitmaps(false)
{
m_historyPoint = m_history.end();
m_nMargin = c_nDefaultMargin;
m_nShadowMargin = c_nDefaultShadowMargin;
m_nPageGap = c_nDefaultPageGap;
m_nFacingGap = c_nDefaultFacingGap;
m_nPageBorder = c_nDefaultPageBorder;
m_nPageShadow = c_nDefaultPageShadow;
CreateSystemIconFont(m_sampleFont);
// Note: screen DPI is reported by Windows through the screen DC.
// However, the default value of 96 causes the common zoom levels
// of 50%, 100%, 150% look bad for common 300dpi and 600dpi documents,
// because the scaling factor will be non-integer. However, by
// explicitely setting dpi value to 100, we can cause common zoom
// levels to require integer subsampling, and the resulting images
// will look better and will be rendered faster.
//
// CScreenDC dcScreen;
// m_nScreenDPI = dcScreen.GetDeviceCaps(LOGPIXELSY);
m_nScreenDPI = 100;
}
CDjVuView::~CDjVuView()
{
for (map<int, HFONT>::iterator it = m_fonts.begin(); it != m_fonts.end(); ++it)
::DeleteObject(it->second);
DeleteBitmaps();
m_dataLock.Lock();
for (set<CDIB*>::iterator it = m_bitmaps.begin(); it != m_bitmaps.end(); ++it)
delete *it;
m_bitmaps.clear();
m_dataLock.Unlock();
if (m_pSource != NULL)
m_pSource->Release();
}
// CDjVuView drawing
void CDjVuView::OnDraw(CDC* pDC)
{
if (pDC->IsPrinting())
{
// TODO: Print preview
Page& page = m_pages[m_nPage];
if (page.pBitmap != NULL)
{
int nOffsetX = pDC->GetDeviceCaps(PHYSICALOFFSETX);
int nOffsetY = pDC->GetDeviceCaps(PHYSICALOFFSETY);
pDC->SetViewportOrg(-nOffsetX, -nOffsetY);
CSize szPage = page.GetSize(m_nRotate);
page.pBitmap->Draw(pDC, CPoint(0, 0), szPage);
}
return;
}
CRect rcViewport(CPoint(0, 0), GetViewportSize());
CRect rcClip;
pDC->GetClipBox(rcClip);
rcClip.IntersectRect(CRect(rcClip), rcViewport);
m_offscreenDC.Create(pDC, rcClip.Size());
m_offscreenDC.SetViewportOrg(-rcClip.TopLeft());
m_offscreenDC.IntersectClipRect(rcClip);
CPoint ptScroll = GetScrollPosition();
rcClip.OffsetRect(ptScroll);
CRect rcIntersect;
for (int nPage = 0; nPage < m_nPageCount; ++nPage)
{
Page& page = m_pages[nPage];
if (m_nLayout == SinglePage || m_nLayout == Facing)
{
if (nPage != m_nPage && !(m_nLayout == Facing && HasFacingPage(m_nPage) && nPage == m_nPage + 1))
continue;
}
else if (m_nLayout == Continuous || m_nLayout == ContinuousFacing)
{
if (!rcIntersect.IntersectRect(m_pages[nPage].rcDisplay, rcClip))
continue;
}
DrawPage(&m_offscreenDC, nPage);
if (page.pBitmap != NULL && page.pBitmap->m_hObject != NULL)
{
int nSaveDC = pDC->SaveDC();
pDC->IntersectClipRect(CRect(page.ptOffset, page.szBitmap) - ptScroll);
// Draw annotations
for (list<Annotation>::iterator lit = page.info.anno.begin(); lit != page.info.anno.end(); ++lit)
DrawAnnotation(&m_offscreenDC, *lit, nPage, &(*lit) == m_pHoverAnno);
// Draw custom annotations
map<int, PageSettings>::iterator it = m_pSource->GetSettings()->pageSettings.find(nPage);
if (it != m_pSource->GetSettings()->pageSettings.end())
{
PageSettings& pageSettings = (*it).second;
for (list<Annotation>::iterator lit = pageSettings.anno.begin(); lit != pageSettings.anno.end(); ++lit)
DrawAnnotation(&m_offscreenDC, *lit, nPage, &(*lit) == m_pHoverAnno);
}
// Draw selection
for (GPosition pos = page.selection; pos; ++pos)
{
GRect rect = page.selection[pos]->rect;
CRect rcText = TranslatePageRect(nPage, rect);
m_offscreenDC.InvertRect(rcText - ptScroll);
}
// Draw transparent text on top of the image to assist dictionaries and
// other programs that hook to TextOut to find text under mouse pointer.
DrawTransparentText(&m_offscreenDC, nPage);
pDC->RestoreDC(nSaveDC);
}
}
// Draw rectangle selection
if (m_nSelectionPage != -1 && m_rcSelection.width() > 1 && m_rcSelection.height() > 1)
{
// All selection corner points are inside m_rcSelection, so it
// needs to be adjusted so that the drawn selection precisely
// follows the mouse on screen.
GRect rcDeflated(m_rcSelection.xmin, m_rcSelection.ymin + 1,
m_rcSelection.width() - 1, m_rcSelection.height() - 1);
CRect rcSel = TranslatePageRect(m_nSelectionPage, rcDeflated);
rcSel.InflateRect(0, 0, 1, 1);
InvertFrame(&m_offscreenDC, rcSel - ptScroll);
}
rcClip.OffsetRect(-ptScroll);
pDC->BitBlt(rcClip.left, rcClip.top, rcClip.Width(), rcClip.Height(),
&m_offscreenDC, rcClip.left, rcClip.top, SRCCOPY);
m_offscreenDC.Release();
}
void CDjVuView::DrawAnnotation(CDC* pDC, const Annotation& anno, int nPage, bool bActive)
{
Page& page = m_pages[nPage];
CPoint ptScroll = GetScrollPosition();
CRect rcBounds = TranslatePageRect(nPage, anno.rectBounds) - ptScroll;
bool bShowAll = m_bShiftDown && (GetFocus() == this || m_nType == Magnify);
bool bShowBounds = false, bAdjustBorder = false;
// Border
if (bActive || bShowAll || !anno.bHideInactiveBorder || anno.bIsLine)
{
bShowBounds = true;
if (!anno.points.empty())
{
vector<POINT> points(anno.points.size() + 1);
for (size_t i = 0; i < anno.points.size(); ++i)
{
GRect rect(anno.points[i].first, anno.points[i].second, 1, 1);
points[i] = CPoint(TranslatePageRect(nPage, rect).TopLeft() - ptScroll);
}
if (anno.bIsLine)
{
if (anno.points.size() == 2)
{
CPen penSolid(PS_SOLID, anno.nLineWidth, anno.crForeground);
CPen* pOldPen = pDC->SelectObject(&penSolid);
pDC->MoveTo(points[0]);
pDC->LineTo(points[1]);
CSize szPage = page.GetSize(m_nRotate);
if (CPoint(points[0]) != points[1] && anno.bHasArrow && szPage.cy > 0)
{
CScreenDC dcScreen;
double fZoom = 1.0*page.szBitmap.cy*page.info.nDPI/(szPage.cy*m_nScreenDPI);
double fArrowLength = 10*fZoom;
double fLength = pow(pow(1.0*points[1].x - points[0].x, 2.0)
+ pow(1.0*points[1].y - points[0].y, 2.0), 0.5);
double fBaseX = points[1].x + (points[0].x - points[1].x)*fArrowLength/fLength;
double fBaseY = points[1].y + (points[0].y - points[1].y)*fArrowLength/fLength;
double fTopX = fBaseX + (points[0].y - points[1].y)*0.5*fArrowLength/fLength;
double fTopY = fBaseY + (points[1].x - points[0].x)*0.5*fArrowLength/fLength;
double fBottomX = fBaseX + (points[1].y - points[0].y)*0.5*fArrowLength/fLength;
double fBottomY = fBaseY + (points[0].x - points[1].x)*0.5*fArrowLength/fLength;
pDC->MoveTo(points[1]);
pDC->LineTo(static_cast<int>(fTopX + 0.5), static_cast<int>(fTopY + 0.5));
pDC->MoveTo(points[1]);
pDC->LineTo(static_cast<int>(fBottomX + 0.5), static_cast<int>(fBottomY + 0.5));
}
pDC->SelectObject(pOldPen);
}
}
else if (anno.points.size() >= 3)
{
points.back() = points.front();
if (anno.nBorderType == Annotation::BorderXOR
|| bShowAll && anno.nBorderType == Annotation::BorderNone)
InvertPolyFrame(pDC, &points[0], (int)points.size());
else if (anno.nBorderType == Annotation::BorderSolid)
FramePoly(pDC, &points[0], (int)points.size(), anno.crBorder);
}
}
else if (anno.bOvalShape)
{
if (anno.nBorderType == Annotation::BorderXOR
|| bShowAll && anno.nBorderType == Annotation::BorderNone)
InvertOvalFrame(pDC, rcBounds);
else if (anno.nBorderType == Annotation::BorderSolid)
FrameOval(pDC, rcBounds, anno.crBorder);
}
else
{
if (anno.nBorderType == Annotation::BorderXOR
|| bShowAll && anno.nBorderType == Annotation::BorderNone)
{
InvertFrame(pDC, rcBounds);
bAdjustBorder = true;
}
else if (anno.nBorderType == Annotation::BorderSolid)
{
FrameRect(pDC, rcBounds, anno.crBorder);
bAdjustBorder = true;
}
}
}
// Fill
if (!anno.bOvalShape && (bActive || !anno.bHideInactiveFill))
{
CRect rcInside = rcBounds;
if (bAdjustBorder)
rcInside.DeflateRect(1, 1);
for (size_t i = 0; i < anno.rects.size(); ++i)
{
CPoint ptScroll = GetScrollPosition();
CRect rect = TranslatePageRect(nPage, anno.rects[i]) - ptScroll;
if (!rect.IntersectRect(CRect(rect), rcInside))
continue;
// Fill
if (anno.nFillType == Annotation::FillXOR)
pDC->InvertRect(rect);
else if (anno.nFillType == Annotation::FillSolid)
HighlightRect(pDC, rect, anno.crFill, anno.fTransparency);
}
}
if (bShowBounds)
{
if (anno.nBorderType == Annotation::BorderShadowIn
|| anno.nBorderType == Annotation::BorderShadowOut)
{
int nBorderWidth = min(anno.nBorderWidth,
min(rcBounds.Width() / 2, rcBounds.Height() / 2));
bool bShadowIn = (anno.nBorderType == Annotation::BorderShadowIn);
COLORREF crWhite = RGB(255, 255, 255);
COLORREF crBlack = RGB(0, 0, 0);
CPoint points1[] = {
CPoint(rcBounds.left, rcBounds.top),
CPoint(rcBounds.right - 1, rcBounds.top),
CPoint(rcBounds.right - nBorderWidth, rcBounds.top + nBorderWidth - 1),
CPoint(rcBounds.left + nBorderWidth - 1, rcBounds.top + nBorderWidth - 1),
CPoint(rcBounds.left + nBorderWidth - 1, rcBounds.bottom - nBorderWidth),
CPoint(rcBounds.left, rcBounds.bottom - 1) };
HighlightPolygon(pDC, points1, 6, bShadowIn ? crBlack : crWhite, bShadowIn ? 0.85 : 0.65);
CPoint points2[] = {
CPoint(rcBounds.right - 1, rcBounds.bottom - 1),
CPoint(rcBounds.left + 1, rcBounds.bottom - 1),
CPoint(rcBounds.left + nBorderWidth, rcBounds.bottom - nBorderWidth),
CPoint(rcBounds.right - nBorderWidth, rcBounds.bottom - nBorderWidth),
CPoint(rcBounds.right - nBorderWidth, rcBounds.top + nBorderWidth),
CPoint(rcBounds.right - 1, rcBounds.top + 1) };
HighlightPolygon(pDC, points2, 6, bShadowIn ? crWhite : crBlack, bShadowIn ? 0.65 : 0.85);
}
else if (anno.nBorderType == Annotation::BorderEtchedIn
|| anno.nBorderType == Annotation::BorderEtchedOut)
{
int nBorderWidth = min(anno.nBorderWidth,
min(rcBounds.Width() / 2, rcBounds.Height() / 2) - 1);
bool bEtchedIn = (anno.nBorderType == Annotation::BorderEtchedIn);
COLORREF crWhite = RGB(255, 255, 255);
COLORREF crBlack = RGB(0, 0, 0);
CRect rcOuterLeft(rcBounds.left, rcBounds.top, rcBounds.left + 1, rcBounds.bottom);
CRect rcOuterTop(rcBounds.left + 1, rcBounds.top, rcBounds.right, rcBounds.top + 1);
CRect rcOuterRight(rcBounds.right - 1, rcBounds.top + 1, rcBounds.right, rcBounds.bottom);
CRect rcOuterBottom(rcBounds.left + 1, rcBounds.bottom - 1, rcBounds.right - 1, rcBounds.bottom);
CRect rcInner = rcBounds;
rcInner.DeflateRect(nBorderWidth, nBorderWidth);
CRect rcInnerLeft(rcInner.left, rcInner.top, rcInner.left + 1, rcInner.bottom);
CRect rcInnerTop(rcInner.left + 1, rcInner.top, rcInner.right, rcInner.top + 1);
CRect rcInnerRight(rcInner.right - 1, rcInner.top + 1, rcInner.right, rcInner.bottom);
CRect rcInnerBottom(rcInner.left + 1, rcInner.bottom - 1, rcInner.right - 1, rcInner.bottom);
COLORREF crShadow = bEtchedIn ? crBlack : crWhite;
COLORREF crHighlight = bEtchedIn ? crWhite : crBlack;
double fTranspShadow = bEtchedIn ? 0.85 : 0.65;
double fTranspHighlight = bEtchedIn ? 0.65 : 0.85;
HighlightRect(pDC, rcOuterLeft, crShadow, fTranspShadow);
HighlightRect(pDC, rcOuterTop, crShadow, fTranspShadow);
HighlightRect(pDC, rcOuterRight, crHighlight, fTranspHighlight);
HighlightRect(pDC, rcOuterBottom, crHighlight, fTranspHighlight);
HighlightRect(pDC, rcInnerLeft, crHighlight, fTranspHighlight);
HighlightRect(pDC, rcInnerTop, crHighlight, fTranspHighlight);
HighlightRect(pDC, rcInnerRight, crShadow, fTranspShadow);
HighlightRect(pDC, rcInnerBottom, crShadow, fTranspShadow);
}
}
if (anno.bAlwaysShowComment)
{
CRect rcText = rcBounds;
rcText.DeflateRect(5, 3);
pDC->SetTextColor(anno.crForeground);
pDC->SetBkMode(TRANSPARENT);
CSize szPage = page.GetSize(m_nRotate);
if (szPage.cy > 0)
{
CFont curFont;
double fZoom = 1.0*page.szBitmap.cy*page.info.nDPI/(szPage.cy*m_nScreenDPI);
LOGFONT lf;
m_sampleFont.GetLogFont(&lf);
int nFontSize = static_cast<int>(lf.lfHeight*fZoom + (lf.lfHeight > 0 ? 0.5 : -0.5));
// Find or create the font with this size.
map<int, HFONT>::iterator it = m_fonts.find(nFontSize);
if (it == m_fonts.end())
{
lf.lfHeight = nFontSize;
HFONT hFont = ::CreateFontIndirect(&lf);
it = m_fonts.insert(make_pair(nFontSize, hFont)).first;
}
HGDIOBJ hOldFont = ::SelectObject(pDC->m_hDC, it->second);
pDC->SetTextCharacterExtra(0);
pDC->DrawText(MakeCString(anno.strComment), rcText,
DT_NOPREFIX | DT_LEFT | DT_TOP | DT_WORDBREAK | DT_END_ELLIPSIS);
::SelectObject(pDC->m_hDC, hOldFont);
}
}
}
void CDjVuView::DrawPage(CDC* pDC, int nPage)
{
Page& page = m_pages[nPage];
COLORREF clrFrame = ::GetSysColor(COLOR_WINDOWFRAME);
COLORREF clrBtnshadow = ::GetSysColor(COLOR_BTNSHADOW);
COLORREF clrBtnface = ::GetSysColor(COLOR_BTNFACE);
COLORREF clrWindow = ::GetSysColor(COLOR_WINDOW);
if (m_displaySettings.bInvertColors)
clrWindow = InvertColor(clrWindow);
COLORREF clrShadow = ChangeBrightness(clrBtnshadow, 0.75);
//COLORREF clrBackground = ChangeBrightness(clrBtnshadow, 1.2);
COLORREF clrBackground = RGB(64, 64, 64);
if (m_nType == Fullscreen || m_nType == Magnify && GetMainFrame()->IsFullscreenMode())
clrBackground = RGB(0, 0, 0);
CPoint ptScroll = GetScrollPosition();
CRect rcClip;
pDC->GetClipBox(rcClip);
rcClip.OffsetRect(ptScroll);
rcClip.IntersectRect(CRect(rcClip), page.rcDisplay);
if (rcClip.IsRectEmpty())
return;
if (page.szBitmap.cx <= 0 || page.szBitmap.cy <= 0)
{
pDC->FillSolidRect(rcClip - ptScroll, clrBackground);
return;
}
if (page.pBitmap == NULL || page.pBitmap->m_hObject == NULL)
{
// Draw white rect
CRect rcPage(page.ptOffset, page.szBitmap);
pDC->FillSolidRect(rcPage - ptScroll, clrWindow);
if (!page.bBitmapRendered &&
page.szBitmap.cx >= c_nHourglassWidth && page.szBitmap.cy >= c_nHourglassHeight)
{
// Draw hourglass
CPoint pt((page.szBitmap.cx - c_nHourglassWidth) / 2, (page.szBitmap.cy - c_nHourglassHeight) / 2);
m_hourglass.Draw(pDC, m_displaySettings.bInvertColors ? 1 : 0,
CPoint(pt + page.ptOffset - ptScroll), ILD_NORMAL);
}
}
else if (page.pBitmap->GetSize() == page.szBitmap)
{
// Draw offscreen bitmap
CPoint ptPartOffset(max(rcClip.left - page.ptOffset.x, 0),
max(rcClip.top - page.ptOffset.y, 0));
CSize szPageClip = page.szBitmap - ptPartOffset;
CSize szPart(min(rcClip.Width(), szPageClip.cx), min(rcClip.Height(), szPageClip.cy));
if (szPart.cx > 0 && szPart.cy > 0)
{
CRect rcPart(ptPartOffset, szPart);
page.pBitmap->DrawDC(pDC,
page.ptOffset + ptPartOffset - ptScroll, rcPart);
}
}
else
{
// Offscreen bitmap has yet wrong size, stretch it
page.pBitmap->Draw(pDC, page.ptOffset - ptScroll, page.szBitmap);
}
CRect rcBorder(page.ptOffset, page.szBitmap);
int nBorder = m_nPageBorder;
while (nBorder-- > 0)
{
// Draw border
rcBorder.InflateRect(1, 1);
FrameRect(pDC, rcBorder - ptScroll, clrFrame);
}
if (m_nPageShadow > 0)
{
// Draw shadow
CRect rcWhiteBar(CPoint(rcBorder.left, rcBorder.bottom),
CSize(m_nPageShadow + 1, m_nPageShadow));
pDC->FillSolidRect(rcWhiteBar - ptScroll, clrBackground);
rcWhiteBar = CRect(CPoint(rcBorder.right, rcBorder.top),
CSize(m_nPageShadow, m_nPageShadow + 1));
pDC->FillSolidRect(rcWhiteBar - ptScroll, clrBackground);
CRect rcShadow(CPoint(rcBorder.left + m_nPageShadow + 1, rcBorder.bottom),
CSize(rcBorder.Width() - 1, m_nPageShadow));
pDC->FillSolidRect(rcShadow - ptScroll, clrShadow);
rcShadow = CRect(CPoint(rcBorder.right, rcBorder.top + m_nPageShadow + 1),
CSize(m_nPageShadow, rcBorder.Height() - 1));
pDC->FillSolidRect(rcShadow - ptScroll, clrShadow);
rcBorder.InflateRect(0, 0, m_nPageShadow, m_nPageShadow);
}
// Fill everything else with backgroundColor
int nSaveDC = pDC->SaveDC();
pDC->IntersectClipRect(page.rcDisplay - ptScroll);
pDC->ExcludeClipRect(rcBorder - ptScroll);
pDC->FillSolidRect(rcClip - ptScroll, clrBackground);
pDC->RestoreDC(nSaveDC);
}
void GetZones(DjVuTXT::Zone* pZone, int nZoneType, const GRect* rect, DjVuSelection& zones)
{
GRect temp;
for (GPosition pos = pZone->children; pos; ++pos)
{
DjVuTXT::Zone* pChild = &pZone->children[pos];
if (rect != NULL && !temp.intersect(pChild->rect, *rect))
continue;
if (pChild->ztype == nZoneType)
zones.append(pChild);
else if (pChild->ztype < nZoneType)
GetZones(pChild, nZoneType, rect, zones);
}
}
void CDjVuView::DrawTransparentText(CDC* pDC, int nPage)
{
Page& page = m_pages[nPage];
if (page.info.pText == NULL)
return;
CPoint ptScroll = GetScrollPosition();
CRect rcClip;
pDC->GetClipBox(rcClip);
rcClip.OffsetRect(ptScroll);
rcClip.IntersectRect(CRect(rcClip), page.rcDisplay);
// Hide the displayed text by using raster operations.
pDC->SetROP2(R2_NOP);
pDC->SetBkMode(TRANSPARENT);
CPen penNull(PS_NULL, 0, RGB(0, 0, 0));
CPen* pOldPen = pDC->SelectObject(&penNull);
pDC->BeginPath();
CPoint ptTopLeft = ScreenToDjVu(nPage, rcClip.TopLeft() - page.ptOffset, true);
CPoint ptBottomRight = ScreenToDjVu(nPage, rcClip.BottomRight() - page.ptOffset, true);
GRect rect(min(ptTopLeft.x, ptBottomRight.x), min(ptTopLeft.y, ptBottomRight.y),
abs(ptTopLeft.x - ptBottomRight.x), abs(ptTopLeft.y - ptBottomRight.y));
DjVuSelection zones;
GetZones(&page.info.pText->page_zone, DjVuTXT::WORD, &rect, zones);
for (GPosition pos = zones; pos; ++pos)
{
DjVuTXT::Zone* pZone = zones[pos];
CString strWord = MakeCString(page.info.pText->textUTF8.substr(pZone->text_start, pZone->text_length));
strWord.TrimLeft();
strWord.TrimRight();
if (strWord.IsEmpty())
continue;
CRect rcWord = TranslatePageRect(nPage, pZone->rect) - ptScroll;
// Round font size up to the next even number to reduce the
// amount of fonts created.
int nFontSize = rcWord.Height() + (rcWord.Height() % 2);
// Find or create the font with this size.
map<int, HFONT>::iterator it = m_fonts.find(nFontSize);
if (it == m_fonts.end())
{
LOGFONT lf;
m_sampleFont.GetLogFont(&lf);
lf.lfHeight = nFontSize;
HFONT hFont = ::CreateFontIndirect(&lf);
it = m_fonts.insert(make_pair(nFontSize, hFont)).first;
}
HGDIOBJ hOldFont = ::SelectObject(pDC->m_hDC, it->second);
pDC->SetTextCharacterExtra(0);
CSize szWordExtent = pDC->GetTextExtent(strWord);
if (strWord.GetLength() > 1)
pDC->SetTextCharacterExtra(static_cast<int>((1.0*rcWord.Width() - szWordExtent.cx)/(strWord.GetLength() - 1)));
pDC->ExtTextOut(rcWord.left, rcWord.top, ETO_CLIPPED, rcWord, strWord + _T(" "), NULL);
::SelectObject(pDC->m_hDC, hOldFont);
}
pDC->EndPath();
// No need to do anything with the path: ExtTextOut has already been called.
// pDC->StrokePath();
pDC->SelectObject(pOldPen);
}
// CDjVuView printing
BOOL CDjVuView::OnPreparePrinting(CPrintInfo* pInfo)
{
pInfo->SetMaxPage(-1);
return DoPreparePrinting(pInfo);
}
// CDjVuView diagnostics
#ifdef _DEBUG
void CDjVuView::AssertValid() const
{
CMyScrollView::AssertValid();
}
void CDjVuView::Dump(CDumpContext& dc) const
{
CMyScrollView::Dump(dc);
}
CDjVuDoc* CDjVuView::GetDocument() const // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDjVuDoc)));
return (CDjVuDoc*)m_pDocument;
}
#endif
// CDjVuView message handlers
void CDjVuView::OnInitialUpdate()
{
CMyScrollView::OnInitialUpdate();
SetTimer(1, 100, NULL);
SetTimer(2, 50, NULL);
m_pSource = GetDocument()->GetSource();
m_pSource->AddRef();
CAppSettings* pAppSettings = theApp.GetAppSettings();
DocSettings* pDocSettings = m_pSource->GetSettings();
CBitmap bitmap;
bitmap.LoadBitmap(IDB_HOURGLASS);
m_hourglass.Create(c_nHourglassWidth, c_nHourglassHeight, ILC_COLOR24 | ILC_MASK, 0, 1);
m_hourglass.Add(&bitmap, RGB(192, 0, 32));
m_toolTip.Create(this, TTS_ALWAYSTIP | TTS_NOPREFIX);
m_toolTip.SendMessage(TTM_SETMAXTIPWIDTH, 0, SHRT_MAX);
m_toolTip.Activate(false);
m_nPageCount = m_pSource->GetPageCount();
m_pages.resize(m_nPageCount);
m_displaySettings = *theApp.GetDisplaySettings();
m_pRenderThread = new CRenderThread(m_pSource, this);
ShowCursor();
theApp.AddObserver(this);
pDocSettings->AddObserver(this);
Invalidate();
if (m_nType == Normal)
{
int nStartupPage = m_nPage;
CPoint ptStartupOffset(0, 0);
if (theApp.GetAppSettings()->bRestoreView)
{
nStartupPage = pDocSettings->nPage;
ptStartupOffset = pDocSettings->ptOffset;
}
nStartupPage = max(0, min(m_nPageCount - 1, nStartupPage));
bool bValidStartupPage = (nStartupPage == pDocSettings->nPage);
m_nMode = pAppSettings->nDefaultMode;
m_nLayout = pAppSettings->nDefaultLayout;
m_bFirstPageAlone = pAppSettings->bFirstPageAlone;
m_bRightToLeft = false;
m_nZoomType = pAppSettings->nDefaultZoomType;
m_fZoom = pAppSettings->fDefaultZoom;
if (m_nZoomType == ZoomPercent)
m_fZoom = 100.0;
Page& page = m_pages[0];
page.Init(m_pSource, 0, false, true);
if (page.info.pAnt != NULL)
{
ReadZoomSettings(page.info.pAnt);
ReadDisplayMode(page.info.pAnt);
}
// Restore zoom
if (pAppSettings->bRestoreView && pDocSettings->nZoomType >= ZoomStretch && pDocSettings->nZoomType <= ZoomPercent)
{
m_nZoomType = pDocSettings->nZoomType;
m_fZoom = max(min(pDocSettings->fZoom, 800.0), 10.0);
pAppSettings->nDefaultZoomType = m_nZoomType;
pAppSettings->fDefaultZoom = m_fZoom;
}
// Restore layout
if (pAppSettings->bRestoreView && pDocSettings->nLayout >= SinglePage && pDocSettings->nLayout <= ContinuousFacing)
{
m_nLayout = pDocSettings->nLayout;
m_bFirstPageAlone = pDocSettings->bFirstPageAlone;
m_bRightToLeft = pDocSettings->bRightToLeft;
theApp.GetAppSettings()->nDefaultLayout = m_nLayout;
theApp.GetAppSettings()->bFirstPageAlone = m_bFirstPageAlone;
}
// Restore mode
if (pAppSettings->bRestoreView && pDocSettings->nDisplayMode >= Color && pDocSettings->nDisplayMode <= Foreground)
m_nDisplayMode = pDocSettings->nDisplayMode;
// Restore rotate
if (pAppSettings->bRestoreView)
m_nRotate = pDocSettings->nRotate;
if (m_nLayout == Continuous || m_nLayout == ContinuousFacing)
UpdateLayout(RECALC);
if (theApp.GetAppSettings()->bRestoreView && bValidStartupPage)
ScrollToPage(nStartupPage, ptStartupOffset);
else
RenderPage(nStartupPage);
AddHistoryPoint();
pDocSettings->nZoomType = m_nZoomType;
pDocSettings->fZoom = m_fZoom;
pDocSettings->nDisplayMode = m_nDisplayMode;
pDocSettings->nLayout = m_nLayout;
pDocSettings->bFirstPageAlone = m_bFirstPageAlone;
pDocSettings->bRightToLeft = m_bRightToLeft;
pDocSettings->nRotate = m_nRotate;
AddObserver(GetMainFrame());
GetMDIChild()->GetThumbnailsView()->AddObserver(this);
AddObserver(GetMDIChild()->GetThumbnailsView());
if (GetMDIChild()->GetContentsTree() != NULL)
GetMDIChild()->GetContentsTree()->AddObserver(this);
if (GetMDIChild()->GetPageIndex() != NULL)
GetMDIChild()->GetPageIndex()->AddObserver(this);
if (GetMDIChild()->HasBookmarksTree())
GetMDIChild()->GetBookmarksTree(false)->AddObserver(this);
UpdateObservers(VIEW_INITIALIZED);
}
}
void CDjVuView::ReadZoomSettings(GP<DjVuANT> pAnt)
{
switch (pAnt->zoom)