123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- /* See LICENSE file for copyright and license details. */
- #include <errno.h>
- #include <stdint.h>
- #include <wchar.h>
- #include <ft2build.h>
- #include FT_FREETYPE_H
- #include FT_ADVANCES_H
- #include <X11/Xlib.h>
- #include <X11/extensions/Xrender.h>
- #include "dtext.h"
- static dt_error load_char(dt_context *ctx, dt_font *fnt, wchar_t c);
- static uint8_t hash_get(dt_row map[DT_HASH_SIZE], wchar_t key, uint8_t def);
- static dt_error hash_set(dt_row map[DT_HASH_SIZE], wchar_t key, uint8_t val);
- dt_error
- dt_init(dt_context **res, Display *dpy, Window win)
- {
- dt_error err;
- dt_context *ctx;
- Visual *visual;
- Pixmap pix;
- XRenderPictureAttributes attrs;
- if (!(ctx = malloc(sizeof(*ctx))))
- return -ENOMEM;
- if ((err = FT_Init_FreeType(&ctx->ft_lib)))
- goto fail_init_ft;
- visual = XDefaultVisual(dpy, XDefaultScreen(dpy));
- ctx->win_format = XRenderFindVisualFormat(dpy, visual);
- XFree(visual);
- ctx->argb32_format = XRenderFindStandardFormat(dpy, PictStandardARGB32);
- ctx->dpy = dpy;
- ctx->pic = XRenderCreatePicture(dpy, win, ctx->win_format, 0, &attrs);
- pix = XCreatePixmap(dpy, DefaultRootWindow(dpy), 1, 1, 32);
- attrs.repeat = 1;
- ctx->fill = XRenderCreatePicture(ctx->dpy, pix, ctx->argb32_format,
- CPRepeat, &attrs);
- XFreePixmap(dpy, pix);
- *res = ctx;
- return 0;
- fail_init_ft:
- free(ctx);
- return err;
- }
- dt_error
- dt_quit(dt_context *ctx)
- {
- dt_error err = 0;
- XRenderFreePicture(ctx->dpy, ctx->fill);
- XRenderFreePicture(ctx->dpy, ctx->pic);
- XFree(ctx->argb32_format);
- XFree(ctx->win_format);
- err = FT_Done_FreeType(ctx->ft_lib);
- free(ctx);
- return err;
- }
- dt_error
- dt_load(dt_context *ctx, dt_font **res, uint8_t size, char const *name)
- {
- dt_error err;
- dt_font *fnt;
- if (!(fnt = malloc(sizeof(*fnt))))
- return -ENOMEM;
- if ((err = FT_New_Face(ctx->ft_lib, name, 0, &fnt->face)))
- goto fail_new_face;
- if ((err = FT_Set_Char_Size(fnt->face, size << 6, 0, 0, 0)))
- goto fail_char_size;
- fnt->gs = XRenderCreateGlyphSet(ctx->dpy, ctx->argb32_format);
- memset(fnt->loaded, 0, DT_HASH_SIZE * sizeof(fnt->loaded[0]));
- *res = fnt;
- return 0;
- fail_char_size:
- FT_Done_Face(fnt->face); // if this fails... just ignore
- fail_new_face:
- free(fnt);
- return err;
- }
- dt_error
- dt_free(dt_context *ctx, dt_font *fnt)
- {
- dt_error err = 0;
- XRenderFreeGlyphSet(ctx->dpy, fnt->gs);
- err = FT_Done_Face(fnt->face);
- free(fnt);
- return err;
- }
- dt_error
- dt_box(dt_font *fnt, dt_bbox *bbox, wchar_t const *txt)
- {
- dt_error err;
- size_t len;
- size_t i;
- FT_Fixed adv;
- if (!(len = wcslen(txt)))
- return -EINVAL;
- memset(bbox, 0, sizeof(*bbox));
- bbox->x = 0;
- bbox->y = - (fnt->face->ascender >> 6);
- for (i = 0; i < len; ++i) {
- if ((err = FT_Get_Advance(fnt->face, txt[i], 0, &adv)))
- return err;
- bbox->w += adv >> 16;
- }
- bbox->h = fnt->face->height >> 6;
- return 0;
- }
- dt_error
- dt_draw(dt_context *ctx, dt_font *fnt, dt_style const *style,
- uint32_t x, uint32_t y, wchar_t const *txt)
- {
- dt_error err;
- XRenderColor col;
- size_t len;
- size_t i;
- col.red = (style->red << 8) + style->red;
- col.green = (style->green << 8) + style->green;
- col.blue = (style->blue << 8) + style->blue;
- col.alpha = 0xFFFF - ((style->alpha << 8) + style->alpha);
- XRenderFillRectangle(ctx->dpy, PictOpSrc, ctx->fill, &col, 0, 0, 1, 1);
- len = wcslen(txt);
- for (i = 0; i < len; ++i)
- if ((err = load_char(ctx, fnt, txt[i])))
- return err;
- #define DO_COMPOSITE(Size, Type) \
- XRenderCompositeString ## Size(ctx->dpy, PictOpOver, ctx->fill, \
- ctx->pic, ctx->argb32_format, \
- fnt->gs, 0, 0, x, y, \
- (Type const *) txt, len)
- if (sizeof(wchar_t) == 1)
- DO_COMPOSITE(8, char);
- else if (sizeof(wchar_t) == 2)
- DO_COMPOSITE(16, uint16_t);
- else
- DO_COMPOSITE(32, uint32_t);
- #undef DO_COMPOSITE
- return 0;
- }
- static dt_error
- load_char(dt_context *ctx, dt_font *fnt, wchar_t c)
- {
- dt_error err;
- FT_GlyphSlot slot;
- Glyph gid;
- XGlyphInfo g;
- char *img;
- size_t x, y, i;
- if (hash_get(fnt->loaded, c, 0))
- return 0;
- if ((err = FT_Load_Char(fnt->face, c, FT_LOAD_RENDER)))
- return err;
- slot = fnt->face->glyph;
- gid = c;
- g.width = slot->bitmap.width;
- g.height = slot->bitmap.rows;
- g.x = - slot->bitmap_left;
- g.y = slot->bitmap_top;
- g.xOff = slot->advance.x >> 6;
- g.yOff = slot->advance.y >> 6;
- if (!(img = malloc(4 * g.width * g.height)))
- return -ENOMEM;
- for (y = 0; y < g.height; ++y)
- for (x = 0; x < g.width; ++x)
- for (i = 0; i < 4; ++i)
- img[4 * (y * g.width + x) + i] =
- slot->bitmap.buffer[y * g.width + x];
- XRenderAddGlyphs(ctx->dpy, fnt->gs, &gid, &g, 1,
- img, 4 * g.width * g.height);
- free(img);
- return hash_set(fnt->loaded, c, 1);
- }
- static uint8_t
- hash_get(dt_row map[DT_HASH_SIZE], wchar_t key, uint8_t def)
- {
- dt_row row;
- size_t i;
- row = map[key % DT_HASH_SIZE];
- for (i = 0; i < row.len; ++i)
- if (row.data[i].k == key)
- return row.data[i].v;
- return def;
- }
- static dt_error
- hash_set(dt_row map[DT_HASH_SIZE], wchar_t key, uint8_t val)
- {
- dt_row row;
- dt_pair *d;
- size_t i;
- row = map[key % DT_HASH_SIZE];
- for (i = 0; i < row.len; ++i) {
- if (row.data[i].k == key) {
- row.data[i].v = val;
- return 0;
- }
- }
- if (row.allocated == row.len) {
- d = row.data;
- if (!(d = realloc(d, (2 * row.len + 1) * sizeof(d[0]))))
- return -ENOMEM;
- row.data = d;
- row.allocated = 2 * row.len + 1;
- }
- ++row.len;
- row.data[row.len - 1].k = key;
- row.data[row.len - 1].v = val;
- map[key % DT_HASH_SIZE] = row;
- return 0;
- }
|