freetype描边和半透明实现

可以参考官网的另外一个c++实现的版本 :  https://www.freetype.org/freetype2/docs/tutorial/example2.cpp

 

我把它转化为C代码如下: 


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include<malloc.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H
#include FT_STROKER_H
#include FT_IMAGE_H
#define WIDTH   80
#define HEIGHT  60


#define uint8 unsigned char
#define uint16 unsigned short
/* origin is the upper left corner */
unsigned char image[HEIGHT][WIDTH];

struct Pixel32
{
	uint8 a;
	uint8 r;
	uint8 g;
	uint8 b;
};

struct Span {
	int x;
	int y;
	int width;
	int coverage;
};

typedef struct Lspan {
	struct Span node;
	struct Lspan *next;
}Node, *PNode;

struct Rect {
	float xmin;
	float xmax;
	float ymin;
	float ymax;
};

struct vec2 {
	float x;
	float y;
};

#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
void Include(const struct vec2 *r, struct Rect *rect)
{
	rect->xmin = MIN(rect->xmin, r->x);
	rect->ymin = MIN(rect->ymin, r->y);
	rect->xmax = MAX(rect->xmax, r->x);
	rect->ymax = MAX(rect->ymax, r->y);
}

void RasterCallback(const int y,
		const int count,
		const FT_Span * const spans,
		void * const user)
{
	PNode sptr = (PNode)user;
	while (sptr->next != NULL)
		sptr = sptr->next;
	int i;
	for (i = 0; i < count; ++i) {
		PNode new = calloc(sizeof(Node), 1);
		if (!new) {
			printf("failed to alloc new node\n");
			break;
		}
		new->next = NULL;
		new->node.x = spans[i].x;
		new->node.y = y;
		new->node.width = spans[i].len;
		new->node.coverage = spans[i].coverage;
		sptr->next = new;
		sptr = sptr->next;
	}
}

void RenderSpans(FT_Library *library,
		FT_Outline *const outl
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值