/** ******************************************************************************* * @file ewolFontBitmap.cpp * @brief ewol Font system (sources) * @author Edouard DUPIN * @date 29/10/2011 * @par Project * ewol * * @par Copyright * Copyright 2011 Edouard DUPIN, all right reserved * * This software is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY. * * Licence summary : * You can modify and redistribute the sources code and binaries. * You can send me the bug-fix * * Term of the licence in in the file licence.txt. * ******************************************************************************* */ #include #include #include #include #include #include #include #if defined(EWOL_X11_MODE__XF86V) # include #elif defined(EWOL_X11_MODE__XRENDER) # include #endif #undef __class__ #define __class__ "ewol::FontBitmap" extern "C" { typedef struct { texCoord_ts posStart; texCoord_ts posStop; etkFloat_t ratio; }UTF8Element_ts; } namespace ewol { class Font { public: Font(etk::File newFile) { m_loadedOK = false; m_filename = newFile; for (int32_t iii=0; iii 0x7F line : " << lineID); } }; public: void SetModeFile(ewol::fontMode_te displayMode, etk::String newFile) { if (displayMode < FONT_MODE_NUMBER) { m_bitmapName[displayMode] = newFile; } }; void LoadMode(ewol::fontMode_te displayMode) { if (displayMode < FONT_MODE_NUMBER) { if (m_bitmapName[displayMode] == "") { EWOL_ERROR("Can not load en empty file for the Font : " << m_filename ); return; } if (m_textureLoaded[displayMode] == true) { EWOL_WARNING("Mode of font is alredy loaded : " << m_filename ); return; } etk::String elementName = m_filename.GetFolder(); elementName += '/'; elementName += m_bitmapName[displayMode]; EWOL_INFO("load text font image : \"" << elementName << "\""); m_textureId[displayMode] = ewol::LoadTexture(elementName); m_textureLoaded[displayMode] = true; } }; etk::File GetFileName(void) { return m_filename; }; etk::String GetName(void) { return m_fontName; }; bool IsLoaded(ewol::fontMode_te displayMode) { return m_textureLoaded[displayMode]; }; private: etk::File m_filename; bool m_loadedOK; etk::String m_fontName; uint32_t m_textureId[FONT_MODE_NUMBER]; bool m_textureLoaded[FONT_MODE_NUMBER]; etk::String m_bitmapName[FONT_MODE_NUMBER]; UTF8Element_ts m_listOfElement[0x80]; public: UTF8Element_ts * GetPointerOnElement(void) { return m_listOfElement; }; uint32_t GetOglId(ewol::fontMode_te displayMode) { return m_textureId[displayMode]; }; }; }; static etk::VectorType listLoadedFonts; void ewol::UnInitFont(void) { for (int32_t iii=0; iiiGetFileName() == fontFileName) { if (true == bold) { listLoadedFonts[iii]->LoadMode(FONT_MODE_BOLD); } if (true == italic) { listLoadedFonts[iii]->LoadMode(FONT_MODE_ITALIC); } if (true == boldItalic) { listLoadedFonts[iii]->LoadMode(FONT_MODE_BOLD_ITALIC); } return true; } } if (fontFileName.GetExtention() != "ebt") { EWOL_ERROR("Not the correct extention of the file" << fontFileName << "supported only *.ebt" ); return false; } ewol::Font * tmpFont = new ewol::Font(fontFileName); if (false == tmpFont->loadedOK()) { EWOL_ERROR("An error apear when loading Font file : " << fontFileName); return false; } if (true == bold) { tmpFont->LoadMode(FONT_MODE_BOLD); } if (true == italic) { tmpFont->LoadMode(FONT_MODE_ITALIC); } if (true == boldItalic) { tmpFont->LoadMode(FONT_MODE_BOLD_ITALIC); } listLoadedFonts.PushBack(tmpFont); return true; } // get the name of the font etk::String ewol::GetFontName(int32_t Id) { if(Id=0) { return listLoadedFonts[Id]->GetName(); } return "No-Name"; } int32_t ewol::GetFontIdWithFileName(etk::File fontFileName) { for (int32_t iii=0; iiiGetFileName() == fontFileName) { return iii; } } return -1; } int32_t ewol::GetFontIdWithName(etk::String fontName) { for (int32_t iii=0; iiiGetName() == fontName) { return iii; } } return -1; } // get the size of a long string in UTF8 (note that \n and \r represent unknown char...) int32_t ewol::GetStringWidth(int32_t fontID, ewol::fontMode_te displayMode, int32_t size, const char * utf8String) { return 0; } // get the size of a specific char in UTF8 int32_t ewol::GetCharWidth(int32_t fontID, ewol::fontMode_te displayMode, int32_t size, const char * utf8String) { return 0; } // draw the text without background void ewol::DrawText(int32_t fontID, ewol::fontMode_te displayMode, int32_t size, coord3D_ts & drawPosition, color_ts textColorFg, const char * utf8String) { if(fontID>=listLoadedFonts.Size() || fontID < 0) { EWOL_WARNING("try to display text with an fontID that does not existed " << fontID); return; } if (false == listLoadedFonts[fontID]->IsLoaded(displayMode)) { listLoadedFonts[fontID]->LoadMode(displayMode); if (false == listLoadedFonts[fontID]->IsLoaded(displayMode)) { EWOL_ERROR("Can not load Font mode : " << displayMode << "of font " << listLoadedFonts[fontID]->GetName() ); return; } } UTF8Element_ts * listOfElement = listLoadedFonts[fontID]->GetPointerOnElement(); char * tmpVal = (char*)utf8String; glColor4f(textColorFg.red, textColorFg.green, textColorFg.blue, textColorFg.alpha); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, listLoadedFonts[fontID]->GetOglId(displayMode)); etkFloat_t posDrawX = drawPosition.x; while(*tmpVal != '\0') { int32_t tmpChar = (int32_t)*tmpVal; if (tmpChar >= 0x80) { tmpChar = 0; } etkFloat_t sizeWidth = size * listOfElement[tmpChar].ratio; if (tmpChar != 0x20) { // TODO : this is really not availlable in the OpenGL ES ==> make with vertex system ... glBegin(GL_QUADS); //m_listOfElement[utf8Value].ratio = (etkFloat_t)w/(etkFloat_t)h; glTexCoord2f(listOfElement[tmpChar].posStart.u, listOfElement[tmpChar].posStart.v); glVertex3f(posDrawX, drawPosition.y, 0.0); glTexCoord2f(listOfElement[tmpChar].posStop.u, listOfElement[tmpChar].posStart.v); glVertex3f(posDrawX + sizeWidth, drawPosition.y, 0.0); glTexCoord2f(listOfElement[tmpChar].posStop.u, listOfElement[tmpChar].posStop.v); glVertex3f(posDrawX + sizeWidth, drawPosition.y + size, 0.0); glTexCoord2f(listOfElement[tmpChar].posStart.u, listOfElement[tmpChar].posStop.v); glVertex3f(posDrawX, drawPosition.y + size, 0.0); glEnd(); } tmpVal++; posDrawX += sizeWidth; } drawPosition.x = posDrawX; glDisable(GL_TEXTURE_2D); } void ewol::DrawText(int32_t fontID, ewol::fontMode_te displayMode, int32_t size, coord2D_ts & drawPosition, const char * utf8String, uint32_t & fontTextureId, etk::VectorType & coord, etk::VectorType & coordTex) { if(fontID>=listLoadedFonts.Size() || fontID < 0) { EWOL_WARNING("try to display text with an fontID that does not existed " << fontID); return; } if (false == listLoadedFonts[fontID]->IsLoaded(displayMode)) { listLoadedFonts[fontID]->LoadMode(displayMode); if (false == listLoadedFonts[fontID]->IsLoaded(displayMode)) { EWOL_ERROR("Can not load Font mode : " << displayMode << "of font " << listLoadedFonts[fontID]->GetName() ); return; } } UTF8Element_ts * listOfElement = listLoadedFonts[fontID]->GetPointerOnElement(); char * tmpVal = (char*)utf8String; // set id of texture ... (i kwnow it was a little dangerous, but this ID is never remove while the program is running... fontTextureId = listLoadedFonts[fontID]->GetOglId(displayMode); etkFloat_t posDrawX = drawPosition.x; while(*tmpVal != '\0') { int32_t tmpChar = (int32_t)*tmpVal; if (tmpChar >= 0x80) { tmpChar = 0; } etkFloat_t sizeWidth = size * listOfElement[tmpChar].ratio; if (tmpChar != 0x20) { coordTex.PushBack(listOfElement[tmpChar].posStart); texCoord_ts tmpTex; tmpTex.u = listOfElement[tmpChar].posStop.u; tmpTex.v = listOfElement[tmpChar].posStart.v; coordTex.PushBack(tmpTex); coordTex.PushBack(listOfElement[tmpChar].posStop); tmpTex.u = listOfElement[tmpChar].posStart.u; tmpTex.v = listOfElement[tmpChar].posStop.v; coordTex.PushBack(tmpTex); coord2D_ts tmpCoord; tmpCoord.x = posDrawX; tmpCoord.y = drawPosition.y; coord.PushBack(tmpCoord); tmpCoord.x = posDrawX + sizeWidth; coord.PushBack(tmpCoord); tmpCoord.y = drawPosition.y + size; coord.PushBack(tmpCoord); tmpCoord.x = posDrawX; coord.PushBack(tmpCoord); } tmpVal++; posDrawX += sizeWidth; } drawPosition.x = posDrawX; }