📄 type1.c
字号:
//// $Id: type1.c,v 1.6 2000/11/10 03:18:35 ymwei Exp $// // type1.c: Type1 font support based on t1lib.// // Copyright (C) 2000, BluePoint Software.//// Current maintainer: Song Lixin.///*** This library is free software; you can redistribute it and/or** modify it under the terms of the GNU Library General Public** License as published by the Free Software Foundation; either** version 2 of the License, or (at your option) any later version.**** This library is distributed in the hope that it will be useful,** but WITHOUT ANY WARRANTY; without even the implied warranty of** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU** Library General Public License for more details.**** You should have received a copy of the GNU Library General Public** License along with this library; if not, write to the Free** Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,** MA 02111-1307, USA*/// Create date: 2000/08/29//// Modify records://// Who When Where For What Status//-----------------------------------------------------------------------------//// TODO://#include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>#include <pthread.h>#include <math.h>#ifndef __ECOS# include <syslog.h>#endif#include "t1lib/t1lib.h"#include "common.h"#include "minigui.h"#include "gdi.h"#include "devfont.h"#include "type1.h"#include "charset.h"#include "fontname.h"#define ADJUST_SIZE 1.1;//#define JDEBUG 1/************************ Init/Term of FreeType fonts ************************/static void* get_char_bitmap (LOGFONT* logfont, DEVFONT* devfont, const unsigned char* mchar, int len);static void* get_char_pixmap (LOGFONT* logfont, DEVFONT* devfont, const unsigned char* mchar, int len, int * pitch);#if 0static void* get_str_pixmap (LOGFONT* logfont, DEVFONT* devfont,const unsigned char* str, int len, long spaceoff);static void* get_str_bitmap (LOGFONT* logfont, DEVFONT* devfont, const unsigned char* str, int len,long spaceoff);#endifstatic int get_font_ascent (LOGFONT* logfont, DEVFONT* devfont);static int get_font_descent (LOGFONT* logfont, DEVFONT* devfont);static int nr_fonts;static TYPE1INFO * type1_infos;static DEVFONT * type1_dev_fonts;static encstruct default_enc;#define SECTION_NAME "type1fonts"#define T1LIBENCFILE "IsoLatin1.enc"/* * I always use full path file name ,so it maybe of no use...:-) * Only for the sake of complety,I set search path to this . * */#define T1FONTPATH "/fonts"#define CHARSPACEUNIT2PIXEL(x) (x/1000)/* * Because the uplayer did not provide the option of whether * to use anti-aliaing.We use it in default. * On day it supports the option,the code may be easily updated. * */BOOL InitType1Fonts (void){ int i,j; char font_name [LEN_DEVFONT_NAME + 1]; int loglevel; char key [11]; char charset [LEN_FONT_NAME + 1]; CHARSETOPS* charset_ops; char file [MAX_PATH + 1]; int font_id;#ifdef JDEBUG syslog(LOG_INFO,"type1.c:Go into InitTypeFonts");#endif /* Does load Type1 fonts? */ if (GetIntValueFromEtcFile (ETCFILEPATH, SECTION_NAME, "font_number",&nr_fonts) < 0 ) return FALSE; if ( nr_fonts < 1) return TRUE; loglevel = 0; loglevel |= NO_LOGFILE; loglevel |= IGNORE_CONFIGFILE; loglevel |= IGNORE_FONTDATABASE; T1_InitLib(loglevel); T1_SetBitmapPad(8); T1_AASetBitsPerPixel(8); T1_AASetGrayValues(0,1,2,3,4); /* * Set some default value base on my own idea. * So u can change it if u have enough reason. * */#if 1 //T1_SetDeviceResolutions(72,72); default_enc.encoding = T1_LoadEncoding (T1LIBENCFILE); default_enc.encfilename = (char *) malloc (strlen (T1LIBENCFILE) + 1); strcpy (default_enc.encfilename, T1LIBENCFILE); T1_SetDefaultEncoding (default_enc.encoding); T1_AASetLevel (T1_AA_LOW);#endif /* Alloc space for devfont and type1info. */ type1_infos = calloc (nr_fonts, sizeof (TYPE1INFO)); type1_dev_fonts = calloc (nr_fonts,sizeof (DEVFONT)); if (type1_infos == NULL || type1_dev_fonts == NULL) { goto error_alloc; } for (i = 0; i < nr_fonts; i++) type1_infos [i].valid = FALSE; /* * I always use full path file name ,so it maybe of no use...:-) * Only for the sake of complety,I set search path to this . * */ T1_SetFileSearchPath (T1_PFAB_PATH | T1_AFM_PATH | T1_ENC_PATH ,T1FONTPATH); for (i = 0; i < nr_fonts; i++) { sprintf (key, "name%d", i); if (GetValueFromEtcFile (ETCFILEPATH, SECTION_NAME, key,font_name, LEN_DEVFONT_NAME) < 0 ) goto error_load; if (!fontGetCharsetFromName (font_name, charset)) { fprintf (stderr, "GDI: Invalid font name (charset): %s.\n",font_name); goto error_load; } if ((charset_ops = GetCharsetOps (charset)) == NULL) { fprintf (stderr, "GDI: Not supported charset: %s.\n", charset); goto error_load; } sprintf (key, "fontfile%d", i); if (GetValueFromEtcFile (ETCFILEPATH, SECTION_NAME, key, file, MAX_PATH) < 0) goto error_load; T1_AddFont(file); font_id = T1_AddFont(file); if (font_id <= 0) goto error_load; j = strstr(file,"pfb") - file; if (font_id <= 0) goto error_load; strncpy(file+j,"afm",3); if (T1_SetAfmFileName(font_id,file) < 0) goto error_load; strncpy(file+j,"pfb",3); strncpy (type1_dev_fonts[i].name, font_name, LEN_DEVFONT_NAME); type1_dev_fonts[i].name [LEN_DEVFONT_NAME] = '\0'; type1_dev_fonts[i].font_ops = &type1_font_ops; type1_dev_fonts[i].charset_ops = charset_ops; type1_dev_fonts[i].data = type1_infos + i; type1_infos[i].font_id = font_id; type1_infos [i].valid = TRUE; } for (i = 0; i < nr_fonts; i++) AddSBDevFont (type1_dev_fonts + i);#ifdef JDEBUG syslog(LOG_INFO,"type1.c:Exit InitTypeFonts");#endif return TRUE; error_load: free (type1_infos); free (type1_dev_fonts); error_alloc: fprintf (stderr, "GDI:font/type1.c: Error when init Type1 fonts\n"); type1_infos = NULL; type1_dev_fonts = NULL; T1_CloseLib(); return FALSE;}void TermType1Fonts (void){#ifdef JDEBUG syslog(LOG_INFO,"type1.c:Go into TermTypeFonts");#endif free (type1_infos); free (type1_dev_fonts); type1_infos = NULL; type1_dev_fonts = NULL; T1_CloseLib();}/* * The width of a character is the amount of horizontal * escapement that the next character is shifted * to the right with respect to the current position * */static int get_char_width (LOGFONT* logfont, DEVFONT* devfont, const unsigned char* mchar, int len){ TYPE1INSTANCEINFO* type1_inst_info = TYPE1_INST_INFO_P (devfont); TYPE1INFO* type1_info = type1_inst_info->type1_info; int width;#ifdef JDEBUG syslog(LOG_INFO,"type1.c:Go into get_char_width");#endif width = T1_GetCharWidth ( type1_info->font_id, *mchar ); width *= type1_inst_info->csUnit2Pixel;#ifdef JDEBUG syslog(LOG_INFO,"type1.c:get_char_width: %c 's width is: %d", *mchar, width);#endif return width;}/* * The width of a character is the amount of horizontal * escapement that the next character is shifted * to the right with respect to the current position * */static int get_max_width (LOGFONT* logfont, DEVFONT* devfont){ TYPE1INSTANCEINFO* type1_inst_info = TYPE1_INST_INFO_P (devfont);#ifdef JDEBUG syslog(LOG_INFO,"type1.c:Go into get_max_width");#endif return type1_inst_info->max_width;}/* * The width of a character is the amount of horizontal * escapement that the next character is shifted * to the right with respect to the current position * */static int get_str_width (LOGFONT* logfont, DEVFONT* devfont, const unsigned char* mstr, int n, int cExtra){ TYPE1INSTANCEINFO* type1_inst_info = TYPE1_INST_INFO_P (devfont); TYPE1INFO* type1_info = type1_inst_info->type1_info; int i; int width; int sum = 0; for ( i = 0; i <= n-1; i++ ) { width = T1_GetCharWidth ( type1_info->font_id, * ( mstr+i ) ); sum += width; } sum = sum * type1_inst_info->csUnit2Pixel + cExtra * ( n - 1 );#ifdef JDEBUG syslog(LOG_INFO,"type1.c:Go into get_str_width");#endif return sum;}static int get_ave_width (LOGFONT* logfont, DEVFONT* devfont){ TYPE1INSTANCEINFO* type1_inst_info = TYPE1_INST_INFO_P (devfont);#ifdef JDEBUG syslog(LOG_INFO,"type1.c:Go into get_ave_width");#endif return type1_inst_info->ave_width;}static int get_font_height (LOGFONT* logfont, DEVFONT* devfont){ TYPE1INSTANCEINFO* type1_inst_info = TYPE1_INST_INFO_P (devfont);#ifdef JDEBUG syslog(LOG_INFO,"type1.c:Go into get_font_height");#endif return type1_inst_info->font_height;}static int get_font_size (LOGFONT* logfont, DEVFONT* devfont, int expect){#ifdef JDEBUG syslog(LOG_INFO,"type1.c:Go into get_font_size");#endif return expect;}static int get_font_ascent (LOGFONT* logfont, DEVFONT* devfont){ TYPE1INSTANCEINFO* type1_inst_info = TYPE1_INST_INFO_P (devfont);#ifdef JDEBUG syslog(LOG_INFO,"type1.c:Go into get_font_ascent");#endif return type1_inst_info->font_ascent;}static int get_font_descent (LOGFONT* logfont, DEVFONT* devfont){ TYPE1INSTANCEINFO* type1_inst_info = TYPE1_INST_INFO_P (devfont);#ifdef JDEBUG syslog(LOG_INFO,"type1.c:Go into get_font_descent");#endif return -type1_inst_info->font_descent;}/* * FIXME: what the meaning of this functin when rotation is taken * into account? * This implementation conflicts with that of freetype now. * */static size_t char_bitmap_size (LOGFONT* logfont, DEVFONT* devfont, const unsigned char* mchar, int len){ int pixel_width,pixel_height; TYPE1INSTANCEINFO* type1_inst_info = TYPE1_INST_INFO_P (devfont);#ifdef JDEBUG syslog(LOG_INFO,"type1.c:Go into char_bitmap_size");#endif get_char_bitmap(logfont,devfont,mchar,len); pixel_width = type1_inst_info->last_rightSideBearing - type1_inst_info->last_leftSideBearing; pixel_height = type1_inst_info->last_ascent - type1_inst_info->last_descent;#ifdef JDEBUG syslog(LOG_INFO,"type1.c:char_bitmap_size:char:%c,width:%d,height:%d",*mchar,pixel_width,pixel_height);#endif return pixel_width * pixel_height ;}static size_t max_bitmap_size (LOGFONT* logfont, DEVFONT* devfont){#ifdef JDEBUG syslog(LOG_INFO,"type1.c:Go into max_bitmap_size");#endif return get_max_width(logfont,devfont) * get_font_height(logfont,devfont);}/* * NULL function * */static void start_str_output (LOGFONT* logfont, DEVFONT* devfont){ return;}/* * call this function before getting the bitmap/pixmap of the char * to get the bbox of the char * *//* * FIXME: Because the limits of the interface(either ours of theirs * we can get the bounding box when rotation is in regard! * In detail: T1lib provide functinos to draw a string, * while we choose to draw it one by one!! * */static int get_char_bbox (LOGFONT* logfont, DEVFONT* devfont, const unsigned char* mchar, int len, int* px, int* py, int* pwidth, int* pheight){ TYPE1INSTANCEINFO* type1_inst_info = TYPE1_INST_INFO_P (devfont); #ifdef JDEBUG syslog(LOG_INFO,"type1.c:Go into get_char_bbox,char: %c",*mchar);#endif get_char_bitmap(logfont, devfont, mchar, len); if(px){ *px += type1_inst_info->last_leftSideBearing;#ifdef JDEBUG syslog(LOG_INFO,"type1.c:get_char_bbox: *px: %d \n",*px);#endif } if(py){ *py = *py + get_font_ascent ( logfont,devfont ) - type1_inst_info->last_ascent;#ifdef JDEBUG syslog(LOG_INFO,"type1.c:get_char_bbox: *py:%d",*py);#endif } if(pwidth) { *pwidth = type1_inst_info->last_rightSideBearing - type1_inst_info->last_leftSideBearing;#ifdef JDEBUG syslog(LOG_INFO,"type1.c:get_char_bbox: *pwidth: %d \n",*pwidth);#endif } if(pheight){ *pheight = type1_inst_info->last_ascent - type1_inst_info->last_descent;#ifdef JDEBUG syslog(LOG_INFO,"type1.c:get_char_bbox: *pheight: %d \n",*pheight);#endif }#ifdef JDEBUG syslog(LOG_INFO,"type1.c:leave get_char_bbox");#endif if(pwidth) return *pwidth; else return -1;}static void* get_char_bitmap (LOGFONT* logfont, DEVFONT* devfont, const unsigned char* mchar, int len){ GLYPH * glyph; TYPE1INSTANCEINFO* type1_inst_info = TYPE1_INST_INFO_P (devfont); TYPE1INFO* type1_info = type1_inst_info->type1_info; unsigned char c = *mchar;#ifdef JDEBUG syslog(LOG_INFO,"type1.c:Go into get_char_bitmap,*machar:%c",*mchar);#endif if(type1_inst_info->last_bitmap_char == *mchar) {#ifdef JDEBUG syslog(LOG_INFO,"type1.c:get_char_bitmap: already ok!");#endif return type1_inst_info->last_bits; } glyph = T1_SetChar (type1_info->font_id, c, type1_inst_info->size, type1_inst_info->pmatrix);#ifdef JDEBUG syslog(LOG_INFO,"type1.c:Go into get_char_bitmap 1");#endif type1_inst_info->last_bitmap_char = *mchar; type1_inst_info->last_pixmap_char = -1; if(type1_inst_info->last_bitmap_str){ free (type1_inst_info->last_bitmap_str); type1_inst_info->last_bitmap_str = NULL; } if(type1_inst_info->last_pixmap_str){ free (type1_inst_info->last_pixmap_str); type1_inst_info->last_pixmap_str = NULL; } type1_inst_info->last_ascent = glyph->metrics.ascent; type1_inst_info->last_descent = glyph->metrics.descent; type1_inst_info->last_leftSideBearing = glyph->metrics.leftSideBearing; type1_inst_info->last_rightSideBearing = glyph->metrics.rightSideBearing; type1_inst_info->last_advanceX = glyph->metrics.advanceX; type1_inst_info->last_advanceY = glyph->metrics.advanceY; type1_inst_info->last_bpp = glyph->bpp;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -