diff --git a/crates/lightgui-designer/src/canvas.rs b/crates/lightgui-designer/src/canvas.rs index a041125..f2dcff5 100644 --- a/crates/lightgui-designer/src/canvas.rs +++ b/crates/lightgui-designer/src/canvas.rs @@ -1307,16 +1307,24 @@ fn refresh_item_style(app: &Shared, id: u64) { let radius = if br_i > 0 { br_i } else { 6 }; let linewidth = if bw_i > 0 { bw_i } else { 1 }; // Pick the first loadable XLFD from the candidate chain (probing - // here, at refresh time) and cache its font id for redraws. + // here, at refresh time) and cache its font id for redraws. A + // wildcard pattern can match a two-byte font first (e.g. a JIS + // fixed font at size 16, which has no misc twin); XDrawString + // with 1-byte Latin text draws nothing with such a font, so every + // match is listed and the first single-byte one wins. let font_xlfd = { let cands = font_candidates(&font_name, font_size, font_bold, font_italic); let mut picked = FONT_DEFAULT.to_string(); if let Some(x11) = a.backend.x11.clone() { - for c in &cands { - if let Ok(fid) = x11.open_font(&[c.as_str()]) { - a.backend.font_ids.insert(c.clone(), fid); - picked = c.clone(); - break; + 'pick: for c in &cands { + for name in x11.list_fonts(c, 64) { + if x11.font_is_single_byte(&name) { + if let Ok(fid) = x11.open_font(&[name.as_str()]) { + a.backend.font_ids.insert(name.clone(), fid); + picked = name; + break 'pick; + } + } } } } else if let Some(first) = cands.into_iter().next() { diff --git a/crates/lightgui-gen/src/gen.rs b/crates/lightgui-gen/src/gen.rs index 29033e1..630264d 100644 --- a/crates/lightgui-gen/src/gen.rs +++ b/crates/lightgui-gen/src/gen.rs @@ -1686,6 +1686,12 @@ fn widget_font_expr(w: &Widget) -> String { const LIGHTGUI_PICK_FONT: &str = r##"// Pick a loadable XLFD for a per-widget font (Phase 1, Flag 2): // family/size/bold/italic, then oblique for italic, then plain, then // fixed at the requested size, then the fixed 13 default. +// A wildcard pattern can match a two-byte font first (e.g. a JIS fixed +// font at size 16, which has no misc twin). XDrawString with 1-byte +// Latin text draws nothing with such a font, so every match is listed +// and the first SINGLE-BYTE font (min_byte1 == max_byte1 == 0) wins; +// its full XLFD name is returned, since the pattern itself would +// re-resolve to the two-byte font. static std::string lightgui_pick_font(Display* display, const std::string& family, int size, bool bold, bool italic) { @@ -1711,11 +1717,20 @@ static std::string lightgui_pick_font(Display* display, cands.push_back("-misc-fixed-medium-r-normal--13-*"); for (const std::string& pat : cands) { int count = 0; - char** list = XListFonts(display, pat.c_str(), 1, &count); - if (list) { - XFreeFontNames(list); - if (count > 0) return pat; + char** list = XListFonts(display, pat.c_str(), 64, &count); + if (!list) continue; + for (int i = 0; i < count; ++i) { + XFontStruct* f = XLoadQueryFont(display, list[i]); + if (!f) continue; + bool single_byte = (f->min_byte1 == 0 && f->max_byte1 == 0); + XFreeFont(display, f); + if (single_byte) { + std::string hit = list[i]; + XFreeFontNames(list); + return hit; + } } + XFreeFontNames(list); } return "-misc-fixed-medium-r-normal--13-*"; } diff --git a/crates/lightgui-gen/tests/fixtures/03_all_props/main.cpp b/crates/lightgui-gen/tests/fixtures/03_all_props/main.cpp index b8f3ace..e342393 100644 --- a/crates/lightgui-gen/tests/fixtures/03_all_props/main.cpp +++ b/crates/lightgui-gen/tests/fixtures/03_all_props/main.cpp @@ -21,6 +21,12 @@ // Pick a loadable XLFD for a per-widget font (Phase 1, Flag 2): // family/size/bold/italic, then oblique for italic, then plain, then // fixed at the requested size, then the fixed 13 default. +// A wildcard pattern can match a two-byte font first (e.g. a JIS fixed +// font at size 16, which has no misc twin). XDrawString with 1-byte +// Latin text draws nothing with such a font, so every match is listed +// and the first SINGLE-BYTE font (min_byte1 == max_byte1 == 0) wins; +// its full XLFD name is returned, since the pattern itself would +// re-resolve to the two-byte font. static std::string lightgui_pick_font(Display* display, const std::string& family, int size, bool bold, bool italic) { @@ -46,11 +52,20 @@ static std::string lightgui_pick_font(Display* display, cands.push_back("-misc-fixed-medium-r-normal--13-*"); for (const std::string& pat : cands) { int count = 0; - char** list = XListFonts(display, pat.c_str(), 1, &count); - if (list) { - XFreeFontNames(list); - if (count > 0) return pat; + char** list = XListFonts(display, pat.c_str(), 64, &count); + if (!list) continue; + for (int i = 0; i < count; ++i) { + XFontStruct* f = XLoadQueryFont(display, list[i]); + if (!f) continue; + bool single_byte = (f->min_byte1 == 0 && f->max_byte1 == 0); + XFreeFont(display, f); + if (single_byte) { + std::string hit = list[i]; + XFreeFontNames(list); + return hit; + } } + XFreeFontNames(list); } return "-misc-fixed-medium-r-normal--13-*"; } diff --git a/crates/xframe-common/src/x11.rs b/crates/xframe-common/src/x11.rs index b20d472..c23ebd3 100644 --- a/crates/xframe-common/src/x11.rs +++ b/crates/xframe-common/src/x11.rs @@ -184,6 +184,41 @@ impl X11 { Err(last_err.unwrap_or_else(|| "no font".into())) } + /// List the font names matching an XLFD pattern (up to max_names). + pub fn list_fonts(&self, pattern: &str, max_names: u16) -> Vec { + match self.conn.list_fonts(max_names, pattern.as_bytes()) { + Ok(cookie) => match cookie.reply() { + Ok(r) => r.names.iter().map(|n| String::from_utf8_lossy(&n.name).into_owned()).collect(), + Err(_) => Vec::new(), + }, + Err(_) => Vec::new(), + } + } + + /// True when the named font is single-byte, i.e. it renders 1-byte + /// Latin text with XDrawString. Two-byte fonts (JIS, GB, KSC) have + /// max_byte1 > 0 and draw nothing for Latin text. + pub fn font_is_single_byte(&self, name: &str) -> bool { + let Ok(fid) = self.conn.generate_id() else { + return false; + }; + let Ok(cookie) = self.conn.open_font(fid, name.as_bytes()) else { + return false; + }; + if cookie.check().is_err() { + return false; + } + let single = match self.conn.query_font(fid) { + Ok(c) => match c.reply() { + Ok(r) => r.min_byte1 == 0 && r.max_byte1 == 0, + Err(_) => false, + }, + Err(_) => false, + }; + let _ = self.conn.close_font(fid); + single + } + pub fn font_ascent_descent(&self, font: Font) -> (i16, i16) { match self.conn.query_font(font) { Ok(c) => match c.reply() {