1 /* Written by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
2 * Understanding is not required. Only obedience.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 import std
.concurrency
;
25 import arsd
.simpledisplay
;
31 import iv
.nanovg
.oui
.blendish
;
41 // ////////////////////////////////////////////////////////////////////////// //
42 BookText
loadBook (string fname
) {
43 __gshared
bool eliteShipsLoaded
= false;
46 //import core.memory : GC;
47 fname
= fname
.expandTilde
.absolutePath
;
48 //writeln("loading '", fname, "'...");
50 auto stt
= MonoTime
.currTime
;
51 auto book
= new BookText(fname
);
54 { import std
.stdio
; writeln("loaded: '", fname
, "' in ", (MonoTime
.currTime
-stt
).total
!"msecs", " milliseconds"); }
59 foreach (string line
; VFile(buildPath(RcDir
, ".lastfile")).byLineCopy
) {
63 while (lines
.length
&& lines
[$-1].isComment
) {
64 comments
~= lines
[$-1];
65 lines
= lines
[0..$-1];
69 } catch (Exception
) {}
70 try { import std
.file
; mkdirRecurse(RcDir
); } catch (Exception
) {}
71 auto fo
= VFile(buildPath(RcDir
, ".lastfile"), "w");
72 foreach (string s
; lines
) fo
.writeln(s
);
73 foreach_reverse (string s
; comments
) fo
.writeln(s
);
76 stateFileName
= buildPath(RcDir
, fname
.baseName
~".rc");
78 if (!eliteShipsLoaded
) {
80 eliteShipsLoaded
= true;
87 // ////////////////////////////////////////////////////////////////////////// //
88 private struct ImageInfo
{
89 enum MaxIdleFrames
= 5;
95 private __gshared ImageInfo
[] nvgImageIds
;
98 void releaseImages (NVGContext vg
) {
99 if (nvgImageIds
.length
) {
100 foreach (ref ImageInfo nfo
; nvgImageIds
) {
102 if (++nfo
.framesNotUsed
> ImageInfo
.MaxIdleFrames
) {
103 //writeln("freeing image with id ", nfo.iid);
104 vg
.deleteImage(nfo
.iid
);
114 private int registerImage (NVGContext vg
, TrueColorImage img
) {
115 if (vg
is null || img
is null) return -1;
116 uint freeIdx
= uint.max
;
117 foreach (immutable idx
, ref ImageInfo nfo
; nvgImageIds
) {
118 if (nfo
.img
is img
) {
119 assert(nfo
.iid
>= 0);
120 nfo
.framesNotUsed
= 0;
123 if (freeIdx
!= uint.max
&& nfo
.iid
< 0) freeIdx
= cast(uint)idx
;
125 int iid
= vg
.createImageFromMemoryImage(img
); //vg.createImageRGBA(img.width, img.height, img.imageData.bytes[], NVGImageFlags.None);
126 if (iid
< 0) return -1;
127 nvgImageIds
~= ImageInfo(iid
, img
, 0);
132 class BookImage
: LayObject
{
136 this (BookText abook
, uint aidx
) { book
= abook
; imageidx
= aidx
; img
= book
.images
[aidx
].img
; }
137 override int width () { return img
.width
; }
138 override int spacewidth () { return 0; }
139 override int height () { return img
.height
; }
140 override int ascent () { return img
.height
; }
141 override int descent () { return 0; }
142 override bool canbreak () { return true; }
143 override bool spaced () { return false; }
144 override void draw (NVGContext vg
, float x
, float y
) {
147 scope(exit
) vg
.restore();
149 int iid
= vg
.registerImage(img
);
151 vg
.fillPaint(vg
.imagePattern(x
, y
, img
.width
, img
.height
, 0, iid
));
152 //vg.globalAlpha(0.5);
153 vg
.rect(x
, y
, img
.width
, img
.height
);
159 // ////////////////////////////////////////////////////////////////////////// //
163 uint wordidx
; // first word
166 Anc
[uint] hrefs
; // `name` is dest
171 // ////////////////////////////////////////////////////////////////////////// //
172 private void fixFont (LayText lay
) {
173 if (lay
.fontStyle
.italic
) {
174 if (lay
.fontStyle
.bold
) lay
.fontStyle
.fontface
= lay
.fontFaceId("textz");
175 else lay
.fontStyle
.fontface
= lay
.fontFaceId("texti");
176 } else if (lay
.fontStyle
.bold
) {
177 if (lay
.fontStyle
.italic
) lay
.fontStyle
.fontface
= lay
.fontFaceId("textz");
178 else lay
.fontStyle
.fontface
= lay
.fontFaceId("textb");
180 lay
.fontStyle
.fontface
= lay
.fontFaceId("text");
184 private void setNormalStyle (LayText lay
) {
185 lay
.fontStyle
.resetAttrs
;
186 lay
.fontStyle
.fontsize
= fsizeText
;
187 lay
.fontStyle
.color
= colorText
.asUint
;
188 lay
.lineStyle
.leftpad
= 0;
189 lay
.lineStyle
.rightpad
= 0;
190 lay
.lineStyle
.paraIndent
= 3;
191 if (optJustify
) lay
.lineStyle
.setJustify
; else lay
.lineStyle
.setLeft
;
195 private void setTitleStyle (LayText lay
) {
196 lay
.fontStyle
.resetAttrs
;
197 lay
.fontStyle
.bold
= true;
198 lay
.fontStyle
.fontsize
= fsizeText
+4;
199 lay
.fontStyle
.color
= colorText
.asUint
;
200 lay
.lineStyle
.leftpad
= 0;
201 lay
.lineStyle
.rightpad
= 0;
202 lay
.lineStyle
.paraIndent
= 0;
203 lay
.lineStyle
.setCenter
;
207 private void setSubtitleStyle (LayText lay
) {
208 lay
.fontStyle
.resetAttrs
;
209 lay
.fontStyle
.bold
= true;
210 lay
.fontStyle
.fontsize
= fsizeText
+2;
211 lay
.fontStyle
.color
= colorText
.asUint
;
212 lay
.lineStyle
.leftpad
= 0;
213 lay
.lineStyle
.rightpad
= 0;
214 lay
.lineStyle
.paraIndent
= 0;
215 lay
.lineStyle
.setCenter
;
219 private void setCiteStyle (LayText lay
) {
220 lay
.fontStyle
.resetAttrs
;
221 lay
.fontStyle
.italic
= true;
222 lay
.fontStyle
.fontsize
= fsizeText
;
223 lay
.fontStyle
.color
= colorText
.asUint
;
224 lay
.lineStyle
.leftpad
= fsizeText
*3;
225 lay
.lineStyle
.rightpad
= fsizeText
*3;
226 lay
.lineStyle
.paraIndent
= 3;
227 if (optJustify
) lay
.lineStyle
.setJustify
; else lay
.lineStyle
.setLeft
;
231 private void setEpigraphStyle (LayText lay
) {
232 lay
.fontStyle
.resetAttrs
;
233 lay
.fontStyle
.italic
= true;
234 lay
.fontStyle
.fontsize
= fsizeText
;
235 lay
.fontStyle
.color
= colorText
.asUint
;
236 lay
.lineStyle
.leftpad
= lay
.width
/2;
237 lay
.lineStyle
.rightpad
= 0;
238 lay
.lineStyle
.paraIndent
= 0;
239 lay
.lineStyle
.setRight
;
243 private void setPoemStyle (LayText lay
) {
244 lay
.fontStyle
.resetAttrs
;
245 lay
.fontStyle
.italic
= true;
246 lay
.fontStyle
.fontsize
= fsizeText
;
247 lay
.fontStyle
.color
= colorText
.asUint
;
248 lay
.lineStyle
.leftpad
= fsizeText
*3;
249 lay
.lineStyle
.rightpad
= 0;
250 lay
.lineStyle
.paraIndent
= 0;
251 lay
.lineStyle
.setLeft
;
256 // ////////////////////////////////////////////////////////////////////////// //
257 public BookMetadata
formatBook (BookText book
, LayText lay
) {
258 assert(book
!is null);
260 BookMetadata meta
= new BookMetadata();
262 void dumpTree (Tag ct
) {
263 while (ct
!is null) {
264 { import std
.stdio
; writeln("tag: ", ct
.name
); }
269 void badTag (Tag tag
, string msg
=null) {
270 import std
.string
: format
, indexOf
;
271 assert(tag
!is null);
273 if (msg
.length
== 0) msg
= "invalid tag: '%s'";
274 if (msg
.indexOf("%s") >= 0) {
275 throw new Exception(msg
.format(tag
.name
));
277 throw new Exception(msg
);
281 void saveTagId (Tag tag
) {
283 import std
.conv
: to
;
284 meta
.ids
~= BookMetadata
.Anc(tag
.id
.to
!dstring
, lay
.nextWordIndex
);
288 void putImage (Tag tag
) {
289 if (tag
is null || tag
.href
.length
< 2 || tag
.href
[0] != '#') return;
290 string iid
= tag
.href
[1..$];
291 //writeln("searching for image with href '", iid, "' (", book.images.length, ")");
292 foreach (immutable idx
, ref BookText
.Image img
; book
.images
) {
294 //writeln("image '", img.id, "' found");
295 lay
.putObject(new BookImage(book
, cast(uint)idx
));
301 void putParaContentInternal (Tag ct
, int boldc
=0, int italicc
=0, int underc
=0) {
302 if (ct
is null) return;
303 auto c
= lay
.fontStyle
.color
;
304 scope(exit
) lay
.fontStyle
.color
= c
;
307 case "strong": ++boldc
; lay
.fontStyle
.bold
= true; break;
308 case "emphasis": ++italicc
; lay
.fontStyle
.italic
= true; break;
309 case "image": putImage(ct
); return;
310 case "style": return;
312 if (ct
.href
.length
) {
313 import std
.conv
: to
;
314 //writeln("href found: '", ct.href, "' (", lay.nextWordIndex, ")");
315 meta
.hrefs
[lay
.nextWordIndex
] = BookMetadata
.Anc(ct
.href
.to
!dstring
, lay
.nextWordIndex
);
317 lay
.fontStyle
.href
= true;
318 lay
.fontStyle
.underline
= true;
319 lay
.fontStyle
.color
= colorTextHref
.asUint
;
322 case "": lay
.put(ct
.text
); return;
326 foreach (Tag tag
; ct
.children
) putParaContentInternal(tag
, boldc
, italicc
, underc
);
328 case "strong": if (--boldc
== 0) lay
.fontStyle
.bold
= false; break;
329 case "emphasis": if (--italicc
== 0) lay
.fontStyle
.italic
= false; break;
330 case "a": if (--underc
== 0) lay
.fontStyle
.underline
= false; lay
.fontStyle
.href
= false; break;
336 void putTagContents (Tag ct
) {
337 if (ct
is null) return;
339 foreach (Tag tag
; ct
.children
) putParaContentInternal(tag
);
343 void putParas (Tag ct
) {
345 foreach (Tag tag
; ct
.children
) {
346 if (tag
.name
.length
== 0) continue;
347 if (tag
.name
== "p") putTagContents(tag
);
348 else if (tag
.name
== "empty-line") lay
.endLine();
349 else if (tag
.name
== "text-author") {}
354 void putAuthor (Tag ct
) {
356 foreach (Tag tag
; ct
.children
) {
357 if (tag
.name
.length
== 0) continue;
358 if (tag
.name
== "text-author") {
365 void putCite (Tag ct
) {
367 scope(exit
) lay
.popStyles
;
373 void putEpigraph (Tag ct
) {
375 scope(exit
) lay
.popStyles
;
376 setEpigraphStyle(lay
);
381 void putStanza (Tag ct
) {
383 foreach (Tag tag
; ct
.children
) {
384 if (tag
.name
.length
== 0) continue;
385 if (tag
.name
== "text-author") continue;
386 if (tag
.name
== "title") badTag(tag
, "titles in stanzas are not supported yet");
387 if (tag
.name
== "subtitle") badTag(tag
, "subtitles in stanzas are not supported yet");
388 if (tag
.name
== "epigraph") badTag(tag
, "epigraphs in poems are not supported yet");
389 if (tag
.name
== "date") continue;
390 if (tag
.name
== "v") { putTagContents(tag
); continue; }
395 void putPoem (Tag ct
) {
398 scope(exit
) lay
.popStyles
;
400 // put epigraph (not yet)
401 // put title and subtitle
402 foreach (Tag tag
; ct
.children
) {
403 if (tag
.name
== "title") {
405 scope(exit
) lay
.popStyles
;
407 lay
.endPara(); // space
408 } else if (tag
.name
== "subtitle") {
410 scope(exit
) lay
.popStyles
;
412 //lay.endPara(); // space
416 foreach (Tag tag
; ct
.children
) {
417 if (tag
.name
.length
== 0) continue;
418 if (tag
.name
== "text-author") continue;
419 if (tag
.name
== "title") continue;
420 if (tag
.name
== "subtitle") continue;
421 if (tag
.name
== "epigraph") badTag(tag
, "epigraphs in poems are not supported yet");
422 if (tag
.name
== "date") continue;
423 if (tag
.name
== "stanza") { putStanza(tag
); lay
.put(LayText
.EndParaCh
); continue; }
429 void putSection (Tag sc
) {
430 bool sectionRegistered
= false;
432 void registerSection (Tag tag
) {
433 import std
.conv
: to
;
434 if (sectionRegistered
) return;
435 sectionRegistered
= true;
436 string text
= tag
.textContent
.xstrip
;
437 //lay.sections ~= lay.curWordIndex;
439 while (text
.length
) {
440 char ch
= text
.ptr
[0];
442 if (ch
<= ' ' || ch
== 127) {
443 if (name
.length
== 0) continue;
444 if (ch
!= '\n') ch
= ' ';
446 if (name
[$-1] > ' ') name
~= "\n...";
449 if (name
[$-1] > ' ') name
~= ' ';
456 if (name
.length
== 0) name
= "* * *";
457 meta
.sections
~= BookMetadata
.Anc(name
.to
!dstring
, lay
.nextWordIndex
);
461 foreach (Tag tag
; sc
.children
) {
462 if (tag
.name
.length
== 0) continue;
463 if (tag
.name
== "title") {
465 scope(exit
) lay
.popStyles
;
467 registerSection(tag
);
469 } else if (tag
.name
== "subtitle") {
471 scope(exit
) lay
.popStyles
;
472 setSubtitleStyle(lay
);
473 registerSection(tag
);
475 } else if (tag
.name
== "epigraph") {
477 scope(exit
) lay
.popStyles
;
478 setEpigraphStyle(lay
);
480 } else if (tag
.name
== "section") {
482 scope(exit
) lay
.popStyles
;
485 } else if (tag
.name
== "p") {
487 } else if (tag
.name
== "image") {
489 scope(exit
) lay
.popStyles
;
490 lay
.lineStyle
.leftpad
= 0;
491 lay
.lineStyle
.rightpad
= 0;
492 lay
.lineStyle
.paraIndent
= 0;
493 lay
.lineStyle
.setCenter
;
496 } else if (tag
.name
== "empty-line") {
498 } else if (tag
.name
== "cite") {
500 } else if (tag
.name
== "table") {
502 scope(exit
) lay
.popStyles
;
503 lay
.fontStyle
.fontsize
+= 8;
504 lay
.lineStyle
.setCenter
;
505 lay
.put("TABLE SKIPPED");
507 } else if (tag
.name
== "poem") {
509 } else if (tag
.name
== "image") {
510 } else if (tag
.name
== "style") {
518 foreach (Tag tag
; book
.content
.children
) {
519 if (tag
.name
== "title") {
521 scope(exit
) lay
.popStyles
;
526 } else if (tag
.name
== "subtitle") {
528 scope(exit
) lay
.popStyles
;
532 } else if (tag
.name
== "epigraph") {
534 } else if (tag
.name
== "section") {
536 scope(exit
) lay
.popStyles
;
539 } else if (tag
.name
== "image") {
550 // ////////////////////////////////////////////////////////////////////////// //
551 private __gshared LayFontStash laf
; // layouter font stash
554 // ////////////////////////////////////////////////////////////////////////// //
555 private void loadFmtFonts () {
556 laf
= new LayFontStash();
558 laf
.addFont("text", textFontName
);
559 laf
.addFont("texti", textiFontName
);
560 laf
.addFont("textb", textbFontName
);
561 laf
.addFont("textz", textzFontName
);
563 laf
.addFont("mono", monoFontName
);
564 laf
.addFont("monoi", monoiFontName
);
565 laf
.addFont("monob", monobFontName
);
566 laf
.addFont("monoz", monozFontName
);
570 // ////////////////////////////////////////////////////////////////////////// //
572 struct ReformatWork
{
573 shared(BookText
) booktext
;
578 struct ReformatWorkComplete
{
580 shared(BookText
) booktext
;
581 shared(LayText
) laytext
;
582 shared(BookMetadata
) meta
;
589 // ////////////////////////////////////////////////////////////////////////// //
590 void reformatThreadFn (Tid ownerTid
) {
595 int newW
= -1, newH
= -1;
602 //{ import std.stdio; writeln("reformat request received..."); }
603 book
= cast(BookText
)w
.booktext
;
604 newFileName
= w
.bookFileName
;
607 if (newW
< 1) newW
= 1;
608 if (newH
< 1) newH
= 1;
614 if (!doQuit
&& newW
> 0 && newH
> 0) {
617 //writeln("loading new book: '", newFileName, "'");
618 book
= loadBook(newFileName
);
621 int maxWidth
= newW
-4-2-BND_SCROLLBAR_WIDTH
-2;
622 if (maxWidth
< 64) maxWidth
= 64;
625 //writeln("layouting...");
626 auto stt
= MonoTime
.currTime
;
627 auto lay
= new LayText(laf
, maxWidth
);
628 lay
.fontStyle
.color
= colorText
.asUint
;
630 auto meta
= book
.formatBook(lay
);
632 auto ett
= MonoTime
.currTime
-stt
;
633 writeln("layouted in ", ett
.total
!"msecs", " milliseconds");
634 auto res
= ReformatWorkComplete(newW
, newH
, cast(shared)book
, cast(shared)lay
, cast(shared)meta
);
636 } catch (Throwable e
) {
637 // here, we are dead and fucked (the exact order doesn't matter)
638 import core
.stdc
.stdlib
: abort
;
639 import core
.stdc
.stdio
: fprintf
, stderr
;
640 import core
.memory
: GC
;
641 import core
.thread
: thread_suspendAll
;
642 GC
.disable(); // yeah
643 thread_suspendAll(); // stop right here, you criminal scum!
644 auto s
= e
.toString();
645 fprintf(stderr
, "\n=== FATAL ===\n%.*s\n", cast(uint)s
.length
, s
.ptr
);
646 abort(); // die, you bitch!
650 send(ownerTid
, QuitWork());