Reputation: 4314
I'm using RMagick and I want to draw text vertically or horizontally (depends on user input) with background color set by user. I've ran into odd problem: get_type_metrics(text) returns invalid data. Actual width / height are smaller than returned by method.
I've been trying to play around with ascent / descent values, but no luck. Is there any way to determine real width / height of text string? I've seen couple solutions drawing text on empty image then determining width height then looking through whole image pixel by pixel, but imho thats stupid and really resource consumptive. Also as I've find out PHP GD has same issue and no solution.
P.S. I can't use Draw#annotate because user should be able to change background box size.
Upvotes: 2
Views: 1837
Reputation: 4314
As I've found out, there's undocumented (in RMagick) bounds
member of TypeMetric
struct which is documented in FreeType Glyph Conventions and your real width & height of text is determined like this
gc = Magick::Draw.new { ... }
# *** setup gc with font, stroke, pointsize, ... *** THATS IMPORTANT!
metrics = gc.get_type_metrics("your text")
box_width = metrics.width # this one is ok by default
box_height = (metrics.bounds.y2 - metrics.bounds.y1).round # this is the actual heihgt
Upvotes: 3