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


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

相关推荐
小码氓6 分钟前
Java填充Word模板
java·开发语言·spring·word
暮鹤筠23 分钟前
[C语言初阶]操作符
c语言·开发语言
蜉蝣之翼❉2 小时前
CRT 不同会导致 fopen 地址不同
c++·mfc
Boilermaker19922 小时前
【Java EE】Mybatis-Plus
java·开发语言·java-ee
aramae2 小时前
C++ -- STL -- vector
开发语言·c++·笔记·后端·visual studio
Tony小周2 小时前
实现一个点击输入框可以弹出的数字软键盘控件 qt 5.12
开发语言·数据库·qt
lixzest3 小时前
C++ Lambda 表达式详解
服务器·开发语言·c++·算法
沉默媛3 小时前
如何安装python以及jupyter notebook
开发语言·python·jupyter
_Chipen4 小时前
C++基础问题
开发语言·c++
灿烂阳光g4 小时前
OpenGL 2. 着色器
c++·opengl