Skip to content
This repository has been archived by the owner on Dec 31, 2022. It is now read-only.

Add support for FontGlyph #187

Merged
merged 13 commits into from
Sep 22, 2022
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
matrix:
go-version: [ 1.12, 1.18 ]
os: [ macos-latest, windows-latest, ubuntu-latest ]
os: [ macos-latest, ubuntu-latest ]
steps:
- name: Set up Go
uses: actions/setup-go@v1
Expand Down
22 changes: 22 additions & 0 deletions DrawList.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,28 @@ func (list DrawList) AddImageV(textureID TextureID, posMin Vec2, posMax Vec2, uv
C.iggAddImage(list.handle(), C.IggTextureID(textureID), posMinArg, posMaxArg, uvMinArg, uvMaxArg, C.IggPackedColor(tintCol))
}

// AddImageQuad calls AddImageQuadV(textureID, p1, p2, p3, p4,
// Vec2{X: 0, Y: 0}, Vec2{X: 1, Y: 0}, Vec2{X: 1, Y: 1}, Vec2{X: 0, Y: 1},
// Packed(color.White)).
func (list DrawList) AddImageQuad(textureID TextureID, p1 Vec2, p2 Vec2, p3 Vec2, p4 Vec2) {
list.AddImageQuadV(textureID, p1, p2, p3, p4,
Vec2{X: 0, Y: 0}, Vec2{X: 1, Y: 0}, Vec2{X: 1, Y: 1}, Vec2{X: 0, Y: 1},
Packed(color.White))
}

// AddImageQuadV adds an image based on given texture ID, with four UV coordinates.
func (list DrawList) AddImageQuadV(textureID TextureID, p1 Vec2, p2 Vec2, p3 Vec2, p4 Vec2, uv1 Vec2, uv2 Vec2, uv3 Vec2, uv4 Vec2, tintCol PackedColor) {
p1Arg, _ := p1.wrapped()
p2Arg, _ := p2.wrapped()
p3Arg, _ := p3.wrapped()
p4Arg, _ := p4.wrapped()
uv1Arg, _ := uv1.wrapped()
uv2Arg, _ := uv2.wrapped()
uv3Arg, _ := uv3.wrapped()
uv4Arg, _ := uv4.wrapped()
C.iggAddImageQuad(list.handle(), C.IggTextureID(textureID), p1Arg, p2Arg, p3Arg, p4Arg, uv1Arg, uv2Arg, uv3Arg, uv4Arg, C.IggPackedColor(tintCol))
}

