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博客


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

相关推荐
饮水桶_T38 分钟前
Git笔记
开发语言·笔记·算法
国科安芯38 分钟前
让每一瓦热都听话:航天器主动热控系统CANFD控制网络的可靠性设计
开发语言·网络·单片机·嵌入式硬件·信息与通信·抗辐射·载荷
CoderYanger1 小时前
Java EE 进阶:12.Spring AOP(第一弹)
java·开发语言·spring boot·后端·spring·java-ee·交互
松仔log1 小时前
Kotlin中级——KMP
android·开发语言·kotlin
H H.1 小时前
Java笔记:String字符串
java·开发语言·经验分享·笔记·其他
鲸渔2 小时前
【AI Agent系列课程26-Python 基础26】复盘:Python 接口开发知识清单
开发语言·python·ai编程
sharemylove2 小时前
Java 异常处理 学习笔记
java·开发语言·经验分享·笔记·学习
西西弗Sisyphus2 小时前
Qt QML 程序是怎么跑起来的(1)qml-only
开发语言·c++·qt
Sailing3 小时前
🔥🔥还在用 any 硬撑通用层?Go 泛型再不补,下一轮重构你就接不住了
后端·面试·go
慧都产品智能化3 小时前
汽车软件工具鉴定:范过度扩张带来的测试与文档成本
c++·测试工具·单元测试·代码复审