Skip to content

Commit

Permalink
Merge pull request #84180 from bruvzg/text_mesh_offset
Browse files Browse the repository at this point in the history
[Text Mesh] Fix incorrectly cached glyph offsets.
  • Loading branch information
akien-mga committed Oct 30, 2023
2 parents 563e0eb + 2a06594 commit 2f1f120
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions scene/resources/primitive_meshes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2726,7 +2726,6 @@ void TextMesh::_generate_glyph_mesh_data(const GlyphMeshKey &p_key, const Glyph
GlyphMeshData &gl_data = cache[p_key];

Dictionary d = TS->font_get_glyph_contours(p_gl.font_rid, p_gl.font_size, p_gl.index);
Vector2 origin = Vector2(p_gl.x_off, p_gl.y_off) * pixel_size;

PackedVector3Array points = d["points"];
PackedInt32Array contours = d["contours"];
Expand All @@ -2746,7 +2745,7 @@ void TextMesh::_generate_glyph_mesh_data(const GlyphMeshKey &p_key, const Glyph
for (int32_t j = start; j <= end; j++) {
if (points[j].z == TextServer::CONTOUR_CURVE_TAG_ON) {
// Point on the curve.
Vector2 p = Vector2(points[j].x, points[j].y) * pixel_size + origin;
Vector2 p = Vector2(points[j].x, points[j].y) * pixel_size;
polygon.push_back(ContourPoint(p, true));
} else if (points[j].z == TextServer::CONTOUR_CURVE_TAG_OFF_CONIC) {
// Conic Bezier arc.
Expand Down Expand Up @@ -2780,7 +2779,7 @@ void TextMesh::_generate_glyph_mesh_data(const GlyphMeshKey &p_key, const Glyph
real_t t2 = t * t;

Vector2 point = p1 + omt2 * (p0 - p1) + t2 * (p2 - p1);
Vector2 p = point * pixel_size + origin;
Vector2 p = point * pixel_size;
polygon.push_back(ContourPoint(p, false));
t += step;
}
Expand Down Expand Up @@ -2814,7 +2813,7 @@ void TextMesh::_generate_glyph_mesh_data(const GlyphMeshKey &p_key, const Glyph
real_t t = step;
while (t < 1.0) {
Vector2 point = p0.bezier_interpolate(p1, p2, p3, t);
Vector2 p = point * pixel_size + origin;
Vector2 p = point * pixel_size;
polygon.push_back(ContourPoint(p, false));
t += step;
}
Expand Down Expand Up @@ -3049,6 +3048,7 @@ void TextMesh::_create_mesh_array(Array &p_arr) const {
GlyphMeshKey key = GlyphMeshKey(glyphs[j].font_rid.get_id(), glyphs[j].index);
_generate_glyph_mesh_data(key, glyphs[j]);
GlyphMeshData &gl_data = cache[key];
const Vector2 gl_of = Vector2(glyphs[j].x_off, glyphs[j].y_off) * pixel_size;

p_size += glyphs[j].repeat * gl_data.triangles.size() * ((has_depth) ? 2 : 1);
i_size += glyphs[j].repeat * gl_data.triangles.size() * ((has_depth) ? 2 : 1);
Expand All @@ -3061,10 +3061,10 @@ void TextMesh::_create_mesh_array(Array &p_arr) const {
}

for (int r = 0; r < glyphs[j].repeat; r++) {
min_p.x = MIN(gl_data.min_p.x + offset.x, min_p.x);
min_p.y = MIN(gl_data.min_p.y - offset.y, min_p.y);
max_p.x = MAX(gl_data.max_p.x + offset.x, max_p.x);
max_p.y = MAX(gl_data.max_p.y - offset.y, max_p.y);
min_p.x = MIN(gl_data.min_p.x + offset.x + gl_of.x, min_p.x);
min_p.y = MIN(gl_data.min_p.y - offset.y + gl_of.y, min_p.y);
max_p.x = MAX(gl_data.max_p.x + offset.x + gl_of.x, max_p.x);
max_p.y = MAX(gl_data.max_p.y - offset.y + gl_of.y, max_p.y);

offset.x += glyphs[j].advance * pixel_size;
}
Expand Down Expand Up @@ -3130,12 +3130,13 @@ void TextMesh::_create_mesh_array(Array &p_arr) const {

int64_t ts = gl_data.triangles.size();
const Vector2 *ts_ptr = gl_data.triangles.ptr();
const Vector2 gl_of = Vector2(glyphs[j].x_off, glyphs[j].y_off) * pixel_size;

for (int r = 0; r < glyphs[j].repeat; r++) {
for (int k = 0; k < ts; k += 3) {
// Add front face.
for (int l = 0; l < 3; l++) {
Vector3 point = Vector3(ts_ptr[k + l].x + offset.x, -ts_ptr[k + l].y + offset.y, depth / 2.0);
Vector3 point = Vector3(ts_ptr[k + l].x + offset.x + gl_of.x, -ts_ptr[k + l].y + offset.y - gl_of.y, depth / 2.0);
vertices_ptr[p_idx] = point;
normals_ptr[p_idx] = Vector3(0.0, 0.0, 1.0);
if (has_depth) {
Expand All @@ -3153,7 +3154,7 @@ void TextMesh::_create_mesh_array(Array &p_arr) const {
if (has_depth) {
// Add back face.
for (int l = 2; l >= 0; l--) {
Vector3 point = Vector3(ts_ptr[k + l].x + offset.x, -ts_ptr[k + l].y + offset.y, -depth / 2.0);
Vector3 point = Vector3(ts_ptr[k + l].x + offset.x + gl_of.x, -ts_ptr[k + l].y + offset.y - gl_of.y, -depth / 2.0);
vertices_ptr[p_idx] = point;
normals_ptr[p_idx] = Vector3(0.0, 0.0, -1.0);
uvs_ptr[p_idx] = Vector2(Math::remap(point.x, min_p.x, max_p.x, real_t(0.0), real_t(1.0)), Math::remap(point.y, -max_p.y, -min_p.y, real_t(0.8), real_t(0.4)));
Expand Down Expand Up @@ -3186,10 +3187,10 @@ void TextMesh::_create_mesh_array(Array &p_arr) const {
real_t seg_len = (ps_ptr[next].point - ps_ptr[l].point).length();

Vector3 quad_faces[4] = {
Vector3(ps_ptr[l].point.x + offset.x, -ps_ptr[l].point.y + offset.y, -depth / 2.0),
Vector3(ps_ptr[next].point.x + offset.x, -ps_ptr[next].point.y + offset.y, -depth / 2.0),
Vector3(ps_ptr[l].point.x + offset.x, -ps_ptr[l].point.y + offset.y, depth / 2.0),
Vector3(ps_ptr[next].point.x + offset.x, -ps_ptr[next].point.y + offset.y, depth / 2.0),
Vector3(ps_ptr[l].point.x + offset.x + gl_of.x, -ps_ptr[l].point.y + offset.y - gl_of.y, -depth / 2.0),
Vector3(ps_ptr[next].point.x + offset.x + gl_of.x, -ps_ptr[next].point.y + offset.y - gl_of.y, -depth / 2.0),
Vector3(ps_ptr[l].point.x + offset.x + gl_of.x, -ps_ptr[l].point.y + offset.y - gl_of.y, depth / 2.0),
Vector3(ps_ptr[next].point.x + offset.x + gl_of.x, -ps_ptr[next].point.y + offset.y - gl_of.y, depth / 2.0),
};
for (int m = 0; m < 4; m++) {
const Vector2 &d = ((m % 2) == 0) ? d1 : d2;
Expand Down

0 comments on commit 2f1f120

Please sign in to comment.