Commit b2ec260aab6b2e29abac7c2f42dfdafe891ea293

Thomas de Grivel 2024-02-12T20:18:18

cairo demo

diff --git a/libc3/window/cairo/cairo_font.h b/libc3/window/cairo/cairo_font.h
index fff82a4..f281fbf 100644
--- a/libc3/window/cairo/cairo_font.h
+++ b/libc3/window/cairo/cairo_font.h
@@ -13,8 +13,6 @@
 #ifndef LIBC3_WINDOW_CAIRO_CAIRO_FONT_H
 #define LIBC3_WINDOW_CAIRO_CAIRO_FONT_H
 
-#include <assert.h>
-#include <err.h>
 #include "types.h"
 
 /* Stack-allocation compatible functions, call cairo_font_clean
diff --git a/libc3/window/cairo/cairo_text.c b/libc3/window/cairo/cairo_text.c
new file mode 100644
index 0000000..47ad37d
--- /dev/null
+++ b/libc3/window/cairo/cairo_text.c
@@ -0,0 +1,39 @@
+/* c3
+ * Copyright 2022,2023 kmx.io <contact@kmx.io>
+ *
+ * Permission is hereby granted to use this software granted the above
+ * copyright notice and this permission paragraph are included in all
+ * copies and substantial portions of this software.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
+ * PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
+ * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
+ * THIS SOFTWARE.
+ */
+#include <libc3/c3.h>
+#include "cairo_text.h"
+
+void cairo_text_outline (cairo_t *cr, double x, double y,
+                         const char *p)
+{
+  cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
+  cairo_move_to(cr, x - 1.0, y - 1.0);
+  cairo_show_text(cr, p);
+  cairo_move_to(cr, x - 1.0, y);
+  cairo_show_text(cr, p);
+  cairo_move_to(cr, x - 1.0, y + 1.0);
+  cairo_show_text(cr, p);
+  cairo_move_to(cr, x, y - 1.0);
+  cairo_show_text(cr, p);
+  cairo_move_to(cr, x, y + 1.0);
+  cairo_show_text(cr, p);
+  cairo_move_to(cr, x + 1.0, y - 1.0);
+  cairo_show_text(cr, p);
+  cairo_move_to(cr, x + 1.0, y);
+  cairo_show_text(cr, p);
+  cairo_move_to(cr, x + 1.0, y + 1.0);
+  cairo_show_text(cr, p);
+  cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
+  cairo_move_to(cr, x, y);
+  cairo_show_text(cr, p);
+}
diff --git a/libc3/window/cairo/cairo_text.h b/libc3/window/cairo/cairo_text.h
new file mode 100644
index 0000000..f8c57f9
--- /dev/null
+++ b/libc3/window/cairo/cairo_text.h
@@ -0,0 +1,27 @@
+/* c3
+ * Copyright 2022,2023 kmx.io <contact@kmx.io>
+ *
+ * Permission is hereby granted to use this software granted the above
+ * copyright notice and this permission paragraph are included in all
+ * copies and substantial portions of this software.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
+ * PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
+ * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
+ * THIS SOFTWARE.
+ */
+#ifndef LIBC3_WINDOW_CAIRO_CAIRO_TEXT_H
+#define LIBC3_WINDOW_CAIRO_CAIRO_TEXT_H
+
+#include "types.h"
+
+/* Stack-allocation compatible functions, call cairo_font_clean
+   after use. */
+void           cairo_font_clean (s_cairo_font *font);
+s_cairo_font * cairo_font_init (s_cairo_font *font, const char *path);
+
+/* Observers */
+void cairo_text_outline (cairo_t *cr, double x, double y,
+                         const char *p);
+
+#endif /* LIBC3_WINDOW_CAIRO_CAIRO_TEXT_H */
diff --git a/libc3/window/cairo/demo/mandelbrot_f128.c b/libc3/window/cairo/demo/mandelbrot_f128.c
index 7d217a0..2b7e0c7 100644
--- a/libc3/window/cairo/demo/mandelbrot_f128.c
+++ b/libc3/window/cairo/demo/mandelbrot_f128.c
@@ -12,6 +12,7 @@
  */
 #include <math.h>
 #include <libc3/c3.h>
