Android graphics library Skia (4)-generate PDF, androidskia

Source: Internet
Author: User
Tags skia

Android graphics library Skia (4)-generate PDF, androidskia

Android graphics library Skia (4)-generate PDF


This article mainly records the process of using the skia library to generate PDF files. In fact, skia is not only available in Android systems, but can also be used in General Embedded Linux systems. The title is written in the previous format. This article uses the Android graphics library Skia (1)-Basic test to generate PNG images and the Android graphics library Skia (3)-combined with Freetype to display text. Example:

/* Simple PDF demo utilizing Skia toolkit.

* Authored by kangear <kangear@163.com>

*/

 

# Include "SkBitmap. h"

# Include "SkDevice. h"

# Include "SkPaint. h"

# Include "SkRect. h"

# Include "SkImageEncoder. h"

 

 

# Include "skypcatalog. h"

# Include "skeydevice. h"

# Include "skeystream. h"

# Include "skpolictypes. h"

# Include "skeydocument. h"

# Include <stdio. h>

# Include <unistd. h>

# Include "SkTypeface. h"

 

// Draw_content ()

 

Int main ()

{

// Declare a raster bitmap, which has an integer width and height,

// And a format (config), and a pointer to the actual pixels.

// Bitmaps can be drawn into a SkCanvas, but they are also used

// Specify the target of a SkCanvas 'Drawing operations.

SkBitmap bitmap;

Bitmap. setConfig (SkBitmap: kARGB_8888_Config, 200,200 );

Bitmap. allocPixels ();

 

// PDF

Skeydevice * dev = new skeydevice (827,116 9 );

SkAutoUnref aur (dev );

 

// A Canvas encapsulates all of the state about drawing into

// Device (bitmap). This includes des a reference to the device itself,

// And a stack of matrix/clip values. For any given draw call (e.g.

// DrawRect), the geometry of the object being drawn is transformed

// By the concatenation of all the matrices in the stack.

// Transformed geometry is clipped by the intersection of all of

// Clips in the stack.

SkCanvas canvas (dev );

 

// SkPaint class holds the style and color information about how

// Draw geometries, text and bitmaps.

SkPaint paint;

 

// SkIRect holds four 32 bit integer coordinates for a rectangle.

 

SkRect r;

 

Paint. setARGB (255,255, 0, 0 );

R. set (25, 25,145,145 );

Canvas. drawRect (r, paint);/** Draw the specified rectangle using

The specified paint. The rectangle

Will be filled or stroked based on

The Style in the paint .*/

 

Paint. setARGB (255, 0,255, 0 );

R. offset (20, 20 );

Canvas. drawRect (r, paint );

 

Paint. setARGB (255, 0, 0,255 );

R. offset (20, 20 );

Canvas. drawRect (r, paint );

Const char gText [] = "123 flat 34 Road 12! ";

// SkTypeface * font = SkTypeface: CreateFromFile ("DroidSansFallbackFull. ttf ");

// Usr/share/fonts/truetype/msttcorefonts/DroidSansFallbackFull. ttf

SkTypeface * font = SkTypeface: CreateFromName ("Droid Sans Fallback", SkTypeface: kNormal );

If (font)

{

Paint. setARGB (255,255, 0, 0 );

Paint. setTypeface (font );

Paint. setTextSize (25 );

Canvas. drawText (gText, sizeof (gText)/sizeof (gText [0]), 0, 25, paint );

}

Else

{

Printf ("font = null! \ N ");

}

 

// SkImageEncoder is the base class for encoding compressed images

// From a specific SkBitmap.

SkImageEncoder: EncodeFile ("snapshot.png", bitmap,

SkImageEncoder: kPNG_Type,

/* Quality ranges from 0 .. 100 */100 );

 

// Save as a pdf file

Skeydocument doc;

Doc. appendPage (dev );

SkDynamicMemoryWStream performance_stream;

Doc. emitPDF (& pai_stream );

Write (STDOUT_FILENO, pai_stream.getstream (), pai_stream.getoffset ());

 

Return 0;

}

 

/*

 

Build:

G ++ \

-I./include \

-I./include/core \

-I./include/images \

-I./include/config \

-I./include/pdf /\

-Wall-o test-skia./test-skia.cpp \

Out/src/images/SkImageDecoder_libpng.o \

Out/libskia. \

'Pkg-config freetype2 -- libs -- cflags '\

'Pkg-config libpng -- libs -- cflags '\

-Lpthread-g

 

Run:

./Test-skia> testbench

 

*/

Effect:

 

Note: When generating a PDF file, if you use the SkTypeface: CreateFromFile method to create a font face, it is a built-in font, And the font in the PDF file cannot be properly displayed in other environments. When Adobe reader is used, the following message is displayed:

 

Text Version:

Cannot extract the embedded font 'droidsansfallback'. Some characters may not display or print correctly.

 

Reference:

1. https://code.google.com/p/skia/wiki/PDFTheoryOfOperation

2. List_of_typefaces

3. PDFReference




Google open-source library skia, a relatively new graphics library, uses the skia library API to resize images? How to Use
After using Fang zhengfei to generate a pdf file, use photoshop to process the pdf file. The text (black text) in the pdf file is displayed in four colors. Why? What should I do?

Do not use 正 PDFCreator. The black color generated with this character is single-black.

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.