From 18909268059dc76911223636b24767c1b8f53181 Mon Sep 17 00:00:00 2001 From: themancalledjakob Date: Tue, 19 Sep 2023 12:44:16 +0200 Subject: [PATCH] more robust outline flipping --- src/gpufont/font.hpp | 52 ++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/src/gpufont/font.hpp b/src/gpufont/font.hpp index 5890e64..1e9cd25 100644 --- a/src/gpufont/font.hpp +++ b/src/gpufont/font.hpp @@ -714,6 +714,7 @@ class Font { //cout << "but charcode is r or n" << endl; continue; } + // if does exist already if(glyphs.count(glyphIdentity) != 0){ continue; } @@ -936,33 +937,38 @@ class Font { convertContour(curves[i], directions[i], bbs[i], &face->glyph->outline, start, face->glyph->outline.contours[i], emSize); start = face->glyph->outline.contours[i] + 1; } + // outmost outline must be clock-wise + // then flip direction the deeper we go inside for(int i = 0; i < n_contours; i++){ - if(directions[i] < 0){ - bool flip = true; - for(int j = 0; j < n_contours; j++){ - if(i != j){ - bool inside = bbs[j].xMin <= bbs[i].xMin - && bbs[j].xMax >= bbs[i].xMax - && bbs[j].yMin <= bbs[i].yMin - && bbs[j].yMax >= bbs[i].yMax; - if(inside){ - flip = false; - break; - } + int insideAnother = 0; + for(int j = 0; j < n_contours; j++){ + if(i != j){ + bool inside = bbs[j].xMin <= bbs[i].xMin + && bbs[j].xMax >= bbs[i].xMax + && bbs[j].yMin <= bbs[i].yMin + && bbs[j].yMax >= bbs[i].yMax; + if(inside){ + insideAnother++; } } - if(flip){ - for(BufferCurve & curve : curves[i]){ - float tmp_x = curve.x0; - float tmp_y = curve.y0; - curve.x0 = curve.x2; - curve.y0 = curve.y2; - curve.x2 = tmp_x; - curve.y2 = tmp_y; - } - std::reverse(curves[i].begin(), curves[i].end()); - } } + bool flip = false; + if((insideAnother % 2 == 0 && directions[i] < 0) + || (insideAnother % 2 == 1 && directions[i] > 0)){ + flip = true; + } + if(flip){ + for(BufferCurve & curve : curves[i]){ + float tmp_x = curve.x0; + float tmp_y = curve.y0; + curve.x0 = curve.x2; + curve.y0 = curve.y2; + curve.x2 = tmp_x; + curve.y2 = tmp_y; + } + std::reverse(curves[i].begin(), curves[i].end()); + } + //} bufferCurves.insert(bufferCurves.end(), curves[i].begin(), curves[i].end()); } }else{