/** ******************************************************************************* * @file ewolFont.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::Font" extern "C" { typedef struct { position_ts posStart; position_ts posStop; intSize_ts size; float ratio; }UTF8Element_ts; } namespace ewol { class Font { public: Font(etk::File newFile) { m_loadedOK = false; m_filename = newFile; for (int32_t iii; 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; // load a font in momory, can be done many time for a specific fontname, if you specify true the font will be loaded in memory, otherwise, font is loaded only when needed the first time bool ewol::AddFont(etk::File fontFileName, bool bold, bool italic, bool boldItalic) { 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)); float posDrawX = drawPosition.x; while(*tmpVal != '\0') { int32_t tmpChar = (int32_t)*tmpVal; if (tmpChar >= 0x80) { tmpChar = 0; } float sizeWidth = size * listOfElement[tmpChar].ratio; if (tmpChar != 0x20) { /* EWOL_DEBUG(" Draw TEX (" << listOfElement[tmpChar].posStart.x*512 << "," << listOfElement[tmpChar].posStart.y*512 << ")"); EWOL_DEBUG(" (" << listOfElement[tmpChar].posStop.x*512 << "," << listOfElement[tmpChar].posStop.y*512 << ")"); EWOL_DEBUG(" display (" << drawPosition.x << "," << drawPosition.y << ")"); EWOL_DEBUG(" (" << drawPosition.x + listOfElement[tmpChar].size.x << "," << drawPosition.y + listOfElement[tmpChar].size.y << ")"); */ glBegin(GL_QUADS); //m_listOfElement[utf8Value].ratio = (float)w/(float)h; glTexCoord2f(listOfElement[tmpChar].posStart.x, listOfElement[tmpChar].posStart.y); glVertex3f(posDrawX, drawPosition.y, 0.0); glTexCoord2f(listOfElement[tmpChar].posStop.x, listOfElement[tmpChar].posStart.y); glVertex3f(posDrawX + sizeWidth, drawPosition.y, 0.0); glTexCoord2f(listOfElement[tmpChar].posStop.x, listOfElement[tmpChar].posStop.y); glVertex3f(posDrawX + sizeWidth, drawPosition.y + size, 0.0); glTexCoord2f(listOfElement[tmpChar].posStart.x, listOfElement[tmpChar].posStop.y); glVertex3f(posDrawX, drawPosition.y + size, 0.0); glEnd(); } tmpVal++; posDrawX += sizeWidth; } drawPosition.x = posDrawX; glDisable(GL_TEXTURE_2D); } // draw the text with a sp\Ufffffffffy background void ewol::DrawTextWithBg( int32_t fontID, ewol::fontMode_te displayMode, int32_t size, coord3D_ts & drawPosition, color_ts textColorFg, color_ts textColorBg, const char * utf8String) { }