Skip to content

Commit

Permalink
update ctrlfnt; pass lint through code
Browse files Browse the repository at this point in the history
  • Loading branch information
phillbush committed Jun 13, 2023
1 parent db5b0f1 commit 23cd43a
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 87 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ ${PROG}: ${OBJS}
${CC} -o $@ ${OBJS} ${LIBS} ${LDFLAGS}

pmenu.o: ctrlfnt.h
${OBJS}: defs.h

.c.o:
${CC} -std=c99 -pedantic ${DEFS} ${INCS} ${CFLAGS} ${CPPFLAGS} -c $<

tags: ${SRCS}
ctags ${SRCS}

lint: ${SRCS}
-mandoc -T lint -W warning ${MAN}
-clang-tidy ${SRCS} -- -std=c99 ${DEFS} ${INCS} ${CPPFLAGS}

clean:
-rm -f ${OBJS} ${PROG} ${PROG:=.core}

Expand All @@ -42,4 +45,4 @@ uninstall:
rm -f ${DESTDIR}${PREFIX}/bin/${PROG}
rm -f ${DESTDIR}${MANPREFIX}/man1/${MAN}

.PHONY: all clean install uninstall
.PHONY: all tags clean install uninstall lint
34 changes: 24 additions & 10 deletions ctrlfnt.3
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
.Fa "cont char *text"
.Fa "int nbytes"
.Fc
.Fc
.Ft int
.Fo ctrlfnt_height
.Fa "CtrlFontSet *fontset"
Expand Down Expand Up @@ -127,11 +126,9 @@ within the given
using
.Fa src
as source color.
It returns
.Ic RETURN_FAILURE
(-1) if the text could not be drawn, or
.Ic RETURN_SUCCESS
(0) otherwise.
It returns the width of the drawn text, or
.Ic -1
if the text could not be drawn because of any error.
.Pp
The
.Fn ctrlfnt_width
Expand All @@ -142,7 +139,13 @@ of
if drawn by
.Fn ctrlfnt_draw
using the given
.Fa fontset .
.Fa fontset ,
or
.Ic -1
if the width could not be computed.
(This function returns the same as
.Fn ctrlfnt_draw
without drawing anything).
.Pp
The
.Fn ctrlfnt_height
Expand All @@ -158,9 +161,20 @@ The
.Fn ctrlfnt_term
function terminates the fontconfig library.
.Sh RETURN VALUES
[...]
The
.Fn ctrlfnt_open
function returns NULL on error.
The
.Fn ctrlfnt_draw ,
.Fn ctrlfnt_width ,
and
.Fn ctrlfnt_height ,
functions return a value less than zero on error.
.Pp
If an error occurs, the functions write a string describing the error into standard output with
.Xr warnx 3 .
.Sh EXAMPLES
[...]
.Sh SEE ALSO
.Xr X 1 ,
.Xr Xft 3
.Xr Xft 3 ,
.Xr X 7
46 changes: 26 additions & 20 deletions ctrlfnt.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <X11/extensions/Xrender.h>
#include <fontconfig/fontconfig.h>

#include "defs.h"
#include "ctrlfnt.h"

