void AppSetup::onResizeWindow(const Int2& newSize) { m_inf.windowSize = newSize; Float2 newSizeF = Float2((float)newSize.width(), (float)newSize.height()); Float2 virtualSizeF = Float2((float)m_inf.virtualSize.width(), (float)m_inf.virtualSize.height()); if (m_inf.automaticFitToWindowSize && (newSize.width() != m_inf.virtualSize.width() || newSize.height() != m_inf.virtualSize.height())) { Float2 ppp(-1.f,-1.f); Int2 virtualPos(-1,-1); float ratioW = (float)newSize.width() / (float)m_inf.virtualSize.width(); float ratioH = (float)newSize.height() / (float)m_inf.virtualSize.height(); if (m_inf.virtualSizeAllowRatioDeformation) { ppp = Float2(ratioW,ratioH); virtualPos = Int2(0,0); } else if (newSizeF.width()/newSizeF.height() > virtualSizeF.width() / virtualSizeF.height()) { ppp = Float2(ratioH,ratioH); virtualPos = Int2((int)((newSizeF.width()-virtualSizeF.width()*ratioH)/2.f), 0); } else { ppp = Float2(ratioW,ratioW); virtualPos = Int2(0, (int)((newSizeF.height()-virtualSizeF.height()*ratioW)/2.f)); } this->setPixelPerPointLowLevel(ppp, virtualPos); } else { this->setPixelPerPointLowLevel(Float2(1.f,1.f), Int2(0,0)); #if defined(USES_WINDOWS_OPENGL) || defined(USES_LINUX) m_openGL->setRealWindowSize(newSize); m_openGL->set2DMode(); #else // do nothing here #endif } m_isUsingVirtualSize = (this->getPixelPerPoint() != Float2(1.f, 1.f) || this->getVirtualTopLeftCornerInWindow() != Int2(0, 0)); }
const std::vector<Point>& BitmapFont::calculateGlyphsPositions(const std::string& text, Fw::AlignmentFlag align, Size *textBoxSize) { // for performance reasons we use statics vectors that are allocated on demand static std::vector<Point> glyphsPositions(1); static std::vector<int> lineWidths(1); int textLength = text.length(); int maxLineWidth = 0; int lines = 0; int glyph; int i; // return if there is no text if(textLength == 0) { if(textBoxSize) textBoxSize->resize(0,m_glyphHeight); return glyphsPositions; } // resize glyphsPositions vector when needed if(textLength > (int)glyphsPositions.size()) glyphsPositions.resize(textLength); // calculate lines width if((align & Fw::AlignRight || align & Fw::AlignHorizontalCenter) || textBoxSize) { lineWidths[0] = 0; for(i = 0; i< textLength; ++i) { glyph = (uchar)text[i]; if(glyph == (uchar)'\n') { lines++; if(lines+1 > (int)lineWidths.size()) lineWidths.resize(lines+1); lineWidths[lines] = 0; } else if(glyph >= 32) { lineWidths[lines] += m_glyphsSize[glyph].width() ; if((i+1 != textLength && text[i+1] != '\n')) // only add space if letter is not the last or before a \n. lineWidths[lines] += m_glyphSpacing.width(); maxLineWidth = std::max<int>(maxLineWidth, lineWidths[lines]); } } } Point virtualPos(0, m_yOffset); lines = 0; for(i = 0; i < textLength; ++i) { glyph = (uchar)text[i]; // new line or first glyph if(glyph == (uchar)'\n' || i == 0) { if(glyph == (uchar)'\n') { virtualPos.y += m_glyphHeight + m_glyphSpacing.height(); lines++; } // calculate start x pos if(align & Fw::AlignRight) { virtualPos.x = (maxLineWidth - lineWidths[lines]); } else if(align & Fw::AlignHorizontalCenter) { virtualPos.x = (maxLineWidth - lineWidths[lines]) / 2; } else { // AlignLeft virtualPos.x = 0; } } // store current glyph topLeft glyphsPositions[i] = virtualPos; // render only if the glyph is valid if(glyph >= 32 && glyph != (uchar)'\n') { virtualPos.x += m_glyphsSize[glyph].width() + m_glyphSpacing.width(); } } if(textBoxSize) { textBoxSize->setWidth(maxLineWidth); textBoxSize->setHeight(virtualPos.y + m_glyphHeight); } return glyphsPositions; }