修改SDL_ttf,支持动态设置字体大小
2013-09-11 15:51
806 查看
最近有个项目使用到SDL_ttf库来渲染字体,但是遇到一点麻烦。大家在使用TTF_OpenFont()打开字体库之后,却无法更改字体大小,我们总不能关闭重新打开吧,因为那也太耗时了。于是乎决定修改源码,成功之后与大家分享。
1.SDL_ttf.h中添加函数extern DECLSPEC void SDLCALL TTF_SetFontSize(TTF_Font *font,int ptsize);
2.修改SDL_ttf.c
SDL_ttf使用的是freetype来加载字体库,然后再使用freetype获取字体编码绘制字体到
1.SDL_ttf.h中添加函数extern DECLSPEC void SDLCALL TTF_SetFontSize(TTF_Font *font,int ptsize);
2.修改SDL_ttf.c
SDL_ttf使用的是freetype来加载字体库,然后再使用freetype获取字体编码绘制字体到
SDL_Surface上。最初实验总是不成功,结果大量的实验,原来是没有清除font导致不正确;加上Flush_Cache(font),成功搞定。
void TTF_SetFontSize(TTF_Font *font,int ptsize) { FT_Fixed scale; FT_Error error; FT_Face face; face = font->face; /* Make sure that our font face is scalable (global metrics) */ if ( FT_IS_SCALABLE(face) ) { /* Set the character size and use default DPI (72) */ error = FT_Set_Char_Size( font->face, 0, ptsize * 64, 0, 0 ); if( error ) { TTF_SetFTError( "Couldn't set font size", error ); TTF_CloseFont( font ); return ; } /* Get the scalable font metrics for this font */ scale = face->size->metrics.y_scale; font->ascent = FT_CEIL(FT_MulFix(face->ascender, scale)); font->descent = FT_CEIL(FT_MulFix(face->descender, scale)); font->height = font->ascent - font->descent + /* baseline */ 1; font->lineskip = FT_CEIL(FT_MulFix(face->height, scale)); font->underline_offset = FT_FLOOR(FT_MulFix(face->underline_position, scale)); font->underline_height = FT_FLOOR(FT_MulFix(face->underline_thickness, scale)); } else { /* Non-scalable font case. ptsize determines which family * or series of fonts to grab from the non-scalable format. * It is not the point size of the font. * */ if ( ptsize >= font->face->num_fixed_sizes ) ptsize = font->face->num_fixed_sizes - 1; font->font_size_family = ptsize; error = FT_Set_Pixel_Sizes( face, face->available_sizes[ptsize].height, face->available_sizes[ptsize].width ); /* With non-scalale fonts, Freetype2 likes to fill many of the * font metrics with the value of 0. The size of the * non-scalable fonts must be determined differently * or sometimes cannot be determined. * */ font->ascent = face->available_sizes[ptsize].height; font->descent = 0; font->height = face->available_sizes[ptsize].height; font->lineskip = FT_CEIL(font->ascent); font->underline_offset = FT_FLOOR(face->underline_position); font->underline_height = FT_FLOOR(face->underline_thickness); } if ( font->underline_height < 1 ) { font->underline_height = 1; } font->glyph_italics *= font->height; Flush_Cache(font); //这个非常重要 }
相关文章推荐
- 动态计算字符串的字体大小并据此设置行间距---NSAttributedString
- ios 动态修改UILabel字体大小
- android studio默认修改主题,字体,文字颜色,大小等属性的设置
- IntelliJ IDEA设置修改字体大小与样式
- 动态设置字体大小
- 修改UItextField输入框placeholder的字体默认颜色和大小,采用KVC方式设置
- 动态设置缩放比例和html字体大小
- 如何在代码中动态设置字体大小
- 动态调整UITableViewCell的高度及字体大小设置
- 移动端应该如何动态设置字体大小?
- ToolBar修改系统后退键图标, 设置menu字体颜色大小
- 动态代码设置Textview的字体大小,引用Dimen资源
- android 中对 gps 默认关闭+语言默认中文的设置+时区设置+修改系统名字+修改系统默认时间+修改屏幕休眠时间+修改usb debug默认关闭+mtk修改默认字体大小
- Android 协调者布局title 字体大小颜色修改,设置布局为最上层
- ios设置字体-黑体,加粗等 修改textField的placeholder的字体颜色、大小
- Eclipse最新修改JSP字体大小设置
- 当你在设置里修改字体大小的时候,到底在修改什么
- Android 代码中动态设置字体大小-TextView.SetTextSize()
- 当你在设置里修改字体大小的时候,到底在修改什么
- 移动端应该如何动态设置字体大小?