+#include "../cairo_text.h"
 #include "mandelbrot_f128.h"
 
 static cairo_surface_t *g_mandelbrot_f128_surface = NULL;
@@ -154,6 +155,23 @@ bool mandelbrot_f128_render (s_sequence *seq)
   cairo_set_source_surface(cr, g_mandelbrot_f128_surface, 0, 0);
   cairo_rectangle(cr, 0, 0, win->w, win->h);
   cairo_fill(cr);
+  char a[128];
+  s_buf buf;
+  buf_init(&buf, false, sizeof(a), a);
+  buf_write_1(&buf, "x: ");
+  buf_inspect_f128(&buf, &next_x);
+  buf_write_u8(&buf, 0);
+  cairo_text_outline(cr, 20, win->h - 100, a);
+  buf_init(&buf, false, sizeof(a), a);
+  buf_write_1(&buf, "y: ");
+  buf_inspect_f128(&buf, &next_y);
+  buf_write_u8(&buf, 0);
+  cairo_text_outline(cr, 20, win->h - 80, a);
+  buf_init(&buf, false, sizeof(a), a);
+  buf_write_1(&buf, "z: ");
+  buf_inspect_f128(&buf, &next_z);
+  buf_write_u8(&buf, 0);
+  cairo_text_outline(cr, 20, win->h - 60, a);
   return true;
 }
 
diff --git a/libc3/window/cairo/demo/window_cairo_demo.c b/libc3/window/cairo/demo/window_cairo_demo.c
index f2f7067..ce05733 100644
--- a/libc3/window/cairo/demo/window_cairo_demo.c
+++ b/libc3/window/cairo/demo/window_cairo_demo.c
@@ -17,6 +17,7 @@
 #include "../../window.h"
 #include "../cairo_font.h"
 #include "../cairo_sprite.h"
+#include "../cairo_text.h"
 #include "../window_cairo.h"
 #include "window_cairo_demo.h"
 #include "bg_rect.h"
@@ -122,32 +123,6 @@ bool window_cairo_demo_load (s_window_cairo *window)
   return true;
 }
 
