Correctly handle Unicode's 'Variation selectors' where a Unicode codepoint

modifies the glyph used to draw the previous Unicode codepoint.

git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@10792 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
Manolo Gouy 2015-07-16 15:16:24 +00:00
parent db6b98e4f7
commit 70297713f2
1 changed files with 19 additions and 6 deletions

View File

@ -304,6 +304,18 @@ static CGFloat surrogate_width(const UniChar *txt, Fl_Font_Descriptor *fl_fontsi
if(must_release) CFRelease(font2);
return a.width;
}
static CGFloat variation_selector_width(CFStringRef str16, Fl_Font_Descriptor *fl_fontsize)
{
CGFloat retval;
CFDictionarySetValue(attributes, kCTFontAttributeName, fl_fontsize->fontref);
CFAttributedStringRef mastr = CFAttributedStringCreate(kCFAllocatorDefault, str16, attributes);
CTLineRef ctline = CTLineCreateWithAttributedString(mastr);
CFRelease(mastr);
retval = CTLineGetOffsetForStringIndex(ctline, 2, NULL);
CFRelease(ctline);
return retval;
}
#endif
static double fl_mac_width(const UniChar* txt, int n, Fl_Font_Descriptor *fl_fontsize) {
@ -319,6 +331,13 @@ if (fl_mac_os_version >= Fl_X::CoreText_threshold) {
i++; // because a pair of UniChar's represent a single character
continue;
}
if (i+1 < n && txt[i+1] >= 0xFE00 && txt[i+1] <= 0xFE0F) { // handles variation selectors
CFStringRef substr = CFStringCreateWithCharacters(NULL, txt + i, 2);
retval += variation_selector_width(substr, fl_fontsize);
CFRelease(substr);
i++;
continue;
}
const int block = 0x10000 / (sizeof(fl_fontsize->width)/sizeof(float*)); // block size
// r: index of the character block containing uni
unsigned int r = uni >> 7; // change 7 if sizeof(width) is changed
@ -492,18 +511,12 @@ static CGColorRef flcolortocgcolor(Fl_Color i)
#endif
static void fl_mac_draw(const char *str, int n, float x, float y, Fl_Graphics_Driver *driver) {
// the range [0xFE00-0xFE0F] corresponds to Unicode's 'variation selectors'
static CFCharacterSetRef set = CFCharacterSetCreateWithCharactersInRange(NULL, CFRangeMake(0xFE00, 16));
CFRange res;
// convert to UTF-16 first
UniChar *uniStr = mac_Utf8_to_Utf16(str, n, &n);
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
if (fl_mac_os_version >= Fl_X::CoreText_threshold) {
CFMutableStringRef str16 = CFStringCreateMutableWithExternalCharactersNoCopy(NULL, uniStr, n, n, kCFAllocatorNull);
if (str16 == NULL) return; // shd not happen
while (CFStringFindCharacterFromSet(str16, set, CFRangeMake(0, CFStringGetLength(str16)), 0, &res)) {
CFStringReplace(str16, res, CFSTR("")); // remove all variation selectors from the input string
}
CGColorRef color = flcolortocgcolor(driver->color());
CFDictionarySetValue (attributes, kCTFontAttributeName, driver->font_descriptor()->fontref);
CFDictionarySetValue (attributes, kCTForegroundColorAttributeName, color);