Skip to content

Commit 530100f

Browse files
committed
Merge branch 'master' of github.com:Immediate-Mode-UI/Nuklear into fix_insert_replace_reset
2 parents b150778 + 831f8d7 commit 530100f

File tree

14 files changed

+157
-289
lines changed

14 files changed

+157
-289
lines changed

.github/workflows/ccpp.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ jobs:
4545
run: make -C demo/x11_opengl3
4646
- name: build x11_rawfb
4747
run: make -C demo/rawfb/x11
48-
- name: build x11_xft
49-
run: make -C demo/x11_xft
5048
- name: build xcb_cairo
5149
run: make -C demo/xcb_cairo
5250
- name: build example

clib.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nuklear",
3-
"version": "4.12.7",
3+
"version": "4.12.8",
44
"repo": "Immediate-Mode-UI/Nuklear",
55
"description": "A small ANSI C gui toolkit",
66
"keywords": ["gl", "ui", "toolkit"],

demo/rawfb/wayland/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ int main ()
516516
pl.gloss = 0;
517517
pl.bloss = 0;
518518

519-
nk_rawfb_init(nk_wayland_ctx.data, nk_wayland_ctx.tex_scratch, WIDTH, HEIGHT, WIDTH*4, pl);
519+
nk_wayland_ctx.rawfb = nk_rawfb_init(nk_wayland_ctx.data, nk_wayland_ctx.tex_scratch, WIDTH, HEIGHT, WIDTH*4, pl);
520520

521521

522522
//4. rendering UI

demo/x11/Makefile

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,25 @@
22
BIN = demo
33

44
# Flags
5-
CFLAGS += -g -std=c89 -Wall -Wextra -pedantic -Wno-unused-function
5+
CFLAGS += -g -std=c89 -Wall -Wextra -pedantic -Wno-unused-function -D_POSIX_C_SOURCE=200809L
66

77
SRC = main.c
88
OBJ = $(SRC:.c=.o)
99

10+
X11_CFLAGS += ${shell pkg-config --cflags x11}
11+
X11_LDFLAGS += ${shell pkg-config --libs x11} -lm
12+
13+
XFT_CFLAGS += ${shell pkg-config --cflags xft x11} -DNK_XLIB_USE_XFT
14+
XFT_LDFLAGS += ${shell pkg-config --libs xft x11} -lm
15+
16+
all: $(BIN) $(BIN)-xft
17+
1018
$(BIN):
1119
@mkdir -p bin
1220
rm -f bin/$(BIN) $(OBJS)
13-
$(CC) $(SRC) $(CFLAGS) -D_POSIX_C_SOURCE=200809L -o bin/$(BIN) -lX11 -lm
21+
$(CC) $(SRC) $(CFLAGS) $(X11_CFLAGS) -o bin/$(BIN) ${LDFLAGS} ${X11_LDFLAGS}
22+
23+
$(BIN)-xft:
24+
@mkdir -p bin
25+
rm -f bin/$(BIN)-xft $(OBJS)
26+
$(CC) $(SRC) $(CFLAGS) $(XFT_CFLAGS) -o bin/$(BIN)-xft ${LDFLAGS} ${LDFLAGS} ${XFT_LDFLAGS}

demo/x11/main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,14 @@ main(void)
159159
xw.height = (unsigned int)xw.attr.height;
160160

161161
/* GUI */
162+
#ifdef NK_XLIB_USE_XFT
163+
xw.font = nk_xfont_create(xw.dpy, "Arial");
164+
ctx = nk_xlib_init(xw.font, xw.dpy, xw.screen, xw.win,
165+
xw.vis, xw.cmap, xw.width, xw.height);
166+
#else
162167
xw.font = nk_xfont_create(xw.dpy, "fixed");
163168
ctx = nk_xlib_init(xw.font, xw.dpy, xw.screen, xw.win, xw.width, xw.height);
169+
#endif
164170

165171
while (running)
166172
{

demo/x11/nuklear_xlib.h

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
#include <X11/Xlib.h>
1717

1818
typedef struct XFont XFont;
19+
#ifdef NK_XLIB_USE_XFT
20+
NK_API struct nk_context* nk_xlib_init(XFont*, Display*, int scrn, Window root, Visual *vis, Colormap cmap, unsigned w, unsigned h);
21+
#else
1922
NK_API struct nk_context* nk_xlib_init(XFont*, Display*, int scrn, Window root, unsigned w, unsigned h);
23+
#endif
2024
NK_API int nk_xlib_handle_event(Display*, int scrn, Window, XEvent*);
2125
NK_API void nk_xlib_render(Drawable screen, struct nk_color clear);
2226
NK_API void nk_xlib_shutdown(void);
@@ -53,6 +57,10 @@ NK_API void nk_xfont_del(Display *dpy, XFont *font);
5357
#include <X11/Xlocale.h>
5458
#include <X11/Xatom.h>
5559

60+
#ifdef NK_XLIB_USE_XFT
61+
#include <X11/Xft/Xft.h>
62+
#endif
63+
5664
#include <sys/time.h>
5765
#include <unistd.h>
5866
#include <time.h>
@@ -80,8 +88,12 @@ struct XFont {
8088
int ascent;
8189
int descent;
8290
int height;
91+
#ifdef NK_XLIB_USE_XFT
92+
XftFont * ft;
93+
#else
8394
XFontSet set;
8495
XFontStruct *xfont;
96+
#endif
8597
struct nk_user_font handle;
8698
};
8799
struct XSurface {
@@ -91,6 +103,9 @@ struct XSurface {
91103
Window root;
92104
Drawable drawable;
93105
unsigned int w, h;
106+
#ifdef NK_XLIB_USE_XFT
107+
XftDraw * ftdraw;
108+
#endif
94109
};
95110
struct XImageWithAlpha {
96111
XImage* ximage;
@@ -112,6 +127,10 @@ static struct {
112127
Cursor cursor;
113128
Display *dpy;
114129
Window root;
130+
#ifdef NK_XLIB_USE_XFT
131+
Visual *vis;
132+
Colormap cmap;
133+
#endif
115134
double last_button_click;
116135
double time_of_last_frame;
117136
} xlib;
@@ -147,6 +166,10 @@ nk_xsurf_create(int screen, unsigned int w, unsigned int h)
147166
XSetLineAttributes(xlib.dpy, surface->gc, 1, LineSolid, CapButt, JoinMiter);
148167
surface->drawable = XCreatePixmap(xlib.dpy, xlib.root, w, h,
149168
(unsigned int)DefaultDepth(xlib.dpy, screen));
169+
#ifdef NK_XLIB_USE_XFT
170+
surface->ftdraw = XftDrawCreate(xlib.dpy, surface->drawable,
171+
xlib.vis, xlib.cmap);
172+
#endif
150173
return surface;
151174
}
152175

@@ -159,6 +182,9 @@ nk_xsurf_resize(XSurface *surf, unsigned int w, unsigned int h)
159182
if(surf->drawable) XFreePixmap(surf->dpy, surf->drawable);
160183
surf->drawable = XCreatePixmap(surf->dpy, surf->root, w, h,
161184
(unsigned int)DefaultDepth(surf->dpy, surf->screen));
185+
#ifdef NK_XLIB_USE_XFT
186+
XftDrawChange(surf->ftdraw, surf->drawable);
187+
#endif
162188
}
163189

164190
NK_INTERN void
@@ -170,6 +196,10 @@ nk_xsurf_scissor(XSurface *surf, float x, float y, float w, float h)
170196
clip_rect.width = (unsigned short)(w+2);
171197
clip_rect.height = (unsigned short)(h+2);
172198
XSetClipRectangles(surf->dpy, surf->gc, 0, 0, &clip_rect, 1, Unsorted);
199+
200+
#ifdef NK_XLIB_USE_XFT
201+
XftDrawSetClipRectangles(surf->ftdraw, 0, 0, &clip_rect, 1);
202+
#endif
173203
}
174204

175205
NK_INTERN void
@@ -416,16 +446,31 @@ nk_xsurf_draw_text(XSurface *surf, short x, short y, const char *text, int len,
416446
XFont *font, struct nk_color cfg)
417447
{
418448
int tx, ty;
449+
#ifdef NK_XLIB_USE_XFT
450+
XRenderColor xrc;
451+
XftColor color;
452+
#else
419453
unsigned long fg = nk_color_from_byte(&cfg.r);
454+
#endif
420455

421456
if(!text || !font || !len) return;
422457

423458
tx = (int)x;
424459
ty = (int)y + font->ascent;
460+
#ifdef NK_XLIB_USE_XFT
461+
xrc.red = cfg.r * 257;
462+
xrc.green = cfg.g * 257;
463+
xrc.blue = cfg.b * 257;
464+
xrc.alpha = cfg.a * 257;
465+
XftColorAllocValue(surf->dpy, xlib.vis, xlib.cmap, &xrc, &color);
466+
XftDrawStringUtf8(surf->ftdraw, &color, font->ft, tx, ty, (FcChar8*)text, len);
467+
XftColorFree(surf->dpy, xlib.vis, xlib.cmap, &color);
468+
#else
425469
XSetForeground(surf->dpy, surf->gc, fg);
426470
if(font->set)
427471
XmbDrawString(surf->dpy,surf->drawable,font->set,surf->gc,tx,ty,(const char*)text,(int)len);
428472
else XDrawString(surf->dpy, surf->drawable, surf->gc, tx, ty, (const char*)text, (int)len);
473+
#endif
429474
}
430475

