# File lib/host_systems/C64.rb, line 229
def self.to_screendump(text,screen_width=default_screen_width)
  
  main_width=font_width*screen_width
  main_height=(pixels_between_characters+font_height)*line_count(text,screen_width)
  
  total_width=main_width+2*border_width
  total_height=main_height+2*border_height
  
    
  foreground_colour=default_foreground_colour
  background_colour=default_background_colour  
  background_colour=COLOR_BLACK if text=~/\x5|\x1c|\x1e|\x1f|\x81|\x90|\x95|\x96|\x97|\x8e|\x99|\x9a|\x9b|\x9c|\x9e|\x9f/

canvas = PNG::Canvas.new total_width,total_height , background_colour  
  #draw the borders

  total_width.times do |x|
    total_height.times do |y|    
      canvas[x,y]=foreground_colour if (x<border_width) || (x>main_width+border_width) || (y<border_height) || (y>border_height+main_height)  
    end
  end
  col=0
  line_number=0
  reverse=false
  mode=:lowercase
  enable_shift_cbm=true
  text.each_byte do |char|
    case char
      #colours first

      when 5  then foreground_colour=COLOR_WHITE  
      when 28 then foreground_colour=COLOR_RED
      when 30 then foreground_colour=COLOR_GREEN
      when 31  then foreground_colour=COLOR_BLUE
      when 129 then foreground_colour=COLOR_ORANGE
      when 144 then foreground_colour=COLOR_BLACK
      when 149 then foreground_colour=COLOR_BROWN
      when 150 then foreground_colour=COLOR_LTRED
      when 151 then foreground_colour=COLOR_DKGRAY
      when 152 then foreground_colour=COLOR_GRAY
      when 153 then foreground_colour=COLOR_LTGREEN
      when 154 then foreground_colour=COLOR_LTBLUE
      when 155 then foreground_colour=COLOR_LTGRAY
      when 156 then foreground_colour=COLOR_PURPLE
      when 158 then foreground_colour=COLOR_YELLOW
      when 159 then foreground_colour=COLOR_CYAN
    #now movement

      when 10 #do nothing

      when 13 , 141 #return

        col=0
        reverse=false
        line_number+=1
      when 17 then line_number+=1
      when 147 #clr/home

        col=0
        line_number=0
        #cls

      when 19 #home

        col=0
        line_number=0
      when 20 #delete

        col-=1
        if col<0 then
          col=screen_width-1
          line_number-=1
        end
        draw_char_at(canvas,@screencode[32],line_number,col,foreground_colour,background_colour,mode,false)                   

      when 157 then col-=1
      when 29 then col+=1
      when 145 then line_number-=1
      when 148 # insert ?



        
      #now fonts

      when 8 then enable_shift_cbm=false
      when 9 then enable_shift_cbm=true
      when 14 then mode=:lowercase if enable_shift_cbm
      when 142 then mode=:graphics if enable_shift_cbm
      when 18 then reverse=true
      when 146 then reverse=false      
      
    else
#        puts "#{line_number},#{col}"

      draw_char_at(canvas,@screencode[char],line_number,col,foreground_colour,background_colour,mode,reverse) 
      col+=1     
    end    
    if (col>=screen_width) then
      line_number+=1
      col=0
    end
    if (col<0) then
      line_number-=1
      col=screen_width-1
    end            
    line_number=0 if line_number<0
  end    
  PNG.new(canvas).raw_bytes
end