/* Importation d'un texte dans une zone rectangulaire
   Version 1.00: 26 avril 1998
   Version 1.01: 3 Février 1999 (correction bug guillemets + gestion ligne)
   Version 1.02: 21 février 1999 (modif appel REQFILE)
   Version 1.03: 5 Janvier 2000 (utilisation GETZONE, traitement coupure lignes trop longues)
   Version 1.04: 13 avril 2000 (adaptation version 205)
   Version 1.05: 10 novembre 2000 (localisation anglais/français)
   $VER: ImportTexte 1.05 (© R.Florac, 10 novembre 2000)
   Bug: ne gère pas les échelles et le mode placement courants */

options results

signal on error
signal on syntax

'LANGUAGE'
if result="français.language" then fr=1
else fr=0

xg=-1; xd=0; yh=0; yb=0
'MODE_TRACÉ=DRAWMODE(-1):ROTATE(0,0):SYMMETRY(0,0):SETSCALE(0,100,100):FIRSTSEL'; obj=result
if obj>0 then do
    'TYPE('obj')'
    if result=22 then do
	'NEXTSEL('obj')'
	if result=0 then do
	    'COORDS('obj')';
	    PARSE VAR result x0 ',' y0 ',' x1 ',' y1
	    xg=minima(x0,x1); xd=maxima(x0,x1)
	    yh=minima(y0,y1); yb=maxima(y0,y1)
	end
    end
end

if xg=-1 then do
    if fr=1 then 'GETZONE("Dessinez la zone où placer le texte")'
    else 'GETZONE("Draw the box where you want to place the text")'
    z=result
    if z="" then exit
    PARSE VAR z x0 ',' y0 ',' x1 ',' y1
    xg=minima(x0,x1); xd=maxima(x0,x1)
    yh=minima(y0,y1); yb=maxima(y0,y1)
end

if fr=1 then 'REQFILE("Nom du fichier texte?", "Travail:texte/ASCII", "")'
else 'REQFILE("Name of the text file?", "Work:Text/ASCII", "")'
fichier=result
y0=yh
if fichier ~= "" then do
    if open(file, fichier, 'R') then do
	y0=y0+10
	'SAVEALL'
	do while ~eof(file)
	    ligne=readln(file)
	    if ligne ~= "" then do
		ligne=translate(ligne,"        ",'09'x)
		ligne=doublage_guillemets(ligne)
		t=words(ligne)
		p=1
		n=t
		if t=1 then do	    /* il y a un seul mot à écrire */
		    'WRITE("'ligne'",'xg','y0')'
		    call ligne_suivante
		end
		else
		do while p<=t
		    mot=subword(ligne,p,n)
		    'TXWIDTH("'mot'")'
		    l=result
		    if l>=xd-xg then do
			n=n-1
		    end
		    else do
			'WRITE("'mot'",'xg','y0')'
			call ligne_suivante
			p=p+n
			n=t-n
		    end
		end
	    end
	end
	close(file)
    end
end
'DRAWMODE(MODE_TRACÉ)'
exit

ligne_suivante:
    y0=y0+10
    if y0>=yb then do
	if fr=1 then 'MESSAGE("Zone trop petite pour"+CHR(10)+"placer tout le texte")'
	else 'MESSAGE("The defined box is too small"+CHR(10)+"to place all the text")'
	close(file)
	'DRAWMODE(MODE_TRACÉ)'
	exit
    return

minima: procedure
    parse arg v1,v2
    if v1<v2 then return v1
    return v2
end

maxima: procedure
    parse arg v1,v2
    if v1>v2 then return v1
    return v2
end

doublage_guillemets: procedure
    parse arg chaine
    t=''
    do i=1 to length(chaine)
	c = substr(chaine,i,1)
	if c='"' then c=c||'"'
	t=t||c
    end
    return t

/* Traitement des erreurs, interruption du programme */
syntax:
erreur=RC
if fr=1 then 'MESSAGE("Script ImportTexte.AmiCAD"+CHR(10)+"Erreur de syntaxe"+CHR(10)+"en ligne 'SIGL'"+CHR(10)+"'errortext(erreur)'")'
else 'MESSAGE("ImportTexte.AmiCAD script"+CHR(10)+"Syntax error"+CHR(10)+"in line 'SIGL'"+CHR(10)+"'errortext(erreur)'")'
'DRAWMODE(MODE_TRACÉ)'
exit

error:
if fr=1 then 'MESSAGE("Script ImportTexte"+CHR(10)+"Erreur en ligne 'SIGL'")'
else 'MESSAGE("ImportTexte.AmiCAD Script"+CHR(10)+"Error in line 'SIGL'")'
'DRAWMODE(MODE_TRACÉ)'
exit
