Merge branch 'font-fallback'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Daniel Eklöf · committed by Daniel Eklöf · parents b8696b9, 00cf9dd · Browse files · 14 files·+857 added−292 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
14 files+857 added−292 removed
- other:+857 added−292 removed
Provenance
- Commit
7d89025- Tree
76782c2- Author
Daniel Eklöf <daniel@ekloef.se>- Committer
Daniel Eklöf <daniel@ekloef.se>- Parent 1
b8696b9- Parent 2
00cf9dd
14 files changed+857 added−292 removed
modified:application.c+5 added-1 removed
92 unmodified lines93 94 bool95 application_execute(struct application *app, const struct prompt *prompt)96 {97+ const size_t clen = wcstombs(NULL, prompt->text, 0);98+ char cprompt[clen + 1];99+ wcstombs(cprompt, prompt->text, clen + 1);100+97- const char *execute = app != NULL ? app->exec : prompt->text;101+ const char *execute = app != NULL ? app->exec : cprompt;102 const char *path = app != NULL ? app->path : NULL;103 104 LOG_DBG("exec(%s)", execute);105 88 unmodified linesmodified:application.h+2 added-2 removed
18 unmodified lines19 20 struct application {21 char *path;22 char *exec;23- char *title;24- char *comment;23+ wchar_t *title;24+ wchar_t *comment;25 struct icon icon;26 unsigned count;27 };28 6 unmodified linesmodified:dmenu.c+9 added-3 removed
9 unmodified lines10 11 void12 dmenu_load_entries(struct application_list *applications)13 {14- tll(char *) entries = tll_init();14+ tll(wchar_t *) entries = tll_init();15 16 errno = 0;17 while (true) {18 char *line = NULL;11 unmodified lines30 while (len > 0 && line[len - 1] == '\n')31 line[len-- - 1] = '\0';32 33 LOG_DBG("%s", line);34+35+ size_t wlen = mbstowcs(NULL, line, 0);36+ wchar_t *wline = malloc(wlen + 1);37+ mbstowcs(wline, line, wlen + 1);34- tll_push_back(entries, line);38+ tll_push_back(entries, wline);39+40+ free(line);41 }42 43 applications->v = malloc(tll_length(entries) * sizeof(applications->v[0]));44 applications->count = tll_length(entries);16 unmodified lines61 62 bool63 dmenu_execute(const struct application *app)64 {59- printf("%s\n", app->title);65+ printf("%S\n", app->title);66 return true;67 }modified:font.c+551 added-31 removed
1 #include "font.h"2 3 #include <stdlib.h>4+#include <stdint.h>5 #include <stdbool.h>6+#include <math.h>7+#include <wchar.h>8+#include <assert.h>9+#include <threads.h>10 6-#include <fontconfig/fontconfig.h>7-#include <cairo-ft.h>11+#include <freetype/tttables.h>12 13 #define LOG_MODULE "font"14+#define LOG_ENABLE_DBG 015 #include "log.h"16+#include "stride.h"17 18+#define min(x, y) ((x) < (y) ? (x) : (y))19+20+static FT_Library ft_lib;21+static mtx_t ft_lock;22+23+static tll(const struct font *) font_cache = tll_init();24+25+static const size_t glyph_cache_size = 512;26+27 static void __attribute__((constructor))28 init(void)29 {30 FcInit();31+ FT_Init_FreeType(&ft_lib);32+ mtx_init(&ft_lock, mtx_plain);33+34+#if defined(LOG_ENABLE_DBG) && LOG_ENABLE_DBG35+ int raw_version = FcGetVersion();36+37+ /* See FC_VERSION in <fontconfig/fontconfig.h> */38+ const int major = raw_version / 10000; raw_version %= 10000;39+ const int minor = raw_version / 100; raw_version %= 100;40+ const int patch = raw_version;41+#endif42+43+ LOG_DBG("fontconfig: %d.%d.%d", major, minor, patch);44 }45 46 static void __attribute__((destructor))47 fini(void)48 {49+ assert(tll_length(font_cache) == 0);50+51+ mtx_destroy(&ft_lock);52+ FT_Done_FreeType(ft_lib);53 FcFini();54 }23-cairo_scaled_font_t *55+56+static bool57+font_destroy_no_free(struct font *font)58+{59+ assert(font->ref_counter > 0);60+ if (--font->ref_counter > 0)61+ return false;62+63+ if (font->face != NULL) {64+ mtx_lock(&ft_lock);65+ FT_Done_Face(font->face);66+ mtx_unlock(&ft_lock);67+ }68+69+ if (font->fc_pattern != NULL)70+ FcPatternDestroy(font->fc_pattern);71+ if (font->fc_fonts != NULL)72+ FcFontSetDestroy(font->fc_fonts);73+74+ free(font->name);75+76+ if (font->cache == NULL)77+ return true;78+79+ for (size_t i = 0; i < glyph_cache_size; i++) {80+ if (font->cache[i] == NULL)81+ continue;82+83+ tll_foreach(*font->cache[i], it) {84+ if (!it->item.valid)85+ continue;86+87+ void *image = pixman_image_get_data(it->item.pix);88+ pixman_image_unref(it->item.pix);89+ free(image);90+ }91+92+ tll_free(*font->cache[i]);93+ free(font->cache[i]);94+ }95+ free(font->cache);96+97+ tll_foreach(font_cache, it) {98+ if (it->item == font) {99+ tll_remove(font_cache, it);100+ break;101+ }102+ }103+104+ return true;105+}106+107+static void108+underline_strikeout_metrics(struct font *font)109+{110+ FT_Face ft_face = font->face;111+ double x_scale = ft_face->size->metrics.x_scale / 65526.;112+ double height = ft_face->size->metrics.height / 64;113+ double descent = ft_face->size->metrics.descender / 64;114+115+ LOG_DBG("ft: x-scale: %f, height: %f, descent: %f",116+ x_scale, height, descent);117+118+ font->underline.position = round(ft_face->underline_position * x_scale / 64.);119+ font->underline.thickness = ceil(ft_face->underline_thickness * x_scale / 64.);120+121+ if (font->underline.position == 0.) {122+ font->underline.position = round(descent / 2.);123+ font->underline.thickness = fabs(round(descent / 5.));124+ }125+126+ LOG_DBG("underline: pos=%d, thick=%d",127+ font->underline.position, font->underline.thickness);128+129+ TT_OS2 *os2 = FT_Get_Sfnt_Table(ft_face, ft_sfnt_os2);130+ if (os2 != NULL) {131+ font->strikeout.position = round(os2->yStrikeoutPosition * x_scale / 64.);132+ font->strikeout.thickness = ceil(os2->yStrikeoutSize * x_scale / 64.);133+ }134+135+ if (font->strikeout.position == 0.) {136+ font->strikeout.position = round(height / 2. + descent);137+ font->strikeout.thickness = font->underline.thickness;138+ }139+140+ LOG_DBG("strikeout: pos=%d, thick=%d",141+ font->strikeout.position, font->strikeout.thickness);142+}143+144+static bool145+from_font_set(FcPattern *pattern, FcFontSet *fonts, int start_idx,146+ struct font *font, bool is_fallback)147+{148+ memset(font, 0, sizeof(*font));149+150+ FcChar8 *face_file = NULL;151+ FcPattern *final_pattern = NULL;152+ int font_idx = -1;153+154+ for (int i = start_idx; i < fonts->nfont; i++) {155+ FcPattern *pat = FcFontRenderPrepare(NULL, pattern, fonts->fonts[i]);156+ assert(pat != NULL);157+158+ if (FcPatternGetString(pat, FC_FT_FACE, 0, &face_file) != FcResultMatch) {159+ if (FcPatternGetString(pat, FC_FILE, 0, &face_file) != FcResultMatch) {160+ FcPatternDestroy(pat);161+ continue;162+ }163+ }164+165+ final_pattern = pat;166+ font_idx = i;167+ break;168+ }169+170+ assert(font_idx != -1);171+ assert(final_pattern != NULL);172+173+ double dpi;174+ if (FcPatternGetDouble(final_pattern, FC_DPI, 0, &dpi) != FcResultMatch)175+ dpi = 96;176+177+ double size;178+ if (FcPatternGetDouble(final_pattern, FC_PIXEL_SIZE, 0, &size)) {179+ LOG_ERR("%s: failed to get size", face_file);180+ FcPatternDestroy(final_pattern);181+ return false;182+ }183+184+ FcBool scalable;185+ if (FcPatternGetBool(final_pattern, FC_SCALABLE, 0, &scalable) != FcResultMatch)186+ scalable = FcTrue;187+188+ double pixel_fixup;189+ if (FcPatternGetDouble(final_pattern, "pixelsizefixupfactor", 0, &pixel_fixup) != FcResultMatch)190+ pixel_fixup = 1.;191+192+ LOG_DBG("loading: %s", face_file);193+194+ mtx_lock(&ft_lock);195+ FT_Face ft_face;196+ FT_Error ft_err = FT_New_Face(ft_lib, (const char *)face_file, 0, &ft_face);197+ mtx_unlock(&ft_lock);198+ if (ft_err != 0)199+ LOG_ERR("%s: failed to create FreeType face", face_file);200+201+ if ((ft_err = FT_Set_Char_Size(ft_face, size * 64, 0, 0, 0)) != 0)202+ LOG_WARN("failed to set character size");203+204+ FcBool fc_hinting;205+ if (FcPatternGetBool(final_pattern, FC_HINTING,0, &fc_hinting) != FcResultMatch)206+ fc_hinting = FcTrue;207+208+ FcBool fc_antialias;209+ if (FcPatternGetBool(final_pattern, FC_ANTIALIAS, 0, &fc_antialias) != FcResultMatch)210+ fc_antialias = FcTrue;211+212+ int fc_hintstyle;213+ if (FcPatternGetInteger(final_pattern, FC_HINT_STYLE, 0, &fc_hintstyle) != FcResultMatch)214+ fc_hintstyle = FC_HINT_SLIGHT;215+216+ int fc_rgba;217+ if (FcPatternGetInteger(final_pattern, FC_RGBA, 0, &fc_rgba) != FcResultMatch)218+ fc_rgba = FC_RGBA_UNKNOWN;219+220+ int load_flags = 0;221+ if (!fc_antialias) {222+ if (!fc_hinting || fc_hintstyle == FC_HINT_NONE)223+ load_flags |= FT_LOAD_MONOCHROME | FT_LOAD_NO_HINTING | FT_LOAD_TARGET_NORMAL;224+ else225+ load_flags |= FT_LOAD_MONOCHROME | FT_LOAD_TARGET_MONO;226+ } else {227+ if (!fc_hinting || fc_hintstyle == FC_HINT_NONE)228+ load_flags |= FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING | FT_LOAD_TARGET_NORMAL;229+ else if (fc_hinting && fc_hintstyle == FC_HINT_SLIGHT)230+ load_flags |= FT_LOAD_DEFAULT | FT_LOAD_TARGET_LIGHT;231+ else if (fc_rgba == FC_RGBA_RGB || fc_rgba == FC_RGBA_BGR)232+ load_flags |= FT_LOAD_DEFAULT | FT_LOAD_TARGET_LCD;233+ else if (fc_rgba == FC_RGBA_VRGB || fc_rgba == FC_RGBA_VBGR)234+ load_flags |= FT_LOAD_DEFAULT | FT_LOAD_TARGET_LCD_V;235+ else236+ load_flags |= FT_LOAD_DEFAULT | FT_LOAD_TARGET_NORMAL;237+ }238+239+ FcBool fc_embeddedbitmap;240+ if (FcPatternGetBool(final_pattern, FC_EMBEDDED_BITMAP, 0, &fc_embeddedbitmap) != FcResultMatch)241+ fc_embeddedbitmap = FcTrue;242+243+ if (!fc_embeddedbitmap)244+ load_flags |= FT_LOAD_NO_BITMAP;245+246+ int render_flags = 0;247+ if (!fc_antialias)248+ render_flags |= FT_RENDER_MODE_MONO;249+ else {250+ if (fc_rgba == FC_RGBA_RGB || fc_rgba == FC_RGBA_BGR)251+ render_flags |= FT_RENDER_MODE_LCD;252+ else if (fc_rgba == FC_RGBA_VRGB || fc_rgba == FC_RGBA_VBGR)253+ render_flags |= FT_RENDER_MODE_LCD_V;254+ else255+ render_flags |= FT_RENDER_MODE_NORMAL;256+ }257+258+ int fc_lcdfilter;259+ if (FcPatternGetInteger(final_pattern, FC_LCD_FILTER, 0, &fc_lcdfilter) != FcResultMatch)260+ fc_lcdfilter = FC_LCD_DEFAULT;261+262+ switch (fc_lcdfilter) {263+ case FC_LCD_NONE: font->lcd_filter = FT_LCD_FILTER_NONE; break;264+ case FC_LCD_DEFAULT: font->lcd_filter = FT_LCD_FILTER_DEFAULT; break;265+ case FC_LCD_LIGHT: font->lcd_filter = FT_LCD_FILTER_LIGHT; break;266+ case FC_LCD_LEGACY: font->lcd_filter = FT_LCD_FILTER_LEGACY; break;267+ }268+269+ FcPatternDestroy(final_pattern);270+271+ int max_x_advance = ft_face->size->metrics.max_advance / 64;272+ int height = ft_face->size->metrics.height / 64;273+ int descent = ft_face->size->metrics.descender / 64;274+ int ascent = ft_face->size->metrics.ascender / 64;275+276+ font->face = ft_face;277+ font->load_flags = load_flags | FT_LOAD_COLOR;278+ font->render_flags = render_flags;279+ font->pixel_size_fixup = scalable ? pixel_fixup : 1.;280+ font->bgr = fc_rgba == FC_RGBA_BGR || fc_rgba == FC_RGBA_VBGR;281+ font->fc_idx = font_idx;282+ font->is_fallback = is_fallback;283+ font->ref_counter = 1;284+285+ font->fextents.height = height * font->pixel_size_fixup;286+ font->fextents.descent = -descent * font->pixel_size_fixup;287+ font->fextents.ascent = ascent * font->pixel_size_fixup;288+ font->fextents.max_x_advance = max_x_advance * font->pixel_size_fixup;289+290+ LOG_DBG("metrics: height: %d, descent: %d, ascent: %d, x-advance: %d",291+ height, descent, ascent, max_x_advance);292+293+ if (!is_fallback) {294+ font->fc_pattern = pattern;295+ font->fc_fonts = fonts;296+ font->cache = calloc(glyph_cache_size, sizeof(font->cache[0]));297+ }298+299+ underline_strikeout_metrics(font);300+ return true;301+}302+303+struct font *304 font_from_name(const char *name)305 {306+ LOG_DBG("instantiating %s", name);307+308+ tll_foreach(font_cache, it) {309+ if (strcmp(it->item->name, name) == 0)310+ return font_clone(it->item);311+ }312+313 FcPattern *pattern = FcNameParse((const unsigned char *)name);314 if (pattern == NULL) {315 LOG_ERR("%s: failed to lookup font", name);316 return NULL;7 unmodified lines324 325 FcDefaultSubstitute(pattern);326 327 FcResult result;41- FcPattern *final_pattern = FcFontMatch(NULL, pattern, &result);42- FcPatternDestroy(pattern);43-44- if (final_pattern == NULL) {328+ FcFontSet *fonts = FcFontSort(NULL, pattern, FcTrue, NULL, &result);329+ if (result != FcResultMatch) {330 LOG_ERR("%s: failed to match font", name);331+ FcPatternDestroy(pattern);332 return NULL;333 }334 49- double size;50- if (FcPatternGetDouble(final_pattern, FC_PIXEL_SIZE, 0, &size)) {51- LOG_ERR("%s: failed to get size", name);52- FcPatternDestroy(final_pattern);335+ struct font *font = malloc(sizeof(*font));336+ if (!from_font_set(pattern, fonts, 0, font, false)) {337+ free(font);338+ FcFontSetDestroy(fonts);339+ FcPatternDestroy(pattern);340 return NULL;341 }342 56- cairo_font_face_t *face = cairo_ft_font_face_create_for_pattern(57- final_pattern);343+ font->name = strdup(name);344+ tll_push_back(font_cache, font);345+ return font;346+}347 59- FcPatternDestroy(final_pattern);348+static size_t349+hash_index(wchar_t wc)350+{351+ return wc % glyph_cache_size;352+}353 61- if (cairo_font_face_status(face) != CAIRO_STATUS_SUCCESS) {62- LOG_ERR("%s: failed to create cairo font face", name);63- cairo_font_face_destroy(face);64- return NULL;354+static bool355+glyph_for_wchar(const struct font *font, wchar_t wc, struct glyph *glyph)356+{357+ /*358+ * LCD filter is per library instance. Thus we need to re-set it359+ * every time...360+ *361+ * Also note that many freetype builds lack this feature362+ * (FT_CONFIG_OPTION_SUBPIXEL_RENDERING must be defined, and isn't363+ * by default) */364+ FT_Error err = FT_Library_SetLcdFilter(ft_lib, font->lcd_filter);365+ if (err != 0 && err != FT_Err_Unimplemented_Feature)366+ goto err;367+368+ FT_UInt idx = FT_Get_Char_Index(font->face, wc);369+ if (idx == 0) {370+ if (font->is_fallback)371+ return false;372+373+ /* No glyph in this font, try fontconfig fallback fonts */374+375+ /* Try fontconfig fallback fonts */376+ assert(font->fc_pattern != NULL);377+ assert(font->fc_fonts != NULL);378+ assert(font->fc_idx != -1);379+380+ for (int i = font->fc_idx + 1; i < font->fc_fonts->nfont; i++) {381+ struct font fallback;382+ if (!from_font_set(font->fc_pattern, font->fc_fonts, i, &fallback, true))383+ continue;384+385+ if (glyph_for_wchar(&fallback, wc, glyph)) {386+ LOG_DBG("%C: used fontconfig fallback", wc);387+ font_destroy_no_free(&fallback);388+ return true;389+ }390+391+ font_destroy_no_free(&fallback);392+ }393+394+ LOG_WARN("%C: no glyph found (in neither the main font, "395+ "nor any fallback fonts)", wc);396 }397 67- cairo_matrix_t matrix, ctm;68- cairo_matrix_init_identity(&ctm);69- cairo_matrix_init_scale(&matrix, size, size);398+ err = FT_Load_Glyph(font->face, idx, font->load_flags);399+ if (err != 0) {400+ LOG_ERR("load failed");401+ goto err;402+ }403 71- cairo_font_options_t *options = cairo_font_options_create();72- cairo_scaled_font_t *scaled_font = cairo_scaled_font_create(73- face, &matrix, &ctm, options);404+ err = FT_Render_Glyph(font->face->glyph, font->render_flags);405+ if (err != 0)406+ goto err;407 75- cairo_font_options_destroy(options);76- cairo_font_face_destroy(face);408+ assert(font->face->glyph->format == FT_GLYPH_FORMAT_BITMAP);409 78- if (cairo_scaled_font_status(scaled_font) != CAIRO_STATUS_SUCCESS) {79- LOG_ERR("%s: failed to create scaled font", name);80- cairo_scaled_font_destroy(scaled_font);81- return NULL;410+ FT_Bitmap *bitmap = &font->face->glyph->bitmap;411+ if (bitmap->width == 0)412+ goto err;413+414+ pixman_format_code_t pix_format;415+ int width;416+ int rows;417+418+ switch (bitmap->pixel_mode) {419+ case FT_PIXEL_MODE_MONO:420+ pix_format = PIXMAN_a1;421+ width = bitmap->width;422+ rows = bitmap->rows;423+ break;424+425+ case FT_PIXEL_MODE_GRAY:426+ pix_format = PIXMAN_a8;427+ width = bitmap->width;428+ rows = bitmap->rows;429+ break;430+431+ case FT_PIXEL_MODE_LCD:432+ pix_format = PIXMAN_x8r8g8b8;433+ width = bitmap->width / 3;434+ rows = bitmap->rows;435+ break;436+437+ case FT_PIXEL_MODE_LCD_V:438+ pix_format = PIXMAN_x8r8g8b8;439+ width = bitmap->width;440+ rows = bitmap->rows / 3;441+ break;442+443+ case FT_PIXEL_MODE_BGRA:444+ pix_format = PIXMAN_a8r8g8b8;445+ width = bitmap->width;446+ rows = bitmap->rows;447+ break;448+449+ default:450+ LOG_ERR("unimplemented: FT pixel mode: %d", bitmap->pixel_mode);451+ goto err;452+ break;453 }454 84- return scaled_font;455+ int stride = stride_for_format_and_width(pix_format, width);456+ assert(stride >= bitmap->pitch);457+458+ uint8_t *data = malloc(rows * stride);459+460+ /* Convert FT bitmap to pixman image */461+ switch (bitmap->pixel_mode) {462+ case FT_PIXEL_MODE_MONO:463+ for (size_t r = 0; r < bitmap->rows; r++) {464+ for (size_t c = 0; c < (bitmap->width + 7) / 8; c++) {465+ uint8_t v = bitmap->buffer[r * bitmap->pitch + c];466+ uint8_t reversed = 0;467+ for (size_t i = 0; i < min(8, bitmap->width - c * 8); i++)468+ reversed |= ((v >> (7 - i)) & 1) << i;469+470+ data[r * stride + c] = reversed;471+ }472+ }473+ break;474+475+ case FT_PIXEL_MODE_GRAY:476+ for (size_t r = 0; r < bitmap->rows; r++) {477+ for (size_t c = 0; c < bitmap->width; c++)478+ data[r * stride + c] = bitmap->buffer[r * bitmap->pitch + c];479+ }480+ break;481+482+ case FT_PIXEL_MODE_BGRA:483+ assert(stride == bitmap->pitch);484+ memcpy(data, bitmap->buffer, bitmap->rows * bitmap->pitch);485+ break;486+487+ case FT_PIXEL_MODE_LCD:488+ for (size_t r = 0; r < bitmap->rows; r++) {489+ for (size_t c = 0; c < bitmap->width; c += 3) {490+ unsigned char _r = bitmap->buffer[r * bitmap->pitch + c + (font->bgr ? 2 : 0)];491+ unsigned char _g = bitmap->buffer[r * bitmap->pitch + c + 1];492+ unsigned char _b = bitmap->buffer[r * bitmap->pitch + c + (font->bgr ? 0 : 2)];493+494+ uint32_t *p = (uint32_t *)&data[r * stride + 4 * (c / 3)];495+ *p = _r << 16 | _g << 8 | _b;496+ }497+ }498+ break;499+500+ case FT_PIXEL_MODE_LCD_V:501+ /* Unverified */502+ for (size_t r = 0; r < bitmap->rows; r += 3) {503+ for (size_t c = 0; c < bitmap->width; c++) {504+ unsigned char _r = bitmap->buffer[(r + (font->bgr ? 2 : 0)) * bitmap->pitch + c];505+ unsigned char _g = bitmap->buffer[(r + 1) * bitmap->pitch + c];506+ unsigned char _b = bitmap->buffer[(r + (font->bgr ? 0 : 2)) * bitmap->pitch + c];507+508+ uint32_t *p = (uint32_t *)&data[r / 3 * stride + 4 * c];509+ *p = _r << 16 | _g << 8 | _b;510+ }511+ }512+ break;513+514+ default:515+ abort();516+ break;517+ }518+519+ pixman_image_t *pix = pixman_image_create_bits_no_clear(520+ pix_format, width, rows, (uint32_t *)data, stride);521+522+ if (pix == NULL) {523+ free(data);524+ goto err;525+ }526+527+ pixman_image_set_component_alpha(528+ pix,529+ bitmap->pixel_mode == FT_PIXEL_MODE_LCD ||530+ bitmap->pixel_mode == FT_PIXEL_MODE_LCD_V);531+532+ if (font->pixel_size_fixup != 1.) {533+ struct pixman_transform scale;534+ pixman_transform_init_scale(535+ &scale,536+ pixman_double_to_fixed(1. / font->pixel_size_fixup),537+ pixman_double_to_fixed(1. / font->pixel_size_fixup));538+ pixman_image_set_transform(pix, &scale);539+ }540+541+ *glyph = (struct glyph){542+ .wc = wc,543+ .cols = wcwidth(wc),544+ .pix = pix,545+ .x = font->face->glyph->bitmap_left / font->pixel_size_fixup,546+ .y = font->face->glyph->bitmap_top * font->pixel_size_fixup,547+ .x_advance = font->face->glyph->advance.x / 64,548+ .width = width,549+ .height = rows,550+ .valid = true,551+ };552+553+ return true;554+555+err:556+ *glyph = (struct glyph){557+ .wc = wc,558+ .valid = false,559+ };560+ return false;561 }562+563+const struct glyph *564+font_glyph_for_wc(struct font *font, wchar_t wc)565+{566+ assert(font->cache != NULL);567+ size_t hash_idx = hash_index(wc);568+ hash_entry_t *hash_entry = font->cache[hash_idx];569+570+ if (hash_entry != NULL) {571+ tll_foreach(*hash_entry, it) {572+ if (it->item.wc == wc)573+ return it->item.valid ? &it->item : NULL;574+ }575+ }576+577+ struct glyph glyph;578+ bool got_glyph = glyph_for_wchar(font, wc, &glyph);579+580+ if (hash_entry == NULL) {581+ hash_entry = calloc(1, sizeof(*hash_entry));582+583+ assert(font->cache[hash_idx] == NULL);584+ font->cache[hash_idx] = hash_entry;585+ }586+587+ assert(hash_entry != NULL);588+ tll_push_back(*hash_entry, glyph);589+ return got_glyph ? &tll_back(*hash_entry) : NULL;590+}591+592+struct font *593+font_clone(const struct font *_font)594+{595+ struct font *font = (struct font *)_font;596+ font->ref_counter++;597+ return font;598+}599+600+void601+font_destroy(struct font *font)602+{603+ if (font_destroy_no_free(font))604+ free(font);605+}modified:font.h+67 added-2 removed
1 #pragma once2 3-#include <cairo.h>3+#include <stdbool.h>4+#include <threads.h>5 6+#include <ft2build.h>7+#include FT_FREETYPE_H8+#include FT_LCD_FILTER_H9+#include <fontconfig/fontconfig.h>10+#include <pixman.h>11+12+#include "tllist.h"13+14+struct glyph {15+ wchar_t wc;16+ int cols;17+18+ pixman_image_t *pix;19+ int x;20+ int y;21+ int x_advance;22+ int width;23+ int height;24+25+ bool valid;26+};27+28+typedef tll(struct glyph) hash_entry_t;29+30+struct font {31+ char *name;32+33+ FcPattern *fc_pattern;34+ FcFontSet *fc_fonts;35+ int fc_idx;36+37+ FT_Face face;38+ int load_flags;39+ int render_flags;40+ FT_LcdFilter lcd_filter;41+ double pixel_size_fixup; /* Scale factor - should only be used with ARGB32 glyphs */42+ bool bgr; /* True for FC_RGBA_BGR and FC_RGBA_VBGR */43+44+ struct {45+ int height;46+ int descent;47+ int ascent;48+ int max_x_advance;49+ } fextents;50+51+ struct {52+ int position;53+ int thickness;54+ } underline;55+56+ struct {57+ int position;58+ int thickness;59+ } strikeout;60+61+ hash_entry_t **cache;62+63+ bool is_fallback;64+ int ref_counter;65+};66+5-cairo_scaled_font_t *font_from_name(const char *name);67+struct font *font_from_name(const char *name);68+struct font *font_clone(const struct font *font);69+const struct glyph *font_glyph_for_wc(struct font *font, wchar_t wc);70+void font_destroy(struct font *font);modified:main.c+88 added-59 removed
1 #include <stdlib.h>2 #include <stdio.h>3 #include <string.h>4 #include <stdbool.h>5+#include <wctype.h>6 #include <unistd.h>7 #include <errno.h>8 #include <getopt.h>9 232 unmodified lines242 243 static size_t244 prompt_next_char(const struct prompt *prompt)245 {245- int clen = mblen(&prompt->text[prompt->cursor], MB_CUR_MAX);246- if (clen < 0) {247- LOG_ERRNO("prompt: %s", prompt->text);246+ if (prompt->text[prompt->cursor] == L'\0')247 return prompt->cursor;249- }250-251- return prompt->cursor + clen;248+ return prompt->cursor + 1;249 }250 251 static size_t252 prompt_prev_char(const struct prompt *prompt)253 {254 if (prompt->cursor == 0)255 return 0;256 260- size_t cursor = prompt->cursor;261- while (cursor > 0) {262- int clen = mblen(&prompt->text[--cursor], MB_CUR_MAX);263- if (clen >= 0)264- break;265- }266-267- return cursor;257+ return prompt->cursor - 1;258 }259 260 static size_t261 prompt_prev_word(const struct prompt *prompt)262 {263 size_t prev_char = prompt_prev_char(prompt);274- const char *space = &prompt->text[prev_char];264+ const wchar_t *space = &prompt->text[prev_char];265 266 /* Ignore initial spaces */277- while (space >= prompt->text && *space == ' ')267+ while (space >= prompt->text && iswspace(*space))268 space--;269 270+ /* Skip non-spaces */280- while (space >= prompt->text && *space != ' ')271+ while (space >= prompt->text && !iswspace(*space))272 space--;273 274 return space - prompt->text + 1;275 }276 277 static size_t278 prompt_next_word(const struct prompt *prompt)279 {289- const char *space = strchr(&prompt->text[prompt->cursor], ' ');290- if (space == NULL)291- return strlen(prompt->text);292- else293- return space - prompt->text + 1;280+ const wchar_t *end = prompt->text + wcslen(prompt->text);281+ const wchar_t *space = &prompt->text[prompt->cursor];282+283+ /* Ignore initial non-spaces */284+ while (space < end && !iswspace(*space))285+ space++;286+287+ /* Skip spaces */288+ while (space < end && iswspace(*space))289+ space++;290+291+ return space - prompt->text;292 }293 294 static void295 keyboard_key(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial,51 unmodified lines347 refresh(c);348 }349 350 else if (sym == XKB_KEY_End || (sym == XKB_KEY_e && effective_mods == ctrl)) {353- c->prompt.cursor = strlen(c->prompt.text);351+ c->prompt.cursor = wcslen(c->prompt.text);352 refresh(c);353 }354 355 else if ((sym == XKB_KEY_b && effective_mods == alt) ||69 unmodified lines425 }426 427 else if ((sym == XKB_KEY_d && effective_mods == ctrl) ||428 (sym == XKB_KEY_Delete && effective_mods == 0)) {431- if (c->prompt.cursor < strlen(c->prompt.text)) {429+ if (c->prompt.cursor < wcslen(c->prompt.text)) {430 size_t next_char = prompt_next_char(&c->prompt);431 memmove(&c->prompt.text[c->prompt.cursor],432 &c->prompt.text[next_char],435- strlen(c->prompt.text) - next_char + 1);433+ (wcslen(c->prompt.text) - next_char + 1) * sizeof(wchar_t));434 update_matches(c);435 refresh(c);436 }437 }438 439 else if (sym == XKB_KEY_BackSpace && effective_mods == 0) {440 if (c->prompt.cursor > 0) {441 size_t prev_char = prompt_prev_char(&c->prompt);444- c->prompt.text[prev_char] = '\0';442+ c->prompt.text[prev_char] = L'\0';443 c->prompt.cursor = prev_char;444 445 update_matches(c);446 refresh(c);4 unmodified lines451 effective_mods == alt)) {452 size_t new_cursor = prompt_prev_word(&c->prompt);453 memmove(&c->prompt.text[new_cursor],454 &c->prompt.text[c->prompt.cursor],457- strlen(c->prompt.text) - c->prompt.cursor + 1);455+ (wcslen(c->prompt.text) - c->prompt.cursor + 1) * sizeof(wchar_t));456 c->prompt.cursor = new_cursor;457 update_matches(c);458 refresh(c);459 }2 unmodified lines462 (sym == XKB_KEY_Delete && effective_mods == ctrl)) {463 size_t next_word = prompt_next_word(&c->prompt);464 memmove(&c->prompt.text[c->prompt.cursor],465 &c->prompt.text[next_word],468- strlen(c->prompt.text) - next_word + 1);466+ (wcslen(c->prompt.text) - next_word + 1) * sizeof(wchar_t));467 update_matches(c);468 refresh(c);469 }470 471 else if (sym == XKB_KEY_k && effective_mods == ctrl) {474- c->prompt.text[c->prompt.cursor] = '\0';472+ c->prompt.text[c->prompt.cursor] = L'\0';473 update_matches(c);474 refresh(c);475 }476 24 unmodified lines501 502 if (count == 0)503 return;504 507- const size_t new_len = strlen(c->prompt.text) + count + 1;508- char *new_text = realloc(c->prompt.text, new_len);505+ const char *b = buf;506+ mbstate_t ps = {0};507+ size_t wlen = mbsnrtowcs(NULL, &b, count, 0, &ps);508+509+ const size_t new_len = wcslen(c->prompt.text) + wlen + 1;510+ wchar_t *new_text = realloc(c->prompt.text, new_len * sizeof(wchar_t));511 if (new_text == NULL)512 return;513 512- memmove(&new_text[c->prompt.cursor + count],514+ memmove(&new_text[c->prompt.cursor + wlen],515 &new_text[c->prompt.cursor],514- strlen(new_text) - c->prompt.cursor + 1);515- memcpy(&new_text[c->prompt.cursor], buf, count);516+ (wcslen(new_text) - c->prompt.cursor + 1) * sizeof(wchar_t));517 518+ b = buf;519+ ps = (mbstate_t){0};520+ mbsnrtowcs(&new_text[c->prompt.cursor], &b, count, wlen + 1, &ps);521+522 c->prompt.text = new_text;518- c->prompt.cursor += count;523+ c->prompt.cursor += wlen;524 520- LOG_DBG("prompt: \"%s\" (cursor=%zu, length=%zu)",525+ LOG_DBG("prompt: \"%S\" (cursor=%zu, length=%zu)",526 c->prompt.text, c->prompt.cursor, new_len);527 528 update_matches(c);529 refresh(c);237 unmodified lines767 const struct match *b = _b;768 return b->application->count - a->application->count;769 }770 771+static wchar_t *772+wcscasestr(const wchar_t *haystack, const wchar_t *needle)773+{774+ const size_t hay_len = wcslen(haystack);775+ const size_t needle_len = wcslen(needle);776+777+ if (needle_len > hay_len)778+ return NULL;779+780+ for (size_t i = 0; i < wcslen(haystack) - wcslen(needle) + 1; i++) {781+ bool matched = true;782+ for (size_t j = 0; j < wcslen(needle); j++) {783+ if (towlower(haystack[i + j]) != towlower(needle[j])) {784+ matched = false;785+ break;786+ }787+ }788+789+ if (matched)790+ return (wchar_t *)&haystack[i];791+ }792+793+ return NULL;794+}795+796 static void797 update_matches(struct context *c)798 {799 /* Nothing entered; all programs found matches */770- if (strlen(c->prompt.text) == 0) {800+ if (wcslen(c->prompt.text) == 0) {801 802 for (size_t i = 0; i < c->applications.count; i++) {803 c->matches[i] = (struct match){804 .application = &c->applications.v[i],19 unmodified lines824 struct application *app = &c->applications.v[i];825 size_t start_title = -1;826 size_t start_comment = -1;827 798- const char *m = strcasestr(app->title, c->prompt.text);828+ const wchar_t *m = wcscasestr(app->title, c->prompt.text);829 if (m != NULL)830 start_title = m - app->title;831 832 if (app->comment != NULL) {803- m = strcasestr(app->comment, c->prompt.text);833+ m = wcscasestr(app->comment, c->prompt.text);834 if (m != NULL)835 start_comment = m - app->comment;836 }837 66 unmodified lines904 905 /* Window content */906 render_prompt(c->render, buf, &c->prompt);907 render_match_list(c->render, buf, c->matches, c->match_count,878- strlen(c->prompt.text), c->selected);908+ wcslen(c->prompt.text), c->selected);909 910 cairo_surface_flush(buf->cairo_surface);911 912 if (c->frame_is_scheduled) {73 unmodified lines986 987 int count;988 sscanf(count_str, "%u", &count);989 990+ size_t wlen = mbstowcs(NULL, title, 0);991+ wchar_t wtitle[wlen + 1];992+ mbstowcs(wtitle, title, wlen + 1);993+994 for (; app_idx < apps->count; app_idx++) {961- int cmp = strcmp(apps->v[app_idx].title, title);995+ int cmp = wcscmp(apps->v[app_idx].title, wtitle);996 997 if (cmp == 0) {998 apps->v[app_idx].count = count;999 app_idx++;34 unmodified lines1034 continue;1035 1036 char count_as_str[11];1037 sprintf(count_as_str, "%u", apps->v[i].count);1004-1005- const size_t title_len = strlen(apps->v[i].title);1038 const size_t count_len = strlen(count_as_str);1039 1008- if (write(fd, apps->v[i].title, title_len) != title_len ||1040+ size_t clen = wcstombs(NULL, apps->v[i].title, 0);1041+ char ctitle[clen + 1];1042+ wcstombs(ctitle, apps->v[i].title, clen + 1);1043+1044+1045+ if (write(fd, ctitle, clen) != clen ||1046 write(fd, "|", 1) != 1 ||1047 write(fd, count_as_str, count_len) != count_len ||1048 write(fd, "\n", 1) != 1)1049 {177 unmodified lines1227 .status = KEEP_RUNNING,1228 .wl = {0},1229 .dmenu_mode = dmenu_mode,1230 .prompt = {1194- .prompt = strdup("> "),1195- .text = calloc(1, 1),1231+ .prompt = wcsdup(L"> "),1232+ .text = calloc(1, sizeof(wchar_t)),1233 .cursor = 01234 },1235 .repeat = {1236 .pipe_read_fd = repeat_pipe_fds[0],7 unmodified lines1244 1245 thrd_t keyboard_repeater_id;1246 thrd_create(&keyboard_repeater_id, &keyboard_repeater, &c);1247 1211- cairo_scaled_font_t *font = font_from_name(font_name);1212- cairo_surface_t *surf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1);1213- cairo_t *cr = cairo_create(surf);1214- cairo_font_extents_t fextents;1215- cairo_set_scaled_font(cr, font);1216- cairo_font_extents(cr, &fextents);1217- cairo_destroy(cr);1218- cairo_surface_destroy(surf);1248+ struct font *font = font_from_name(font_name);1249+ LOG_DBG("height: %d, ascent: %d, descent: %d",1250+ font->fextents.height, font->fextents.ascent, font->fextents.descent);1251 1220- LOG_DBG("height: %f, ascent: %f, descent: %f",1221- fextents.height, fextents.ascent, fextents.descent);1222-1252 if (dmenu_mode)1253 dmenu_load_entries(&c.applications);1254 else1226- xdg_find_programs(icon_theme, fextents.height, &c.applications);1255+ xdg_find_programs(icon_theme, font->fextents.height, &c.applications);1256 c.matches = malloc(c.applications.count * sizeof(c.matches[0]));1257 read_cache(&c.applications);1258 1259 c.wl.display = wl_display_connect(NULL);100 unmodified lines1360 /* Trigger a 'configure' event, after which we'll have the width */1361 wl_surface_commit(c.wl.surface);1362 wl_display_roundtrip(c.wl.display);1363 1335- const double line_height = 2 * y_margin + fextents.height;1364+ const double line_height = 2 * y_margin + font->fextents.height;1365 max_matches = (height - 2 * border_width - line_height) / line_height;1366 LOG_DBG("max matches: %d", max_matches);1367 1368 struct options options = {128 unmodified linesmodified:meson.build+6 added-1 removed
38 unmodified lines39 language: 'c',40 )41 42 threads = dependency('threads')43+pixman = dependency('pixman-1')44+freetype = dependency('freetype2')45 fontconfig = dependency('fontconfig')46 cairo = dependency('cairo')47 cairo_ft = dependency('cairo-ft')48 librsvg = dependency('librsvg-2.0')39 unmodified lines88 'main.c',89 'prompt.h',90 'render.c', 'render.h',91 'shm.c', 'shm.h',92+ 'stride.h',93 'tllist.h',94 'xdg.c', 'xdg.h',95 wl_proto_src + wl_proto_headers,96 dependencies: [threads,97+ pixman,98+ freetype,99+ fontconfig,100 cairo,101 cairo_ft,96- fontconfig,102 librsvg,103 wayland_client,104 wayland_cursor,105 wlroots,5 unmodified linesmodified:prompt.h+3 added-2 removed
1 #pragma once2 3 #include <stddef.h>4+#include <wchar.h>5 6 struct prompt {6- char *prompt;7- char *text;7+ wchar_t *prompt;8+ wchar_t *text;9 size_t cursor;10 };modified:render.c+94 added-182 removed
8 unmodified lines9 #include "font.h"10 11 struct render {12 struct options options;13- cairo_scaled_font_t *regular_font;13+ struct font *regular_font;14 };15 16 void17 render_background(const struct render *render, struct buffer *buf)42 unmodified lines60 cairo_set_source_rgba(buf->cairo, bg->r, bg->g, bg->b, bg->a);61 cairo_fill(buf->cairo);62 }63 64-void65-render_prompt(const struct render *render, struct buffer *buf,66- const struct prompt *prompt)64+static pixman_color_t65+rgba2pixman(struct rgba rgba)66 {68- const double x_margin = render->options.x_margin;69- const double y_margin = render->options.y_margin;70- const double border_size = render->options.border_size;67+ uint16_t r = rgba.r * 65535.0;68+ uint16_t g = rgba.g * 65535.0;69+ uint16_t b = rgba.b * 65535.0;70+ uint16_t a = rgba.a * 65535.0;71 72- cairo_set_scaled_font(buf->cairo, render->regular_font);72+ uint16_t a_div = 0xffff / a;73 74- cairo_font_extents_t fextents;75- cairo_font_extents(buf->cairo, &fextents);74+ return (pixman_color_t){75+ .red = r / a_div,76+ .green = g / a_div,77+ .blue = b / a_div,78+ .alpha = a,79+ };80+}81 77- /* Text to render is the prompt followed by the entered text */78- char text[strlen(prompt->prompt) + strlen(prompt->text) + 1];79- strcpy(text, prompt->prompt);80- strcat(text, prompt->text);81-82- cairo_glyph_t *glyphs = NULL;83- cairo_text_cluster_t *clusters = NULL;84- cairo_text_cluster_flags_t cluster_flags;85- int num_glyphs = 0;86- int num_clusters = 0;87-88- cairo_status_t cr_status = cairo_scaled_font_text_to_glyphs(89- render->regular_font, 0, 0, text, -1, &glyphs, &num_glyphs,90- &clusters, &num_clusters, &cluster_flags);91- if (cr_status != CAIRO_STATUS_SUCCESS) {92- LOG_ERR("cairo failure: TODO: error message");93- return;82+static void83+render_glyph(pixman_image_t *pix, const struct glyph *glyph, int x, int y, const pixman_color_t *color)84+{85+ if (pixman_image_get_format(glyph->pix) == PIXMAN_a8r8g8b8) {86+ /* Glyph surface is a pre-rendered image (typically a color emoji...) */87+ pixman_image_composite32(88+ PIXMAN_OP_OVER, glyph->pix, NULL, pix, 0, 0, 0, 0,89+ x + glyph->x, y - glyph->y,90+ glyph->width, glyph->height);91+ } else {92+ /* Glyph surface is an alpha mask */93+ pixman_image_t *src = pixman_image_create_solid_fill(color);94+ pixman_image_composite32(95+ PIXMAN_OP_OVER, src, glyph->pix, pix, 0, 0, 0, 0,96+ x + glyph->x, y - glyph->y,97+ glyph->width, glyph->height);98+ pixman_image_unref(src);99 }100+}101 96- cairo_text_extents_t extents;97- cairo_scaled_font_glyph_extents(98- render->regular_font, glyphs, num_glyphs, &extents);102+void103+render_prompt(const struct render *render, struct buffer *buf,104+ const struct prompt *prompt)105+{106+ struct font *font = render->regular_font;107+ const size_t prompt_len = wcslen(prompt->prompt);108+ const size_t text_len = wcslen(prompt->text);109 100- for (int i = 0; i < num_glyphs; i++) {101- glyphs[i].x += border_size + x_margin;102- glyphs[i].y += border_size + y_margin + fextents.height - fextents.descent;103- }110+ cairo_surface_flush(buf->cairo_surface);111 105- const struct rgba *tc = &render->options.text_color;112+ pixman_color_t fg = rgba2pixman(render->options.text_color);113+ pixman_image_t *pix = pixman_image_create_bits_no_clear(114+ PIXMAN_a8r8g8b8, buf->width, buf->height, buf->mmapped, buf->stride);115+ assert(pix != NULL);116 107- cairo_set_source_rgba(buf->cairo, tc->r, tc->g, tc->b, tc->a);108- cairo_set_operator(buf->cairo, CAIRO_OPERATOR_OVER);109- cairo_show_text_glyphs(110- buf->cairo, text, -1, glyphs, num_glyphs,111- clusters, num_clusters, cluster_flags);117+ int x = render->options.border_size + render->options.x_margin;118+ int y = render->options.border_size + render->options.y_margin + font->fextents.ascent;119 113- /* prompt->cursor is the *byte* position, but we need the114- * *character| position */115- size_t cursor = 0;116- size_t cursor_at_glyph = 0;120+ for (size_t i = 0; i < prompt_len + text_len; i++) {121+ wchar_t wc = i < prompt_len ? prompt->prompt[i] : prompt->text[i - prompt_len];122+ const struct glyph *glyph = font_glyph_for_wc(font, wc);123+ if (glyph == NULL)124+ continue;125 118- const size_t prompt_len = strlen(prompt->prompt);119- const size_t text_len = strlen(prompt->text);126+ render_glyph(pix, glyph, x, y, &fg);127+ x += glyph->x_advance;128 121- /* Count glyphs that make up the prompt */122- for (size_t i = 0; i < prompt_len;) {123- int clen = mblen(&prompt->prompt[i], MB_CUR_MAX);124- if (clen < 0) {125- LOG_ERRNO("prompt: %s", prompt->prompt);126- break;129+ /* Cursor */130+ if (prompt->cursor + prompt_len - 1 == i) {131+ pixman_image_fill_rectangles(132+ PIXMAN_OP_SRC, pix, &fg,133+ 1, &(pixman_rectangle16_t){134+ x, y - font->fextents.ascent,135+ font->underline.thickness, font->fextents.ascent + font->fextents.descent});136 }128-129- i += clen;130- cursor_at_glyph++;137 }138 133- /* Count glyphs *before* the cursor */134- while (cursor < prompt->cursor && cursor < text_len) {135- int clen = mblen(&prompt->text[cursor], MB_CUR_MAX);136- if (clen < 0) {137- LOG_ERRNO("prompt: %s", prompt->text);138- break;139- }140-141- cursor += clen;142- cursor_at_glyph++;143- }144-145- int cursor_x;146- if (cursor_at_glyph == 0)147- cursor_x = border_size + x_margin;148- else if (cursor_at_glyph >= num_glyphs)149- cursor_x = border_size + x_margin + extents.x_advance;150- else151- cursor_x = glyphs[cursor_at_glyph].x;152-153- cairo_set_line_width(buf->cairo, 1);154- cairo_move_to(buf->cairo, cursor_x - 0.5, border_size + y_margin);155- cairo_line_to(buf->cairo, cursor_x - 0.5, border_size + y_margin + fextents.height);156- cairo_stroke(buf->cairo);157-158- cairo_glyph_free(glyphs);159- cairo_text_cluster_free(clusters);160-161-#if 0162- const struct rgba *bc = &render->options.border_color;163-164- cairo_set_source_rgba(buf->cairo, bc->r, bc->g, bc->b, bc->a);165- cairo_set_line_width(buf->cairo, border_size);166- cairo_move_to(buf->cairo, 0, border_size + 2 * y_margin + fextents.height + border_size / 2);167- cairo_line_to(buf->cairo, buf->width, border_size + 2 * y_margin + fextents.height + border_size / 2);168- cairo_stroke(buf->cairo);169-#endif139+ pixman_image_unref(pix);140+ cairo_surface_mark_dirty(buf->cairo_surface);141 }142 143 static void173-render_glyphs(struct buffer *buf, struct rgba color,174- const cairo_glyph_t *glyphs, int num_glyphs)144+render_match_text(struct buffer *buf, double *_x, double _y,145+ const wchar_t *text, ssize_t start, size_t length,146+ struct font *font,147+ struct rgba _regular_color,148+ struct rgba _match_color)149 {176- cairo_set_source_rgba(buf->cairo, color.r, color.g, color.b, color.a);177- cairo_set_operator(buf->cairo, CAIRO_OPERATOR_OVER);178- cairo_show_glyphs(buf->cairo, glyphs, num_glyphs);179-}150+ pixman_color_t regular_color = rgba2pixman(_regular_color);151+ pixman_color_t match_color = rgba2pixman(_match_color);152 181-static void182-render_match_text(struct buffer *buf, double *x, double y,183- const char *text, ssize_t start, size_t length,184- cairo_scaled_font_t *font, struct rgba regular_color,185- struct rgba match_color)186-{187- cairo_set_scaled_font(buf->cairo, font);153+ cairo_surface_flush(buf->cairo_surface);154+ pixman_image_t *pix = pixman_image_create_bits_no_clear(155+ PIXMAN_a8r8g8b8, buf->width, buf->height, buf->mmapped, buf->stride);156+ assert(pix != NULL);157 189- cairo_glyph_t *glyphs = NULL;190- cairo_text_cluster_t *clusters = NULL;191- cairo_text_cluster_flags_t cluster_flags;192- int num_glyphs = 0;193- int num_clusters = 0;158+ int x = *_x;159+ int y = _y;160 195- cairo_status_t cr_status = cairo_scaled_font_text_to_glyphs(196- font, 0, 0, text, -1, &glyphs, &num_glyphs,197- &clusters, &num_clusters, &cluster_flags);161+ for (size_t i = 0; i < wcslen(text); i++) {162+ const struct glyph *glyph = font_glyph_for_wc(font, text[i]);163+ if (glyph == NULL)164+ continue;165 199- if (cr_status != CAIRO_STATUS_SUCCESS) {200- LOG_ERR("cairo failure: TODO: error message");201- return;166+ bool is_match = start >= 0 && i >= start && i < start + length;167+ render_glyph(pix, glyph, x, y, is_match ? &match_color : ®ular_color);168+ x += glyph->x_advance;169 }170 204- cairo_text_extents_t extents;205- cairo_scaled_font_glyph_extents(font, glyphs, num_glyphs, &extents);206-207- for (int j = 0; j < num_glyphs; j++) {208- glyphs[j].x += *x;209- glyphs[j].y += y;210- }211-212- if (start >= 0) {213-214- /*215- * start/length are in bytes, but we need to know which216- * *glyph* the match starts at, and how many *glyphs* the match217- * is. For regular ascii, these will always be the same, but218- * for multibyte UTF-8 characters not so.219- */220-221- /* Calculate at which *glyph* the matching part starts */222- size_t idx = 0;223- size_t glyph_start = 0;224- while (idx < start) {225- int clen = mblen(&text[idx], MB_CUR_MAX);226- if (clen < 0) {227- LOG_ERRNO("match text: %s", text);228- break;229- }230-231- glyph_start++;232- idx += clen;233- }234-235- /* Find out how many *glyphs* that match */236- size_t glyph_match_count = 0;237- while (idx < start + length) {238- int clen = mblen(&text[idx], MB_CUR_MAX);239- if (clen < 0) {240- LOG_ERRNO("match text: %s", text);241- break;242- }243-244- glyph_match_count++;245- idx += clen;246- }247-248- render_glyphs(buf, regular_color, &glyphs[0], glyph_start);249- render_glyphs(buf, match_color, &glyphs[start], glyph_match_count);250- render_glyphs(buf, regular_color, &glyphs[start + length],251- num_glyphs - glyph_start - glyph_match_count);252- } else253- render_glyphs(buf, regular_color, glyphs, num_glyphs);254-255- cairo_glyph_free(glyphs);256- cairo_text_cluster_free(clusters);257-258- *x += extents.width;171+ *_x = x;172+ pixman_image_unref(pix);173+ cairo_surface_mark_dirty(buf->cairo_surface);174 }175 176 void177 render_match_list(const struct render *render, struct buffer *buf,178 const struct match matches[], size_t match_count,179 size_t match_length, size_t selected)180 {181+ struct font *font = render->regular_font;182 const double x_margin = render->options.x_margin;183 const double y_margin = render->options.y_margin;184 const double border_size = render->options.border_size;185 186 assert(match_count == 0 || selected < match_count);187 272- cairo_set_scaled_font(buf->cairo, render->regular_font);273-274- cairo_font_extents_t fextents;275- cairo_font_extents(buf->cairo, &fextents);276-277- const double row_height = 2 * y_margin + fextents.height;188+ const double row_height = 2 * y_margin + font->fextents.height;189 const double first_row = 1 * border_size + row_height;190 const double sel_margin = x_margin / 3;191 192 /*193 * LOG_DBG("height=%f, ascent=%f, descent=%f", fextents.height, fextents.ascent,194 * fextents.descent);195 */285- double y = first_row + (row_height + fextents.height) / 2 - fextents.descent;196+ double y = first_row + (row_height + font->fextents.height) / 2 - font->fextents.descent;197 198 for (size_t i = 0; i < match_count; i++) {199 const struct match *match = &matches[i];200 22 unmodified lines223 double height = cairo_image_surface_get_height(surf);224 double scale = 1.0;225 226 if (height > row_height) {316- scale = fextents.height / height;227+ scale = font->fextents.height / height;228 LOG_DBG("%s: scaling: %f (row-height: %f, size=%fx%f)",229 match->application->title, scale, row_height, width, height);230 231 width *= scale;20 unmodified lines252 253 RsvgDimensionData dim;254 rsvg_handle_get_dimensions(svg, &dim);255 345- double height = fextents.height;256+ double height = font->fextents.height;257 double scale = height / dim.height;258 259 cairo_save(buf->cairo);260 cairo_set_operator(buf->cairo, CAIRO_OPERATOR_OVER);23 unmodified lines284 }285 }286 287 struct render *377-render_init(cairo_scaled_font_t *font, struct options options)288+render_init(struct font *font, struct options options)289 {290 struct render *render = calloc(1, sizeof(*render));291 render->options = options;292 render->regular_font = font;5 unmodified lines298 {299 if (render == NULL)300 return;301 391- cairo_scaled_font_destroy(render->regular_font);302+ //cairo_scaled_font_destroy(render->regular_font);303+ font_destroy(render->regular_font);304 free(render);305 }modified:render.h+2 added-1 removed
1 #pragma once2 3 #include <cairo.h>4 5+#include "font.h"6 #include "shm.h"7 #include "match.h"8 #include "tllist.h"9 14 unmodified lines24 struct rgba selection_color;25 };26 27 struct render;27-struct render *render_init(cairo_scaled_font_t *font, struct options options);28+struct render *render_init(struct font *font, struct options options);29 void render_destroy(struct render *render);30 31 void render_background(const struct render *render, struct buffer *buf);32 7 unmodified linesmodified:shm.c+1 added
116 unmodified lines117 buffers,118 ((struct buffer){119 .width = width,120 .height = height,121+ .stride = stride,122 .busy = true,123 .size = size,124 .mmapped = mmapped,125 .wl_buf = buf,40 unmodified linesmodified:shm.h+1 added
7 unmodified lines8 9 struct buffer {10 int width;11 int height;12+ int stride;13 14 bool busy;15 size_t size;16 void *mmapped;9 unmodified linesadded:stride.h+9 added
1+#pragma once2+3+#include <pixman.h>4+5+static inline int6+stride_for_format_and_width(pixman_format_code_t format, int width)7+{8+ return (((PIXMAN_FORMAT_BPP(format) * width + 7) / 8 + 4 - 1) & -4);9+}modified:xdg.c+19 added-8 removed
2 unmodified lines3 #include <stdio.h>4 #include <stdlib.h>5 #include <string.h>6 #include <stdbool.h>7+#include <wchar.h>8 #include <unistd.h>9 10 #include <sys/types.h>11 #include <sys/stat.h>162 unmodified lines174 if (f == NULL)175 return;176 177 bool is_desktop_entry = false;178+177- char *name = NULL;179+ wchar_t *name = NULL;180+ wchar_t *generic_name = NULL;181+182 char *exec = NULL;183 char *path = NULL;180- char *generic_name = NULL;184 char *icon = NULL;185 bool visible = true;186 187 while (true) {35 unmodified lines223 const char *key = strtok(line, "=");224 char *value = strtok(NULL, "\n");225 226 if (key != NULL && value != NULL) {224- if (strcmp(key, "Name") == 0)225- name = strdup(value);227+ if (strcmp(key, "Name") == 0) {228+ assert(name == NULL);229+ size_t wlen = mbstowcs(NULL, value, 0);230+ name = malloc((wlen + 1) * sizeof(wchar_t));231+ mbstowcs(name, value, wlen + 1);232+ }233 234 else if (strcmp(key, "Exec") == 0)235 exec = strdup(value);236 237 else if (strcmp(key, "Path") == 0)238 path = strdup(value);239 233- else if (strcmp(key, "GenericName") == 0)234- generic_name = strdup(value);240+ else if (strcmp(key, "GenericName") == 0) {241+ assert(generic_name == NULL);242+ size_t wlen = mbstowcs(NULL, value, 0);243+ generic_name = malloc((wlen + 1) * sizeof(wchar_t));244+ mbstowcs(generic_name, value, wlen + 1);245+ }246 247 else if (strcmp(key, "Icon") == 0)248 icon = strdup(value);249 12 unmodified lines262 263 if (is_desktop_entry && visible && name != NULL && exec != NULL) {264 bool already_added = false;265 tll_foreach(*applications, it) {255- if (strcmp(it->item.title, name) == 0) {266+ if (wcscmp(it->item.title, name) == 0) {267 already_added = true;268 break;269 }270 }76 unmodified lines347 sort_application_by_title(const void *_a, const void *_b)348 {349 const struct application *a = _a;350 const struct application *b = _b;340- return strcmp(a->title, b->title);351+ return wcscmp(a->title, b->title);352 }353 354 void355 xdg_find_programs(const char *icon_theme, int icon_size,100 unmodified lines