struct VArray {
Expand Down Expand Up @@ -108,7 +107,7 @@ openxftfontset(Display *display, const char *fontspec, double fontsize)
fontset->fonts[0] = openxftfont(display, "", fontsize);
if (fontset->fonts[0] == NULL)
goto error;
return fontset;
goto done;
}
for (t = strtok_r(s, ",", &last);
t != NULL;
Expand All @@ -120,6 +119,7 @@ openxftfontset(Display *display, const char *fontspec, double fontsize)
}
if (fontset->nmemb == 0)
goto error;
done:
free(s);
return fontset;
error:
Expand Down Expand Up @@ -338,9 +338,10 @@ drawxftstring(CtrlFontSet *fontset, Picture picture, Picture src,
size_t nwritten = 0;
size_t n = 0;
int x = rect.x;
int w = 0;

if (nbytes == 0)
return RETURN_SUCCESS;
return 0;
while (end < text + nbytes && end < text + MAXGLYPHS)
glyphs[nglyphs++] = getnextutf8char(end, &end);
while (nwritten < nglyphs) {
Expand All @@ -358,7 +359,7 @@ drawxftstring(CtrlFontSet *fontset, Picture picture, Picture src,
font,
picture,
0, 0,
x,
x + w,
rect.y + rect.height / 2
+ font->ascent / 2
- font->descent / 2,
Expand All @@ -373,13 +374,13 @@ drawxftstring(CtrlFontSet *fontset, Picture picture, Picture src,
n,
&extents
);
x += extents.width;
w += extents.width;
nwritten += n;
}
return RETURN_SUCCESS;
return w;
}

static void
static int
drawxmbstring(CtrlFontSet *fontset, Pixmap pix, GC gc, XRectangle rect,
const char *text, int nbytes)
{
Expand All @@ -395,16 +396,17 @@ drawxmbstring(CtrlFontSet *fontset, Pixmap pix, GC gc, XRectangle rect,
text,
nbytes
);
return box.width;
}

static void
static int
drawxstring(CtrlFontSet *fontset, Pixmap pix, GC gc, XRectangle rect,
const char *text, int nbytes)
{
XChar2b glyphs[MAXGLYPHS];
int nglyphs;

nglyphs = utf8toxchar2b(glyphs, LEN(glyphs), text, nbytes);
nglyphs = utf8toxchar2b(glyphs, MAXGLYPHS, text, nbytes);
XSetFont(fontset->display, gc, fontset->xlfd_font->fid);
XDrawString16(
fontset->display,
Expand All @@ -417,6 +419,7 @@ drawxstring(CtrlFontSet *fontset, Pixmap pix, GC gc, XRectangle rect,
glyphs,
nglyphs
);
return XTextWidth16(fontset->xlfd_font, glyphs, nglyphs);
}

static int
Expand All @@ -426,6 +429,7 @@ drawx(CtrlFontSet *fontset, Picture picture, Picture src,
Pixmap pix = None;
Picture mask = None;
GC gc = NULL;
int retval;

pix = XCreatePixmap(
fontset->display,
Expand Down Expand Up @@ -461,9 +465,11 @@ drawx(CtrlFontSet *fontset, Picture picture, Picture src,
);
XSetForeground(fontset->display, gc, 1);
if (fontset->xlfd_font != NULL)
drawxstring(fontset, pix, gc, rect, text, nbytes);
retval = drawxstring(fontset, pix, gc, rect, text, nbytes);
else if (fontset->xlfd_fontset != NULL)
drawxmbstring(fontset, pix, gc, rect, text, nbytes);
retval = drawxmbstring(fontset, pix, gc, rect, text, nbytes);
else
goto error;
XRenderComposite(
fontset->display,
PictOpOver,
Expand All @@ -478,15 +484,15 @@ drawx(CtrlFontSet *fontset, Picture picture, Picture src,
XFreeGC(fontset->display, gc);
XFreePixmap(fontset->display, pix);
XRenderFreePicture(fontset->display, mask);
return RETURN_SUCCESS;
return retval;
error:
if (gc != NULL)
XFreeGC(fontset->display, gc);
if (pix != None)
XFreePixmap(fontset->display, pix);
if (mask != None)
XRenderFreePicture(fontset->display, mask);
return RETURN_FAILURE;
return -1;
}

static int
Expand Down Expand Up @@ -541,7 +547,7 @@ widthxstring(CtrlFontSet *fontset, const char *text, int nbytes)
XChar2b glyphs[MAXGLYPHS];
int nglyphs;

nglyphs = utf8toxchar2b(glyphs, LEN(glyphs), text, nbytes);
nglyphs = utf8toxchar2b(glyphs, MAXGLYPHS, text, nbytes);
return XTextWidth16(fontset->xlfd_font, glyphs, nglyphs);
}

Expand Down Expand Up @@ -582,19 +588,19 @@ ctrlfnt_open(Display *display, int screen, Visual *visual, Colormap
}
switch (fonttype) {
case XLFD_FONT:
fontset->xlfd_font = openxfont(display, str, TRUE);
fontset->xlfd_font = openxfont(display, str, 1);
break;
case XLFD_FONTSET:
fontset->xlfd_fontset = openxfontset(display, str, TRUE);
fontset->xlfd_fontset = openxfontset(display, str, 1);
break;
case XFT_FONTSET:
fontset->xft_fontset = openxftfontset(display, str, fontsize);
break;
case GUESSTYPE:
if (hascomma)
fontset->xlfd_fontset = openxfontset(display, str, FALSE);
fontset->xlfd_fontset = openxfontset(display, str, 0);
else
fontset->xlfd_font = openxfont(display, str, FALSE);
fontset->xlfd_font = openxfont(display, str, 0);
if (fontset->xlfd_font == NULL && fontset->xlfd_fontset == NULL)
fontset->xft_fontset = openxftfontset(display, str, fontsize);
break;
Expand All @@ -614,14 +620,14 @@ ctrlfnt_draw(CtrlFontSet *fontset, Picture picture, Picture src,
XRectangle rect, const char *text, int nbytes)
{
if (fontset == NULL)
return RETURN_FAILURE;
return -1;
if (fontset->xft_fontset != NULL)
return drawxftstring(fontset, picture, src, rect, text, nbytes);
if (fontset->xlfd_fontset != NULL)
return drawx(fontset, picture, src, rect, text, nbytes);
if (fontset->xlfd_font != NULL)
return drawx(fontset, picture, src, rect, text, nbytes);
return RETURN_FAILURE;
return -1;
}

int
Expand Down
14 changes: 0 additions & 14 deletions defs.h

This file was deleted.

16 changes: 8 additions & 8 deletions pmenu.1
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ An item without initial tabs is a top-level item.
The image is a string of the form
.Qq Ic "IMG:/path/to/image.png" .
It specifies the path to an image file to be shown as icon in the slice.
If thepath does not begin with
.Qq Pp "/" ,
.Qq Pp "./" ,
If the path does not begin with
.Qq Pa "/" ,
.Qq Pa "./" ,
or
.Qq Pp "../" ,
.Qq Pa "../" ,
the file is searched on the paths specified in the
.Ev ICONPATH
environment variable.
Expand Down Expand Up @@ -144,13 +144,13 @@ Pie slices can be selected using
and
.Ic Esc .
.Bl -tag -width Ds
.Ic Tab
.It Ic Tab
Cycle through the slices in the clockwise direction.
.Ic Shift-Tab
.It Ic Shift-Tab
Cycle through the slices in the counterclockwise direction.
.Ic Enter
.It Ic Enter
Select the highlighted item.
.Ic Esc
.It Ic Esc
Close a menu.
.El
.Sh RESOURCES
Expand Down
Loading

0 comments on commit 23cd43a

Please sign in to comment.