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
|
#include <mbgl/text/shaping.hpp>
#import "CFHandle.hpp"
#import <Foundation/Foundation.h>
#import <CoreText/CoreText.h>
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 CFDictionaryRefHandle = CFHandle<CFDictionaryRef, CFTypeRef, CFRelease>;
using CTFontDescriptorRefHandle = CFHandle<CTFontDescriptorRef, CFTypeRef, CFRelease>;
using CTLineRefHandle = CFHandle<CTLineRef, CFTypeRef, CFRelease>;
#define PTR_OR_ARRAY(name) (name##Ptr ?: name)
float shapeLine(Shaping& shaping, const std::u16string& text, const float y) {
float maxLineLength = 0;
NSDictionary *fontAttributes = @{
(NSString *)kCTFontSizeAttribute: @24.0
};
CFStringRefHandle string(CFStringCreateWithCharacters(NULL, reinterpret_cast<UniChar*>(const_cast<char16_t*>(text.c_str())), text.length()));
CFAttributedStringRef attrStr = CFAttributedStringCreate(NULL, *string, (CFDictionaryRef)fontAttributes);
CTLineRef line = CTLineCreateWithAttributedString(attrStr);
CFArrayRef runs = CTLineGetGlyphRuns(line);
CGSize runAdvance = CGSizeZero;
for (CFIndex i = 0; i < CFArrayGetCount(runs); i++) {
CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runs, i);
CFDictionaryRef attrs = CTRunGetAttributes(run);
CTFontRef fontRef = (CTFontRef)CFDictionaryGetValue(attrs, CFSTR("NSFont"));
CFStringRef fontNameRef = CTFontCopyName(fontRef, kCTFontFamilyNameKey);
CFIndex length = CFStringGetLength(fontNameRef);
UniChar* buffer = (UniChar*)malloc(length * sizeof(UniChar));
CFStringGetCharacters(fontNameRef, CFRangeMake(0, length), buffer);
std::u16string fontName((char16_t*)buffer, length);
CFRelease(fontNameRef);
free(buffer);
const CGGlyph *runGlyphsPtr = CTRunGetGlyphsPtr(run);
CFIndex runGlyphCount = CTRunGetGlyphCount(run);
CGGlyph *runGlyphs = NULL;
if (!runGlyphsPtr) {
runGlyphs = (CGGlyph*)malloc(runGlyphCount * sizeof(CGGlyph));
CTRunGetGlyphs(run, CFRangeMake(0, 0), runGlyphs);
}
NSDictionary* nsAttrs = (__bridge NSDictionary*)attrs;
CTFontRef runFont = CTFontCreateWithName(fontNameRef, [nsAttrs[@"NSFontSizeAttribute"] doubleValue], NULL);
CGRect boundingRects[runGlyphCount];
CTFontGetBoundingRectsForGlyphs(runFont, kCTFontOrientationHorizontal, PTR_OR_ARRAY(runGlyphs), boundingRects, runGlyphCount);
CGSize advances[runGlyphCount];
CTFontGetAdvancesForGlyphs(runFont, kCTFontOrientationHorizontal, PTR_OR_ARRAY(runGlyphs), advances, runGlyphCount);
//CGSize runAdvance = CGSizeZero;
for (CFIndex j = 0; j < runGlyphCount; j++) {
CGRect frame = boundingRects[j];
frame.origin.x += runAdvance.width;
//frame.origin.y += runAdvance.height;
runAdvance.width += advances[j].width;
runAdvance.height += advances[j].height;
shaping.positionedGlyphs.emplace_back(GlyphID(fontName, PTR_OR_ARRAY(runGlyphs)[j]), frame.origin.x, y + frame.origin.y - frame.size.height, false);
maxLineLength = std::max<float>(maxLineLength, frame.origin.x + frame.size.width);
}
free(runGlyphs);
}
return maxLineLength;
}
GlyphIDs getGlyphDependencies(const std::u16string& text) {
GlyphIDs dependencies;
NSDictionary *fontAttributes = @{
(NSString *)kCTFontSizeAttribute: @24.0
};
CFStringRefHandle string(CFStringCreateWithCharacters(NULL, reinterpret_cast<UniChar*>(const_cast<char16_t*>(text.c_str())), text.length()));
CFAttributedStringRef attrStr = CFAttributedStringCreate(NULL, *string, (CFDictionaryRef)fontAttributes);
CTLineRef line = CTLineCreateWithAttributedString(attrStr);
CFArrayRef runs = CTLineGetGlyphRuns(line);
for (CFIndex i = 0; i < CFArrayGetCount(runs); i++) {
CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runs, i);
CFDictionaryRef attrs = CTRunGetAttributes(run);
CTFontRef fontRef = (CTFontRef)CFDictionaryGetValue(attrs, CFSTR("NSFont"));
CFStringRef fontNameRef = CTFontCopyName(fontRef, kCTFontFamilyNameKey);
CFIndex length = CFStringGetLength(fontNameRef);
UniChar* buffer = (UniChar*)malloc(length * sizeof(UniChar));
CFStringGetCharacters(fontNameRef, CFRangeMake(0, length), buffer);
std::u16string fontName((char16_t*)buffer, length);
CFRelease(fontNameRef);
free(buffer);
const CGGlyph *runGlyphsPtr = CTRunGetGlyphsPtr(run);
CFIndex runGlyphCount = CTRunGetGlyphCount(run);
CGGlyph *runGlyphs = NULL;
if (!runGlyphsPtr) {
runGlyphs = (CGGlyph*)malloc(runGlyphCount * sizeof(CGGlyph));
CTRunGetGlyphs(run, CFRangeMake(0, 0), runGlyphs);
}
for (CFIndex j = 0; j < runGlyphCount; j++) {
dependencies.insert(GlyphID(fontName, PTR_OR_ARRAY(runGlyphs)[j]));
}
free(runGlyphs);
}
return dependencies;
}
}; // end mbgl
|