summaryrefslogtreecommitdiff
path: root/platform/darwin/src/local_glyph_rasterizer.mm
blob: b28d67d267cb266a2cd8376c6353f1d4f5766cc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#include <mbgl/text/local_glyph_rasterizer.hpp>
#include <mbgl/util/i18n.hpp>
#include <mbgl/util/platform.hpp>
#include <mbgl/util/constants.hpp>

#include <unordered_map>
#include <iterator>

#import <Foundation/Foundation.h>
#import <CoreText/CoreText.h>
#import <ImageIO/ImageIO.h>

#if TARGET_OS_IPHONE
    #import <UIKit/UIKit.h>
#else
    #import <AppKit/AppKit.h>
#endif

#import "CFHandle.hpp"

/// Enables local glyph rasterization for all writing systems, not just CJK.
#define MBGL_DARWIN_NO_REMOTE_FONTS 1

namespace mbgl {

using CGColorSpaceHandle = CFHandle<CGColorSpaceRef, CGColorSpaceRef, CGColorSpaceRelease>;
using CGContextHandle = CFHandle<CGContextRef, CGContextRef, CGContextRelease>;
using CFStringRefHandle = CFHandle<CFStringRef, CFTypeRef, CFRelease>;
using CFAttributedStringRefHandle = CFHandle<CFAttributedStringRef, CFTypeRef, CFRelease>;
using CFMutableArrayRefHandle = CFHandle<CFMutableArrayRef, CFTypeRef, CFRelease>;
using CFDictionaryRefHandle = CFHandle<CFDictionaryRef, CFTypeRef, CFRelease>;
using CTFontRefHandle = CFHandle<CTFontRef, CFTypeRef, CFRelease>;
using CTFontDescriptorRefHandle = CFHandle<CTFontDescriptorRef, CFTypeRef, CFRelease>;
using CTLineRefHandle = CFHandle<CTLineRef, CFTypeRef, CFRelease>;

CTFontDescriptorRef createFontDescriptor(const FontStack& fontStack, NSArray<NSString *>* fallbackFontNames, bool isVertical);

/**
 Draws glyphs applying fonts that are installed on the system or bundled with
 the application. This is a more flexible and performant alternative to
 typesetting text using glyph sheets downloaded from a server.
 
 This implementation is similar to the local glyph rasterization in GL JS:
  - Only CJK glyphs are drawn locally, because it’s much more noticeable when a
    non-CJK font mismatches the style than when a CJK font mismatches the style.
    (Unlike GL JS, this implementation does respect the font’s metrics, so it
    does work with variable-width fonts.)
  - Fallback fonts can be specified globally, similar to the seldom-used
    advanced font preferences in most Web browsers.
 
 This is a first step toward fully local font rendering:
 <https://github.com/mapbox/mapbox-gl-native/issues/7862>. Further improvements:
  - Make sure the font size is 24 points (`util::ONE_EM`) at all times, not the
    system default of 12 points, to avoid blobbiness after rasterization.
  - Sniff an appropriate font weight and style from the font stack’s font names,
    as GL JS and the Android map SDK do.
  - Enable local glyph rasterization for all writing systems, not just CJK. This
    would require providing a more attractive default Latin font than Helvetica
    or Arial Unicode MS.
  - Allow the developer to specify a `CTFontDescriptor` or
    `NSFontDescriptor`/`UIFontDescriptor` to customize more attributes besides
    the font (but not the font size).
  - Render glyphs directly to SDF using Core Text’s `CTFontCreatePathForGlyph()`
    instead of going through TinySDF.
  - Typeset an entire text label in one shot using `CTFramesetter` to take
    advantage of Core Text’s superior complex text shaping capabilities and
    support for astral plane characters. mbgl would need to stop conflating
    codepoints with glyph IDs.
*/
class LocalGlyphRasterizer::Impl {
public:
    /**
     Creates a new rasterizer with the given font names as a fallback.
     
     The fallback font names can also be specified in the style as a font stack
     or in the `MGLIdeographicFontFamilyName` key of
     `NSUserDefaults.standardUserDefaults`. The font stack takes precedence,
     followed by the `MGLIdeographicFontFamilyName` user default, then finally
     the `fallbackFontNames_` parameter as a last resort.
     
     @param fallbackFontNames_ A list of font names, one per line. Each font
        name can be the PostScript name or display name of a specific font face
        or a font family name. Set this parameter to `nullptr` to disable local
        glyph rasterization globally. The system font is a good default value to
        pass into this constructor.
     */
    Impl(const optional<std::string> fallbackFontNames_)
    {
        fallbackFontNames = [[NSUserDefaults standardUserDefaults] stringArrayForKey:@"MGLIdeographicFontFamilyName"];
        if (fallbackFontNames_) {
            fallbackFontNames = [fallbackFontNames ?: @[] arrayByAddingObjectsFromArray:[@(fallbackFontNames_->c_str()) componentsSeparatedByString:@"\n"]];
        }
    }
    
