bool pdf_plot_text(int x, int y, const char *text, size_t length, const plot_font_style_t *fstyle) { #ifdef PDF_DEBUG NSLOG(netsurf, INFO, ". %d %d %.*s", x, y, (int)length, text); #endif char *word; HPDF_Font pdf_font; HPDF_REAL size; if (length == 0) return true; apply_clip_and_mode(true, fstyle->foreground, NS_TRANSPARENT, 0., DashPattern_eNone); haru_nsfont_apply_style(fstyle, pdf_doc, pdf_page, &pdf_font, &size); pdfw_gs_font(pdf_page, pdf_font, size); /* FIXME: UTF-8 to current font encoding needs to done. Or the font * encoding needs to be UTF-8 or other Unicode encoding. */ word = (char *)malloc( sizeof(char) * (length+1) ); if (word == NULL) return false; memcpy(word, text, length); word[length] = '\0'; HPDF_Page_TextOut (pdf_page, x, page_height - y, word); free(word); return true; }
/** * 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; }
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; }
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; }