Quote
That is what it was designed for, to wrap before, and as close to, the specified width, otherwise it wouldn't have much use. I have updated my post.

Ok.
Let me also clarify: I meant that it should use the maximum width available.

I wrote a custom $wrap2 identifier which does the job I/we are talking about:


Code
alias wrap2 {
  ;$1 = text $2 = font $3 = size, $4 = width, $5 = B $6 = C $7 = D $8 = N $9 = word $10 = optional C
  if ($1 == $null) || ($2 == $null) || ($3 == $null) || ($4 == $null) || ($8 == $null) { echo -s * $wrap2 insufficient params | return }
  if ($4 == 0) {
    if ($8 == 0) return 0
    return 
  }
  elseif ($width($1,$2,$3,$5,$6,$7) <= $4) {
    if ($8 == 0) return 1
    return $1
  }
  ;else echo -s $v1
  ;if no word
  if (!$9) {
    ;if N = 0 return total number of lines
    if ($8 == 0) return $ceil($calc($width($1,$2,$3,$5,$6,$7) / $4))
    else {
      var %index 1,%n 0,%seek,%len
      ;otherwise, we get each line and return it if N correspond
      while ($mid($1,%index,1) != $null) {
        %seek = 1
        %len = $calc($len($1) - %index + 1)
        while (%seek <= %len) && ($width($mid($1,%index,%seek),$2,$3,$5,$6,$7) <= $4) {
          inc %seek        
        }
        dec %seek
        inc %n
        if (%n == $8) returnex $mid($1,%index,%seek)
        %index = %index + %seek
      }
    }
    ;last line because seek > len
    returnex $mid($1,%index)
  }
  else {

    var %n 0,%seek 1,%rp $iif($chr($10) isin \[]-,\ $+ $v1,$v1),%p /([^\x20 $+ %rp $+ ]+)([\x20 $+ %rp $+ ]+|$)/
    var %index 1
    while ($regex(wrap2,$mid($1,%seek),%p)) {
      var -p %wt %r $+ $regml(wrap2,1)
      if ($width(%wt,$2,$3,$5,$6,$7) > $4) {
        inc %n
        if (!%r) {
          if ($8 == 0) return 0
          return 
        }
        if (%n == $8) {
          returnex $mid(%wt,1,- $+ $calc($len($regml(wrap2,1) $+ $regml(wrap2,2)) + 1))
        }
        unset %r
        var %no 1
      }
      if (!%no) {
        var -p %r %r $+ $regml(wrap2,1) $+ $regml(wrap2,2)
        inc %seek $len($regml(wrap2,1) $+ $regml(wrap2,2))
      }
      unset %no
    }
    inc %n
  }
  if ($8 == 0) return %n
  returnex %wt
}


And I wrote a code demonstrating the neat differences (which is, in fact, very small and hard to see in practice)

Code
alias testingwrap {
  window -pdfoBC +slt @tw -1 -1 400 1000
  testingwrap_refresh
}

alias testingwrap_refresh {
  drawrect -frn @tw 16777215 0 0 0 $window(@tw).dw $window(@tw).dh
  var %1 This is a line wrapped with stuff ok very long line
  var %font segoe ui symbol,%size 15
  var %x 0,%y 0 ,%w $window(@tw).dw
  var %a 1,%wrap1 $wrap(%1,%font,%size,%w,0,0)
  while (%a <= %wrap1) {
    drawtext -rn @tw 0 $qt(%font) %size %x %y $wrap(%1,%font,%size,%w,0,%a)
    inc %a
    inc %y 20
  }

  var %a 1,%wrap1 $wrap2(%1,%font,%size,%w,0,0,0,0,0)
  var %y = 200
  while (%a <= %wrap1) {
    drawtext -rn @tw 0 $qt(%font) %size %x %y $wrap2(%1,%font,%size,%w,0,0,0,%a,0)
    inc %a
    inc %y 20
  }
  var %a 1,%wrap1 $wrap(%1,%font,%size,%w,1,0)
  var %y = 400
  while (%a <= %wrap1) {
    drawtext -rn @tw 0 $qt(%font) %size %x %y $wrap(%1,%font,%size,%w,1,%a)
    inc %a
    inc %y 20
  }

  var %a 1,%wrap1 $wrap2(%1,%font,%size,%w,0,0,0,0,1)
  var %y = 600
  while (%a <= %wrap1) {
    drawtext -rn @tw 0 $qt(%font) %size %x %y $wrap2(%1,%font,%size,%w,0,0,0,%a,1)
    inc %a
    inc %y 20
  }
  drawdot @Tw
  .timertw -ho 1 30 testingwrap_refresh
}
on *:close:@tw:.timertw off
First line = mIRC's $wrap with word = 0.
Second line = $wrap2 with word = 0
Third line = mIRC's $wrap with word = 1
Fourth line = $wrap2 with word = 1

As i said the difference is not very big at where it wraps.
However $wrap actually breaks on the rule of word boundary (when word = 1) when the width is too small to fit a word, in this case it will show the maximum possible width of the word and then wrap, which allows to see the text even with small widths.

My custom alias is not doing that, instead it will report that the total number of line is 0, because you won't be able to draw the word entirely. Note that this version of the alias still make it possible to access the early part of a wrapped line as long as they fit, even though the next chunk might not fit and would return $null, but N = 0 always return 0 when any of the chunk/word can't fit, it could be changed.

Also $wrap has some bugs without the word parameter, this bug was reported years ago: $wrap(Hello World,Tahoma,11,1,0,1) is H but should be $null because you cannot draw H with one pixel.
If at least $wrap(Hello World,Tahoma,11,1,0,2) were 'e', it would make sense but there i think it should return $null.



Another topic is that we cannot currently draw consecutives spaces with /drawtext, and to overcome this issue, we must change spaces to something else, maybe a switch for /drawtext to draw from a binvar could be added?
My alias allows for a $10 parameter to be a ascii/code point value to be used as a character for boundary instead of the space ascii 32.
Unicode space characters exist, but we would need a way to pass that to $wrap.

If not for the old bug, I don't think $wrap is that buggy with word parameter = 0, it's not pixel perfect but good enough for practice, it may still be great to see $wrap fixed/extended to be pixel perfect.

I'll make more test with word = 1 but it would be great to be able to draw consecutives space with drawtext to see this in action.

Note: $wrap does need to be extended to support the new parameter of $width


#mircscripting @ irc.swiftirc.net == the best mIRC help channel