Skip to content

Commit 8bb2c56

Browse files
committed
Better text height estimation for AGG and... slightly better for Quartz.
1 parent 31b4ea9 commit 8bb2c56

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

Sources/AGGRenderer/CPPAGGRenderer/CPPAGGRenderer.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,9 @@ namespace CPPAGGRenderer{
481481
void get_text_size(const char *s, float size, float* outW, float* outH){
482482
font_width = font_height = size;
483483
m_contour.width(-font_weight*font_height*0.05);
484-
float x = 0;
485-
float y = 0;
484+
float x = 0;
485+
float maxY = 0;
486+
float minY = 0;
486487
// set rotation of font engine to zero before calculating text width
487488
agg::trans_affine matrix;
488489
matrix *= agg::trans_affine_rotation(agg::deg2rad(0));
@@ -496,16 +497,16 @@ namespace CPPAGGRenderer{
496497
const agg::glyph_cache* glyph = m_fman.glyph(*s);
497498
if(glyph){
498499
x+=glyph->advance_x;
499-
float height = glyph->bounds.y2 - glyph->bounds.y1;
500-
y = max(y, height);
500+
maxY = max(maxY, (float)glyph->bounds.y2);
501+
minY = min(minY, (float)glyph->bounds.y1);
501502
}
502503
++s;
503504
}
504505
}
505506
if (outW)
506507
*outW = x;
507508
if (outH)
508-
*outH = y;
509+
*outH = maxY - abs(minY);
509510
}
510511

511512
unsigned save_image(const char *s, const char** errorDesc){

Sources/QuartzRenderer/QuartzRenderer.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public class QuartzRenderer: Renderer {
4343
height: Int(imageSize.height))
4444
context.setFillColor(Color.white.cgColor)
4545
context.fill(rect)
46+
self.context.setShouldSmoothFonts(false)
47+
// context.setShouldAntialias(false)
4648
}
4749
}
4850

@@ -64,6 +66,8 @@ public class QuartzRenderer: Renderer {
6466
self.context.setAllowsFontSmoothing(true)
6567
self.context.setShouldSmoothFonts(fontSmoothing)
6668
self.isExternalContext = false
69+
self.context.setShouldSmoothFonts(false)
70+
// context.setShouldAntialias(false)
6771
}
6872

6973
/// Creates a renderer with the given external context and dimensions..
@@ -485,7 +489,8 @@ public class QuartzRenderer: Renderer {
485489
#endif
486490
let string = NSAttributedString(string: "\(text)", attributes: attributes)
487491
let size = string.size()
488-
return Size(width: Float(size.width), height: Float(size.height))
492+
// FIXME: 'size.height' is always too big and misaligns text.
493+
return Size(width: Float(size.width), height: s)
489494
}
490495

491496
enum WritePNGError: Error {

0 commit comments

Comments
 (0)