commit 5a86694d2ead6de5874bd2dc26fc7c531b81b9f6
parent 42f4ff374b3ba05c9e35e1682f49b3601b7a21ee
Author: lumidify <nobody@lumidify.org>
Date:   Sat, 28 Mar 2020 20:50:40 +0100
Fix Urdu rendering
Diffstat:
| M | text-hb.c |  |  | 40 | ++++++++++++++++------------------------ | 
1 file changed, 16 insertions(+), 24 deletions(-)
diff --git a/text-hb.c b/text-hb.c
@@ -299,6 +299,7 @@ ltk_load_font(LtkTextManager *tm, char *path)
 uint16_t
 ltk_get_font(LtkTextManager *tm, char *path)
 {
+	printf("%s\n", path);
 	int ret;
 	khint_t k;
 	uint16_t id;
@@ -317,17 +318,14 @@ ltk_get_font(LtkTextManager *tm, char *path)
 LtkTextLine *
 ltk_create_text_line(LtkTextManager *tm, char *text, uint16_t fontid, uint16_t size)
 {
-	LtkFont *font;
-	LtkFont *default_font;
+	/* NOTE: This doesn't actually take fontid into account right now - should it? */
 	LtkTextLine *tl = malloc(sizeof(LtkTextLine));
 	tl->start_segment = NULL;
 	LtkTextSegment *cur_ts = NULL;
 	LtkTextSegment *new_ts = NULL;
-	uint16_t default_font_id = fontid;
-	uint16_t last_font_id = fontid;
 	uint16_t cur_font_id = fontid;
-	int k = kh_get(fontstruct, tm->font_cache, fontid);
-	font = default_font = kh_value(tm->font_cache, k);
+	int k;
+	LtkFont *font;
 
 	unsigned int ulen = u8_strlen(text);
 	FriBidiChar *log_str = malloc(sizeof(FriBidiChar) * ulen);
@@ -350,13 +348,10 @@ ltk_create_text_line(LtkTextManager *tm, char *text, uint16_t fontid, uint16_t s
 	size_t last_pos = 0;
 	size_t start_pos = 0;
 	uint32_t ch;
-	uint32_t gid;
-	for (int p = 0; p < ulen; p++) {
-		gid = stbtt_FindGlyphIndex(&font->info, vis_str[p]);
+
+	for (int p = 0; p <= ulen; p++) {
 		cur_script = hb_unicode_script(ufuncs, vis_str[p]);
-		// FIXME: handle inherited and common scripts at beginning of string
-		if (!gid || (last_script != cur_script && cur_script != HB_SCRIPT_INHERITED && cur_script != HB_SCRIPT_COMMON)) {
-			/* This is extremely efficient... */
+		if (p == ulen || (last_script != cur_script && cur_script != HB_SCRIPT_INHERITED && cur_script != HB_SCRIPT_COMMON)) {
 			FcPattern *pat = FcPatternDuplicate(tm->fcpattern);
 			FcPattern *match;
 			FcResult result;
@@ -364,36 +359,33 @@ ltk_create_text_line(LtkTextManager *tm, char *text, uint16_t fontid, uint16_t s
 			FcConfigSubstitute(NULL, pat, FcMatchPattern);
 			FcDefaultSubstitute(pat);
 			FcCharSet *cs = FcCharSetCreate();
-			FcCharSetAddChar(cs, vis_str[p]);
+			for (int i = start_pos; i < p; i++) {
+				FcCharSetAddChar(cs, vis_str[i]);
+			}
 			FcPatternAddCharSet(pat, FC_CHARSET, cs);
 			match = FcFontMatch(NULL, pat, &result);
 			char *file;
 			FcPatternGetString(match, FC_FILE, 0, &file);
-			last_font_id = cur_font_id;
 			cur_font_id = ltk_get_font(tm, file);
 			k = kh_get(fontstruct, tm->font_cache, cur_font_id);
 			font = kh_value(tm->font_cache, k);
 			FcPatternDestroy(match);
 			FcPatternDestroy(pat);
-
-			new_ts = ltk_create_text_segment(tm, vis_str + start_pos, p - start_pos, last_font_id, size, last_script);
-			// FIXME: error
+			// handle case that this is the last character
+			if (p == ulen) {
+				last_script = cur_script;
+			}
+			new_ts = ltk_create_text_segment(tm, vis_str + start_pos, p - start_pos, cur_font_id, size, last_script);
 			if (!new_ts) continue;
 			new_ts->next = NULL;
 			if (!tl->start_segment) tl->start_segment = new_ts;
 			if (cur_ts) cur_ts->next = new_ts;
 			cur_ts = new_ts;
-			start_pos = p;
 
+			start_pos = p;
 			last_script = cur_script;
 		}
 	}
-	new_ts = ltk_create_text_segment(tm, vis_str + start_pos, ulen - start_pos, cur_font_id, size, last_script);
-	// FIXME: error if new_ts null
-	new_ts->next = NULL;
-	if (!tl->start_segment) tl->start_segment = new_ts;
-	if (cur_ts) cur_ts->next = new_ts;
-	cur_ts = new_ts;
 
 	free(vis_str);
 	free(log_str);