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


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

相关推荐
java1234_小锋10 分钟前
一周学会Matplotlib3 Python 数据可视化-绘制自相关图
开发语言·python·信息可视化·matplotlib·matplotlib3
甄超锋11 分钟前
Java Maven更换国内源
java·开发语言·spring boot·spring·spring cloud·tomcat·maven
旺小仔.25 分钟前
双指针和codetop复习
数据结构·c++·算法
凢en42 分钟前
Perl——qw()函数
开发语言·perl
jingfeng51443 分钟前
C++ STL-string类底层实现
前端·c++·算法
郝学胜-神的一滴1 小时前
基于C++的词法分析器:使用正则表达式的实现
开发语言·c++·程序人生·正则表达式·stl
努力努力再努力wz2 小时前
【c++深入系列】:万字详解模版(下)
java·c++·redis
林开落L3 小时前
库的制作与原理
linux·开发语言·动静态库·库的制作
m0_480502643 小时前
Rust 入门 泛型和特征-特征对象 (十四)
开发语言·后端·rust
瓦特what?3 小时前
关于C++的#include的超超超详细讲解
java·开发语言·数据结构·c++·算法·信息可视化·数据挖掘