C++字体库开发之go语言绑定六

export.h
cpp 复制代码
#define FONTVIEW_API __declspec(dllimport)
typedef void* GoFontSetPtr;


#ifdef __cplusplus
extern "C"{
#endif
FONTVIEW_API GoFontSetPtr openFontSet(const char* stream,size_t size);
FONTVIEW_API void freeFontSet(GoFontSetPtr fontset);
#ifdef __cplusplus
}
#endif
fontview.go
Go 复制代码
/*
#cgo CXXFLAGS: -std=c++11
#cgo CFLAGS: -I${SRCDIR}/include
#cgo LDFLAGS: -L${SRCDIR}/lib -lstdc++ -lfontview_d -lfreetyped -lharfbuzz_d
#include "export.h"
#include <stdlib.h>
*/

import "C"
import (
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"path/filepath"
	"strings"
	"unsafe"
)

func parseFont(path string) (err error) {
	if len(path) == 0 {
		return
	}

	// file name
	_, fileName := filepath.Split(path)

	// read file
	b, err := ioutil.ReadFile(path)
	if err != nil {
		fmt.Printf("FontType.ReadFile err: %v, fileName: %v\n", err, fileName)
		return
	}

	// 解析字体集
	cData := (*C.char)(unsafe.Pointer(&(b[0])))
	cDataLen := GoIntToCInt(len(b))
	fontSet := C.openFontSet(cData, cDataLen)
	if fontSet == nil {
		fmt.Printf("open font return nil, fileName: %v\n", fileName)
		err = errors.New("open font return nil")
		return
	}
	defer C.freeFontSet(fontSet)
	return
}
输出

parse FontType path: C:\Windows\Fonts\kartikab.ttf

parse FontType path: C:\Windows\Fonts\kokila.ttf

parse FontType path: C:\Windows\Fonts\kokilab.ttf

parse FontType path: C:\Windows\Fonts\kokilabi.ttf

parse FontType path: C:\Windows\Fonts\kokilai.ttf

parse FontType path: C:\Windows\Fonts\l_10646.ttf

参考

C++字体库开发之fontconfig使用五-CSDN博客


创作不易,小小的支持一下吧!

相关推荐
Larry_Yanan1 小时前
Qt多进程(六)共享内存和信号量
开发语言·qt
BD_Marathon1 小时前
bean基础配置
java·开发语言
东方忘忧1 小时前
Qt使用QDesktopServices::openUrl打开系统默认应用(如浏览器,文件,文件夹和邮件)
开发语言·qt
计算机内卷的N天1 小时前
qt的模态和非模态状态
开发语言·qt
锦瑟弦音2 小时前
go2rtc实现获取rtsp视频到vue
go
半桶水专家8 小时前
go语言中的结构体嵌入详解
开发语言·后端·golang
在屏幕前出油9 小时前
二、Python面向对象编程基础——理解self
开发语言·python
粉红色回忆9 小时前
用链表实现了简单版本的malloc/free函数
数据结构·c++
阿方索9 小时前
python文件与数据格式化
开发语言·python
写代码的小球10 小时前
C++计算器(学生版)
c++·算法