Text and font control tutorials for the jpgraph Chinese user manual

Source: Internet
Author: User
Tags italic font italic font style

Summary: The installation and configuration of jpgraph fonts has been described in the previous PHP jpgraph installation configuration tutorial, and the use of fonts and text in Jpgraph class libraries is important, and jpgraph can control the rotation, alignment, and font size of text. You can also introduce custom fonts that users need. With the Jpgraph tool class Symchar you can find relevant special characters (such as mathematical operators) to facilitate the chart ... The installation and configuration of jpgraph fonts has been described in the previous PHP jpgraph installation configuration tutorial, and the use of fonts and text in the Jpgraph class library is important, and jpgraph can control the rotation, alignment, font size of text, You can also introduce custom fonts that users need. The Jpgraph tool class Symchar can be used to find relevant special characters (such as mathematical operators) to facilitate the text description of the chart. The Jpgraph class library supports the configuration of character encodings in Chinese, Japanese, and English to accommodate different locales.

Before you begin this tutorial make sure that you have installed TTF fonts that are supported, refer to the Jpgraph font installation and configuration method tutorial.

For better display quality and the ability to display all characters, it is recommended to use TTF fonts instead of bitmap fonts.

All files in the Jpgraph class library are UTF-8 encoded.

  Different types of font descriptions

The class library supports two basic types of fonts

1.Bitmap Font

There are three built-in bitmap fonts, which are ff_font0,ff_font1 and Ff_font2. The advantage of bitmap fonts is that they are always ready to use after installing GD. However, bitmap fonts only support 7-bit ASCII, so it is not possible to use bitmap fonts if you need to display any characters. The available bitmap font size is also restricted, with three corresponding fonts, ff_font0 is the smallest font, FF_FONT2 is the largest available bitmap font.

  jpgraph Font Usage Example : The following script shows a typical bitmap font usage

1
2
3
4
5
6
<?php
$graph = new Graph (...);

Adjust the title to use the largest built-in bitmap font and bold
$graph->title->setfont (Ff_font2,fs_bold);
?>

2.TTF Font

TTF fonts provide better visual quality for text. TTF fonts support all sizes, while there are more fonts to choose from. All font families except three bitmap fonts are TTF fonts. In order to use these fonts, the installation of fonts must be configured according to the requirements of the class library.

  jpgraph TTF Font usages: The following script shows a typical use of a graphical caption using Arial TTF fonts

1
2
3
4
5
6
<?php
$graph = new Graph (...);

Adjust the title size to 14 and use Arial font to bold at the same time
$graph->title->setfont (ff_arial,fs_bold,14);
?>

  Font family and font type

  Default TTF Font

Prior to version 3.5, bitmap fonts were used by default and can be specified from ff_font0 to Ff_font2. Starting with version 3.5, the default font is now set to open source TTF Dejavu font (FF_DV_SANSSERIF, Dejavusans.ttf). This TTF font is contained in the Jpgraph source code (Src/font) and does not require a single font to be specified.

You can change the default font by changing the Ff_default value defined in the jpgraph_ttf.inc.php file. In order to use another font you must be prepared for the corresponding font file.

If the FreeType library is not supported in your PHP environment configuration, you can use the original bitmap font to ensure compatibility.

All graphical objects that use text allow you to specify a font by calling the SetFont () method, which has three parameters.

1, font series . For example, Arial,times Roman ttf font, or you can use a medium-sized bitmap font when using bitmap fonts. The font family must be specified by the prefix ff_. For all supported font families, refer to the Latin font Family specification chart supported in table 8.2 in the Jpgraph English help document.

2, font style . That is, italic, bold, or bold italic. All font styles are the Normal style by default. Font styles can be specified by prefix fs_. Supported font styles are fs_normal (normal font style), Fs_bold (bold font style), fs_italic (italic font style), fs_bolditalic bold italic style font (requires font family support only)

3, font size . Integer form and in pixels PT

  Note : Depending on the version of the Dejavu font installed on the system, there are two naming conventions for font files. The Jpgraph class library tries to see whether the font file can be found in either of these naming methods.

  Understanding text alignment and anchor points

When the text string specifies the position of the screen, the default position is the top-left corner of the string bounding box that can be obtained aligned. The upper-left corner is the anchor point for the text string. Anchor alignment can be adjusted by calling the Text::setalignment ($aHorAlign, $vertAlign) method. Two parameters as a text string, the values of the parameters that can be accepted are as follows

  Horizontal alignment : ' Left ', ' center ', ' right '

  Vertical alignment : ' Bottom ', ' center ' (or ' middle '), ' top '

  Note : Even if you look at bitmap fonts and TTF fonts from an API perspective, sharing the same user API interface is different. There are a number of subtle differences in how built-in bitmap fonts and TrueType fonts are rendered on the screen.

Manually setting anchor alignment is most useful when adding text objects to the chart to achieve the desired alignment effect. You can see an example of how an anchor point changes


Text alignment supported by Jpgraph


  Note : The Setalignment () method may seem strange, and its true meaning is to use the Setanchor () method. This is due to the historical cause of the naming scheme.

  Rotate text with Jpgraph

Bitmap fonts and TTF fonts support varying degrees of rotated text. Bitmap fonts may only use horizontal or vertical text, such as 0-degree or 90-degree rotations. TTF fonts support any angle.

If you use the bitmap font and specify an angle other than 0 or 90 degrees, an error will be displayed.

The most common use of rotated text is to adjust the x-axis labels so that they appear at a 45-degree angle. To rotate the label, you need to use Axis::setlabelangle ().

In addition to the label axis, you can rotate almost any text object except the chart title, which can only be horizontal. For text objects to be added anywhere on the chart, you need to use the Text::setangle () method to specify the desired text angle. Another common place is when a single data point is labeled with a text label that can be rotated.

  Note : When you rotate a paragraph of text, the alignment within the paragraph has been reset to left. It is not possible to use center or right paragraph alignment in rotated text.

  Formatting text paragraphs

The text rendering engine in the Jpgraph class library provides some basic text paragraph formatting.

1, Jpgraph class library support multi-line text as a paragraph

2. The Jpgraph class Library can adjust the alignment of text within a paragraph: left, right, or centered.

3. If text rendering starts, it will support line wrapping.

All word processing is done through the word processing class (defined in the jpgraph_text.inc.php file), which can be used to add any text to the chart or to manipulate the text on labels and headings inside the class library. All of this text is an instance of the text class.

The alignment control of a paragraph is mainly done through the Text::setparagraphalign ($aAlignment) method. Arguments are text strings ' left ', ' right ', one of ' center '

The Jpgraph class library does not support the alignment of kerning between individual characters in a paragraph. Because the complexity is higher.

The paragraph has different alignment


Jpgraph supported paragraphs are aligned differently


Inserting a newline character "\ n" in the text causes the line of text to wrap and start the next line. Note that line breaks must be in double quotes, not single quotes.

Bitmap font rotation, such as vertical, does not support automatic line wrapping. The TTF font must be used if the vertical text needs to be wrapped.

  How to add a commonly used TTF font

In addition to the predefined fonts, the Jpgraph class library makes it easy to use three of custom fonts. This is done by first specifying the font file name that you want to use, and then specifying one of the Ff_userfont1,ff_userfont2 or Ff_userfont3 font families. Installing a new font is accomplished by invoking one or more methods.

Graph::setuserfont1 ($aNormal, $aBold, $aItalic, $aBoldIt) (function Setuserfont () with the same name)

Graph::setuserfont2 ($aNormal, $aBold, $aItalic, $aBoldIt)

Graph::setuserfont3 ($aNormal, $aBold, $aItalic, $aBoldIt)

The parameters for these methods must be a font file name that contains the full path. All parameters except $anormal are optional.

Specify and install an instance of the user-specified font as follows

1
2
3
4
5
6
7
8
9
10
11
<?php
// ...
$graph->setuserfont ('/usr/share/fonts/ttf/digital.ttf ');

$graph->title->setfont (ff_userfont,fs_normal,12);

$graph->title->set (' Test title '. $pi);

// ...

?>

  Inserting Unicode Entities

One reason to use TTF fonts is that you may be able to insert Unicode characters/entities. With this, the characters in the extended range that are not normally available on the keyboard in Western Europe can be used.

For example, classic mathematical symbols, such as the Greek alphabet "PI". To make these and other commonly used special characters easier to use, the Jpgraph Class Library provides a tool class that makes it easy to use these characters without having to find the appropriate Unicode entity each time.

