| 1 |
// Copyright (C) 2010 by Ben Cressey. |
| 2 |
// |
| 3 |
// This file is part of Gargoyle. |
| 4 |
// |
| 5 |
// Gargoyle is free software; you can redistribute it and/or modify |
| 6 |
// it under the terms of the GNU General Public License as published by |
| 7 |
// the Free Software Foundation; either version 2 of the License, or |
| 8 |
// (at your option) any later version. |
| 9 |
// |
| 10 |
// Gargoyle is distributed in the hope that it will be useful, |
| 11 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
// GNU General Public License for more details. |
| 14 |
// |
| 15 |
// You should have received a copy of the GNU General Public License |
| 16 |
// along with Gargoyle; if not, write to the Free Software |
| 17 |
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
|
| 19 |
#import <Cocoa/Cocoa.h> |
| 20 |
|
| 21 |
#include <cstdlib> |
| 22 |
#include <string> |
| 23 |
|
| 24 |
#include "font.h" |
| 25 |
#include "glk.h" |
| 26 |
#include "garglk.h" |
| 27 |
|
| 28 |
static NSMutableArray *gli_registered_fonts = nil; |
| 29 |
static NSDistributedLock *gli_font_lock = nil; |
| 30 |
|
| 31 |
bool garglk::fontreplace(const std::string &font, FontType type) |
| 32 |
{ |
| 33 |
if (font.empty()) { |
| 34 |
return false; |
| 35 |
} |
| 36 |
|
| 37 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 38 |
|
| 39 |
NSString *fontFamily = [NSString stringWithUTF8String: font.c_str()]; |
| 40 |
NSFontDescriptor *fontFamilyDescriptor = |
| 41 |
[[NSFontDescriptor fontDescriptorWithFontAttributes: nil] fontDescriptorWithFamily:fontFamily]; |
| 42 |
|
| 43 |
NSArray *fontMatches = |
| 44 |
[fontFamilyDescriptor matchingFontDescriptorsWithMandatoryKeys: nil]; |
| 45 |
|
| 46 |
FontFiller filler(type); |
| 47 |
|
| 48 |
for (NSFontDescriptor *sysfont in fontMatches) { |
| 49 |
// find path for font |
| 50 |
CFURLRef urlRef = static_cast<CFURLRef>(CTFontDescriptorCopyAttribute((CTFontDescriptorRef)sysfont, kCTFontURLAttribute)); |
| 51 |
if (!urlRef) { |
| 52 |
continue; |
| 53 |
} |
| 54 |
|
| 55 |
CFStringRef fontPathRef = CFURLCopyFileSystemPath(urlRef, kCFURLPOSIXPathStyle); |
| 56 |
|
| 57 |
if (fontPathRef && CFStringGetLength(fontPathRef) > 0) { |
| 58 |
NSString *fontPath = (__bridge NSString *)fontPathRef; |
| 59 |
NSLog(@"fontPath: %@", fontPath); |
| 60 |
|
| 61 |
std::string file = [fontPath UTF8String]; |
| 62 |
auto traits = [sysfont symbolicTraits]; |
| 63 |
|
| 64 |
if ((traits & NSFontBoldTrait) && (traits & NSFontItalicTrait)) { |
| 65 |
filler.add(FontFiller::Style::BoldItalic, file); |
| 66 |
} else if (traits & NSFontBoldTrait) { |
| 67 |
filler.add(FontFiller::Style::Bold, file); |
| 68 |
} else if (traits & NSFontItalicTrait) { |
| 69 |
filler.add(FontFiller::Style::Italic, file); |
| 70 |
} else { |
| 71 |
filler.add(FontFiller::Style::Regular, file); |
| 72 |
} |
| 73 |
|
| 74 |
CFRelease(fontPathRef); |
| 75 |
} |
| 76 |
|
| 77 |
CFRelease(urlRef); |
| 78 |
} |
| 79 |
|
| 80 |
[pool drain]; |
| 81 |
|
| 82 |
return filler.fill(); |
| 83 |
} |
| 84 |
|
| 85 |
void fontload() |
| 86 |
{ |
| 87 |
// 'GARGLK_RESOURCES' should be the path to the Bundle's Resources directory |
| 88 |
const char *env = std::getenv("GARGLK_RESOURCES"); |
| 89 |
if (!env) { |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 94 |
|
| 95 |
gli_font_lock = [[NSDistributedLock alloc] initWithPath: [NSString stringWithFormat: @"%@/com.googlecode.garglk.fontlock", NSTemporaryDirectory()]]; |
| 96 |
|
| 97 |
if (gli_font_lock) { |
| 98 |
while (![gli_font_lock tryLock]) { |
| 99 |
if ([[NSDate date] timeIntervalSinceDate: [gli_font_lock lockDate]] > 30) { |
| 100 |
[gli_font_lock breakLock]; |
| 101 |
} else { |
| 102 |
[NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.25]]; |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
// obtain a list of all files in the Fonts directory |
| 108 |
NSString *fontFolder = [[NSString stringWithUTF8String: env] stringByAppendingPathComponent: @"Fonts"]; |
| 109 |
NSArray *fontFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: fontFolder error: nil]; |
| 110 |
|
| 111 |
// create a collection to hold the registered font URLs |
| 112 |
gli_registered_fonts = [NSMutableArray new]; |
| 113 |
|
| 114 |
for (NSString *fontFile in fontFiles) { |
| 115 |
// convert font file name into a file URL |
| 116 |
NSString *fontPath = [fontFolder stringByAppendingPathComponent: fontFile]; |
| 117 |
NSURL *fontURL = [NSURL fileURLWithPath: fontPath]; |
| 118 |
|
| 119 |
// register font with system |
| 120 |
CFErrorRef error = nullptr; |
| 121 |
if (!CTFontManagerRegisterFontsForURL((__bridge CFURLRef)fontURL, kCTFontManagerScopeProcess, &error)) { |
| 122 |
CFStringRef msg = CFErrorCopyFailureReason(error); |
| 123 |
NSLog(@"fontload: %@", msg); |
| 124 |
CFRelease(msg); |
| 125 |
continue; |
| 126 |
} |
| 127 |
|
| 128 |
[gli_registered_fonts addObject:fontURL]; |
| 129 |
} |
| 130 |
|
| 131 |
[pool drain]; |
| 132 |
} |
| 133 |
|
| 134 |
void fontunload() |
| 135 |
{ |
| 136 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 137 |
|
| 138 |
if (gli_registered_fonts) { |
| 139 |
// uregister fonts |
| 140 |
for (NSURL *url in gli_registered_fonts) { |
| 141 |
CFURLRef urlRef = (__bridge CFURLRef)url; |
| 142 |
|
| 143 |
CFErrorRef error = nullptr; |
| 144 |
if (!CTFontManagerUnregisterFontsForURL(urlRef, kCTFontManagerScopeProcess, &error)) { |
| 145 |
CFStringRef msg = CFErrorCopyFailureReason(error); |
| 146 |
NSLog(@"fontunload: %@", msg); |
| 147 |
CFRelease(msg); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
// remove all objects |
| 152 |
[gli_registered_fonts removeAllObjects]; |
| 153 |
[gli_registered_fonts release]; |
| 154 |
gli_registered_fonts = nil; |
| 155 |
} |
| 156 |
|
| 157 |
if (gli_font_lock) { |
| 158 |
[gli_font_lock unlock]; |
| 159 |
[gli_font_lock release]; |
| 160 |
gli_font_lock = nil; |
| 161 |
} |
| 162 |
|
| 163 |
[pool drain]; |
| 164 |
} |