Esempio n. 1
0
FCITX_EXPORT_API
bool fcitx_color_parse(FcitxColor* color, const char* str)
{
    size_t idx = 0;

    // skip space
    while (str[idx] && fcitx_utils_isspace(str[idx])) {
        idx ++;
    }

    if (str[idx] == '#') {
        // count the digit length
        size_t len = 0;
        const char* digits = &str[idx + 1];
        while(digits[len] &&
            (fcitx_utils_isdigit(digits[len]) ||
             ('A' <= digits[len] && digits[len] <= 'F') |
             ('a' <= digits[len] && digits[len] <= 'f'))) {
            len++;
        }
        if (len != 8) {
            return false;
        }

        int r, g, b, a;
        r = to_hex_digit(digits[0], digits[1]);
        digits += 2;
        g = to_hex_digit(digits[0], digits[1]);
        digits += 2;
        b = to_hex_digit(digits[0], digits[1]);
        digits += 2;
        a = to_hex_digit(digits[0], digits[1]);

        if (IsColorValid(r) && IsColorValid(g) && IsColorValid(b) && IsColorValid(a)) {
            color->red = r / 255.0;
            color->green = g / 255.0;
            color->blue = b / 255.0;
            color->alpha = a / 255.0;
            return true;
        }
    } else {
        int r, g, b;
        if (sscanf(str, "%d %d %d", &r, &g, &b) != 3) {
            return false;
        }

        if (IsColorValid(r) && IsColorValid(g) && IsColorValid(b)) {
            color->red = r / 255.0;
            color->green = g / 255.0;
            color->blue = b / 255.0;
            color->alpha = 1.0;
            return true;
        }
    }

    return false;
}
Esempio n. 2
0
static void to_hex(char *dest, const uint8_t *src, size_t len)
{
  size_t i;
  for(i = 0; i < len; ++i) {
    *dest++ = to_hex_digit(src[i] >> 4);
    *dest++ = to_hex_digit(src[i] & 0xf);
  }
}
/** Return a printable text string.
 *
 * Takes a string, and any ASCII characters that are not printable are
 * replaced by the corresponding escape sequence in the returned
 * string.
 *
 * @param outstr Output string pointer
 * @param in Input string pointer
 * @param inlen Maximum length of string to read not including terminating 0.
 * @param outlen Maximum length of string to return not including terminating 0.
 * 0 means MAXSTRINGLEN.
 * @param quoted Put double quotes around the string. If input string is truncated
 * in will have "..." after the second quote.
 * @param user Set this to indicate the input string pointer is a userspace pointer.
 */
