diff --git a/graphics/macgui/macmenu.cpp b/graphics/macgui/macmenu.cpp index 1bee51e4756..ba5b4671fb3 100644 --- a/graphics/macgui/macmenu.cpp +++ b/graphics/macgui/macmenu.cpp @@ -33,6 +33,7 @@ #include "graphics/macgui/macwindowmanager.h" #include "graphics/macgui/macwindow.h" #include "graphics/macgui/macmenu.h" +#include "graphics/text_renderer.h" namespace Graphics { @@ -746,11 +747,12 @@ static void underlineAccelerator(ManagedSurface *dst, const Font *font, const Co if (shortcutPos == -1) return; - Common::U32String s(str); + int visualPos = getVisualPosition(str, shortcutPos); + Common::U32String s = Common::convertBiDiU32String(str); // Erase characters only if it is not end of the string - if ((uint)(shortcutPos + 1) < s.size()) - s.erase(shortcutPos + 1); + if ((uint)(visualPos + 1) < s.size()) + s.erase(visualPos + 1); int pos2 = font->getStringWidth(s); s.deleteLastChar(); @@ -821,20 +823,17 @@ bool MacMenu::draw(ManagedSurface *g, bool forceRedraw) { int y = it->bbox.top + (_wm->_fontMan->hasBuiltInFonts() ? 2 : 1); if (it->unicode) { - Common::U32String line = convertBiDiU32String(it->unicodeText); + // Common::U32String line = convertBiDiU32String(it->unicodeText); - int accOff = line == it->unicodeText ? 0 : _font->getStringWidth(line); - if (_align == kTextAlignRight) { - accOff += it->bbox.width() - kMenuLeftMargin - _font->getStringWidth(line); - } + int accOff = _align == kTextAlignRight ? it->bbox.width() - _font->getStringWidth(it->unicodeText) : 0; // FLIP ON RENDER // if (_align == kTextAlignRight) { // x = r.right + r.left - it->bbox.width() - x; // } - _font->drawString(&_screen, line, x, y, it->bbox.width(), color, _align); - underlineAccelerator(&_screen, _font, line, x + accOff, y, it->shortcutPos, color); + TextRenderer::drawU32String(&_screen, it->unicodeText, *_font, x, y, it->bbox.width(), color, _align); + underlineAccelerator(&_screen, _font, it->unicodeText, x + accOff, y, it->shortcutPos, color); } else { const Font *font = getMenuFont(it->style); @@ -867,17 +866,10 @@ void MacMenu::renderSubmenu(MacMenuSubMenu *menu, bool recursive) { _screen.fillRect(*r, _wm->_colorWhite); _screen.frameRect(*r, _wm->_colorBlack); - if (_align != kTextAlignRight) { - _screen.vLine(r->right, r->top + 3, r->bottom + 1, _wm->_colorBlack); - _screen.vLine(r->right + 1, r->top + 3, r->bottom + 1, _wm->_colorBlack); - _screen.hLine(r->left + 3, r->bottom, r->right + 1, _wm->_colorBlack); - _screen.hLine(r->left + 3, r->bottom + 1, r->right + 1, _wm->_colorBlack); - } else { - _screen.vLine(r->left, r->top + 3, r->bottom + 1, _wm->_colorBlack); - _screen.vLine(r->left - 1, r->top + 3, r->bottom + 1, _wm->_colorBlack); - _screen.hLine(r->left - 3, r->bottom, r->right - 1, _wm->_colorBlack); - _screen.hLine(r->left - 3, r->bottom + 1, r->right - 1, _wm->_colorBlack); - } + _screen.vLine(r->right, r->top + 3, r->bottom + 1, _wm->_colorBlack); + _screen.vLine(r->right + 1, r->top + 3, r->bottom + 1, _wm->_colorBlack); + _screen.hLine(r->left + 3, r->bottom, r->right + 1, _wm->_colorBlack); + _screen.hLine(r->left + 3, r->bottom + 1, r->right + 1, _wm->_colorBlack); int x = r->left + kMenuDropdownPadding; int y = r->top + 1; @@ -890,12 +882,11 @@ void MacMenu::renderSubmenu(MacMenuSubMenu *menu, bool recursive) { Common::String text(menu->items[i]->text); Common::String acceleratorText(getAcceleratorString(menu->items[i], "")); - Common::U32String unicodeText = convertBiDiU32String(menu->items[i]->unicodeText); + // Common::U32String unicodeText = convertBiDiU32String(menu->items[i]->unicodeText); + Common::U32String unicodeText(menu->items[i]->unicodeText); + + int accOff = _align == kTextAlignRight ? r->width() - _font->getStringWidth(unicodeText) : 0; - int accOff = unicodeText == menu->items[i]->unicodeText ? 0 : _font->getStringWidth(unicodeText); - if (_align == kTextAlignRight) { - accOff += r->width() - kMenuDropdownPadding - _font->getStringWidth(unicodeText); - } int shortcutPos = menu->items[i]->shortcutPos; @@ -929,7 +920,7 @@ void MacMenu::renderSubmenu(MacMenuSubMenu *menu, bool recursive) { } if (menu->items[i]->unicode) { - _font->drawString(s, unicodeText, tx, ty, r->width(), color, _align); + TextRenderer::drawU32String(s, unicodeText, *_font, tx, ty, r->width(), color, _align); underlineAccelerator(s, _font, unicodeText, tx + accOff, ty, shortcutPos, color); } else { const Font *font = getMenuFont(menu->items[i]->style); diff --git a/graphics/module.mk b/graphics/module.mk index f2a0a52364d..dd9b01c5c11 100644 --- a/graphics/module.mk +++ b/graphics/module.mk @@ -32,6 +32,7 @@ MODULE_OBJS := \ screen.o \ sjis.o \ surface.o \ + text_renderer.o \ transform_struct.o \ transform_tools.o \ transparent_surface.o \ diff --git a/graphics/text_renderer.cpp b/graphics/text_renderer.cpp new file mode 100644 index 00000000000..86e04925b50 --- /dev/null +++ b/graphics/text_renderer.cpp @@ -0,0 +1,52 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include "common/str-bidi.h" +#include "graphics/text_renderer.h" + +#ifdef USE_FRIBIDI +#include +#endif + +namespace Graphics { + +namespace TextRenderer { + +void drawString(Surface *dst, const Common::String &str, const Graphics::Font &font, int x, int y, int w, uint32 color, const Common::CodePage page, TextAlign align, int deltax, bool useEllipsis) { + font.drawString(dst, convertBiDiString(str, page), x, y, w, color, align, deltax, useEllipsis); +} + +void drawU32String(Surface *dst, const Common::U32String &str, const Graphics::Font &font, int x, int y, int w, uint32 color, TextAlign align, int deltax) { + font.drawString(dst, convertBiDiU32String(str), x, y, w, color, align, deltax); +} + +void drawString(ManagedSurface *dst, const Common::String &str, const Graphics::Font &font, int x, int y, int w, uint32 color, const Common::CodePage page, TextAlign align, int deltax, bool useEllipsis) { + font.drawString(dst, convertBiDiString(str, page), x, y, w, color, align, deltax, useEllipsis); +} + +void drawU32String(ManagedSurface *dst, const Common::U32String &str, const Graphics::Font &font, int x, int y, int w, uint32 color, TextAlign align, int deltax) { + font.drawString(dst, convertBiDiU32String(str), x, y, w, color, align, deltax); +} + +} // End of namespace TextRenderer + +} // End of namespace Graphics diff --git a/graphics/text_renderer.h b/graphics/text_renderer.h new file mode 100644 index 00000000000..0fadaa3f7a0 --- /dev/null +++ b/graphics/text_renderer.h @@ -0,0 +1,47 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef GRAPHICS_TEXT_RENDERER_H +#define GRAPHICS_TEXT_RENDERER_H + +#include "common/str.h" +#include "common/ustr.h" +#include "common/str-bidi.h" +#include "graphics/font.h" + +namespace Graphics { + +/** + * high-level methods for drawing strings (with bidi support). + */ +namespace TextRenderer { + + // TODO: Add doxygen comments to this + void drawString(Surface *dst, const Common::String &str, const Graphics::Font &font, int x, int y, int w, uint32 color, const Common::CodePage page, TextAlign align = kTextAlignLeft, int deltax = 0, bool useEllipsis = true); + void drawU32String(Surface *dst, const Common::U32String &str, const Graphics::Font &font, int x, int y, int w, uint32 color, TextAlign align = kTextAlignLeft, int deltax = 0); + void drawString(ManagedSurface *dst, const Common::String &str, const Graphics::Font &font, int x, int y, int w, uint32 color, const Common::CodePage page, TextAlign align = kTextAlignLeft, int deltax = 0, bool useEllipsis = true); + void drawU32String(ManagedSurface *dst, const Common::U32String &str, const Graphics::Font &font, int x, int y, int w, uint32 color, TextAlign align = kTextAlignLeft, int deltax = 0); +} + +} // End of namespace Graphics + +#endif