From 128a90671a76df2537e36b52341dc207a3c381bc Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Wed, 10 Oct 2018 21:58:35 -0400 Subject: [PATCH] updated Font tag to only change edit region --- TODO.md | 1 - inc/edit.h | 14 +++++++------- src/lib/view.c | 12 ++++++------ src/lib/x11.c | 34 ++++++++++++++++++---------------- 4 files changed, 31 insertions(+), 30 deletions(-) diff --git a/TODO.md b/TODO.md index 4292385..9347ba5 100644 --- a/TODO.md +++ b/TODO.md @@ -3,7 +3,6 @@ ## STAGING * centering logic in view.c seems broken -* Font tag should only change font in edit region, not tags region * implement new version of tfetch (plumb) * implement tide registrar * implement tctl command diff --git a/inc/edit.h b/inc/edit.h index 8fcca19..1780a16 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -99,13 +99,13 @@ typedef struct { enum { CURSOR = (1 << 0), CENTER = (1 << 1), - } sync_flags; - Buf buffer; /* the buffer used to populate the view */ - size_t index; - size_t width; - size_t nvisible; - size_t nrows; /* number of rows and columns in the view */ - Row** rows; /* array of row data structures */ + } sync_flags; /* flags controlling how the view is synced to cursor */ + Buf buffer; /* the buffer used to populate the view */ + size_t index; /* */ + size_t width; /* width of the view in pixels */ + size_t nvisible; /* number of visible lines */ + size_t nrows; /* number of rows and columns in the view */ + Row** rows; /* array of row data structures */ } View; enum { diff --git a/src/lib/view.c b/src/lib/view.c index 7ba62d9..14aa2dc 100644 --- a/src/lib/view.c +++ b/src/lib/view.c @@ -4,7 +4,7 @@ #include /* Provided by x11.c */ -extern size_t glyph_width(int c); +extern size_t glyph_width(View* view, int c); #define BUF (&(view->buffer)) #define CSRPOS (view->buffer.selection.end) @@ -84,22 +84,22 @@ void view_reload(View* view) { } } -static size_t rune_width(int c, size_t xpos, size_t width) { +static size_t rune_width(View* view, int c, size_t xpos, size_t width) { if (c == '\r') return 0; else if (c == '\n') return (width-xpos); else if (c == '\t') - return (glyph_width(c) - (xpos % glyph_width(c))); + return (glyph_width(view, c) - (xpos % glyph_width(view, c))); else - return glyph_width(c); + return glyph_width(view, c); } size_t view_limitrows(View* view, size_t maxrows) { size_t nrows = 1, off = 0, xpos = 0; while (nrows < maxrows && off < buf_end(&(view->buffer))) { Rune rune = buf_getrat(&(view->buffer), off); - xpos += rune_width(rune, xpos, view->width); + xpos += rune_width(view, rune, xpos, view->width); /* if the line is full, reset the line and increase row count */ if (xpos > view->width) { xpos = 0, nrows++; @@ -121,7 +121,7 @@ static size_t add_row(View* view, size_t off) { /* populate the row with characters */ for (size_t xpos = 0; xpos < view->width;) { int rune = buf_getrat(&(view->buffer), off); - size_t rwidth = rune_width(rune, xpos, view->width); + size_t rwidth = rune_width(view, rune, xpos, view->width); xpos += rwidth; if (xpos <= view->width) { size_t len = view->rows[view->nrows-1]->len + 1; diff --git a/src/lib/x11.c b/src/lib/x11.c index 099bdb6..0d32430 100644 --- a/src/lib/x11.c +++ b/src/lib/x11.c @@ -39,6 +39,7 @@ struct XWin { XIC xic; XIM xim; GC gc; + XftFont* tagfont; XftFont* font; }; @@ -286,10 +287,11 @@ static void xftcolor(XftColor* xc, int id) { XftColorAllocValue(X.display, X.visual, X.colormap, &(xc->color), xc); } -size_t glyph_width(int c) { +size_t glyph_width(View* view, int c) { FcChar32 rune = (FcChar32)c; XGlyphInfo extents; - XftTextExtents32(X.display, X.font, &rune, 1, &extents); + XftFont* font = (&Regions[TAGS] == view ? X.tagfont : X.font); + XftTextExtents32(X.display, font, &rune, 1, &extents); if (c == '\t') return (TabWidth * extents.xOff); else @@ -313,15 +315,14 @@ static void draw_statbox(drawcsr* csr) { } } -static void draw_view(int i, size_t nrows, drawcsr* csr, int bg, int fg, int sel) { - size_t fheight = X.font->height; +static void draw_view(View* view, XftFont* font, size_t nrows, drawcsr* csr, int bg, int fg, int sel) { + size_t fheight = font->height; size_t csrx = SIZE_MAX, csry = SIZE_MAX; bool csr_drawn = false; /* draw the view to the window */ - View* view = win_view(i); view_resize(view, (csr->w - csr->x), nrows); view_update(view, &csrx, &csry); - draw_rect(bg, csr->x, csr->y, csr->w, (nrows * fheight) + 9); + draw_rect(bg, csr->x, csr->y, csr->w, ((nrows + 1) * fheight) + 9); for (size_t i = 0; i < nrows; i++) { Row* row = view_getrow(view, i + view->index); size_t x = (csr->x + 2), y = (csr->y + 2 + (i * fheight)); @@ -338,14 +339,14 @@ static void draw_view(int i, size_t nrows, drawcsr* csr, int bg, int fg, int sel draw_rect((i == TAGS ? TagsCsr : EditCsr), x-1, y+fheight-3, 3, 3); csr_drawn = true; } - specs[i].glyph = XftCharIndex(X.display, X.font, rune); + specs[i].glyph = XftCharIndex(X.display, font, rune); specs[i].x = x; - specs[i].y = y + X.font->ascent; + specs[i].y = y + font->ascent; x += row->cols[i].width; } XftColor fgc; xftcolor(&fgc, EditFg); - XftDrawGlyphSpec(X.xft, &fgc, X.font, specs, row->len); + XftDrawGlyphSpec(X.xft, &fgc, font, specs, row->len); XftColorFree(X.display, X.visual, X.colormap, &fgc); } csr->y += (nrows * fheight) + 3; @@ -366,7 +367,7 @@ static void draw_scroll(drawcsr* csr) { size_t vend = view->rows[view->nrows-1]->off + view->rows[view->nrows-1]->len; double scroll_vis = (double)(vend - vbeg) / (double)bend; double scroll_off = ((double)vbeg / (double)bend); - size_t thumbreg = (csr->y - Divider) + 4; + size_t thumbreg = (csr->h - Divider) + 4; size_t thumboff = (size_t)((thumbreg * scroll_off) + Divider); size_t thumbsz = (size_t)(thumbreg * scroll_vis); if (thumbsz < 5) thumbsz = 5; @@ -522,7 +523,6 @@ static void (*EventHandlers[LASTEvent])(XEvent*) = { static void xupdate(Job* job) { int nevents; - size_t fheight = X.font->height; /* process events from the queue */ XEventsQueued(X.display, QueuedAfterFlush); XGetMotionEvents(X.display, X.self, CurrentTime, CurrentTime, &nevents); @@ -532,16 +532,17 @@ static void xupdate(Job* job) { (EventHandlers[e.type])(&e); } /* determine the size of the regions */ - size_t maxrows = ((X.height - 7) / fheight); - size_t tagrows = view_limitrows(win_view(TAGS), maxrows / 4); - size_t editrows = maxrows - tagrows; + size_t maxtagrows = ((X.height/4) / X.tagfont->height); + size_t tagrows = view_limitrows(win_view(TAGS), maxtagrows); + size_t tagregsz = (tagrows * X.tagfont->height) + 7; + size_t editrows = (X.height - tagregsz) / X.font->height ; /* draw the regions to the window */ drawcsr csr = { .w = X.width, .h = X.height }; csr.x += ScrollWidth + 1; draw_statbox(&csr); - draw_view(TAGS, tagrows, &csr, TagsBg, TagsFg, TagsSel); + draw_view(&Regions[TAGS], X.tagfont, tagrows, &csr, TagsBg, TagsFg, TagsSel); draw_hrule(&csr); - draw_view(EDIT, editrows, &csr, EditBg, EditFg, EditSel); + draw_view(&Regions[EDIT], X.font, editrows, &csr, EditBg, EditFg, EditSel); draw_scroll(&csr); XCopyArea(X.display, X.pixmap, X.self, X.gc, 0, 0, X.width, X.height, 0, 0); XFlush(X.display); @@ -567,6 +568,7 @@ void win_init(KeyBinding* bindings) { X.screen = DefaultScreen(X.display); X.depth = DefaultDepth(X.display, X.screen); font_load(Fonts[FontSel = 0]); + X.tagfont = X.font; x11_window("unnamed"); /* initialize selection atoms */ for (int i = 0; i < (sizeof(Selections) / sizeof(Selections[0])); i++) -- 2.51.0