📄 devfont.c
字号:
{ if( ((unsigned char)cc[0] > 0xa0) && ((unsigned char)cc[1] > 0xa0) ) return 1; } cc[1] = '\0'; return 1;}static voidexpandcchar(PMWHZKFONT pf, int bg, int fg, unsigned char* c, MWPIXELVAL* bitmap){ int i=0; int c1, c2, seq; int x,y; unsigned char *font; int b = 0; /* keep gcc happy with b = 0 - MW */ int pixelsize; pixelsize=sizeof(MWPIXELVAL); c1 = c[0]; c2 = c[1]; if (use_big5) { seq=0; /* ladd=loby-(if(loby<127)?64:98)*/ c2-=(c2<127?64:98); /* hadd=(hiby-164)*157*/ if (c1>=0xa4) /* standard font*/ { seq=(((c1-164)*157)+c2); if (seq>=5809) seq-=408; } /* hadd=(hiby-161)*157*/ if (c1<=0xa3) /* special font*/ seq=(((c1-161)*157)+c2)+13094; } else seq=((c1 - 161)*94 + c2 - 161); font = pf->cfont_address + ((seq) * (pf->font_height * ((pf->cfont_width + 7) / 8))); for (y = 0; y < pf->font_height; y++) for (x = 0; x < pf->cfont_width; x++) { if (x % 8 == 0) b = *font++; if (b & (128 >> (x % 8))) /* pixel */ bitmap[i++]=fg; else bitmap[i++]=bg; } }static void expandchar(PMWHZKFONT pf, int bg, int fg, int c, MWPIXELVAL* bitmap){ int i=0; int x,y; unsigned char *font; int pixelsize; int b = 0; /* keep gcc happy with b = 0 - MW */ pixelsize=sizeof(MWPIXELVAL); font = pf->afont_address + c * (pf->font_height * ((pf->afont_width + 7) / 8)); for (y = 0; y < pf->font_height; y++) for (x = 0; x < pf->afont_width; x++) { if (x % 8 == 0) b = *font++; if (b & (128 >> (x % 8))) /* pixel */ bitmap[i++]=fg; else bitmap[i++]=bg; }}/* * Draw ASCII text string using HZK type font */static voidhzk_drawtext(PMWFONT pfont, PSD psd, MWCOORD ax, MWCOORD ay, const void *text, int cc, int flags){ PMWHZKFONT pf=(PMWHZKFONT)pfont; unsigned char c[2]; MWPIXELVAL *bitmap; unsigned char s1[3]; char *s,*sbegin; s=(char *)text; if(cc==1) { s1[0]=*((unsigned char*)text); s1[1]=0x0; s1[2]=0x0; s=s1; } sbegin=s; bitmap = (MWPIXELVAL *)ALLOCA(pf->cfont_width * pf->font_height * sizeof(MWPIXELVAL)); while( getnextchar(s, c) ) { if( c[1] != '\0') { expandcchar(pf, gr_background,gr_foreground, c, bitmap); /* Now draw the bitmap ... */ if (flags&MWTF_TOP) GdArea(psd,ax, ay, pf->cfont_width, pf->font_height, bitmap, MWPF_PIXELVAL); else GdArea(psd,ax, ay-pf->font_height+2, pf->cfont_width, pf->font_height, bitmap, MWPF_PIXELVAL); s += 2; ax += pf->cfont_width; } else { expandchar(pf, gr_background,gr_foreground, c[0], bitmap); /* Now draw the bitmap ... */ if (flags&MWTF_TOP) GdArea(psd,ax, ay, pf->afont_width, pf->font_height, bitmap, MWPF_PIXELVAL); else GdArea(psd,ax, ay-pf->font_height+2, pf->afont_width, pf->font_height, bitmap, MWPF_PIXELVAL); s += 1; ax += pf->afont_width; } if(s>=sbegin+cc)break; } FREEA(bitmap);}/* * Return information about a specified font. */static MWBOOLhzk_getfontinfo(PMWFONT pfont, PMWFONTINFO pfontinfo){ PMWHZKFONT pf=(PMWHZKFONT)pfont; int i; pfontinfo->height = pf->font_height; pfontinfo->maxwidth = pf->cfont_width; pfontinfo->baseline = pf->font_height-2; pfontinfo->firstchar = 0; pfontinfo->lastchar = 0; pfontinfo->fixed = TRUE; for(i=0; i<=256; i++) pfontinfo->widths[i] = pf->afont_width; return TRUE;}static voidhzk_gettextsize(PMWFONT pfont, const void *text, int cc, MWCOORD *pwidth, MWCOORD *pheight, MWCOORD *pbase){ PMWHZKFONT pf=(PMWHZKFONT)pfont; unsigned char c[2]; char *s,*sbegin; unsigned char s1[3]; int ax=0; s=(char *)text; if(cc==0) { *pwidth = 0; *pheight = pf->font_height; *pbase = pf->font_height-2; } if(cc==1) { s1[0]=*((unsigned char*)text); s1[1]=0x0; s1[2]=0x0; s=s1; } sbegin=s; while( getnextchar(s, c) ) { if( c[1] != '\0') { s += 2; ax += pf->cfont_width; } else { s += 1; ax += pf->afont_width; } if(s>=sbegin+cc) { /*fprintf(stderr,"s=%x,sbegin=%x,cc=%x\n",s,sbegin,cc);*/ break; } } /*fprintf(stderr,"ax=%d,\n",ax);*/ *pwidth = ax; *pheight = pf->font_height; *pbase = pf->font_height-2;}static voidhzk_destroyfont(PMWFONT pfont){ PMWHZKFONT pf=(PMWHZKFONT)pfont; UnloadFont(pf); free(pf);}static voidhzk_setfontsize(PMWFONT pfont, MWCOORD fontsize){ PMWHZKFONT pf=(PMWHZKFONT)pfont; /* jmt: hzk_setfontsize not supported*/ /* & pf->fontsize can't be changed*/ /* because of hzk_id() :p*/ pf->fontsize=pf->font_height;}#endif /* HAVE_HZK_SUPPORT*//* FIXME: this routine should work for all font renderers...*/intGdGetTextSizeEx(PMWFONT pfont, const void *str, int cc,int nMaxExtent, int* lpnFit, int* alpDx,MWCOORD *pwidth,MWCOORD *pheight, MWCOORD *pbase, int flags){#ifdef HAVE_FREETYPE_SUPPORT unsigned short buf[256]; unsigned short* text; PMWFREETYPEFONT pf = (PMWFREETYPEFONT)pfont; int defencoding = pf->fontprocs->encoding; int x = 0; int i; TT_UShort curchar; TT_Glyph_Metrics metrics; TT_Face_Properties properties; TT_Instance_Metrics imetrics; if ((cc<0)||(!str)) { *pwidth = *pheight = *pbase = 0; return 0; } /* convert encoding if required*/ if((flags & MWTF_PACKMASK) != defencoding) { cc = GdConvertEncoding(str, flags, cc, buf, defencoding); flags &= ~MWTF_PACKMASK; flags |= defencoding; text=buf; } else text =(unsigned short*)str; if(cc <= 0) { *pwidth = *pheight = *pbase = 0; return 0; } TT_Get_Face_Properties (pf->face, &properties); TT_Get_Instance_Metrics(pf->instance, &imetrics); pf->last_glyph_code = -1; /* reset kerning*/ pf->last_pen_pos = -32767; if (lpnFit) *lpnFit=-1; for (i = 0; i < cc; i++) { curchar = TT_Char_Index (pf->char_map,text[i]); if (TT_Load_Glyph (pf->instance, pf->glyph, curchar, TTLOAD_SCALE_GLYPH|TTLOAD_HINT_GLYPH) != TT_Err_Ok) { fprintf(stderr, "Unable to load glyph with index=%d\n",curchar); return 0; } TT_Get_Glyph_Metrics (pf->glyph, &metrics); if ((pf->fontattr&MWTF_KERNING) && pf->can_kern) { x += compute_kernval(pf, curchar) / 64; } x += metrics.advance / 64; if((lpnFit)&&(alpDx)) { if (x<=nMaxExtent) alpDx[i]=x; else if (*lpnFit==-1) (*lpnFit)=i; } /* Kerning point syndrome avoidance */ if (pf->last_pen_pos > x) x = pf->last_pen_pos; pf->last_pen_pos = x; pf->last_glyph_code = curchar; } if ((lpnFit)&&(*lpnFit==-1)) *lpnFit=cc; *pwidth = x; *pheight = (((properties.horizontal->Ascender * imetrics.y_scale)/ 0x10000) >> 6) - (((properties.horizontal->Descender * imetrics.y_scale)/ 0x10000) >> 6); /* FIXME: is it what's required ??*/ if (pbase) *pbase = (((-properties.horizontal->Descender) * imetrics.y_scale)/ 0x10000) >> 6; return 1;#else /* HAVE_FREETYPE_SUPPORT*/ *pwidth = *pheight = *pbase = 0; return 0;#endif}#ifdef HAVE_FREETYPE_SUPPORT#include <dirent.h>/* * This function is taken almost verbatim from ftdump.c from * the freetype library (version 1.3.1) */static char *tt_lookup_name(TT_Face face){ TT_Face_Properties prop; unsigned short i, n; unsigned short platform, encoding, language, id; char *string; char *name_buffer; unsigned short string_len; int j, found; int index = 4; /* I dont know why as yet.. */ int name_len; TT_Get_Face_Properties(face, &prop); n = prop.num_Names; for ( i = 0; i < n; i++ ) { TT_Get_Name_ID( face, i, &platform, &encoding, &language, &id ); TT_Get_Name_String( face, i, &string, &string_len ); if (id == index ) { /* The following code was inspired from Mark Leisher's */ /* ttf2bdf package */ found = 0; /* Try to find a Microsoft English name */ if ( platform == 3 ) for ( j = 1; j >= 0; j-- ) if ( encoding == j ) /* Microsoft ? */ if ( (language & 0x3FF) == 0x009 ) { /* English language */ found = 1; break; } if ( !found && platform == 0 && language == 0 ) found = 1; /* Found a Unicode Name. */ if ( found ) { if ( string_len > 512 ) string_len = 512; name_len = 0; name_buffer = (char*)malloc((string_len / 2) + 1); for ( i = 1; i < string_len; i += 2 ) name_buffer[name_len++] = string[i]; name_buffer[name_len] = '\0'; return name_buffer; } } } /* Not found */ return NULL;}static char *get_tt_name(char *p){ TT_Face face; char *ret; /*printf("Trying to open: %s!\n",p);*/ if (TT_Open_Face(engine, p, &face) != TT_Err_Ok) { fprintf(stderr, "Error opening font: %s\n", p); return NULL; } ret = tt_lookup_name(face); TT_Close_Face(face); return ret;}voidGdFreeFontList(MWFONTLIST ***fonts, int n){ int i; MWFONTLIST *g, **list = *fonts; for (i = 0; i < n; i++) { g = list[i]; if(g) { if(g->mwname) free(g->mwname); if(g->ttname) free(g->ttname); free(g); } } free(list); *fonts = 0;}voidGdGetFontList(MWFONTLIST ***fonts, int *numfonts){ DIR *dir; struct dirent *dent; char *p, *ftmp; int pl, idx = 0; MWFONTLIST **list; if (TT_Err_Ok != TT_Init_FreeType(&engine)) { fprintf(stderr, "Unable to initialize freetype\n"); *numfonts = -1; return ; } dir = opendir(FREETYPE_FONT_DIR); if (dir <= 0) { fprintf(stderr, "Error opening font directory\n"); *numfonts = -1; return ; } /* get the number of strings we need to allocate */ while ((dent = readdir(dir)) != NULL) { p = strrchr(dent->d_name, '.'); if (strcasecmp(p, ".ttf") == 0) idx++; } *numfonts = idx; rewinddir(dir); /* allocate strings */ list = (MWFONTLIST**)malloc(idx * sizeof(MWFONTLIST*)); for (pl = 0; pl < idx; pl++) list[pl] = (MWFONTLIST*)malloc(sizeof(MWFONTLIST)); *fonts = list; idx = 0; while ((dent = readdir(dir)) != NULL) { /* check extension */ p = strrchr(dent->d_name, '.'); if (strcasecmp(p, ".ttf") == 0) { /* get full path */ p = 0; pl = strlen(FREETYPE_FONT_DIR) + strlen(dent->d_name) * sizeof(char) + 2; p = (char*)malloc(pl); p = (char*)memset(p, '\0', pl); p = (char*)strcat(p, FREETYPE_FONT_DIR); p = (char*)strcat(p, "/"); p = (char*)strcat(p, dent->d_name); if((ftmp = get_tt_name(p)) != NULL) { list[idx]->ttname = ftmp; list[idx]->mwname = malloc(strlen(dent->d_name) + 1); list[idx]->mwname = strcpy(list[idx]->mwname, dent->d_name); idx++; } free(p); } } closedir(dir);}#else /* !HAVE_FREETYPE_SUPPORT*/voidGdFreeFontList(MWFONTLIST ***fonts, int n){}voidGdGetFontList(MWFONTLIST ***fonts, int *numfonts){ *numfonts = -1;}#endif /* !HAVE_FREETYPE_SUPPORT*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -