Exemple #1
0
/****************************************************************************
  Return the size of the given text in the given font.  This size should
  include the ascent and descent of the text.  Either of width or height
  may be NULL in which case those values simply shouldn't be filled out.
****************************************************************************/
void get_text_size(int *width, int *height,
		   enum client_font font, const char *text)
{
	int w, h;
	measure_string(&w, &h, text);

  if (width) {
    *width = w;
  }
  if (height) {
    *height = h;
  }
}
Exemple #2
0
size_t json_measure_ex(json_value *value, json_serialize_opts opts) {
	size_t total = 1; /* null terminator */
	size_t newlines = 0;
	size_t depth = 0;
	size_t indents = 0;
	int flags;
	int bracket_size, comma_size, colon_size;

	flags = get_serialize_flags(opts);

	/* to reduce branching
	 */
	bracket_size = flags & f_spaces_around_brackets ? 2 : 1;
	comma_size = flags & f_spaces_after_commas ? 2 : 1;
	colon_size = flags & f_spaces_after_colons ? 2 : 1;

	while (value) {
		json_int_t integer;
		json_object_entry *entry;

		switch (value->type) {
			case json_array:

				if (((json_builder_value *)value)->length_iterated == 0) {
					if (value->u.array.length == 0) {
						total += 2; /* `[]` */
						break;
					}

					total += bracket_size; /* `[` */

					++depth;
					MEASURE_NEWLINE(); /* \n after [ */
				}

				if (((json_builder_value *)value)->length_iterated == value->u.array.length) {
					--depth;
					MEASURE_NEWLINE();
					total += bracket_size; /* `]` */

					((json_builder_value *)value)->length_iterated = 0;
					break;
				}

				if (((json_builder_value *)value)->length_iterated > 0) {
					total += comma_size; /* `, ` */

					MEASURE_NEWLINE();
				}

				((json_builder_value *)value)->length_iterated++;
				value = value->u.array.values[((json_builder_value *)value)->length_iterated - 1];
				continue;

			case json_object:

				if (((json_builder_value *)value)->length_iterated == 0) {
					if (value->u.object.length == 0) {
						total += 2; /* `{}` */
						break;
					}

					total += bracket_size; /* `{` */

					++depth;
					MEASURE_NEWLINE(); /* \n after { */
				}

				if (((json_builder_value *)value)->length_iterated == value->u.object.length) {
					--depth;
					MEASURE_NEWLINE();
					total += bracket_size; /* `}` */

					((json_builder_value *)value)->length_iterated = 0;
					break;
				}

				if (((json_builder_value *)value)->length_iterated > 0) {
					total += comma_size; /* `, ` */
					MEASURE_NEWLINE();
				}

				entry = value->u.object.values + (((json_builder_value *)value)->length_iterated++);

				total += 2 + colon_size; /* `"": ` */
				total += measure_string(entry->name_length, entry->name);

				value = entry->value;
				continue;
#ifdef CARYLL_USE_PRE_SERIALIZED
			case json_pre_serialized:

				total += value->u.string.length;
				break;
#endif
			case json_string:
				total += 2; /* `""` */
				total += measure_string(value->u.string.length, value->u.string.ptr);
				break;

			case json_integer:

				integer = value->u.integer;

				if (integer < 0) {
					total += 1; /* `-` */
					integer = -integer;
				}

				++total; /* first digit */

				while (integer >= 10) {
					++total; /* another digit */
					integer /= 10;
				}

				break;

			case json_double: {
				char buffer[256];
				emyg_dtoa(value->u.dbl, buffer);
				total += strlen(buffer);
				break;
			}

			case json_boolean:

				total += value->u.boolean ? 4 : /* `true` */
				             5;                 /* `false` */

				break;

			case json_null:

				total += 4; /* `null` */
				break;

			default:
				break;
		};

		value = value->parent;
	}

	if (opts.mode == json_serialize_mode_multiline) {
		total += newlines * (((opts.opts & json_serialize_opt_CRLF) ? 2 : 1) + opts.indent_size);
		total += indents * opts.indent_size;
	}

	return total;
}