431476

@@ -563,6 +608,9 @@ nk_xsurf_blit(Drawable target, XSurface *surf, unsigned int w, unsigned int h)
563608
NK_INTERN void
564609
nk_xsurf_del(XSurface *surf)
565610
{
611+
#ifdef NK_XLIB_USE_XFT
612+
XftDrawDestroy(surf->ftdraw);
613+
#endif
566614
XFreePixmap(surf->dpy, surf->drawable);
567615
XFreeGC(surf->dpy, surf->gc);
568616
free(surf);
@@ -571,6 +619,17 @@ nk_xsurf_del(XSurface *surf)
571619
NK_API XFont*
572620
nk_xfont_create(Display *dpy, const char *name)
573621
{
622+
#ifdef NK_XLIB_USE_XFT
623+
XFont *font = (XFont*)calloc(1, sizeof(XFont));
624+
font->ft = XftFontOpenName(dpy, XDefaultScreen(dpy), name);
625+
if (!font->ft) {
626+
fprintf(stderr, "missing font: %s\n", name);
627+
return font;
628+
}
629+
font->ascent = font->ft->ascent;
630+
font->descent = font->ft->descent;
631+
font->height = font->ft->height;
632+
#else
574633
int n;
575634
char *def, **missing;
576635
XFont *font = (XFont*)calloc(1, sizeof(XFont));
@@ -600,13 +659,26 @@ nk_xfont_create(Display *dpy, const char *name)
600659
font->descent = font->xfont->descent;
601660
}
602661
font->height = font->ascent + font->descent;
662+
#endif
603663
return font;
604664
}
605665

606666
NK_INTERN float
607667
nk_xfont_get_text_width(nk_handle handle, float height, const char *text, int len)
608668
{
609669
XFont *font = (XFont*)handle.ptr;
670+
671+
#ifdef NK_XLIB_USE_XFT
672+
XGlyphInfo g;
673+
674+
NK_UNUSED(height);
675+
676+
if(!font || !text)
677+
return 0;
678+
679+
XftTextExtentsUtf8(xlib.dpy, font->ft, (FcChar8*)text, len, &g);
680+
return g.xOff;
681+
#else
610682
XRectangle r;
611683

612684
NK_UNUSED(height);
@@ -621,21 +693,29 @@ nk_xfont_get_text_width(nk_handle handle, float height, const char *text, int le
621693
int w = XTextWidth(font->xfont, (const char*)text, len);
622694
return (float)w;
623695
}
696+
#endif
624697
}
625698

626699
NK_API void
627700
nk_xfont_del(Display *dpy, XFont *font)
628701
{
629702
if(!font) return;
703+
#ifdef NK_XLIB_USE_XFT
704+
XftFontClose(dpy, font->ft);
705+
#else
630706
if(font->set)
631707
XFreeFontSet(dpy, font->set);
632708
else
633709
XFreeFont(dpy, font->xfont);
710+
#endif
634711
free(font);
635712
}
636713

637714
NK_API struct nk_context*
638715
nk_xlib_init(XFont *xfont, Display *dpy, int screen, Window root,
716+
#ifdef NK_XLIB_USE_XFT
717+
Visual *vis, Colormap cmap,
718+
#endif
639719
unsigned int w, unsigned int h)
640720
{
641721
struct nk_user_font *font = &xfont->handle;
@@ -644,6 +724,10 @@ nk_xlib_init(XFont *xfont, Display *dpy, int screen, Window root,
644724
font->width = nk_xfont_get_text_width;
645725
xlib.dpy = dpy;
646726
xlib.root = root;
727+
#ifdef NK_XLIB_USE_XFT
728+
xlib.vis = vis;
729+
xlib.cmap = cmap;
730+
#endif
647731

648732
if (!setlocale(LC_ALL,"")) return 0;
649733
if (!XSupportsLocale()) return 0;

demo/x11_xft/Makefile

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)