Finish multiline support

This commit is contained in:
Samuel Dudik 2020-08-01 21:00:20 +02:00
parent ed9d201bce
commit 7161997b6f
3 changed files with 58 additions and 66 deletions

View file

@ -2,14 +2,15 @@ const static char *background_color = "#3e3e3e";
const static char *border_color = "#ececec"; const static char *border_color = "#ececec";
const static char *font_color = "#ececec"; const static char *font_color = "#ececec";
const static char *font_pattern = "Inconsolata:style=Medium:size=13"; const static char *font_pattern = "Inconsolata:style=Medium:size=13";
const static unsigned line_spacing = 5;
const static unsigned int padding = 15; const static unsigned int padding = 15;
const static unsigned int width = 200; const static unsigned int width = 200;
const static unsigned int border_size = 2; const static unsigned int border_size = 2;
const static unsigned int pos_x = 40; const static unsigned int pos_x = 30;
const static unsigned int pos_y = 50; const static unsigned int pos_y = 50;
enum corners { TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT }; enum corners { TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT };
enum corners corner = TOP_RIGHT; enum corners corner = TOP_RIGHT;
const static unsigned int duration = 15; /* in seconds */ const static unsigned int duration = 5; /* in seconds */

117
herbe.c
View file

@ -4,46 +4,12 @@
#include <stdlib.h> #include <stdlib.h>
#include <signal.h> #include <signal.h>
#include <unistd.h> #include <unistd.h>
#include <math.h>
#include "config.h" #include "config.h"
Display *display; Display *display;
Window window; Window window;
int get_eol(char *body, XftFont *font)
{
int body_len = strlen(body);
XGlyphInfo info;
XftTextExtentsUtf8(display, font, body, body_len, &info);
int max_text_width = width - 2 * padding;
if (info.width < max_text_width)
return body_len;
int eol = max_text_width / font->max_advance_width;
info.width = 0;
while (info.width < max_text_width)
{
eol++;
XftTextExtentsUtf8(display, font, body, eol, &info);
}
return --eol;
// if (body[eol] == ' ')
// return --eol;
// while (body[eol] != ' ')
// {
// eol--;
// }
// return ++eol;
}
void expire() void expire()
{ {
XEvent event; XEvent event;
@ -77,8 +43,8 @@ int main(int argc, char *argv[])
Visual *visual = DefaultVisual(display, screen); Visual *visual = DefaultVisual(display, screen);
Colormap colormap = DefaultColormap(display, screen); Colormap colormap = DefaultColormap(display, screen);
int window_width = DisplayWidth(display, screen); int screen_width = DisplayWidth(display, screen);
int window_height = DisplayHeight(display, screen); int screen_height = DisplayHeight(display, screen);
XftColor color; XftColor color;
@ -91,31 +57,66 @@ int main(int argc, char *argv[])
XftFont *font = XftFontOpenName(display, screen, font_pattern); XftFont *font = XftFontOpenName(display, screen, font_pattern);
int max_text_width = width - 2 * padding;
int eols_size = 5;
int *eols = malloc(eols_size * sizeof(int));
eols[0] = 0;
int remainder = strlen(body);
int num_of_lines = 1;
while (1)
{
XGlyphInfo info;
info.width = 0;
int eol = max_text_width / font->max_advance_width;
while (info.width < max_text_width)
{
eol++;
XftTextExtentsUtf8(display, font, body, eol, &info);
}
--eol;
if (eol >= remainder)
{
if (eols_size < num_of_lines + 1)
{
eols_size += 5;
eols = realloc(eols, eols_size * sizeof(int));
}
eols[num_of_lines] = eols[num_of_lines - 1] + remainder;
num_of_lines++;
break;
}
remainder -= eol;
if (eols_size < num_of_lines + 1)
{
eols_size += 5;
eols = realloc(eols, eols_size * sizeof(int));
}
eols[num_of_lines] = eols[num_of_lines - 1] + eol;
num_of_lines++;
}
unsigned int x = pos_x; unsigned int x = pos_x;
unsigned int y = pos_y; unsigned int y = pos_y;
unsigned int height = font->ascent - font->descent + padding * 2; unsigned int text_height = font->ascent - font->descent;
unsigned int height = (num_of_lines - 2) * line_spacing + (num_of_lines - 1) * text_height + 2 * padding;
switch (corner) switch (corner)
{ {
case BOTTOM_RIGHT: case BOTTOM_RIGHT:
y = window_height - height - border_size * 2 - pos_y; y = screen_height - height - border_size * 2 - pos_y;
case TOP_RIGHT: case TOP_RIGHT:
x = window_width - width - border_size * 2 - pos_x; x = screen_width - width - border_size * 2 - pos_x;
break; break;
case BOTTOM_LEFT: case BOTTOM_LEFT:
y = window_height - height - border_size * 2 - pos_y; y = screen_height - height - border_size * 2 - pos_y;
} }
XGlyphInfo info; window = XCreateWindow(display, RootWindow(display, screen), x, y, width, height, border_size, DefaultDepth(display, screen), CopyFromParent, visual,
XftTextExtentsUtf8(display, font, body, strlen(body), &info); CWOverrideRedirect | CWBackPixel | CWBorderPixel, &attributes);
int num_of_lines = ceil((float)info.width / (width - 2 * padding));
window = XCreateWindow(
display, RootWindow(display, screen), x,
y, width, (num_of_lines - 1) * 5 + num_of_lines * (font->ascent - font->descent) + 2 * padding, border_size,
DefaultDepth(display, screen), CopyFromParent,
visual,
CWOverrideRedirect | CWBackPixel | CWBorderPixel, &attributes);
XftDraw *draw = XftDrawCreate(display, window, visual, colormap); XftDraw *draw = XftDrawCreate(display, window, visual, colormap);
XftColorAllocName(display, visual, colormap, font_color, &color); XftColorAllocName(display, visual, colormap, font_color, &color);
@ -124,14 +125,6 @@ int main(int argc, char *argv[])
XMapWindow(display, window); XMapWindow(display, window);
int eols[num_of_lines + 1];
eols[0] = 0;
for (int i = 1; i < num_of_lines + 1; i++)
{
eols[i] = eols[i - 1] + get_eol(body + eols[i - 1], font);
}
XEvent event; XEvent event;
while (1) while (1)
@ -141,16 +134,14 @@ int main(int argc, char *argv[])
if (event.type == Expose) if (event.type == Expose)
{ {
XClearWindow(display, window); XClearWindow(display, window);
for (int i = 1; i < num_of_lines; i++)
for (int i = 1; i < num_of_lines + 1; i++) XftDrawStringUtf8(draw, &color, font, padding, line_spacing * (i - 1) + text_height * i + padding, body + eols[i - 1], eols[i] - eols[i - 1]);
{
XftDrawStringUtf8(draw, &color, font, padding, 5 * (i - 1) + (font->ascent - font->descent) * i + padding, body + eols[i - 1], eols[i] - eols[i - 1]);
}
} }
if (event.type == ButtonPress) if (event.type == ButtonPress)
break; break;
} }
free(eols);
XftDrawDestroy(draw); XftDrawDestroy(draw);
XftColorFree(display, visual, colormap, &color); XftColorFree(display, visual, colormap, &color);
XftFontClose(display, font); XftFontClose(display, font);

View file

@ -8,4 +8,4 @@ uninstall:
rm /usr/local/bin/herbe rm /usr/local/bin/herbe
clean: clean:
rm -f herbe rm -f herbe