    /**
     Returns whether local glyph rasterization is enabled globally.
     
     The developer can disable local glyph rasterization by specifying no
     fallback font names.
     */
    bool isEnabled() { return fallbackFontNames; }
    
private:
    NSArray<NSString *> *fallbackFontNames;
};

LocalGlyphRasterizer::LocalGlyphRasterizer(const optional<std::string>& fontFamily)
    : impl(std::make_unique<Impl>(fontFamily))
{}

LocalGlyphRasterizer::~LocalGlyphRasterizer()
{}

/**
 Returns whether the rasterizer can rasterize a glyph for the given codepoint.
 
 @param glyphID A font-agnostic Unicode codepoint, not a glyph index.
 @returns Whether a glyph for the codepoint can be rasterized.
 */
bool LocalGlyphRasterizer::canRasterizeGlyph(const FontStack&, GlyphID glyphID) {
#if MBGL_DARWIN_NO_REMOTE_FONTS
    return impl->isEnabled();
#else
    return util::i18n::allowsFixedWidthGlyphGeneration(glyphID) && impl->isEnabled();
#endif
}

/**
 Draws the given codepoint into an image, gathers metrics about the glyph, and
 returns the image.
 
 @param glyphID A font-agnostic Unicode codepoint, not a glyph index.
 @param font The font to apply to the codepoint.
 @param size The size of the glyph.
 @returns An image containing the glyph.
 */
PremultipliedImage drawGlyphBitmap(GlyphID glyphID, CTFontRef font, Size size) {
    CGGlyph glyphs[] = { glyphID };
    PremultipliedImage rgbaBitmap(size);
    
    CGColorSpaceHandle colorSpace(CGColorSpaceCreateDeviceRGB());
    if (!colorSpace) {
        throw std::runtime_error("CGColorSpaceCreateDeviceRGB failed");
    }
    
    constexpr const size_t bitsPerComponent = 8;
    constexpr const size_t bytesPerPixel = 4;
    const size_t bytesPerRow = bytesPerPixel * size.width;

    CGContextHandle context(CGBitmapContextCreate(
        rgbaBitmap.data.get(),
        size.width,
        size.height,
        bitsPerComponent,
        bytesPerRow,
        *colorSpace,
        kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast));
    if (!context) {
        throw std::runtime_error("CGBitmapContextCreate failed");
    }
    
    CGContextSetShouldSubpixelPositionFonts(*context, false);
    CGContextSetShouldSubpixelQuantizeFonts(*context, false);
    
    CGPoint positions[1];
    positions[0] = CGPointMake(0.0, 0.0);
    CTFontDrawGlyphs(font, glyphs, positions, 1, *context);
    
//    const CGFloat *black = CGColorGetComponents(CGColorGetConstantColor(kCGColorBlack));
//    CGContextSetFillColor(*context, black);
//    CGContextFillRect(*context, CGRectMake(0, 0, size.width, size.height));
    
    constexpr const size_t bitsPerPixel = bitsPerComponent * bytesPerPixel;
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, rgbaBitmap.data.get(), bytesPerPixel * size.width * size.height, [](void*, const void*, size_t) {});
    CGImageRef image = CGImageCreate(size.width, size.height, bitsPerComponent, bitsPerPixel,
                                     bytesPerRow, *colorSpace,
                                     kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast, provider,
                                     NULL, false, kCGRenderingIntentDefault);
    CGDataProviderRelease(provider);
    CGImageRelease(image);
    return rgbaBitmap;
}

/**
 Returns all the information mbgl needs about a glyph representation of the
 given codepoint.
 
 @param fontStack The best matching font in this font stack is applied to the
    codepoint.
 @param glyphID A font-agnostic Unicode codepoint, not a glyph index.
 @returns A glyph representation of the given codepoint with its bitmap data and
    metrics set.
 */
Glyph LocalGlyphRasterizer::rasterizeGlyph(const FontStack& fontStack, GlyphID glyphID) {
    Glyph manufacturedGlyph;
    CFStringRef fontName = (__bridge CFStringRef)@(fontStack.front().c_str());
    CTFontRefHandle font(CTFontCreateWithName(fontName, 0.0, NULL));
    
    manufacturedGlyph.id = glyphID;

    // TODO: Plumb through vertical text.
    CTFontOrientation orientation = kCTFontOrientationHorizontal;
    
    CGRect boundingRects[1];
    CGGlyph glyphs[] = { glyphID };
    CGRect boundingRect = CTFontGetBoundingRectsForGlyphs(*font, orientation, glyphs, boundingRects, 1);
    if (CGRectIsNull(boundingRect)) {
        throw std::runtime_error("CTFontGetBoundingRectsForGlyphs failed");
    }
    manufacturedGlyph.metrics.left = std::round(CGRectGetMinX(boundingRects[0]));
    manufacturedGlyph.metrics.top = std::round(CGRectGetMinY(boundingRects[0]));
    manufacturedGlyph.metrics.width = std::ceil(CGRectGetWidth(boundingRects[0]));
    manufacturedGlyph.metrics.height = std::ceil(CGRectGetHeight(boundingRects[0]));
    
    CGSize advances[1];
    CTFontGetAdvancesForGlyphs(*font, orientation, glyphs, advances, 1);
    manufacturedGlyph.metrics.advance = std::ceil(advances[0].width);
    
#if TARGET_OS_IPHONE
    CGFloat scaleFactor = UIScreen.mainScreen.scale;
#else
    CGFloat scaleFactor = NSScreen.mainScreen.backingScaleFactor;
#endif
    Size size(MAX(manufacturedGlyph.metrics.width, 1) * scaleFactor,
              MAX(manufacturedGlyph.metrics.height, 1) * scaleFactor);
    PremultipliedImage rgbaBitmap = drawGlyphBitmap(glyphID, *font, size);
    
    // Copy alpha values from RGBA bitmap into the AlphaImage output
    manufacturedGlyph.bitmap = AlphaImage(size);
    for (uint32_t i = 0; i < size.width * size.height; i++) {
        manufacturedGlyph.bitmap.data[i] = rgbaBitmap.data[4 * i + 3];
    }

    return manufacturedGlyph;
}

/**
 Creates a font descriptor representing the given font stack and any global
 fallback fonts.
 
 @param fontStack The font stack that takes precedence.
 @param fallbackFontNames A list of font names to use as fallbacks if none of
    the fonts in the font stack is available.
 @param isVertical Whether the text to be drawn is laid out vertically.
 @returns A font descriptor representing the first font in the font stack with a
    cascade list representing the rest of the fonts in the font stack and any
    fallback fonts. The font descriptor is not cached.
 
 @post The caller is responsible for releasing the font descriptor.
 */
CTFontDescriptorRef createFontDescriptor(const FontStack& fontStack, NSArray<NSString *>* fallbackFontNames, bool isVertical) {
    NSMutableArray *fontNames = [NSMutableArray arrayWithCapacity:fontStack.size() + fallbackFontNames.count];
    for (auto& fontName : fontStack) {
        // Per the Mapbox Style Specification, the text-font property comes with
        // these last resort fonts by default, but they shouldn’t take
        // precedence over any application or system fallback font that may be
        // more appropriate to the current device.
        if (fontName != util::LAST_RESORT_ALPHABETIC_FONT && fontName != util::LAST_RESORT_PAN_UNICODE_FONT) {
            [fontNames addObject:@(fontName.c_str())];
        }
    }
    [fontNames addObjectsFromArray:fallbackFontNames];
    
    if (!fontNames.count) {
        NSDictionary *fontAttributes = @{
            (NSString *)kCTFontSizeAttribute: @(util::ONE_EM),
        };
        return CTFontDescriptorCreateWithAttributes((CFDictionaryRef)fontAttributes);
    }
    
    // Apply the first font name to the returned font descriptor; apply the rest
    // of the font names to the cascade list.
    CFStringRef mainFontName = (__bridge CFStringRef)fontNames.firstObject;
    CFMutableArrayRefHandle fallbackDescriptors(CFArrayCreateMutable(kCFAllocatorDefault, fontNames.count, &kCFTypeArrayCallBacks));
    for (NSString *name in fontNames) {
        NSDictionary *fontAttributes = @{
            (NSString *)kCTFontSizeAttribute: @(util::ONE_EM),
            // The name could be any of these three attributes of the font. It’s
            // OK if it doesn’t match all three; Core Text will pick the font
            // that matches the most attributes.
            (NSString *)kCTFontNameAttribute: name,
            (NSString *)kCTFontDisplayNameAttribute: name,
            (NSString *)kCTFontFamilyNameAttribute: name,
        };
        
        CTFontDescriptorRefHandle descriptor(CTFontDescriptorCreateWithAttributes((CFDictionaryRef)fontAttributes));
        CFArrayAppendValue(*fallbackDescriptors, *descriptor);
    }
    
    CTFontOrientation orientation = isVertical ? kCTFontOrientationVertical : kCTFontOrientationHorizontal;

    CFStringRef keys[] = {
        kCTFontSizeAttribute,
        kCTFontNameAttribute, kCTFontDisplayNameAttribute, kCTFontFamilyNameAttribute,
        kCTFontCascadeListAttribute,
        kCTFontOrientationAttribute,
    };
    CFTypeRef values[] = {
        (__bridge CFNumberRef)@(util::ONE_EM),
        mainFontName, mainFontName, mainFontName,
        *fallbackDescriptors,
        (__bridge CFNumberRef)@(orientation),
    };

    CFDictionaryRefHandle attributes(
        CFDictionaryCreate(kCFAllocatorDefault, (const void**)&keys,
            (const void**)&values, sizeof(keys) / sizeof(keys[0]),
            &kCFTypeDictionaryKeyCallBacks,
            &kCFTypeDictionaryValueCallBacks));
    return CTFontDescriptorCreateWithAttributes(*attributes);
}