-static void render_text (cairo_t *cr, double x, double y,
-                         const char *p)
-{
-  cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
-  cairo_move_to(cr, x - 1.0, y - 1.0);
-  cairo_show_text(cr, p);
-  cairo_move_to(cr, x - 1.0, y);
-  cairo_show_text(cr, p);
-  cairo_move_to(cr, x - 1.0, y + 1.0);
-  cairo_show_text(cr, p);
-  cairo_move_to(cr, x, y - 1.0);
-  cairo_show_text(cr, p);
-  cairo_move_to(cr, x, y + 1.0);
-  cairo_show_text(cr, p);
-  cairo_move_to(cr, x + 1.0, y - 1.0);
-  cairo_show_text(cr, p);
-  cairo_move_to(cr, x + 1.0, y);
-  cairo_show_text(cr, p);
-  cairo_move_to(cr, x + 1.0, y + 1.0);
-  cairo_show_text(cr, p);
-  /* text */
-  cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
-  cairo_move_to(cr, x, y);
-  cairo_show_text(cr, p);
-}
-
 bool window_cairo_demo_render (s_window_cairo *window)
 {
   cairo_t *cr;
@@ -166,7 +141,7 @@ bool window_cairo_demo_render (s_window_cairo *window)
   cairo_set_font_size(cr, 20);
   cairo_set_font(cr, &g_font_courier_new);
   cairo_text_extents(cr, seq->title, &te);
-  render_text(cr, 20.0, window->h - te.height - te.y_bearing - 20,
+  cairo_text_outline(cr, 20.0, window->h - te.height - te.y_bearing - 20,
               seq->title);
   /* progress bar */
   cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
@@ -183,7 +158,7 @@ bool window_cairo_demo_render (s_window_cairo *window)
   char fps[32];
   snprintf(fps, sizeof(fps), "%f", (f64) seq->frame / seq->t);
   cairo_text_extents(cr, fps, &te);
-  render_text(cr, 20.0, 20.0 + te.height, fps);
+  cairo_text_outline(cr, 20.0, 20.0 + te.height, fps);
   return true;
 }
 
diff --git a/libc3/window/cairo/quartz/sources.mk b/libc3/window/cairo/quartz/sources.mk
index b1bdd8a..09a109b 100644
--- a/libc3/window/cairo/quartz/sources.mk
+++ b/libc3/window/cairo/quartz/sources.mk
@@ -1,18 +1,18 @@
 # sources.mk generated by update_sources
 HEADERS = \
 	"quartz_to_xkbcommon.h" \
-	"window_cairo_quartz.h" \
 	"window_cairo_quartz_app_delegate.h" \
-	"window_cairo_quartz_view.h" \
+	"window_cairo_quartz.h" \
 	"window_cairo_quartz_view_controller.h" \
+	"window_cairo_quartz_view.h" \
 	"xkbquartz.h" \
 
 SOURCES = \
 	"quartz_to_xkbcommon.c" \
 
 OBJC_SOURCES = \
-	"window_cairo_quartz.m" \
 	"window_cairo_quartz_app_delegate.m" \
-	"window_cairo_quartz_view.m" \
+	"window_cairo_quartz.m" \
 	"window_cairo_quartz_view_controller.m" \
+	"window_cairo_quartz_view.m" \
 
diff --git a/libc3/window/cairo/quartz/sources.sh b/libc3/window/cairo/quartz/sources.sh
index 03cc583..baf8b5a 100644
--- a/libc3/window/cairo/quartz/sources.sh
+++ b/libc3/window/cairo/quartz/sources.sh
@@ -1,4 +1,4 @@
 # sources.sh generated by update_sources
-HEADERS='quartz_to_xkbcommon.h window_cairo_quartz.h window_cairo_quartz_app_delegate.h window_cairo_quartz_view.h window_cairo_quartz_view_controller.h xkbquartz.h '
+HEADERS='quartz_to_xkbcommon.h window_cairo_quartz_app_delegate.h window_cairo_quartz.h window_cairo_quartz_view_controller.h window_cairo_quartz_view.h xkbquartz.h '
 SOURCES='quartz_to_xkbcommon.c '
-OBJC_SOURCES='window_cairo_quartz.m window_cairo_quartz_app_delegate.m window_cairo_quartz_view.m window_cairo_quartz_view_controller.m '
+OBJC_SOURCES='window_cairo_quartz_app_delegate.m window_cairo_quartz.m window_cairo_quartz_view_controller.m window_cairo_quartz_view.m '
diff --git a/libc3/window/cairo/sources.mk b/libc3/window/cairo/sources.mk
index 4f3037a..cd231cc 100644
--- a/libc3/window/cairo/sources.mk
+++ b/libc3/window/cairo/sources.mk
@@ -2,11 +2,13 @@
 HEADERS = \
 	"cairo_font.h" \
 	"cairo_sprite.h" \
+	"cairo_text.h" \
 	"types.h" \
 	"window_cairo.h" \
 
 SOURCES = \
 	"cairo_font.c" \
 	"cairo_sprite.c" \
+	"cairo_text.c" \
 	"window_cairo.c" \
 
diff --git a/libc3/window/cairo/sources.sh b/libc3/window/cairo/sources.sh
index 35b3d02..7fcb3b5 100644
--- a/libc3/window/cairo/sources.sh
+++ b/libc3/window/cairo/sources.sh
@@ -1,3 +1,3 @@
 # sources.sh generated by update_sources
-HEADERS='cairo_font.h cairo_sprite.h types.h window_cairo.h '
-SOURCES='cairo_font.c cairo_sprite.c window_cairo.c '
+HEADERS='cairo_font.h cairo_sprite.h cairo_text.h types.h window_cairo.h '
+SOURCES='cairo_font.c cairo_sprite.c cairo_text.c window_cairo.c '