/* Copyright (c) 2023 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "uitoolkit/UITypeface_BitmapFont.h" #define stemobject_implementation UITypeface_BitmapFont stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_entry(getLineHeight); stemobject_vtable_entry(measureString); stemobject_vtable_entry(positionXAtIndex); stemobject_vtable_entry(indexAtPositionX); stemobject_vtable_entry(writeGlyphsWithCallback); stemobject_vtable_entry(characterIndexToCodepoint); stemobject_vtable_entry(codepointToCharacterIndex); stemobject_vtable_end(); UITypeface_BitmapFont * UITypeface_BitmapFont_create(BitmapFont * bitmapFont, BitmapFont_drawOptions drawOptions) { stemobject_create_implementation(init, bitmapFont, drawOptions) } bool UITypeface_BitmapFont_init(UITypeface_BitmapFont * self, BitmapFont * font, BitmapFont_drawOptions drawOptions) { call_super(init, self); self->font = font; self->drawOptions = drawOptions; return true; } void UITypeface_BitmapFont_dispose(UITypeface_BitmapFont * self) { call_super_virtual(dispose, self); } float UITypeface_BitmapFont_getLineHeight(UITypeface_BitmapFont * self) { return self->font->pointSize; } float UITypeface_BitmapFont_measureString(UITypeface_BitmapFont * self, String string) { return BitmapFont_measureString(self->font, string.bytes, string.length, self->drawOptions) * self->font->pointSize; } float UITypeface_BitmapFont_positionXAtIndex(UITypeface_BitmapFont * self, String string, size_t index) { return BitmapFont_measureString(self->font, string.bytes, index, self->drawOptions) * self->font->pointSize; } size_t UITypeface_BitmapFont_indexAtPositionX(UITypeface_BitmapFont * self, String string, float positionX, bool * outLeadingEdge) { return BitmapFont_indexAtPositionX(self->font, string.bytes, string.length, self->font->pointSize, positionX, 0.0f, self->drawOptions, outLeadingEdge); } struct glyphCallbackContext { UITypeface_glyphCallback callback; void * callbackContext; }; static void glyphCallback(BitmapFont * font, Rect4f vertexBounds, Rect4f textureBounds, void * context) { struct glyphCallbackContext * contextStruct = context; contextStruct->callback(vertexBounds, textureBounds, false, contextStruct->callbackContext); } void UITypeface_BitmapFont_writeGlyphsWithCallback(UITypeface_BitmapFont * self, String string, Vector2f offset, float scale, UITypeface_glyphCallback callback, void * callbackContext) { struct glyphCallbackContext contextStruct = {callback, callbackContext}; BitmapFont_writeStringWithCallback(self->font, string.bytes, string.length, self->font->pointSize * scale, offset, VECTOR2f(0.0f, 1.0f), self->drawOptions, glyphCallback, &contextStruct); } uint32_t UITypeface_BitmapFont_characterIndexToCodepoint(UITypeface_BitmapFont * self, unsigned int characterIndex) { if (characterIndex < BITMAPFONT_NUM_CHARS) { return characterIndex + BITMAPFONT_PRINTABLE_MIN; } return UINT32_MAX; } unsigned int UITypeface_BitmapFont_codepointToCharacterIndex(UITypeface_BitmapFont * self, uint32_t codepoint) { if (codepoint >= BITMAPFONT_PRINTABLE_MIN && codepoint <= BITMAPFONT_PRINTABLE_MAX) { return codepoint - BITMAPFONT_PRINTABLE_MIN; } return UINT_MAX; }