📄 drawtext.c
字号:
/* load and cache glyphs */
yMax = -32000;
yMin = 32000;
for (c=0; c < 256; c++)
{
/* Load char */
error = FT_Load_Char( ci->face, (unsigned char) c, FT_LOAD_RENDER | FT_LOAD_MONOCHROME );
if (error) continue; /* ignore errors */
/* Save bitmap */
ci->bitmaps[c] = ci->face->glyph->bitmap;
/* Save bitmap left */
ci->bitmap_left[c] = ci->face->glyph->bitmap_left;
/* Save bitmap top */
ci->bitmap_top[c] = ci->face->glyph->bitmap_top;
/* Save advance */
ci->advance[c] = ci->face->glyph->advance.x >> 6;
/* Save glyph */
error = FT_Get_Glyph( ci->face->glyph, &(ci->glyphs[c]) );
/* Save glyph index */
ci->glyphs_index[c] = FT_Get_Char_Index( ci->face, (unsigned char) c );
/* Measure text height to calculate text_height (or the maximum text height) */
FT_Glyph_Get_CBox( ci->glyphs[ c ], ft_glyph_bbox_pixels, &bbox );
if (bbox.yMax > yMax)
yMax = bbox.yMax;
if (bbox.yMin < yMin)
yMin = bbox.yMin;
}
ci->text_height = yMax - yMin;
ci->baseline = yMax;
return 0;
}
static inline void draw_glyph(AVPicture *picture, FT_Bitmap *bitmap, unsigned int x, unsigned int y, unsigned int width, unsigned int height, unsigned char yuv_fgcolor[3], unsigned char yuv_bgcolor[3], int outline)
{
int r, c;
int spixel, dpixel[3], in_glyph=0;
if (bitmap->pixel_mode == ft_pixel_mode_mono)
{
in_glyph = 0;
for (r=0; (r < bitmap->rows) && (r+y < height); r++)
{
for (c=0; (c < bitmap->width) && (c+x < width); c++)
{
/* pixel in the picture (destination) */
GET_PIXEL(picture, dpixel, (c+x), (y+r));
/* pixel in the glyph bitmap (source) */
spixel = bitmap->buffer[r*bitmap->pitch +c/8] & (0x80>>(c%8));
if (spixel)
COPY_3(dpixel, yuv_fgcolor);
if (outline)
{
/* border detection: */
if ( (!in_glyph) && (spixel) )
/* left border detected */
{
in_glyph = 1;
/* draw left pixel border */
if (c-1 >= 0)
SET_PIXEL(picture, yuv_bgcolor, (c+x-1), (y+r));
}
else if ( (in_glyph) && (!spixel) )
/* right border detected */
{
in_glyph = 0;
/* 'draw' right pixel border */
COPY_3(dpixel, yuv_bgcolor);
}
if (in_glyph)
/* see if we have a top/bottom border */
{
/* top */
if ( (r-1 >= 0) && (! bitmap->buffer[(r-1)*bitmap->pitch +c/8] & (0x80>>(c%8))) )
/* we have a top border */
SET_PIXEL(picture, yuv_bgcolor, (c+x), (y+r-1));
/* bottom */
if ( (r+1 < height) && (! bitmap->buffer[(r+1)*bitmap->pitch +c/8] & (0x80>>(c%8))) )
/* we have a bottom border */
SET_PIXEL(picture, yuv_bgcolor, (c+x), (y+r+1));
}
}
SET_PIXEL(picture, dpixel, (c+x), (y+r));
}
}
}
}
static inline void draw_box(AVPicture *picture, unsigned int x, unsigned int y, unsigned int width, unsigned int height, unsigned char yuv_color[3])
{
int i, j;
for (j = 0; (j < height); j++)
for (i = 0; (i < width); i++)
{
SET_PIXEL(picture, yuv_color, (i+x), (y+j));
}
}
void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
{
ContextInfo *ci = (ContextInfo *) ctx;
FT_Face face = ci->face;
FT_GlyphSlot slot = face->glyph;
unsigned char *text = ci->text;
unsigned char c;
int x = 0, y = 0, i=0, size=0;
unsigned char buff[MAXSIZE_TEXT];
unsigned char tbuff[MAXSIZE_TEXT];
time_t now = time(0);
int str_w, str_w_max;
FT_Vector pos[MAXSIZE_TEXT];
FT_Vector delta;
if (ci->file)
{
int fd = open(ci->file, O_RDONLY);
if (fd < 0)
{
text = ci->text;
av_log(NULL, AV_LOG_INFO, "WARNING: The file could not be opened. Using text provided with -t switch: %s", strerror(errno));
}
else
{
int l = read(fd, tbuff, sizeof(tbuff) - 1);
if (l >= 0)
{
tbuff[l] = 0;
text = tbuff;
}
else
{
text = ci->text;
av_log(NULL, AV_LOG_INFO, "WARNING: The file could not be read. Using text provided with -t switch: %s", strerror(errno));
}
close(fd);
}
}
else
{
text = ci->text;
}
strftime(buff, sizeof(buff), text, localtime(&now));
text = buff;
size = strlen(text);
/* measure string size and save glyphs position*/
str_w = str_w_max = 0;
x = ci->x;
y = ci->y;
for (i=0; i < size; i++)
{
c = text[i];
/* kerning */
if ( (ci->use_kerning) && (i > 0) && (ci->glyphs_index[c]) )
{
FT_Get_Kerning( ci->face,
ci->glyphs_index[ text[i-1] ],
ci->glyphs_index[c],
ft_kerning_default,
&delta );
x += delta.x >> 6;
}
if (( (x + ci->advance[ c ]) >= width ) || ( c == '\n' ))
{
str_w = width - ci->x - 1;
y += ci->text_height;
x = ci->x;
}
/* save position */
pos[i].x = x + ci->bitmap_left[c];
pos[i].y = y - ci->bitmap_top[c] + ci->baseline;
x += ci->advance[c];
if (str_w > str_w_max)
str_w_max = str_w;
}
if (ci->bg)
{
/* Check if it doesn't pass the limits */
if ( str_w_max + ci->x >= width )
str_w_max = width - ci->x - 1;
if ( y >= height )
y = height - 1 - 2*ci->y;
/* Draw Background */
draw_box( picture, ci->x, ci->y, str_w_max, y - ci->y, ci->bgcolor );
}
/* Draw Glyphs */
for (i=0; i < size; i++)
{
c = text[i];
if (
( (c == '_') && (text == ci->text) ) || /* skip '_' (consider as space)
IF text was specified in cmd line
(which doesn't like neasted quotes) */
( c == '\n' ) /* Skip new line char, just go to new line */
)
continue;
/* now, draw to our target surface */
draw_glyph( picture,
&(ci->bitmaps[ c ]),
pos[i].x,
pos[i].y,
width,
height,
ci->fgcolor,
ci->bgcolor,
ci->outline );
/* increment pen position */
x += slot->advance.x >> 6;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -