All pastes #2103784 Raw Edit

Someone

public text v1 · immutable
#2103784 ·published 2012-01-18 12:48 UTC
rendered paste body
    public function adjustTextSize (o) {
     if (o.autosize) {
        
        var st:TextField = new TextField();
        st.type = TextFieldType.DYNAMIC;
        st.embedFonts = true;
        st.multiline = true;
        st.wordWrap = true;
        st.width = o.width;
        st.height = o.height;
        st.text = o.text;
        var tft:TextFormat = new TextFormat(usable_font_name,o.size,0xFFFFFF);
        tft.align = "center";
        
        v = 128;
        j = k = 256;
        
        var vc = 0;
        
        while (true) {
          tft.size = j;
         tft.letterSpacing = getLetterSpacing(j);
          st.setTextFormat(tft);
          
          if (st.textHeight > o.height-8) { // too big
            j = j-v;
          } else { // not big enough?
            k = j;
            j = j+v;
          }
          v = v/2;
          if (v <= 1) {
            vc++;
            if (vc >= 5) {
              break;
            }
          }
        }
        
        if (k<12) {
          k = 12;
        }
        if (k>127) {
          k = 127;
        }
        o.size = k;
      }
    }