Rank: Member
Groups: Member
Joined: 8/30/2004(UTC) Posts: 12
|
Hi,
I want to write a text paragraph on an image, with GM. Is there a VBS code sample to do this.
I can limit the lenght of each line to a X number of letters, but as all the letter doesn't have the same width, the chapter is not justified.
For instance : the line //MMM22222TTT// is longer that //iiiisniniii// also they contains the same number of letters
I guess it is a current problem, you have a solution for.
Thanks
Etienne
|
|
|
|
Rank: Advanced Member
Groups: Member, Administration Joined: 8/2/2003(UTC) Posts: 876
Thanks: 2 times Was thanked: 27 time(s) in 27 post(s)
|
Hi Etienne,
Currently Graphics Mill 2.0 is not suitable to draw multiline text. You can play with MeasureTextWidth method, but it will be quite untrivial to write good algorithm to calculate when to start new line.
It should be working something like that: you break your string into substrings and analyze whether it fits necessary width (using MeasureTextWidth). For example, you have string "Abcdef". You want to set paragraph width to 10. The simplest way would be to take "A" and check if it shorter than 10. If yes, repeat the same for "Ab", etc. As soon as you find the maximum number of characters from the beginning of the string, you insert here a line break, and repeat the same for the rest of string.
This algorithm is simple and not optimal (too much calls of MeasureTextWidth, it may work too slow for long strings). It may be improved by reducing number of substring length measurement (for example, use technique similar to binary search, etc).
Another way to improve speed is to use precalculated table of characters and its length. However it is acceptable only if you always using the single font settings...
Good news: upcoming Graphics Mill 3.0 for .NET will have this feature implemented. You will be able to pass a string and bounding rectangle, and Graphics Mill will do all job described above itself.
|
|
|
|
Rank: Member
Groups: Member
Joined: 8/30/2004(UTC) Posts: 12
|
I have written a piece of code, by spliting the body_ in spaces (not perfect, i don't know how to retrieve the number of splits) , but it works to me .. Code:<!-- METADATA TYPE="typelib" UUID="{3CE48541-DE7A-4909-9314-9D0ED0D1CA5A}"-->
<%
on error resume next
' création des fichiers de téléchargement
fich = DataPath & "message.jpg"
fich2 = DataPath & "destination.jpg"
Set objBitmap = Server.CreateObject("GraphicsMill.Bitmap")
objBitmap.LoadFromFile(fich)
With objBitmap.Graphics
'.Engine = DrawingEngineGdiplus
.Antialiasing = True
' Adjust font settings
.TextFormat.FontSize = 40
.TextFormat.FontName = "Verdana"
.TextFormat.IsBold = False
.TextFormat.IsItalic = False
.TextFormat.IsUnderlined = False
.TextFormat.FontColor = Colorgray
' contenu initial du texte
cont_txt = " " & body_
' dissociation du text en mots
cont_wor = split(cont_txt," ")
' nombre de mots écrits
i = 1
' nombre max de mots
nb_wor_max = len(cont_txt)
' largeur max d'une ligne
lar_max = 1157
' départ d'une ligne
lar_dep = 130
' largeur ligne en cours
lar_encours = lar_dep
' largeur espace
lar_espace = 17
' hauteur de ligne
haut_ = 50
' hauteur depart
haut_dep = 190
' hauteur en cours
haut_encours = haut_dep
do while not i = nb_wor_max
dblTextWidth = .TextFormat.MeasureTextWidth(cont_wor(i))
if err.number > 0 then
i = nb_wor_max
else
if (lar_encours + dblTextWidth) < lar_max then
.DrawText cont_wor(i), lar_encours, haut_encours
lar_encours = lar_encours + dblTextWidth + lar_espace
else
lar_encours = lar_dep
haut_encours = haut_encours + haut_
.DrawText cont_wor(i), lar_encours, haut_encours
lar_encours = lar_encours + dblTextWidth + lar_espace
end if
i = i + 1
end if
loop
End With
objBitmap.Effects.UnsharpMask 100, 1, 3
objBitmap.Formats.JpegIsProgressive = True
objBitmap.Formats.JpegQuality = 100
objBitmap.SaveToFile fich2
set objBitmap = nothing
Edited by user Monday, December 24, 2007 4:46:25 PM(UTC)
| Reason: Not specified
|
|
|
|
Rank: Member
Groups: Member
Joined: 8/30/2004(UTC) Posts: 12
|
it is ok with the instr method : Code:' contenu initial du texte
cont_txt = body_
' dissociation du text en mots
cont_wor = split(cont_txt," ")
' nombre de mots écrits
i = 0
' nombre max de mots
nb_wor_max = instr(cont_txt," ")
' largeur max d'une ligne
lar_max = 1157
' départ d'une ligne
lar_dep = 130
' largeur ligne en cours
lar_encours = lar_dep
' largeur espace
lar_espace = 17
' hauteur de paragraphe
haut_ = 50
' hauteur depart
haut_dep = 190
' hauteur en cours
haut_encours = haut_dep
' nombre de mots ecrits
nb_wor_wr = 0
do while not i = nb_wor_max
dblTextWidth = .TextFormat.MeasureTextWidth(cont_wor(i))
if (lar_encours + dblTextWidth) < lar_max then
.DrawText cont_wor(i), lar_encours, haut_encours
lar_encours = lar_encours + dblTextWidth + lar_espace
else
lar_encours = lar_dep
haut_encours = haut_encours + haut_
.DrawText cont_wor(i), lar_encours, haut_encours
lar_encours = lar_encours + dblTextWidth + lar_espace
end if
i = i + 1
loop
Edited by user Monday, December 24, 2007 4:46:45 PM(UTC)
| Reason: Not specified
|
|
|
|
Rank: Member
Groups: Member
Joined: 10/21/2004(UTC) Posts: 3
|
With this new functionality, will I be able to set my font attributes, declare a bounding rectangle, and then pass a string and have it write at the maximum size that will fit within the rectangle I declared?
If that is not the case, then what will happen if I try to write more text than can fit inside the box I decalre?
Also, are there any other new features related to drawing text?
Thanks.
|
|
|
|
Rank: Advanced Member
Groups: Member, Administration Joined: 8/2/2003(UTC) Posts: 876
Thanks: 2 times Was thanked: 27 time(s) in 27 post(s)
|
Yes, you will be able to specify bounding rectangle (and font attributes of course) and string will be automatically "broken" to multiple lines. If text exceeds of bounding rectangle, you can specify behavior what to do: whether clip it or not, add ellipsis or not, etc.
|
|
|
|
Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.