/* 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_BitmapFont2.h" #define stemobject_implementation UITypeface_BitmapFont2 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_entry(encodeCharacterIndexString); stemobject_vtable_end(); UITypeface_BitmapFont2 * UITypeface_BitmapFont2_create(BitmapFont2 * font, float scale, int spacingAdjustment, BitmapFont_drawOptions drawOptions) { stemobject_create_implementation(init, font, scale, spacingAdjustment, drawOptions) } bool UITypeface_BitmapFont2_init(UITypeface_BitmapFont2 * self, BitmapFont2 * font, float scale, int spacingAdjustment, BitmapFont_drawOptions drawOptions) { call_super(init, self); self->font = font; self->scale = scale; self->spacingAdjustment = spacingAdjustment; self->drawOptions = drawOptions; return true; } void UITypeface_BitmapFont2_dispose(UITypeface_BitmapFont2 * self) { call_super_virtual(dispose, self); } float UITypeface_BitmapFont2_getLineHeight(UITypeface_BitmapFont2 * self) { return self->font->height * self->scale; } float UITypeface_BitmapFont2_measureString(UITypeface_BitmapFont2 * self, String string) { return BitmapFont2_measureString(self->font, string, self->spacingAdjustment, self->drawOptions) * self->scale; } float UITypeface_BitmapFont2_positionXAtIndex(UITypeface_BitmapFont2 * self, String string, size_t index) { return BitmapFont2_positionXAtIndex(self->font, string, index, self->spacingAdjustment, self->drawOptions) * self->scale; } size_t UITypeface_BitmapFont2_indexAtPositionX(UITypeface_BitmapFont2 * self, String string, float positionX, bool * outLeadingEdge) { return BitmapFont2_indexAtPositionX(self->font, string, positionX / self->scale, self->spacingAdjustment, self->drawOptions, outLeadingEdge); } struct glyphCallbackContext { UITypeface_glyphCallback callback; void * callbackContext; }; static void glyphCallback(BitmapFont2 * font, Rect4f vertexBounds, Rect4f textureBounds, bool fixedColor, void * context) { struct glyphCallbackContext * contextStruct = context; contextStruct->callback(vertexBounds, textureBounds, fixedColor, contextStruct->callbackContext); } void UITypeface_BitmapFont2_writeGlyphsWithCallback(UITypeface_BitmapFont2 * self, String string, Vector2f offset, float scale, UITypeface_glyphCallback callback, void * callbackContext) { struct glyphCallbackContext contextStruct = {callback, callbackContext}; BitmapFont2_writeStringWithCallback(self->font, string, offset, VECTOR2f(0.0f, 1.0f), self->scale * scale, self->spacingAdjustment, self->drawOptions, glyphCallback, &contextStruct); } uint32_t UITypeface_BitmapFont2_characterIndexToCodepoint(UITypeface_BitmapFont2 * self, unsigned int characterIndex) { if (characterIndex >= self->font->characterCount) { return UINT32_MAX; } return self->font->characters[characterIndex].codepoint; } unsigned int UITypeface_BitmapFont2_codepointToCharacterIndex(UITypeface_BitmapFont2 * self, uint32_t codepoint) { return BitmapFont2_getCharacterIndex(self->font, codepoint); } String UITypeface_BitmapFont2_encodeCharacterIndexString(UITypeface_BitmapFont2 * self, String string) { return BitmapFont2_encodeCharacterIndexString(self->font, string); } String UITypeface_BitmapFont2_decodeCharacterIndexString(UITypeface_BitmapFont2 * self, String string, TextEncoding encoding) { return BitmapFont2_decodeCharacterIndexString(self->font, string, encoding); }