/* DO NOT EDIT THIS FILE */
/* This file is autogenerated by the text-database code generator */

#include <stdarg.h>
#include <touchgfx/TextProvider.hpp>
#include <touchgfx/Texts.hpp>
#include <touchgfx/TypedText.hpp>
#include <touchgfx/Unicode.hpp>
#include <touchgfx/hal/HAL.hpp>
#include <touchgfx/lcd/LCD.hpp>
#include <texts/TypedTextDatabase.hpp>

uint16_t touchgfx::Font::getStringWidth(const touchgfx::Unicode::UnicodeChar* text, ...) const
{
    va_list pArg;
    va_start(pArg, text);
    uint16_t width = getStringWidth<%= is_rtl? ? 'RTL' : 'LTR' %>(TEXT_DIRECTION_LTR, text, pArg);
    va_end(pArg);
    return width;
}

uint16_t touchgfx::Font::getStringWidth(touchgfx::TextDirection textDirection, const touchgfx::Unicode::UnicodeChar* text, ...) const
{
    va_list pArg;
    va_start(pArg, text);
    uint16_t width = getStringWidth<%= is_rtl? ? 'RTL' : 'LTR' %>(textDirection, text, pArg);
    va_end(pArg);
    return width;
}

touchgfx::Unicode::UnicodeChar touchgfx::TextProvider::getNextLigature(TextDirection direction)
{
<% if is_rtl? %>
    nextCharacters.replaceAt0(unicodeConverter(direction));
<% end %>
    if (fontGsubTable && nextCharacters.peekChar())
    {
        substituteGlyphs();
        if (nextCharacters.peekChar(1) == 0x093F) // Hindi I-matra
        {
            nextCharacters.replaceAt1(nextCharacters.peekChar());
            nextCharacters.replaceAt0(0x093F);
        }
    }
    return getNextChar();
}

void touchgfx::TextProvider::initializeInternal()
{
    fillInputBuffer();
<% if is_rtl? %>
    unicodeConverterInit();
<% end %>
}

void touchgfx::LCD::drawString(touchgfx::Rect widgetArea, const touchgfx::Rect& invalidatedArea, const touchgfx::LCD::StringVisuals& stringVisuals, const touchgfx::Unicode::UnicodeChar* format, ...)
{
    va_list pArg;
    va_start(pArg, format);
    drawString<%= is_rtl? ? 'RTL' : 'LTR' %>(widgetArea, invalidatedArea, stringVisuals, format, pArg);
    va_end(pArg);
}

// Default TypedTextDatabase
extern const touchgfx::TypedText::TypedTextData* const typedTextDatabaseArray[];

<% if generate_binary_files? %>
extern const touchgfx::Unicode::UnicodeChar EmptyLanguageTexts[];
extern const uint32_t EmptyLanguageIndices[];

<% else %>
<%   if remap_global? %>
TEXT_LOCATION_FLASH_PRAGMA
KEEP extern const touchgfx::Unicode::UnicodeChar texts_all_languages[] TEXT_LOCATION_FLASH_ATTRIBUTE = {
<%     substrings_and_offsets = get_substrings_and_offsets %>
<%     if substrings_and_offsets.empty? %>
    0 // No texts in application
<%     else %>
<%       substrings_and_offsets.each do |(unicode_array, offset, last)| %>
    <%= unicode_array.map{|x|"0x%x"%x} * ', ' %><%= last ? '' : ',' %> // @<%= offset %> "<%= unicode_array_to_string(unicode_array) %>"
<%       end %>
<%     end %>
};

<%   end %>
<%   get_capitalized_languages.each do |lang| %>
TEXT_LOCATION_FLASH_PRAGMA
KEEP extern const uint32_t indices<%= lang %>[] TEXT_LOCATION_FLASH_ATTRIBUTE;

<%     if !remap_global? %>
TEXT_LOCATION_FLASH_PRAGMA
KEEP extern const touchgfx::Unicode::UnicodeChar texts<%= lang %>[] TEXT_LOCATION_FLASH_ATTRIBUTE;

