Skip to content

Commit

Permalink
GRAPHICS: Detect substitute for Mac fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
sev- committed Oct 8, 2016
1 parent 66a0ca6 commit c0b7658
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 47 deletions.
158 changes: 112 additions & 46 deletions graphics/macgui/macfontmanager.cpp
Expand Up @@ -28,7 +28,55 @@

namespace Graphics {

// Source: Apple IIGS Technical Note #41, "Font Family Numbers"
// http://apple2.boldt.ca/?page=til/tn.iigs.041
static const char *const fontNames[] = {
"Chicago", // system font
"Geneva", // application font
"New York",
"Geneva",

"Monaco",
"Venice",
"London",
"Athens",

"San Francisco",
"Toronto",
NULL,
"Cairo",
"Los Angeles", // 12

"Zapf Dingbats",
"Bookman",
"Helvetica Narrow",
"Palatino",
NULL,
"Zapf Chancery",
NULL,

"Times", // 20
"Helvetica",
"Courier",
"Symbol",
"Taliesin", // mobile?
NULL,
NULL,
NULL,
NULL,
NULL,
NULL, // 30
NULL,
NULL,
"Avant Garde",
"New Century Schoolbook"
};

MacFontManager::MacFontManager() {
for (uint i = 0; i < ARRAYSIZE(fontNames); i++)
if (fontNames[i])
_fontNames.setVal(fontNames[i], i);

loadFonts();
}

Expand All @@ -55,8 +103,12 @@ void MacFontManager::loadFonts() {
delete stream;

Common::String fontName;
MacFont *macfont;

if (font->getFamilyName() && *font->getFamilyName()) {
fontName = Common::String::format("%s-%s-%d", font->getFamilyName(), font->getFontSlant(), font->getFontSize());

macfont = new MacFont(_fontNames.getVal(font->getFamilyName(), kMacFontNonStandard), font->getFontSize(), parseFontSlant(font->getFontSlant()));
} else { // Get it from the file name
fontName = (*it)->getName();

Expand All @@ -69,10 +121,13 @@ void MacFontManager::loadFonts() {
break;
}
}

macfont = new MacFont(kMacFontNonStandard);
macfont->setName(fontName);
}

FontMan.assignFontToName(fontName, font);
_fontRegistry.setVal(fontName, font);
_fontRegistry.setVal(fontName, macfont);

debug(2, " %s", fontName.c_str());
}
Expand Down Expand Up @@ -107,49 +162,20 @@ const Font *MacFontManager::getFont(MacFont macFont) {
return font;
}

// Source: Apple IIGS Technical Note #41, "Font Family Numbers"
// http://apple2.boldt.ca/?page=til/tn.iigs.041
static const char *const fontNames[] = {
"Chicago", // system font
"Geneva", // application font
"New York",
"Geneva",
int MacFontManager::parseFontSlant(Common::String slant) {
slant.toUppercase();

"Monaco",
"Venice",
"London",
"Athens",

"San Francisco",
"Toronto",
NULL,
"Cairo",
"Los Angeles", // 12
if (slant == "I")
return kMacFontItalic;
if (slant == "B")
return kMacFontBold;
if (slant == "R")
return kMacFontRegular;

"Zapf Dingbats",
"Bookman",
"Helvetica Narrow",
"Palatino",
NULL,
"Zapf Chancery",
NULL,
warning("Unknown font slant '%s'", slant.c_str());

"Times", // 20
"Helvetica",
"Courier",
"Symbol",
"Taliesin", // mobile?
NULL,
NULL,
NULL,
NULL,
NULL,
NULL, // 30
NULL,
NULL,
"Avant Garde",
"New Century Schoolbook"
};
return kMacFontRegular;
}

