#!/usr/local/bin/perl
$gpcItemString = 'w';
$gpcItemFntColBegin = 'st';
$gpcItemFntColEnd = 'fn';


sub iReadParse {
#	local ( *pc_string ) = @_ if @_;
	local ( $i_ii , $pc_key , $pc_val , $pc_string );

	# Read in text
	if ( &iIsMethodGet() ) {
		$pc_string = $ENV{ 'QUERY_STRING' };
	}elsif ( &iIsMethodPost() ) {
		read( STDIN , $pc_string , $ENV{ 'CONTENT_LENGTH' } );
	}

	@gpcKey = split( /&/ , $pc_string );

	foreach $i_ii ( 0 .. $#gpcKey ) {
		# Convert plus's to spaces
		$gpcKey[ $i_ii ] =~ s/\+/ /g;

		# Split into key and value.
		( $pc_key , $pc_val ) = split( /=/ , $gpcKey[ $i_ii ] , 2 );

		# Convert %XX from hex numbers to alphanumeric
		$pc_key =~ s/%(..)/pack( "c" , hex( $1 ) )/ge;
		$pc_val =~ s/%(..)/pack( "c" , hex( $1 ) )/ge;
		# Associate key and value
		$gpcKey{ $pc_key } = "\0" if ( defined( $gpcKey{ $pc_key } ) );
		$gpcKey{ $pc_key } = $pc_val;
#		$gpcKey{ $pc_key } .= "\0" if ( defined( $gpcKey{ $pc_key } ) );
#		$gpcKey{ $pc_key } .= $pc_val;
	}

	return length( $gpcKey );
}

sub iIsMethodGet {
	return ( $ENV{ 'REQUEST_METHOD' } eq "GET" );
}

sub iIsMethodPost {
	return ( $ENV{ 'REQUEST_METHOD' } eq "POST" );
}

#require 'global_gradation.pl';

$\ = "\n";			# print の改行ｺｰﾄﾞの設定
$, = "";			# print で表示するｱｲﾃﾑが複数ある時のｱｲﾃﾑ間に挿入する文字の設定
$[ = 0;				# 最初の添え字の要素の設定
$| = 1;				# print, write でﾊﾞｯﾌｧﾘﾝｸﾞをしない。


@giFont = ();
@giColor = (
	{
		iStart => 0 ,
#		iEnd => 0 , 
		iDif => 0
	}
  , {
		iStart => 0 ,
#		iEnd => 0 , 
		iDif => 0
	}
  , {
		iStart => 0 ,
#		iEnd => 0 , 
		iDif => 0
	}
);

sub iDispGradationHtml {
	my( $i_ret , $pc_html );
	my( $pc_bg_col );
	my( $i_length , $i_grad_f , $i_size_f , $i_close_f , $i_font_f , $i_ug_f , $i_strong_f , $i_tilt_f );
	my( $i_base , $i_mod , $i_def , $i_k , $i_d );
	my( @pi_per , @pi_size , @pi_col );
	my( $i_ii , $i_jj , $pc_tmp );

	if ( $gpcKey{ $gpcItemBackColSelect } == 1 ) {
		$pc_bg_col = $gpcKey{ $gpcItemBackColVal };
	}
	else {
		$pc_bg_col = $gpiColor[ $gpcKey{ $gpcItemBackColNo } ];
	}
	$i_length = length $gpcKey{ $gpcItemString };
	$i_font_f = ( $gpcKey{ $gpcItemFont } != -1 ) ? 0 : 1;
	$i_grad_f = ( $gpcKey{ $gpcItemFntColBegin } eq $gpcKey{ $gpcItemFntColEnd } ) ? 0 : 1;
	$i_size_f = ( $giFont[ 0 ] == $giFont[ 1 ] && $giFont[ 0 ] != 0 ) ? 0 : 1;
	$i_ug_f = ( $gpcKey{ $gpcItemUG } == 1 ) ? 1 : 0;
	$i_strong_f = ( $gpcKey{ $gpcItemStrong } == 1 ) ? 1 : 0;
	$i_tilt_f = ( $gpcKey{ $gpcItemTilt } == 1 ) ? 1 : 0;
	$pc_html = '';
	$i_close_f = 0;
	
	$pi_per[ 0 ] = 0;
	if ( $i_length > 1 ) {
		$i_base = int( 100 / ( $i_length - 1 ) );
		$i_mod = 100 % ( $i_length - 1 );
		$i_def = 0;
		for( $i_ii = 1 ; $i_ii < $i_length ; $i_ii ++ ) {
			if ( $i_def < $i_mod / ( $i_length - 1 ) * $i_ii ) {
				$i_def ++;
				$pi_per[ $i_ii ] = $pi_per[ $i_ii - 1 ] + $i_base + 1;
			}
			else {
				$pi_per[ $i_ii ] = $pi_per[ $i_ii - 1 ] + $i_base;
			}
		}
		if ( $gpcKey{ $gpcItemDir } == 2 ) {
			$gpcKey{ $gpcItemString } =~ tr/ー/｜/;					# 'ー' → "｜"
		}
	}
	if ( $i_grad_f == 0 && $i_size_f == 0 ) {
		if ( $gpcKey{ $gpcItemDir } == 1 ) {
			$pc_html .= $gpcKey{ $gpcItemString };
		}
		else {
			for( $i_ii = 0 ; $i_ii < $i_length ; $i_ii ++ ) {
				$pc_tmp = substr( $gpcKey{ $gpcItemString } , $i_ii , 2 );
				if ( $pc_tmp =~ m/[\201-\237\340-\374][\100-\176\200-\374]/ ) {
					$pc_html .= $pc_tmp;
					$i_ii ++;
				}
				else {
					$pc_tmp = substr( $gpcKey{ $gpcItemString } , $i_ii , 1 );
					$pc_tmp =~ s/&/&amp;/g;					# '&' → "&amp;"
					$pc_tmp =~ s/</&lt;/g;					# '<' → "&lt;"
					$pc_tmp =~ s/>/&gt;/g;					# '>' → "&gt;"
					$pc_tmp =~ s/"/&quot;/g;				# '"' → "&quot;"
					$pc_html .= $pc_tmp;
				}
				if ( $gpcKey{ $gpcItemDir } == 2 ) {
					$pc_tmp = substr( $gpcKey{ $gpcItemString } , $i_ii + 1 , 2 );
					if ( $pc_tmp =~ m/[\201-\237\340-\374][\100-\176\200-\374]/ ) {
						if ( ( $pc_tmp eq '゜' ) || ( $pc_tmp eq '゛' ) || ( $pc_tmp eq '’' ) ) {
							$pc_html .= $pc_tmp;
							$i_ii += 2;
						}
					}
					else {
						$pc_tmp = substr( $gpcKey{ $gpcItemString } , $i_ii + 1 , 1 );
						if ( ( $pc_tmp eq 'ﾟ' ) || ( $pc_tmp eq 'ﾞ' ) || ( $pc_tmp eq '\'' ) ) {
							$pc_html .= $pc_tmp;
							$i_ii ++;
						}
					}
				}
				$pc_html .= '<Br>';
			}
		}
	}
	else {
		$i_size_f = ( $giFont[ 0 ] != $giFont[ 1 ] ) ? 1 : 0;
		if ( $i_size_f ) {
			$i_base = $giFont[ 0 ];
			$i_def = $giFont[ 1 ] - $giFont[ 0 ];
			if ( $i_def > 0 ) {
				$i_k = $i_base;
				$i_d = ( $i_def + 1 ) / $i_length;
			}
			else {
				$i_k = $i_base + 1;
				$i_d = ( $i_def - 1 ) / $i_length;
			}
		}
		for( $i_ii = 0 ; $i_ii < $i_length ; $i_ii ++ ) {
			$pc_html .= '<Font';
			if ( $i_grad_f ) {
				$pc_html .= ' Color="#';
				for( $i_jj = 0 ; $i_jj < 3 ; $i_jj ++ ) {
					$pc_html .= sprintf( "%02X" , ( $giColor[ $i_jj ] -> { iStart } + int( $giColor[ $i_jj ] -> { iDif } * $pi_per[ $i_ii ] / 100 ) ) );
				}
				$pc_html .= '"';
			}
			$pc_html .= '>';
			$pc_tmp = substr( $gpcKey{ $gpcItemString } , $i_ii , 2 );
			if ( $pc_tmp =~ m/[\201-\237\340-\374][\100-\176\200-\374]/ ) {
				$pc_html .= $pc_tmp;
				$i_ii ++;
			}
			else {
				$pc_tmp = substr( $gpcKey{ $gpcItemString } , $i_ii , 1 );
				$pc_tmp =~ s/&/&amp;/g;					# '&' → "&amp;"
				$pc_tmp =~ s/</&lt;/g;					# '<' → "&lt;"
				$pc_tmp =~ s/>/&gt;/g;					# '>' → "&gt;"
				$pc_tmp =~ s/"/&quot;/g;				# '"' → "&quot;"
				$pc_html .= $pc_tmp;
			}
			if ( $gpcKey{ $gpcItemDir } == 2 ) {
				$pc_tmp = substr( $gpcKey{ $gpcItemString } , $i_ii + 1 , 2 );
				if ( $pc_tmp =~ m/[\201-\237\340-\374][\100-\176\200-\374]/ ) {
					if ( ( $pc_tmp eq '゜' ) || ( $pc_tmp eq '゛' ) || ( $pc_tmp eq '’' ) ) {
						$pc_html .= $pc_tmp;
						$i_ii += 2;
					}
				}
				else {
					$pc_tmp = substr( $gpcKey{ $gpcItemString } , $i_ii + 1 , 1 );
					if ( ( $pc_tmp eq 'ﾟ' ) || ( $pc_tmp eq 'ﾞ' ) || ( $pc_tmp eq '\'' ) ) {
						$pc_html .= $pc_tmp;
						$i_ii ++;
					}
				}
			}
			$pc_html .= '</Font>';
			if ( $gpcKey{ $gpcItemDir } == 2 ) {
				$pc_html .= '<Br>';
			}
		}
	}
	if ( $i_close_f ) {
		$pc_html .= '</Font>';
	}

	print <<"__DISP_GRADATION__";
Content-type: text/html

<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=shift_jis">
<meta http-equiv="cache-control" content="no-cache">
<TITLE>ｸﾞﾗﾃﾞｰｼｮﾝ作成★</TITLE>
</HEAD>

<BODY>
<Hr>
<center>ｸﾞﾗﾃﾞｰｼｮﾝ作成★</center>
<Hr>
できあがり★II<Br>
$pc_html<Br>
<Form Action="sss" method="get">
<textarea class=input name=com rows="5" cols="20" size="30">$pc_html</textarea>
</P>
</FORM>
<Br>
<FORM METHOD="POST" ACTION="gr.cgi">
■ 変換文字列 ｾ<Br>
<INPUT TYPE="TEXT" NAME="w" SIZE="30" value="$gpcKey{ $gpcItemString }"><Br>
■ ｽﾀｰﾄカラー [<Br>
#<INPUT TYPE="TEXT" NAME="st" SIZE="6" istyle="3" value="$gpcKey{ $gpcItemFntColBegin }"><Br>
■ ﾌｨﾆｯｼｭカラー ]<Br>
#<INPUT TYPE="TEXT" NAME="fn" SIZE="6" istyle="3" value="$gpcKey{ $gpcItemFntColEnd }"><Br>
<INPUT TYPE="SUBMIT" VALUE="変換するﾚ">
</FORM>
<Hr>
<Font Size="1"><div align="right"><A Href="http://cbox.ryne.jp">C-BoX</A> &#43;r[t](Perl)
</BODY>
</HTML>
__DISP_GRADATION__

	$i_ret = 0;

L_EXIT:;

	return $i_ret;
}

#
#
#
sub iCheckItems {
	my( $i_ret );
	my( $i_ii );

	if ( length $gpcKey{ $gpcItemString } == 0 ) {
		$i_ret = -1;
		goto L_EXIT;
	}
		$_ = $gpcKey{ $gpcItemFntColBegin };
		s/[0-9A-Fa-f]//g;
		if ( length != 0 ) {
			$i_ret = -2;
			goto L_EXIT;
		}
		if ( length $gpcKey{ $gpcItemFntColBegin } != 6 ) {
			$i_ret = -3;
			goto L_EXIT;
		}
		$_ = $gpcKey{ $gpcItemFntColEnd };
		s/[0-9A-Fa-f]//g;
		if ( length != 0 ) {
			$i_ret = -4;
			goto L_EXIT;
		}
		if ( length $gpcKey{ $gpcItemFntColEnd } != 6 ) {
			$i_ret = -5;
			goto L_EXIT;
		}

	if ( $gpcKey{ $gpcItemBackColSelect } == 1 ) {
		$_ = $gpcKey{ $gpcItemBackColVal };
		s/[0-9A-Fa-f]//g;
		if ( length != 0 ) {
			$i_ret = -6;
			goto L_EXIT;
		}
		if ( length $gpcKey{ $gpcItemBackColVal } != 6 ) {
			$i_ret = -7;
			goto L_EXIT;
		}
	}
	else {
		$gpcKey{ $gpcItemBackColVal } = $gpiColor[ $gpcKey{ $gpcItemBackColNo } ];
	}

	$giFont[ 0 ] = $gpcKey{ $gpcItemFontBegin };
	$giFont[ 1 ] = $gpcKey{ $gpcItemFontEnd };
	if ( $giFont[ 0 ] == 0 && $giFont[ 1 ] != 0 ) {
		$giFont[ 0 ] = $giFont[ 1 ];
	}
	elsif ( $giFont[ 0 ] != 0 && $giFont[ 1 ] == 0 ) {
		$giFont[ 1 ] = $giFont[ 0 ];
	}
	$gpcKey{ $gpcItemFntColBegin } =~ tr/a-f/A-F/;
	$gpcKey{ $gpcItemFntColEnd } =~ tr/a-f/A-F/;
	$gpcKey{ $gpcItemBackColVal } =~ tr/a-f/A-F/;

	for( $i_ii = 0 ; $i_ii < 3 ; $i_ii ++ ) {
		$giColor[ $i_ii ] -> { iStart } = hex( substr( $gpcKey{ $gpcItemFntColBegin } , $i_ii * 2 , 2 ) );
#		$giColor[ $i_ii ] -> { iEnd } = hex( substr( $gpcKey{ $gpcItemFntColEnd } , $i_ii * 2 , 2 ) );
		$giColor[ $i_ii ] -> { iDif } = hex( substr( $gpcKey{ $gpcItemFntColEnd } , $i_ii * 2 , 2 ) ) - $giColor[ $i_ii ] -> { iStart };
	}

	$i_ret = 0;

L_EXIT:;
	return $i_ret;
}

#
#
#
sub vDispError {
	my( $iError ) = $_[ 0 ];
	my( $pc_message );

	if ( $iError == -1 ) {
		$pc_message = '';
	}
	elsif ( $iError == -2 ) {
		$pc_message = 'ｽﾀｰﾄカラーの指定が変です';
	}
	elsif ( $iError == -3 ) {
		$pc_message = 'ｽﾀｰﾄカラーの指定が変です';
	}
	elsif ( $iError == -4 ) {
		$pc_message = 'ﾌｨﾆｯｼｭカラーの指定が変です';
	}
	elsif ( $iError == -5 ) {
		$pc_message = 'ﾌｨﾆｯｼｭカラーの指定が変です';
	}
	elsif ( $iError == 1 ) {
		$pc_message = '';
	}

	print <<"__DISP_ERROR__";
Content-type: text/html; charset=x-sjis

<HTML>

<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=shift_jis">
<meta http-equiv="cache-control" content="no-cache">
<TITLE>ｸﾞﾗﾃﾞｰｼｮﾝ作成★</TITLE>
</HEAD>
<Body>
<Hr>
<center>ｸﾞﾗﾃﾞｰｼｮﾝ作成★</center>
<Hr>

<FORM METHOD="POST" ACTION="gr.cgi">
■ 変換文字列 ｾ<Br>
<INPUT TYPE="TEXT" NAME="w" SIZE="30" value="$gpcKey{ $gpcItemString }"><Br>
■ ｽﾀｰﾄカラー [<Br>
#<INPUT TYPE="TEXT" NAME="st" SIZE="6" istyle="3" value="$gpcKey{ $gpcItemFntColBegin }"><Br>
■ ﾌｨﾆｯｼｭカラー ]<Br>
#<INPUT TYPE="TEXT" NAME="fn" SIZE="6" istyle="3" value="$gpcKey{ $gpcItemFntColEnd }"><Br>
<INPUT TYPE="SUBMIT" VALUE="変換するﾚ">
</FORM>
<Br>
$pc_message
<Hr>
<Font Size="1"><div align="right"><A Href="http://cbox.ryne.jp">C-BoX</A> &#43;r[t](Perl)
</BODY>
</HTML>
__DISP_ERROR__
}

#
#
#
sub main {
	my( $i_result );

	#
	# ﾌﾟﾛｸﾞﾗﾑの初期化
	#
	&iReadParse;

		if ( $i_result = &iCheckItems ) {
			&vDispError( $i_result );
		}
		elsif ( $i_result = &iDispGradationHtml ) {
			&vDispError( $i_result );
		}
	
	
}

&main;
