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/>.
22 import std
.concurrency
;
27 import arsd
.simpledisplay
;
33 import iv
.nanovg
.oui
.blendish
;
46 // ////////////////////////////////////////////////////////////////////////// //
47 BookText
loadBook (string fname
) {
48 __gshared
bool eliteShipsLoaded
= false;
51 //import core.memory : GC;
52 fname
= fname
.expandTilde
.absolutePath
;
53 //writeln("loading '", fname, "'...");
55 auto stt
= MonoTime
.currTime
;
56 auto book
= new BookText(fname
);
59 writeln("loaded: '", fname
, "' in ", (MonoTime
.currTime
-stt
).total
!"msecs", " milliseconds");
64 foreach (string line
; File(buildPath(RcDir
, ".lastfile")).byLineCopy
) {
68 while (lines
.length
&& lines
[$-1].isComment
) {
69 comments
~= lines
[$-1];
70 lines
= lines
[0..$-1];
74 } catch (Exception
) {}
75 try { import std
.file
; mkdirRecurse(RcDir
); } catch (Exception
) {}
76 auto fo
= File(buildPath(RcDir
, ".lastfile"), "w");
77 foreach (string s
; lines
) fo
.writeln(s
);
78 foreach_reverse (string s
; comments
) fo
.writeln(s
);
81 stateFileName
= buildPath(RcDir
, fname
.baseName
~".rc");
83 if (!eliteShipsLoaded
) {
85 eliteShipsLoaded
= true;
92 // ////////////////////////////////////////////////////////////////////////// //
93 private void formatBook (BookText book
, LayText lay
) {
94 assert(book
!is null);
96 void dumpTree (Tag ct
) {
98 writeln("tag: ", ct
.name
);
103 void putParaContentInternal (Tag ct
) {
104 if (ct
is null) return;
106 case "strong": lay
.put(LayText
.BoldOnCh
); break;
107 case "emphasis": lay
.put(LayText
.ItalicOnCh
); break;
108 case "image": return;
109 case "style": return;
113 case "": lay
.put(ct
.text
); return;
114 default: dumpTree(ct
); throw new Exception("invalid content tag: '"~ct
.name
~"'");
116 foreach (Tag tag
; ct
.children
) putParaContentInternal(tag
);
118 case "strong": lay
.put(LayText
.BoldOnCh
); break;
119 case "emphasis": lay
.put(LayText
.ItalicOnCh
); break;
124 void putTagContents (Tag ct
) {
125 if (ct
is null) return;
126 foreach (Tag tag
; ct
.children
) putParaContentInternal(tag
);
127 lay
.put(LayText
.EndParaCh
);
130 void putParas (Tag ct
) {
131 foreach (Tag tag
; ct
.children
) {
132 if (tag
.name
.length
== 0) continue;
133 if (tag
.name
== "p") {
135 } else if (tag
.name
== "empty-line") {
136 lay
.put(LayText
.EndLineCh
);
137 } else if (tag
.name
== "text-author") {
140 throw new Exception("invalid content tag: '"~tag
.name
~"'");
145 void putAuthor (Tag ct
) {
146 foreach (Tag tag
; ct
.children
) {
147 if (tag
.name
.length
== 0) continue;
148 if (tag
.name
== "text-author") {
149 lay
.put(LayText
.EndParaCh
);
151 lay
.put(LayText
.EndParaCh
);
156 void putCite (Tag ct
) {
157 lay
.put(LayText
.CiteCh
);
160 lay
.put(LayText
.NormalTextCh
);
163 void putEpigraph (Tag ct
) {
164 lay
.put(LayText
.EpigraphCh
);
167 lay
.put(LayText
.NormalTextCh
);
170 void putStanza (Tag ct
) {
171 foreach (Tag tag
; ct
.children
) {
172 if (tag
.name
.length
== 0) continue;
173 if (tag
.name
== "text-author") continue;
174 if (tag
.name
== "title") { dumpTree(ct
); throw new Exception("titles in stanzas are not supported yet"); }
175 if (tag
.name
== "subtitle") { dumpTree(ct
); throw new Exception("subtitles in stanzas are not supported yet"); }
176 if (tag
.name
== "epigraph") { dumpTree(ct
); throw new Exception("epigraphs in poems are not supported yet"); }
177 if (tag
.name
== "date") continue;
178 if (tag
.name
== "v") { putTagContents(tag
); continue; }
180 throw new Exception("invalid content tag: '"~tag
.name
~"'");
184 void putPoem (Tag ct
) {
185 lay
.put(LayText
.PoemCh
);
186 // put epigraph (not yet)
187 // put title and subtitle
188 foreach (Tag tag
; ct
.children
) {
189 if (tag
.name
== "title") {
190 lay
.size
= lay
.size
+4;
191 scope(exit
) lay
.size
= lay
.size
-4;
193 lay
.put(LayText
.EndParaCh
);
194 } else if (tag
.name
== "subtitle") {
195 lay
.size
= lay
.size
+2;
196 scope(exit
) lay
.size
= lay
.size
-2;
198 lay
.put(LayText
.EndParaCh
);
201 foreach (Tag tag
; ct
.children
) {
202 if (tag
.name
.length
== 0) continue;
203 if (tag
.name
== "text-author") continue;
204 if (tag
.name
== "title") continue;
205 if (tag
.name
== "subtitle") continue;
206 if (tag
.name
== "epigraph") { dumpTree(ct
); throw new Exception("epigraphs in poems are not supported yet"); }
207 if (tag
.name
== "date") continue;
208 if (tag
.name
== "stanza") { putStanza(tag
); lay
.put(LayText
.EndParaCh
); continue; }
210 throw new Exception("invalid content tag: '"~tag
.name
~"'");
213 lay
.put(LayText
.NormalTextCh
);
216 void putSection (Tag sc
) {
217 bool sectionRegistered
= false;
219 void registerSection (Tag tag
) {
220 import std
.conv
: to
;
221 if (sectionRegistered
) return;
222 sectionRegistered
= true;
223 string text
= tag
.textContent
.xstrip
;
224 lay
.sections
~= lay
.curWordIndex
;
226 while (text
.length
) {
227 char ch
= text
.ptr
[0];
229 if (ch
<= ' ' || ch
== 127) {
230 if (name
.length
== 0) continue;
231 if (ch
!= '\n') ch
= ' ';
233 if (name
[$-1] > ' ') name
~= "\n...";
236 if (name
[$-1] > ' ') name
~= ' ';
243 if (name
.length
== 0) name
= "* * *";
244 lay
.sectionNames
~= name
.to
!dstring
;
247 foreach (Tag tag
; sc
.children
) {
248 if (tag
.name
.length
== 0) continue;
249 if (tag
.name
== "title") {
250 lay
.size
= lay
.size
+4;
251 scope(exit
) lay
.size
= lay
.size
-4;
252 lay
.put(LayText
.TitleCh
);
253 registerSection(tag
);
255 lay
.put(LayText
.NormalTextCh
);
256 } else if (tag
.name
== "subtitle") {
257 lay
.size
= lay
.size
+2;
258 scope(exit
) lay
.size
= lay
.size
-2;
259 lay
.put(LayText
.SubtitleCh
);
260 registerSection(tag
);
262 lay
.put(LayText
.NormalTextCh
);
263 } else if (tag
.name
== "epigraph") {
265 } else if (tag
.name
== "section") {
266 lay
.put(LayText
.EndParaCh
);
268 } else if (tag
.name
== "p") {
270 } else if (tag
.name
== "empty-line") {
271 lay
.put(LayText
.EndParaCh
);
272 } else if (tag
.name
== "cite") {
274 } else if (tag
.name
== "table") {
275 lay
.put(LayText
.CiteCh
);
276 lay
.put("TABLE SKIPPED");
277 lay
.put(LayText
.NormalTextCh
);
278 } else if (tag
.name
== "poem") {
280 } else if (tag
.name
== "image") {
281 } else if (tag
.name
== "style") {
284 throw new Exception("invalid tag in section: '"~tag
.name
~"'");
289 foreach (Tag tag
; book
.content
.children
) {
290 if (tag
.name
== "title") {
291 lay
.size
= lay
.size
+4;
292 scope(exit
) lay
.size
= lay
.size
-4;
293 lay
.put(LayText
.EndParaCh
);
294 lay
.put(LayText
.EndParaCh
);
295 lay
.put(LayText
.TitleCh
);
297 lay
.put(LayText
.NormalTextCh
);
298 } else if (tag
.name
== "subtitle") {
299 lay
.size
= lay
.size
+2;
300 scope(exit
) lay
.size
= lay
.size
-2;
301 lay
.put(LayText
.SubtitleCh
);
303 lay
.put(LayText
.NormalTextCh
);
304 } else if (tag
.name
== "epigraph") {
306 } else if (tag
.name
== "section") {
315 // ////////////////////////////////////////////////////////////////////////// //
316 private __gshared LayoutFonts laf
; // layouter font stash
319 // ////////////////////////////////////////////////////////////////////////// //
320 private void loadFmtFonts () {
321 laf
= new LayoutFonts();
323 void loadFont (string name
, string path
) {
324 int fid
= laf
.fs
.fonsAddFont(name
, path
);
325 if (fid
< 0) throw new Exception("can't load font '"~name
~"' from '"~path
~"'");
328 loadFont("text", textFontName
);
329 loadFont("texti", textiFontName
);
330 loadFont("textb", textbFontName
);
331 loadFont("textz", textzFontName
);
333 loadFont("mono", monoFontName
);
334 loadFont("monoi", monoiFontName
);
335 loadFont("monob", monobFontName
);
336 loadFont("monoz", monozFontName
);
340 // ////////////////////////////////////////////////////////////////////////// //
342 struct ReformatWork
{
343 shared(BookText
) booktext
;
348 struct ReformatWorkComplete
{
350 shared(BookText
) booktext
;
351 shared(LayText
) laytext
;
358 // ////////////////////////////////////////////////////////////////////////// //
359 void reformatThreadFn (Tid ownerTid
) {
364 int newW
= -1, newH
= -1;
371 writeln("reformat request received...");
372 book
= cast(BookText
)w
.booktext
;
373 newFileName
= w
.bookFileName
;
376 if (newW
< 1) newW
= 1;
377 if (newH
< 1) newH
= 1;
383 if (!doQuit
&& newW
> 0 && newH
> 0) {
386 //writeln("loading new book: '", newFileName, "'");
387 book
= loadBook(newFileName
);
390 int maxWidth
= newW
-4-2-BND_SCROLLBAR_WIDTH
-2;
391 if (maxWidth
< 64) maxWidth
= 64;
394 //writeln("layouting...");
395 auto stt
= MonoTime
.currTime
;
396 auto lay
= new LayText(laf
, maxWidth
);
398 lay
.size
= fsizeText
;
399 lay
.normJustify
= optJustify
;
401 book
.formatBook(lay
);
403 writeln("layouted in ", (MonoTime
.currTime
-stt
).total
!"msecs", " milliseconds");
404 auto res
= ReformatWorkComplete(newW
, newH
, cast(shared)book
, cast(shared)lay
);
406 } catch (Throwable e
) {
407 // here, we are dead and fucked (the exact order doesn't matter)
408 import core
.stdc
.stdlib
: abort
;
409 import core
.stdc
.stdio
: fprintf
, stderr
;
410 import core
.memory
: GC
;
411 import core
.thread
: thread_suspendAll
;
412 GC
.disable(); // yeah
413 thread_suspendAll(); // stop right here, you criminal scum!
414 auto s
= e
.toString();
415 fprintf(stderr
, "\n=== FATAL ===\n%.*s\n", cast(uint)s
.length
, s
.ptr
);
416 abort(); // die, you bitch!
420 send(ownerTid
, QuitWork());