kb_machblue_core_text.c
上传用户:fy98168
上传日期:2015-06-26
资源大小:13771k
文件大小:9k
- //*****************************************************************************
- //File Name: kb_machblue_text.c
- //
- //Description: text function
- //
- // used by Machblue to access the platform's font api
- // used by Machblue to access the platform's text drawing api
- //
- //Author: wisco & steven
- //
- //Date: 2006.12.29
- //
- //Version: v1.0
- //*****************************************************************************
- #include "gendef.h"
- #include "Text.h"
- #include "machblue_defines.h"
- #include "machblue_porting_core.h"
- #include "kb_machblue_client_define.h"
- #define MB_TEXT_MAX_PIXELS 640*48
- /* 20点阵字库数据 */
- extern const unsigned char j_zk20[355700];
- /**
- * Creates a new font object.
- * font < pointer to font to create >
- * font_name < name of the font to create >
- * style < style of the font to create >
- * size < size of the font to create >
-
- * @return MB_SUCCESS and updates "font" on success, MB_FAILURE othrewise.
- */
- mb_error_t mb_font_create(mb_font_t *font,const mb_char_t *font_name,mb_font_style_t style,int size)
- {
- kb_movie_font_t *p_font;
- //mb_printf("nmb_font_create");
- p_font=(kb_movie_font_t *)mb_malloc(sizeof(kb_movie_font_t));
- if(p_font==NULL)
- {
- mb_printf("n[Machblue]:Font create malloc error.");
- return MB_FAILURE;
- }
- switch(size)
- {
- case 16:
- break;
-
- case 20:
- p_font->m_dfb_font.Ascender=10;
- p_font->m_dfb_font.Descender=10;
- p_font->m_dfb_font.Height=20;
- p_font->m_dfb_font.MaxAdvance=0;
- p_font->m_dfb_font.Kerning=0;
- p_font->m_dfb_font.StringWidth=0;
- p_font->m_dfb_font.StringExtents=0;
- p_font->m_dfb_font.GlyphExtents=0;
- p_font->address.Address=(UINT8 *)(j_zk20);
- p_font->address.English_Adderss=(UINT8 *)(j_zk20+3200);
- p_font->address.Chinese_Adderss=(UINT8 *)(j_zk20+14100);
- break;
-
- case 24:
- break;
-
- default:
- p_font->m_dfb_font.Ascender=10;
- p_font->m_dfb_font.Descender=10;
- p_font->m_dfb_font.Height=20;
- p_font->m_dfb_font.MaxAdvance=0;
- p_font->m_dfb_font.Kerning=0;
- p_font->m_dfb_font.StringWidth=0;
- p_font->m_dfb_font.StringExtents=0;
- p_font->m_dfb_font.GlyphExtents=0;
- p_font->address.Address=(UINT8 *)(j_zk20);
- p_font->address.English_Adderss=(UINT8 *)(j_zk20+3200);
- p_font->address.Chinese_Adderss=(UINT8 *)(j_zk20+14100);
- break;
- }
- p_font->font_name=font_name;
- p_font->m_size=size;
-
- *font=(mb_font_t)p_font;
-
- return MB_SUCCESS;
- }
- /**
- * Deletes an existing font object.
- *
- * font < font object to delete >
- *
- * @return MB_SUCCESS on success and MB_FAILURE on failure.
- */
- mb_error_t mb_font_delete(mb_font_t font)
- {
- kb_movie_font_t *p_font=(kb_movie_font_t *)font;
- //mb_printf("nmb_font_delete");
- if(p_font==NULL)
- {
- mb_printf("n[Machblue]:Font delete NULL.");
- return MB_FAILURE;
- }
-
- mb_free(p_font);
-
- return MB_SUCCESS;
- }
- /**
- * Sets a new font size to the font object.
- * font < font object to modify >
- * new_size < new size to set >
-
- * @return MB_SUCCESS on success and MB_FAILURE on failure.
- */
- mb_error_t mb_font_size_set(mb_font_t font,int new_size)
- {
- //mb_printf("nmb_font_size_set");
- return MB_SUCCESS;
- }
- /**
- * Gets a font object attributes.
- * font < font object to query >
- * size < pointer to int to store font size >
- * ascender < pointer to int to store font ascender >
- * spacing < pointer to int to store font spacing >
-
- * @return MB_SUCCESS on success and MB_FAILURE on failure.
- */
- mb_error_t mb_font_attributes_get(mb_font_t font,int *out_size,int *out_ascender,int *out_spacing)
- {
- kb_movie_font_t *p_font=(kb_movie_font_t *)font;
- //mb_printf("nmb_font_attributes_get [%d][%d][%d]",p_font->m_size,p_font->m_dfb_font.Ascender,p_font->m_dfb_font.MaxAdvance);
- if(p_font==NULL)
- {
- mb_printf("n[Machblue]:Font get attributes NULL.");
- return MB_FAILURE;
- }
- *out_size=p_font->m_size;
- *out_ascender=p_font->m_dfb_font.Ascender;
- *out_spacing=p_font->m_dfb_font.MaxAdvance;
- return MB_SUCCESS;
- }
- /**
- * Gets a text extent (bounds).
- * text < text to evaluate >
- * length < length in characters of the text to evaluate >
- * font < font to use for the extent calculation >
- * bounds < pointer to mb_rect_t used to store calculated extent >
-
- * @return MB_SUCCESS on success and MB_FAILURE on failure.
- */
- mb_error_t mb_text_bounds_get(const mb_char_t *text,int length,mb_font_t font,mb_rect_t *bounds)
- {
- kb_movie_font_t *pFont=(kb_movie_font_t *)font;
- INT32 strWidth,strHeight;
- if(pFont==NULL)
- {
- mb_printf("n[Machblue]:Text get bounds NULL.");
- return MB_FAILURE;
- }
- switch(pFont->m_size)
- {
- case 16:
- case 32:
- KB_TxtSetCharType(TEXT_ENG16X8);
- KB_TxtSetHZType(TEXT_HZ16X16);
- break;
-
- case 20:
- case 40:
- KB_TxtSetCharType(TEXT_ENG20X10);
- KB_TxtSetHZType(TEXT_HZ20X20);
- break;
-
- case 24:
- case 48:
- KB_TxtSetCharType(TEXT_ENG24X12);
- KB_TxtSetHZType(TEXT_HZ24X24);
- break;
- default:
- KB_TxtSetCharType(TEXT_ENG20X10);
- KB_TxtSetHZType(TEXT_HZ20X20);
- break;
- }
-
- KB_TxtGetStrWidHei((char*)text,&strWidth,&strHeight);
- bounds->x=0;
- bounds->y=0;
- bounds->height=(int)strHeight;
- bounds->width=(int)strWidth;
- //mb_printf("nmb_text_bounds_get text[%s] length[%d] [%d][%d]",text,length,strHeight,strWidth);
-
- return MB_SUCCESS;
- }
- /**
- * Draws a text into a surface.
- * text < text to draw >
- * length < length in characters of the text to draw >
- * surface < destination surface to draw text to >
- * font, < font to draw text with >
- * baseline_anchor_point < anchor point to draw text at (relative to surface) >
- * clipping_rect < text clipping rectangle (relative to surface) >
- * color < color of the text to draw in native surface format >
-
- * @return MB_SUCCESS on success and MB_FAILURE on failure.
- */
- UINT32 mb_color_translate(unsigned int color)
- {
- UINT32 a1,a2,a3,a4;
- //mb_printf("nmb_color_translate color[%d]",color);
-
- a1 = (UINT32)((UINT32)((UINT32)color/(16*16*16))%16);
- a2 = (UINT32)((UINT32)((UINT32)color/(16*16))%16);
- a3 = (UINT32)((UINT32)((UINT32)color/16)%16);
- a4 = (UINT32)((UINT32)color%16);
-
- a1 = a1 * 17;
- a2 = a2 * 17;
- a3 = a3 * 17;
- a4 = a4 * 17;
-
- a1 = a1 * (16*16*16*16*16*16);
- a2 = a2 * (16*16*16*16);
- a3 = a3 * (16*16);
- a4 = a4;
- return a1+a2+a3+a4;
- }
- mb_error_t mb_text_draw(const mb_char_t *text,int length,mb_surface_t surface,mb_font_t font,
- mb_point_t *baseline_anchor_point,mb_rect_t *clipping_rect,unsigned int color)
- {
- unsigned char nTimeHR,nTimeVT,*pData,*pSurBuf,*pSrc,*pDes;
- ColrRef bgColor;
- KB_OSDRect osdRect;
- kb_movie_font_t *pFont=(kb_movie_font_t *)font;
- mb_rect_t tRect;
- unsigned long index,strWidth,strHeight,offset;
- int sPitch;
- UINT32 text_color;
- if(pFont->m_size==32||pFont->m_size==40||pFont->m_size==48)
- {
- nTimeVT=2;
- nTimeHR=2;
- }
- else
- {
- nTimeVT=1;
- nTimeHR=1;
- }
- bgColor=KB_COLOR_TRANSPARENCY;
- switch(pFont->m_size)
- {
- case 16:
- case 32:
- KB_TxtSetCharType(TEXT_ENG16X8);
- KB_TxtSetHZType(TEXT_HZ16X16);
- break;
-
- case 20:
- case 40:
- KB_TxtSetCharType(TEXT_ENG20X10);
- KB_TxtSetHZType(TEXT_HZ20X20);
- break;
-
- case 24:
- case 48:
- KB_TxtSetCharType(TEXT_ENG24X12);
- KB_TxtSetHZType(TEXT_HZ24X24);
- break;
- default:
- KB_TxtSetCharType(TEXT_ENG20X10);
- KB_TxtSetHZType(TEXT_HZ20X20);
- break;
- }
- osdRect.nX=clipping_rect->x;
- osdRect.nY=clipping_rect->y;
- osdRect.nWidth=clipping_rect->width;
- osdRect.nHeight=clipping_rect->height;
- text_color = mb_color_translate(color);
- pData=KB_TxtGetDataFromLib(&osdRect,text_color,bgColor,nTimeHR,nTimeVT,(UINT8*)text,&strWidth,&strHeight);
- if(pData==NULL)
- {
- mb_printf("n[Machblue]:Text draw error.");
- return MB_FAILURE;
- }
- tRect.x=baseline_anchor_point->x;
- tRect.y=baseline_anchor_point->y;
- tRect.width=(unsigned int)strWidth;
- tRect.height=(unsigned int)strHeight;
- mb_surface_lock(surface,&sPitch,(void **)&pSurBuf);
-
- pSurBuf+=(tRect.y*KB_OSD_WIDTH+tRect.x)*KB_OSD_BYTES;
- for(index=0;index<tRect.height;index++)
- {
- pSrc=(unsigned char*)&pData[index*strWidth*KB_OSD_BYTES];
- pDes=(unsigned char*)&pSurBuf[index*KB_OSD_WIDTH*KB_OSD_BYTES];
- for(offset=0;offset<((unsigned long)(tRect.width)*KB_OSD_BYTES);)
- {
- if((pSrc[offset]!=0)&&(pSrc[offset+1]!=0))
- {
- pDes[offset]=pSrc[offset];
- pDes[offset+1]=pSrc[offset+1];
- }
- offset+=2;
- }
- }
-
- return MB_SUCCESS ;
- }