// InstData.h — shared data types and deterministic demo values for the // XFrame Instrumentation widget section (tank level, switches, lamps, LEDs, // gauges, panel furniture). // // No hardware I/O, no network: demos and tests run on deterministic values // defined here, so results are reproducible. // Attribution: XFrame. #pragma once #include #include #include #include #include #include #include namespace xframe { // Tank / channel level, percent full, clamped to [0, 100]. inline double inst_clamp_level(double pct) { if (pct < 0.0) return 0.0; if (pct > 100.0) return 100.0; return pct; } // Alarm bands for a level, in percent. hi_hi >= hi >= lo >= lo_lo. struct InstAlarmBands { double hi_hi = 95.0; double hi = 80.0; double lo = 20.0; double lo_lo = 5.0; }; // Switch positions. Centre-off rockers use Off as the middle position; // breakers use Tripped for the fault position. enum class InstSwitchState { Off = 0, On = 1, Tripped = 2 }; // One illuminator: on/off plus brightness 0-100 (100 = full). struct InstLamp { bool on = false; int brightness = 100; }; // One RGB status LED: three independent channels, each an independent // illuminator. Any of the 8 colour combinations plus off. struct InstRgb { bool red = false; bool green = false; bool blue = false; }; // Deterministic demo level for a widget, in [0, 100]. Same seed gives the // same level on every run and every platform. inline double inst_demo_level(unsigned seed = 42) { std::uint64_t x = 0x9E3779B97F4A7C15ULL ^ (static_cast(seed) * 0xBF58476D1CE4E5B9ULL); x ^= x >> 12; x ^= x << 25; x ^= x >> 27; const std::uint64_t v = x * 0x2545F4914F6CDD1DULL; return static_cast(v % 9001) / 100.0 + 5.0; // 5.00 .. 95.00 } // Deterministic demo value for a gauge, in [min, max]. inline double inst_demo_value(double min_v, double max_v, unsigned seed = 42) { if (max_v <= min_v) return min_v; const double f = inst_demo_level(seed) / 100.0; return min_v + f * (max_v - min_v); } // Bold instrumentation font: the 13 px medium core font grown by 33% // (13 * 1.33 = 17.29) and made bold. The exact 17 px size does not exist // in the server fixed-font set (probed Nov 26, 2026: bold sizes available // are 13, 14, 15, 18), so 18 px is the target and the others are // fallbacks in probe order. Lazy static, loaded once; never XFreeFont'd: // a process-lifetime resource, the display closes before any destructor // runs (same rationale as the SStyle_Manager comment in // core/xframe_core.h). Returns nullptr if no size loads, in which case // callers keep the style's own font. inline XFontStruct* inst_bold_font(Display* display) { if (!display) return nullptr; static XFontStruct* cached = nullptr; static bool tried = false; if (tried) return cached; tried = true; static const char* kNames[] = { "-misc-fixed-bold-r-normal--18-*", "-misc-fixed-bold-r-normal--17-*", "-misc-fixed-bold-r-normal--16-*", "-misc-fixed-bold-r-normal--20-*", }; for (const char* name : kNames) { cached = XLoadQueryFont(display, name); if (cached) break; } return cached; } // Scaled monitor fonts (Dec 5, 2026, monitor proportions audit): the C // monitor widgets derive every font size from the widget's current // dimensions, so they need core fonts at arbitrary pixel sizes, bold and // medium. Probed Dec 5, 2026 on the dallas Xvfb: bitmap bold exists only // at 13/14/15/18 px and bitmap medium only at 13/18/20 px, but the // misc-fixed family is scalable — an XLFD with an explicit pixel size and // wildcards for the remaining fields loads at that size (20/24/28 px // verified for bold; the same pattern works for medium). So try the // scalable XLFD at the requested size first, then bitmap fallbacks. // Loaded fonts are cached per (size, weight) in a process-lifetime table // and never XFreeFont'd (same rationale as inst_bold_font above). // Returns nullptr when nothing loads; callers then keep the style font. inline XFontStruct* scaled_font(Display* display, int px, bool bold) { if (!display) return nullptr; if (px < 8) px = 8; if (px > 64) px = 64; struct Entry { int px; bool bold; XFontStruct* font; }; static std::vector cache; for (auto& e : cache) if (e.px == px && e.bold == bold) return e.font; XFontStruct* font = nullptr; char name[96]; // Scalable XLFD: explicit pixel size, wildcards elsewhere. std::snprintf(name, sizeof(name), "-misc-fixed-%s-r-normal--%d-*-75-75-c-*-iso8859-1", bold ? "bold" : "medium", px); font = XLoadQueryFont(display, name); if (!font) { // Bitmap fallbacks, nearest size first. static const int kBoldFallbacks[] = {18, 20, 15, 14, 13}; static const int kMediumFallbacks[] = {20, 18, 13}; const int* fallbacks = bold ? kBoldFallbacks : kMediumFallbacks; int n = bold ? 5 : 3; for (int i = 0; i < n && !font; ++i) { std::snprintf(name, sizeof(name), "-misc-fixed-%s-r-normal--%d-*", bold ? "bold" : "medium", fallbacks[i]); font = XLoadQueryFont(display, name); } } if (!font) font = XLoadQueryFont(display, "fixed"); if ((int)cache.size() < 24) cache.push_back(Entry{px, bold, font}); return font; } // Width of `s` in `font` (XTextWidth), or 8 px/char when no font. inline int font_text_width(XFontStruct* font, const std::string& s) { if (!font) return (int)s.size() * 8; return XTextWidth(font, s.c_str(), (int)s.size()); } // Draw `s` at baseline (x, baseline_y) in `font` and foreground `fg`. // No-op when the font failed to load. inline void draw_text_font(Display* display, Window window, GC gc, XFontStruct* font, int x, int baseline_y, const std::string& s, unsigned long fg) { if (!font || s.empty()) return; XSetFont(display, gc, font->fid); XSetForeground(display, gc, fg); XDrawString(display, window, gc, x, baseline_y, s.c_str(), (int)s.size()); } // Clamp a derived font size: at least `lo` px, at most `hi` px. inline int clamp_font_px(double px, int lo, int hi) { int v = (int)std::lround(px); if (v < lo) v = lo; if (v > hi) v = hi; return v; } // Scaled bold fonts — Inst* proportions fix (Dec 2026 audit, G1). // Audit sizes are CAP heights (glyph height); a core font's pixel size // is ~cap / 0.72 (probed on hot_dallas: misc-fixed bold 48 px renders // ~34 px cap-height digits; helvetica bold the same). The X server only // ships bitmap fixed fonts at 13/14/15/18 px, but the scalable XLFD // entries ("-...---0-75-75-...") render cleanly at any size // (verified 14..48 px, mono + helvetica). Cached per cap height; // falls back to inst_bold_font() when a size cannot load. inline int inst_px_for_cap(int cap) { if (cap <= 0) return 18; return (cap * 100 + 71) / 72; } // Bold monospace font with cap height ~cap px (numerals, values). inline XFontStruct* inst_cap_font(Display* display, int cap) { if (!display) return nullptr; static std::map cache; auto it = cache.find(cap); if (it != cache.end()) return it->second; int px = inst_px_for_cap(cap); char name[160]; XFontStruct* f = nullptr; std::snprintf(name, sizeof name, "-misc-fixed-bold-r-normal--%d-0-75-75-c-0-iso8859-1", px); f = XLoadQueryFont(display, name); if (!f) { std::snprintf(name, sizeof name, "-adobe-helvetica-bold-r-normal--%d-0-75-75-p-0-iso8859-1", px); f = XLoadQueryFont(display, name); } if (!f) f = inst_bold_font(display); cache[cap] = f; return f; } // Bold proportional font with cap height ~cap px (titles, word labels). inline XFontStruct* inst_cap_font_prop(Display* display, int cap) { if (!display) return nullptr; static std::map cache; auto it = cache.find(cap); if (it != cache.end()) return it->second; int px = inst_px_for_cap(cap); char name[160]; XFontStruct* f = nullptr; std::snprintf(name, sizeof name, "-adobe-helvetica-bold-r-normal--%d-0-75-75-p-0-iso8859-1", px); f = XLoadQueryFont(display, name); if (!f) { std::snprintf(name, sizeof name, "-urw-nimbus sans l-bold-r-normal--%d-0-0-0-p-0-iso8859-1", px); f = XLoadQueryFont(display, name); } if (!f) f = inst_bold_font(display); cache[cap] = f; return f; } // Largest proportional-bold cap height (<= max_cap) whose rendering of // `text` fits in target_w px; 8 px floor. Used by InstNameplate so the // engraving fills ~40-50% of the plate width. inline XFontStruct* inst_prop_font_fit(Display* display, const std::string& text, int target_w, int max_cap) { if (!display || text.empty() || target_w <= 0) return inst_cap_font_prop(display, 16); for (int cap = max_cap; cap >= 8; --cap) { XFontStruct* f = inst_cap_font_prop(display, cap); if (!f) continue; if (XTextWidth(f, text.c_str(), (int)text.size()) <= target_w) return f; } return inst_cap_font_prop(display, 8); } } // namespace xframe