const char *MacFontManager::getFontName(int id, int size, int slant) {
static char name[128];
Expand Down Expand Up @@ -181,21 +207,61 @@ const char *MacFontManager::getFontName(MacFont &font) {
}

void MacFontManager::generateFontSubstitute(MacFont &macFont) {
if (_fontRegistry.contains(getFontName(macFont.getId(), macFont.getSize() * 2, macFont.getSlant()))) {
generateFont(macFont, MacFont(macFont.getId(), macFont.getSize() * 2, macFont.getSlant()));
Common::String name;

// First we try twice size
name = getFontName(macFont.getId(), macFont.getSize() * 2, macFont.getSlant());
if (_fontRegistry.contains(name) && !_fontRegistry[name]->isGenerated()) {
generateFont(macFont, *_fontRegistry[name]);

return;
}

if (_fontRegistry.contains(getFontName(macFont.getId(), macFont.getSize() / 2, macFont.getSlant()))) {
generateFont(macFont, MacFont(macFont.getId(), macFont.getSize() / 2, macFont.getSlant()));
// Now half size
name = getFontName(macFont.getId(), macFont.getSize() / 2, macFont.getSlant());
if (_fontRegistry.contains(name)) {
generateFont(macFont, *_fontRegistry[name]);

return;
}

// No simple substitute was found. Looking for neighborhood fonts

// First we gather all font sizes for this font
Common::Array<int> sizes;
for (Common::HashMap<Common::String, MacFont *>::iterator i = _fontRegistry.begin(); i != _fontRegistry.end(); ++i) {
if (i->_value->getId() == macFont.getId() && i->_value->getSlant() == macFont.getSlant())
sizes.push_back(i->_value->getSize());
}

if (sizes.empty()) {
warning("No viable substitute found for font %s", getFontName(macFont));
return;
}

// Now looking next larger font, and store the largest one for next check
int candidate = 1000;
int maxSize = sizes[0];
for (uint i = 0; i < sizes.size(); i++) {
if (sizes[i] > macFont.getSize() && sizes[i] < candidate)
candidate = sizes[i];

if (sizes[i] > maxSize)
maxSize = sizes[i];
}

if (candidate != 1000) {
generateFont(macFont, MacFont(macFont.getId(), candidate, macFont.getSlant()));
return;
}

// Now next smaller font, which is the biggest we have
generateFont(macFont, MacFont(macFont.getId(), maxSize, macFont.getSlant()));
}

void MacFontManager::generateFont(MacFont fromFont, MacFont toFont) {
warning("Found font substitute from font %s to %s", getFontName(fromFont), getFontName(toFont));
debugN("Found font substitute for font %s ", getFontName(fromFont));
debug("as %s", getFontName(toFont));
}

} // End of namespace Graphics
12 changes: 11 additions & 1 deletion graphics/macgui/macfontmanager.h
Expand Up @@ -28,6 +28,7 @@
namespace Graphics {

enum {
kMacFontNonStandard = -1,
kMacFontChicago = 0
};

Expand All @@ -46,6 +47,7 @@ class MacFont {
_size = size;
_slant = slant;
_fallback = fallback;
_generated = false;
}

int getId() { return _id; };
Expand All @@ -55,13 +57,17 @@ class MacFont {
void setName(Common::String &name) { _name = name; }
void setName(const char *name) { _name = name; }
FontManager::FontUsage getFallback() { return _fallback; }
bool isGenerated() { return _generated; }
void setGenerated(bool gen) { _generated = gen; }

private:
int _id;
int _size;
int _slant;
Common::String _name;
FontManager::FontUsage _fallback;

bool _generated;
};

class MacFontManager {
Expand Down Expand Up @@ -98,7 +104,11 @@ class MacFontManager {

private:
bool _builtInFonts;
Common::HashMap<Common::String, BdfFont *> _fontRegistry;
Common::HashMap<Common::String, MacFont *> _fontRegistry;

Common::HashMap<Common::String, int> _fontNames;

int parseFontSlant(Common::String slant);
};

} // End of namespace Graphics
Expand Down

0 comments on commit c0b7658

Please sign in to comment.