13 #include <pango/pango.h>
14 #include <pango/pangoft2.h>
19 #define MAXTEXT (1 << 20)
22 #define CTRLKEY(x) ((x) - 96)
32 {"default", "<span size=\"xx-small\">"}
36 static char name
[128];
37 static PangoContext
*context
;
39 static int height
= 1;
41 static PangoFontMap
*fontmap
;
42 static FT_Bitmap
*bitmap
;
43 static char text
[MAXTEXT
];
44 static char *pos
= text
;
47 static FT_Bitmap
*create_bitmap(int width
, int height
)
49 FT_Bitmap
*bitmap
= g_slice_new(FT_Bitmap
);
50 bitmap
->width
= width
;
51 bitmap
->pitch
= (bitmap
->width
+ 3) & ~3;
52 bitmap
->rows
= height
;
53 bitmap
->buffer
= g_malloc(bitmap
->pitch
* bitmap
->rows
);
54 bitmap
->num_grays
= 256;
55 bitmap
->pixel_mode
= ft_pixel_mode_grays
;
56 memset(bitmap
->buffer
, 0, bitmap
->pitch
* bitmap
->rows
);
60 static void destroy_bitmap(FT_Bitmap
*bitmap
)
62 g_free(bitmap
->buffer
);
63 g_slice_free(FT_Bitmap
, bitmap
);
66 #define COLORMERGE(f, b, c) ((b) + (((f) - (b)) * (c) >> 8u))
68 static void view_write(int sr
)
72 int nr
= MIN(height
- sr
, fb_rows());
73 int nc
= MIN(width
, fb_cols());
74 for (r
= 0; r
< nr
; r
++) {
75 for (c
= 0; c
< nc
; c
++) {
76 int v
= bitmap
->buffer
[(sr
+ r
) * bitmap
->pitch
+ c
];
77 int r
= COLORMERGE(0xd5, 0x00, 255 - v
);
78 int g
= COLORMERGE(0xdd, 0x00, 255 - v
);
79 int b
= COLORMERGE(0xff, 0x00, 255 - v
);
80 buf
[c
] = fb_color(r
, g
, b
);
82 fb_set(r
, 0, buf
, width
);
86 static PangoLayout
*make_layout(PangoContext
*context
, char *text
, double size
)
88 static PangoFontDescription
*font_description
;
89 PangoLayout
*layout
= pango_layout_new(context
);
90 pango_layout_set_markup(layout
, text
, -1);
91 pango_layout_set_wrap(layout
, PANGO_WRAP_WORD_CHAR
);
92 pango_layout_set_width(layout
, fb_cols() << 10);
93 pango_layout_set_height(layout
, -1);
94 pango_layout_set_alignment(layout
, PANGO_ALIGN_RIGHT
);
95 pango_layout_set_font_description(layout
, font_description
);
96 pango_font_description_free(font_description
);
100 static void do_output(PangoContext
*context
, char *text
)
103 PangoRectangle logical_rect
;
104 pango_context_set_language(context
, pango_language_get_default());
105 pango_context_set_base_dir(context
, PANGO_DIRECTION_RTL
);
106 layout
= make_layout(context
, text
, -1);
107 pango_layout_get_pixel_extents(layout
, NULL
, &logical_rect
);
108 pango_ft2_render_layout(bitmap
, layout
, 0, 0);
109 width
= MAX(width
, MAX(logical_rect
.x
+ logical_rect
.width
,
110 PANGO_PIXELS(pango_layout_get_width(layout
))));
111 height
= MAX(logical_rect
.y
+ logical_rect
.height
,
112 PANGO_PIXELS(pango_layout_get_height(layout
)));
113 g_object_unref(layout
);
116 static int readkey(void)
119 if (read(STDIN_FILENO
, &b
, 1) <= 0)
124 void txtwin_append(char *s
, char *tag
)
128 for (i
= 0; i
< ntags
; i
++)
129 if (tag
&& !strcmp(tag
, tags
[i
].name
))
131 snprintf(pos
, MAXTEXT
- (pos
- text
),
132 "%s%s</span>", tags
[t
].desc
, s
);
133 pos
= strchr(pos
, '\0');
136 void txtwin_tag(char *name
, char *fg
, char *bg
, char *font
)
138 strcpy(tags
[ntags
].name
, name
);
139 snprintf(tags
[ntags
].desc
, sizeof(tags
[ntags
].desc
),
140 "<span font=\"%s\">", font
);
144 static int getcount(int def
)
146 int result
= count
? count
: def
;
151 static void printinfo(void)
154 printf("CNOOR \t\t\t %d%% \t\t\t %s \x1b[K",
155 head
* 100 / height
, name
);
159 void txtwin_loop(void)
161 int step
= fb_rows() / PAGESTEPS
;
163 struct termios oldtermios
, termios
;
164 tcgetattr(STDIN_FILENO
, &termios
);
165 oldtermios
= termios
;
167 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &termios
);
168 bitmap
= create_bitmap(width
, height
);
169 do_output(context
, text
);
170 destroy_bitmap(bitmap
);
171 bitmap
= create_bitmap(width
, height
);
172 do_output(context
, text
);
174 while ((c
= readkey()) != -1) {
180 tcsetattr(STDIN_FILENO
, 0, &oldtermios
);
184 count
= count
* 10 + c
- '0';
188 head
+= step
* getcount(1);
191 head
-= step
* getcount(1);
195 head
+= fb_rows() * getcount(1) - step
;
199 head
-= fb_rows() * getcount(1) - step
;
207 head
= height
- fb_rows();
211 head
= height
* getcount(0) / 100;
217 /* no need to redraw */
220 head
= MAX(0, MIN(head
, height
- fb_rows()));
225 void txtwin_gtk_init(int argc
, char **argv
)
228 g_set_prgname("cpan");
229 setlocale(LC_ALL
, "");
232 void txtwin_init(char *sura
)
234 char *clear
= "\x1b[2J\x1b[H";
235 char *hide
= "\x1b[?25l";
236 fontmap
= pango_ft2_font_map_new();
237 pango_ft2_font_map_set_resolution(PANGO_FT2_FONT_MAP(fontmap
), DPI
, DPI
);
238 context
= pango_font_map_create_context(PANGO_FONT_MAP(fontmap
));
241 write(STDIN_FILENO
, clear
, strlen(clear
));
242 write(STDIN_FILENO
, hide
, strlen(hide
));
246 void txtwin_free(void)
248 char *show
= "\x1b[?25h\n";
250 destroy_bitmap(bitmap
);
251 g_object_unref(context
);
252 g_object_unref(fontmap
);
253 write(STDIN_FILENO
, show
, strlen(show
));