Skip to content
Myr

Merge branch 'use-fcft'

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 0d802e7, 92a5072 · Browse files · 16 files+34 added−1323 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

16 files+34 added−1323 removed

  • other:+34 added−1323 removed

16 files+34 added−1323 removed

deleted:external/tllistnot shown

Its text is not shown, deleted.

added:subprojects/fcftnot shown

Its text is not shown, added.

added:subprojects/tllistnot shown

Its text is not shown, added.

deleted:subprojects/tllist/LICENSE-21 removed
1MIT License23Copyright (c) 2018 Daniel Eklöf45Permission is hereby granted, free of charge, to any person obtaining a copy6of this software and associated documentation files (the "Software"), to deal7in the Software without restriction, including without limitation the rights8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell9copies of the Software, and to permit persons to whom the Software is10furnished to do so, subject to the following conditions:1112The above copyright notice and this permission notice shall be included in all13copies or substantial portions of the Software.1415THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21SOFTWARE.
deleted:subprojects/tllist/meson.build-5 removed
1project('tllist', 'c', version: '1.0.0', license: 'MIT', meson_version: '>=0.50.0')2tllist = declare_dependency(include_directories: '.')34unittest = executable('unittest', 'test.c', dependencies: [tllist])5test('unittest', unittest)
deleted:subprojects/tllist/README.md-301 removed
1# tllist23**tllist** is a *T*yped *L*inked *L*ist C header file only4library implemented using pre-processor macros.5671. [Description](#description)81. [Usage](#usage)9 1. [Declaring a variable](#declaring-a-variable)10 1. [Adding items - basic](#adding-items-basic)11 1. [List length](#list-length)12 1. [Accessing items](#accessing-items)13 1. [Iterating](#iterating)14 1. [Removing items - basic](#removing-items-basic)15 1. [Adding items - advanced](#adding-items-advanced)16 1. [Removing items - advanced](#removing-items-advanced)17 1. [Freeing](#freeing)181. [Integrating](#integrating)19 1. [Meson](#meson)201. [API](#api)21 1. [Cheat sheet](#cheat-sheet)222324## Description2526Most C implementations of linked list are untyped. That is, their data27carriers are typically `void *`. This is error prone since your28compiler will not be able to help you correct your mistakes (_oh, was29it pointer-to-a-pointer... I though it was just a pointer..._).3031**tllist** addresses this by using pre-processor macros to implement32dynamic types, where the data carrier is typed to whatever you want;33both **primitive** data types are supported as well as aggregated ones34such as **structs**, **enums** and **unions**.3536Being a double-linked list, most operations are constant in time37(including pushing and popping both to/from front and back).3839The memory overhead is fairly small; each item carries, besides its40data, a _prev_ and _next_ pointer (i.e. a constant 16 byte overhead41per item on 64-bit architectures).4243The list itself has two _head_ and _tail_ pointers, plus a _length_44variable (typically 8 bytes on 64-bit architectures) to make list45length lookup constant in time.4647Thus, assuming 64-bit pointers (and a 64-bit `size_t` type), the total48overhead is `3*8 + n*2*8` bytes.495051## Usage5253### Declaring a variable54551. **Declare a variable**5657 ```c58 /* Declare a variable using an anonymous type */59 tll(int) an_integer_list = tll_init();60 ```6162632. **Typedef**6465 ```c66 /* First typedef the list type */67 typedef tll(int) an_integer_list_t;6869 /* Then declare a variable using that typedef */70 an_integer_list_t an_integer_list = tll_init();71 ```72733. **Named struct**7475 ```c76 /* First declare named struct */77 tll(int, an_integer_list);7879 /* Then declare a variable using that named struct */80 struct an_integer_list an_integer_list = tll_init();81 ```8283### Adding items - basic8485Use `tll_push_back()` or `tll_push_front()` to add elements to the86back or front of the list.8788```c89tll_push_back(an_integer_list, 4711);90tll_push_front(an_integer_list, 1234);91```929394### List length9596`tll_length()` returns the length (number of items) in a list.9798```c99tll_push_back(an_integer_list, 1234);100tll_push_back(an_integer_list, 5678);101printf("length: %zu\n", tll_length(an_integer_list));102```103104Outputs:105106 length: 2107108109### Accessing items110111For the front and back items, you can use `tll_front()` and112`tll_back()` respectively. For any other item in the list, you need to113iterate the list and find the item yourself.114115```c116tll_push_back(an_integer_list, 1234);117tll_push_back(an_integer_list, 5555);118tll_push_back(an_integer_list, 6789);119120printf("front: %d\n", tll_front(an_integer_list));121printf("back: %d\n", tll_back(an_integer_list));122```123124Outputs:125126 front: 1234127 back: 6789128129130### Iterating131132You can iterate the list either forward or backwards, using133`tll_foreach()` and `tll_rforeach()` respectively.134135The `it` variable should be treated as an opaque iterator type, where136`it->item` is the item.137138In reality, it is simply a pointer to the linked list entry, and since139tllist is a header-only implementation, you do have access to e.g. the140next/prev pointers. There should not be any need to access anything141except `item` however.142143Note that `it` can be named anything.144145```c146tll_push_back(an_integer_list, 1);147tll_push_back(an_integer_list, 2);148149tll_foreach(an_integer_list, it) {150 printf("forward: %d\n", it->item);151}152153tll_rforeach(an_integer_list, it) {154 printf("reverse: %d\n", it->item);155}156```157158Outputs:159160 forward: 1161 forward: 2162 reverse: 2163 reverse: 1164165166### Removing items - basic167168`tll_pop_front()` and `tll_pop_back()` removes the front/back item and169returns it.170171```c172tll_push_back(an_integer_list, 1234);173tll_push_back(an_integer_list, 5678);174175printf("front: %d\n", tll_pop_front(an_integer_list));176printf("back: %d\n", tll_pop_back(an_integer_list));177printf("length: %zu\n", tll_length(an_integer_list));178```179180Outputs:181182 front: 1234183 back: 5678184 length: 0185186187### Adding items - advanced188189Given an iterator, you can insert new items before or after that190iterator, using `tll_insert_before()` and `tll_insert_after()`.191192```c193tll_foreach(an_integer_list, it) {194 if (it->item == 1234) {195 tll_insert_before(an_integer_list, it, 7777);196 break;197 }198}199```200201Q: Why do I have to pass **both** the _list_ and the _iterator_ to202 `tll_insert_before()`?203204A: If not, **each** element in the list would have to contain a205 pointer to the owning list, which would significantly increase the206 overhead.207208209### Removing items - advanced210211Similar to how you can add items while iterating a list, you can also212remove them.213214Note that the `*foreach()` functions are **safe** in this regard - it215is perfectly OK to remove the "current" item.216217```c218tll_foreach(an_integer_list, it) {219 if (it->item.delete_me)220 tll_remove(an_integer_list, it);221}222```223224To make it slightly easier to handle cases where the item _itself_225must be free:d as well, there is also `tll_remove_and_free()`. It226works just like `tll_remove()`, but takes an additional argument; a227callback that will be called for each item.228229```c230tll(int *) int_p_list = tll_init();231232int *a = malloc(sizeof(*a));233int *b = malloc(sizeof(*b));234235*a = 1234;236*b = 5678;237238tll_push_back(int_p_list, a);239tll_push_back(int_p_list, b);240241tll_foreach(int_p_list, it) {242 tll_remove_and_free(int_p_list, it, free);243}244```245246247### Freeing248249To remove **all** items, use `tll_free()`, or250`tll_free_and_free()`. These are just convenience functions and251calling these are equivalent to:252253```c254tll_foreach(an_integer_list, it) {255 tll_remove(an_integer_list, it);256}257```258259Note that there is no need to call `tll_free()` on an empty260(`tll_length(list) == 0`) list.261262263## Integrating264265The easiest way may be to simply copy `tllist.h` into your266project. But see sections below for other ways.267268269### Meson270271You can use tllist as a subproject. In your main project's272`meson.build`, to something like:273274```meson275tllist = subproject('tllist').get_variable('tllist')276executable('you-executable', ..., dependencies: [tllist])277```278279280## API281282### Cheat sheet283284| Function | Description | Context | Complexity |285|-------------------------------------|-------------------------------------------------------|--------------------|-----------:|286| `list = tll_init()` | initialize a new tllist variable to an empty list | Variable init | O(1) |287| `tll_length(list)` | returns the length (number of items) of a list | | O(1) |288| `tll_push_front(list, item)` | inserts _item_ at the beginning of the list | | O(1) |289| `tll_push_back(list, item)` | inserts _item_ at the end of the list | | O(1) |290| `tll_front(list)` | returns the first item in the list | | O(1) |291| `tll_back(list)` | returns the last item in the list | | O(1) |292| `tll_pop_front(list)` | removes and returns the first item in the list | | O(1) |293| `tll_pop_back(list)` | removes and returns the last item in the list | | O(1) |294| `tll_foreach(list, it)` | iterates the list from the beginning to the end | | O(n) |295| `tll_rforeach(list, it)` | iterates the list from the end to the beginning | | O(n) |296| `tll_insert_before(list, it, item)` | inserts _item_ before _it_. | `tll_(r)foreach()` | O(1) |297| `tll_insert_after(list, it, item)` | inserts _item_ after _it_. | `tll_(r)foreach()` | O(1) |298| `tll_remove(list, it)` | removes _it_ from the list. | `tll_(r)foreach()` | O(1) |299| `tll_remove_and_free(list, it, cb)` | removes _it_ from the list, and calls `cb(it->item)`. | `tll_(r)foreach()` | O(1) |300| `tll_free(list)` | removes **all** items from the list | | O(n) |301| `tll_free_and_free(list, cb)` | removes **all** items from the list, and calls `cb(it->item)` for each item. | | O(n) |
deleted:subprojects/tllist/test.c-96 removed
1#undef NDEBUG2#include <stdlib.h>3#include <string.h>4#include <stdbool.h>5#include <assert.h>67#include <tllist.h>89int10main(int argc, const char *const *argv)11{12 tll(int) l = tll_init();13 assert(tll_length(l) == 0);1415 /* push back */16 tll_push_back(l, 123); assert(tll_length(l) == 1);17 tll_push_back(l, 456); assert(tll_length(l) == 2);18 tll_push_back(l, 789); assert(tll_length(l) == 3);1920 assert(tll_front(l) == 123);21 assert(tll_back(l) == 789);2223 /* push front */24 tll_push_front(l, 0xabc); assert(tll_length(l) == 4);2526 assert(tll_front(l) == 0xabc);27 assert(tll_back(l) == 789);2829 /* Pop back */30 assert(tll_pop_back(l) == 789);31 assert(tll_back(l) == 456);3233 /* Pop front */34 assert(tll_pop_front(l) == 0xabc);35 assert(tll_front(l) == 123);3637 /* foreach */38 assert(tll_length(l) == 2);3940 int seen[tll_length(l)];41 memset(seen, 0, tll_length(l) * sizeof(seen[0]));4243 size_t count = 0;44 tll_foreach(l, it)45 seen[count++] = it->item;4647 assert(count == tll_length(l));48 assert(seen[0] == 123);49 assert(seen[1] == 456);5051 /* rforeach */52 memset(seen, 0, tll_length(l) * sizeof(seen[0]));53 count = 0;54 tll_rforeach(l, it)55 seen[count++] = it->item;5657 assert(count == tll_length(l));58 assert(seen[0] == 456);59 assert(seen[1] == 123);6061 /* remove */62 tll_push_back(l, 789);63 tll_foreach(l, it) {64 if (it->item > 123 && it->item < 789)65 tll_remove(l, it);66 }67 assert(tll_length(l) == 2);68 assert(tll_front(l) == 123);69 assert(tll_back(l) == 789);7071 /* insert before */72 tll_foreach(l, it) {73 if (it->item == 123)74 tll_insert_before(l, it, 0xabc);75 }76 assert(tll_length(l) == 3);77 assert(tll_front(l) == 0xabc);78 assert(tll_back(l) == 789);7980 /* insert after */81 tll_foreach(l, it) {82 if (it->item == 789)83 tll_insert_after(l, it, 999);84 }85 assert(tll_length(l) == 4);86 assert(tll_front(l) == 0xabc);87 assert(tll_back(l) == 999);8889 /* free */90 tll_free(l);91 assert(tll_length(l) == 0);92 assert(l.head == NULL);93 assert(l.tail == NULL);9495 return EXIT_SUCCESS;96}
deleted:subprojects/tllist/tllist.h-180 removed
1#pragma once23#include <stdlib.h>4#include <stddef.h>5#include <assert.h>67#define TLL_PASTE2( a, b) a##b8#define TLL_PASTE( a, b) TLL_PASTE2( a, b)910/* Utility macro to generate a list element struct with a unique struct tag */11#define TLL_UNIQUE_INNER_STRUCT(TYPE, ID) \12 struct TLL_PASTE(__tllist_ , ID) { \13 TYPE item; \14 struct TLL_PASTE(__tllist_, ID) *prev; \15 struct TLL_PASTE(__tllist_, ID) *next; \16 } *head, *tail;1718/*19 * Defines a new typed-list type, or directly instantiate a typed-list variable20 *21 * Example a, declare a variable (list of integers):22 * tll(int) my_list;23 *24 * Example b, declare a type, and then use the type:25 * tll(int, my_list_type);26 * struct my_list_type my_list;27 */28#define tll(TYPE, ...) \29 struct __VA_ARGS__ { \30 TLL_UNIQUE_INNER_STRUCT(TYPE, __COUNTER__) \31 size_t length; \32 }3334/* Initializer: tll(int) my_list = tll_init(); */35#define tll_init() {.head = NULL, .tail = NULL, .length = 0}3637/* Length/size of list: printf("size: %zu\n", tll_length(my_list)); */38#define tll_length(list) (list).length3940/* Adds a new item to the back of the list */41#define tll_push_back(list, new_item) \42 do { \43 tll_insert_after(list, (list).tail, new_item); \44 if ((list).head == NULL) \45 (list).head = (list).tail; \46 } while (0)4748/* Adds a new item to the front of the list */49#define tll_push_front(list, new_item) \50 do { \51 tll_insert_before(list, (list).head, new_item); \52 if ((list).tail == NULL) \53 (list).tail = (list).head; \54 } while (0)5556/*57 * Iterates the list. <it> is an iterator pointer. You can access the58 * list item with ->item:59 *60 * tll(int) my_list = vinit();61 * tll_push_back(my_list, 5);62 *63 * tll_foreach(my_list i) {64 * printf("%d\n", i->item);65 * }66*/67#define tll_foreach(list, it) \68 for (__typeof__(*(list).head) *it = (list).head, \69 *it_next = it != NULL ? it->next : NULL; \70 it != NULL; \71 it = it_next, \72 it_next = it_next != NULL ? it_next->next : NULL)7374/* Same as tll_foreach(), but iterates backwards */75#define tll_rforeach(list, it) \76 for (__typeof__(*(list).tail) *it = (list).tail, \77 *it_prev = it != NULL ? it->prev : NULL; \78 it != NULL; \79 it = it_prev, \80 it_prev = it_prev != NULL ? it_prev->prev : NULL)8182/*83 * Inserts a new item after <it>, which is an iterator. I.e. you can84 * only call this from inside a tll_foreach() or tll_rforeach() loop.85 */86#define tll_insert_after(list, it, new_item) \87 do { \88 __typeof__((list).head) __e = malloc(sizeof(*__e)); \89 __e->item = (new_item); \90 __e->prev = (it); \91 __e->next = (it) != NULL ? (it)->next : NULL; \92 if ((it) != NULL) { \93 if ((it)->next != NULL) \94 (it)->next->prev = __e; \95 (it)->next = __e; \96 } \97 if ((it) == (list).tail) \98 (list).tail = __e; \99 (list).length++; \100 } while (0)101102/*103 * Inserts a new item before <it>, which is an iterator. I.e. you can104 * only call this from inside a tll_foreach() or tll_rforeach() loop.105 */106#define tll_insert_before(list, it, new_item) \107 do { \108 __typeof__((list).head) __e = malloc(sizeof(*__e)); \109 __e->item = (new_item); \110 __e->prev = (it) != NULL ? (it)->prev : NULL; \111 __e->next = (it); \112 if ((it) != NULL) { \113 if ((it)->prev != NULL) \114 (it)->prev->next = __e; \115 (it)->prev = __e; \116 } \117 if ((it) == (list).head) \118 (list).head = __e; \119 (list).length++; \120 } while (0)121122/*123 * Removes an entry from the list. <it> is an iterator. I.e. you can124 * only call this from inside a tll_foreach() or tll_rforeach() loop.125 */126#define tll_remove(list, it) \127 do { \128 assert((list).length > 0); \129 __typeof__((list).head) __prev = it->prev; \130 __typeof__((list).head) __next = it->next; \131 if (__prev != NULL) \132 __prev->next = __next; \133 else \134 (list).head = __next; \135 if (__next != NULL) \136 __next->prev = __prev; \137 else \138 (list).tail = __prev; \139 free(it); \140 (list).length--; \141 } while (0)142143/* Same as tll_remove(), but calls free_callback(it->item) */144#define tll_remove_and_free(list, it, free_callback) \145 do { \146 free_callback((it)->item); \147 tll_remove((list), (it)); \148 } while (0)149150#define tll_front(list) (list).head->item151#define tll_back(list) (list).tail->item152153/*154 * Removes the first element from the list, and returns it (note:155 * returns the *actual* item, not an iterator.156 */157#define tll_pop_front(list) \158 ({__typeof__((list).head) it = (list).head; \159 __typeof__((list).head->item) __ret = it->item; \160 tll_remove((list), it); \161 __ret; \162 })163164/* Same as tll_pop_front(), but returns/removes the *last* element */165#define tll_pop_back(list) \166 ({__typeof__((list).tail) it = (list).tail; \167 __typeof__((list).tail->item) __ret = it->item; \168 tll_remove((list), it); \169 __ret; \170 })171172/* Frees the list. This call is *not* needed if the list is already empty. */173#define tll_free(list) \174 tll_foreach(list, __it) \175 tll_remove(list, __it)176177/* Same as tll_free(), but also calls free_callback(item) for every item */178#define tll_free_and_free(list, free_callback) \179 tll_foreach(list, __it) \180 tll_remove_and_free(list, __it, free_callback)
modified:.gitmodules+5 added-3 removed
1[submodule "external/wlr-protocols"]2 path = external/wlr-protocols3 url = https://github.com/swaywm/wlr-protocols.git4 branch = master5[submodule "external/tllist"]6 path = external/tllist5[submodule "subprojects/tllist"]6 path = subprojects/tllist7 url = https://codeberg.org/dnkl/tllist.git8 branch = master8[submodule "subprojects/fcft"]9 path = subprojects/fcft10 url = https://codeberg.org/dnkl/fcft.git
deleted:font.c-628 removed
1#include "font.h"23#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>1011#include <freetype/tttables.h>1213#define LOG_MODULE "font"14#define LOG_ENABLE_DBG 015#include "log.h"16#include "stride.h"1718#define min(x, y) ((x) < (y) ? (x) : (y))1920static FT_Library ft_lib;2122static tll(const struct font *) font_cache = tll_init();2324static const size_t glyph_cache_size = 512;2526static void __attribute__((constructor))27init(void)28{29 FcInit();30 FT_Init_FreeType(&ft_lib);3132#if defined(LOG_ENABLE_DBG) && LOG_ENABLE_DBG33 int raw_version = FcGetVersion();3435 /* See FC_VERSION in <fontconfig/fontconfig.h> */36 const int major = raw_version / 10000; raw_version %= 10000;37 const int minor = raw_version / 100; raw_version %= 100;38 const int patch = raw_version;39#endif4041 LOG_DBG("fontconfig: %d.%d.%d", major, minor, patch);42}4344static void __attribute__((destructor))45fini(void)46{47 assert(tll_length(font_cache) == 0);4849 FT_Done_FreeType(ft_lib);50 FcFini();51}5253static bool54font_destroy_no_free(struct font *font)55{56 assert(font->ref_counter > 0);57 if (--font->ref_counter > 0)58 return false;5960 if (font->face != NULL)61 FT_Done_Face(font->face);6263 if (font->fc_pattern != NULL)64 FcPatternDestroy(font->fc_pattern);65 if (font->fc_fonts != NULL)66 FcFontSetDestroy(font->fc_fonts);6768 free(font->name);6970 if (font->cache == NULL)71 return true;7273 for (size_t i = 0; i < glyph_cache_size; i++) {74 if (font->cache[i] == NULL)75 continue;7677 tll_foreach(*font->cache[i], it) {78 if (!it->item.valid)79 continue;8081 void *image = pixman_image_get_data(it->item.pix);82 pixman_image_unref(it->item.pix);83 free(image);84 }8586 tll_free(*font->cache[i]);87 free(font->cache[i]);88 }89 free(font->cache);9091 tll_foreach(font_cache, it) {92 if (it->item == font) {93 tll_remove(font_cache, it);94 break;95 }96 }9798 return true;99}100101static void102underline_strikeout_metrics(struct font *font)103{104 FT_Face ft_face = font->face;105 double y_scale = ft_face->size->metrics.y_scale / 65536.;106 double height = ft_face->size->metrics.height / 64.;107 double descent = ft_face->size->metrics.descender / 64.;108109 LOG_DBG("ft: y-scale: %f, height: %f, descent: %f",110 y_scale, height, descent);111112 font->underline.position = ft_face->underline_position * y_scale / 64.;113 font->underline.thickness = ft_face->underline_thickness * y_scale / 64.;114115 if (font->underline.position == 0.) {116 font->underline.position = descent / 2.;117 font->underline.thickness = fabs(descent / 5.);118 }119120 LOG_DBG("underline: pos=%f, thick=%f",121 font->underline.position, font->underline.thickness);122123 TT_OS2 *os2 = FT_Get_Sfnt_Table(ft_face, ft_sfnt_os2);124 if (os2 != NULL) {125 font->strikeout.position = os2->yStrikeoutPosition * y_scale / 64.;126 font->strikeout.thickness = os2->yStrikeoutSize * y_scale / 64.;127 }128129 if (font->strikeout.position == 0.) {130 font->strikeout.position = height / 2. + descent;131 font->strikeout.thickness = font->underline.thickness;132 }133134 LOG_DBG("strikeout: pos=%f, thick=%f",135 font->strikeout.position, font->strikeout.thickness);136}137138static bool139from_font_set(FcPattern *pattern, FcFontSet *fonts, int start_idx,140 struct font *font, bool is_fallback)141{142 memset(font, 0, sizeof(*font));143144 FcChar8 *face_file = NULL;145 FcPattern *final_pattern = NULL;146 int font_idx = -1;147148 for (int i = start_idx; i < fonts->nfont; i++) {149 FcPattern *pat = FcFontRenderPrepare(NULL, pattern, fonts->fonts[i]);150 assert(pat != NULL);151152 if (FcPatternGetString(pat, FC_FT_FACE, 0, &face_file) != FcResultMatch) {153 if (FcPatternGetString(pat, FC_FILE, 0, &face_file) != FcResultMatch) {154 FcPatternDestroy(pat);155 continue;156 }157 }158159 final_pattern = pat;160 font_idx = i;161 break;162 }163164 assert(font_idx != -1);165 assert(final_pattern != NULL);166167 double dpi;168 if (FcPatternGetDouble(final_pattern, FC_DPI, 0, &dpi) != FcResultMatch)169 dpi = 96;170171 double size;172 double pixel_size;173 if (FcPatternGetDouble(final_pattern, FC_SIZE, 0, &size) != FcResultMatch ||174 FcPatternGetDouble(final_pattern, FC_PIXEL_SIZE, 0, &pixel_size) != FcResultMatch)175 {176 LOG_ERR("%s: failed to get size", face_file);177 FcPatternDestroy(final_pattern);178 return false;179 }180181 FcBool scalable;182 if (FcPatternGetBool(final_pattern, FC_SCALABLE, 0, &scalable) != FcResultMatch)183 scalable = FcTrue;184185 FcBool outline;186 if (FcPatternGetBool(final_pattern, FC_OUTLINE, 0, &outline) != FcResultMatch)187 outline = FcTrue;188189 double pixel_fixup;190 if (FcPatternGetDouble(final_pattern, "pixelsizefixupfactor", 0, &pixel_fixup) != FcResultMatch) {191 /*192 * Force a fixup factor on scalable bitmap fonts (typically193 * emoji fonts). The fixup factor is194 * requested-pixel-size / actual-pixels-size195 */196 if (scalable && !outline) {197 double original_pixel_size;198 if (FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &original_pixel_size) != FcResultMatch) {199 double original_size;200 if (FcPatternGetDouble(pattern, FC_SIZE, 0, &original_size) != FcResultMatch)201 original_size = size;202 original_pixel_size = original_size * dpi / 72;203 }204 pixel_fixup = original_pixel_size / pixel_size;205206 LOG_DBG("estimated fixup factor to %f (requested pixel size: %f, actual: %f)",207 pixel_fixup, original_pixel_size, pixel_size);208 } else209 pixel_fixup = 1.;210 }211212 LOG_DBG("loading: %s", face_file);213214 FT_Face ft_face;215 FT_Error ft_err = FT_New_Face(ft_lib, (const char *)face_file, 0, &ft_face);216 if (ft_err != 0)217 LOG_ERR("%s: failed to create FreeType face", face_file);218219 if ((ft_err = FT_Set_Pixel_Sizes(ft_face, 0, pixel_size)) != 0) {220 LOG_WARN("failed to set pixel size");221 FT_Done_Face(ft_face);222 FcPatternDestroy(final_pattern);223 return false;224 }225226 FcBool fc_hinting;227 if (FcPatternGetBool(final_pattern, FC_HINTING,0, &fc_hinting) != FcResultMatch)228 fc_hinting = FcTrue;229230 FcBool fc_antialias;231 if (FcPatternGetBool(final_pattern, FC_ANTIALIAS, 0, &fc_antialias) != FcResultMatch)232 fc_antialias = FcTrue;233234 int fc_hintstyle;235 if (FcPatternGetInteger(final_pattern, FC_HINT_STYLE, 0, &fc_hintstyle) != FcResultMatch)236 fc_hintstyle = FC_HINT_SLIGHT;237238 int fc_rgba;239 if (FcPatternGetInteger(final_pattern, FC_RGBA, 0, &fc_rgba) != FcResultMatch)240 fc_rgba = FC_RGBA_UNKNOWN;241242 int load_flags = 0;243 if (!fc_antialias) {244 if (!fc_hinting || fc_hintstyle == FC_HINT_NONE)245 load_flags |= FT_LOAD_MONOCHROME | FT_LOAD_NO_HINTING | FT_LOAD_TARGET_NORMAL;246 else247 load_flags |= FT_LOAD_MONOCHROME | FT_LOAD_TARGET_MONO;248 } else {249 if (!fc_hinting || fc_hintstyle == FC_HINT_NONE)250 load_flags |= FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING | FT_LOAD_TARGET_NORMAL;251 else if (fc_hinting && fc_hintstyle == FC_HINT_SLIGHT)252 load_flags |= FT_LOAD_DEFAULT | FT_LOAD_TARGET_LIGHT;253 else if (fc_rgba == FC_RGBA_RGB || fc_rgba == FC_RGBA_BGR)254 load_flags |= FT_LOAD_DEFAULT | FT_LOAD_TARGET_LCD;255 else if (fc_rgba == FC_RGBA_VRGB || fc_rgba == FC_RGBA_VBGR)256 load_flags |= FT_LOAD_DEFAULT | FT_LOAD_TARGET_LCD_V;257 else258 load_flags |= FT_LOAD_DEFAULT | FT_LOAD_TARGET_NORMAL;259 }260261 FcBool fc_embeddedbitmap;262 if (FcPatternGetBool(final_pattern, FC_EMBEDDED_BITMAP, 0, &fc_embeddedbitmap) != FcResultMatch)263 fc_embeddedbitmap = FcTrue;264265 if (!fc_embeddedbitmap)266 load_flags |= FT_LOAD_NO_BITMAP;267268 int render_flags = 0;269 if (!fc_antialias)270 render_flags |= FT_RENDER_MODE_MONO;271 else {272 if (fc_rgba == FC_RGBA_RGB || fc_rgba == FC_RGBA_BGR)273 render_flags |= FT_RENDER_MODE_LCD;274 else if (fc_rgba == FC_RGBA_VRGB || fc_rgba == FC_RGBA_VBGR)275 render_flags |= FT_RENDER_MODE_LCD_V;276 else277 render_flags |= FT_RENDER_MODE_NORMAL;278 }279280 int fc_lcdfilter;281 if (FcPatternGetInteger(final_pattern, FC_LCD_FILTER, 0, &fc_lcdfilter) != FcResultMatch)282 fc_lcdfilter = FC_LCD_DEFAULT;283284 switch (fc_lcdfilter) {285 case FC_LCD_NONE: font->lcd_filter = FT_LCD_FILTER_NONE; break;286 case FC_LCD_DEFAULT: font->lcd_filter = FT_LCD_FILTER_DEFAULT; break;287 case FC_LCD_LIGHT: font->lcd_filter = FT_LCD_FILTER_LIGHT; break;288 case FC_LCD_LEGACY: font->lcd_filter = FT_LCD_FILTER_LEGACY; break;289 }290291 FcPatternDestroy(final_pattern);292293 font->face = ft_face;294 font->load_flags = load_flags | FT_LOAD_COLOR;295 font->render_flags = render_flags;296 font->pixel_size_fixup = pixel_fixup;297 font->bgr = fc_rgba == FC_RGBA_BGR || fc_rgba == FC_RGBA_VBGR;298 font->fc_idx = font_idx;299 font->is_fallback = is_fallback;300 font->ref_counter = 1;301302 double max_x_advance = ft_face->size->metrics.max_advance / 64.;303 double height = ft_face->size->metrics.height / 64.;304 double descent = ft_face->size->metrics.descender / 64.;305 double ascent = ft_face->size->metrics.ascender / 64.;306307 font->height = height * font->pixel_size_fixup;308 font->descent = -descent * font->pixel_size_fixup;309 font->ascent = ascent * font->pixel_size_fixup;310 font->max_x_advance = max_x_advance * font->pixel_size_fixup;311312 LOG_DBG("metrics: height: %d, descent: %d, ascent: %d, x-advance: %d",313 font->height, font->descent, font->ascent, font->max_x_advance);314315 if (!is_fallback) {316 font->fc_pattern = pattern;317 font->fc_fonts = fonts;318 font->cache = calloc(glyph_cache_size, sizeof(font->cache[0]));319 }320321 underline_strikeout_metrics(font);322 return true;323}324325struct font *326font_from_name(const char *name)327{328 LOG_DBG("instantiating %s", name);329330 tll_foreach(font_cache, it) {331 if (strcmp(it->item->name, name) == 0)332 return font_clone(it->item);333 }334335 FcPattern *pattern = FcNameParse((const unsigned char *)name);336 if (pattern == NULL) {337 LOG_ERR("%s: failed to lookup font", name);338 return NULL;339 }340341 if (!FcConfigSubstitute(NULL, pattern, FcMatchPattern)) {342 LOG_ERR("%s: failed to do config substitution", name);343 FcPatternDestroy(pattern);344 return NULL;345 }346347 FcDefaultSubstitute(pattern);348349 FcResult result;350 FcFontSet *fonts = FcFontSort(NULL, pattern, FcTrue, NULL, &result);351 if (result != FcResultMatch) {352 LOG_ERR("%s: failed to match font", name);353 FcPatternDestroy(pattern);354 return NULL;355 }356357 struct font *font = malloc(sizeof(*font));358 if (!from_font_set(pattern, fonts, 0, font, false)) {359 free(font);360 FcFontSetDestroy(fonts);361 FcPatternDestroy(pattern);362 return NULL;363 }364365 font->name = strdup(name);366 tll_push_back(font_cache, font);367 return font;368}369370static size_t371hash_index(wchar_t wc)372{373 return wc % glyph_cache_size;374}375376static bool377glyph_for_wchar(const struct font *font, wchar_t wc, struct glyph *glyph)378{379 /*380 * LCD filter is per library instance. Thus we need to re-set it381 * every time...382 *383 * Also note that many freetype builds lack this feature384 * (FT_CONFIG_OPTION_SUBPIXEL_RENDERING must be defined, and isn't385 * by default) */386 FT_Error err = FT_Library_SetLcdFilter(ft_lib, font->lcd_filter);387 if (err != 0 && err != FT_Err_Unimplemented_Feature)388 goto err;389390 FT_UInt idx = FT_Get_Char_Index(font->face, wc);391 if (idx == 0) {392 if (font->is_fallback)393 return false;394395 /* No glyph in this font, try fontconfig fallback fonts */396397 /* Try fontconfig fallback fonts */398 assert(font->fc_pattern != NULL);399 assert(font->fc_fonts != NULL);400 assert(font->fc_idx != -1);401402 for (int i = font->fc_idx + 1; i < font->fc_fonts->nfont; i++) {403 struct font fallback;404 if (!from_font_set(font->fc_pattern, font->fc_fonts, i, &fallback, true))405 continue;406407 if (glyph_for_wchar(&fallback, wc, glyph)) {408 LOG_DBG("%C: used fontconfig fallback", wc);409 font_destroy_no_free(&fallback);410 return true;411 }412413 font_destroy_no_free(&fallback);414 }415416 LOG_WARN("%C: no glyph found (in neither the main font, "417 "nor any fallback fonts)", wc);418 }419420 err = FT_Load_Glyph(font->face, idx, font->load_flags);421 if (err != 0) {422 LOG_ERR("load failed");423 goto err;424 }425426 err = FT_Render_Glyph(font->face->glyph, font->render_flags);427 if (err != 0)428 goto err;429430 assert(font->face->glyph->format == FT_GLYPH_FORMAT_BITMAP);431432 FT_Bitmap *bitmap = &font->face->glyph->bitmap;433 if (bitmap->width == 0)434 goto err;435436 pixman_format_code_t pix_format;437 int width;438 int rows;439440 switch (bitmap->pixel_mode) {441 case FT_PIXEL_MODE_MONO:442 pix_format = PIXMAN_a1;443 width = bitmap->width;444 rows = bitmap->rows;445 break;446447 case FT_PIXEL_MODE_GRAY:448 pix_format = PIXMAN_a8;449 width = bitmap->width;450 rows = bitmap->rows;451 break;452453 case FT_PIXEL_MODE_LCD:454 pix_format = PIXMAN_x8r8g8b8;455 width = bitmap->width / 3;456 rows = bitmap->rows;457 break;458459 case FT_PIXEL_MODE_LCD_V:460 pix_format = PIXMAN_x8r8g8b8;461 width = bitmap->width;462 rows = bitmap->rows / 3;463 break;464465 case FT_PIXEL_MODE_BGRA:466 pix_format = PIXMAN_a8r8g8b8;467 width = bitmap->width;468 rows = bitmap->rows;469 break;470471 default:472 LOG_ERR("unimplemented: FT pixel mode: %d", bitmap->pixel_mode);473 goto err;474 break;475 }476477 int stride = stride_for_format_and_width(pix_format, width);478 assert(stride >= bitmap->pitch);479480 uint8_t *data = malloc(rows * stride);481482 /* Convert FT bitmap to pixman image */483 switch (bitmap->pixel_mode) {484 case FT_PIXEL_MODE_MONO:485 for (size_t r = 0; r < bitmap->rows; r++) {486 for (size_t c = 0; c < (bitmap->width + 7) / 8; c++) {487 uint8_t v = bitmap->buffer[r * bitmap->pitch + c];488 uint8_t reversed = 0;489 for (size_t i = 0; i < min(8, bitmap->width - c * 8); i++)490 reversed |= ((v >> (7 - i)) & 1) << i;491492 data[r * stride + c] = reversed;493 }494 }495 break;496497 case FT_PIXEL_MODE_GRAY:498 for (size_t r = 0; r < bitmap->rows; r++) {499 for (size_t c = 0; c < bitmap->width; c++)500 data[r * stride + c] = bitmap->buffer[r * bitmap->pitch + c];501 }502 break;503504 case FT_PIXEL_MODE_BGRA:505 assert(stride == bitmap->pitch);506 memcpy(data, bitmap->buffer, bitmap->rows * bitmap->pitch);507 break;508509 case FT_PIXEL_MODE_LCD:510 for (size_t r = 0; r < bitmap->rows; r++) {511 for (size_t c = 0; c < bitmap->width; c += 3) {512 unsigned char _r = bitmap->buffer[r * bitmap->pitch + c + (font->bgr ? 2 : 0)];513 unsigned char _g = bitmap->buffer[r * bitmap->pitch + c + 1];514 unsigned char _b = bitmap->buffer[r * bitmap->pitch + c + (font->bgr ? 0 : 2)];515516 uint32_t *p = (uint32_t *)&data[r * stride + 4 * (c / 3)];517 *p = _r << 16 | _g << 8 | _b;518 }519 }520 break;521522 case FT_PIXEL_MODE_LCD_V:523 /* Unverified */524 for (size_t r = 0; r < bitmap->rows; r += 3) {525 for (size_t c = 0; c < bitmap->width; c++) {526 unsigned char _r = bitmap->buffer[(r + (font->bgr ? 2 : 0)) * bitmap->pitch + c];527 unsigned char _g = bitmap->buffer[(r + 1) * bitmap->pitch + c];528 unsigned char _b = bitmap->buffer[(r + (font->bgr ? 0 : 2)) * bitmap->pitch + c];529530 uint32_t *p = (uint32_t *)&data[r / 3 * stride + 4 * c];531 *p = _r << 16 | _g << 8 | _b;532 }533 }534 break;535536 default:537 abort();538 break;539 }540541 pixman_image_t *pix = pixman_image_create_bits_no_clear(542 pix_format, width, rows, (uint32_t *)data, stride);543544 if (pix == NULL) {545 free(data);546 goto err;547 }548549 pixman_image_set_component_alpha(550 pix,551 bitmap->pixel_mode == FT_PIXEL_MODE_LCD ||552 bitmap->pixel_mode == FT_PIXEL_MODE_LCD_V);553554 if (font->pixel_size_fixup != 1.) {555 struct pixman_transform scale;556 pixman_transform_init_scale(557 &scale,558 pixman_double_to_fixed(1. / font->pixel_size_fixup),559 pixman_double_to_fixed(1. / font->pixel_size_fixup));560 pixman_image_set_transform(pix, &scale);561 pixman_image_set_filter(pix, PIXMAN_FILTER_BEST, NULL, 0);562 }563564 *glyph = (struct glyph){565 .wc = wc,566 .cols = wcwidth(wc),567 .pix = pix,568 .x = font->face->glyph->bitmap_left * font->pixel_size_fixup,569 .y = ceil(font->face->glyph->bitmap_top * font->pixel_size_fixup),570 .x_advance = ceil(font->face->glyph->advance.x / 64. * font->pixel_size_fixup),571 .width = width,572 .height = rows,573 .valid = true,574 };575576 return true;577578err:579 *glyph = (struct glyph){580 .wc = wc,581 .valid = false,582 };583 return false;584}585586const struct glyph *587font_glyph_for_wc(struct font *font, wchar_t wc)588{589 assert(font->cache != NULL);590 size_t hash_idx = hash_index(wc);591 hash_entry_t *hash_entry = font->cache[hash_idx];592593 if (hash_entry != NULL) {594 tll_foreach(*hash_entry, it) {595 if (it->item.wc == wc)596 return it->item.valid ? &it->item : NULL;597 }598 }599600 struct glyph glyph;601 bool got_glyph = glyph_for_wchar(font, wc, &glyph);602603 if (hash_entry == NULL) {604 hash_entry = calloc(1, sizeof(*hash_entry));605606 assert(font->cache[hash_idx] == NULL);607 font->cache[hash_idx] = hash_entry;608 }609610 assert(hash_entry != NULL);611 tll_push_back(*hash_entry, glyph);612 return got_glyph ? &tll_back(*hash_entry) : NULL;613}614615struct font *616font_clone(const struct font *_font)617{618 struct font *font = (struct font *)_font;619 font->ref_counter++;620 return font;621}622623void624font_destroy(struct font *font)625{626 if (font_destroy_no_free(font))627 free(font);628}
deleted:font.h-68 removed
1#pragma once23#include <stdbool.h>4#include <threads.h>56#include <ft2build.h>7#include FT_FREETYPE_H8#include FT_LCD_FILTER_H9#include <fontconfig/fontconfig.h>10#include <pixman.h>1112#include "tllist.h"1314struct glyph {15 wchar_t wc;16 int cols;1718 pixman_image_t *pix;19 int x;20 int y;21 int x_advance;22 int width;23 int height;2425 bool valid;26};2728typedef tll(struct glyph) hash_entry_t;2930struct font {31 char *name;3233 FcPattern *fc_pattern;34 FcFontSet *fc_fonts;35 int fc_idx;3637 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 */4344 int height;45 int descent;46 int ascent;47 int max_x_advance;4849 struct {50 double position;51 double thickness;52 } underline;5354 struct {55 double position;56 double thickness;57 } strikeout;5859 hash_entry_t **cache;6061 bool is_fallback;62 int ref_counter;63};6465struct font *font_from_name(const char *name);66struct font *font_clone(const struct font *font);67const struct glyph *font_glyph_for_wc(struct font *font, wchar_t wc);68void font_destroy(struct font *font);
modified:main.c+9 added-4 removed
9 unmodified lines
10#include <sys/stat.h>11#include <fcntl.h>1213#include <tllist.h>14#include <fcft/fcft.h>1516#define LOG_MODULE "fuzzel"17#define LOG_ENABLE_DBG 018#include "log.h"19#include "application.h"20#include "dmenu.h"21#include "fdm.h"21#include "font.h"22#include "match.h"23#include "render.h"24#include "shm.h"25#include "version.h"
321 unmodified lines
347 struct render *render = NULL;348 struct wayland *wayl = NULL;349 struct font *font = NULL;350351 font_list_t font_list = tll_init();352 tll_push_back(font_list, font_name);351 if ((font = font_from_name(font_name)) == NULL)353 if ((font = font_from_name(font_list, NULL)) == NULL) {354 tll_free(font_list);355 goto out;356 }357 tll_free(font_list);358359 LOG_DBG(360 "font: height: %d, ascent: %d, descent: %d",356 font->fextents.height, font->fextents.ascent, font->fextents.descent);361 font->height, font->ascent, font->descent);362363 /* Calculate number of entries we can show, based on the total364 * height and the fonts line height */365 const double line_height
1 unmodified line
367 const size_t max_matches =368 (render_options.height - 2 * render_options.border_size - line_height)369 / line_height;370366 LOG_DBG("max matches: %d", max_matches);371 LOG_DBG("max matches: %zu", max_matches);372373 /* Load applications */374 if ((apps = applications_init()) == NULL)375 goto out;
49 unmodified lines
modified:meson.build+15 added-7 removed
14 unmodified lines
15 language: 'c',16)1718pixman = dependency('pixman-1')19freetype = dependency('freetype2')20fontconfig = dependency('fontconfig')19cairo = dependency('cairo')20cairo_ft = dependency('cairo-ft')21librsvg = dependency('librsvg-2.0')22wayland_protocols = dependency('wayland-protocols')
34 unmodified lines
57 build_always_stale: true,58 output: 'version.h',59 command: [generate_version_sh, meson.project_version(), '@SOURCE_DIR@', '@OUTPUT@'])6063tllist = subproject('tllist').get_variable('tllist')61tllist_proj = subproject('tllist', required: false)62if tllist_proj.found()63 tllist = tllist_proj.get_variable('tllist')64else65 tllist = dependency('tllist')66endif6768fcft_proj = subproject('fcft', required: false)69if fcft_proj.found()70 fcft = fcft_proj.get_variable('fcft')71else72 fcft = dependency('fcft')73endif7475executable(76 'fuzzel',77 'application.c', 'application.h',78 'dmenu.c', 'dmenu.h',79 'fdm.c', 'fdm.h',70 'font.c', 'font.h',80 'icon.c', 'icon.h',81 'log.c', 'log.h',82 'main.c',83 'match.c', 'match.h',
4 unmodified lines
88 'wayland.c', 'wayland.h',89 'xdg.c', 'xdg.h',90 wl_proto_src + wl_proto_headers, version,91 dependencies: [pixman,83 freetype,84 fontconfig,92 cairo,93 cairo_ft,94 librsvg,95 wayland_client,96 wayland_cursor,97 wlroots,98 xkb,92 tllist],99 tllist,100 fcft],101 install: true)102103subdir('completions')104subdir('doc')
modified:PKGBUILD+1 added-4 removed
4 unmodified lines
5arch=('x86_64')6url=https://codeberg.org/dnkl/fuzzel7license=(mit)8makedepends=('meson' 'ninja' 'scdoc')9depends=(10 'libxkbcommon'11 'wayland' 'wlroots'12 'freetype2' 'fontconfig' 'cairo' 'librsvg')9depends=('libxkbcommon' 'wayland' 'wlroots' 'cairo' 'librsvg' 'tllist' 'fcft')10source=()1112pkgver() {13 cd ../.git &> /dev/null && git describe --tags --long | sed 's/^v//;s/\([^-]*-g\)/r\1/;s/-/./g' ||
11 unmodified lines
modified:render.c+2 added-5 removed
2 unmodified lines
3#include <stdlib.h>4#include <stdio.h>5#include <string.h>67#include <fcft/fcft.h>89#define LOG_MODULE "render"10#include "log.h"9#include "font.h"1112struct render {13 struct render_options options;14 struct font *regular_font;
158 unmodified lines
173 const double row_height = 2 * y_margin + font->height;174 const double first_row = 1 * border_size + row_height;175 const double sel_margin = x_margin / 3;176176 /*177 * LOG_DBG("height=%f, ascent=%f, descent=%f", fextents.height, fextents.ascent,178 * fextents.descent);179 */177 double y = first_row + (row_height + font->height) / 2 - font->descent;178179 for (size_t i = 0; i < match_count; i++) {180 const struct match *match = matches_get(matches, i);//&matches[i];
112 unmodified lines
modified:render.h+2 added-1 removed
1 unmodified line
23#include <pixman.h>4#include <cairo.h>56#include "font.h"6#include <fcft/fcft.h>78#include "shm.h"9#include "match.h"10#include "tllist.h"11
37 unmodified lines