GlyphDependencies getGlyphDependencies(const FontStack& fontStack, const std::string& text, bool isVertical) {
    // TODO: Implement global fallback fonts.
    CTFontDescriptorRefHandle descriptor(createFontDescriptor(fontStack, @[], isVertical));
    CTFontRefHandle font(CTFontCreateWithFontDescriptor(*descriptor, 0.0, NULL));

    CFStringRef keys[] = { kCTFontAttributeName };
    CFTypeRef values[] = { *font };

    CFDictionaryRefHandle attributes(
        CFDictionaryCreate(kCFAllocatorDefault, (const void**)&keys,
            (const void**)&values, sizeof(keys) / sizeof(keys[0]),
            &kCFTypeDictionaryKeyCallBacks,
            &kCFTypeDictionaryValueCallBacks));
    if (!attributes) {
        throw std::runtime_error("Unable to create attributed string attributes dictionary");
    }

    CFStringRef string = (__bridge CFStringRef)@(text.c_str());
    CFAttributedStringRefHandle attrString(CFAttributedStringCreate(kCFAllocatorDefault, string, *attributes));
    if (!attrString) {
        throw std::runtime_error("Unable to create attributed string");
    }
    CTLineRefHandle line(CTLineCreateWithAttributedString(*attrString));
    if (!line) {
        throw std::runtime_error("Unable to create line from attributed string");
    }
    
    CFArrayRef glyphRuns = CTLineGetGlyphRuns(*line);
    GlyphDependencies dependencies;
    for (CFIndex i = 0; i < CFArrayGetCount(glyphRuns); i++) {
        CTRunRef glyphRun = (CTRunRef)CFArrayGetValueAtIndex(glyphRuns, 0);
        CFRange wholeRunRange = CFRangeMake(0, CTRunGetGlyphCount(glyphRun));
        
        CTFontRef glyphFont = (CTFontRef)CFDictionaryGetValue(CTRunGetAttributes(glyphRun), kCTFontAttributeName);
        CFStringRefHandle glyphFontName(CTFontCopyName(glyphFont, kCTFontPostScriptNameKey));
        FontStack glyphFontStack = {{ [(__bridge NSString *)*glyphFontName UTF8String] }};
        
        // Use CTRunGetGlyphsPtr() if available.
        CGGlyph glyphs[wholeRunRange.length];
        CTRunGetGlyphs(glyphRun, wholeRunRange, glyphs);
        
        GlyphIDs& glyphIDs = dependencies[glyphFontStack];
        glyphIDs.insert(glyphs, glyphs + wholeRunRange.length);
    }
    return dependencies;
}

std::vector<GlyphID> getGlyphIDs(const FontStack& fontStack, const std::string& text, bool isVertical) {
    // TODO: Implement global fallback fonts.
    CTFontDescriptorRefHandle descriptor(createFontDescriptor(fontStack, @[], isVertical));
    CTFontRefHandle font(CTFontCreateWithFontDescriptor(*descriptor, 0.0, NULL));

    CFStringRef keys[] = { kCTFontAttributeName };
    CFTypeRef values[] = { *font };

    CFDictionaryRefHandle attributes(
        CFDictionaryCreate(kCFAllocatorDefault, (const void**)&keys,
            (const void**)&values, sizeof(keys) / sizeof(keys[0]),
            &kCFTypeDictionaryKeyCallBacks,
            &kCFTypeDictionaryValueCallBacks));

    CFStringRef string = (__bridge CFStringRef)@(text.c_str());
    CFAttributedStringRefHandle attrString(CFAttributedStringCreate(kCFAllocatorDefault, string, *attributes));
    CTLineRefHandle line(CTLineCreateWithAttributedString(*attrString));
    
    CFArrayRef glyphRuns = CTLineGetGlyphRuns(*line);
    std::vector<GlyphID> glyphIDs;
    for (CFIndex i = 0; i < CFArrayGetCount(glyphRuns); i++) {
        CTRunRef glyphRun = (CTRunRef)CFArrayGetValueAtIndex(glyphRuns, 0);
        CFRange wholeRunRange = CFRangeMake(0, CTRunGetGlyphCount(glyphRun));
        
        CTFontRef glyphFont = (CTFontRef)CFDictionaryGetValue(CTRunGetAttributes(glyphRun), kCTFontAttributeName);
        CFStringRefHandle glyphFontName(CTFontCopyName(glyphFont, kCTFontPostScriptNameKey));
        FontStack glyphFontStack = {{ [(__bridge NSString *)*glyphFontName UTF8String] }};
        
        // Use CTRunGetGlyphsPtr() if available.
        CGGlyph glyphs[wholeRunRange.length];
        CTRunGetGlyphs(glyphRun, wholeRunRange, glyphs);
        
        glyphIDs.insert(glyphIDs.end(), glyphs, glyphs + wholeRunRange.length);
    }
    return glyphIDs;
}

} // namespace mbgl