Explorar el Código

Use the proportional font in text fields

ngui text inputs now draw through ufont instead of the 8x16 bitmap and derive caret, selection, click mapping, field height and horizontal scrolling from measured prefix widths.  The terminal and editor keep their bitmap cell grids; docs and AGENTS describe the split.
Johannes Zellner hace 1 semana
padre
commit
812cc089a9
Se han modificado 4 ficheros con 104 adiciones y 69 borrados
  1. 12 8
      AGENTS.md
  2. 1 1
      docs/graphics.html
  3. 5 4
      docs/userland.html
  4. 86 56
      ngui.c

+ 12 - 8
AGENTS.md

@@ -318,9 +318,11 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   live showcase of every control and primitive the toolkit exports.  Buttons
   fire on the release edge and keep `active` until `ui->released`: inferring
   release from the sampled `ui->buttons` races the WM event queue and drops
-  clicks.  Text fields support mouse drag-selection, double-click select-all,
-  Ctrl+Left/Right word jumps, Ctrl+Delete/Ctrl+Backspace word deletion and
-  Ctrl+A select-all; typing or Backspace/Delete replaces a selection.  At the
+  clicks.  Text fields use the proportional font with measured caret,
+  selection and horizontal scrolling, and support mouse drag-selection,
+  double-click select-all, Ctrl+Left/Right word jumps,
+  Ctrl+Delete/Ctrl+Backspace word deletion and Ctrl+A select-all; typing or
+  Backspace/Delete replaces a selection.  At the
   end of the line Ctrl+Delete removes the word before the caret instead of
   doing nothing.
 - **Graphics**: `vga.c/h`, `vga_copy.S`, `blit.c/h`, `gpu.c`, `ati.c`, `nvidia.c`,
@@ -383,8 +385,10 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   (`uwm_lib`) load faces and leave the 8×16 bitmap fallback when they are
   absent.  The **browser calls `ngui_font_detach` before `heap_reset()` and
   reattaches after `heap_start()`**, because its page heap is reset on every
-  navigation.  Terminal/editor/text fields deliberately keep the 8×16 cell
-  grid.  Host test: `make test-font` (`ufont_test.c`); `make fonts`
+  navigation.  Terminal and editor cells deliberately keep the 8×16 bitmap
+  grid; ngui text fields use the proportional face with measured caret,
+  selection and horizontal scrolling.  Host test: `make test-font`
+  (`ufont_test.c`); `make fonts`
   regenerates the subsets.  No hinting, CFF/variable fonts, shaping, bidi or
   full Unicode yet.
 - **Shell**: `ucmd_term.c` is the windowed shell (rows, scrollback, Tab,
@@ -1202,7 +1206,7 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
    `PACK_MAX` 512 KB (not the old ~255 KB low-gap limit).  The browser and
    its ring-3 dependencies are compiled with function sections and linked
    with `--gc-sections`; the current pack with the UI fonts is ~343 KiB
-   (351180 bytes), leaving ~169 KiB under the cap.  Command growth
+   (351472 bytes), leaving ~169 KiB under the cap.  Command growth
    still needs care because `libdeflate-gzip -12` is already required (plain
    `gzip -9` is over the cap).
 9. SMP is **ACPI/MADT only**: no Intel MP-table fallback, so a Pentium II SMP
@@ -1352,5 +1356,5 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     bitmap font.  The 256 KB arena is shared by five faces plus glyph masks;
     a very text-heavy page can exhaust it and fall back to re-rasterizing
     evicted glyphs.  The browser must detach/re-attach its font arena around
-    `heap_reset()`, and terminal/editor/text-input cell grids intentionally
-    stay on the 8×16 bitmap font.
+    `heap_reset()`; terminal/editor cell grids stay on the 8×16 bitmap font,
+    while ngui text fields use the proportional face.

+ 1 - 1
docs/graphics.html

@@ -263,7 +263,7 @@
 			<code>solitaire</code> and the real <code>gallery.elf</code>.
 		</p>
 		<ul>
-			<li><strong>Text fields</strong> support drag-selection, double-click select-all, Ctrl+Left/Right word jumps, Ctrl+Delete/Backspace word deletion and Ctrl+A. Their cell grid stays on the 8×16 bitmap font.</li>
+			<li><strong>Text fields</strong> support drag-selection, double-click select-all, Ctrl+Left/Right word jumps, Ctrl+Delete/Backspace word deletion and Ctrl+A. They use the proportional face with measured caret, selection and horizontal scrolling; the terminal and editor keep the 8×16 cell grid.</li>
 			<li><strong>Clip stack</strong> keeps nested panes and popups inside their parents.</li>
 			<li><strong>Layout cuts</strong> split a rectangle into rows, columns and fixed margins without a constraint solver.</li>
 			<li><strong>No retained widget tree.</strong> Apps redraw from state every frame, which keeps the toolkit small and predictable.</li>

+ 5 - 4
docs/userland.html

@@ -308,10 +308,11 @@
 			TrueType engine in <span class="file">ufont.c</span> with the Arimo
 			and Liberation Mono subsets in the pack. CSS and ngui can select
 			regular/bold/italic/mono faces at 8–48 px; missing faces fall back to
-			the 8×16 bitmap. Terminal contents, the editor and text-input cells
-			stay bitmap. The browser detaches the font arena before
-			<code>heap_reset()</code> and reattaches after the page heap is
-			mapped.
+			the 8×16 bitmap. Terminal contents and the editor stay bitmap;
+			ngui text fields use the proportional face for text, caret,
+			selection and horizontal scrolling. The browser detaches the font
+			arena before <code>heap_reset()</code> and reattaches after the page
+			heap is mapped.
 		</p>
 	</section>
 

+ 86 - 56
ngui.c

@@ -675,16 +675,6 @@ static void ngui_bitmap_glyph(struct ngui *ui, int x, int y, unsigned char c,
 	}
 }
 
-static void ngui_bitmap_text(struct ngui *ui, int x, int y, const char *s,
-			     unsigned short color)
-{
-	unsigned int i;
-
-	for (i = 0; s && s[i]; i++)
-		ngui_bitmap_glyph(ui, x + (int)i * 8, y,
-				  (unsigned char)s[i], color, 1, 0);
-}
-
 static struct ufont_face *ngui_face(struct ngui *ui, int bold, int italic,
 				    int mono)
 {
@@ -1272,56 +1262,93 @@ static void input_move_to(struct ngui_input *in, unsigned int pos)
 /* Two presses in the same field within this window select all of its text. */
 #define NGUI_DOUBLE_CLICK_MS 500
 
-/* Character position under a content-relative x, clamped to the visible
- * part of the field. */
-static unsigned int input_pos_at(const struct ngui_input *in, int inner_x,
-				 int cols, int x)
+static int input_text_w(struct ngui *ui, const char *s, unsigned int n)
 {
-	int col = (x - inner_x) / 8;
-	unsigned int pos;
+	return (int)ngui_text_width_px(ui, s, n, ui->font_px, 0, 0, 0);
+}
 
-	if (col < 0)
-		col = 0;
-	if (cols > 0 && col > cols)
-		col = cols;
-	pos = in->scroll + (unsigned int)col;
-	if (pos > in->len)
-		pos = in->len;
-	return pos;
+/* Character position under a content-relative x, clamped to the visible
+ * part of the field.  Proportional text means the prefix must be measured. */
+static unsigned int input_pos_at(struct ngui *ui, const struct ngui_input *in,
+				 int inner_x, int x)
+{
+	unsigned int i, n, best = 0;
+	int rel = x - inner_x;
+
+	if (rel <= 0 || in->scroll >= in->len)
+		return in->scroll;
+	n = in->len - in->scroll;
+	for (i = 1; i <= n; i++) {
+		if (input_text_w(ui, in->text + in->scroll, i) > rel)
+			break;
+		best = i;
+	}
+	return in->scroll + best;
 }
 
 static void input_draw_selection(struct ngui *ui, const struct ngui_input *in,
-				 struct ngui_rect inner, int cols)
+				 struct ngui_rect inner)
 {
 	unsigned int a, b, va, vb;
+	int xa, xb;
 
 	input_sel_bounds(in, &a, &b);
-	if (a == b || cols <= 0)
+	if (a == b || in->scroll >= in->len)
 		return;
 	va = a < in->scroll ? in->scroll : a;
-	vb = b > in->scroll + (unsigned int)cols
-	     ? in->scroll + (unsigned int)cols : b;
-	if (va < vb)
-		ngui_fill_rect(ui, rect_make(inner.x + (int)(va - in->scroll) * 8,
-					     inner.y, (int)(vb - va) * 8, 16),
+	vb = b < in->len ? b : in->len;
+	if (va >= vb)
+		return;
+	xa = input_text_w(ui, in->text + in->scroll, va - in->scroll);
+	xb = input_text_w(ui, in->text + in->scroll, vb - in->scroll);
+	if (xb > xa)
+		ngui_fill_rect(ui, rect_make(inner.x + xa, inner.y,
+					     xb - xa,
+					     ngui_text_line_height(ui,
+								  ui->font_px,
+								  0)),
 			       ui->theme.selection);
 }
 
+/* Scroll the first-visible-character index until the caret is visible. */
+static void input_scroll_to_cursor(struct ngui *ui, struct ngui_input *in,
+				   int inner_w)
+{
+	if (in->scroll > in->len)
+		in->scroll = in->len;
+	if (in->cursor < in->scroll)
+		in->scroll = in->cursor;
+	if (inner_w <= 0) {
+		in->scroll = in->len;
+		return;
+	}
+	while (in->scroll < in->cursor &&
+	       input_text_w(ui, in->text + in->scroll,
+			    in->cursor - in->scroll) > inner_w)
+		in->scroll++;
+	if (in->scroll > in->len)
+		in->scroll = in->len;
+}
+
 int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
 		    struct ngui_input *in)
 {
 	struct ngui_rect inner;
 	int focused = (ui->focus == id);
 	int result = 0;
-	int cols, k;
+	int avail, line_h, k;
 	int changed = 0;
 
-	inner = rect_make(r.x + 4, r.y + (r.h - 16) / 2, r.w - 8, 16);
+	line_h = ngui_text_line_height(ui, ui->font_px, 0);
+	if (line_h < 1)
+		line_h = 16;
+	inner = rect_make(r.x + 4, r.y + (r.h - line_h) / 2, r.w - 8,
+			  line_h);
 	if (inner.w < 0)
 		inner.w = 0;
-	cols = inner.w / 8;
-	if (cols < 0)
-		cols = 0;
+	if (inner.h < 1)
+		inner.h = 1;
+	avail = inner.w;
 
 	if (ui->pressed &&
 	    ngui_rect_contains(&r, ui->press_x, ui->press_y)) {
@@ -1337,7 +1364,7 @@ int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
 			in->cursor = in->len;
 			in->dragging = 0;
 		} else {
-			unsigned int pos = input_pos_at(in, inner.x, cols,
+			unsigned int pos = input_pos_at(ui, in, inner.x,
 							ui->press_x);
 
 			in->cursor = pos;
@@ -1359,7 +1386,7 @@ int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
 	 * release *event* is authoritative: the sampled button state can lag
 	 * the event queue by a frame. */
 	if (in->dragging && (ui->buttons & 1) && !ui->released) {
-		in->cursor = input_pos_at(in, inner.x, cols, ui->mouse_x);
+		in->cursor = input_pos_at(ui, in, inner.x, ui->mouse_x);
 		/* Keep redrawing when the pointer leaves the window. */
 		ngui_invalidate(ui);
 	}
@@ -1441,32 +1468,35 @@ int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
 	ngui_fill_rect(ui, r, focused ? ui->theme.panel_alt : ui->theme.panel);
 	ngui_frame(ui, r, focused ? ui->theme.accent : ui->theme.border);
 
-	if (in->scroll > in->len)
-		in->scroll = in->len;
-	if (in->cursor < in->scroll)
-		in->scroll = in->cursor;
-	if (cols > 0 && in->cursor >= in->scroll + (unsigned int)cols)
-		in->scroll = in->cursor - (unsigned int)cols + 1;
-	if (in->scroll > in->len)
-		in->scroll = in->len;
+	input_scroll_to_cursor(ui, in, avail);
 
 	ngui_push_clip(ui, inner);
 	if (in->len == 0 && in->placeholder) {
-		ngui_bitmap_text(ui, inner.x, inner.y, in->placeholder,
-				 ui->theme.text_dim);
+		ngui_text_px(ui, inner.x, inner.y, in->placeholder,
+			     slen(in->placeholder), ui->theme.text_dim,
+			     ui->font_px, 0, 0, 0);
 	} else {
 		if (focused)
-			input_draw_selection(ui, in, inner, cols);
-		ngui_bitmap_text(ui, inner.x, inner.y, in->text + in->scroll,
-				 ui->theme.text);
+			input_draw_selection(ui, in, inner);
+		ngui_text_px(ui, inner.x, inner.y, in->text + in->scroll,
+			     slen(in->text + in->scroll), ui->theme.text,
+			     ui->font_px, 0, 0, 0);
 	}
 
 	if (focused && ui->caret_on) {
-		int col = (int)in->cursor - (int)in->scroll;
-
-		if (col >= 0 && col <= cols)
-			ngui_fill_rect(ui, rect_make(inner.x + col * 8, inner.y,
-						     1, 16), ui->theme.accent);
+		int cx = 0;
+
+		if (in->cursor > in->scroll)
+			cx = input_text_w(ui, in->text + in->scroll,
+					  in->cursor - in->scroll);
+		if (cx < 0)
+			cx = 0;
+		if (avail > 0 && cx >= avail)
+			cx = avail - 1;
+		if (avail > 0 && cx < avail)
+			ngui_fill_rect(ui, rect_make(inner.x + cx, inner.y,
+						     1, inner.h),
+				       ui->theme.accent);
 	}
 	ngui_pop_clip(ui);