-
Notifications
You must be signed in to change notification settings - Fork 0
/
bk_paper.c
2852 lines (2516 loc) · 75.2 KB
/
bk_paper.c
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
/*
* Paper printing pre-backend for Halibut.
*
* This module does all the processing common to both PostScript
* and PDF output: selecting fonts, line wrapping and page breaking
* in accordance with font metrics, laying out the contents and
* index pages, generally doing all the page layout. After this,
* bk_ps.c and bk_pdf.c should only need to do linear translations
* into their literal output format.
*/
/*
* TODO in future work:
*
* - linearised PDF, perhaps?
*
* - I'm uncertain of whether I need to include a ToUnicode CMap
* in each of my font definitions in PDF. Currently things (by
* which I mean cut and paste out of acroread) seem to be
* working fairly happily without it, but I don't know.
*
* - rather than the ugly aux_text mechanism for rendering chapter
* titles, we could actually build the correct word list and
* wrap it as a whole.
*
* - get vertical font metrics and use them to position the PDF
* xref boxes more pleasantly
*
* - configurability
* * page header and footer should be configurable; we should
* be able to shift the page number elsewhere, and add other
* things such as the current chapter/section title and fixed
* text
* * remove the fixed mapping from heading levels to heading
* styles; offer a menu of styles from which the user can
* choose at every heading level
* * first-line indent in paragraphs
* * fixed text: `Contents', `Index', the colon-space and full
* stop in chapter title constructions
* * configurable location of contents?
* * certainly configurably _remove_ the contents, and possibly
* also the index
* * double-sided document switch?
* + means you have two header/footer formats which
* alternate
* + and means that mandatory page breaks before chapter
* titles should include a blank page if necessary to
* start the next section to a right-hand page
*
* - title pages
*
* - ability to use Type 1 fonts without AFM files
* * we need to parse the font to extract its metrics
*
* - character substitution for better typography?
* * use real ellipsis rather than ...
* * a hyphen in a word by itself might prefer to be an en-dash
* * (Americans might even want a convenient way to use an
* em-dash)
* * DON'T DO ANY OF THE ABOVE WITHIN \c OR \cw!
* * substituting `minus' for `hyphen' in the standard encoding
* is probably preferable in Courier, though certainly not in
* the main text font
* * if I do do this lot, I'm rather inclined to at least try
* to think up a configurable way to do it so that Americans
* can do em-dash tricks without my intervention and other
* people can do other odd things too.
*/
#include <assert.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include "halibut.h"
#include "paper.h"
typedef struct paper_conf_Tag paper_conf;
typedef struct paper_idx_Tag paper_idx;
typedef struct {
font_data *fonts[NFONTS];
int font_size;
} font_cfg;
struct paper_conf_Tag {
int paper_width;
int paper_height;
int left_margin;
int top_margin;
int right_margin;
int bottom_margin;
int indent_list_bullet;
int indent_list_after;
int indent_list;
int indent_quote;
int base_leading;
int base_para_spacing;
int chapter_top_space;
int sect_num_left_space;
int chapter_underline_depth;
int chapter_underline_thickness;
int rule_thickness;
font_cfg fbase, fcode, ftitle, fchapter, *fsect;
int nfsect;
int contents_indent_step;
int contents_margin;
int leader_separation;
int index_gutter;
int index_cols;
int index_minsep;
int pagenum_fontsize;
int footer_distance;
wchar_t *lquote, *rquote, *bullet;
wchar_t *contents_text, *index_text;
/* These are derived from the above */
int base_width;
int page_height;
int index_colwidth;
};
struct paper_idx_Tag {
/*
* Word list giving the page numbers on which this index entry
* appears. Also the last word in the list, for ease of
* construction.
*/
word *words;
word *lastword;
/*
* The last page added to the list (so we can ensure we don't
* add one twice).
*/
page_data *lastpage;
};
enum {
word_PageXref = word_NotWordType + 1
};
/* Flags for render_string() */
#define RS_NOLIG 1
static font_data *make_std_font(font_list *fontlist, psdata *psd,
const char *name);
static void wrap_paragraph(para_data *pdata, word *words,
int w, int i1, int i2, paper_conf *conf);
static page_data *page_breaks(line_data *first, line_data *last,
int page_height, int ncols, int headspace);
static int render_string(page_data *page, font_data *font, int fontsize,
int x, int y, wchar_t *str, unsigned flags);
static int render_line(line_data *ldata, int left_x, int top_y,
xref_dest *dest, keywordlist *keywords, indexdata *idx,
paper_conf *conf);
static void render_para(para_data *pdata, paper_conf *conf,
keywordlist *keywords, indexdata *idx,
paragraph *index_placeholder, page_data *index_page);
static int string_width(font_data *font, wchar_t const *string, bool *errs,
unsigned flags);
static int paper_width_simple(para_data *pdata, word *text, paper_conf *conf);
static para_data *code_paragraph(int indent, word *words, paper_conf *conf);
static para_data *rule_paragraph(int indent, paper_conf *conf);
static void add_rect_to_page(page_data *page, int x, int y, int w, int h);
static para_data *make_para_data(int ptype, int paux, int indent, int rmargin,
word *pkwtext, word *pkwtext2, word *pwords,
paper_conf *conf);
static void standard_line_spacing(para_data *pdata, paper_conf *conf);
static wchar_t *prepare_outline_title(word *first, wchar_t *separator,
word *second);
static word *fake_word(wchar_t *text);
static word *fake_space_word(void);
static word *fake_page_ref(page_data *page);
static word *fake_end_ref(void);
static word *prepare_contents_title(word *first, wchar_t *separator,
word *second);
static void fold_into_page(page_data *dest, page_data *src, int right_shift);
static bool fonts_ok(wchar_t *string, ...)
{
font_data *font;
va_list ap;
bool ret = true;
va_start(ap, string);
while ( (font = va_arg(ap, font_data *)) != NULL) {
bool errs;
(void) string_width(font, string, &errs, 0);
if (errs) {
ret = false;
break;
}
}
va_end(ap);
return ret;
}
static void paper_cfg_fonts(font_data **fonts, font_list *fontlist,
wchar_t *wp, filepos *fpos, psdata *psd,
errorstate *es) {
font_data *f;
char *fn;
int i;
for (i = 0; i < NFONTS && *wp; i++, wp = uadv(wp)) {
fn = utoa_dup(wp, CS_ASCII);
f = make_std_font(fontlist, psd, fn);
if (f)
fonts[i] = f;
else
/* FIXME: proper error */
err_nofont(es, fpos, wp);
}
}
static paper_conf paper_configure(paragraph *source, font_list *fontlist,
psdata *psd, errorstate *es) {
paragraph *p;
paper_conf ret;
/*
* Defaults.
*/
ret.paper_width = 595 * UNITS_PER_PT;
ret.paper_height = 842 * UNITS_PER_PT;
ret.left_margin = 72 * UNITS_PER_PT;
ret.top_margin = 72 * UNITS_PER_PT;
ret.right_margin = 72 * UNITS_PER_PT;
ret.bottom_margin = 108 * UNITS_PER_PT;
ret.indent_list_bullet = 6 * UNITS_PER_PT;
ret.indent_list_after = 18 * UNITS_PER_PT;
ret.indent_quote = 18 * UNITS_PER_PT;
ret.base_leading = UNITS_PER_PT;
ret.base_para_spacing = 10 * UNITS_PER_PT;
ret.chapter_top_space = 72 * UNITS_PER_PT;
ret.sect_num_left_space = 12 * UNITS_PER_PT;
ret.chapter_underline_depth = 14 * UNITS_PER_PT;
ret.chapter_underline_thickness = 3 * UNITS_PER_PT;
ret.rule_thickness = 1 * UNITS_PER_PT;
ret.fbase.font_size = 12;
ret.fbase.fonts[FONT_NORMAL] =
make_std_font(fontlist, psd, "Times-Roman");
ret.fbase.fonts[FONT_EMPH] =
make_std_font(fontlist, psd, "Times-Italic");
ret.fbase.fonts[FONT_STRONG] =
make_std_font(fontlist, psd, "Times-Bold");
ret.fbase.fonts[FONT_CODE] =
make_std_font(fontlist, psd, "Courier");
ret.fcode.font_size = 12;
ret.fcode.fonts[FONT_NORMAL] =
make_std_font(fontlist, psd, "Courier-Bold");
ret.fcode.fonts[FONT_EMPH] =
make_std_font(fontlist, psd, "Courier-Oblique");
ret.fcode.fonts[FONT_STRONG] =
make_std_font(fontlist, psd, "Courier-Bold");
ret.fcode.fonts[FONT_CODE] =
make_std_font(fontlist, psd, "Courier");
ret.ftitle.font_size = 24;
ret.ftitle.fonts[FONT_NORMAL] =
make_std_font(fontlist, psd, "Helvetica-Bold");
ret.ftitle.fonts[FONT_EMPH] =
make_std_font(fontlist, psd, "Helvetica-BoldOblique");
ret.ftitle.fonts[FONT_STRONG] =
make_std_font(fontlist, psd,"Helvetica-Bold");
ret.ftitle.fonts[FONT_CODE] =
make_std_font(fontlist, psd, "Courier-Bold");
ret.fchapter.font_size = 20;
ret.fchapter.fonts[FONT_NORMAL] =
make_std_font(fontlist, psd, "Helvetica-Bold");
ret.fchapter.fonts[FONT_EMPH] =
make_std_font(fontlist, psd,"Helvetica-BoldOblique");
ret.fchapter.fonts[FONT_STRONG] =
make_std_font(fontlist, psd,"Helvetica-Bold");
ret.fchapter.fonts[FONT_CODE] =
make_std_font(fontlist, psd, "Courier-Bold");
ret.nfsect = 3;
ret.fsect = snewn(ret.nfsect, font_cfg);
ret.fsect[0].font_size = 16;
ret.fsect[0].fonts[FONT_NORMAL] =
make_std_font(fontlist, psd, "Helvetica-Bold");
ret.fsect[0].fonts[FONT_EMPH] =
make_std_font(fontlist, psd,"Helvetica-BoldOblique");
ret.fsect[0].fonts[FONT_STRONG] =
make_std_font(fontlist, psd,"Helvetica-Bold");
ret.fsect[0].fonts[FONT_CODE] =
make_std_font(fontlist, psd, "Courier-Bold");
ret.fsect[1].font_size = 14;
ret.fsect[1].fonts[FONT_NORMAL] =
make_std_font(fontlist, psd, "Helvetica-Bold");
ret.fsect[1].fonts[FONT_EMPH] =
make_std_font(fontlist, psd, "Helvetica-BoldOblique");
ret.fsect[1].fonts[FONT_STRONG] =
make_std_font(fontlist, psd, "Helvetica-Bold");
ret.fsect[1].fonts[FONT_CODE] =
make_std_font(fontlist, psd, "Courier-Bold");
ret.fsect[2].font_size = 13;
ret.fsect[2].fonts[FONT_NORMAL] =
make_std_font(fontlist, psd, "Helvetica-Bold");
ret.fsect[2].fonts[FONT_EMPH] =
make_std_font(fontlist, psd, "Helvetica-BoldOblique");
ret.fsect[2].fonts[FONT_STRONG] =
make_std_font(fontlist, psd, "Helvetica-Bold");
ret.fsect[2].fonts[FONT_CODE] =
make_std_font(fontlist, psd, "Courier-Bold");
ret.contents_indent_step = 24 * UNITS_PER_PT;
ret.contents_margin = 84 * UNITS_PER_PT;
ret.leader_separation = 12 * UNITS_PER_PT;
ret.index_gutter = 36 * UNITS_PER_PT;
ret.index_cols = 2;
ret.index_minsep = 18 * UNITS_PER_PT;
ret.pagenum_fontsize = 12;
ret.footer_distance = 32 * UNITS_PER_PT;
ret.lquote = L"\x2018\0\x2019\0'\0'\0\0";
ret.rquote = uadv(ret.lquote);
ret.bullet = L"\x2022\0-\0\0";
ret.contents_text = L"Contents";
ret.index_text = L"Index";
/*
* Two-pass configuration so that we can pick up global config
* (e.g. `quotes') before having it overridden by specific
* config (`paper-quotes'), irrespective of the order in which
* they occur.
*/
for (p = source; p; p = p->next) {
if (p->type == para_Config) {
if (!ustricmp(p->keyword, L"quotes")) {
if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) {
ret.lquote = uadv(p->keyword);
ret.rquote = uadv(ret.lquote);
}
}
}
}
for (p = source; p; p = p->next) {
p->private_data = NULL;
if (p->type == para_Config) {
if (!ustricmp(p->keyword, L"paper-quotes")) {
if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) {
ret.lquote = uadv(p->keyword);
ret.rquote = uadv(ret.lquote);
}
} else if (!ustricmp(p->keyword, L"contents")) {
ret.contents_text = uadv(p->keyword);
} else if (!ustricmp(p->keyword, L"index")) {
ret.index_text = uadv(p->keyword);
} else if (!ustricmp(p->keyword, L"paper-bullet")) {
ret.bullet = uadv(p->keyword);
} else if (!ustricmp(p->keyword, L"paper-page-width")) {
ret.paper_width =
(int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-page-height")) {
ret.paper_height =
(int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-left-margin")) {
ret.left_margin =
(int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-top-margin")) {
ret.top_margin =
(int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-right-margin")) {
ret.right_margin =
(int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-bottom-margin")) {
ret.bottom_margin =
(int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-list-indent")) {
ret.indent_list_bullet =
(int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-listitem-indent")) {
ret.indent_list =
(int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-quote-indent")) {
ret.indent_quote =
(int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-base-leading")) {
ret.base_leading =
(int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-base-para-spacing")) {
ret.base_para_spacing =
(int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-chapter-top-space")) {
ret.chapter_top_space =
(int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-sect-num-left-space")) {
ret.sect_num_left_space =
(int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-chapter-underline-depth")) {
ret.chapter_underline_depth =
(int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-chapter-underline-thickness")) {
ret.chapter_underline_thickness =
(int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-rule-thickness")) {
ret.rule_thickness =
(int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-contents-indent-step")) {
ret.contents_indent_step =
(int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-contents-margin")) {
ret.contents_margin =
(int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-leader-separation")) {
ret.leader_separation =
(int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-index-gutter")) {
ret.index_gutter =
(int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-index-minsep")) {
ret.index_minsep =
(int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-footer-distance")) {
ret.footer_distance =
(int) 0.5 + FUNITS_PER_PT * utof(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-base-font-size")) {
ret.fbase.font_size = utoi(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-index-columns")) {
ret.index_cols = utoi(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-pagenum-font-size")) {
ret.pagenum_fontsize = utoi(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-base-fonts")) {
paper_cfg_fonts(ret.fbase.fonts, fontlist, uadv(p->keyword),
&p->fpos, psd, es);
} else if (!ustricmp(p->keyword, L"paper-code-font-size")) {
ret.fcode.font_size = utoi(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-code-fonts")) {
paper_cfg_fonts(ret.fcode.fonts, fontlist, uadv(p->keyword),
&p->fpos, psd, es);
} else if (!ustricmp(p->keyword, L"paper-title-font-size")) {
ret.ftitle.font_size = utoi(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-title-fonts")) {
paper_cfg_fonts(ret.ftitle.fonts, fontlist, uadv(p->keyword),
&p->fpos, psd, es);
} else if (!ustricmp(p->keyword, L"paper-chapter-font-size")) {
ret.fchapter.font_size = utoi(uadv(p->keyword));
} else if (!ustricmp(p->keyword, L"paper-chapter-fonts")) {
paper_cfg_fonts(ret.fchapter.fonts, fontlist, uadv(p->keyword),
&p->fpos, psd, es);
} else if (!ustricmp(p->keyword, L"paper-section-font-size")) {
wchar_t *q = uadv(p->keyword);
int n = 0;
if (uisdigit(*q)) {
n = utoi(q);
q = uadv(q);
}
if (n >= ret.nfsect) {
int i;
ret.fsect = sresize(ret.fsect, n+1, font_cfg);
for (i = ret.nfsect; i <= n; i++)
ret.fsect[i] = ret.fsect[ret.nfsect-1];
ret.nfsect = n+1;
}
ret.fsect[n].font_size = utoi(q);
} else if (!ustricmp(p->keyword, L"paper-section-fonts")) {
wchar_t *q = uadv(p->keyword);
int n = 0;
if (uisdigit(*q)) {
n = utoi(q);
q = uadv(q);
}
if (n >= ret.nfsect) {
int i;
ret.fsect = sresize(ret.fsect, n+1, font_cfg);
for (i = ret.nfsect; i <= n; i++)
ret.fsect[i] = ret.fsect[ret.nfsect-1];
ret.nfsect = n+1;
}
paper_cfg_fonts(ret.fsect[n].fonts, fontlist, q, &p->fpos,
psd, es);
}
}
}
/*
* Set up the derived fields in the conf structure.
*/
ret.base_width =
ret.paper_width - ret.left_margin - ret.right_margin;
ret.page_height =
ret.paper_height - ret.top_margin - ret.bottom_margin;
ret.indent_list = ret.indent_list_bullet + ret.indent_list_after;
ret.index_colwidth =
(ret.base_width - (ret.index_cols-1) * ret.index_gutter)
/ ret.index_cols;
/*
* Now process fallbacks on quote characters and bullets. We
* use string_width() to determine whether all of the relevant
* fonts contain the same character, and fall back whenever we
* find a character which not all of them support.
*/
/* Quote characters need not be supported in the fixed code fonts,
* but must be in the title and body fonts. */
while (*uadv(ret.rquote) && *uadv(uadv(ret.rquote))) {
int n;
if (fonts_ok(ret.lquote,
ret.fbase.fonts[FONT_NORMAL],
ret.fbase.fonts[FONT_EMPH],
ret.fbase.fonts[FONT_STRONG],
ret.ftitle.fonts[FONT_NORMAL],
ret.ftitle.fonts[FONT_EMPH],
ret.ftitle.fonts[FONT_STRONG],
ret.fchapter.fonts[FONT_NORMAL],
ret.fchapter.fonts[FONT_EMPH],
ret.fchapter.fonts[FONT_STRONG], NULL) &&
fonts_ok(ret.rquote,
ret.fbase.fonts[FONT_NORMAL],
ret.fbase.fonts[FONT_EMPH],
ret.fbase.fonts[FONT_STRONG],
ret.ftitle.fonts[FONT_NORMAL],
ret.ftitle.fonts[FONT_EMPH],
ret.ftitle.fonts[FONT_STRONG],
ret.fchapter.fonts[FONT_NORMAL],
ret.fchapter.fonts[FONT_EMPH],
ret.fchapter.fonts[FONT_STRONG], NULL)) {
for (n = 0; n < ret.nfsect; n++)
if (!fonts_ok(ret.lquote,
ret.fsect[n].fonts[FONT_NORMAL],
ret.fsect[n].fonts[FONT_EMPH],
ret.fsect[n].fonts[FONT_STRONG], NULL) ||
!fonts_ok(ret.rquote,
ret.fsect[n].fonts[FONT_NORMAL],
ret.fsect[n].fonts[FONT_EMPH],
ret.fsect[n].fonts[FONT_STRONG], NULL))
break;
if (n == ret.nfsect)
break;
}
ret.lquote = uadv(ret.rquote);
ret.rquote = uadv(ret.lquote);
}
/* The bullet character only needs to be supported in the normal body
* font (not even in italics). */
while (*ret.bullet && *uadv(ret.bullet) &&
!fonts_ok(ret.bullet, ret.fbase.fonts[FONT_NORMAL], NULL))
ret.bullet = uadv(ret.bullet);
return ret;
}
void *paper_pre_backend(paragraph *sourceform, keywordlist *keywords,
indexdata *idx, psdata *psd, errorstate *es) {
paragraph *p;
document *doc;
int indent;
bool used_contents;
para_data *pdata, *firstpara = NULL, *lastpara = NULL;
para_data *firstcont, *lastcont;
line_data *firstline, *lastline, *firstcontline, *lastcontline;
page_data *pages;
font_list *fontlist;
paper_conf *conf, ourconf;
bool has_index;
int pagenum;
paragraph index_placeholder_para;
page_data *first_index_page = NULL;
init_std_fonts(psd);
fontlist = snew(font_list);
fontlist->head = fontlist->tail = NULL;
ourconf = paper_configure(sourceform, fontlist, psd, es);
conf = &ourconf;
/*
* Set up a data structure to collect page numbers for each
* index entry.
*/
{
int i;
indexentry *entry;
has_index = false;
for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) {
paper_idx *pi = snew(paper_idx);
has_index = true;
pi->words = pi->lastword = NULL;
pi->lastpage = NULL;
entry->backend_data = pi;
}
}
/*
* Format the contents entry for each heading.
*/
{
word *contents_title;
contents_title = fake_word(conf->contents_text);
firstcont = make_para_data(para_UnnumberedChapter, 0, 0, 0,
NULL, NULL, contents_title, conf);
lastcont = firstcont;
lastcont->next = NULL;
firstcontline = firstcont->first;
lastcontline = lastcont->last;
for (p = sourceform; p; p = p->next) {
word *words;
int indent;
switch (p->type) {
case para_Chapter:
case para_Appendix:
case para_UnnumberedChapter:
case para_Heading:
case para_Subsect:
switch (p->type) {
case para_Chapter:
case para_Appendix:
words = prepare_contents_title(p->kwtext, L": ", p->words);
indent = 0;
break;
case para_UnnumberedChapter:
words = prepare_contents_title(NULL, NULL, p->words);
indent = 0;
break;
case para_Heading:
case para_Subsect:
words = prepare_contents_title(p->kwtext2, L" ", p->words);
indent = (p->aux + 1) * conf->contents_indent_step;
break;
}
pdata = make_para_data(para_Normal, p->aux, indent,
conf->contents_margin,
NULL, NULL, words, conf);
pdata->next = NULL;
pdata->contents_entry = p;
lastcont->next = pdata;
lastcont = pdata;
/*
* Link all contents line structures together into
* a big list.
*/
if (pdata->first) {
if (lastcontline) {
lastcontline->next = pdata->first;
pdata->first->prev = lastcontline;
} else {
firstcontline = pdata->first;
pdata->first->prev = NULL;
}
lastcontline = pdata->last;
lastcontline->next = NULL;
}
break;
}
}
/*
* And one extra one, for the index.
*/
if (has_index) {
pdata = make_para_data(para_Normal, 0, 0,
conf->contents_margin,
NULL, NULL,
fake_word(conf->index_text), conf);
pdata->next = NULL;
pdata->contents_entry = &index_placeholder_para;
lastcont->next = pdata;
lastcont = pdata;
if (pdata->first) {
if (lastcontline) {
lastcontline->next = pdata->first;
pdata->first->prev = lastcontline;
} else {
firstcontline = pdata->first;
pdata->first->prev = NULL;
}
lastcontline = pdata->last;
lastcontline->next = NULL;
}
}
}
/*
* Do the main paragraph formatting.
*/
indent = 0;
used_contents = false;
firstline = lastline = NULL;
for (p = sourceform; p; p = p->next) {
p->private_data = NULL;
switch (p->type) {
/*
* These paragraph types are either invisible or don't
* define text in the normal sense. Either way, they
* don't require wrapping.
*/
case para_IM:
case para_BR:
case para_Biblio:
case para_NotParaType:
case para_Config:
case para_VersionID:
case para_NoCite:
break;
/*
* These paragraph types don't require wrapping, but
* they do affect the line width to which we wrap the
* rest of the paragraphs, so we need to pay attention.
*/
case para_LcontPush:
indent += conf->indent_list; break;
case para_LcontPop:
indent -= conf->indent_list; assert(indent >= 0); break;
case para_QuotePush:
indent += conf->indent_quote; break;
case para_QuotePop:
indent -= conf->indent_quote; assert(indent >= 0); break;
/*
* This paragraph type is special. Process it
* specially.
*/
case para_Code:
pdata = code_paragraph(indent, p->words, conf);
p->private_data = pdata;
if (pdata->first != pdata->last) {
pdata->first->penalty_after += 100000;
pdata->last->penalty_before += 100000;
}
break;
/*
* This paragraph is also special.
*/
case para_Rule:
pdata = rule_paragraph(indent, conf);
p->private_data = pdata;
break;
/*
* All of these paragraph types require wrapping in the
* ordinary way. So we must supply a set of fonts, a
* line width and auxiliary information (e.g. bullet
* text) for each one.
*/
case para_Chapter:
case para_Appendix:
case para_UnnumberedChapter:
case para_Heading:
case para_Subsect:
case para_Normal:
case para_BiblioCited:
case para_Bullet:
case para_NumberedList:
case para_DescribedThing:
case para_Description:
case para_Copyright:
case para_Title:
pdata = make_para_data(p->type, p->aux, indent, 0,
p->kwtext, p->kwtext2, p->words, conf);
p->private_data = pdata;
break;
}
if (p->private_data) {
pdata = (para_data *)p->private_data;
/*
* If this is the first non-title heading, we link the
* contents section in before it.
*/
if (!used_contents && pdata->outline_level > 0) {
used_contents = true;
if (lastpara)
lastpara->next = firstcont;
else
firstpara = firstcont;
lastpara = lastcont;
assert(lastpara->next == NULL);
if (lastline) {
lastline->next = firstcontline;
firstcontline->prev = lastline;
} else {
firstline = firstcontline;
firstcontline->prev = NULL;
}
assert(lastcontline != NULL);
lastline = lastcontline;
lastline->next = NULL;
}
/*
* Link all line structures together into a big list.
*/
if (pdata->first) {
if (lastline) {
lastline->next = pdata->first;
pdata->first->prev = lastline;
} else {
firstline = pdata->first;
pdata->first->prev = NULL;
}
lastline = pdata->last;
lastline->next = NULL;
}
/*
* Link all paragraph structures together similarly.
*/
pdata->next = NULL;
if (lastpara)
lastpara->next = pdata;
else
firstpara = pdata;
lastpara = pdata;
}
}
/*
* Now we have an enormous linked list of every line of text in
* the document. Break it up into pages.
*/
pages = page_breaks(firstline, lastline, conf->page_height, 0, 0);
/*
* Number the pages.
*/
{
char buf[40];
page_data *page;
pagenum = 0;
for (page = pages; page; page = page->next) {
sprintf(buf, "%d", ++pagenum);
page->number = ufroma_dup(buf, CS_ASCII);
}
if (has_index) {
first_index_page = snew(page_data);
first_index_page->next = first_index_page->prev = NULL;
first_index_page->first_line = NULL;
first_index_page->last_line = NULL;
first_index_page->first_text = first_index_page->last_text = NULL;
first_index_page->first_xref = first_index_page->last_xref = NULL;
first_index_page->first_rect = first_index_page->last_rect = NULL;
/* And don't forget the as-yet-uncreated index. */
sprintf(buf, "%d", ++pagenum);
first_index_page->number = ufroma_dup(buf, CS_ASCII);
}
}
/*
* Now we're ready to actually lay out the pages. We do this by
* looping over _paragraphs_, since we may need to track cross-
* references between lines and even across pages.
*/
for (pdata = firstpara; pdata; pdata = pdata->next)
render_para(pdata, conf, keywords, idx,
&index_placeholder_para, first_index_page);
/*
* Now we've laid out the main body pages, we should have
* acquired a full set of page numbers for the index.
*/
if (has_index) {
int i;
indexentry *entry;
word *index_title;
para_data *firstidx, *lastidx;
line_data *firstidxline, *lastidxline, *ldata;
page_data *ipages, *ipages2, *page;
/*
* Create a set of paragraphs for the index.
*/
index_title = fake_word(conf->index_text);
firstidx = make_para_data(para_UnnumberedChapter, 0, 0, 0,
NULL, NULL, index_title, conf);
lastidx = firstidx;
lastidx->next = NULL;
firstidxline = firstidx->first;
lastidxline = lastidx->last;
for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) {
paper_idx *pi = (paper_idx *)entry->backend_data;
para_data *text, *pages;
if (!pi->words)
continue;
text = make_para_data(para_Normal, 0, 0,
conf->base_width - conf->index_colwidth,
NULL, NULL, entry->text, conf);
pages = make_para_data(para_Normal, 0, 0,
conf->base_width - conf->index_colwidth,
NULL, NULL, pi->words, conf);
text->justification = LEFT;
pages->justification = RIGHT;
text->last->space_after = pages->first->space_before =
conf->base_leading / 2;
pages->last->space_after = text->first->space_before =
conf->base_leading;
assert(text->first);
assert(pages->first);
assert(lastidxline);
assert(lastidx);
/*
* If feasible, fold the two halves of the index entry
* together.
*/
if (text->last->real_shortfall + pages->first->real_shortfall >
conf->index_colwidth + conf->index_minsep) {
text->last->space_after = -1;
pages->first->space_before = -pages->first->line_height+1;
}
lastidx->next = text;
text->next = pages;
pages->next = NULL;
lastidx = pages;
/*
* Link all index line structures together into
* a big list.
*/
text->last->next = pages->first;
pages->first->prev = text->last;
lastidxline->next = text->first;
text->first->prev = lastidxline;
lastidxline = pages->last;
/*
* Breaking an index entry anywhere is so bad that I
* think I'm going to forbid it totally.
*/
for (ldata = text->first; ldata && ldata->next;
ldata = ldata->next) {
ldata->next->space_before += ldata->space_after + 1;
ldata->space_after = -1;
}
}
/*
* Now break the index into pages.
*/
ipages = page_breaks(firstidxline, firstidxline, conf->page_height,
0, 0);
ipages2 = page_breaks(firstidxline->next, lastidxline,
conf->page_height,
conf->index_cols,
firstidxline->space_before +
firstidxline->line_height +
firstidxline->space_after);
/*
* This will have put each _column_ of the index on a
* separate page, which isn't what we want. Fold the pages
* back together.
*/
page = ipages2;
while (page) {
int i;
for (i = 1; i < conf->index_cols; i++)
if (page->next) {
page_data *tpage;
fold_into_page(page, page->next,
i * (conf->index_colwidth +
conf->index_gutter));
tpage = page->next;
page->next = page->next->next;
if (page->next)
page->next->prev = page;
sfree(tpage);
}
page = page->next;
}
/* Also fold the heading on to the same page as the index items. */
fold_into_page(ipages, ipages2, 0);
ipages->next = ipages2->next;
if (ipages->next)
ipages->next->prev = ipages;