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; }
static PyObject * pyrufl_split(PyObject *self /* Not used */, PyObject *args) { const char *font_family; rufl_style font_style; unsigned int font_size; const char *string; int length; int width; size_t char_offset = 0; int actual_x = 0; if (!PyArg_ParseTuple(args, "siIs#i", &font_family, &font_style, &font_size, &string, &length, &width)) return NULL; rufl_split(font_family, font_style, font_size, string, length, width, &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); }
/** * 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; }
/** * Reflow a text area from the given line onwards * * \param ta Text area to reflow * \param line Line number to begin reflow on */ void ro_textarea_reflow(struct text_area *ta, unsigned int line) { rufl_code code; char *text; unsigned int len; size_t b_off; int x; char *space; unsigned int line_count = 0; os_box extent; os_error *error; /** \todo pay attention to line parameter */ /** \todo create horizontal scrollbar if needed */ ta->line_count = 0; if (!ta->lines) { ta->lines = malloc(LINE_CHUNK_SIZE * sizeof(struct line_info)); if (!ta->lines) { LOG(("malloc failed")); return; } } if (!(ta->flags & TEXTAREA_MULTILINE)) { /* Single line */ ta->lines[line_count].b_start = 0; ta->lines[line_count++].b_length = ta->text_len - 1; ta->line_count = line_count; return; } for (len = ta->text_len - 1, text = ta->text; len > 0; len -= b_off, text += b_off) { code = rufl_split(ta->font_family, ta->font_style, ta->font_size, text, len, ta->vis_width - MARGIN_LEFT - MARGIN_RIGHT, &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; } if (line_count > 0 && line_count % LINE_CHUNK_SIZE == 0) { struct line_info *temp = realloc(ta->lines, (line_count + LINE_CHUNK_SIZE) * sizeof(struct line_info)); if (!temp) { LOG(("realloc failed")); return; } ta->lines = temp; } /* handle CR/LF */ for (space = text; space < text + b_off; space++) { if (*space == '\r' || *space == '\n') break; } if (space != text + b_off) { /* Found newline; use it */ ta->lines[line_count].b_start = text - ta->text; ta->lines[line_count++].b_length = space - text; /* CRLF / LFCR pair */ if (*space == '\r' && *(space + 1) == '\n') space++; else if (*space == '\n' && *(space + 1) == '\r') space++; b_off = space + 1 - text; if (len - b_off == 0) { /* reached end of input => add last line */ ta->lines[line_count].b_start = text + b_off - ta->text; ta->lines[line_count++].b_length = 0; } continue; } if (len - b_off > 0) { /* find last space (if any) */ for (space = text + b_off; space > text; space--) if (*space == ' ') break; if (space != text) b_off = space + 1 - text; } ta->lines[line_count].b_start = text - ta->text; ta->lines[line_count++].b_length = b_off; } ta->line_count = line_count; /* and now update extent */ extent.x0 = 0; extent.y1 = 0; extent.x1 = ta->vis_width; extent.y0 = -ta->line_height * line_count - ta->line_spacing; if (extent.y0 > (int)-ta->vis_height) /* haven't filled window yet */ return; error = xwimp_set_extent(ta->window, &extent); if (error) { LOG(("xwimp_set_extent: 0x%x: %s", error->errnum, error->errmess)); return; } /* Create vertical scrollbar if we don't already have one */ if (!ro_gui_wimp_check_window_furniture(ta->window, wimp_WINDOW_VSCROLL)) { wimp_window_state state; wimp_w parent; bits linkage; unsigned int vscroll_width; /* Save window parent & linkage flags */ state.w = ta->window; error = xwimp_get_window_state_and_nesting(&state, &parent, &linkage); if (error) { LOG(("xwimp_get_window_state_and_nesting: 0x%x: %s", error->errnum, error->errmess)); return; } /* Now, attempt to create vertical scrollbar */ ro_gui_wimp_update_window_furniture(ta->window, wimp_WINDOW_VSCROLL, wimp_WINDOW_VSCROLL); /* Get new window state */ 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; } /* Get scroll width */ vscroll_width = ro_get_vscroll_width(NULL); /* Shrink width by difference */ state.visible.x1 -= vscroll_width; /* and reopen window */ error = xwimp_open_window_nested(PTR_WIMP_OPEN(&state), parent, linkage); if (error) { LOG(("xwimp_open_window_nested: 0x%x: %s", error->errnum, error->errmess)); return; } /* finally, update visible width */ ta->vis_width -= vscroll_width; /* Now we've done that, we have to reflow the text area */ ro_textarea_reflow(ta, 0); } }