Reputation: 1531
I am having quite a bit of trouble getting mini_magick to draw text that contains quotes, double quotes, etc. on an image. I have tried various modifications on c.draw
to no avail. What is the best way to escape these characters so that c.draw will display them without an error?
error: non-conforming drawing primitive definition
m'`
produced by
c.draw "text 8,8 'I'm'"
Including other special character such as é will result in an error as well. I would like to be able to accept text strings from users as input, therefore the need for Unicode compatibility.
Upvotes: 2
Views: 2066
Reputation: 1
#Try lightweight GD2: https://www.ruby-toolbox.com/search?q=GD2
require 'gd2-ffij'
PATH_TO_FONT = "/usr/share/fonts/truetype/DroidSans.ttf"
image = GD2::Image::TrueColor.new(512, 512)
image.draw do |pen|
pen.font = GD2::Font::TrueType[PATH_TO_FONT, 32]
pen.color = image.palette.resolve(GD2::Color[128, 16, 16])
pen.move_to(256, 128)
pen.text(GD2::VERSION, 5)
end
image.export('./one.jpg')
Upvotes: 0
Reputation: 90253
Did you see...
?
In any case, the following works for me on the commandline:
convert \
-size 500x100 xc:none \
-box yellow \
-pointsize 72 \
-gravity center \
-draw "text 8,8 ' \'I\'m\' '" \
-trim \
+repage \
special-chars.png
and produces this:
For more complicated text drawing requirements, you are strongly advised to circumvent all the escaping by writing your drawing commands into a separate *.mvg
(Magick Vector Graphic) file. For example with this content in 1.mvg
:
text 8,8 "öäü ß ÄÖÜ é"
and this command:
convert \
-size 250x100 xc:none \
-box yellow \
-pointsize 72 \
-gravity center \
-draw @1.mvg \
-trim \
+repage \
special-chars.png
you'll get
Or even, with 2.mvg
:
push graphic-context viewbox 0 0 600 100 push graphic-context fill 'orange' rectangle 0,0 600,100 pop graphic-context push graphic-context fill 'white' font Palatino-Roman font-size 48 stroke-width 2 gravity SouthEast text 8,8 "äöü ß ÄÖÜ é" pop graphic-context push graphic-context fill 'green' rectangle 10,10 300,90 pop graphic-context push graphic-context fill 'red' font Palatino-Bold-Italic font-size 28 stroke-width 1 text 18,40 "€ ¥ © ℉ ậ ḁ å ǎ à ç ë ĵ" pop graphic-context pop graphic-contextand this command:
convert 2.mvg 2.png
you can get:
Upvotes: 1