一般來說, 每種基本字體, 都會有在其基礎上變化字形的附加字體。比如,字體Arial, 就有其附加字體Arial Bold (粗體), Arial Italic(斜體), 及Arial Bold Italic(粗斜體)。一般你都可以找到或購買到相應的附加字體。
但有時為了應急,或對字體字形沒有非常嚴格的要求。在這樣的情況下,我們可以采用人工字形生成(Artificial font styles)。Artificial font styles是Acrobat的一個功能,它根據基本字形而模擬生成粗體,斜體及粗斜體。PDFlib支持這一功能,並遵守Acrobat對此功能的限制。目前此功能之局限於:
1.Acrobat標准字體, 就簡體中文來說也就是PDFlib自帶的STSong-Light,AdobeSongStd-Light-Acro,及STSongStd-Light-Acro三種簡體中文字體。
2.PDFlib可以訪問的.otf OpenType字體,並使用表1.1的編碼(見), 且“embedding”參數設為假。
下面是一個相關的例子--C 源程序(附上生成的pdf文件 –PDFlib_cs5.pdf)。
/*******************************************************************/ /* This example demostrates the usage of Artificial font styles /* under Chinese Simplifed Windows. /*******************************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "pdflib.h" int main(void) { PDF *p = NULL; int Font_H = 0, Font_CS = 0, Left = 50, y = 700; const int INCRY = 25; const char TextUnicode[] = "\x80\x7B\x53\x4F\x2D\x4E\x87\x65"; const int TEXTLEN = 8; /* create a new PDFlib object */ if ((p = PDF_new()) == (PDF *) 0) { printf("Couldn''t create PDFlib object (out of memory)!\n"); return(2); } PDF_TRY(p) { if (PDF_begin_document(p, "pdflib_cs5.pdf", 0, "") == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); return(2); } PDF_set_info(p, "Creator", "pdflib_cs5.c"); PDF_set_info(p, "Author", "[email protected]"); PDF_set_info(p, "Title", "Usage of Artificial font styles"); /* Start a new page. */ PDF_begin_page_ext(p, a4_width, a4_height, ""); Font_H = PDF_load_font(p, "Helvetica-Bold", 0, "winansi", ""); PDF_setfont(p, Font_H, 24); PDF_show_xy(p, "Artificial Font Styles", Left + 100, y); /* Normal */ y -= 2 * INCRY; PDF_setfont(p, Font_H, 14); PDF_show_xy(p, "Normal", Left, y); y -= INCRY; Font_CS = PDF_load_font(p, "STSong-Light", 0, "UniGB-UCS2-H", ""); PDF_setfont(p, Font_CS, 14); PDF_show_xy2(p, TextUnicode, TEXTLEN, Left, y); /* Italic */ y -= 2 * INCRY; PDF_setfont(p, Font_H, 14); PDF_show_xy(p, "Italic", Left, y); y -= INCRY; Font_CS = PDF_load_font(p, "STSong-Light", 0, "UniGB-UCS2-H", "fontstyle italic"); PDF_setfont(p, Font_CS, 14); PDF_show_xy2(p, TextUnicode, TEXTLEN, Left, y); /* Bold */ y -= 2 * INCRY; PDF_setfont(p, Font_H, 14); PDF_show_xy(p, "Bold", Left, y); y -= INCRY; Font_CS = PDF_load_font(p, "STSong-Light", 0, "UniGB-UCS2-H", "fontstyle bold"); PDF_setfont(p, Font_CS, 14); PDF_show_xy2(p, TextUnicode, TEXTLEN, Left, y); /* Bold-italic */ y -= 2 * INCRY; PDF_setfont(p, Font_H, 14); PDF_show_xy(p, "Bold-italic", Left, y); y -= INCRY; Font_CS = PDF_load_font(p, "STSong-Light", 0, "UniGB-UCS2-H", "fontstyle bolditalic"); PDF_setfont(p, Font_CS, 14); PDF_show_xy2(p, TextUnicode, TEXTLEN, Left, y); /* End of page. */ PDF_end_page_ext(p, ""); PDF_end_document(p, ""); } PDF_CATCH(p) { printf("PDFlib exception occurred in pdflib_cs5 sample:\n"); printf("[%d] %s: %s\n", PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p)); PDF_delete(p); return(2); } PDF_delete(p); return 0; }
本文配套源碼