summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2021-09-07 20:07:38 +0200
committerAki <please@ignore.pl>2021-09-07 20:07:38 +0200
commite6d6284517ade1e71c9962909c1268cbfd5b6f9e (patch)
tree4ab4a1e3793a22dc0a4119cd19a15089081f4ebf
parent55ad9c74c62e896c67d585e6ec0f58745fe0dd52 (diff)
downloadtext-e6d6284517ade1e71c9962909c1268cbfd5b6f9e.zip
text-e6d6284517ade1e71c9962909c1268cbfd5b6f9e.tar.gz
text-e6d6284517ade1e71c9962909c1268cbfd5b6f9e.tar.bz2
Expanded input to drawing function
-rw-r--r--text.c77
1 files changed, 60 insertions, 17 deletions
diff --git a/text.c b/text.c
index 17a6cbd..b89b72a 100644
--- a/text.c
+++ b/text.c
@@ -9,12 +9,35 @@
#include <xcb/xcb.h>
#include <xcb/xcb_util.h>
+enum CommandType
+{
+ COMMAND_NOTHING,
+ COMMAND_DONE,
+ COMMAND_BLOCK,
+ COMMAND_SPAN,
+};
+
+union Command
+{
+ int type;
+ struct {
+ int type;
+ int length;
+ const char * data;
+ } block;
+ struct {
+ int type;
+ int position;
+ int length;
+ } span;
+};
+
int init_x(void);
-void draw_text(cairo_t *, const char *);
+void draw_body(cairo_t *);
xcb_visualtype_t * find_visual(xcb_screen_t *);
static const int MARGIN = 20;
-static const char * const FONT = "Serif 34";
+static const char * const FONT = "Serif 16";
static int width = 800;
static int height = 600;
@@ -24,12 +47,23 @@ static xcb_screen_t * s;
static xcb_visualtype_t * v;
static xcb_window_t w;
+#define _BLOCK(_txt) {.block = {.type = COMMAND_BLOCK, .length = sizeof(_txt) - 1, .data = _txt}}
+union Command body[] = {
+ _BLOCK(
+ "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam auctor porta eros at tempus. Aliquam "
+ "elementum lectus id mi fermentum, non consectetur urna lobortis."
+ ),
+ {.span = {.type = COMMAND_SPAN, .position = 27, .length = 11}},
+ _BLOCK("Phasellus dignissim rhoncus magna at imperdiet."),
+ {.type = COMMAND_DONE},
+};
+#undef _BLOCK
+
int main(int argc, const char ** argv)
{
- const char * text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
- if (2 == argc)
- text = argv[2];
+ (void) argc;
+ (void) argv;
const int res = init_x();
if (-1 == res)
return 1;
@@ -54,8 +88,7 @@ int main(int argc, const char ** argv)
case XCB_EXPOSE:
cairo_set_source_rgb(ctx, 1., 1., 1.);
cairo_paint(ctx);
- cairo_set_source_rgb(ctx, 0., 0., 0.);
- draw_text(ctx, text);
+ draw_body(ctx);
cairo_surface_flush(surface);
xcb_flush(c);
break;
@@ -116,17 +149,27 @@ xcb_visualtype_t * find_visual(xcb_screen_t * screen)
}
-void draw_text(cairo_t * ctx, const char * text)
+void draw_body(cairo_t * ctx)
{
- cairo_move_to(ctx, MARGIN, MARGIN);
- PangoLayout * layout = pango_cairo_create_layout(ctx);
PangoFontDescription * desc = pango_font_description_from_string(FONT);
- pango_layout_set_font_description(layout, desc);
+ cairo_set_source_rgb(ctx, 0., 0., 0.);
+ int y = MARGIN;
+ for (union Command * command = body; COMMAND_DONE != command->type; ++command)
+ {
+ if (COMMAND_SPAN == command->type)
+ continue;
+ cairo_move_to(ctx, MARGIN, y);
+ PangoLayout * layout = pango_cairo_create_layout(ctx);
+ pango_layout_set_font_description(layout, desc);
+ pango_layout_set_wrap(layout, PANGO_WRAP_WORD);
+ pango_layout_set_width(layout, (width - 2 * MARGIN) * PANGO_SCALE);
+ pango_layout_set_text(layout, command->block.data, command->block.length);
+ pango_cairo_update_layout(ctx, layout);
+ pango_cairo_show_layout(ctx, layout);
+ int height;
+ pango_layout_get_pixel_size(layout, NULL, &height);
+ y += height + MARGIN;
+ g_object_unref(layout);
+ }
pango_font_description_free(desc);
- pango_layout_set_text(layout, text, -1);
- pango_layout_set_wrap(layout, PANGO_WRAP_WORD);
- pango_layout_set_width(layout, (width - 2 * MARGIN) * PANGO_SCALE);
- pango_cairo_update_layout(ctx, layout);
- pango_cairo_show_layout(ctx, layout);
- g_object_unref(layout);
}