Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

text-buffer: apply affine transform to last line #71

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions text-buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,31 @@ text_buffer_move_last_line( text_buffer_t * self, float dy )
}
}

// ----------------------------------------------------------------------------
void
text_buffer_transform_last_line( text_buffer_t * self, const float* m )
{
size_t i, j;
float x, y, z;
for( i=self->line_start; i < vector_size( self->buffer->items ); ++i )
{
ivec4 *item = (ivec4 *) vector_get( self->buffer->items, i);
for( j=item->vstart; j<item->vstart+item->vcount; ++j)
{
glyph_vertex_t* v =
(glyph_vertex_t *) vector_get( self->buffer->vertices, j );

// affine matrix multiplication "v = m*v".
// m is a 4x4 matrix (column-major) and fourth element of v is 1 in an affine transform.
x = m[0]*v->x + m[4]*v->y + m[8]*v->z + m[12];
y = m[1]*v->x + m[5]*v->y + m[9]*v->z + m[13];
z = m[2]*v->x + m[6]*v->y + m[10]*v->z + m[14];
v->x = x;
v->y = y;
v->z = z;
}
}
}

// ----------------------------------------------------------------------------
void
Expand Down Expand Up @@ -221,6 +246,8 @@ text_buffer_add_text( text_buffer_t * self,
self->origin = *pen;
}

self->line_start = vector_size( self->buffer->items );

text_buffer_add_wchar( self, pen, markup, text[0], 0 );
for( i=1; i<length; ++i )
{
Expand Down
10 changes: 10 additions & 0 deletions text-buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,16 @@ text_buffer_delete( text_buffer_t * self );
vec2 * pen, markup_t * markup,
wchar_t current, wchar_t previous );

/**
* Applies an affine transform to all characters added since the last newline character '\n' or the beginning of the last text_buffer_add_text.
* This can be used to move, rescale and/or rotate the last line.
*
* @param self a text buffer
* @param matrix an array of 16 floats that defines a column major 4x4 affine transformation matrix.
*/
void
text_buffer_transform_last_line( text_buffer_t * self, const float* matrix );

/**
* Clear text buffer
*
Expand Down