]> git.sur5r.net Git - i3/i3/blobdiff - libi3/string.c
Remove conditional compilation for cairo/pangocairo (#2480)
[i3/i3] / libi3 / string.c
index 009312d6946ea69f865605850b61f684fbe6e7d0..19039a23564f4ac43948c3acba568900e3262f98 100644 (file)
@@ -2,7 +2,7 @@
  * vim:ts=4:sw=4:expandtab
  *
  * i3 - an improved dynamic tiling window manager
- * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
+ * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
  *
  * string.c: Define an i3String type to automagically handle UTF-8/UCS-2
  *           conversions. Some font backends need UCS-2 (X core fonts),
@@ -13,6 +13,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include <glib.h>
+
 #include "libi3.h"
 
 struct _i3String {
@@ -20,6 +22,7 @@ struct _i3String {
     xcb_char2b_t *ucs2;
     size_t num_glyphs;
     size_t num_bytes;
+    bool pango_markup;
 };
 
 /*
@@ -28,7 +31,7 @@ struct _i3String {
  *
  */
 i3String *i3string_from_utf8(const char *from_utf8) {
-    i3String *str = scalloc(sizeof(i3String));
+    i3String *str = scalloc(1, sizeof(i3String));
 
     /* Get the text */
     str->utf8 = sstrdup(from_utf8);
@@ -39,6 +42,19 @@ i3String *i3string_from_utf8(const char *from_utf8) {
     return str;
 }
 
+/*
+ * Build an i3String from an UTF-8 encoded string in Pango markup.
+ *
+ */
+i3String *i3string_from_markup(const char *from_markup) {
+    i3String *str = i3string_from_utf8(from_markup);
+
+    /* Set the markup flag */
+    str->pango_markup = true;
+
+    return str;
+}
+
 /*
  * Build an i3String from an UTF-8 encoded string with fixed length.
  * To be used when no proper NUL-terminaison is available.
@@ -46,10 +62,10 @@ i3String *i3string_from_utf8(const char *from_utf8) {
  *
  */
 i3String *i3string_from_utf8_with_length(const char *from_utf8, size_t num_bytes) {
-    i3String *str = scalloc(sizeof(i3String));
+    i3String *str = scalloc(1, sizeof(i3String));
 
     /* Copy the actual text to our i3String */
-    str->utf8 = scalloc(sizeof(char) * (num_bytes + 1));
+    str->utf8 = scalloc(num_bytes + 1, 1);
     strncpy(str->utf8, from_utf8, num_bytes);
     str->utf8[num_bytes] = '\0';
 
@@ -59,18 +75,31 @@ i3String *i3string_from_utf8_with_length(const char *from_utf8, size_t num_bytes
     return str;
 }
 
+/*
+ * Build an i3String from an UTF-8 encoded string in Pango markup with fixed
+ * length.
+ *
+ */
+i3String *i3string_from_markup_with_length(const char *from_markup, size_t num_bytes) {
+    i3String *str = i3string_from_utf8_with_length(from_markup, num_bytes);
+
+    /* set the markup flag */
+    str->pango_markup = true;
+
+    return str;
+}
+
 /*
  * Build an i3String from an UCS-2 encoded string.
  * Returns the newly-allocated i3String.
  *
  */
 i3String *i3string_from_ucs2(const xcb_char2b_t *from_ucs2, size_t num_glyphs) {
-    i3String *str = scalloc(sizeof(i3String));
+    i3String *str = scalloc(1, sizeof(i3String));
 
     /* Copy the actual text to our i3String */
-    size_t num_bytes = num_glyphs * sizeof(xcb_char2b_t);
-    str->ucs2 = scalloc(num_bytes);
-    memcpy(str->ucs2, from_ucs2, num_bytes);
+    str->ucs2 = scalloc(num_glyphs, sizeof(xcb_char2b_t));
+    memcpy(str->ucs2, from_ucs2, num_glyphs * sizeof(xcb_char2b_t));
 
     /* Store the length */
     str->num_glyphs = num_glyphs;
@@ -81,6 +110,16 @@ i3String *i3string_from_ucs2(const xcb_char2b_t *from_ucs2, size_t num_glyphs) {
     return str;
 }
 
+/**
+ * Copies the given i3string.
+ * Note that this will not free the source string.
+ */
+i3String *i3string_copy(i3String *str) {
+    i3String *copy = i3string_from_utf8(i3string_as_utf8(str));
+    copy->pango_markup = str->pango_markup;
+    return copy;
+}
+
 /*
  * Free an i3String.
  *
@@ -133,6 +172,31 @@ size_t i3string_get_num_bytes(i3String *str) {
     return str->num_bytes;
 }
 
+/*
+ * Whether the given i3String is in Pango markup.
+ */
+bool i3string_is_markup(i3String *str) {
+    return str->pango_markup;
+}
+
+/*
+ * Set whether the i3String should use Pango markup.
+ */
+void i3string_set_markup(i3String *str, bool pango_markup) {
+    str->pango_markup = pango_markup;
+}
+
+/*
+ * Escape pango markup characters in the given string.
+ */
+i3String *i3string_escape_markup(i3String *str) {
+    const char *text = i3string_as_utf8(str);
+    char *escaped = g_markup_escape_text(text, -1);
+    i3String *result = i3string_from_utf8(escaped);
+    free(escaped);
+    return result;
+}
+
 /*
  * Returns the number of glyphs in an i3String.
  *