示例#1
0
/**
 * Measure the width of a string.
 *
 * \param  fstyle  style for this text
 * \param  string  string to measure (no UTF-8 currently)
 * \param  length  length of string
 * \param  width   updated to width of string[0..length]
 * \return  true on success, false on error and error reported
 */
bool haru_nsfont_width(const plot_font_style_t *fstyle,
		const char *string, size_t length,
	 	int *width)
{
	HPDF_Doc pdf;
	HPDF_Page page;
	char *string_nt;
	HPDF_REAL width_real;

	*width = 0;

	if (length == 0)
		return true;

	if (!haru_nsfont_init(&pdf, &page, string, &string_nt, length))
		return false;

	if (!haru_nsfont_apply_style(fstyle, pdf, page, NULL, NULL)) {
		free(string_nt);
		HPDF_Free(pdf);
		return false;
	}

	width_real = HPDF_Page_TextWidth(page, string_nt);
	*width = width_real;

#ifdef FONT_HARU_DEBUG
	LOG("Measuring string: %s ; Calculated width: %f %i", string_nt, width_real, *width);
#endif
	free(string_nt);
	HPDF_Free(pdf);

	return true;
}
示例#2
0
bool haru_nsfont_position_in_string(const plot_font_style_t *fstyle,
		const char *string, size_t length,
		int x, size_t *char_offset, int *actual_x)
{
	HPDF_Doc pdf;
	HPDF_Page page;
	char *string_nt;
	HPDF_UINT offset;
	HPDF_REAL real_width;

	if (!haru_nsfont_init(&pdf, &page, string, &string_nt, length))
		return false;

	if (HPDF_Page_SetWidth(page, x) != HPDF_OK
			|| !haru_nsfont_apply_style(fstyle, pdf, page, NULL, NULL)) {
		free(string_nt);
		HPDF_Free(pdf);
		return false;
	}


	offset = HPDF_Page_MeasureText(page, string_nt, x,
			HPDF_FALSE, &real_width);


	if (real_width < x)
		*char_offset = offset;
	else {
		assert(fabs(real_width - x) < FLT_EPSILON);
		assert(offset > 0);
		*char_offset = offset - 1;
	}

	/*TODO: this is only the right edge of the character*/
	*actual_x = real_width;

#ifdef FONT_HARU_DEBUG
	LOG("Position in string: %s at x: %i; Calculated position: %i", string_nt, x, *char_offset);
#endif
	free(string_nt);
	HPDF_Free(pdf);

	return true;
}
示例#3
0
bool haru_nsfont_split(const plot_font_style_t *fstyle,
		const char *string, size_t length,
		int x, size_t *char_offset, int *actual_x)
{
	HPDF_Doc pdf;
	HPDF_Page page;
	char *string_nt;
	HPDF_REAL real_width;
	HPDF_UINT offset;


	if (!haru_nsfont_init(&pdf, &page, string, &string_nt, length))
		return false;

	if (HPDF_Page_SetWidth(page, x) != HPDF_OK
		    || !haru_nsfont_apply_style(fstyle, pdf, page, NULL, NULL)) {
		free(string_nt);
		HPDF_Free(pdf);
		return false;
	}

	offset = HPDF_Page_MeasureText(page, string_nt, x,
			HPDF_TRUE, &real_width);

#ifdef FONT_HARU_DEBUG
	LOG("Splitting string: %s for width: %i ; Calculated position: %i Calculated real_width: %f", string_nt, x, *char_offset, real_width);
#endif
	*char_offset = offset - 1;

	/*TODO: this is only the right edge of the character*/
	*actual_x = real_width;

	free(string_nt);
	HPDF_Free(pdf);

	return true;
}