Newline characters with AnnotateImage() ?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
jamesanderson

Newline characters with AnnotateImage() ?

Post by jamesanderson »

I'm sure someone can answer this quickly. I can't seem to get AnnotateImage to support any form of newline character. Does it support using your own newlines?

For example, in my app I'm creating a black image to add a caption to, I use FormatMagickCaption() to add newlines if necessary. This works, however, some of my users may want to add newlines in specific places. Does anyone here know if AnnotateImage() supports that?

Thanks,
James

Before I forget, how about support for french chars? I wasn't thinking it did, but I may as well ask.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Newline characters with AnnotateImage() ?

Post by fmw42 »

I think it should do both (if you have proper utf-8 capabilities for your french characters with accent marks), at least in command line mode. I know nothing about doing it in an API.

For the new line, see the example

convert rose: -fill white -stroke black -font Candice -pointsize 20 \
-gravity center -annotate 0 '%wx%h\nPixels' annotate_rose.gif

at http://www.imagemagick.org/Usage/text/#annotate

but you can always break the line into separate -annotate commands and run them all in the same command line, see

convert -size 100x60 xc:skyblue \
-annotate 300x300+20+50 "First" \
-annotate 300x300+35+50 "Second" \
-annotate 300x300+50+50 "Third" \
-annotate 300x300+65+50 "Fourth" \
-annotate 300x300+80+50 "Fifth" \
annotated_labels.jpg

at the same location in the link above.

Also see http://www.imagemagick.org/Usage/text/#unicode
jamesanderson

Re: Newline characters with AnnotateImage() ?

Post by jamesanderson »

Using Annotate several times would not be a problem at all, I'd love to do that, however, I should clarify, I'm using the MagickCore API. Before I get into changing the code, I'll ask, do you think that will produce those results?

Here's some example code:

Code: Select all

char *pText = new char[2048];   
strcpy(pText, m_szCaption);  
CloneString(&textInfo.text, pText);
FormatMagickCaption(m_pCaptionImage, &textInfo, &metrics, &pText);
CloneString(&textInfo.text, pText);
bSuccess = AnnotateImage(m_pCaptionImage, &textInfo);
delete pText;
would you suggest that I simply create a loop around the AnnotateImage call? like so:

Code: Select all

char *pText = new char[2048];  
strcpy(pText, m_szCaption);  
CloneString(&textInfo.text, pText);
FormatMagickCaption(m_pCaptionImage, &textInfo, &metrics, &pText);
CloneString(&textInfo.text, pText);

// pseudo: break up string by newline chars

// pseudo: count num of newlines

for(int i = 0; i < numNewlines; i++) {
    bSuccess = AnnotateImage(m_pCaptionImage, &textInfo);
}
delete pText;
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Newline characters with AnnotateImage() ?

Post by fmw42 »

Sorry I am not an API programmer, I just script the command line. So someone else would have to help you there.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Newline characters with AnnotateImage() ?

Post by anthony »

Lets get back to basics.


You were having trouble inserting newline in your annotate string.

First Annotate from the command line when given a string will parse the string and substitute percent escapes and backslash \n escapes, before actually passing the string to the annotate library core function. You will need to find out if the API is doing this. - It probably isn't.

If it isn't than you need to use your API string functions to insert newlines and other string substitutions into the string being passed to the Annotate function. API's are good for doing this. Command line isn't which is why it has 'escapes'.

C++ I believe will insert a newline when it sees "\n" in a double quotes string constant. (most languages do this actually).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
jamesanderson

Re: Newline characters with AnnotateImage() ?

Post by jamesanderson »

So, to follow your advise I started looking through the String.c library for a function that could help. The only thing I found that looked promising was FormatMagickString. Now only if I could figure out a valid example of the "format" argument in the function.

Thanks for the info!

By the way, simply entering newline chars like "\n" doesn't work. I had tried that and ended up with a "\n" in my text. For instance my test of Hello \n World resulted in just that, rather than:

Hello
World

-James
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Newline characters with AnnotateImage() ?

Post by fmw42 »

Hello \n World
Just guessing, but try adding (single or double) quotes around \n
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Newline characters with AnnotateImage() ?

Post by el_supremo »

There are two ways to draw text on an image. One is to annotate the MagickWand image. The other is to draw on a DrawingWand and then render that onto a magickwand. The MagickWand annotation operation does not handle newlines whereas the drawing wand operation does.

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Newline characters with AnnotateImage() ?

Post by anthony »

el_supremo wrote:There are two ways to draw text on an image. One is to annotate the MagickWand image. The other is to draw on a DrawingWand and then render that onto a magickwand. The MagickWand annotation operation does not handle newlines whereas the drawing wand operation does.

Pete
Do you mean newline escapes "\n", or actual newlines in the string passed to the function. The distinction is very important.

The AnnotateImage() function does not do that escape expansion. That is a function of the API interface. The "convert" CLI command API for example runs the input string through InterpretImageProperties() to do the escape substitutions (as well as the '@' read literial string from file, before passing the string (and appropriate drawing parameters) to AnnotateImage()

You can see this if you look at wand/mogrify.c (which handles "convert" and "mogrify" option handling) and do a search for "annotate" (with the quotes). Note that string (with quotes) will appear twice, first with the function, the second time for syntax checking.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Newline characters with AnnotateImage() ?

Post by el_supremo »

The distinction is very important.
Aha. In my case I was passing \n via a TCL script so I presume the interpreter had already converted it to a linefeed before AnnotateImage or DrawAnnotation saw it. AnnotateImage seemed to interpret linefeed as the end of string because it didn't draw anything after that. DrawAnnotation drew the second line of text. I'll maybe play with that a bit more (or read the code).

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
Post Reply