<commit_before># Copyright 2008-2010 Universidad Politécnica de Madrid and Agora Systems S.A.
#
# This file is part of VCC (Virtual Conference Center).
#
# VCC is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# VCC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with VCC.  If not, see <http://www.gnu.org/licenses/>.

# Require Station Controller
require_dependency "#{ RAILS_ROOT }/vendor/plugins/station/app/controllers/logos_controller"
require 'RMagick'

class LogosController
  include Magick
  
  #for i in 1..5
   #   create_auto_logo "Global Plaza Project", i
    #end
  
  def new 
    if params[:text]
      #debugger
     for i in 1..5
        create_auto_logo params[:text], i
     end    
     render :template => "spaces/_generate_text_logos", :layout => false
   end
  
   if params[:upload]
      #debugger
     
     render :template => "logos/precrop_without_space", :layout => false
   end
   
   
  end
  
  def precrop
    
    if params['logo']['media'].blank?
      redirect_to request.referer
      return
    end

    @logo = space.logo || Logo.new 

    temp_logo = TempLogo.new(Logo, space, params[:logo])
    TempLogo.to_session(session, temp_logo)

    render :template => "logos/precrop",
           :layout => false,
           :locals => {:logo_crop_text => t('logo.crop'),
                       :form_for => [space,@logo],
                       :form_url => space_logo_path(space),
                       :image => temp_logo.image 
                      }
  end
  
  def precrop_without_space
    
    if params['logo']['media'].blank?
      redirect_to request.referer
      return
    end

    @logo = Logo.new 

     #debugger

    f = File.open(File.join(TMP_PATH,"precroplogo-#{params[:rand]}"), "w+")
    f.write(params['logo']['media'].read)
    f.close
    @image = "tmp/" + File.basename(f.path)
    session[:tmp_logo] = {}
    session[:tmp_logo][:basename] = File.basename(f.path)
    session[:tmp_logo][:original_filename] = params['logo']['media'].original_filename
    session[:tmp_logo][:content_type] = params['logo']['media'].content_type

    reshape_image f.path, Logo::ASPECT_RATIO_F
    resize_if_bigger f.path, 600
    
 
    @logo_crop_text = "Crop space logo"
    @form_for       = [@logo]
    @form_url       = space_logo_path(@space.id)
    
    render :template => "logos/precrop_without_space", :layout => false
    
  end
  
  def create
    if params[:crop_size].present?
      temp_logo = TempLogo.from_session(session)
      params[:logo] = temp_logo.crop_and_resize params[:crop_size]
    end
    @logo = space.build_logo(params[:logo])
    if @logo.save
      flash[:success] = t('logo.created')
      redirect_to edit_space_path(space)
    else
      flash[:error] = t('error', :count => @logo.errors.size) + @logo.errors.to_xml
      redirect_to edit_space_path(space)
    end
    
  end
  
  def update
    if params[:crop_size].present?
      temp_logo = TempLogo.from_session(session)
      params[:logo] = temp_logo.crop_and_resize params[:crop_size]
    end
    @logo = space.logo
    if @logo.update_attributes(params[:logo])
      flash[:success] = t('logo.created')
      redirect_to edit_space_path(space)
    else
      flash[:error] = t('error', :count => @logo.errors.size) + @logo.errors.to_xml
      redirect_to edit_space_path(space)
    end   
  end
  
  private
  
  def max_word_length text
    first_pos = 0
    max_length = 0
    while !((pos = (text+" ").index(' ', first_pos)).nil?)
      if (pos - first_pos) > max_length
        max_length = pos - first_pos
      end
      first_pos = pos + 1
    end
    return max_length
  end
  
  def count_potential_lines text
    return text.count(" ")
  end
  
  def multiline_point_size text, width, height
    size_based_on_width = 1.7 * width / max_word_length(text)
    size_based_on_lines = 0.6 * height / count_potential_lines(text)
    if size_based_on_width > size_based_on_lines
      return size_based_on_lines
    else
      return size_based_on_width
    end
  end
  
  def singleline_point_size text, width
    return 1.7 * width / text.length
  end
  
  def create_auto_logo text, logo_style
    
    # We establish the paths for the pre-defined images, and the temporal dir for the generated logo
    images_path = File.join(RAILS_ROOT, "public", "images")
    tmp_path = File.join(images_path, "tmp")
    final_path = FileUtils.mkdir_p(tmp_path + "/#{params[:rand_name]}")
    background_generic = File.join(images_path, "vcc-logo-bg.png")
    #background_generated = File.join(tmp_path, "vcc-logo-#{params[:rand_name]}-#{logo_style}.png")
    background_generated = File.join(final_path, "vcc-logo-#{params[:rand_name]}-#{logo_style}.png")
    
    # We open, read-only, the generic background image
    f = File.open(background_generic, "r+")
    img = Magick::Image.read(f).first
    
    # This will be the blank image which will contain the text
    logo_text = Magick::Image.new(img.columns, img.rows)
    # To create the text, we use a new "Draw" object, and set some basic styles
    gc = Magick::Draw.new
    gc.font_family = "Helvetica"
    gc.font_style = Magick::ObliqueStyle
    gc.font_weight = Magick::LighterWeight
    gc.gravity = Magick::CenterGravity
    gc.stroke = "darkblue"
    gc.stroke_opacity(1)
    gc.text_antialias(true)
    gc.stroke_antialias(true)
    gc.stroke_linecap("round")
    gc.fill = "darkblue"

    # Depending on the desired logo_style, we create a text or another 
    case logo_style
      when 1
        gc.pointsize = 0.7 * (multiline_point_size text+"\\n", img.columns, img.rows)
        gc.gravity = Magick::SouthGravity
        gc.annotate(logo_text,img.columns,img.rows,0,0,text+"\\n")
        auto_logo = img.composite!(logo_text, Magick::CenterGravity, Magick::ColorBurnCompositeOp)<commit_msg>Fixed text generated logo permission bug<commit_after># Copyright 2008-2010 Universidad Politécnica de Madrid and Agora Systems S.A.
#
# This file is part of VCC (Virtual Conference Center).
#
# VCC is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# VCC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with VCC.  If not, see <http://www.gnu.org/licenses/>.

# Require Station Controller
require_dependency "#{ RAILS_ROOT }/vendor/plugins/station/app/controllers/logos_controller"
require 'RMagick'

class LogosController
  include Magick
  
  #for i in 1..5
   #   create_auto_logo "Global Plaza Project", i
    #end
  
  def new 
    if params[:text]
      #debugger
     for i in 1..5
        create_auto_logo params[:text], i
     end    
     render :template => "spaces/_generate_text_logos", :layout => false
   end
  
   if params[:upload]
      #debugger
     
     render :template => "logos/precrop_without_space", :layout => false
   end
   
   
  end
  
  def precrop
    
    if params['logo']['media'].blank?
      redirect_to request.referer
      return
    end

    @logo = space.logo || Logo.new 

    temp_logo = TempLogo.new(Logo, space, params[:logo])
    TempLogo.to_session(session, temp_logo)

    render :template => "logos/precrop",
           :layout => false,
           :locals => {:logo_crop_text => t('logo.crop'),
                       :form_for => [space,@logo],
                       :form_url => space_logo_path(space),
                       :image => temp_logo.image 
                      }
  end
  
  def precrop_without_space
    
    if params['logo']['media'].blank?
      redirect_to request.referer
      return
    end

    @logo = Logo.new 

     #debugger

    f = File.open(File.join(TMP_PATH,"precroplogo-#{params[:rand]}"), "w+")
    f.write(params['logo']['media'].read)
    f.close
    @image = "tmp/" + File.basename(f.path)
    session[:tmp_logo] = {}
    session[:tmp_logo][:basename] = File.basename(f.path)
    session[:tmp_logo][:original_filename] = params['logo']['media'].original_filename
    session[:tmp_logo][:content_type] = params['logo']['media'].content_type

    reshape_image f.path, Logo::ASPECT_RATIO_F
    resize_if_bigger f.path, 600
    
 
    @logo_crop_text = "Crop space logo"
    @form_for       = [@logo]
    @form_url       = space_logo_path(@space.id)
    
    render :template => "logos/precrop_without_space", :layout => false
    
  end
  
  def create
    if params[:crop_size].present?
      temp_logo = TempLogo.from_session(session)
      params[:logo] = temp_logo.crop_and_resize params[:crop_size]
    end
    @logo = space.build_logo(params[:logo])
    if @logo.save
      flash[:success] = t('logo.created')
      redirect_to edit_space_path(space)
    else
      flash[:error] = t('error', :count => @logo.errors.size) + @logo.errors.to_xml
      redirect_to edit_space_path(space)
    end
    
  end
  
  def update
    if params[:crop_size].present?
      temp_logo = TempLogo.from_session(session)
      params[:logo] = temp_logo.crop_and_resize params[:crop_size]
    end
    @logo = space.logo
    if @logo.update_attributes(params[:logo])
      flash[:success] = t('logo.created')
      redirect_to edit_space_path(space)
    else
      flash[:error] = t('error', :count => @logo.errors.size) + @logo.errors.to_xml
      redirect_to edit_space_path(space)
    end   
  end
  
  private
  
  def max_word_length text
    first_pos = 0
    max_length = 0
    while !((pos = (text+" ").index(' ', first_pos)).nil?)
      if (pos - first_pos) > max_length
        max_length = pos - first_pos
      end
      first_pos = pos + 1
    end
    return max_length
  end
  
  def count_potential_lines text
    return text.count(" ")
  end
  
  def multiline_point_size text, width, height
    size_based_on_width = 1.7 * width / max_word_length(text)
    size_based_on_lines = 0.6 * height / count_potential_lines(text)
    if size_based_on_width > size_based_on_lines
      return size_based_on_lines
    else
      return size_based_on_width
    end
  end
  
  def singleline_point_size text, width
    return 1.7 * width / text.length
  end
  
  def create_auto_logo text, logo_style
    
    # We establish the paths for the pre-defined images, and the temporal dir for the generated logo
    images_path = File.join(RAILS_ROOT, "public", "images")
    tmp_path = File.join(images_path, "tmp")
    final_path = FileUtils.mkdir_p(tmp_path + "/#{params[:rand_name]}")
    background_generic = File.join(images_path, "vcc-logo-bg.png")
    #background_generated = File.join(tmp_path, "vcc-logo-#{params[:rand_name]}-#{logo_style}.png")
    background_generated = File.join(final_path, "vcc-logo-#{params[:rand_name]}-#{logo_style}.png")
    
    # We open, read-only, the generic background image
    f = File.open(background_generic, "r")
    img = Magick::Image.read(f).first
    
    # This will be the blank image which will contain the text
    logo_text = Magick::Image.new(img.columns, img.rows)
    # To create the text, we use a new "Draw" object, and set some basic styles
    gc = Magick::Draw.new
    gc.font_family = "Helvetica"
    gc.font_style = Magick::ObliqueStyle
    gc.font_weight = Magick::LighterWeight
    gc.gravity = Magick::CenterGravity
    gc.stroke = "darkblue"
    gc.stroke_opacity(1)
    gc.text_antialias(true)
    gc.stroke_antialias(true)
    gc.stroke_linecap("round")
    gc.fill = "darkblue"

    # Depending on the desired logo_style, we create a text or another 
    case logo_style
      when 1
        gc.pointsize = 0.7 * (multiline_point_size text+"\\n", img.columns, img.rows)
        gc.gravity = Magick::SouthGravity
        gc.annotate(logo_text,img.columns,img.rows,0,0,text+"\\n")
        auto_logo = img.composite!(logo_text, Magick::CenterGravity, Magick::ColorBurnCompositeOp)
{'fraction_non_alphanumeric': 0.11097833014659018, 'fraction_numerical': 0.004939451880178458, 'mean_word_length': 3.0028698979591835, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5089772', 'n_tokens_mistral': 4193, 'n_tokens_neox': 4067, 'n_words': 996}