/** * 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; }
static PyObject * pyrufl_x_to_offset(PyObject *self /* Not used */, PyObject *args) { const char *font_family; rufl_style font_style; unsigned int font_size; const char *string; int length; int click_x; size_t char_offset = 0; int actual_x = 0; if (!PyArg_ParseTuple(args, "siIs#i", &font_family, &font_style, &font_size, &string, &length, &click_x)) return NULL; rufl_x_to_offset(font_family, font_style, font_size, string, length, click_x, &char_offset, &actual_x); return Py_BuildValue("ii", (int) char_offset, actual_x); }
int main(void) { char utf8_test[] = "Hello, world! ὕαλον " "Uherské Hradiště. 𐀀"; int width; size_t char_offset; int x; int actual_x; struct rufl_decomp_funcs funcs = { move_to, line_to, cubic_to }; int bbox[4]; try(rufl_init(), "rufl_init"); rufl_dump_state(); try(rufl_paint("NewHall", rufl_WEIGHT_400, 240, utf8_test, sizeof utf8_test - 1, 1200, 1000, 0), "rufl_paint"); try(rufl_width("NewHall", rufl_WEIGHT_400, 240, utf8_test, sizeof utf8_test - 1, &width), "rufl_width"); printf("width: %i\n", width); for (x = 0; x < width + 100; x += 100) { try(rufl_x_to_offset("NewHall", rufl_WEIGHT_400, 240, utf8_test, sizeof utf8_test - 1, x, &char_offset, &actual_x), "rufl_x_to_offset"); printf("x to offset: %i -> %i %zi \"%s\"\n", x, actual_x, char_offset, utf8_test + char_offset); try(rufl_split("NewHall", rufl_WEIGHT_400, 240, utf8_test, sizeof utf8_test - 1, x, &char_offset, &actual_x), "rufl_split"); printf("split: %i -> %i %zi \"%s\"\n", x, actual_x, char_offset, utf8_test + char_offset); } try(rufl_decompose_glyph("Homerton", rufl_WEIGHT_400, 1280, "A", 1, &funcs, 0), "rufl_decompose_glyph"); try(rufl_paint_callback("NewHall", rufl_WEIGHT_400, 240, utf8_test, sizeof utf8_test - 1, 1200, 1000, callback, 0), "rufl_paint_callback"); try(rufl_font_bbox("NewHall", rufl_WEIGHT_400, 240, bbox), "rufl_font_bbox"); printf("bbox: %i %i %i %i\n", bbox[0], bbox[1], bbox[2], bbox[3]); rufl_quit(); return 0; } void try(rufl_code code, const char *context) { if (code == rufl_OK) return; else if (code == rufl_OUT_OF_MEMORY) printf("error: %s: out of memory\n", context); else if (code == rufl_FONT_MANAGER_ERROR) printf("error: %s: Font Manager error %x %s\n", context, rufl_fm_error->errnum, rufl_fm_error->errmess); else if (code == rufl_FONT_NOT_FOUND) printf("error: %s: font not found\n", context); else if (code == rufl_IO_ERROR) printf("error: %s: io error: %i %s\n", context, errno, strerror(errno)); else if (code == rufl_IO_EOF) printf("error: %s: eof\n", context); else printf("error: %s: unknown error\n", context); rufl_quit(); exit(1); } int move_to(os_coord *to, void *user) { (void) user; printf("Move to (%d,%d)\n", to->x, to->y); return 0; } int line_to(os_coord *to, void *user) { (void) user; printf("Line to (%d,%d)\n", to->x, to->y); return 0; } int cubic_to(os_coord *control1, os_coord *control2, os_coord *to, void *user) { (void) user; printf("Bezier to (%d,%d),(%d,%d),(%d,%d)\n", control1->x, control1->y, control2->x, control2->y, to->x, to->y); return 0; } void callback(void *context, const char *font_name, unsigned int font_size, const char *s8, unsigned short *s16, unsigned int n, int x, int y) { (void) context; printf("callback: \"%s\", %u, ", font_name, font_size); if (s8) printf("s8 \"%.*s\" ", n, s8); else { printf("s16 \""); for (unsigned int i = 0; i != n; i++) printf("%x ", (unsigned int) s16[i]); printf("\" "); } printf("%i %i\n", x, y); }
/** * Set the caret's position * * \param self Text area * \param x X position of caret on the screen * \param y Y position of caret on the screen */ void ro_textarea_set_caret_xy(uintptr_t self, int x, int y) { struct text_area *ta; wimp_window_state state; size_t b_off, c_off, temp; int line; os_coord os_line_height; rufl_code code; os_error *error; ta = (struct text_area *)self; if (!ta || ta->magic != MAGIC) { LOG(("magic doesn't match")); return; } if (ta->flags & TEXTAREA_READONLY) return; os_line_height.x = 0; os_line_height.y = (int)((float)(ta->line_height - ta->line_spacing) * 0.62) + 1; ro_convert_pixels_to_os_units(&os_line_height, (os_mode)-1); state.w = ta->window; error = xwimp_get_window_state(&state); if (error) { LOG(("xwimp_get_window_state: 0x%x: %s", error->errnum, error->errmess)); return; } x = x - (state.visible.x0 - state.xscroll) - MARGIN_LEFT; y = (state.visible.y1 - state.yscroll) - y; line = y / ta->line_height; if (line < 0) line = 0; if (ta->line_count - 1 < (unsigned)line) line = ta->line_count - 1; code = rufl_x_to_offset(ta->font_family, ta->font_style, ta->font_size, ta->text + ta->lines[line].b_start, ta->lines[line].b_length, x, &b_off, &x); if (code != rufl_OK) { if (code == rufl_FONT_MANAGER_ERROR) LOG(("rufl_x_to_offset: 0x%x: %s", rufl_fm_error->errnum, rufl_fm_error->errmess)); else LOG(("rufl_x_to_offset: 0x%x", code)); return; } for (temp = 0, c_off = 0; temp < b_off + ta->lines[line].b_start; temp = utf8_next(ta->text, ta->text_len, temp)) c_off++; ro_textarea_set_caret((uintptr_t)ta, c_off); }