📄 type1.c
字号:
/*** $Id: type1.c,v 1.21 2003/09/25 04:08:55 snig Exp $** ** type1.c: Type1 font support based on t1lib.** ** Copyright (C) 2003 Feynman Software.** Copyright (C) 2000 ~ 2002 Wei Yongming, Song Lixin.**** Current maintainer: Wei Yongming**** Create date: 2000/08/29*//*** This program is free software; you can redistribute it and/or modify** it under the terms of the GNU General Public License as published by** the Free Software Foundation; either version 2 of the License, or** (at your option) any later version.**** This program 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 General Public License for more details.**** You should have received a copy of the GNU General Public License** along with this program; if not, write to the Free Software** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*//*** TODO:*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#include "common.h"#include "minigui.h"#include "gdi.h"#include "misc.h"#include "devfont.h"#include "charset.h"#include "fontname.h"#ifdef _TYPE1_SUPPORT#include <t1lib.h>#include "type1.h"#define ADJUST_SIZE 1.1;/************************ Init/Term of FreeType fonts ************************/static const void* get_char_bitmap (LOGFONT* logfont, DEVFONT* devfont, const unsigned char* mchar, int len);static const void* get_char_pixmap (LOGFONT* logfont, DEVFONT* devfont, const unsigned char* mchar, int len, int * pitch);static 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 "/usr/local/lib/minigui/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; /* Does load Type1 fonts? */ if (GetMgEtcIntValue (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 (GetMgEtcValue (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 (GetMgEtcValue (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); 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){ 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; width = T1_GetCharWidth ( type1_info->font_id, *mchar ); width *= type1_inst_info->csUnit2Pixel; 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); 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 ); return sum;}static int get_ave_width (LOGFONT* logfont, DEVFONT* devfont){ TYPE1INSTANCEINFO* type1_inst_info = TYPE1_INST_INFO_P (devfont); return type1_inst_info->ave_width;}static int get_font_height (LOGFONT* logfont, DEVFONT* devfont){ TYPE1INSTANCEINFO* type1_inst_info = TYPE1_INST_INFO_P (devfont); return type1_inst_info->font_height;}static int get_font_size (LOGFONT* logfont, DEVFONT* devfont, int expect){ return expect;}static int get_font_ascent (LOGFONT* logfont, DEVFONT* devfont){ TYPE1INSTANCEINFO* type1_inst_info = TYPE1_INST_INFO_P (devfont); return type1_inst_info->font_ascent;}static int get_font_descent (LOGFONT* logfont, DEVFONT* devfont){ TYPE1INSTANCEINFO* type1_inst_info = TYPE1_INST_INFO_P (devfont); 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); 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; return pixel_width * pixel_height ;}static size_t max_bitmap_size (LOGFONT* logfont, DEVFONT* devfont){ 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); get_char_bitmap(logfont, devfont, mchar, len); if(px){ *px += type1_inst_info->last_leftSideBearing; } if(py){ *py -= type1_inst_info->last_ascent; } if(pwidth) { *pwidth = type1_inst_info->last_rightSideBearing - type1_inst_info->last_leftSideBearing; } if(pheight){ *pheight = type1_inst_info->last_ascent - type1_inst_info->last_descent; } if(pwidth) return *pwidth; else return -1;}static const 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; if(type1_inst_info->last_bitmap_char == *mchar) { return type1_inst_info->last_bits; } glyph = T1_SetChar (type1_info->font_id, c, type1_inst_info->size, type1_inst_info->pmatrix); 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; /*free the last char's bitmap*/ if(!type1_inst_info->last_bits) free(type1_inst_info->last_bits); type1_inst_info->last_bits = glyph->bits; /*change the endian*/ { int height,width; int i,j,k; unsigned char c; unsigned char d; height = type1_inst_info->last_ascent - type1_inst_info->last_descent ; width = type1_inst_info->last_rightSideBearing - type1_inst_info->last_leftSideBearing ; width = ( width + 7 ) >> 3 << 3 ; //fprintf(stderr, "height:%d, width %d\n", height, width); for (i = 0; i < height; i++) { for (j = 0; j < width/8 ; j++) { c = type1_inst_info->last_bits[ i * width / 8 + j]; if( c ) { d = 0; for ( k = 0; k < 8; k++ ) { if ( ( c >> k ) & 0x01 ) d |= 0x80 >> k ; } type1_inst_info->last_bits[ i * width / 8 + j] = d ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -