Skip to content

Commit

Permalink
Add pixel offset
Browse files Browse the repository at this point in the history
Add a formatting command to offset the text position by a pixel amount.
  • Loading branch information
Stebalien committed Feb 17, 2016
1 parent c788534 commit 892e43a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
4 changes: 4 additions & 0 deletions README.pod
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ Aligns the following text to the center of the screen.

Aligns the following text to the right side of the screen.

=item B<O>I<width>

Offset the current position by I<width> pixels in the alignment direction.

=item B<B>I<color>

Set the text background color. The parameter I<color> can be I<-> or a color in one of the formats mentioned before. The special value I<-> resets the color to the default one.
Expand Down
54 changes: 42 additions & 12 deletions lemonbar.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,8 @@ xcb_void_cookie_t xcb_poly_text_16_simple(xcb_connection_t * c,
}

int
draw_char (monitor_t *mon, font_t *cur_font, int x, int align, uint16_t ch)
shift (monitor_t *mon, int x, int align, int ch_width)
{
int ch_width;

ch_width = (cur_font->width_lut) ?
cur_font->width_lut[ch - cur_font->char_min].character_width:
cur_font->width;

switch (align) {
case ALIGN_C:
xcb_copy_area(c, mon->pixmap, mon->pixmap, gc[GC_DRAW],
Expand All @@ -213,6 +207,34 @@ draw_char (monitor_t *mon, font_t *cur_font, int x, int align, uint16_t ch)

// Draw the background first
fill_rect(mon->pixmap, gc[GC_CLEAR], x, 0, ch_width, bh);
return x;
}

void
draw_lines (monitor_t *mon, int x, int w)
{
/* We can render both at the same time */
if (attrs & ATTR_OVERL)
fill_rect(mon->pixmap, gc[GC_ATTR], x, 0, w, bu);
if (attrs & ATTR_UNDERL)
fill_rect(mon->pixmap, gc[GC_ATTR], x, bh - bu, w, bu);
}

void
draw_shift (monitor_t *mon, int x, int align, int w)
{
x = shift(mon, x, align, w);
draw_lines(mon, x, w);
}

int
draw_char (monitor_t *mon, font_t *cur_font, int x, int align, uint16_t ch)
{
int ch_width = (cur_font->width_lut) ?
cur_font->width_lut[ch - cur_font->char_min].character_width:
cur_font->width;

x = shift(mon, x, align, ch_width);

// xcb accepts string in UCS-2 BE, so swap
ch = (ch >> 8) | (ch << 8);
Expand All @@ -222,11 +244,7 @@ draw_char (monitor_t *mon, font_t *cur_font, int x, int align, uint16_t ch)
x, bh / 2 + cur_font->height / 2 - cur_font->descent,
1, &ch);

// We can render both at the same time
if (attrs & ATTR_OVERL)
fill_rect(mon->pixmap, gc[GC_ATTR], x, 0, ch_width, bu);
if (attrs & ATTR_UNDERL)
fill_rect(mon->pixmap, gc[GC_ATTR], x, bh - bu, ch_width, bu);
draw_lines(mon, x, ch_width);

return ch_width;
}
Expand Down Expand Up @@ -490,6 +508,7 @@ parse (char *text)
if (p[0] == '%' && p[1] == '{' && (block_end = strchr(p++, '}'))) {
p++;
while (p < block_end) {
int w;
while (isspace(*p))
p++;

Expand Down Expand Up @@ -542,6 +561,17 @@ parse (char *text)
p++;
pos_x = 0;
break;
case 'O':
errno = 0;
w = (int) strtoul(p, &p, 10);
if (errno)
continue;

draw_shift(cur_mon, pos_x, align, w);

pos_x += w;
area_shift(cur_mon->window, align, w);
break;

case 'T':
if (*p == '-') { //Reset to automatic font selection
Expand Down

0 comments on commit 892e43a

Please sign in to comment.