Freetype-go Learning

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Freetype-go's source is here https://code.google.com/p/freetype-go/

Its role is to produce PNG images with text

A few concepts to solve first:

What is FreeType?

FreeType is a portable, efficient font engine.

Fonts appear on your computer in two ways: Dot Matrix and vector. For one word, bitmap fonts hold the rendering information for each point. The disadvantage of this approach is that the amount of data stored is very large, and the operation support for zooming out is not good. Therefore, vector fonts appear. For one word, vector fonts are saved with the word's drawing formula. This drawing formula includes the font outline (outline) and the font refinement (hint). The font outline uses Bezier curves to draw the outer lines of the word. In the case of large resolution, the font needs to be fine-tuned. The formula for the drawing word is called the font data (glyph). In a font file, each word corresponds to a glyph. There is a character mapping table (CHARMAP) in the font file.

For vector fonts, TrueType is the most widely used. Its extension is typically OTF or TTF. have been widely supported on the WINDOWS,LINUX,OSX. The font files that we see in the. TTF and. TTC are TrueType fonts. Where TTC is a collection file (collection) for multiple TTF.

Steps

TrueType is just a font, and to make the font appear on the screen, you need a font-driven library. Where FreeType is such an efficient font-driven engine. A Chinese character from font to display FreeType roughly a few steps:

Loading fonts

Set Font size

Load Glyph

Conversion of font size, etc.

Drawing fonts

It is particularly important to note that FreeType does not only drive TrueType fonts, it can also drive a variety of other vector fonts, and can even drive bitmap fonts.

Freetype-go

So freetype-go is using the go language to achieve the FreeType drive.

This is a implementation of the Freetype font engine in the Go programming language.

code example:

Draw a picture with text func (this *signer) drawstringimage (text string) (image. Image, error) {fontbytes, err: = Ioutil. ReadFile (This.fontpath) if err! = Nil {return nil, err} font, err: = FreeType. Parsefont (fontbytes) if err! = Nil {return nil, err} FG, bg: = image. White, image. Black Rgba: = image. Newrgba (image. Rect (0, 0,.)) draw. Draw (Rgba, Rgba. Bounds (), BG, image. ZP, Draw. SRC) C: = FreeType. Newcontext () c.setdpi (this. DPI) C.setfont (font) c.setfontsize (this. FontSize) C.setclip (Rgba.    Bounds ()) C.SETDST (RGBA) c.setsrc (FG)//Draw the text. PT: = FreeType. Pt (10+int (c.pointtofix32 () >>8)) for _, S: = Range strings. Split (text, "\ r \ n") {_, Err = C.drawstring (S, PT) pt. Y + = c.pointtofix32 (1.5)} return Rgba, nil}

Reference Documentation:

http://blog.csdn.net/absurd/article/details/1354499

https://developer.apple.com/fonts/TTRefMan/

Http://www.microsoft.com/typography/otspec/otff.htm

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.