-
Notifications
You must be signed in to change notification settings - Fork 106
/
cre.cpp
4371 lines (3914 loc) · 155 KB
/
cre.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
/*
KindlePDFViewer: CREngine abstraction for Lua
Copyright (C) 2012 Hans-Werner Hilse <[email protected]>
Qingping Hou <[email protected]>
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 3 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, see <http://www.gnu.org/licenses/>.
*/
#ifndef DEBUG_CRENGINE
#define DEBUG_CRENGINE 0
#endif
extern "C" {
#include "blitbuffer.h"
#include "drawcontext.h"
#include "cre.h"
}
#include "crengine.h"
#include "lvdocview.h"
#include "lvimg.h"
static void replaceColor( char * str, lUInt32 color ) {
// in line like "0 c #80000000",
// replace value of color
for ( int i=0; i<8; i++ ) {
str[i+5] = toHexDigit((color>>28) & 0xF);
color <<= 4;
}
}
/// set list of battery icons to display battery state
static LVRefVec<LVImageSource> getBatteryIcons(lUInt32 color) {
CRLog::debug("Making list of Battery icon bitmats");
lUInt32 cl1 = 0x00000000|(color&0xFFFFFF);
lUInt32 cl2 = 0x40000000|(color&0xFFFFFF);
lUInt32 cl3 = 0x80000000|(color&0xFFFFFF);
lUInt32 cl4 = 0xF0000000|(color&0xFFFFFF);
static char color1[] = "0 c #80000000";
static char color2[] = "X c #80000000";
static char color3[] = "o c #80AAAAAA";
static char color4[] = ". c #80FFFFFF";
#define BATTERY_HEADER \
"28 15 5 1", \
color1, \
color2, \
color3, \
color4, \
" c None",
static const char * battery8[] = {
BATTERY_HEADER
" .........................",
" .00000000000000000000000.",
" .0.....................0.",
"....0.XXXX.XXXX.XXXX.XXXX.0.",
".0000.XXXX.XXXX.XXXX.XXXX.0.",
".0..0.XXXX.XXXX.XXXX.XXXX.0.",
".0..0.XXXX.XXXX.XXXX.XXXX.0.",
".0..0.XXXX.XXXX.XXXX.XXXX.0.",
".0..0.XXXX.XXXX.XXXX.XXXX.0.",
".0..0.XXXX.XXXX.XXXX.XXXX.0.",
".0000.XXXX.XXXX.XXXX.XXXX.0.",
"....0.XXXX.XXXX.XXXX.XXXX.0.",
" .0.....................0.",
" .00000000000000000000000.",
" .........................",
};
static const char * battery7[] = {
BATTERY_HEADER
" .........................",
" .00000000000000000000000.",
" .0.....................0.",
"....0.oooo.XXXX.XXXX.XXXX.0.",
".0000.oooo.XXXX.XXXX.XXXX.0.",
".0..0.oooo.XXXX.XXXX.XXXX.0.",
".0..0.oooo.XXXX.XXXX.XXXX.0.",
".0..0.oooo.XXXX.XXXX.XXXX.0.",
".0..0.oooo.XXXX.XXXX.XXXX.0.",
".0..0.oooo.XXXX.XXXX.XXXX.0.",
".0000.oooo.XXXX.XXXX.XXXX.0.",
"....0.oooo.XXXX.XXXX.XXXX.0.",
" .0.....................0.",
" .00000000000000000000000.",
" .........................",
};
static const char * battery6[] = {
BATTERY_HEADER
" .........................",
" .00000000000000000000000.",
" .0.....................0.",
"....0......XXXX.XXXX.XXXX.0.",
".0000......XXXX.XXXX.XXXX.0.",
".0..0......XXXX.XXXX.XXXX.0.",
".0..0......XXXX.XXXX.XXXX.0.",
".0..0......XXXX.XXXX.XXXX.0.",
".0..0......XXXX.XXXX.XXXX.0.",
".0..0......XXXX.XXXX.XXXX.0.",
".0000......XXXX.XXXX.XXXX.0.",
"....0......XXXX.XXXX.XXXX.0.",
" .0.....................0.",
" .00000000000000000000000.",
" .........................",
};
static const char * battery5[] = {
BATTERY_HEADER
" .........................",
" .00000000000000000000000.",
" .0.....................0.",
"....0......oooo.XXXX.XXXX.0.",
".0000......oooo.XXXX.XXXX.0.",
".0..0......oooo.XXXX.XXXX.0.",
".0..0......oooo.XXXX.XXXX.0.",
".0..0......oooo.XXXX.XXXX.0.",
".0..0......oooo.XXXX.XXXX.0.",
".0..0......oooo.XXXX.XXXX.0.",
".0000......oooo.XXXX.XXXX.0.",
"....0......oooo.XXXX.XXXX.0.",
" .0.....................0.",
" .00000000000000000000000.",
" .........................",
};
static const char * battery4[] = {
BATTERY_HEADER
" .........................",
" .00000000000000000000000.",
" .0.....................0.",
"....0...........XXXX.XXXX.0.",
".0000...........XXXX.XXXX.0.",
".0..0...........XXXX.XXXX.0.",
".0..0...........XXXX.XXXX.0.",
".0..0...........XXXX.XXXX.0.",
".0..0...........XXXX.XXXX.0.",
".0..0...........XXXX.XXXX.0.",
".0000...........XXXX.XXXX.0.",
"....0...........XXXX.XXXX.0.",
" .0.....................0.",
" .00000000000000000000000.",
" .........................",
};
static const char * battery3[] = {
BATTERY_HEADER
" .........................",
" .00000000000000000000000.",
" .0.....................0.",
"....0...........oooo.XXXX.0.",
".0000...........oooo.XXXX.0.",
".0..0...........oooo.XXXX.0.",
".0..0...........oooo.XXXX.0.",
".0..0...........oooo.XXXX.0.",
".0..0...........oooo.XXXX.0.",
".0..0...........oooo.XXXX.0.",
".0000...........oooo.XXXX.0.",
"....0...........oooo.XXXX.0.",
" .0.....................0.",
" .00000000000000000000000.",
" .........................",
};
static const char * battery2[] = {
BATTERY_HEADER
" .........................",
" .00000000000000000000000.",
" .0.....................0.",
"....0................XXXX.0.",
".0000................XXXX.0.",
".0..0................XXXX.0.",
".0..0................XXXX.0.",
".0..0................XXXX.0.",
".0..0................XXXX.0.",
".0..0................XXXX.0.",
".0000................XXXX.0.",
"....0................XXXX.0.",
" .0.....................0.",
" .00000000000000000000000.",
" .........................",
};
static const char * battery1[] = {
BATTERY_HEADER
" .........................",
" .00000000000000000000000.",
" .0.....................0.",
" .0................oooo.0.",
".0000................oooo.0.",
".0..0................oooo.0.",
".0..0................oooo.0.",
".0..0................oooo.0.",
".0..0................oooo.0.",
".0..0................oooo.0.",
".0000................oooo.0.",
" .0................oooo.0.",
" .0.....................0.",
" .00000000000000000000000.",
" .........................",
};
static const char * battery0[] = {
BATTERY_HEADER
" .........................",
" .00000000000000000000000.",
" .0.....................0.",
" .0.....................0.",
".0000.....................0.",
".0..0.....................0.",
".0..0.....................0.",
".0..0.....................0.",
".0..0.....................0.",
".0..0.....................0.",
".0000.....................0.",
"....0.....................0.",
" .0.....................0.",
" .00000000000000000000000.",
" .........................",
};
//#endif
static const char * battery_charge[] = {
BATTERY_HEADER
" .........................",
" .00000000000000000000000.",
" .0.....................0.",
"....0.....................0.",
".0000............XX.......0.",
".0..0...........XXXX......0.",
".0..0..XX......XXXXXX.....0.",
".0..0...XXX...XXXX..XX....0.",
".0..0....XXX..XXXX...XX...0.",
".0..0.....XXXXXXX.....XX..0.",
".0000.......XXXX..........0.",
"....0........XX...........0.",
" .0.....................0.",
" .00000000000000000000000.",
" .........................",
};
static const char * battery_frame[] = {
BATTERY_HEADER
" .........................",
" .00000000000000000000000.",
" .0.....................0.",
"....0.....................0.",
".0000.....................0.",
".0..0.....................0.",
".0..0.....................0.",
".0..0.....................0.",
".0..0.....................0.",
".0..0.....................0.",
".0000.....................0.",
"....0.....................0.",
" .0.....................0.",
" .00000000000000000000000.",
" .........................",
};
const char * * icon_bpm[] = {
battery_charge,
battery0,
battery1,
battery2,
battery3,
battery4,
battery5,
battery6,
battery7,
battery8,
battery_frame,
NULL
};
replaceColor( color1, cl1 );
replaceColor( color2, cl2 );
replaceColor( color3, cl3 );
replaceColor( color4, cl4 );
LVRefVec<LVImageSource> icons;
for ( int i=0; icon_bpm[i]; i++ ) {
icons.add(LVCreateXPMImageSource( icon_bpm[i] ));
}
return icons;
}
// Single global object to handle callbacks from crengine to Lua
class CreCallbackForwarder : public LVDocViewCallback
{
bool _active;
lua_State * _L; // stack (of the coroutine) that set the callback
// References in the Lua registry, to prevent these objects to be gc()'ed
int _r_L; // ref to L, to prevent it from being gc'ed when coroutine ends
int _r_cb; // ref to provided Lua function to be forwarded crengine events
public:
CreCallbackForwarder() :
_active(false),
// As a crengine document is opened from inside a coroutine, but can be closed
// from the main thread, and as Lua 5.1 does not provide access to the main thread
// from a coroutine, we need to store the coroutine lua_State to keep a reference
// to it and prevent if from being gc'ed.
// Fortunatly, the registry index is shared by the main thread and all coroutines
_L(NULL),
_r_L(LUA_NOREF),
_r_cb(LUA_NOREF)
{ }
void setCallback(lua_State *L) {
// Cleanup any previous references
unsetCallback(L);
// Get and keep reference to the callback (first on the stack)
_r_cb = luaL_ref(L, LUA_REGISTRYINDEX);
// printf("CreCallbackForwarder._r_cb = %x\n", _r_cb);
// Push current coroutine/thread (L) onto the stack, and keep a reference to it,
// so it does not get gc'ed when our coroutine exits
lua_pushthread(L);
_r_L = luaL_ref(L, LUA_REGISTRYINDEX);
// printf("CreCallbackForwarder._r_L = %x\n", _r_L);
_L = L; // Store L, so we can fetch the callback from _r_cb
_active = true;
}
void unsetCallback(lua_State *L) {
_active = false;
if (_r_cb != LUA_NOREF) {
luaL_unref(L, LUA_REGISTRYINDEX, _r_cb);
_r_cb = LUA_NOREF;
}
if (_r_L != LUA_NOREF) {
luaL_unref(L, LUA_REGISTRYINDEX, _r_L);
_r_L = LUA_NOREF;
}
_L = NULL;
}
void callback(const char * event) {
if (!_active)
return;
lua_rawgeti(_L, LUA_REGISTRYINDEX, _r_cb);
lua_pushstring(_L, event);
lua_pcall(_L, 1, 0, 0);
}
void callback(const char * event, int number) {
if (!_active)
return;
lua_rawgeti(_L, LUA_REGISTRYINDEX, _r_cb);
lua_pushstring(_L, event);
lua_pushinteger(_L, number);
lua_pcall(_L, 2, 0, 0);
}
void callback(const char * event, const char * str) {
if (!_active)
return;
lua_rawgeti(_L, LUA_REGISTRYINDEX, _r_cb);
lua_pushstring(_L, event);
lua_pushstring(_L, str);
lua_pcall(_L, 2, 0, 0);
}
virtual void OnLoadFileStart( lString32 filename ) {
callback("OnLoadFileStart", UnicodeToLocal(filename).c_str());
}
virtual void OnLoadFileFormatDetected( doc_format_t fileFormat) {
callback("OnLoadFileFormatDetected", UnicodeToLocal(getDocFormatName(fileFormat)).c_str());
}
virtual void OnLoadFileProgress( int percent) {
callback("OnLoadFileProgress", percent);
}
virtual void OnLoadFileEnd() {
callback("OnLoadFileEnd");
}
virtual void OnLoadFileError(lString32 message) {
callback("OnLoadFileError", UnicodeToLocal(message).c_str());
}
virtual void OnNodeStylesUpdateStart() {
callback("OnNodeStylesUpdateStart");
}
virtual void OnNodeStylesUpdateProgress(int percent) {
callback("OnNodeStylesUpdateProgress", percent);
}
virtual void OnNodeStylesUpdateEnd() {
callback("OnNodeStylesUpdateEnd");
}
virtual void OnFormatStart() {
callback("OnFormatStart");
}
virtual void OnFormatProgress(int percent) {
callback("OnFormatProgress", percent);
}
virtual void OnFormatEnd() {
callback("OnFormatEnd");
}
virtual void OnDocumentReady() {
callback("OnDocumentReady");
}
virtual void OnSaveCacheFileStart() {
callback("OnSaveCacheFileStart");
}
virtual void OnSaveCacheFileProgress(int percent) {
callback("OnSaveCacheFileProgress", percent);
}
virtual void OnSaveCacheFileEnd() {
callback("OnSaveCacheFileEnd");
}
// virtual void OnLoadFileFirstPagesReady() { } // useless
// virtual void OnExportProgress(int percent) { } // Export to WOL format
// virtual void OnExternalLink(lString32 /*url*/, ldomNode * /*node*/) { }
// virtual void OnImageCacheClear() { }
// virtual bool OnRequestReload() { return false; }
};
CreCallbackForwarder * cre_callback_forwarder = NULL;
typedef struct CreDocument {
LVDocView *text_view;
ldomDocument *dom_doc;
} CreDocument;
static int setCallback(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
if ( cre_callback_forwarder == NULL ) {
cre_callback_forwarder = new CreCallbackForwarder();
}
if (lua_isfunction(L, 2)) {
cre_callback_forwarder->setCallback(L);
doc->text_view->setCallback(cre_callback_forwarder);
}
else {
doc->text_view->setCallback(NULL);
cre_callback_forwarder->unsetCallback(L);
}
return 0;
}
static int initCache(lua_State *L) {
const char *cache_path = luaL_checkstring(L, 1);
int cache_size = luaL_checkint(L, 2);
bool compress_cached_data = true;
if (lua_isboolean(L, 3)) {
compress_cached_data = lua_toboolean(L, 3);
}
float storage_max_uncompressed_size_factor = (float)luaL_optnumber(L, 4, 1.0);
// Setting this to false uses more disk space for cache,
// but speed up rendering and page turns quite a bit
compressCachedData(compress_cached_data);
// Increase the 4 hardcoded TEXT_CACHE_UNPACKED_SPACE, ELEM_CACHE_UNPACKED_SPACE,
// RECT_CACHE_UNPACKED_SPACE and STYLE_CACHE_UNPACKED_SPACE by this factor
setStorageMaxUncompressedSizeFactor(storage_max_uncompressed_size_factor);
ldomDocCache::init(lString32(cache_path), cache_size);
return 0;
}
static int initHyphDict(lua_State *L) {
const char *dict_path = luaL_checkstring(L, 1);
HyphMan::initDictionaries(lString32(dict_path));
return 0;
}
static int newDocView(lua_State *L) {
int width = luaL_checkint(L, 1);
int height = luaL_checkint(L, 2);
LVDocViewMode view_mode = (LVDocViewMode)luaL_checkint(L, 3);
CreDocument *doc = (CreDocument*) lua_newuserdata(L, sizeof(CreDocument));
luaL_getmetatable(L, "credocument");
lua_setmetatable(L, -2);
doc->text_view = new LVDocView(-1, true); // bitsPerPixel=-1, noDefaultDocument=true
//doc->text_view->setBackgroundColor(0xFFFFFF);
//doc->text_view->setTextColor(0x000000);
//doc->text_view->doCommand(DCMD_SET_DOC_FONTS, 1);
//doc->text_view->doCommand(DCMD_SET_INTERNAL_STYLES, 1);
doc->text_view->setViewMode(view_mode, -1);
doc->text_view->Resize(width, height);
doc->text_view->setPageHeaderInfo(PGHDR_AUTHOR|PGHDR_TITLE|PGHDR_PAGE_NUMBER|PGHDR_PAGE_COUNT|PGHDR_CHAPTER_MARKS|PGHDR_CLOCK);
doc->text_view->setBatteryIcons(getBatteryIcons(0x000000));
return 1;
}
static int readDefaults(lua_State *L) {
// This is to be called only when the document is opened to be
// read by ReaderUI - not when the document is opened to just
// get its metadata or cover image - as it will affect some
// crengine global variables and state, whose change would
// affect the currently opened for reading document.
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
// it will overwrite all settings by values found in ./data/cr3.ini
CRPropRef props = doc->text_view->propsGetCurrent();
LVStreamRef stream = LVOpenFileStream("data/cr3.ini", LVOM_READ);
if ( !stream.isNull() && props->loadFromStream(stream.get()) ) {
doc->text_view->propsApply(props);
} else {
// Tweak the default settings to be slightly less random
props->setString(PROP_FALLBACK_FONT_FACES, "Noto Sans CJK SC");
props->setString(PROP_HYPHENATION_DICT, "English_US.pattern");
props->setString(PROP_STATUS_FONT_FACE, "Noto Sans");
props->setString(PROP_FONT_FACE, "Noto Serif");
// Note: the values we set here don't really matter, they will
// be re-set/overridden by readerfont.lua on each book load
props->setInt(PROP_FONT_HINTING, 2); // autohint, to be conservative (some ttf fonts' bytecode is truly crappy)
props->setInt(PROP_FONT_KERNING, 3); // harfbuzz (slower than freetype kerning, but needed for proper arabic)
// props->setInt(PROP_FONT_KERNING_ENABLED, 1);
props->setString("styles.pre.font-face", "font-family: \"Droid Sans Mono\"");
// Disable crengine image scaling options (we prefer scaling them via crengine.render.dpi)
props->setInt(PROP_IMG_SCALING_ZOOMIN_INLINE_MODE, 0);
props->setInt(PROP_IMG_SCALING_ZOOMIN_INLINE_SCALE, 1);
props->setInt(PROP_IMG_SCALING_ZOOMOUT_INLINE_MODE, 0);
props->setInt(PROP_IMG_SCALING_ZOOMOUT_INLINE_SCALE, 1);
props->setInt(PROP_IMG_SCALING_ZOOMIN_BLOCK_MODE, 0);
props->setInt(PROP_IMG_SCALING_ZOOMIN_BLOCK_SCALE, 1);
props->setInt(PROP_IMG_SCALING_ZOOMOUT_BLOCK_MODE, 0);
props->setInt(PROP_IMG_SCALING_ZOOMOUT_BLOCK_SCALE, 1);
stream = LVOpenFileStream("data/cr3.ini", LVOM_WRITE);
props->saveToStream(stream.get());
}
return 0;
}
static int saveDefaults(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
CRPropRef props = doc->text_view->propsGetCurrent();
LVStreamRef stream = LVOpenFileStream("data/cr3.ini", LVOM_WRITE);
return props->saveToStream(stream.get());
}
static int getLatestDomVersion(lua_State *L) {
lua_pushinteger(L, gDOMVersionCurrent);
return 1;
}
static int getDomVersionWithNormalizedXPointers(lua_State *L) {
lua_pushinteger(L, DOM_VERSION_WITH_NORMALIZED_XPOINTERS); // defined in lvtinydom.h
return 1;
}
static int setUserHyphenationDict(lua_State *L) {
const char *filename = luaL_checkstring(L, 1);
bool reload = lua_toboolean(L, 2);
lua_pushinteger(L, UserHyphDict::init(filename, reload));
return 1;
}
static int getHyphenationForWord(lua_State *L) {
const char *word = luaL_checkstring(L, 1);
lString32 hyphenation = UserHyphDict::getHyphenation(word);
lua_pushstring(L, UnicodeToLocal(hyphenation).c_str());
return 1;
}
static int softHyphenateText(lua_State *L) {
const char *lang = luaL_checkstring(L, 1);
const char *text = luaL_checkstring(L, 2);
TextLangCfg * lang_cfg = TextLangMan::getTextLangCfg( lString32(lang), true );
lString32 utext = Utf8ToUnicode(text);
// We provide use_default_hyph_method=true, to use the hyph dict for
// that language, even if hyphenation is disabled in crengine
lString32 hyphenated_text = lang_cfg->softHyphenateText(utext, true);
lua_pushstring(L, UnicodeToLocal(hyphenated_text).c_str());
return 1;
}
static int getIntProperty(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
const char *propName = luaL_checkstring(L, 2);
int value;
CRPropRef props = doc->text_view->propsGetCurrent();
props->getInt(propName, value);
lua_pushinteger(L, value);
return 1;
}
static int setIntProperty(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
const char *propName = luaL_checkstring(L, 2);
int value = luaL_checkint(L, 3);
CRPropRef props = LVCreatePropsContainer();
props->setInt(propName, value);
doc->text_view->propsApply(props);
return 0;
}
static int getStringProperty(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
const char *propName = luaL_checkstring(L, 2);
lString32 value;
CRPropRef props = doc->text_view->propsGetCurrent();
props->getString(propName, value);
lua_pushstring(L, UnicodeToLocal(value).c_str());
return 1;
}
static int setStringProperty(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
const char *propName = luaL_checkstring(L, 2);
const char *value = luaL_checkstring(L, 3);
CRPropRef props = LVCreatePropsContainer();
props->setString(propName, value);
doc->text_view->propsApply(props);
return 0;
}
static int getGammaLevel(lua_State *L) {
lua_pushnumber(L, fontMan->GetGamma());
return 1;
}
static int getGammaIndex(lua_State *L) {
lua_pushinteger(L, fontMan->GetGammaIndex());
return 1;
}
static int setGammaIndex(lua_State *L) {
int index = luaL_checkint(L, 1);
fontMan->SetGammaIndex(index);
return 0;
}
static int getHyphDictList(lua_State *L) {
HyphDictionaryList *list = HyphMan::getDictList();
lua_createtable(L, list->length(), 0);
for(int i = 0; i < list->length(); i++) {
lua_pushstring(L, UnicodeToLocal(list->get(i)->getId()).c_str());
lua_rawseti(L, -2, i+i);
}
return 1;
}
static int getSelectedHyphDict(lua_State *L) {
lua_pushstring(L, UnicodeToLocal(HyphMan::getSelectedDictionary()->getId()).c_str());
lua_pushinteger(L, TextLangMan::getMainLangHyphMethod()->getLeftHyphenMin());
lua_pushinteger(L, TextLangMan::getMainLangHyphMethod()->getRightHyphenMin());
return 3;
}
static int setHyphDictionary(lua_State *L) {
const char *id = luaL_checkstring(L, 1);
HyphMan::getDictList()->activate((lString32)id);
return 0;
}
static int getTextLangStatus(lua_State *L) {
lua_pushstring(L, UnicodeToLocal(TextLangMan::getMainLang()).c_str());
lua_pushstring(L, UnicodeToLocal(TextLangMan::getMainLangHyphMethod()->getId()).c_str());
LVPtrVector<TextLangCfg> *list = TextLangMan::getLangCfgList();
lua_createtable(L, 0, list->length());
for(int i = 0; i < list->length(); i++) {
TextLangCfg * lang_cfg = list->get(i);
// Key
lua_pushstring(L, UnicodeToLocal(lang_cfg->getLangTag()).c_str());
// Value: table
lua_createtable(L, 0, 3);
lua_pushstring(L, "hyph_dict_name");
lua_pushstring(L, UnicodeToLocal(lang_cfg->getDefaultHyphMethod()->getId()).c_str());
lua_rawset(L, -3);
lua_pushstring(L, "hyph_nb_patterns");
lua_pushinteger(L, lang_cfg->getDefaultHyphMethod()->getCount());
lua_rawset(L, -3);
lua_pushstring(L, "hyph_mem_size");
lua_pushinteger(L, lang_cfg->getDefaultHyphMethod()->getSize());
lua_rawset(L, -3);
lua_rawset(L, -3);
}
return 3;
}
static int loadDocument(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
const char *file_name = luaL_checkstring(L, 2);
bool only_metadata = false;
if (lua_isboolean(L, 3)) {
only_metadata = lua_toboolean(L, 3);
}
doc->text_view->LoadDocument(file_name, only_metadata);
doc->dom_doc = doc->text_view->getDocument();
bool loaded = false;
if (doc->dom_doc) { loaded = true ;}
lua_pushboolean(L, loaded);
return 1;
}
static int renderDocument(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
doc->text_view->Render();
return 0;
}
static int closeDocument(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
/* should be safe if called twice */
if(doc->text_view != NULL) {
// Call close() to have the cache explicitly saved now
// while we still have a callback (to show its progress).
doc->text_view->close();
// Remove any callback
if (cre_callback_forwarder) {
doc->text_view->setCallback(NULL);
cre_callback_forwarder->unsetCallback(L);
}
delete doc->text_view;
doc->text_view = NULL;
// Destroyed by text_view->close()
doc->dom_doc = NULL;
}
return 0;
}
static int isBuiltDomStale(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
lua_pushboolean(L, doc->dom_doc->isBuiltDomStale());
return 1;
}
static int hasCacheFile(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
lua_pushboolean(L, doc->dom_doc->hasCacheFile());
return 1;
}
static int isCacheFileStale(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
lua_pushboolean(L, doc->dom_doc->isCacheFileStale());
return 1;
}
static int invalidateCacheFile(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
doc->dom_doc->invalidateCacheFile();
return 0;
}
static int getCacheFilePath(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
lString32 cache_path = doc->dom_doc->getCacheFilePath();
if (cache_path.empty())
return 0;
lua_pushstring(L, UnicodeToLocal(cache_path).c_str());
return 1;
}
static int getStatistics(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
lString32 stats = doc->dom_doc->getStatistics();
lua_pushstring(L, UnicodeToLocal(stats).c_str());
return 1;
}
static int getUnknownEntities(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
lString32Collection unknown_entities = doc->dom_doc->getUnknownEntities();
lua_pushstring(L, UnicodeToLocal(unknown_entities[0]).c_str());
lua_pushstring(L, UnicodeToLocal(unknown_entities[1]).c_str());
lua_pushstring(L, UnicodeToLocal(unknown_entities[2]).c_str());
return 3;
}
static int getDocumentFormat(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
lString32 docformat = getDocFormatName(doc->text_view->getDocFormat());
lua_pushstring(L, UnicodeToLocal(docformat).c_str());
return 1;
}
static int getDocumentProps(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
lua_createtable(L, 0, 6);
lua_pushstring(L, "title");
lua_pushstring(L, UnicodeToLocal(doc->text_view->getTitle()).c_str());
lua_rawset(L, -3);
lua_pushstring(L, "authors");
lua_pushstring(L, UnicodeToLocal(doc->text_view->getAuthors()).c_str());
lua_rawset(L, -3);
lua_pushstring(L, "language");
lua_pushstring(L, UnicodeToLocal(doc->text_view->getLanguage()).c_str());
lua_rawset(L, -3);
lua_pushstring(L, "series");
lua_pushstring(L, UnicodeToLocal(doc->text_view->getSeries()).c_str());
lua_rawset(L, -3);
lua_pushstring(L, "description");
lua_pushstring(L, UnicodeToLocal(doc->text_view->getDescription()).c_str());
lua_rawset(L, -3);
lua_pushstring(L, "keywords");
lua_pushstring(L, UnicodeToLocal(doc->text_view->getKeywords()).c_str());
lua_rawset(L, -3);
lua_pushstring(L, "identifiers");
lua_pushstring(L, UnicodeToLocal(doc->text_view->getIdentifiers()).c_str());
lua_rawset(L, -3);
return 1;
}
static int setAltDocumentProp(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
const char *prop = luaL_checkstring(L, 2);
if (lua_isstring(L, 3)) {
const char *value = luaL_checkstring(L, 3);
doc->text_view->getAltDocProps()->setString(prop, value);
}
else {
doc->text_view->getAltDocProps()->deleteProperty(prop);
}
return 0;
}
static int getDocumentRenderingHash(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
bool extended = false;
if (lua_isboolean(L, 2)) {
extended = lua_toboolean(L, 2);
}
lua_pushinteger(L, doc->text_view->getDocumentRenderingHash(extended));
return 1;
}
// Partial rerendering support
static int canBePartiallyRerendered(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
if (doc->dom_doc) {
lua_pushboolean(L, doc->dom_doc->canBePartiallyRerendered());
return 1;
}
return 0;
}
static int isPartialRerenderingEnabled(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
if (doc->dom_doc) {
lua_pushboolean(L, doc->dom_doc->isPartialRerenderingEnabled());
return 1;
}
return 0;
}
static int enablePartialRerendering(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
bool enable = lua_toboolean(L, 2);
if (doc->dom_doc) {
bool ret = doc->dom_doc->enablePartialRerendering(enable);
lua_pushboolean(L, ret);
return 1;
}
return 0;
}
static int getPartialRerenderingsCount(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
if (doc->dom_doc) {
lua_pushinteger(L, doc->dom_doc->getPartialRerenderingsCount());
return 1;
}
return 0;
}
static int isRerenderingDelayed(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
if (doc->dom_doc) {
lua_pushboolean(L, doc->dom_doc->isRerenderingDelayed(lua_toboolean(L, 2)));
return 1;
}
return 0;
}
static int getPages(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
bool internal = false;
if (lua_isboolean(L, 2)) {
internal = lua_toboolean(L, 2);
}
lua_pushinteger(L, doc->text_view->getPageCount(internal));
return 1;
}
static int getCurrentPage(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
bool internal = false;
if (lua_isboolean(L, 2)) {
internal = lua_toboolean(L, 2);
}
int page = doc->text_view->getCurPage(internal);
lua_pushinteger(L, page+1);
return 1;
}
static int getPageFlow(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
int pageno = luaL_checkint(L, 2);
lua_pushinteger(L, doc->text_view->getPageFlow(pageno-1));
return 1;
}
static int hasNonLinearFlows(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
lua_pushboolean(L, doc->text_view->hasNonLinearFlows());
return 1;
}
static int getPageFromXPointer(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
const char *xpointer_str = luaL_checkstring(L, 2);
int page = 1;
ldomXPointer xp = doc->dom_doc->createXPointer(lString32(xpointer_str));
if ( !xp.isNull() ) { // Found in document
// Ensure xp points to a visible node that has a y in the document.
// If it is invisible, get the next visible node
ldomXPointerEx xpe = xp;
if ( xpe.isText() )
xpe.parent();
if ( xpe.getNode()->getRendMethod() == erm_invisible ) {
xpe = xp;
while ( xpe.nextElement() ) {
if ( xpe.getNode()->getRendMethod() != erm_invisible ) {
xp = xpe;
break;
}
}
}
page = doc->text_view->getBookmarkPage(xp) + 1;
}
lua_pushinteger(L, page);
return 1;
}
static int getPosFromXPointer(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
const char *xpointer_str = luaL_checkstring(L, 2);
int y = 0;
int x = 0;
ldomXPointer xp = doc->dom_doc->createXPointer(lString32(xpointer_str));
if ( !xp.isNull() ) { // Found in document
// Ensure xp points to a visible node that has a y in the document.
// If it is invisible, get the next visible node
ldomXPointerEx xpe = xp;
if ( xpe.isText() )
xpe.parent();
if ( xpe.getNode()->getRendMethod() == erm_invisible ) {
xpe = xp;
while ( xpe.nextElement() ) {
if ( xpe.getNode()->getRendMethod() != erm_invisible ) {
xp = xpe;
break;
}
}
}
lvPoint pt = xp.toPoint(true); // extended=true, for better accuracy
if (pt.y > 0) {
y = pt.y;
}
x = pt.x;
}
lua_pushinteger(L, y);
// Also returns the x value (as the 2nd returned value, as its
// less interesting to current code than the y value)
lua_pushinteger(L, x);
return 2;
}
static int getCurrentPos(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");