st-flexipatch

My st-flexipatch configuration
git clone git://git.ethandl.dev/st-flexipatch
Log | Files | Refs | README | LICENSE

boxdraw.c (6194B)


      1 /*
      2  * Copyright 2018 Avi Halachmi (:avih) avihpit@yahoo.com https://github.com/avih
      3  * MIT/X Consortium License
      4  */
      5 
      6 #include <X11/Xft/Xft.h>
      7 
      8 /* Rounded non-negative integers division of n / d  */
      9 #define DIV(n, d) (((n) + (d) / 2) / (d))
     10 
     11 static Display *xdpy;
     12 static Colormap xcmap;
     13 static XftDraw *xd;
     14 static Visual *xvis;
     15 
     16 static void drawbox(int, int, int, int, XftColor *, XftColor *, ushort);
     17 static void drawboxlines(int, int, int, int, XftColor *, ushort);
     18 
     19 /* public API */
     20 
     21 void
     22 boxdraw_xinit(Display *dpy, Colormap cmap, XftDraw *draw, Visual *vis)
     23 {
     24 	xdpy = dpy; xcmap = cmap; xd = draw, xvis = vis;
     25 }
     26 
     27 int
     28 isboxdraw(Rune u)
     29 {
     30 	Rune block = u & ~0xff;
     31 	return (boxdraw && block == 0x2500 && boxdata[(uint8_t)u]) ||
     32 	       (boxdraw_braille && block == 0x2800);
     33 }
     34 
     35 /* the "index" is actually the entire shape data encoded as ushort */
     36 ushort
     37 boxdrawindex(const Glyph *g)
     38 {
     39 	if (boxdraw_braille && (g->u & ~0xff) == 0x2800)
     40 		return BRL | (uint8_t)g->u;
     41 	if (boxdraw_bold && (g->mode & ATTR_BOLD))
     42 		return BDB | boxdata[(uint8_t)g->u];
     43 	return boxdata[(uint8_t)g->u];
     44 }
     45 
     46 void
     47 drawboxes(int x, int y, int cw, int ch, XftColor *fg, XftColor *bg,
     48           const XftGlyphFontSpec *specs, int len)
     49 {
     50 	for ( ; len-- > 0; x += cw, specs++)
     51 		drawbox(x, y, cw, ch, fg, bg, (ushort)specs->glyph);
     52 }
     53 
     54 /* implementation */
     55 
     56 void
     57 drawbox(int x, int y, int w, int h, XftColor *fg, XftColor *bg, ushort bd)
     58 {
     59 	ushort cat = bd & ~(BDB | 0xff);  /* mask out bold and data */
     60 	if (bd & (BDL | BDA)) {
     61 		/* lines (light/double/heavy/arcs) */
     62 		drawboxlines(x, y, w, h, fg, bd);
     63 
     64 	} else if (cat == BBD) {
     65 		/* lower (8-X)/8 block */
     66 		int d = DIV((uint8_t)bd * h, 8);
     67 		XftDrawRect(xd, fg, x, y + d, w, h - d);
     68 
     69 	} else if (cat == BBU) {
     70 		/* upper X/8 block */
     71 		XftDrawRect(xd, fg, x, y, w, DIV((uint8_t)bd * h, 8));
     72 
     73 	} else if (cat == BBL) {
     74 		/* left X/8 block */
     75 		XftDrawRect(xd, fg, x, y, DIV((uint8_t)bd * w, 8), h);
     76 
     77 	} else if (cat == BBR) {
     78 		/* right (8-X)/8 block */
     79 		int d = DIV((uint8_t)bd * w, 8);
     80 		XftDrawRect(xd, fg, x + d, y, w - d, h);
     81 
     82 	} else if (cat == BBQ) {
     83 		/* Quadrants */
     84 		int w2 = DIV(w, 2), h2 = DIV(h, 2);
     85 		if (bd & TL)
     86 			XftDrawRect(xd, fg, x, y, w2, h2);
     87 		if (bd & TR)
     88 			XftDrawRect(xd, fg, x + w2, y, w - w2, h2);
     89 		if (bd & BL)
     90 			XftDrawRect(xd, fg, x, y + h2, w2, h - h2);
     91 		if (bd & BR)
     92 			XftDrawRect(xd, fg, x + w2, y + h2, w - w2, h - h2);
     93 
     94 	} else if (bd & BBS) {
     95 		/* Shades - data is 1/2/3 for 25%/50%/75% alpha, respectively */
     96 		int d = (uint8_t)bd;
     97 		XftColor xfc;
     98 		XRenderColor xrc = { .alpha = 0xffff };
     99 
    100 		xrc.red = DIV(fg->color.red * d + bg->color.red * (4 - d), 4);
    101 		xrc.green = DIV(fg->color.green * d + bg->color.green * (4 - d), 4);
    102 		xrc.blue = DIV(fg->color.blue * d + bg->color.blue * (4 - d), 4);
    103 
    104 		XftColorAllocValue(xdpy, xvis, xcmap, &xrc, &xfc);
    105 		XftDrawRect(xd, &xfc, x, y, w, h);
    106 		XftColorFree(xdpy, xvis, xcmap, &xfc);
    107 
    108 	} else if (cat == BRL) {
    109 		/* braille, each data bit corresponds to one dot at 2x4 grid */
    110 		int w1 = DIV(w, 2);
    111 		int h1 = DIV(h, 4), h2 = DIV(h, 2), h3 = DIV(3 * h, 4);
    112 
    113 		if (bd & 1)   XftDrawRect(xd, fg, x, y, w1, h1);
    114 		if (bd & 2)   XftDrawRect(xd, fg, x, y + h1, w1, h2 - h1);
    115 		if (bd & 4)   XftDrawRect(xd, fg, x, y + h2, w1, h3 - h2);
    116 		if (bd & 8)   XftDrawRect(xd, fg, x + w1, y, w - w1, h1);
    117 		if (bd & 16)  XftDrawRect(xd, fg, x + w1, y + h1, w - w1, h2 - h1);
    118 		if (bd & 32)  XftDrawRect(xd, fg, x + w1, y + h2, w - w1, h3 - h2);
    119 		if (bd & 64)  XftDrawRect(xd, fg, x, y + h3, w1, h - h3);
    120 		if (bd & 128) XftDrawRect(xd, fg, x + w1, y + h3, w - w1, h - h3);
    121 
    122 	}
    123 }
    124 
    125 void
    126 drawboxlines(int x, int y, int w, int h, XftColor *fg, ushort bd)
    127 {
    128 	/* s: stem thickness. width/8 roughly matches underscore thickness. */
    129 	/* We draw bold as 1.5 * normal-stem and at least 1px thicker.      */
    130 	/* doubles draw at least 3px, even when w or h < 3. bold needs 6px. */
    131 	int mwh = MIN(w, h);
    132 	int base_s = MAX(1, DIV(mwh, 8));
    133 	int bold = (bd & BDB) && mwh >= 6;  /* possibly ignore boldness */
    134 	int s = bold ? MAX(base_s + 1, DIV(3 * base_s, 2)) : base_s;
    135 	int w2 = DIV(w - s, 2), h2 = DIV(h - s, 2);
    136 	/* the s-by-s square (x + w2, y + h2, s, s) is the center texel.    */
    137 	/* The base length (per direction till edge) includes this square.  */
    138 
    139 	int light = bd & (LL | LU | LR | LD);
    140 	int double_ = bd & (DL | DU | DR | DD);
    141 
    142 	if (light) {
    143 		/* d: additional (negative) length to not-draw the center   */
    144 		/* texel - at arcs and avoid drawing inside (some) doubles  */
    145 		int arc = bd & BDA;
    146 		int multi_light = light & (light - 1);
    147 		int multi_double = double_ & (double_ - 1);
    148 		/* light crosses double only at DH+LV, DV+LH (ref. shapes)  */
    149 		int d = arc || (multi_double && !multi_light) ? -s : 0;
    150 
    151 		if (bd & LL)
    152 			XftDrawRect(xd, fg, x, y + h2, w2 + s + d, s);
    153 		if (bd & LU)
    154 			XftDrawRect(xd, fg, x + w2, y, s, h2 + s + d);
    155 		if (bd & LR)
    156 			XftDrawRect(xd, fg, x + w2 - d, y + h2, w - w2 + d, s);
    157 		if (bd & LD)
    158 			XftDrawRect(xd, fg, x + w2, y + h2 - d, s, h - h2 + d);
    159 	}
    160 
    161 	/* double lines - also align with light to form heavy when combined */
    162 	if (double_) {
    163 		/*
    164 		* going clockwise, for each double-ray: p is additional length
    165 		* to the single-ray nearer to the previous direction, and n to
    166 		* the next. p and n adjust from the base length to lengths
    167 		* which consider other doubles - shorter to avoid intersections
    168 		* (p, n), or longer to draw the far-corner texel (n).
    169 		*/
    170 		int dl = bd & DL, du = bd & DU, dr = bd & DR, dd = bd & DD;
    171 		if (dl) {
    172 			int p = dd ? -s : 0, n = du ? -s : dd ? s : 0;
    173 			XftDrawRect(xd, fg, x, y + h2 + s, w2 + s + p, s);
    174 			XftDrawRect(xd, fg, x, y + h2 - s, w2 + s + n, s);
    175 		}
    176 		if (du) {
    177 			int p = dl ? -s : 0, n = dr ? -s : dl ? s : 0;
    178 			XftDrawRect(xd, fg, x + w2 - s, y, s, h2 + s + p);
    179 			XftDrawRect(xd, fg, x + w2 + s, y, s, h2 + s + n);
    180 		}
    181 		if (dr) {
    182 			int p = du ? -s : 0, n = dd ? -s : du ? s : 0;
    183 			XftDrawRect(xd, fg, x + w2 - p, y + h2 - s, w - w2 + p, s);
    184 			XftDrawRect(xd, fg, x + w2 - n, y + h2 + s, w - w2 + n, s);
    185 		}
    186 		if (dd) {
    187 			int p = dr ? -s : 0, n = dl ? -s : dr ? s : 0;
    188 			XftDrawRect(xd, fg, x + w2 + s, y + h2 - p, s, h - h2 + p);
    189 			XftDrawRect(xd, fg, x + w2 - s, y + h2 - n, s, h - h2 + n);
    190 		}
    191 	}
    192 }