Good point. Well, it may be too slow, but if a glyph isn't found(via CTFontGetGlyphsForCharacters) how about calling CTFontCreateForString with just that UniChar?
Pain in the butt though...
If a glyph could not be encoded, a value of 0 is passed back at the corresponding index in the glyphs array and the function returns False.
A little wrapper around obj-c
template <typename T>
class JuceOBJCType
{
public:
inline JuceOBJCType(const T* t = nil) : objCtype(t) {}
inline JuceOBJCType(const JuceOBJCType &helper) : objCtype(helper.objCtype) { if (objCtype) [objCtype retain]; }
inline ~JuceOBJCType() { if (objCtype) [objCtype release]; }
inline operator T*() { return objCtype; }
inline T * get(){
return objCtype;
}
inline JuceOBJCType operator =(const JuceOBJCType &helper)
{
if (objCtype !=helper.objCtype)
{
[helper.objCtype retain];
[objCtype release];
objCtype=helper.objCtype;
}
return *this;
}
inline T *operator&() { return &objCtype; }
protected:
T * objCtype;
};
void createGlyphsForString (String::CharPointerType text, const int length, HeapBlock <CGGlyph>& glyphs)
{
//http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/TextLayout/Tasks/DrawingStrings.html
textStorage=[[NSTextStorage alloc] init];
layoutManager=[[NSLayoutManager alloc] init];
[textStorage.get() addLayoutManager:layoutManager];
textContainer=[[NSTextContainer alloc] init];
[layoutManager.get() addTextContainer:textContainer];
[[textStorage.get() mutableString] setString:juceStringToNS(String(text,length))];
#if SUPPORT_10_4_FONTS
#if ! SUPPORT_ONLY_10_4_FONTS
if (NEW_CGFONT_FUNCTIONS_UNAVAILABLE)
#endif
{
glyphs.malloc (sizeof (NSGlyph) * length, 1);
NSGlyph* const nsGlyphs = reinterpret_cast<NSGlyph*> (glyphs.getData());
for (int i = 0; i < [layoutManager.get() numberOfGlyphs]; ++i)
{ nsGlyphs[i] = [layoutManager.get() glyphAtIndex:i];//(NSGlyph) [nsFont _defaultGlyphForChar: text.getAndAdvance()];
jassert(textStorage !=nil);
NSRange aRange=NSMakeRange(1,1); // Let's see what font is at the index now...
NSLog([[textStorage.get() attribute:NSFontAttributeName
atIndex:1 effectiveRange:&aRange]fontName]);
}
return;
}
#endif
#if ! SUPPORT_ONLY_10_4_FONTS
if (charToGlyphMapper == nullptr)
charToGlyphMapper = new CharToGlyphMapper (fontRef);
glyphs.malloc (length);
for (int i = 0; i < length; ++i)
glyphs[i] = (CGGlyph) charToGlyphMapper->getGlyphForCharacter (text.getAndAdvance());
#endif
}
//==============================================================================
CGFontRef fontRef;
float fontHeightToCGSizeFactor;
CGAffineTransform renderingTransform;
JuceOBJCType<NSTextStorage> textStorage;
JuceOBJCType<NSLayoutManager> layoutManager;
JuceOBJCType<NSTextContainer> textContainer;
That only returns one font. What we'd need to handle is a string where some of the characters end up using a fallback font, and other don'
Thanks Justin! The main worry about your approach is that it'll be vastly slower than the old code.
(Interesting that you wrote an obj-c wrapper - I've considered writing one of those many times, but never quite got around to it.)
Return to General JUCE discussion
Users browsing this forum: Daven, Google [Bot] and 4 guests