static int _stp_text_str(char *outstr, const char *in, int inlen, int outlen,
			 int quoted, int user, int buffer)
{
	int c = 0;
	char *out = outstr;

	if (inlen <= 0 || inlen > MAXSTRINGLEN-1)
		inlen = MAXSTRINGLEN-1;
	if (outlen <= 0 || outlen > MAXSTRINGLEN-1)
		outlen = MAXSTRINGLEN-1;
	if (quoted) {
		outlen = max(outlen, 5) - 2;
		*out++ = '"';
	}

	while (inlen > 0) {
		int num = 1;

		int n = _stp_decode_utf8(in, inlen, user, &c);
		if (n <= 0)
			goto bad;
		if ((c == 0 && !buffer) || outlen <= 0)
			break;
		in += n;
		inlen -= n;

		if (n > 1) {
			/* UTF-8, print \uXXXX or \UXXXXXXXX */
			int i;
			num = (c <= 0xFFFF) ? 6 : 10;
			if (outlen < num)
				break;

			*out++ = '\\';
			*out++ = (c <= 0xFFFF) ? 'u' : 'U';
			for (i = num - 3; i >= 0; --i) {
				char nibble = (c >> (i * 4)) & 0xF;
				*out++ = to_hex_digit(nibble);
			}

		}
		else if (isascii(c) && isprint(c)
Esempio n. 4
0
static gunichar
json_scanner_get_unichar (JsonScanner *scanner,
                          guint       *line_p,
                          guint       *position_p)
{
  gunichar uchar;
  gchar ch;
  gint i;

  uchar = 0;
  for (i = 0; i < 4; i++)
    {
      ch = json_scanner_get_char (scanner, line_p, position_p);

      if (is_hex_digit (ch))
        uchar += ((gunichar) to_hex_digit (ch) << ((3 - i) * 4));
      else
        break;
    }

  g_assert (g_unichar_validate (uchar) || g_unichar_type (uchar) == G_UNICODE_SURROGATE);

  return uchar;
}
Esempio n. 5
0
static gboolean
on_captured_event (ClutterText *text,
                   ClutterEvent *event,
                   gpointer      dummy G_GNUC_UNUSED)
{
  gboolean is_unicode_mode = FALSE;
  gunichar c;
  guint keyval;

  if (event->type != CLUTTER_KEY_PRESS)
    return FALSE;

  is_unicode_mode = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (text),
                                                        "unicode-mode"));

  c = clutter_event_get_key_unicode (event);
  keyval = clutter_event_get_key_symbol (event);
  if (keyval == CLUTTER_KEY_U)
    {
      if (is_unicode_mode)
        {
          GString *str = g_object_get_data (G_OBJECT (text), "unicode-str");

          clutter_text_set_preedit_string (text, NULL, NULL, 0);

          g_object_set_data (G_OBJECT (text), "unicode-mode",
                             GINT_TO_POINTER (FALSE));
          g_object_set_data (G_OBJECT (text), "unicode-str",
                             NULL);

          g_string_free (str, TRUE);

          return FALSE;
        }

      if (clutter_event_has_control_modifier (event))
        {
          PangoAttrList *attrs;
          PangoAttribute *a;
          GString *str = g_string_sized_new (5);

          g_string_append (str, "u");

          g_object_set_data (G_OBJECT (text),
                             "unicode-mode",
                             GINT_TO_POINTER (TRUE));
          g_object_set_data (G_OBJECT (text),
                             "unicode-str",
                             str);

          attrs = pango_attr_list_new ();

          a = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
          a->start_index = 0;
          a->end_index = str->len;
          pango_attr_list_insert (attrs, a);

          clutter_text_set_preedit_string (text, str->str, attrs, str->len);

          pango_attr_list_unref (attrs);

          return TRUE;
        }

      return FALSE;
    }
  else if (is_unicode_mode && is_hex_digit (c))
    {
      GString *str = g_object_get_data (G_OBJECT (text), "unicode-str");
      PangoAttrList *attrs;
      PangoAttribute *a;
      gchar buf[8];
      gsize len;

      len = g_unichar_to_utf8 (c, buf);
      buf[len] = '\0';

      g_string_append (str, buf);

      g_print ("added '%s' to '%s' (len:%d)\n",
               buf,
               str->str,
               (int) str->len);

      attrs = pango_attr_list_new ();

      a = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
      a->start_index = 0;
      a->end_index = str->len;
      pango_attr_list_insert (attrs, a);

      clutter_text_set_preedit_string (text, str->str, attrs, str->len);

      pango_attr_list_unref (attrs);

      return TRUE;
    }
  else if (is_unicode_mode && (keyval == CLUTTER_KEY_BackSpace))
    {
      GString *str = g_object_get_data (G_OBJECT (text), "unicode-str");
      PangoAttrList *attrs;
      PangoAttribute *a;

      g_string_truncate (str, str->len - 1);

      attrs = pango_attr_list_new ();

      a = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
      a->start_index = 0;
      a->end_index = str->len;
      pango_attr_list_insert (attrs, a);

      clutter_text_set_preedit_string (text, str->str, attrs, str->len);

      pango_attr_list_unref (attrs);

      return TRUE;
    }
  else if (is_unicode_mode &&
           (keyval == CLUTTER_KEY_Return ||
            keyval == CLUTTER_KEY_KP_Enter ||
            keyval == CLUTTER_KEY_ISO_Enter ||
            keyval == CLUTTER_KEY_KP_Space))
    {
      GString *str = g_object_get_data (G_OBJECT (text), "unicode-str");
      const gchar *contents = clutter_text_get_text (text);
      gunichar uchar = 0;
      gchar ch;
      gint i;

      clutter_text_set_preedit_string (text, NULL, NULL, 0);

      g_object_set_data (G_OBJECT (text), "unicode-mode",
                         GINT_TO_POINTER (FALSE));
      g_object_set_data (G_OBJECT (text), "unicode-str",
                         NULL);

      for (i = 0; i < str->len; i++)
        {
          ch = str->str[i];

          if (is_hex_digit (ch))
            uchar += ((gunichar) to_hex_digit (ch) << ((4 - i) * 4));
        }

      g_assert (g_unichar_validate (uchar));

      g_string_overwrite (str, 0, contents);
      g_string_insert_unichar (str,
                               clutter_text_get_cursor_position (text),
                               uchar);

      i = clutter_text_get_cursor_position (text);
      clutter_text_set_text (text, str->str);

      if (i >= 0)
        i += 1;
      else
        i = -1;

      clutter_text_set_cursor_position (text, i);
      clutter_text_set_selection_bound (text, i);

      g_string_free (str, TRUE);

      return TRUE;
    }
  else
    return FALSE;
}