text: expose method to extend existing path with new text

This commit is contained in:
K. Lange 2023-05-06 15:56:42 +09:00
parent 86385a8925
commit 6dc7c09c82
3 changed files with 19 additions and 6 deletions

View File

@ -49,6 +49,7 @@ extern void tt_contour_transform(struct TT_Contour * cnt, gfx_matrix_t matrix);
/* Internal methods to draw paths into vector contours */
extern struct TT_Contour * tt_draw_glyph_into(struct TT_Contour * contour, struct TT_Font * font, float x_offset, float y_offset, unsigned int glyph);
extern struct TT_Contour * tt_prepare_string(struct TT_Font * font, float x, float y, const char * s, float * out_width);
extern struct TT_Contour * tt_prepare_string_into(struct TT_Contour * contour, struct TT_Font * font, float x, float y, const char * s, float * out_width);
/* Draw with texture from sprite */
extern void tt_path_paint_sprite(gfx_context_t * ctx, const struct TT_Shape * shape, sprite_t * sprite, gfx_matrix_t matrix);

View File

@ -1105,18 +1105,23 @@ KRK_Method(Font,draw_glyph_into) {
KRK_Method(Font,prepare_string) {
INIT_CHECK(Font);
struct _yutani_TTContour * contour = NULL;
float x, y;
const char * s;
if (!krk_parseArgs(".ffs", (const char*[]){"x","y","s"},
&x, &y, &s)) return NONE_VAL();
if (!krk_parseArgs(".ffs|O!", (const char*[]){"x","y","s","into"},
&x, &y, &s, TTContour, &contour)) return NONE_VAL();
float out_width = 0;
KrkTuple * out_tuple = krk_newTuple(2); /* contour, width */
krk_push(OBJECT_VAL(out_tuple));
struct _yutani_TTContour * contour = (struct _yutani_TTContour*)krk_newInstance(TTContour);
contour->contour = tt_prepare_string(self->fontData, x, y, s, &out_width);
/* if @c into is unset, make a new one to store result; otherwise, @c into is updated */
if (!contour) {
contour = (struct _yutani_TTContour*)krk_newInstance(TTContour);
}
contour->contour = tt_prepare_string_into(contour->contour, self->fontData, x, y, s, &out_width);
out_tuple->values.values[out_tuple->values.count++] = OBJECT_VAL(contour);
out_tuple->values.values[out_tuple->values.count++] = FLOATING_VAL(out_width);

View File

@ -812,8 +812,10 @@ float tt_glyph_width(struct TT_Font * font, unsigned int glyph) {
}
__attribute__((visibility("protected")))
struct TT_Contour * tt_prepare_string(struct TT_Font * font, float x, float y, const char * s, float * out_width) {
struct TT_Contour * contour = tt_contour_start(0, 0);
struct TT_Contour * tt_prepare_string_into(struct TT_Contour * contour, struct TT_Font * font, float x, float y, const char * s, float * out_width) {
if (contour == NULL) {
contour = tt_contour_start(0, 0);
}
float x_offset = x;
uint32_t cp = 0;
@ -832,6 +834,11 @@ struct TT_Contour * tt_prepare_string(struct TT_Font * font, float x, float y, c
return contour;
}
__attribute__((visibility("protected")))
struct TT_Contour * tt_prepare_string(struct TT_Font * font, float x, float y, const char * s, float * out_width) {
return tt_prepare_string_into(NULL, font, x, y, s, out_width);
}
int tt_draw_string(gfx_context_t * ctx, struct TT_Font * font, int x, int y, const char * s, uint32_t color) {
float width;
struct TT_Contour * contour = tt_prepare_string(font,x,y,s,&width);