轉帖|其它|編輯:郝浩|2011-01-28 11:22:02.000|閱讀 1481 次
概述:除了PDFlib自帶字體外,用戶還可以使用安裝在系統上的字體及其他用戶字體。本文主要介紹如何在PDFlib中使用其他簡體中文字體,希望對大家有幫助。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
除了PDFlib自帶字體外,用戶還可以使用安裝在系統上的字體及其他用戶字體。
PDFlib稱安裝在Windows和Mac操作系統中的(存在于或被拷入相應系統字體目錄的)TrueType, OpenType 和PostScript字體為宿主字體(Host Font)。PDFlib可直接引用字體名進行調用,但必須與文件名完全相同(嚴格區分大小寫)。例如,調用安裝在Windows系統中的字體:C:WINDOWSFontsSimHei.ttf
int Font_CS = 0;
Font_CS = PDF_load_font(p, "SimHei", 0, "unicode", "");
需要注意的是,字體名可能與字體文件名不同,甚至相同的字體在不同語言的操作系統下字體名稱會有所不同。在 Windows 環境下查看字體名,可雙擊該字體文件,窗口打開后的第一行字除結尾的 TrueType, OpenType 外為字體名。例如,調用安裝在 Windows 系統中的字體: C:WINDOWSFontsSimHei.ttf ,雙擊該文件后,窗口的第一行為“黑體 TrueType” 。則該文件的字體名為“黑體”。在 PDFlib 中若要調用多字節的文件名,須以 BOM+ UTF8 的形式。 “黑體”的 BOM+ UTF8 的形式為“ xEFxBBxBFxE9xBBx91xE4xBDx93 ”。
因此對于中文黑體, 在中文WINDOWS下,則我們使用
PDF_load_font(p, "xEFxBBxBFxE9xBBx91xE4xBDx93", 0, "unicode", "");
在英文WINDOWS下則應使用
PDF_load_font(p, "SimHei", 0, "unicode", "");
(小技巧: 我們可以使用Windows2000/XP自帶的notepad獲得UTF8編碼,具體方法舉例:在notepad中輸入"黑體"并保存, 保存時在編碼下拉框中選擇UTF-8, 然后用UltraEdit,WinHex,VC等可以進行二進制編輯的工具打開該文件即可取得帶BOM的UTF8字符串)
除安裝在Windows系統中的字體之外,PDFlib還可以調用其他用戶字體。但在調用之時,需要給出路徑名。如我想用C:Program FilesAdobeAcrobat 7.0ResourceCIDFontAdobeSongStd-Light.otf 這個字體:
Font_CS= PDF_load_font(p,
"C:\Program Files\Adobe\Acrobat 7.0\Resource\CIDFont\ AdobeSongStd-Light",
0, "unicode", "");
但這里有個例外,那就是.ttc(TrueType Collection)字體。.ttc是集合字體文件,每個文件中含有多種字體。所以用戶不能用文件名調用字體,而是要用真正的字體名。比方說,我們知道C:WINDOWSFontsMSGOTHIC.TTC 包含三種字體依次名為MS Gothic,MS PGothic,和MS UI Gothic。我們可以用以它們相應的字體名調用:
int Font_E = 0;
Font_E= PDF_load_font(p, "MS Gothic", 0, "winansi", ""); /* Use MS Gothic */
PDF_setfont(p, Font_E, 20);
PDF_show_xy(p, "MS Gothic font:" , 50, 800);
Font_E= PDF_load_font(p, "MS PGothic", 0, "winansi", ""); /* Use MS PGothic */
……
Font_E= PDF_load_font(p, "MS UI Gothic", 0, "winansi", ""); /* Use MS UI Gothic */
可是我們經常并不清楚.ttc里包含哪些字體。在這種情況PDFlib提供了另一種調用方式—索引(Index)。用此方式,首先須給字體文件名一個別名,然后在別名后加冒號再加數字(0表示文件中第一種字體,1 表示第二種,依次類推。)
int Font_E = 0;
/* Give “C:WINDOWSFontsMSGOTHIC.TTC an alias “gothic” */
PDF_set_parameter(p, "FontOutline", "gothic=C:\WINDOWS\Fonts\MSGOTHIC.TTC");
Font_E= PDF_load_font(p, "gothic:0", 0, "winansi", ""); /* Use MS Gothic */
Font_E= PDF_load_font(p, "gothic:1", 0, "winansi", ""); /* Use MS PGothic */
Font_E= PDF_load_font(p, "gothic:2", 0, "winansi", ""); /* Use MS UI Gothic */
下面是一個相關的例子--C 源程序
/*******************************************************************/
/* This example demostrates the usage of host font and other fonts
/* based on Chinese Simplifed Windows.
/*******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pdflib.h"
int main(void)
{
PDF *p = NULL;
int i = 0, j = 0, Left = 50, Top = 800;
int Font_E = 0, Font_CS = 0;
char fontfile[1024];
char buf[1024];
char TextUnicode[] = "x80x7Bx53x4Fx2Dx4Ex87x65";
/* create a new PDFlib object */
if ((p = PDF_new()) == (PDF *) 0)
{
printf("Couldn't create PDFlib object (out of memory)!
");
return(2);
}
PDF_TRY(p) {
if (PDF_begin_document(p, "pdflib_cs2.pdf", 0, "") == -1)
{
printf("Error: %s
", PDF_get_errmsg(p));
return(2);
}
PDF_set_info(p, "Creator", "pdflib_cs2.c");
PDF_set_info(p, "Author", "myi@pdflib.com");
PDF_set_info(p, "Title", "Output Chinese Simplify with host font and others");
/* Start a new page. */
PDF_begin_page_ext(p, a4_width, a4_height, "");
Font_E = PDF_load_font(p, "Helvetica-Bold", 0, "winansi", "");
/* Using host font -- C:WINDOWSFontsSimHei.ttf.
PDFlib is using BOM UTF8 string to calling multi-byte character string
SimHei.ttf font name is "黑體", its corresponding BOM UTF8 string is
"xEFxBBxBFxE9xBBx91xE4xBDx93" */
Font_CS= PDF_load_font(p, "xEFxBBxBFxE9xBBx91xE4xBDx93", 0, "unicode", "");
/* Font_CS= PDF_load_font(p, "SimHei", 0, "unicode", ""); */
PDF_setfont(p, Font_E, 20);
PDF_show_xy(p, "SimHei font:" , Left, Top);
PDF_setfont(p, Font_CS, 24);
Top-=30;
PDF_show_xy(p, TextUnicode , Left, Top);
/* Using other disk-based font file that is not installed in system directory --
C:PSFONTSCSgkai00mp.ttf*/
Top-=50;
strcpy(fontfile, "C:\PSFONTS\CS\gkai00mp.ttf");
sprintf(buf, "kai=%s", fontfile);
/* Defines kai as alias for ..gkai00mp.ttf */
PDF_set_parameter(p, "FontOutline", buf);
Font_CS= PDF_load_font(p, "kai", 0, "unicode", "");
PDF_setfont(p, Font_E, 20);
PDF_show_xy(p, "AR PL KaitiM GB font:" , Left, Top);
PDF_setfont(p, Font_CS, 24);
Top-=30;
PDF_show_xy(p, TextUnicode , Left, Top);
/* Using TrueType collection font with index -- C:WINDOWSFontssimsun.ttc*/
Top-=50;
strcpy(fontfile, "C:\WINDOWS\Fonts\simsun.ttc");
sprintf(buf, "simsun=%s", fontfile);
/* Defines AdobeSongStd as alias for ..AdobeSongStd-Light.otf
This only need to claim once will be sufficient to
configure all fonts in simsun.ttc*/
PDF_set_parameter(p, "FontOutline", buf);
/* TTC files contain multiple separate fonts.
Address 1st font by appending a colon character and 0 after alias simsun */
Font_CS= PDF_load_font(p, "simsun:0", 0, "unicode", "");
PDF_setfont(p, Font_E, 20);
PDF_show_xy(p, "simsun:0 font:", Left, Top);
PDF_setfont(p, Font_CS, 24);
Top-=30;
PDF_show_xy2(p, TextUnicode, 8, Left, Top);
/*Address 2nd font by appending a colon character and 1 after alias simsun */
Top-=50;
Font_CS= PDF_load_font(p, "simsun:1", 0, "unicode", "");
PDF_setfont(p, Font_E, 20);
PDF_show_xy(p, "simsun:1 font:", Left, Top);
PDF_setfont(p, Font_CS, 24);
Top-=30;
PDF_show_xy2(p, TextUnicode, 8, Left, Top);
/* End of page. */
PDF_end_page_ext(p, "");
PDF_end_document(p, "");
}
PDF_CATCH(p) {
printf("PDFlib exception occurred in pdflib_cs2 sample:
");
printf("[%d] %s: %s
",
PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p));
PDF_delete(p);
return(2);
}
PDF_delete(p);
return 0;
}
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:網絡轉載