/
main.c
3629 lines (3072 loc) · 104 KB
/
main.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
/*
* main.c
*
* Implementation of 'mangl' - a graphical man page viewer
*
* Initial commit: 2019-09-07
*
* Author: Ziga Lenarcic <ziga.lenarcic@gmail.com>
*/
#include <sys/types.h>
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <math.h>
#include <sys/stat.h>
#include <glob.h>
#include <getopt.h>
#include <err.h>
#include <stdbool.h>
#include <ctype.h>
#ifndef __APPLE__
#include <GL/gl.h>
#endif
#ifdef __APPLE__
#include <OpenGL/gl.h>
#endif
#include <GLFW/glfw3.h>
#include "ft2build.h"
#include FT_FREETYPE_H
#include "stretchy_buffer.h"
#include "hashmap.h"
#include "manpath.h"
#include "icon.h"
#include "mandoc/mandoc.h"
#include "mandoc/roff.h"
#include "mandoc/mandoc_parse.h"
#include "mandoc/manconf.h"
#include "mandoc/out.h"
#include "mandoc/mandoc_aux.h"
#include "mandoc/term.h"
#define MANGL_VERSION_MAJOR 1
#define MANGL_VERSION_MINOR 1
#define MANGL_VERSION_PATCH 5
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
#define ZMALLOC(type, n) ((type *)calloc(n, sizeof(type)))
#define MAX(x,y) (((x) > (y)) ? (x) : (y))
#define MIN(x,y) (((x) < (y)) ? (x) : (y))
static const struct option longopts[] =
{
{"no-fork", no_argument, NULL, 'f'},
{"help", no_argument, NULL, 'h'},
{"local-file", no_argument, NULL, 'l'},
{"version", no_argument, NULL, 'V'},
{NULL, 0, NULL, 0},
};
static const char * const default_man_paths[] = {
"/usr/share/man",
"/usr/X11R6/man",
"/usr/local/man"
};
enum DISPLAY_MODES {
D_MANPAGE = 0,
D_SEARCH = 1
};
enum DIMENSIONS {
DIM_SCROLLBAR_WIDTH = 0,
DIM_SCROLLBAR_THUMB_MARGIN,
DIM_SCROLLBAR_THUMB_MIN_HEIGHT,
DIM_DOCUMENT_MARGIN,
DIM_SEARCH_WIDTH,
DIM_SCROLL_AMOUNT,
DIM_GUI_PADDING,
DIM_TEXT_HORIZONTAL_MARGIN,
};
static const int dimensions[] =
{
/*scrollbar_width */ 12,
/*scrollbar_thumb_margin */ 0,
/*scrollbar_thumb_min_height*/ 20,
/*document_margin */ 29,
/*search_width */ 300,
/*scroll amount */ 40,
/*gui padding */ 9,
/*text horizontal margin */ 4,
};
#define FONT_TEXTURE_SIZE 256
#define FONT_IMAGE_WIDTH 112
#define FONT_IMAGE_HEIGHT 84
#define FONT_CHAR_WIDTH 7
#define FONT_CHAR_HEIGHT 14
#include "font_image.h"
typedef struct {
int x;
int y;
int x2;
int y2;
} recti;
typedef struct {
recti document_rectangle;
int highlight;
char link[1024];
char pwd[1024];
} link_t;
typedef struct {
recti document_rectangle;
} search_t;
typedef struct CharDescription_
{
int available;
float tex_coord0_x;
float tex_coord0_y;
float tex_coord1_x;
float tex_coord1_y;
int width;
int height;
int top;
int left;
int advance;
} CharDescription;
typedef struct FontData_
{
uint8_t *bitmap;
int bitmap_width;
int bitmap_height;
CharDescription chars[128];
int character_width;
int character_height;
int line_height;
double font_size;
GLuint texture_id;
} FontData;
FontData builtinFont = {
.bitmap = NULL,
.bitmap_width = 0,
.bitmap_height = 0,
.character_width = 6, // actual width and height
.character_height = 9,
.line_height = FONT_CHAR_HEIGHT,
.font_size = 10.0,
.texture_id = 0
};
FontData *mainFont = &builtinFont;
FontData *loadedFont;
struct {
char font_file[512];
int font_size;
double gui_scale;
double line_spacing;
int line_length;
int current_line_length;
} settings = { .font_size = 10, .gui_scale = 1.0, .line_spacing = 1.0, .line_length = 78,
.current_line_length = 78};
int display_mode = D_SEARCH;
char search_term[512];
char **manpage_names;
char **manpage_names_lower;
#define N_SHOWN_RESULTS 12
int results_selected_index = 0;
int results_shown_lines = N_SHOWN_RESULTS;
int results_view_offset = 0;
struct {
int idx;
int goodness;
} matches[100];
int matches_count = 0;
int scrollbar_thumb_position;
int scrollbar_thumb_size;
int scrollbar_thumb_hover;
int scrollbar_dragging = 0;
int scrollbar_thumb_mouse_down_y = 0;
int scrollbar_thumb_mouse_down_thumb_position = 0;
int initial_window_rows = 40;
int line_length_toggle;
GLFWwindow *window;
int window_width;
int window_height;
double mouse_x = 0.0;
double mouse_y = 0.0;
bool redisplay_needed = false;
map_t manpage_database;
map_t manpage_database_pwd;
FT_Library library;
void update_window_title(void);
void terminal_mdoc(void *, const struct roff_meta *);
void terminal_man(void *, const struct roff_meta *);
void exit_program(int code)
{
if (window)
glfwDestroyWindow(window);
glfwTerminate();
exit(code);
}
double clamp(double val, double min, double max)
{
if (val > max) return max;
if (val < min) return min;
return val;
}
bool inside_recti(recti r, int x, int y)
{
return (x >= r.x) && (y >= r.y) && (x < r.x2) && (y < r.y2);
}
unsigned round_to_power_of_2(unsigned x)
{
for (int i = 31; i >= 0; i--)
{
if (x == (1 << i))
return 1 << i;
if ((x & (1 << i)) != 0)
return 1 << (i + 1);
}
return 0;
}
int get_font_file(char *font)
{
FILE *f = fopen(font, "rb");
if (f)
{
fclose(f);
return 1;
}
char cmd[512];
snprintf(cmd, sizeof(cmd), "fc-match --format=%%{file} \"%s\"", font);
FILE *proc = popen(cmd, "r");
int proc_fd = fileno(proc);
/* wait for output */
fd_set fd_s;
FD_ZERO(&fd_s);
FD_SET(proc_fd, &fd_s);
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 100 * 1000;
int ret = select(proc_fd + 1, &fd_s, NULL, NULL, &timeout);
if (ret > 0)
{
/* read the output */
if (proc != NULL)
{
char tmp_out[1024];
char *ptr = fgets(tmp_out, sizeof(tmp_out), proc);
pclose(proc);
if (ptr)
{
strcpy(font, ptr);
return 1;
}
}
}
return 0;
}
void copy_bitmap(uint8_t *dst, int w_dst, int h_dst, int x, int y, const uint8_t *src, int w_src, int h_src, int pitch_src)
{
for (int j = 0; j < h_src; j++)
{
if ((j + y) >= h_dst)
break;
int bytes_to_copy = w_src;
if ((bytes_to_copy + x) > w_dst)
{
bytes_to_copy = w_dst - x; // clip
}
memcpy(&dst[(j + y) * w_dst + x], &src[j * pitch_src], bytes_to_copy);
}
}
void copy_bitmap_1bit(uint8_t *dst, int w_dst, int h_dst, int x, int y, const uint8_t *src, int w_src, int h_src, int pitch_src)
{
for (int j = 0; j < h_src; j++)
{
if ((j + y) >= h_dst)
break;
for (int i = 0; i < w_src; i++)
{
if ((x + i) >= w_dst)
break;
int byte = i / 8;
int bit = 7 - (i % 8);
dst[(j + y) * w_dst + x + i] = (src[j * pitch_src + byte] & (1U << bit)) ? 255 : 0;
}
}
}
int get_dimension(int dimension_index)
{
/* at 10 pt font is 6x9 pixels */
switch (dimension_index)
{
case DIM_DOCUMENT_MARGIN:
case DIM_SCROLL_AMOUNT:
case DIM_GUI_PADDING:
{
double font_scale = mainFont->character_height / 9.0;
return (int)(font_scale * dimensions[dimension_index]);
}
case DIM_SEARCH_WIDTH:
case DIM_TEXT_HORIZONTAL_MARGIN:
{
double font_horizontal_scale = mainFont->character_width / 6.0;
return (int)(font_horizontal_scale * dimensions[dimension_index]);
}
case DIM_SCROLLBAR_WIDTH:
case DIM_SCROLLBAR_THUMB_MARGIN:
case DIM_SCROLLBAR_THUMB_MIN_HEIGHT:
default:
return (int)(settings.gui_scale * dimensions[dimension_index]);
}
}
int init_freetype(void)
{
int error = FT_Init_FreeType(&library);
if (error)
{
fprintf(stderr, "Failed to initialize freetype\n");
exit(EXIT_FAILURE);
}
return 0;
}
int render_font_texture(const char *font_file, int font_size_px)
{
FT_Face face;
FILE *f = fopen(font_file, "rb");
if (f == NULL)
{
fprintf(stderr, "Font file missing: \"%s\"\n", font_file);
return -1;
}
else
{
fclose(f);
}
int error = FT_New_Face(library, font_file, 0, &face);
if (error == FT_Err_Unknown_File_Format)
{
fprintf(stderr, "Unknown font format: %s\n", font_file);
return -1;
}
else if (error)
{
fprintf(stderr, "File not found: %s\n", font_file);
return -1;
}
else
{
//printf("Font \"%s\" loaded\n", font_file);
}
//error = FT_Set_Pixel_Sizes(face, 0, font_size_px);
error = FT_Set_Char_Size(face, 0, font_size_px * 64, 96, 96);
if (error)
{
fprintf(stderr, "FT_Set_Char_Size error: %d\n", error);
return -1;
}
int font_width = 0;
int font_height = 0;
/* get character size of X, H */
const char *str = "XH";
while (*str)
{
int glyph_index = FT_Get_Char_Index(face, *str);
str++;
if (glyph_index > 0)
{
FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
error = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
if (error)
{
fprintf(stderr, "FT_Render_Glyph: Glyph render error: %d\n", error);
}
FT_Bitmap *bmp = &face->glyph->bitmap;
font_width = MAX(font_width, bmp->width);
font_height = MAX(font_height, bmp->rows);
}
}
if ((font_width <= 0) || (font_height <= 0))
{
fprintf(stderr, "Failed to determine font size\n");
return -1;
}
/* now allocate a texture of a good size */
FontData *font = ZMALLOC(FontData, 1);
font->font_size = font_size_px;
font->character_width = font_width;
font->character_height = font_height;
font->line_height = face->size->metrics.height / 64 + 1; /* add 1 px of line height to make text more breathy */
font->bitmap_width = round_to_power_of_2(16 * (font_width + 2));
font->bitmap_height = round_to_power_of_2(6 * (font_height * 2));
font->bitmap_width = MAX(font->bitmap_width, font->bitmap_height);
font->bitmap_height = MAX(font->bitmap_width, font->bitmap_height);
font->bitmap = ZMALLOC(uint8_t, font->bitmap_width * font->bitmap_height);
for (int i = 32; i < 128; i++)
{
int glyph_index = FT_Get_Char_Index(face, i);
if (glyph_index > 0)
{
FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
error = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
if (error)
{
fprintf(stderr, "FT_Render_Glyph: Glyph render error: %d\n", error);
continue;
}
FT_Bitmap *bmp = &face->glyph->bitmap;
int w = bmp->width;
int h = bmp->rows;
int pitch = bmp->pitch;
int left = face->glyph->bitmap_left;
int top = face->glyph->bitmap_top;
//printf("w %d h %d pitch %d left %d top %d\n", w, h, pitch, left, top);
int col = i % 16;
int row = i / 16;
int dst_x = col * (font_width + 2) + left + 1;
int dst_y = row * (font_height * 2) - top;
font->chars[i].available = 1;
font->chars[i].top = top;
font->chars[i].left = left;
font->chars[i].width = w;
font->chars[i].height = h;
font->chars[i].advance = face->glyph->advance.x / 64;
const float pixel_x = 1.0f / font->bitmap_width;
const float pixel_y = 1.0f / font->bitmap_height;
font->chars[i].tex_coord0_x = pixel_x * dst_x;
font->chars[i].tex_coord0_y = pixel_y * dst_y;
font->chars[i].tex_coord1_x = pixel_x * (dst_x + w);
font->chars[i].tex_coord1_y = pixel_y * (dst_y + h);
if (bmp->pixel_mode == FT_PIXEL_MODE_GRAY)
copy_bitmap(font->bitmap, font->bitmap_width, font->bitmap_height, dst_x, dst_y, bmp->buffer, w, h, pitch);
else if (bmp->pixel_mode == FT_PIXEL_MODE_MONO)
{
// monochrome
copy_bitmap_1bit(font->bitmap, font->bitmap_width, font->bitmap_height, dst_x, dst_y, bmp->buffer, w, h, pitch);
}
else
{
fprintf(stderr, "Unsupported pixel mode (not 8 bit or 1 bit)\n");
}
}
else
{
//printf("no glyph for %d\n", i);
continue;
}
}
FT_Done_Face(face);
loadedFont = font;
return 0;
}
void print_usage(const char *exe)
{
fprintf(stderr, "Usage: mangl [OPTION]... [[SECTION] PAGE]\n");
fprintf(stderr, "Display the manpage PAGE in section SECTION in a graphical application\n");
fprintf(stderr, "or open the application with the search screen.\n");
fprintf(stderr, "\n");
fprintf(stderr, " -f, --no-fork don't fork the GUI\n");
fprintf(stderr, " -h, --help print usage\n");
fprintf(stderr, " -V, --version print version and quit\n");
fprintf(stderr, " -l, --local-file interpret the PAGE argument as a local filename\n");
fprintf(stderr, "\n");
fprintf(stderr, "Report bugs to ziga.lenarcic@gmail.com.\n");
exit(1);
}
enum SPAN_TYPE
{
SPAN_TITLE = 1,
SPAN_TEXT = 2,
SPAN_SECTION = 3,
SPAN_LINK = 4,
SPAN_URL = 5,
};
struct span {
char *buffer;
int buffer_size;
int length;
int type;
struct span *next;
};
struct manpage
{
char manpage_name[128];
char manpage_section[64];
char filename[1024];
char pwd[1024];
struct {
struct span **lines;
int n_lines;
int lines_allocated;
} document;
int scroll_position;
link_t *links;
int search_start_scroll_position;
int search_input_active;
char search_string[256];
int search_visible;
search_t searches[100];
int search_num;
int search_index;
};
struct manpage *page;
struct manpage *formatting_page; // used when formatting page
struct page_description {
char filename[256];
char pwd[256];
struct manpage *ptr;
};
struct page_description *page_stack;
size_t stack_pos; // index of displayed page + 1
void open_new_page(const char *filename, const char *pwd);
void page_back(void);
void page_forward(void);
void add_line(struct manpage *p)
{
#define STARTING_LINES 256
if (p->document.n_lines == 0)
{
p->document.lines = ZMALLOC(struct span *, STARTING_LINES);
p->document.lines_allocated = STARTING_LINES;
}
else if (p->document.n_lines >= p->document.lines_allocated)
{
int new_n_lines = p->document.lines_allocated * 2;
struct span **old_lines = p->document.lines;
p->document.lines = ZMALLOC(struct span *, new_n_lines);
memcpy(p->document.lines, old_lines, sizeof(struct span *) * p->document.n_lines);
free(old_lines);
p->document.lines_allocated = new_n_lines;
}
p->document.n_lines++;
p->document.lines[p->document.n_lines - 1] = ZMALLOC(struct span, 1);
}
struct span* get_last_span(struct manpage *p)
{
if (p->document.n_lines == 0) return NULL;
struct span *s = p->document.lines[p->document.n_lines - 1];
while (s && s->next)
{
s = s->next;
}
return s;
}
void add_to_span(struct span *s, int letter)
{
#define STARTING_SPAN_SIZE 32
char letter_2 = 0;
switch (letter)
{
case 0x2010: /* Hyphen */
case 0x2013: /* En dash */
case 0x2014: /* Em dash */
case 0x2022: /* Bullet */
case 0x2212: /* Minus sign */
case 0x2500: /* Box drawings light horizontal */
case 0x2501: /* Box drawings heavy horizontal */
letter = '-';
break;
case 0x2217: /* Asterisk Operator */
letter = '*';
break;
case 0x2502: /* Box drawings light vertical */
case 0x2503: /* Box drawings heavy vertical */
letter = '|';
break;
case 0x2265: /* Greater than or equal */
{
letter = '>';
letter_2 = '=';
}
break;
case 0x2264: /* Less than or equal */
{
letter = '<';
letter_2 = '=';
}
break;
case 160: /* Non-breaking space */
case 0x2002: /* En space */
letter = ' ';
break;
case 0x201c:
case 0x201d: /* Left and right double quotation mark */
letter = '"';
break;
case 0x2018:
case 0x2019: /* Left and right single quotation mark */
letter = '\'';
break;
case 0x27e8:
letter = '<';
break;
case 0x27e9:
letter = '>';
break;
}
if ((letter >= 0x250c) && (letter <= 0x254b))
{
letter = '+'; /* various cross symbols */
}
if (letter < 256)
{
int chars = (letter_2 > 0) ? 2 : 1;
if (s->buffer_size == 0)
{
s->buffer = ZMALLOC(char, STARTING_SPAN_SIZE);
s->buffer_size = STARTING_SPAN_SIZE;
s->length = 0;
}
else if ((s->length + chars) >= s->buffer_size)
{
int new_buffer_size = s->buffer_size * 2;
char *old_buffer = s->buffer;
s->buffer = ZMALLOC(char, new_buffer_size);
memcpy(s->buffer, old_buffer, s->length);
free(old_buffer);
s->buffer_size = new_buffer_size;
}
s->buffer[s->length++] = letter;
if (letter_2 > 0)
s->buffer[s->length++] = letter_2;
}
else
{
fprintf(stderr, "Letter %d, 0x%x\n", letter, letter);
}
}
void free_span(struct span *s)
{
while (s)
{
if (s->buffer)
free(s->buffer);
struct span *tmp = s;
s = s->next;
free(tmp);
}
}
void free_manpage(struct manpage *p)
{
for (int i = 0; i < p->document.n_lines; i++)
free_span(p->document.lines[i]);
}
static void format_headf(struct termp *p, const struct roff_meta *meta)
{
//printf("%s\n", __func__);
}
static void format_footf(struct termp *p, const struct roff_meta *meta)
{
//printf("%s\n", __func__);
}
static void format_letter(struct termp *p, int letter)
{
struct span *s = get_last_span(formatting_page);
add_to_span(s, letter);
}
static void format_begin(struct termp *p)
{
//printf("%s\n", __func__);
(*p->headf)(p, p->argf);
}
static void format_end(struct termp *p)
{
//printf("%s\n", __func__);
(*p->footf)(p, p->argf);
}
static void format_endline(struct termp *p)
{
//printf("%s\n", __func__);
p->line++;
p->tcol->offset -= p->ti;
p->ti = 0;
add_line(formatting_page);
}
static void format_advance(struct termp *p, size_t len)
{
//printf("%s %zu\n", __func__, len);
for (int i = 0; i < len; i++)
{
//printf(" ");
struct span *s = get_last_span(formatting_page);
add_to_span(s, ' ');
}
}
static void format_setwidth(struct termp *p, int a, int b)
{
//printf("%s\n", __func__);
}
static size_t format_width(const struct termp *p, int a)
{
return a != ASCII_BREAK; /* 1 unless it's ASCII_BREAK (zero width space) */
}
static int format_hspan(const struct termp *p, const struct roffsu *su)
{
//printf("%s\n", __func__);
double r = 0.0;
switch (su->unit)
{
case SCALE_BU:
r = su->scale;
break;
case SCALE_CM:
r = su->scale * 240.0 / 2.54;
break;
case SCALE_FS:
r = su->scale * 65536.0;
break;
case SCALE_IN:
r = su->scale * 240.0;
break;
case SCALE_MM:
r = su->scale * 0.24;
break;
case SCALE_VS:
case SCALE_PC:
r = su->scale * 40.0;
break;
case SCALE_PT:
r = su->scale * 10.0 / 3.0;
break;
case SCALE_EN:
case SCALE_EM:
r = su->scale * 24.0;
break;
default:
fprintf(stderr, "Unknown unit.\n");
break;
}
return (r > 0.0) ? (r + 0.01) : (r - 0.01);
}
void *mangl_formatter(int width, int indent)
{
struct termp *p = mandoc_calloc(1, sizeof(struct termp));
p->tcol = p->tcols = mandoc_calloc(1, sizeof(struct termp_col));
p->maxtcol = 1;
p->line = 1;
p->defrmargin = p->lastrmargin = width;
/* allocate font stack */
p->fontsz = 8;
p->fontq = mandoc_reallocarray(NULL, p->fontsz, sizeof(*p->fontq));
p->fontq[0] = p->fontl = TERMFONT_NONE;
p->synopsisonly = 0;
// enable if mdoc style is needed
p->mdocstyle = 1;
p->defindent = indent; // change indent
p->flags = 0;
p->type = TERMTYPE_CHAR;
p->enc = TERMENC_UTF8;
/* functions */
p->headf = &format_headf;
p->footf = &format_footf;
p->letter = &format_letter;
p->begin = &format_begin;
p->end = &format_end;
p->endline = &format_endline;
p->advance = &format_advance;
p->setwidth = &format_setwidth;
p->width = &format_width;
p->hspan = &format_hspan;
p->ps = NULL;
return p;
}
void mangl_formatter_free(void *formatter)
{
struct termp *p = (struct termp *)formatter;
term_free(p);
}
void display_manpage_stdout(struct manpage *p)
{
printf("Manpage to stdout:\n");
for (int i = 0; i < p->document.n_lines; i++)
{
struct span *s = p->document.lines[i];
while (s)
{
if (s->length > 0)
printf("%s", s->buffer);
s = s->next;
}
//printf(" .END OF LINE\n");
printf("\n");
}
printf(".END OF MANPAGE\n");
}
int get_line_advance(void)
{
if (mainFont)
return (int)(settings.line_spacing * mainFont->line_height);
return 2 * FONT_CHAR_HEIGHT;
}
int get_line_height(void)
{
if (mainFont)
return (int)(mainFont->line_height);
return 2 * FONT_CHAR_HEIGHT;
}
int get_character_width(void)
{
if (mainFont)
return mainFont->chars['X'].advance;
return FONT_CHAR_WIDTH;
}
int document_width(void)
{
return 2 * get_dimension(DIM_DOCUMENT_MARGIN) + ((settings.current_line_length + 2) * get_character_width());
}
int line_length_from_window_width(int window_width)
{
return (window_width - 2 * get_dimension(DIM_DOCUMENT_MARGIN) - get_dimension(DIM_SCROLLBAR_WIDTH)) /
get_character_width() - 2;
}
int document_height(void)
{
return page->document.n_lines * get_line_advance() + 2 * get_dimension(DIM_DOCUMENT_MARGIN);
}
void find_links(struct manpage *p)
{
for (int i = 0; i < p->document.n_lines; i++)
{
struct span *s = p->document.lines[i];
char line[2048];
line[0] = 0;
while (s)
{
if (s->length > 0)
{
int pos = 0;
char *in = s->buffer;
while (*in && (pos < (ARRAY_SIZE(line) - 1)))
{
if (*in == '\b')
{
if (pos > 0) pos--;
}