update drwl

This commit is contained in:
sewn 2024-07-22 17:16:53 +03:00
parent 4fb17188e8
commit ca47635f2e
No known key found for this signature in database
3 changed files with 138 additions and 78 deletions

152
drwl.h
View file

@ -1,6 +1,31 @@
/*
* drwl - https://codeberg.org/sewn/drwl
* See LICENSE file for copyright and license details.
*
* Copyright (c) 2023-2024 sewn <sewn@disroot.org>
* Copyright (c) 2024 notchoc <notchoc@disroot.org>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* The UTF-8 Decoder included is from Bjoern Hoehrmann:
* Copyright (c) 2008-2010 Bjoern Hoehrmann <bjoern@hoehrmann.de>
* See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
*/
#pragma once
@ -8,56 +33,52 @@
#include <fcft/fcft.h>
#include <pixman-1/pixman.h>
#define BETWEEN(X, A, B) ((A) <= (X) && (X) <= (B))
enum { ColFg, ColBg }; /* colorscheme index */
typedef struct fcft_font Fnt;
typedef struct {
pixman_image_t *pix;
struct fcft_font *font;
Fnt *font;
uint32_t *scheme;
} Drwl;
#define UTF_INVALID 0xFFFD
#define UTF_SIZ 4
#define UTF8_ACCEPT 0
#define UTF8_REJECT 12
#define UTF8_INVALID 0xFFFD
static const unsigned char utfbyte[UTF_SIZ + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
static const unsigned char utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
static const uint32_t utfmin[UTF_SIZ + 1] = { 0, 0, 0x80, 0x800, 0x10000};
static const uint32_t utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
static const uint8_t utf8d[] = {
// The first part of the table maps bytes to character classes that
// to reduce the size of the transition table and create bitmasks.
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8,
// The second part is a transition table that maps a combination
// of a state of the automaton and a character class to a state.
0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
12,36,12,12,12,12,12,12,12,12,12,12,
};
static inline uint32_t
utf8decodebyte(const char c, size_t *i)
utf8decode(uint32_t *state, uint32_t *codep, uint8_t byte)
{
for (*i = 0; *i < (UTF_SIZ + 1); ++(*i))
if (((unsigned char)c & utfmask[*i]) == utfbyte[*i])
return (unsigned char)c & ~utfmask[*i];
return 0;
}
uint32_t type = utf8d[byte];
static inline size_t
utf8decode(const char *c, uint32_t *u)
{
size_t i, j, len, type;
uint32_t udecoded;
*codep = (*state != UTF8_ACCEPT) ?
(byte & 0x3fu) | (*codep << 6) :
(0xff >> type) & (byte);
*u = UTF_INVALID;
udecoded = utf8decodebyte(c[0], &len);
if (!BETWEEN(len, 1, UTF_SIZ))
return 1;
for (i = 1, j = 1; i < UTF_SIZ && j < len; ++i, ++j) {
udecoded = (udecoded << 6) | utf8decodebyte(c[i], &type);
if (type)
return j;
}
if (j < len)
return 0;
*u = udecoded;
if (!BETWEEN(*u, utfmin[len], utfmax[len]) || BETWEEN(*u, 0xD800, 0xDFFF))
*u = UTF_INVALID;
for (i = 1; *u > utfmax[i]; ++i)
;
return len;
*state = utf8d[256 + *state + type];
return *state;
}
static int
@ -83,7 +104,7 @@ drwl_create(void)
}
static void
drwl_setfont(Drwl *drwl, struct fcft_font *font)
drwl_setfont(Drwl *drwl, Fnt *font)
{
if (drwl)
drwl->font = font;
@ -91,21 +112,21 @@ drwl_setfont(Drwl *drwl, struct fcft_font *font)
/*
* Returned font is set within the drawing context if given.
* Caller must call drwl_destroy_font on returned font when done using it,
* otherwise use drwl_destroy when instead given a drwl context.
* Caller must call drwl_font_destroy on returned font when done using it,
* otherwise use drwl_destroy when instead given a drawing context.
*/
static struct fcft_font *
drwl_load_font(Drwl *drwl, size_t fontcount,
const char *fonts[static fontcount], const char *attributes)
static Fnt *
drwl_font_create(Drwl *drwl, size_t count,
const char *names[static count], const char *attributes)
{
struct fcft_font *font = fcft_from_name(fontcount, fonts, attributes);
Fnt *font = fcft_from_name(count, names, attributes);
if (drwl)
drwl_setfont(drwl, font);
return font;
}
static void
drwl_destroy_font(struct fcft_font *font)
drwl_font_destroy(Fnt *font)
{
fcft_destroy(font);
}
@ -114,9 +135,9 @@ static inline pixman_color_t
convert_color(uint32_t clr)
{
return (pixman_color_t){
((clr >> 24) & 0xFF) * 0x101,
((clr >> 16) & 0xFF) * 0x101,
((clr >> 8) & 0xFF) * 0x101,
((clr >> 24) & 0xFF) * 0x101 * (clr & 0xFF) / 0xFF,
((clr >> 16) & 0xFF) * 0x101 * (clr & 0xFF) / 0xFF,
((clr >> 8) & 0xFF) * 0x101 * (clr & 0xFF) / 0xFF,
(clr & 0xFF) * 0x101
};
}
@ -182,9 +203,9 @@ drwl_text(Drwl *drwl,
unsigned int lpad, const char *text, int invert)
{
int ty;
int utf8charlen, render = x || y || w || h;
int render = x || y || w || h;
long x_kern;
uint32_t cp = 0, last_cp = 0;
uint32_t cp = 0, last_cp = 0, state;
pixman_color_t clr;
pixman_image_t *fg_pix = NULL;
int noellipsis = 0;
@ -210,10 +231,16 @@ drwl_text(Drwl *drwl,
fcft_subpixel_mode = FCFT_SUBPIXEL_NONE;
// U+2026 == …
eg = fcft_rasterize_char_utf32(drwl->font, 0x2026, fcft_subpixel_mode);
if (render)
eg = fcft_rasterize_char_utf32(drwl->font, 0x2026, fcft_subpixel_mode);
while (*text) {
utf8charlen = utf8decode(text, &cp);
for (const char *p = text, *pp; pp = p, *p; p++) {
for (state = UTF8_ACCEPT; *p && utf8decode(&state, &cp, *p) > UTF8_REJECT; p++);
if (!*p || state == UTF8_REJECT) {
cp = UTF8_INVALID;
if (p > pp)
p--;
}
glyph = fcft_rasterize_char_utf32(drwl->font, cp, fcft_subpixel_mode);
if (!glyph)
@ -226,10 +253,9 @@ drwl_text(Drwl *drwl,
ty = y + (h - drwl->font->height) / 2 + drwl->font->ascent;
/* draw ellipsis if remaining text doesn't fit */
if (!noellipsis && x_kern + glyph->advance.x + eg->advance.x > w && *(text + 1) != '\0') {
if (drwl_text(drwl, 0, 0, 0, 0, 0, text, 0)
- glyph->advance.x < eg->advance.x) {
if (render && !noellipsis && x_kern + glyph->advance.x + eg->advance.x > w && *(p + 1) != '\0') {
// cannot fit ellipsis after current codepoint
if (drwl_text(drwl, 0, 0, 0, 0, 0, pp, 0) + x_kern <= w) {
noellipsis = 1;
} else {
w -= eg->advance.x;
@ -254,7 +280,6 @@ drwl_text(Drwl *drwl,
PIXMAN_OP_OVER, fg_pix, glyph->pix, drwl->pix, 0, 0, 0, 0,
x + glyph->x, ty - glyph->y, glyph->width, glyph->height);
text += utf8charlen;
x += glyph->advance.x;
w -= glyph->advance.x;
}
@ -285,17 +310,18 @@ drwl_font_getwidth_clamp(Drwl *drwl, const char *text, unsigned int n)
static void
drwl_finish_drawing(Drwl *drwl)
{
if (drwl && drwl->pix)
if (!drwl)
return;
if (drwl->pix)
pixman_image_unref(drwl->pix);
drwl->pix = NULL;
}
static void
drwl_destroy(Drwl *drwl)
{
if (drwl->pix)
pixman_image_unref(drwl->pix);
if (drwl->font)
drwl_destroy_font(drwl->font);
drwl_font_destroy(drwl->font);
free(drwl);
}