// PushClipRect performs render-level scissoring.
// It calls PushClipRectV(min, max, false).
func (list DrawList) PushClipRect(min, max Vec2) {
Expand Down
77 changes: 77 additions & 0 deletions Font.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import "C"
// Font describes one loaded font in an atlas.
type Font uintptr

// FontGlyph represents a single font glyph from a font atlas.
type FontGlyph uintptr

// DefaultFont can be used to refer to the default font of the current font atlas without
// having the actual font reference.
const DefaultFont Font = 0
Expand Down Expand Up @@ -43,3 +46,77 @@ func CalcTextSize(text string, hideTextAfterDoubleHash bool, wrapWidth float32)
func (font Font) handle() C.IggFont {
return C.IggFont(font)
}

// FontSize returns the height of the given font.
func (font Font) FontSize() float32 {
return float32(C.iggFontFontSize(font.handle()))
}

// FindGlyph returns the FontGlyph corresponding to the given rune.
func (font Font) FindGlyph(ch rune) FontGlyph {
return FontGlyph(C.iggFindGlyph(font.handle(), C.int(ch)))
}

func (glyph FontGlyph) handle() C.IggFontGlyph {
return C.IggFontGlyph(glyph)
}

// Colored returns whether the glyph is colored.
func (glyph FontGlyph) Colored() bool {
return C.iggFontGlyphColored(glyph.handle()) != 0
}

// Visible indicates whether the glyph is visible; it will be false for a space character, for example.
func (glyph FontGlyph) Visible() bool {
return C.iggFontGlyphVisible(glyph.handle()) != 0
}

// Codepoint returns the codepoint associated with the glyph.
func (glyph FontGlyph) Codepoint() int {
return int(C.iggFontGlyphCodepoint(glyph.handle()))
}

// AdvanceX returns the horizontal difference to the next character after the glyph is drawn.
func (glyph FontGlyph) AdvanceX() float32 {
return float32(C.iggFontGlyphAdvanceX(glyph.handle()))
}

// X0 returns the lower x coordinate of the glyph.
func (glyph FontGlyph) X0() float32 {
return float32(C.iggFontGlyphX0(glyph.handle()))
}

// Y0 returns the lower y coordinate of the glyph.
func (glyph FontGlyph) Y0() float32 {
return float32(C.iggFontGlyphY0(glyph.handle()))
}

// X1 returns the upper x coordinate of the glyph.
func (glyph FontGlyph) X1() float32 {
return float32(C.iggFontGlyphX1(glyph.handle()))
}

// Y1 returns the upper y coordinate of the glyph.
func (glyph FontGlyph) Y1() float32 {
return float32(C.iggFontGlyphY1(glyph.handle()))
}

// U0 returns the u texture coordinate associated with the X0() vertex of the glyph.
func (glyph FontGlyph) U0() float32 {
return float32(C.iggFontGlyphU0(glyph.handle()))
}

// V0 returns the v texture coordinate associated with the Y0() vertex of the glyph.
func (glyph FontGlyph) V0() float32 {
return float32(C.iggFontGlyphV0(glyph.handle()))
}

// U1 returns the u texture coordinate associated with the X1() vertex of the glyph.
func (glyph FontGlyph) U1() float32 {
return float32(C.iggFontGlyphU1(glyph.handle()))
}

// V1 returns the v texture coordinate associated with the Y1() vertex of the glyph.
func (glyph FontGlyph) V1() float32 {
return float32(C.iggFontGlyphV1(glyph.handle()))
}
5 changes: 5 additions & 0 deletions FontAtlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ func (atlas FontAtlas) SetTextureID(id TextureID) {
C.iggFontAtlasSetTextureID(atlas.handle(), id.handle())
}

// TextureID returns the renderer-specific texture identifier for the font atlas.
func (atlas FontAtlas) TextureID() TextureID {
return TextureID(C.iggFontAtlasGetTextureID(atlas.handle()))
}

// Build pixels data. This is called automatically for you by the TextureData*** functions.
func (atlas FontAtlas) Build() bool {
return C.iggFontAtlasBuild(atlas.handle()) != 0
Expand Down
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/inkyblackness/imgui-go)](https://goreportcard.com/report/github.com/inkyblackness/imgui-go)
[![Lint Status](https://github.com/inkyblackness/imgui-go/workflows/golangci-lint/badge.svg)](https://github.com/inkyblackness/imgui-go/actions)

## Sunsetting announcement
**This wrapper is about to be discontinued. Please consider using https://github.com/AllenDang/cimgui-go instead.
That project is an auto-generated wrapper and makes it easier to be at the latest version of *Dear ImGui*.**

**This repository here, along with the examples, is planned to become read-only on 2022-12-31.**

---

This library is a [Go](https://www.golang.org) wrapper for **[Dear ImGui](https://github.com/ocornut/imgui)**.

This wrapper started as a special-purpose wrapper for use within InkyBlackness.
Expand Down Expand Up @@ -72,7 +80,10 @@ Contributions to support more build environments are happily accepted. See file

## Alternatives

Before this project was created, the following alternatives were considered - and ignored:
Since 2022-08, there is https://github.com/AllenDang/cimgui-go , which is an auto-generated wrapper that
makes it easier to be at the latest version of **Dear ImGui**. It is recommended to use that one instead.

Before inkyblackness/imgui-go was created, the following alternatives were considered - and ignored:
* `kdrag0n/go-imgui` (no longer available). Reasons for dismissal at time of decision:
* Auto-generated bloat, which doesn't help
* Was using old API (1.5x)
Expand Down
5 changes: 5 additions & 0 deletions State.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ func IsWindowHovered() bool {
return IsWindowHoveredV(HoveredFlagsNone)
}

// KeyIndex returns the key index corresponding to an imgui key.
func KeyIndex(key int) int {
return int(C.iggGetKeyIndex(C.int(key)))
}

// IsKeyDown returns true if the corresponding key is currently being held down.
func IsKeyDown(key int) bool {
return C.iggIsKeyDown(C.int(key)) != 0
Expand Down
14 changes: 14 additions & 0 deletions wrapper/DrawList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ void iggAddImage(IggDrawList handle, IggTextureID textureID, IggVec2* pMin, IggV
list->AddImage(reinterpret_cast<ImTextureID>(textureID), *pMinArg, *pMaxArg, *uvMinArg, *uvMaxArg, col);
}

void iggAddImageQuad(IggDrawList handle, IggTextureID textureID, IggVec2* p1, IggVec2* p2, IggVec2* p3, IggVec2* p4, IggVec2* uv1, IggVec2* uv2, IggVec2* uv3, IggVec2* uv4, IggPackedColor col) {
Vec2Wrapper p1Arg(p1);
Vec2Wrapper p2Arg(p2);
Vec2Wrapper p3Arg(p3);
Vec2Wrapper p4Arg(p4);
Vec2Wrapper uv1Arg(uv1);
Vec2Wrapper uv2Arg(uv2);
Vec2Wrapper uv3Arg(uv3);
Vec2Wrapper uv4Arg(uv4);

ImDrawList *list = reinterpret_cast<ImDrawList *>(handle);
list->AddImageQuad(reinterpret_cast<ImTextureID>(textureID), *p1Arg, *p2Arg, *p3Arg, *p4Arg, *uv1Arg, *uv2Arg, *uv3Arg, *uv4Arg, col);
}

void iggPushClipRect(IggDrawList handle, IggVec2 const *min, IggVec2 const *max, IggBool intersectWithCurrentClipRect)
{
ImDrawList *list = reinterpret_cast<ImDrawList *>(handle);
Expand Down
1 change: 1 addition & 0 deletions wrapper/DrawList.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extern void iggAddTriangle(IggDrawList handle, IggVec2 *p1, IggVec2 *p2, IggVec2
extern void iggAddTriangleFilled(IggDrawList handle, IggVec2 *p1, IggVec2 *p2, IggVec2 *p3, IggPackedColor col);
extern void iggAddText(IggDrawList handle, IggVec2 const *pos, IggPackedColor col, const char *text, int length);
extern void iggAddImage(IggDrawList handle, IggTextureID textureID, IggVec2* pMin, IggVec2* pMax, IggVec2* uvMin, IggVec2* uvMax, IggPackedColor col);
extern void iggAddImageQuad(IggDrawList handle, IggTextureID textureID, IggVec2* p1, IggVec2* p2, IggVec2* p3, IggVec2* p4, IggVec2* uv1, IggVec2* uv2, IggVec2* uv3, IggVec2* uv4, IggPackedColor col);

extern void iggPushClipRect(IggDrawList handle, IggVec2 const *min, IggVec2 const *max, IggBool intersectWithCurrentClipRect);
extern void iggPopClipRect(IggDrawList handle);
Expand Down
84 changes: 84 additions & 0 deletions wrapper/Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,87 @@ void iggCalcTextSize(const char *text, int length, IggBool hide_text_after_doubl
{
exportValue(*value, ImGui::CalcTextSize(text, text + length, hide_text_after_double_hash, wrap_width));
}

float iggFontFontSize(IggFont handle)
{
ImFont *font = reinterpret_cast<ImFont *>(handle);
return font->FontSize;
}

IggFontGlyph iggFindGlyph(IggFont handle, int ch)
{
ImFont *font = reinterpret_cast<ImFont *>(handle);
return (IggFontGlyph)font->FindGlyph(ch);
}

int iggFontGlyphColored(IggFontGlyph handle)
{
ImFontGlyph *glyph = reinterpret_cast<ImFontGlyph *>(handle);
return glyph->Colored;
}

int iggFontGlyphVisible(IggFontGlyph handle)
{
ImFontGlyph *glyph = reinterpret_cast<ImFontGlyph *>(handle);
return glyph->Visible;
}

int iggFontGlyphCodepoint(IggFontGlyph handle)
{
ImFontGlyph *glyph = reinterpret_cast<ImFontGlyph *>(handle);
return glyph->Codepoint;
}

float iggFontGlyphAdvanceX(IggFontGlyph handle)
{
ImFontGlyph *glyph = reinterpret_cast<ImFontGlyph *>(handle);
return glyph->AdvanceX;
}

float iggFontGlyphX0(IggFontGlyph handle)
{
ImFontGlyph *glyph = reinterpret_cast<ImFontGlyph *>(handle);
return glyph->X0;
}

float iggFontGlyphY0(IggFontGlyph handle)
{
ImFontGlyph *glyph = reinterpret_cast<ImFontGlyph *>(handle);
return glyph->Y0;
}

float iggFontGlyphX1(IggFontGlyph handle)
{
ImFontGlyph *glyph = reinterpret_cast<ImFontGlyph *>(handle);
return glyph->X1;
}

float iggFontGlyphY1(IggFontGlyph handle)
{
ImFontGlyph *glyph = reinterpret_cast<ImFontGlyph *>(handle);
return glyph->Y1;
}

float iggFontGlyphU0(IggFontGlyph handle)
{
ImFontGlyph *glyph = reinterpret_cast<ImFontGlyph *>(handle);
return glyph->U0;
}

float iggFontGlyphV0(IggFontGlyph handle)
{
ImFontGlyph *glyph = reinterpret_cast<ImFontGlyph *>(handle);
return glyph->V0;
}

float iggFontGlyphU1(IggFontGlyph handle)
{
ImFontGlyph *glyph = reinterpret_cast<ImFontGlyph *>(handle);
return glyph->U1;
}

float iggFontGlyphV1(IggFontGlyph handle)
{
ImFontGlyph *glyph = reinterpret_cast<ImFontGlyph *>(handle);
return glyph->V1;
}
14 changes: 14 additions & 0 deletions wrapper/Font.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ extern void iggPushFont(IggFont handle);
extern void iggPopFont(void);
extern void iggCalcTextSize(const char *text, int length, IggBool hide_text_after_double_hash, float wrap_width, IggVec2 *value);
extern float iggGetFontSize();
extern float iggFontFontSize(IggFont handle);
extern IggFontGlyph iggFindGlyph(IggFont font, int ch);
extern int iggFontGlyphColored(IggFontGlyph glyph);
extern int iggFontGlyphVisible(IggFontGlyph glyph);
extern int iggFontGlyphCodepoint(IggFontGlyph glyph);
extern float iggFontGlyphAdvanceX(IggFontGlyph glyph);
extern float iggFontGlyphX0(IggFontGlyph glyph);
extern float iggFontGlyphY0(IggFontGlyph glyph);
extern float iggFontGlyphX1(IggFontGlyph glyph);
extern float iggFontGlyphY1(IggFontGlyph glyph);
extern float iggFontGlyphU0(IggFontGlyph glyph);
extern float iggFontGlyphV0(IggFontGlyph glyph);
extern float iggFontGlyphU1(IggFontGlyph glyph);
extern float iggFontGlyphV1(IggFontGlyph glyph);

#ifdef __cplusplus
}
Expand Down
6 changes: 6 additions & 0 deletions wrapper/FontAtlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ void iggFontAtlasSetTextureID(IggFontAtlas handle, IggTextureID id)
fontAtlas->SetTexID(reinterpret_cast<ImTextureID>(id));
}

IggTextureID iggFontAtlasGetTextureID(IggFontAtlas handle)
{
ImFontAtlas *fontAtlas = reinterpret_cast<ImFontAtlas *>(handle);
return IggTextureID(fontAtlas->TexID);
}

IggBool iggFontAtlasBuild(IggFontAtlas handle)
{
ImFontAtlas *fontAtlas = reinterpret_cast<ImFontAtlas *>(handle);
Expand Down
1 change: 1 addition & 0 deletions wrapper/FontAtlas.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extern void iggFontAtlasGetTexDataAsAlpha8(IggFontAtlas handle, unsigned char **
extern void iggFontAtlasGetTexDataAsRGBA32(IggFontAtlas handle, unsigned char **pixels,
int *width, int *height, int *bytesPerPixel);
extern void iggFontAtlasSetTextureID(IggFontAtlas handle, IggTextureID id);
extern IggTextureID iggFontAtlasGetTextureID(IggFontAtlas handle);
extern IggBool iggFontAtlasBuild(IggFontAtlas handle);

extern unsigned int iggFontAtlasGetFontBuilderFlags(IggFontAtlas handle);
Expand Down
5 changes: 5 additions & 0 deletions wrapper/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ IggBool iggIsWindowHovered(int flags)
return ImGui::IsWindowHovered(flags) ? 1 : 0;
}

int iggGetKeyIndex(int key)
{
return ImGui::GetKeyIndex(key);
}

IggBool iggIsKeyDown(int key)
{
return ImGui::IsKeyDown(key);
Expand Down
1 change: 1 addition & 0 deletions wrapper/State.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extern IggBool iggIsWindowCollapsed();
extern IggBool iggIsWindowFocused(int flags);
extern IggBool iggIsWindowHovered(int flags);

extern int iggGetKeyIndex(int key);
extern IggBool iggIsKeyDown(int key);
extern IggBool iggIsKeyPressed(int key, IggBool repeat);
extern IggBool iggIsKeyReleased(int key);
Expand Down
2 changes: 2 additions & 0 deletions wrapper/Types.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <stddef.h>
#include <stdint.h>

#ifdef __cplusplus
Expand All @@ -17,6 +18,7 @@ typedef void *IggDrawList;
typedef void *IggFontAtlas;
typedef void *IggFontConfig;
typedef void *IggFont;
typedef void *IggFontGlyph;
typedef void *IggGlyphRanges;
typedef void *IggGuiStyle;
typedef void *IggInputTextCallbackData;
Expand Down