The usual way to specify those characters that are not available on the keyboard is to specify Unicode code, which needs to include a prefix in the text string. For example, the Unicode character pi is the hexadecimal 03c0, in the string you need to write: This is Pi pi & #0960. Note that the decimal encoding code given in the string always uses 4 bits (pre-populated 0 if necessary). The Jpgraph class library provides an easy way to Symchar classes because of the tedious need to find the encoding of special characters.

  Tool Class Symchar

Because it is tedious to find the corresponding values for all character codes, the Symchar class allows you to find these characters in common English names in order to simplify these tasks. For example, create a Greek character pi, which can be implemented

1
2
3
4
5
6
7
8
<?php
// ...
$pi = Symchar::get (' pi ');

$graph->title->set (' Test is pi '. $pi);

// ...
?>

The entities that correspond to all symbolic names supported by the Symchar class are listed in Table 8.3.

Symchar::get () The first parameter should be the name of the entity.

  Character encoding

If you do not use Japanese, Chinese, Cyrillic, Greek or Hebrew languages, this section can be skipped.

The core problem with the Jpgraph class library is that it does not know what encoding the input string is. Because of the need for TTF fonts, it is very useful to tell the class library to enter the encoding so that the class library can do the necessary character encoding conversions to produce UTF-8 or UTF-16 encoding. The main supported locale encoding options are explained below.

The files for the Jpgraph library and all instances by default are UTF-8 encoded.

All of the below mentioned can be found in the jpgraph_ttf.inc.php file.

  Japanese encoding options

Only one of the possible options can be specified.

Assume_eucjp_encoding True/false assumes that you have entered Japanese text in the EUC-JP encoding environment. If this definition is true, then using PHP's mbstring library in the Jpgraph class library can automate the conversion from EUC-JP to UTF8. Note that multi-byte extensions in PHP are not typically enabled.

Otherwise, the input character encoding is assumed to be using UTF-8. Note that displaying Japanese characters (Kanji, hiragana, and Katakana) must specify a Japanese font family (Ff_mincho, Ff_pmincho, ff_gothic, or ff_pgothic)

  Chinese character options

If the font is specified as a built-in Ff_simsun for the library, the GB2312 is converted to UTF-8. The conversion table is stored in the file jpgraph_gb2312.inc.php.

If the font is specified as Ff_chinese, it is not converted, because assuming the input string is already UTF-8, only the font's default text system column is changed.

If the font is specified as Ff_big5, then it is assumed that the input string is BIG5 encoded, and that the internal conversion to UTF-8 encoding is done through the iconv () function. This means that PHP must have built-in support for Iconv (). By default, this is not compiled into PHP (some versions of PHP installed in Windows are already supported, UNIX needs to be compiled manually, and "–width-iconv" needs to be configured). If this method does not exist, the Jpgraph class library generates an error message.

  Cyrillic Encoding Options

In order to do the correct conversion, language_cyrillic must be defined as true, and if you are running the Jpgraph class library in a multiuser environment, you may need to adjust the langugae_charset option, which is defined as follows

Language_cyrillic true/false supports custom Unicode Cyrillic support.

Cyrillic_from_windows True/false If you set to True, the input character encoding is assumed to be Windows1251 encoded, and if False is assumed to be koi8-r.

Language_charset String This constant is used to automatically detect if the Cyrillic conversion is really necessary if enabled. Just specify the encoding you want to use, such as "Windows-1251".

Typical string UTF-8 or utf-8, this comparison is case insensitive. The conversion does not occur if the character set is not a derived character set of ' koi8-r ' or ' windows-1251′ '. This constant is very important in a multi-user, multi-lingual environment.

Usage:

1
2
3
<?php
Define (' Language_charset ', $locale _char_set);
?>

$locale _char_set is a global string variable.

  Greek character encoding options

To correct the translation from Greek to Unicode,language_greek should be specified as True

Language_greek True/false Whether the Greek language supports special Unicode characters

Greek_from_windows True/false If you set this definition to true, the conversion of the Greek character will assume that the input text is Windows1251.

At this point, the Jpgraph class library of text characters and font control instructions, the tutorial is finished, familiar with and master the font and text control method is necessary to use Jpgraph to draw a chart, I hope to use jpgraph friends to help.

Reprint please indicate source: jpgraph Chinese User manual text and font control tutorial
Http://www.php1.cn/Content/jpgraph_ZhongWenShiYongShouCeZhiWenBenHeZiTiKongZhiJiaoCheng.html

Text and font control tutorials for the jpgraph Chinese user manual

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.