📄 freetype.c
字号:
p_region->picture = p_region_tmp->picture; free( p_region_tmp ); /* Calculate text color components */ i_y = (uint8_t)(( 66 * p_line->i_red + 129 * p_line->i_green + 25 * p_line->i_blue + 128) >> 8) + 16; i_u = (int8_t)(( -38 * p_line->i_red - 74 * p_line->i_green + 112 * p_line->i_blue + 128) >> 8) + 128; i_v = (int8_t)(( 112 * p_line->i_red - 94 * p_line->i_green - 18 * p_line->i_blue + 128) >> 8) + 128; /* Build palette */ fmt.p_palette->i_entries = 16; for( i = 0; i < 8; i++ ) { fmt.p_palette->palette[i][0] = 0; fmt.p_palette->palette[i][1] = 0x80; fmt.p_palette->palette[i][2] = 0x80; fmt.p_palette->palette[i][3] = pi_gamma[i]; fmt.p_palette->palette[i][3] = (int)fmt.p_palette->palette[i][3] * (255 - p_line->i_alpha) / 255; } for( i = 8; i < fmt.p_palette->i_entries; i++ ) { fmt.p_palette->palette[i][0] = i * 16 * i_y / 256; fmt.p_palette->palette[i][1] = i_u; fmt.p_palette->palette[i][2] = i_v; fmt.p_palette->palette[i][3] = pi_gamma[i]; fmt.p_palette->palette[i][3] = (int)fmt.p_palette->palette[i][3] * (255 - p_line->i_alpha) / 255; } p_dst = p_region->picture.Y_PIXELS; i_pitch = p_region->picture.Y_PITCH; /* Initialize the region pixels */ memset( p_dst, 0, i_pitch * p_region->fmt.i_height ); for( ; p_line != NULL; p_line = p_line->p_next ) { int i_glyph_tmax = 0; int i_bitmap_offset, i_offset, i_align_offset = 0; for( i = 0; p_line->pp_glyphs[i] != NULL; i++ ) { FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ]; i_glyph_tmax = __MAX( i_glyph_tmax, p_glyph->top ); } if( p_line->i_width < i_width ) { if( ( p_region->p_style && p_region->p_style->i_text_align == SUBPICTURE_ALIGN_RIGHT ) || ( !p_region->p_style && (p_region->i_align & 0x3) == SUBPICTURE_ALIGN_RIGHT ) ) { i_align_offset = i_width - p_line->i_width; } else if( ( p_region->p_style && p_region->p_style->i_text_align != SUBPICTURE_ALIGN_LEFT ) || ( !p_region->p_style && (p_region->i_align & 0x3) != SUBPICTURE_ALIGN_LEFT ) ) { i_align_offset = ( i_width - p_line->i_width ) / 2; } } for( i = 0; p_line->pp_glyphs[i] != NULL; i++ ) { FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ]; i_offset = ( p_line->p_glyph_pos[ i ].y + i_glyph_tmax - p_glyph->top + 2 ) * i_pitch + p_line->p_glyph_pos[ i ].x + p_glyph->left + 2 + i_align_offset; for( y = 0, i_bitmap_offset = 0; y < p_glyph->bitmap.rows; y++ ) { for( x = 0; x < p_glyph->bitmap.width; x++, i_bitmap_offset++ ) { if( p_glyph->bitmap.buffer[i_bitmap_offset] ) p_dst[i_offset+x] = ((int)p_glyph->bitmap.buffer[i_bitmap_offset] + 8)/16; } i_offset += i_pitch; } } } /* Outlining (find something better than nearest neighbour filtering ?) */ if( 1 ) { uint8_t *p_dst = p_region->picture.Y_PIXELS; uint8_t *p_top = p_dst; /* Use 1st line as a cache */ uint8_t left, current; for( y = 1; y < (int)fmt.i_height - 1; y++ ) { if( y > 1 ) memcpy( p_top, p_dst, fmt.i_width ); p_dst += p_region->picture.Y_PITCH; left = 0; for( x = 1; x < (int)fmt.i_width - 1; x++ ) { current = p_dst[x]; p_dst[x] = ( 8 * (int)p_dst[x] + left + p_dst[x+1] + p_top[x -1]+ p_top[x] + p_top[x+1] + p_dst[x -1 + p_region->picture.Y_PITCH ] + p_dst[x + p_region->picture.Y_PITCH] + p_dst[x + 1 + p_region->picture.Y_PITCH]) / 16; left = current; } } memset( p_top, 0, fmt.i_width ); } return VLC_SUCCESS;}static void DrawBlack( line_desc_t *p_line, int i_width, subpicture_region_t *p_region, int xoffset, int yoffset ){ uint8_t *p_dst = p_region->picture.A_PIXELS; int i_pitch = p_region->picture.A_PITCH; int x,y; for( ; p_line != NULL; p_line = p_line->p_next ) { int i_glyph_tmax=0, i = 0; int i_bitmap_offset, i_offset, i_align_offset = 0; for( i = 0; p_line->pp_glyphs[i] != NULL; i++ ) { FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ]; i_glyph_tmax = __MAX( i_glyph_tmax, p_glyph->top ); } if( p_line->i_width < i_width ) { if( ( p_region->p_style && p_region->p_style->i_text_align == SUBPICTURE_ALIGN_RIGHT ) || ( !p_region->p_style && (p_region->i_align & 0x3) == SUBPICTURE_ALIGN_RIGHT ) ) { i_align_offset = i_width - p_line->i_width; } else if( ( p_region->p_style && p_region->p_style->i_text_align != SUBPICTURE_ALIGN_LEFT ) || ( !p_region->p_style && (p_region->i_align & 0x3) != SUBPICTURE_ALIGN_LEFT ) ) { i_align_offset = ( i_width - p_line->i_width ) / 2; } } for( i = 0; p_line->pp_glyphs[i] != NULL; i++ ) { FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ]; i_offset = ( p_line->p_glyph_pos[ i ].y + i_glyph_tmax - p_glyph->top + 3 + yoffset ) * i_pitch + p_line->p_glyph_pos[ i ].x + p_glyph->left + 3 + i_align_offset +xoffset; for( y = 0, i_bitmap_offset = 0; y < p_glyph->bitmap.rows; y++ ) { for( x = 0; x < p_glyph->bitmap.width; x++, i_bitmap_offset++ ) { if( p_glyph->bitmap.buffer[i_bitmap_offset] ) if( p_dst[i_offset+x] < ((int)p_glyph->bitmap.buffer[i_bitmap_offset]) ) p_dst[i_offset+x] = ((int)p_glyph->bitmap.buffer[i_bitmap_offset]); } i_offset += i_pitch; } } } }/***************************************************************************** * Render: place string in picture ***************************************************************************** * This function merges the previously rendered freetype glyphs into a picture *****************************************************************************/static int RenderYUVA( filter_t *p_filter, subpicture_region_t *p_region, line_desc_t *p_line, int i_width, int i_height ){ uint8_t *p_dst_y,*p_dst_u,*p_dst_v,*p_dst_a; video_format_t fmt; int i, x, y, i_pitch, i_alpha; uint8_t i_y, i_u, i_v; /* YUV values, derived from incoming RGB */ subpicture_region_t *p_region_tmp; if( i_width == 0 || i_height == 0 ) return VLC_SUCCESS; /* Create a new subpicture region */ memset( &fmt, 0, sizeof(video_format_t) ); fmt.i_chroma = VLC_FOURCC('Y','U','V','A'); fmt.i_aspect = 0; fmt.i_width = fmt.i_visible_width = i_width + 6; fmt.i_height = fmt.i_visible_height = i_height + 6; fmt.i_x_offset = fmt.i_y_offset = 0; p_region_tmp = spu_CreateRegion( p_filter, &fmt ); if( !p_region_tmp ) { msg_Err( p_filter, "cannot allocate SPU region" ); return VLC_EGENERIC; } p_region->fmt = p_region_tmp->fmt; p_region->picture = p_region_tmp->picture; free( p_region_tmp ); /* Calculate text color components */ i_y = (uint8_t)__MIN(abs( 2104 * p_line->i_red + 4130 * p_line->i_green + 802 * p_line->i_blue + 4096 + 131072 ) >> 13, 235); i_u = (uint8_t)__MIN(abs( -1214 * p_line->i_red + -2384 * p_line->i_green + 3598 * p_line->i_blue + 4096 + 1048576) >> 13, 240); i_v = (uint8_t)__MIN(abs( 3598 * p_line->i_red + -3013 * p_line->i_green + -585 * p_line->i_blue + 4096 + 1048576) >> 13, 240); i_alpha = p_line->i_alpha; p_dst_y = p_region->picture.Y_PIXELS; p_dst_u = p_region->picture.U_PIXELS; p_dst_v = p_region->picture.V_PIXELS; p_dst_a = p_region->picture.A_PIXELS; i_pitch = p_region->picture.A_PITCH; /* Initialize the region pixels */ if( p_filter->p_sys->i_effect != EFFECT_BACKGROUND ) { memset( p_dst_y, 0x00, i_pitch * p_region->fmt.i_height ); memset( p_dst_u, 0x80, i_pitch * p_region->fmt.i_height ); memset( p_dst_v, 0x80, i_pitch * p_region->fmt.i_height ); memset( p_dst_a, 0, i_pitch * p_region->fmt.i_height ); } else { memset( p_dst_y, 0x0, i_pitch * p_region->fmt.i_height ); memset( p_dst_u, 0x80, i_pitch * p_region->fmt.i_height ); memset( p_dst_v, 0x80, i_pitch * p_region->fmt.i_height ); memset( p_dst_a, 0x80, i_pitch * p_region->fmt.i_height ); } if( p_filter->p_sys->i_effect == EFFECT_OUTLINE || p_filter->p_sys->i_effect == EFFECT_OUTLINE_FAT ) { DrawBlack( p_line, i_width, p_region, 0, 0); DrawBlack( p_line, i_width, p_region, -1, 0); DrawBlack( p_line, i_width, p_region, 0, -1); DrawBlack( p_line, i_width, p_region, 1, 0); DrawBlack( p_line, i_width, p_region, 0, 1); } if( p_filter->p_sys->i_effect == EFFECT_OUTLINE_FAT ) { DrawBlack( p_line, i_width, p_region, -1, -1); DrawBlack( p_line, i_width, p_region, -1, 1); DrawBlack( p_line, i_width, p_region, 1, -1); DrawBlack( p_line, i_width, p_region, 1, 1); DrawBlack( p_line, i_width, p_region, -2, 0); DrawBlack( p_line, i_width, p_region, 0, -2); DrawBlack( p_line, i_width, p_region, 2, 0); DrawBlack( p_line, i_width, p_region, 0, 2); DrawBlack( p_line, i_width, p_region, -2, -2); DrawBlack( p_line, i_width, p_region, -2, 2); DrawBlack( p_line, i_width, p_region, 2, -2); DrawBlack( p_line, i_width, p_region, 2, 2); DrawBlack( p_line, i_width, p_region, -3, 0); DrawBlack( p_line, i_width, p_region, 0, -3); DrawBlack( p_line, i_width, p_region, 3, 0); DrawBlack( p_line, i_width, p_region, 0, 3); } for( ; p_line != NULL; p_line = p_line->p_next ) { int i_glyph_tmax = 0; int i_bitmap_offset, i_offset, i_align_offset = 0; for( i = 0; p_line->pp_glyphs[i] != NULL; i++ ) { FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ]; i_glyph_tmax = __MAX( i_glyph_tmax, p_glyph->top ); } if( p_line->i_width < i_width ) { if( ( p_region->p_style && p_region->p_style->i_text_align == SUBPICTURE_ALIGN_RIGHT ) || ( !p_region->p_style && (p_region->i_align & 0x3) == SUBPICTURE_ALIGN_RIGHT ) ) { i_align_offset = i_width - p_line->i_width; } else if( ( p_region->p_style && p_region->p_style->i_text_align != SUBPICTURE_ALIGN_LEFT ) || ( !p_region->p_style && (p_region->i_align & 0x3) != SUBPICTURE_ALIGN_LEFT ) ) { i_align_offset = ( i_width - p_line->i_width ) / 2; } } for( i = 0; p_line->pp_glyphs[i] != NULL; i++ ) { FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ]; i_offset = ( p_line->p_glyph_pos[ i ].y + i_glyph_tmax - p_glyph->top + 3 ) * i_pitch + p_line->p_glyph_pos[ i ].x + p_glyph->left + 3 + i_align_offset; for( y = 0, i_bitmap_offset = 0; y < p_glyph->bitmap.rows; y++ ) { for( x = 0; x < p_glyph->bitmap.width; x++, i_bitmap_offset++ ) { if( p_glyph->bitmap.buffer[i_bitmap_offset] ) { p_dst_y[i_offset+x] = ((p_dst_y[i_offset+x] *(255-(int)p_glyph->bitmap.buffer[i_bitmap_offset])) + i_y * ((int)p_glyph->bitmap.buffer[i_bitmap_offset])) >> 8; p_dst_u[i_offset+x] = i_u; p_dst_v[i_offset+x] = i_v; if( p_filter->p_sys->i_effect == EFFECT_BACKGROUND ) p_dst_a[i_offset+x] = 0xff; } } i_offset += i_pitch; } } } /* Apply the alpha setting */ for( i = 0; i < (int)fmt.i_height * i_pitch; i++ ) p_dst_a[i] = p_dst_a[i] * (255 - i_alpha) / 255; return VLC_SUCCESS;}/** * This function renders a text subpicture region into another one. * It also calculates the size needed for this string, and renders the * needed glyphs into memory. It is used as pf_add_string callback in * the vout method by this module */static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out, subpicture_region_t *p_region_in ){ filter_sys_t *p_sys = p_filter->p_sys; line_desc_t *p_lines = 0, *p_line = 0, *p_next = 0, *p_prev = 0; int i, i_pen_y, i_pen_x, i_error, i_glyph_index, i_previous; uint32_t *psz_unicode, *psz_unicode_orig = 0, i_char, *psz_line_start; int i_string_length; char *psz_string; vlc_iconv_t iconv_handle = (vlc_iconv_t)(-1); int i_font_color, i_font_alpha, i_font_size, i_red, i_green, i_blue; FT_BBox line; FT_BBox glyph_size; FT_Vector result; FT_Glyph tmp_glyph; /* Sanity check */ if( !p_region_in || !p_region_out ) return VLC_EGENERIC; psz_string = p_region_in->psz_text; if( !psz_string || !*psz_string ) return VLC_EGENERIC; if( p_region_in->p_style ) { i_font_color = __MAX( __MIN( p_region_in->p_style->i_font_color, 0xFFFFFF ), 0 ); i_font_alpha = __MAX( __MIN( p_region_in->p_style->i_font_alpha, 255 ), 0 ); i_font_size = __MAX( __MIN( p_region_in->p_style->i_font_size, 255 ), 0 );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -