您尚未登录。

楼主 #1 2018-02-04 09:02:48

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

参考网上例程,做了一个用libfreetype解析微软雅黑msyh.ttf文件的demo

// test.c
// gcc -o test test.c `freetype-config --cflags`   `freetype-config --libs`
#include <freetype2/ft2build.h>
#include <freetype2/freetype.h>

static FT_Library library;
static FT_Face face;

int LoadChar(wchar_t cvalue, int textsize, int *x, int* bitmap_left, 
      int* bitmap_top, int* rows, int* width, unsigned char** buf)
{
	int ret = FT_Load_Char(face, cvalue, FT_LOAD_RENDER); //0=SUCCESS, otherwise=FAIL
	*x = face->glyph->advance.x;
	*bitmap_left = face->glyph->bitmap_left;
	*bitmap_top = face->glyph->bitmap_top;
	*rows = face->glyph->bitmap.rows;
	*width = face->glyph->bitmap.width;
	*buf = face->glyph->bitmap.buffer;

	printf("%s(%s)\n", __FILE__, __FUNCTION__);
	printf("-------------------------\n");
	printf("%15s = %ld\n", "advance.x", face->glyph->advance.x);
	printf("%15s = %d\n", "bitmap_left", face->glyph->bitmap_left);
	printf("%15s = %d\n", "bitmap_top", face->glyph->bitmap_top);
	printf("%15s = %d\n", "bitmap.rows", face->glyph->bitmap.rows);
	printf("%15s = %d\n", "bitmap.width", face->glyph->bitmap.width);
	printf("%15s = %ld\n", "bitmap.buffer", sizeof(face->glyph->bitmap.buffer));
	printf("%15s = %ld\n", "bitmap_buffer", strlen(face->glyph->bitmap.buffer));
	printf("-------------------------\n");
	unsigned char*  buffer; 
	buffer = face->glyph->bitmap.buffer;
	int i=0, j=0;
	int height = *rows;
	int wid = *width;

	for(j = 0; j < height; j++)
	{
		for(i = 0; i < wid ; i++)
		{
			printf("%02x ", buffer[i + wid*j]);
		}
		printf("\n");
	}

	return 1;
  }

int main(int argc, char** argv)
{
	printf("%s(%s)\n", __FILE__, __FUNCTION__);
	int bError = FT_Init_FreeType(&library);
	if(bError) return 0;
	bError = FT_New_Face(library,(char*)"[ins]/mnt/hgfs/D/Downloads/msyh.ttf[/ins]",0, &face);
	if(bError) return 0;

	int textHeight = 20;
	FT_Set_Pixel_Sizes(face, textHeight, 0);

	int left, top, rows, width, x;
	unsigned char* buf;
	wchar_t ch = 'W';
	LoadChar(ch, textHeight, &x, &left, &top, &rows, &width, &buf);
	return 0;   
}

W输出:

hexing@ubuntu:/tmp$ ./test 
test.c(main)
test.c(LoadChar)
-------------------------
      advance.x = 1280
    bitmap_left = 0
     bitmap_top = 15
    bitmap.rows = 15
   bitmap.width = 20
  bitmap.buffer = 8
  bitmap_buffer = 3
-------------------------
de ff 1f 00 00 00 00 00 29 ff ff 29 00 00 00 00 00 1e ff dd 
9a ff 5d 00 00 00 00 00 7a ff ff 7b 00 00 00 00 00 5b ff 99 
56 ff 9b 00 00 00 00 00 cc ff ff cd 00 00 00 00 00 98 ff 55 
13 fd d9 00 00 00 00 1e ff e3 e7 ff 1f 00 00 00 00 d5 fd 12 
00 cd ff 18 00 00 00 6f ff a6 af ff 71 00 00 00 13 fe cc 00 
00 89 ff 56 00 00 00 c0 ff 50 5a ff c3 00 00 00 4e ff 88 00 
00 45 ff 94 00 00 15 fc f0 07 0b f4 fd 17 00 00 8b ff 44 00 
00 08 f7 d2 00 00 63 ff 9e 00 00 a5 ff 67 00 00 c8 f7 08 00 
00 00 bc fe 12 00 b5 ff 44 00 00 4b ff b9 00 0a fa bb 00 00 
00 00 78 ff 4f 0d f8 e7 03 00 00 05 ec fa 10 42 ff 77 00 00 
00 00 34 ff 8d 58 ff 91 00 00 00 00 97 ff 5e 7e ff 33 00 00 
00 00 02 ed c7 a7 ff 38 00 00 00 00 3c ff ae b9 ec 02 00 00 
00 00 00 ab ee e9 de 00 00 00 00 00 01 e0 ea e9 aa 00 00 00 
00 00 00 67 ff ff 85 00 00 00 00 00 00 88 ff ff 66 00 00 00 
00 00 00 22 ff ff 2c 00 00 00 00 00 00 2d ff ff 22 00 00 00 

T输出:

hexing@ubuntu:/tmp$ ./test 
test.c(main)
test.c(LoadChar)
-------------------------
      advance.x = 704
    bitmap_left = 1
     bitmap_top = 15
    bitmap.rows = 15
   bitmap.width = 10
  bitmap.buffer = 8
  bitmap_buffer = 20
-------------------------
ff ff ff ff ff ff ff ff ff ff 
ff ff ff ff ff ff ff ff ff ff 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 
00 00 00 00 ff ff 00 00 00 00 




离线

楼主 #2 2018-02-04 09:10:09

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: 参考网上例程,做了一个用libfreetype解析微软雅黑msyh.ttf文件的demo

如果要想顯示漢字,改動一行即可:

wchar_t ch = L'漢';





离线

#3 2018-08-01 09:08:31

abc3240660
会员
注册时间: 2018-07-31
已发帖子: 100
积分: 100

Re: 参考网上例程,做了一个用libfreetype解析微软雅黑msyh.ttf文件的demo

stm32上能用不?

离线

#4 2018-08-01 14:31:44

达克罗德
会员
注册时间: 2018-04-10
已发帖子: 1,134
积分: 1086.5

Re: 参考网上例程,做了一个用libfreetype解析微软雅黑msyh.ttf文件的demo

abc3240660 说:

stm32上能用不?

我见多F4的项目用了。随着现在芯片越来越强大越来越便宜,矢量字体应该会流行

离线

#5 2018-08-01 15:37:26

abc3240660
会员
注册时间: 2018-07-31
已发帖子: 100
积分: 100

Re: 参考网上例程,做了一个用libfreetype解析微软雅黑msyh.ttf文件的demo

stm32上 字体太大时 耗时好多啊

比如正常的一个循环延时100ms用delay_ms(100),但是绘大字体后,delay_ms(90),才能让一个循环是100ms

离线

#6 2018-08-02 21:47:05

sblpp
会员
注册时间: 2018-02-14
已发帖子: 164
积分: 64

Re: 参考网上例程,做了一个用libfreetype解析微软雅黑msyh.ttf文件的demo

谢谢分享!

离线

#7 2019-01-25 11:18:57

trigger
会员
注册时间: 2018-12-30
已发帖子: 41
积分: 41

Re: 参考网上例程,做了一个用libfreetype解析微软雅黑msyh.ttf文件的demo

20190125111232.png

文件下载: freetype-2_9_1_20190125.7z

工程路径: freetype-2.9.1\builds\windows\vc2010\freetype.sln

可以用 VC2015 打开/编译/调试

离线

#8 2019-04-10 01:15:45

Hinsun
会员
注册时间: 2019-04-09
已发帖子: 2
积分: 2

Re: 参考网上例程,做了一个用libfreetype解析微软雅黑msyh.ttf文件的demo

@晕哥,你这个demo有两个问题请教:
1、FT_Set_Pixel_Sizes(face, textHeight, 0); 这里应该是笔误哈,textHeight和0这两个参数位置要互换?
2、设置的像素高度为20px,但为什么字模点阵里面只有16行的高呢?
是不是freetype有特殊处理,只获取了字的完全高度?如果我想把转换为20px的点阵,有接口支持么?谢谢。

离线

#9 2019-07-11 09:31:52

john78
会员
注册时间: 2018-07-19
已发帖子: 219
积分: 167

Re: 参考网上例程,做了一个用libfreetype解析微软雅黑msyh.ttf文件的demo

这生成的点阵数据和16位置是什么对应关系?
按照这个数据描点,文字有毛刺

离线

#10 2019-12-16 11:47:44

tianjjff
会员
注册时间: 2018-12-24
已发帖子: 129
积分: 22

Re: 参考网上例程,做了一个用libfreetype解析微软雅黑msyh.ttf文件的demo

晕哥,请教下,Makefile不是很懂,这两个是什么作用'freetype-config --cflags`   `freetype-config --libs`

离线

楼主 #11 2019-12-16 13:44:26

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,223
积分: 9197

Re: 参考网上例程,做了一个用libfreetype解析微软雅黑msyh.ttf文件的demo

tianjjff 说:

晕哥,请教下,Makefile不是很懂,这两个是什么作用'freetype-config --cflags`   `freetype-config --libs`

用来生成这个命令行: gcc -o test test.c `freetype-config --cflags`   `freetype-config --libs`

$ freetype-config --cflags --libs
-I/usr/include/freetype2 -I/usr/include/libpng16 -lfreetype

$ pkg-config freetype2 --cflags --libs
-I/usr/include/freetype2 -I/usr/include/libpng16 -lfreetype

其实  /usr/bin/freetype-config 就是一个脚本, 实际上还是调用 pkg-config:

$ whereis freetype-config
freetype-config: /usr/bin/freetype-config /usr/share/man/man1/freetype-config.1.gz
$ cat /usr/bin/freetype-config
#! /bin/sh
#
# Copyright 2000-2017 by
# David Turner, Robert Wilhelm, and Werner Lemberg.
#
# This file is part of the FreeType project, and may only be used, modified,
# and distributed under the terms of the FreeType project license,
# LICENSE.TXT.  By continuing to use, modify, or distribute this file you
# indicate that you have read the license and understand and accept it
# fully.

LC_ALL=C
export LC_ALL


# if `pkg-config' is available, use values from `freetype2.pc'
/usr/bin/pkg-config --atleast-pkgconfig-version 0.24 >/dev/null 2>&1
if test $? -eq 0 ; then
  # note that option `--variable' is not affected by the
  # PKG_CONFIG_SYSROOT_DIR environment variable
  if test "x$SYSROOT" != "x" ; then
    PKG_CONFIG_SYSROOT_DIR="$SYSROOT"
    export PKG_CONFIG_SYSROOT_DIR
  fi

  prefix=`/usr/bin/pkg-config --variable prefix freetype2`
  exec_prefix=`/usr/bin/pkg-config --variable exec_prefix freetype2`

  includedir=`/usr/bin/pkg-config --variable includedir freetype2`
  libdir=`/usr/bin/pkg-config --variable libdir freetype2`

  version=`/usr/bin/pkg-config --modversion freetype2`

  cflags=`/usr/bin/pkg-config --cflags freetype2`
  dynamic_libs=`/usr/bin/pkg-config --libs freetype2`
  static_libs=`/usr/bin/pkg-config --static --libs freetype2`
else
  prefix="/usr"
  exec_prefix="/usr"

  includedir="/usr/include"

  version=21.0.15

  cflags="-I${SYSROOT}$includedir/freetype2"
  dynamic_libs="-lfreetype"
  static_libs="-lfreetype -lz -lpng16 -lm -lz -lm -lz"
fi

orig_prefix=$prefix
orig_exec_prefix=$exec_prefix

orig_includedir=$includedir

include_suffix=`echo $includedir | sed "s|$prefix||"`


usage()
{
  cat <<EOF
Usage: freetype-config [OPTION]...
Get FreeType compilation and linking information.

Options:
  --prefix               display \`--prefix' value used for building the
                         FreeType library
  --prefix=PREFIX        override \`--prefix' value with PREFIX
  --exec-prefix          display \`--exec-prefix' value used for building
                         the FreeType library
  --exec-prefix=EPREFIX  override \`--exec-prefix' value with EPREFIX
  --version              display libtool version of the FreeType library
  --ftversion            display FreeType version number
  --libs                 display flags for linking with the FreeType library
  --libtool              display library name for linking with libtool
  --cflags               display flags for compiling with the FreeType
                         library
  --static               make command line options display flags
                         for static linking
  --help                 display this help and exit
EOF
  exit $1
}


if test $# -eq 0 ; then
  usage 1 1>&2
fi


while test $# -gt 0 ; do
  case "$1" in
  -*=*)
    optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'`
    ;;
  *)
    optarg=
    ;;
  esac

  case $1 in
  --prefix=*)
    prefix=$optarg
    local_prefix=yes
    ;;
  --prefix)
    echo_prefix=yes
    ;;
  --exec-prefix=*)
    exec_prefix=$optarg
    exec_prefix_set=yes
    local_prefix=yes
    ;;
  --exec-prefix)
    echo_exec_prefix=yes
    ;;
  --version)
    echo_version=yes
    break
    ;;
  --ftversion)
    echo_ft_version=yes
    ;;
  --cflags)
    echo_cflags=yes
    ;;
  --libs)
    echo_libs=yes
    ;;
  --libtool)
    echo 2>&1 "The use of libfreetype6.la is deprecated in Debian."
    exit 1
    ;;
  --static)
    show_static=yes
    ;;
  --help)
    usage 0
    ;;
  *)
    usage 1 1>&2
    ;;
  esac
  shift
done


if test "$local_prefix" = "yes" ; then
  if test "$exec_prefix_set" != "yes" ; then
    exec_prefix=$prefix
  fi
fi

if test "$local_prefix" = "yes" ; then
  includedir=${prefix}${include_suffix}
fi


if test "$echo_version" = "yes" ; then
  echo $version
fi

if test "$echo_prefix" = "yes" ; then
  echo ${SYSROOT}$prefix
fi

if test "$echo_exec_prefix" = "yes" ; then
  echo ${SYSROOT}$exec_prefix
fi

if test "$echo_ft_version" = "yes" ; then
  major=`grep define ${SYSROOT}$includedir/freetype2/freetype/freetype.h \
         | grep FREETYPE_MAJOR \
         | sed 's/.*[   ]\([0-9][0-9]*\).*/\1/'`
  minor=`grep define ${SYSROOT}$includedir/freetype2/freetype/freetype.h \
         | grep FREETYPE_MINOR \
         | sed 's/.*[   ]\([0-9][0-9]*\).*/\1/'`
  patch=`grep define ${SYSROOT}$includedir/freetype2/freetype/freetype.h \
         | grep FREETYPE_PATCH \
         | sed 's/.*[   ]\([0-9][0-9]*\).*/\1/'`
  echo $major.$minor.$patch
fi

if test "$echo_cflags" = "yes" ; then
  echo $cflags | sed "s|$orig_includedir/freetype2|$includedir/freetype2|"
fi

if test "$echo_libs" = "yes" ; then
  if test "$show_static" = "yes" ; then
    libs="$static_libs"
  else
    libs="$dynamic_libs"
  fi
  echo $libs
fi

# EOF




离线

#12 2019-12-16 13:58:13

tianjjff
会员
注册时间: 2018-12-24
已发帖子: 129
积分: 22

Re: 参考网上例程,做了一个用libfreetype解析微软雅黑msyh.ttf文件的demo

懂了,谢谢晕哥!!!

离线

#13 2020-07-30 20:46:37

cjslc001
会员
注册时间: 2020-07-30
已发帖子: 8
积分: 8

Re: 参考网上例程,做了一个用libfreetype解析微软雅黑msyh.ttf文件的demo

感觉不错,不知道整个代码有多大,可以考虑下代价。

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn