Automatic selection of Chinese and English fonts using ASP

Source: Internet
Author: User
Tags define definition html tags return client
Using ASP to realize the automatic selection of Chinese and English fonts











in Windows systems, the display of characters for each language has a default font, for example, in most applications, the default display font for the Simplified Chinese (GB2312) character is "XXFarEastFont-Arial", and the English character is the word. Using this default font can not only take care of most of the client system does not appear to lack such a font embarrassing situation, it is important to achieve this type of character display and printing to the maximum of the neat and beautiful, and people's reading habits consistent.


Ask questions


in Word 97/2000, the default font style for document body is: The font of Chinese characters is XXFarEastFont-Arial (English, GB2312, or GBK), and the English character's font is "Times New Roman". In fact, GB2312 encoded in the song font in itself contains the English character display and print support, but why does word have to set up another? As shown in Figure 1: The font for the first line of English letters in the figure is set to "Times New Roman" and the second line is set to "XXFarEastFont-Arial".








Figure 1


from Figure 1 can be seen, is set to the song body English character display effect is close to the Simplified Chinese pinyin, as the English paragraph this setting is not very consistent with people's reading habits, so word of the Chinese and English fonts are not superfluous.


in the Simplified Chinese web page, the font settings for the text (<body>......</body>) that the browser defaults to or are specified by the Web page creator are generally sung, so that the same font settings in both English and Chinese will make the font display lose the overall beauty.


in order to solve this problem, we can make a page of all the English characters that appear in the font style settings, but this will not only greatly aggravate the work of the Web page production staff, so that the code looks cumbersome procrastination, but also greatly increase the length of the page code, resulting in slow page download. If you set the font for the main part of the entire page to the English font style, such as "Times New Roman" and so on, in this case, although the browser in the display of Chinese characters will automatically use the default font settings, but the same page in different settings of the browser will be an unexpected folding line , paragraph confusion, and so on display error.


Programming Implementation


This article describes an automatic selection method for Chinese and English fonts implemented in ASP using server-side JScript scripts. This method can not only easily achieve the effect in Word, but also to make the client output of the HTML source code more compact and concise. The source code and a brief description are as follows:


<%@ language=jscript%>


<html>


<head>


<meta http-equiv= "Content-type" content= "text/html"; charset=gb2312 ">


<meta name= "Author" content= "contion" >


<title> Auto-Select Font Script test page </title>


<script language= "JScript" runat= "Server" >/* If you want to use this function in client-side HTML (that is, normal HTML files), set "language" to "JavaScript", And the runat= "Server" Removed * *


//auto-select font function


function Cwrite (strput)


{ 


var Strinput,intlength,blntagclosed,blnhtmltags,strwrite,chrati,argv,argcstrclass;


strinput= ' +strput;


intlength=strinput.length;


blntagclosed=true;


Blnhtmltags=false;


strwrite= ';


argv=cwrite.arguments;


Argc=argv.length;


strclass= (argc>1)? Argv[1]: ' en '; /* If not with parameters, then the default English style for this page CSS style definition of en. The definition of a CSS style sheet can refer to the code below. */


/* Returns NULL if the input string is "null"/*


if (strput==null) return strwrite;


for (var i=0;i<intlength;i++)


{


Chrati=strinput.charat (i);


if (Strinput.charat (i-1) = = ' > ' && blnhtmltags==true) blnhtmltags=false; /* hit ">", then think the current character is not an HTML tag. HTML tags are also english characters, but it makes no sense to set them in font, because the HTML tags themselves are not displayed. */


/* hit "<", HTML tag start * *


if (chrati== ' < ') blnhtmltags=true;


if (chrati!= ')


{


//Filter Space


if (IFWESTERNCHR (Chrati) &&!blnhtmltags)


{ 


/* If it is an English character and is not within the HTML tag * *


if (blntagclosed) {//* * * can be added <font> modified only if the <font>......</font> tag is complete


strwrite+= ' <font class= ' +str


class+ ' "> ' +chrati;


Blntagclosed=false;


}


else if (!blntagclosed) Strwrite+=chrati;


}


else{


if (!blntagclosed)


{


strwrite+= ' </font> ' +chrati;


//<font> mark to "closed"


blntagclosed=true;


}


else if (blntagclosed) Strwrite+=chrati;


}


}


else Strwrite+=chrati;


}


if (!blntagclosed) strwrite+= ' </font> '


//Returns the font-decorated HTML string


return strwrite;


}


/* Determines whether the current character is an English or Chinese character/*


function Ifwesternchr (chrinput)


{ 


var chrinput_e;


Chrinput_e=escape (chrinput);


if (chrinput_e.length== (chrinput.length) *6 | | chrinput== ' \ r ' | | chrinput== ' \ n ') return false; * * Only non-normal ASCII characters after the escape () function encoded length will be equal to the original length of 6 times times, so this method can avoid the internal code directly determine whether the current character is Chinese. */


else return true;


}


</script>


<style>


<!--The following CSS styles for reference only. Note: En is the default English font style class for the program, so be sure to define it.


body{color: #000080;


font-family: Song body; FONT-SIZE:12PT}


. En {color: #ff0000;


font-family: "Times New Roman";


Font-weight:bold;


Font-style:italic;


font-size:12pt}


-->


</style>


</head>


<body bgcolor= "#ffffff" >


<%


/* Below is a call example that you can refer to in the following way to invoke the function written above. When referencing in an HTML file, be careful to replace all ASP functions and objects in this example in the form of the corresponding client script, such as "Response.Write" should be replaced with "document.write"/


var strwrite;


strwrite= ' In this test page, the font for all half-width characters is set to times New Roman and is shown in bold red italic. ’;


strwrite+= ' Of course, this is just for testing convenience and eye-catching, you should define the appropriate font for yourself. ’;


strwrite+= ' such as: "Computer", "World Wide Web" and so on, "?" Is the English question mark, "," is the English comma, ';


strwrite+= ' "1234567890" is the numeric display style for this font. This looks like the Chinese ellipsis "..." and the English ellipsis ';


strwrite+= ' ... ' is really different. Medium: Is this test correct? En:is This test correct? ';


Response.Write (' <p> ' +cwrite (strwrite, ' en ') + ' </p> ');


%>


</body>


</html>














Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.