/** * Paint a string. * * \param fstyle plot style for this text * \param string UTF-8 string to measure * \param length length of string * \param x x coordinate * \param y y coordinate * \return true on success, false on error and error reported */ bool nsfont_paint(const plot_font_style_t *fstyle, const char *string, size_t length, int x, int y) { const char *font_family; unsigned int font_size; unsigned int flags = rufl_BLEND_FONT; rufl_style font_style; rufl_code code; nsfont_read_style(fstyle, &font_family, &font_size, &font_style); if (font_size == 0) return true; if (no_font_blending || print_active) flags = 0; code = rufl_paint(font_family, font_style, font_size, string, length, x, y, flags); if (code != rufl_OK) { if (code == rufl_FONT_MANAGER_ERROR) { LOG("rufl_paint: rufl_FONT_MANAGER_ERROR: 0x%x: %s", rufl_fm_error->errnum, rufl_fm_error->errmess); } else { LOG("rufl_paint: 0x%x", code); } } return true; }
/** * Find the position in a string where an x coordinate falls. * * \param fstyle style for this text * \param string UTF-8 string to measure * \param length length of string * \param x x coordinate to search for * \param char_offset updated to offset in string of actual_x, [0..length] * \param actual_x updated to x coordinate of character closest to x * \return true on success, false on error and error reported */ static nserror ro_font_position(const plot_font_style_t *fstyle, const char *string, size_t length, int x, size_t *char_offset, int *actual_x) { const char *font_family; unsigned int font_size; rufl_style font_style; rufl_code code; nsfont_read_style(fstyle, &font_family, &font_size, &font_style); if (font_size == 0) { *char_offset = 0; *actual_x = 0; return NSERROR_OK; } code = rufl_x_to_offset(font_family, font_style, font_size, string, length, x * 2, char_offset, actual_x); if (code != rufl_OK) { if (code == rufl_FONT_MANAGER_ERROR) LOG("rufl_x_to_offset: rufl_FONT_MANAGER_ERROR: ""0x%x: %s", rufl_fm_error->errnum, rufl_fm_error->errmess); else LOG("rufl_x_to_offset: 0x%x", code); /* ro_warn_user("MiscError", "font error"); */ *char_offset = 0; *actual_x = 0; return NSERROR_INVALID; } *actual_x /= 2; return NSERROR_OK; }
/** * Measure the width of a string. * * \param fstyle plot style for this text * \param string UTF-8 string to measure * \param length length of string * \param width updated to width of string[0..length) * \return true on success, false on error and error reported */ static nserror ro_font_width(const plot_font_style_t *fstyle, const char *string, size_t length, int *width) { const char *font_family; unsigned int font_size; rufl_style font_style; rufl_code code; nsfont_read_style(fstyle, &font_family, &font_size, &font_style); if (font_size == 0) { *width = 0; return NSERROR_OK; } code = rufl_width(font_family, font_style, font_size, string, length, width); if (code != rufl_OK) { if (code == rufl_FONT_MANAGER_ERROR) LOG("rufl_width: rufl_FONT_MANAGER_ERROR: 0x%x: %s", rufl_fm_error->errnum, rufl_fm_error->errmess); else LOG("rufl_width: 0x%x", code); /* ro_warn_user("MiscError", "font error"); */ *width = 0; return NSERROR_INVALID; } *width /= 2; return NSERROR_OK; }
bool nsfont_split(const plot_font_style_t *fstyle, const char *string, size_t length, int x, size_t *char_offset, int *actual_x) { const char *font_family; unsigned int font_size; rufl_style font_style; rufl_code code; nsfont_read_style(fstyle, &font_family, &font_size, &font_style); if (font_size == 0) { *char_offset = 0; *actual_x = 0; return true; } code = rufl_split(font_family, font_style, font_size, string, length, x * 2, char_offset, actual_x); if (code != rufl_OK) { if (code == rufl_FONT_MANAGER_ERROR) LOG(("rufl_split: rufl_FONT_MANAGER_ERROR: " "0x%x: %s", rufl_fm_error->errnum, rufl_fm_error->errmess)); else LOG(("rufl_split: 0x%x", code)); /* warn_user("MiscError", "font error"); */ return false; } while (*char_offset && string[*char_offset] != ' ') (*char_offset)--; code = rufl_width(font_family, font_style, font_size, string, *char_offset, actual_x); if (code != rufl_OK) { if (code == rufl_FONT_MANAGER_ERROR) LOG(("rufl_width: rufl_FONT_MANAGER_ERROR: 0x%x: %s", rufl_fm_error->errnum, rufl_fm_error->errmess)); else LOG(("rufl_width: 0x%x", code)); /* warn_user("MiscError", "font error"); */ return false; } *actual_x /= 2; return true; }
bool ro_save_draw_text(int x, int y, const char *text, size_t length, const plot_font_style_t *fstyle) { pencil_code code; const char *font_family; unsigned int font_size; rufl_style font_style; nsfont_read_style(fstyle, &font_family, &font_size, &font_style); code = pencil_text(ro_save_draw_diagram, x * 2, -y * 2, font_family, font_style, font_size, text, length, fstyle->foreground << 8); if (code != pencil_OK) return ro_save_draw_error(code); return true; }
/** * Find where to split a string to make it fit a width. * * \param fstyle style for this text * \param string UTF-8 string to measure * \param length length of string, in bytes * \param x width available * \param char_offset updated to offset in string of actual_x, [1..length] * \param actual_x updated to x coordinate of character closest to x * \return true on success, false on error and error reported * * On exit, char_offset indicates first character after split point. * * Note: char_offset of 0 should never be returned. * * Returns: * char_offset giving split point closest to x, where actual_x <= x * else * char_offset giving split point closest to x, where actual_x > x * * Returning char_offset == length means no split possible */ static nserror ro_font_split(const plot_font_style_t *fstyle, const char *string, size_t length, int x, size_t *char_offset, int *actual_x) { const char *font_family; unsigned int font_size; rufl_style font_style; rufl_code code; nsfont_read_style(fstyle, &font_family, &font_size, &font_style); if (font_size == 0) { *char_offset = 0; *actual_x = 0; return NSERROR_OK; } code = rufl_split(font_family, font_style, font_size, string, length, x * 2, char_offset, actual_x); if (code != rufl_OK) { if (code == rufl_FONT_MANAGER_ERROR) { LOG("rufl_split: rufl_FONT_MANAGER_ERROR: ""0x%x: %s", rufl_fm_error->errnum, rufl_fm_error->errmess); } else { LOG("rufl_split: 0x%x", code); } /* ro_warn_user("MiscError", "font error"); */ *char_offset = 0; *actual_x = 0; return NSERROR_INVALID; } if (*char_offset != length) { /* we found something to split at */ size_t orig = *char_offset; /* ensure a space at <= the split point we found */ while (*char_offset && string[*char_offset] != ' ') { (*char_offset)--; } /* nothing valid found <= split point, advance to next space */ if (*char_offset == 0) { *char_offset = orig; while ((*char_offset != length) && (string[*char_offset] != ' ')) { (*char_offset)++; } } } code = rufl_width(font_family, font_style, font_size, string, *char_offset, actual_x); if (code != rufl_OK) { if (code == rufl_FONT_MANAGER_ERROR) { LOG("rufl_width: rufl_FONT_MANAGER_ERROR: 0x%x: %s", rufl_fm_error->errnum, rufl_fm_error->errmess); } else { LOG("rufl_width: 0x%x", code); } /* ro_warn_user("MiscError", "font error"); */ *char_offset = 0; *actual_x = 0; return NSERROR_INVALID; } *actual_x /= 2; return NSERROR_OK; }