diff options
Diffstat (limited to 'src/osd/modules/font')
-rw-r--r-- | src/osd/modules/font/font_dwrite.cpp | 55 | ||||
-rw-r--r-- | src/osd/modules/font/font_module.h | 2 | ||||
-rw-r--r-- | src/osd/modules/font/font_none.cpp | 16 | ||||
-rw-r--r-- | src/osd/modules/font/font_osx.cpp | 42 | ||||
-rw-r--r-- | src/osd/modules/font/font_sdl.cpp | 38 | ||||
-rw-r--r-- | src/osd/modules/font/font_windows.cpp | 34 |
6 files changed, 115 insertions, 72 deletions
diff --git a/src/osd/modules/font/font_dwrite.cpp b/src/osd/modules/font/font_dwrite.cpp index f60ef7580c4..ec152fb1b0d 100644 --- a/src/osd/modules/font/font_dwrite.cpp +++ b/src/osd/modules/font/font_dwrite.cpp @@ -6,14 +6,23 @@ */ #include "font_module.h" -#include "modules/osdmodule.h" + #include "modules/lib/osdlib.h" -#if defined(OSD_WINDOWS) || defined(OSD_UWP) +#if defined(OSD_WINDOWS) -#include <windows.h> +#include "corestr.h" +#include "winutil.h" + +#include "osdcore.h" +#include "strconv.h" + +#include <cmath> #include <memory> +#include <stdexcept> + +#include <windows.h> // Windows Imaging Components #include <wincodec.h> @@ -32,9 +41,10 @@ DEFINE_GUID(GUID_WICPixelFormat8bppAlpha, 0xe6cd0116, 0xeeba, 0x4161, 0xaa, 0x85 #include <wrl/client.h> #undef interface -#include "strconv.h" -#include "corestr.h" -#include "winutil.h" + +namespace osd { + +namespace { using namespace Microsoft::WRL; @@ -146,7 +156,7 @@ HRESULT SaveBitmap2(bitmap_argb32 &bitmap, const WCHAR *filename) uint32_t* pRow = pBitmap.get() + (y * bitmap.width()); for (int x = 0; x < bitmap.width(); x++) { - uint32_t pixel = bitmap.pix32(y, x); + uint32_t pixel = bitmap.pix(y, x); pRow[x] = (pixel == 0xFFFFFFFF) ? rgb_t(0xFF, 0x00, 0x00, 0x00) : rgb_t(0xFF, 0xFF, 0xFF, 0xFF); } } @@ -226,7 +236,7 @@ public: { if (m_designUnitsPerEm != other.m_designUnitsPerEm || m_emSizeInDip != other.m_emSizeInDip) { - throw emu_fatalerror("Attempted subtraction of FontDimension with different scale."); + throw std::invalid_argument("Attempted subtraction of FontDimension with different scale."); } return FontDimension(m_designUnitsPerEm, m_emSizeInDip, m_designUnits - other.m_designUnits); @@ -236,7 +246,7 @@ public: { if (m_designUnitsPerEm != other.m_designUnitsPerEm || m_emSizeInDip != other.m_emSizeInDip) { - throw emu_fatalerror("Attempted addition of FontDimension with different scale."); + throw std::invalid_argument("Attempted addition of FontDimension with different scale."); } return FontDimension(m_designUnitsPerEm, m_emSizeInDip, m_designUnits + other.m_designUnits); @@ -353,20 +363,16 @@ public: // accept qualifiers from the name std::string name(_name); if (name.compare("default") == 0) -#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) - name = "Tahoma"; -#else name = "Segoe UI"; -#endif bool bold = (strreplace(name, "[B]", "") + strreplace(name, "[b]", "") > 0); bool italic = (strreplace(name, "[I]", "") + strreplace(name, "[i]", "") > 0); // convert the face name - std::wstring familyName = osd::text::to_wstring(name.c_str()); + std::wstring familyName = text::to_wstring(name); // find the font HR_RET0(find_font( - familyName.c_str(), + familyName, bold ? DWRITE_FONT_WEIGHT_BOLD : DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STRETCH_NORMAL, italic ? DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL, @@ -570,7 +576,7 @@ public: // copy the bits into it for (int y = 0; y < bitmap.height(); y++) { - uint32_t *dstrow = &bitmap.pix32(y); + uint32_t *dstrow = &bitmap.pix(y); uint8_t *srcrow = &pixels[(y + actbounds.min_y) * bmwidth]; for (int x = 0; x < bitmap.width(); x++) { @@ -624,7 +630,7 @@ private: HR_RETHR(fonts->FindFamilyName(familyName.c_str(), &family_index, &exists)); if (!exists) { - osd_printf_error("Font with family name %S does not exist.\n", familyName.c_str()); + osd_printf_error("Font with family name %s does not exist.\n", text::from_wstring(familyName)); return E_FAIL; } @@ -675,7 +681,7 @@ public: return true; } - virtual int init(const osd_options &options) override + virtual int init(osd_interface &osd, const osd_options &options) override { HRESULT result; @@ -743,7 +749,7 @@ public: std::unique_ptr<WCHAR[]> name = nullptr; HR_RET0(get_localized_familyname(names, name)); - std::string utf8_name = osd::text::from_wstring(name.get()); + std::string utf8_name = text::from_wstring(name.get()); name.reset(); // Review: should the config name, be unlocalized? @@ -807,8 +813,15 @@ private: } }; +} // anonymous namespace + +} // namespace osd + #else -MODULE_NOT_SUPPORTED(font_dwrite, OSD_FONT_PROVIDER, "dwrite") + +namespace osd { namespace { MODULE_NOT_SUPPORTED(font_dwrite, OSD_FONT_PROVIDER, "dwrite") } } + #endif -MODULE_DEFINITION(FONT_DWRITE, font_dwrite) + +MODULE_DEFINITION(FONT_DWRITE, osd::font_dwrite) diff --git a/src/osd/modules/font/font_module.h b/src/osd/modules/font/font_module.h index 81ffaca74e5..7fe04e32628 100644 --- a/src/osd/modules/font/font_module.h +++ b/src/osd/modules/font/font_module.h @@ -24,7 +24,7 @@ class font_module { public: - virtual ~font_module() { } + virtual ~font_module() = default; /** attempt to allocate a font instance */ virtual osd_font::ptr font_alloc() = 0; diff --git a/src/osd/modules/font/font_none.cpp b/src/osd/modules/font/font_none.cpp index dc6116625a8..abf56007b00 100644 --- a/src/osd/modules/font/font_none.cpp +++ b/src/osd/modules/font/font_none.cpp @@ -4,9 +4,12 @@ * font_none.c * */ - #include "font_module.h" -#include "modules/osdmodule.h" + + +namespace osd { + +namespace { class osd_font_none : public osd_font { @@ -23,10 +26,15 @@ class font_none : public osd_module, public font_module public: font_none() : osd_module(OSD_FONT_PROVIDER, "none"), font_module() { } - virtual int init(const osd_options &options) override { return 0; } + virtual int init(osd_interface &osd, const osd_options &options) override { return 0; } virtual osd_font::ptr font_alloc() override { return std::make_unique<osd_font_none>(); } virtual bool get_font_families(std::string const &font_path, std::vector<std::pair<std::string, std::string> > &result) override { return false; } }; -MODULE_DEFINITION(FONT_NONE, font_none) +} // anonymous namespace + +} // namespace osd + + +MODULE_DEFINITION(FONT_NONE, osd::font_none) diff --git a/src/osd/modules/font/font_osx.cpp b/src/osd/modules/font/font_osx.cpp index 5074c1f4bcd..a020d51ddce 100644 --- a/src/osd/modules/font/font_osx.cpp +++ b/src/osd/modules/font/font_osx.cpp @@ -6,12 +6,12 @@ */ #include "font_module.h" -#include "modules/osdmodule.h" #ifdef SDLMAME_MACOSX -#include "corealloc.h" +#include "emucore.h" #include "fileio.h" +#include "osdcore.h" #include <ApplicationServices/ApplicationServices.h> #include <CoreFoundation/CoreFoundation.h> @@ -54,8 +54,20 @@ private: bool osd_font_osx::open(std::string const &font_path, std::string const &name, int &height) { - osd_printf_verbose("osd_font_osx::open: name=\"%s\"\n", name.c_str()); - CFStringRef const font_name(CFStringCreateWithCString(nullptr, name.c_str(), kCFStringEncodingUTF8)); + osd_printf_verbose("osd_font_osx::open: name=\"%s\"\n", name); + + CFStringRef font_name; + if (name == "default") + { + // Arial Unicode MS comes with Mac OS X 10.5 and later and is the only Mac default font with + // the Unicode characters used by the vgmplay and aristmk5 layouts. + font_name = CFStringCreateWithCString(nullptr, "Arial Unicode MS", kCFStringEncodingUTF8); + } + else + { + font_name = CFStringCreateWithCString(nullptr, name.c_str(), kCFStringEncodingUTF8); + } + if (!font_name) { osd_printf_verbose("osd_font_osx::open: failed to create CFString from font name (invalid UTF-8?)\n"); @@ -73,7 +85,7 @@ bool osd_font_osx::open(std::string const &font_path, std::string const &name, i CFRelease(font_name); if (!font_descriptor) { - osd_printf_verbose("osd_font_osx::open: failed to create CoreText font descriptor for \"%s\"\n", name.c_str()); + osd_printf_verbose("osd_font_osx::open: failed to create CoreText font descriptor for \"%s\"\n", name); return false; } @@ -81,7 +93,7 @@ bool osd_font_osx::open(std::string const &font_path, std::string const &name, i CFRelease(font_descriptor); if (!ct_font) { - osd_printf_verbose("osd_font_osx::open: failed to create CoreText font for \"%s\"\n", name.c_str()); + osd_printf_verbose("osd_font_osx::open: failed to create CoreText font for \"%s\"\n", name); return false; } @@ -141,9 +153,16 @@ bool osd_font_osx::get_bitmap(char32_t chnum, bitmap_argb32 &bitmap, std::int32_ osd_printf_verbose("osd_font_osd::get_bitmap: failed to get glyph for U+%04X\n", unsigned(chnum)); // try to get glyph metrics + #if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101100 + CGRect const bounds(CTFontGetBoundingRectsForGlyphs(m_font, kCTFontOrientationHorizontal, &glyph, nullptr, count)); + CGSize advance(CGSizeZero); + CTFontGetAdvancesForGlyphs(m_font, kCTFontOrientationHorizontal, &glyph, &advance, count); + #else CGRect const bounds(CTFontGetBoundingRectsForGlyphs(m_font, kCTFontHorizontalOrientation, &glyph, nullptr, count)); CGSize advance(CGSizeZero); CTFontGetAdvancesForGlyphs(m_font, kCTFontHorizontalOrientation, &glyph, &advance, count); + #endif + // CGNullRect can indicate failure, but it's also a valid rectangle for spaces in some fonts (e.g. Hiragino family) if (CGRectEqualToRect(bounds, CGRectNull) && CGSizeEqualToSize(advance, CGSizeZero)) @@ -171,7 +190,12 @@ bool osd_font_osx::get_bitmap(char32_t chnum, bitmap_argb32 &bitmap, std::int32_ CGContextSetRGBFillColor(context_ref, 1.0, 1.0, 1.0, 1.0); CGContextSetFont(context_ref, font_ref); CGContextSetFontSize(context_ref, POINT_SIZE); + #if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090 + CGPoint pos = CGPointMake(0, 0); + CTFontDrawGlyphs(m_font, &glyph, &pos, 1, context_ref); + #else CGContextShowGlyphs(context_ref, &glyph, count); + #endif CGFontRelease(font_ref); CGContextRelease(context_ref); } @@ -187,7 +211,7 @@ class font_osx : public osd_module, public font_module public: font_osx() : osd_module(OSD_FONT_PROVIDER, "osx"), font_module() { } - virtual int init(const osd_options &options) override { return 0; } + virtual int init(osd_interface &osd, const osd_options &options) override { return 0; } virtual osd_font::ptr font_alloc() override { return std::make_unique<osd_font_osx>(); } virtual bool get_font_families(std::string const &font_path, std::vector<std::pair<std::string, std::string> > &result) override; @@ -218,8 +242,8 @@ private: bool font_osx::get_font_families(std::string const &font_path, std::vector<std::pair<std::string, std::string> > &result) { CFStringRef keys[] = { kCTFontCollectionRemoveDuplicatesOption }; - std::uintptr_t values[ARRAY_LENGTH(keys)] = { 1 }; - CFDictionaryRef const options = CFDictionaryCreate(kCFAllocatorDefault, (void const **)keys, (void const **)values, ARRAY_LENGTH(keys), &kCFTypeDictionaryKeyCallBacks, nullptr); + std::uintptr_t values[std::size(keys)] = { 1 }; + CFDictionaryRef const options = CFDictionaryCreate(kCFAllocatorDefault, (void const **)keys, (void const **)values, std::size(keys), &kCFTypeDictionaryKeyCallBacks, nullptr); CTFontCollectionRef const collection = CTFontCollectionCreateFromAvailableFonts(nullptr); CFRelease(options); if (!collection) return false; diff --git a/src/osd/modules/font/font_sdl.cpp b/src/osd/modules/font/font_sdl.cpp index 62a97d39149..514e0cc0784 100644 --- a/src/osd/modules/font/font_sdl.cpp +++ b/src/osd/modules/font/font_sdl.cpp @@ -6,20 +6,17 @@ */ #include "font_module.h" -#include "modules/osdmodule.h" #if defined(SDLMAME_UNIX) && !defined(SDLMAME_MACOSX) && !defined(SDLMAME_HAIKU) && !defined(SDLMAME_ANDROID) #include "corestr.h" -#include "corealloc.h" +#include "emucore.h" #include "fileio.h" #include "unicode.h" +#include "osdcore.h" -#ifdef SDLMAME_EMSCRIPTEN -#include <SDL_ttf.h> -#else #include <SDL2/SDL_ttf.h> -#endif + #if !defined(SDLMAME_HAIKU) && !defined(SDLMAME_EMSCRIPTEN) #include <fontconfig/fontconfig.h> #endif @@ -88,15 +85,15 @@ bool osd_font_sdl::open(std::string const &font_path, std::string const &_name, // if no success, try the font path if (!font) { - osd_printf_verbose("Searching font %s in -%s path/s\n", family.c_str(), font_path.c_str()); + osd_printf_verbose("Searching font %s in -%s path/s\n", family, font_path); //emu_file file(options().font_path(), OPEN_FLAG_READ); - emu_file file(font_path.c_str(), OPEN_FLAG_READ); - if (file.open(family.c_str()) == osd_file::error::NONE) + emu_file file(font_path, OPEN_FLAG_READ); + if (!file.open(family)) { std::string full_name = file.fullpath(); font = TTF_OpenFont_Magic(full_name, POINT_SIZE, 0); if (font) - osd_printf_verbose("Found font %s\n", full_name.c_str()); + osd_printf_verbose("Found font %s\n", full_name); } } @@ -112,7 +109,7 @@ bool osd_font_sdl::open(std::string const &font_path, std::string const &_name, { if (!BDF_Check_Magic(name)) { - osd_printf_verbose("font %s is not TrueType or BDF, using MAME default\n", name.c_str()); + osd_printf_verbose("font %s is not TrueType or BDF, using MAME default\n", name); } return false; } @@ -162,7 +159,7 @@ bool osd_font_sdl::get_bitmap(char32_t chnum, bitmap_argb32 &bitmap, std::int32_ { SDL_Color const fcol = { 0xff, 0xff, 0xff }; char ustr[16]; - ustr[utf8_from_uchar(ustr, ARRAY_LENGTH(ustr), chnum)] = '\0'; + ustr[utf8_from_uchar(ustr, std::size(ustr), chnum)] = '\0'; std::unique_ptr<SDL_Surface, void (*)(SDL_Surface *)> const drawsurf(TTF_RenderUTF8_Solid(m_font.get(), ustr, fcol), &SDL_FreeSurface); // was nothing returned? @@ -174,7 +171,7 @@ bool osd_font_sdl::get_bitmap(char32_t chnum, bitmap_argb32 &bitmap, std::int32_ // copy the rendered character image into it for (int y = 0; y < bitmap.height(); y++) { - std::uint32_t *const dstrow = &bitmap.pix32(y); + std::uint32_t *const dstrow = &bitmap.pix(y); std::uint8_t const *const srcrow = reinterpret_cast<std::uint8_t const *>(drawsurf->pixels) + (y * drawsurf->pitch); for (int x = 0; x < drawsurf->w; x++) @@ -194,14 +191,13 @@ bool osd_font_sdl::get_bitmap(char32_t chnum, bitmap_argb32 &bitmap, std::int32_ osd_font_sdl::TTF_Font_ptr osd_font_sdl::TTF_OpenFont_Magic(std::string const &name, int fsize, long index) { emu_file file(OPEN_FLAG_READ); - if (file.open(name.c_str()) == osd_file::error::NONE) + if (!file.open(name)) { unsigned char const ttf_magic[] = { 0x00, 0x01, 0x00, 0x00, 0x00 }; unsigned char const ttc1_magic[] = { 0x74, 0x74, 0x63, 0x66, 0x00, 0x01, 0x00, 0x00 }; unsigned char const ttc2_magic[] = { 0x74, 0x74, 0x63, 0x66, 0x00, 0x02, 0x00, 0x00 }; - auto buffer_size = std::max({ sizeof(ttf_magic), sizeof(ttc1_magic), sizeof(ttc2_magic) }); - unsigned char buffer[buffer_size]; - auto const bytes_read = file.read(buffer, buffer_size); + unsigned char buffer[std::max({ sizeof(ttf_magic), sizeof(ttc1_magic), sizeof(ttc2_magic) })]; + auto const bytes_read = file.read(buffer, std::size(buffer)); file.close(); if (((bytes_read >= sizeof(ttf_magic)) && !std::memcmp(buffer, ttf_magic, sizeof(ttf_magic))) || @@ -215,7 +211,7 @@ osd_font_sdl::TTF_Font_ptr osd_font_sdl::TTF_OpenFont_Magic(std::string const &n bool osd_font_sdl::BDF_Check_Magic(std::string const &name) { emu_file file(OPEN_FLAG_READ); - if (file.open(name.c_str()) == osd_file::error::NONE) + if (!file.open(name)) { unsigned char const magic[] = { 'S', 'T', 'A', 'R', 'T', 'F', 'O', 'N', 'T' }; unsigned char buffer[sizeof(magic)]; @@ -304,7 +300,7 @@ public: return std::make_unique<osd_font_sdl>(); } - virtual int init(const osd_options &options) override + virtual int init(osd_interface &osd, const osd_options &options) override { if (TTF_Init() == -1) { @@ -350,10 +346,10 @@ bool font_sdl::get_font_families(std::string const &font_path, std::vector<std:: { auto const compare_fonts = [](std::pair<std::string, std::string> const &a, std::pair<std::string, std::string> const &b) -> bool { - int const second = core_stricmp(a.second.c_str(), b.second.c_str()); + int const second = core_stricmp(a.second, b.second); if (second < 0) return true; else if (second > 0) return false; - else return core_stricmp(b.first.c_str(), b.first.c_str()) < 0; + else return core_stricmp(b.first, b.first) < 0; }; std::string config((const char *)val.u.s); std::string display(config); diff --git a/src/osd/modules/font/font_windows.cpp b/src/osd/modules/font/font_windows.cpp index 95f10c0a051..0ec65b67aea 100644 --- a/src/osd/modules/font/font_windows.cpp +++ b/src/osd/modules/font/font_windows.cpp @@ -1,22 +1,19 @@ // license:BSD-3-Clause // copyright-holders:Aaron Giles /* - * font_windows.c + * font_windows.cpp * */ #include "font_module.h" -#include "modules/osdmodule.h" #if defined(OSD_WINDOWS) || defined(SDLMAME_WIN32) -#include "font_module.h" -#include "modules/osdmodule.h" - #include "strconv.h" +#include "unicode.h" #include "corestr.h" -#include "corealloc.h" +#include "osdcore.h" #include <cstring> @@ -27,7 +24,10 @@ #include <io.h> +namespace osd { + namespace { + class osd_font_windows : public osd_font { public: @@ -84,8 +84,8 @@ bool osd_font_windows::open(std::string const &font_path, std::string const &_na logfont.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE; // copy in the face name - osd::text::tstring face = osd::text::to_tstring(name); - _tcsncpy(logfont.lfFaceName, face.c_str(), ARRAY_LENGTH(logfont.lfFaceName)); + text::tstring face = text::to_tstring(name); + _tcsncpy(logfont.lfFaceName, face.c_str(), std::size(logfont.lfFaceName)); logfont.lfFaceName[sizeof(logfont.lfFaceName) / sizeof(TCHAR)-1] = 0; // create the font @@ -109,8 +109,8 @@ bool osd_font_windows::open(std::string const &font_path, std::string const &_na } // if it doesn't match our request, fail - std::string utf = osd::text::from_tstring(&realname[0]); - int result = core_stricmp(utf.c_str(), name.c_str()); + std::string utf = text::from_tstring(&realname[0]); + int result = core_stricmp(utf, name); // if we didn't match, nuke our font and fall back if (result != 0) @@ -200,7 +200,7 @@ bool osd_font_windows::get_bitmap(char32_t chnum, bitmap_argb32 &bitmap, int32_t // now draw the character char16_t tempchar[UTF16_CHAR_MAX]; - UINT const count = INT(utf16_from_uchar(tempchar, ARRAY_LENGTH(tempchar), chnum)); + UINT const count = INT(utf16_from_uchar(tempchar, std::size(tempchar), chnum)); SetTextColor(dummyDC, RGB(0xff, 0xff, 0xff)); SetBkColor(dummyDC, RGB(0x00, 0x00, 0x00)); ExtTextOutW(dummyDC, 50 + abc.abcA, 50, ETO_OPAQUE, nullptr, reinterpret_cast<LPCWSTR>(tempchar), count, nullptr); @@ -260,7 +260,7 @@ bool osd_font_windows::get_bitmap(char32_t chnum, bitmap_argb32 &bitmap, int32_t // copy the bits into it for (int y = 0; y < bitmap.height(); y++) { - uint32_t *dstrow = &bitmap.pix32(y); + uint32_t *dstrow = &bitmap.pix(y); uint8_t *srcrow = &bits[(y + actbounds.min_y) * rowbytes]; for (int x = 0; x < bitmap.width(); x++) { @@ -289,7 +289,7 @@ class font_win : public osd_module, public font_module public: font_win() : osd_module(OSD_FONT_PROVIDER, "win"), font_module() { } - virtual int init(const osd_options &options) override { return 0; } + virtual int init(osd_interface &osd, const osd_options &options) override { return 0; } virtual osd_font::ptr font_alloc() override { return std::make_unique<osd_font_windows>(); } @@ -299,7 +299,7 @@ private: static int CALLBACK font_family_callback(LOGFONT const *lpelfe, TEXTMETRIC const *lpntme, DWORD FontType, LPARAM lParam) { auto &result = *reinterpret_cast<std::vector<std::pair<std::string, std::string> > *>(lParam); - std::string face = osd::text::from_tstring(lpelfe->lfFaceName); + std::string face = text::from_tstring(lpelfe->lfFaceName); if ((face[0] != '@') && (result.empty() || (result.back().first != face))) result.emplace_back(face, face); return TRUE; } @@ -327,10 +327,12 @@ bool font_win::get_font_families(std::string const &font_path, std::vector<std:: } // anonymous namespace +} // namespace osd + #else // defined(OSD_WINDOWS) || defined(SDLMAME_WIN32) -MODULE_NOT_SUPPORTED(font_win, OSD_FONT_PROVIDER, "win") +namespace osd { namespace { MODULE_NOT_SUPPORTED(font_win, OSD_FONT_PROVIDER, "win") } } #endif // defined(OSD_WINDOWS) || defined(SDLMAME_WIN32) -MODULE_DEFINITION(FONT_WINDOWS, font_win) +MODULE_DEFINITION(FONT_WINDOWS, osd::font_win) |