<%      end %>
<%   end %>
<% end %>
// Array holding dynamically installed languages
struct TranslationHeader
{
    uint32_t offset_to_texts;
    uint32_t offset_to_indices;
    uint32_t offset_to_typedtext;
};
static const TranslationHeader* languagesArray[<%= has_languages? ? get_number_of_languages : 1 %>] = { 0 };

// Compiled and linked in languages
static const uint32_t* const staticLanguageIndices[] = {
<% if generate_binary_files? %>
    EmptyLanguageIndices
<% else %>
<%   if !has_languages? %>
    0
<%   else %>
<%     get_capitalized_languages.each_with_index do |lang, index| %>
    indices<%= lang %><%= (index == get_number_of_languages - 1) ? '': ',' %>
<%     end %>
<%   end %>
<% end %>
};
<% if generate_binary_files? %>
static const touchgfx::Unicode::UnicodeChar* const staticLanguageTexts[] = {
    EmptyLanguageTexts
};
<% else %>
<%   if not remap_global? %>
static const touchgfx::Unicode::UnicodeChar* const staticLanguageTexts[] = {
<%     if !has_languages? %>
    0
<%     else %>
<%       get_capitalized_languages.each_with_index do |lang, index| %>
    texts<%= lang %><%= (index == get_number_of_languages - 1) ? '': ',' %>
<%       end %>
<%     end %>
};
<%   end %>
<% end %>

touchgfx::LanguageId touchgfx::Texts::currentLanguage = static_cast<touchgfx::LanguageId>(0);
static const touchgfx::Unicode::UnicodeChar* currentLanguagePtr = 0;
static const uint32_t* currentLanguageIndices = 0;

void touchgfx::Texts::setLanguage(touchgfx::LanguageId id)
{
    const touchgfx::TypedText::TypedTextData* currentLanguageTypedText = 0;
    if (id < <%= has_languages? ? get_number_of_languages : 1 %>)
    {
        if (languagesArray[id] != 0)
        {
            // Dynamic translation is added
            const TranslationHeader* translation = languagesArray[id];
            currentLanguagePtr = (const touchgfx::Unicode::UnicodeChar*)(((const uint8_t*)translation) + translation->offset_to_texts);
            currentLanguageIndices = (const uint32_t*)(((const uint8_t*)translation) + translation->offset_to_indices);
            currentLanguageTypedText = (const touchgfx::TypedText::TypedTextData*)(((const uint8_t*)translation) + translation->offset_to_typedtext);
        }
<% if generate_binary_files? %>
        else
        {
            // Compiled and linked empty texts and indices in typedTextData
            currentLanguagePtr = staticLanguageTexts[0];
            currentLanguageIndices = staticLanguageIndices[0];
            currentLanguageTypedText = typedTextDatabaseArray[0];
        }
<% else %>
        else
        {
            // Compiled and linked in languages
<% if remap_global? %>
            currentLanguagePtr = texts_all_languages;
            currentLanguageIndices = staticLanguageIndices[id];
<% else %>
            currentLanguagePtr = staticLanguageTexts[id];
            currentLanguageIndices = staticLanguageIndices[id];
<% end %>
            currentLanguageTypedText = typedTextDatabaseArray[id];
        }
<% end %>
    }

    if (currentLanguageTypedText)
    {
        currentLanguage = id;
        touchgfx::TypedText::registerTypedTextDatabase(currentLanguageTypedText,
                                                       TypedTextDatabase::getFonts(), TypedTextDatabase::getInstanceSize());
    }
}

void touchgfx::Texts::setTranslation(touchgfx::LanguageId id, const void* translation)
{
    languagesArray[id] = (const TranslationHeader*)translation;
}

const touchgfx::Unicode::UnicodeChar* touchgfx::Texts::getText(TypedTextId id) const
{
    return &currentLanguagePtr[currentLanguageIndices[id]];
}
