OSStatus ATSUStyleSetFontIDAndSize(ATSUStyle iStyle, ATSUFontID fid, float fsize) { ATSUAttributeTag tags[2] = {kATSUFontTag, kATSUSizeTag}; Fixed ffsize = X2Fix(fsize); ByteCount counts[2] = {sizeof(ATSUFontID*), sizeof(Fixed*)}; ATSUAttributeValuePtr vals[2] = {&fid, &ffsize}; return ATSUSetAttributes( iStyle, 2, tags, counts, vals); }
static ATSUStyle CreateSizedCopyOfStyle(ATSUStyle inStyle, cairo_matrix_t *scale) { ATSUStyle style; OSStatus err; // Set the style's size CGAffineTransform theTransform = CGAffineTransformMakeWithCairoFontScale(scale); Fixed theSize = FloatToFixed(CGSizeApplyAffineTransform (CGSizeMake(1.0, 1.0), theTransform).height); const ATSUAttributeTag theFontStyleTags[] = { kATSUSizeTag }; const ByteCount theFontStyleSizes[] = { sizeof(Fixed) }; ATSUAttributeValuePtr theFontStyleValues[] = { &theSize }; err = ATSUCreateAndCopyStyle(inStyle, &style); err = ATSUSetAttributes(style, sizeof(theFontStyleTags) / sizeof(ATSUAttributeTag), theFontStyleTags, theFontStyleSizes, theFontStyleValues); return style; }
// Sets kATSUQDBoldfaceTag for the given style // void MySetBoldfaceTag(ATSUStyle iStyle) { Boolean setToTrue = true; ATSUAttributeTag tag = kATSUQDBoldfaceTag; ByteCount size = sizeof(Boolean); ATSUAttributeValuePtr value = (ATSUAttributeValuePtr) &setToTrue; verify_noerr( ATSUSetAttributes(iStyle, 1, &tag, &size, &value) ); }
static OSStatus CreateStyleObjectWithFontName( char *fontName, Fixed *fontSize, RGBColor *fontColor, ATSUStyle *newStyle ) { OSStatus err; ATSUFontID fontID; ATSUAttributeTag tags[3]; ByteCount tagValueSizes[3]; ATSUAttributeValuePtr tagValuePtrs[3]; // try to find the font based on the font name err = ATSUFindFontFromName( fontName, strlen( fontName ), kFontFullName, kFontNoPlatform, kFontNoScript, kFontNoLanguage, &fontID ); require_noerr( err, CreateStyleObjectWithFontName_err ); // first, create the new style object err = ATSUCreateStyle( newStyle ); // set up the three tags that we are setting in the style tags[0] = kATSUFontTag; tagValueSizes[0] = sizeof( ATSUFontID ); tagValuePtrs[0] = &fontID; tags[1] = kATSUSizeTag; tagValueSizes[1] = sizeof( Fixed ); tagValuePtrs[1] = fontSize; tags[2] = kATSUColorTag; tagValueSizes[2] = sizeof( RGBColor ); tagValuePtrs[2] = fontColor; // set the attributes in the style object err = ATSUSetAttributes( *newStyle, 3, tags, tagValueSizes, tagValuePtrs ); check_noerr( err ); // if there was an error, then dispose of the style if ( err != noErr ) { ATSUDisposeStyle( *newStyle ); } CreateStyleObjectWithFontName_err: return err; }
// Updates the ATSUI style to the current font and size // void UpdateATSUIStyle(void) { ATSUAttributeTag tags[2]; ByteCount sizes[2]; ATSUAttributeValuePtr values[2]; tags[0] = kATSUFontTag; sizes[0] = sizeof(ATSUFontID); values[0] = &gFont; tags[1] = kATSUSizeTag; sizes[1] = sizeof(Fixed); values[1] = &gPointSize; verify_noerr( ATSUSetAttributes(gStyle, 2, tags, sizes, values) ); }
static cairo_status_t _cairo_atsui_font_glyph_extents(void *abstract_font, cairo_glyph_t * glyphs, int num_glyphs, cairo_text_extents_t * extents) { cairo_atsui_font_t *font = abstract_font; OSStatus err; assert(num_glyphs == 1); GlyphID theGlyph = glyphs[0].index; ATSGlyphIdealMetrics metricsH, metricsV; ATSUStyle style; ATSUCreateAndCopyStyle(font->unscaled_style, &style); err = ATSUGlyphGetIdealMetrics(style, 1, &theGlyph, 0, &metricsH); ATSUVerticalCharacterType verticalType = kATSUStronglyVertical; const ATSUAttributeTag theTag[] = { kATSUVerticalCharacterTag }; const ByteCount theSizes[] = { sizeof(verticalType) }; ATSUAttributeValuePtr theValues[] = { &verticalType }; err = ATSUSetAttributes(style, 1, theTag, theSizes, theValues); err = ATSUGlyphGetIdealMetrics(style, 1, &theGlyph, 0, &metricsV); extents->x_bearing = metricsH.sideBearing.x; extents->y_bearing = metricsV.advance.y; extents->width = metricsH.advance.x - metricsH.sideBearing.x - metricsH.otherSideBearing.x; extents->height = -metricsV.advance.y - metricsV.sideBearing.y - metricsV.otherSideBearing.y; extents->x_advance = metricsH.advance.x; extents->y_advance = 0; ATSUDisposeStyle(style); return CAIRO_STATUS_SUCCESS; }
void QFontEngineMac::getUnscaledGlyph(glyph_t glyph, QPainterPath *path, glyph_metrics_t *metrics) { ATSUStyle unscaledStyle; ATSUCreateAndCopyStyle(style, &unscaledStyle); int emSquare = properties().emSquare.toInt(); const int maxAttributeCount = 4; ATSUAttributeTag tags[maxAttributeCount + 1]; ByteCount sizes[maxAttributeCount + 1]; ATSUAttributeValuePtr values[maxAttributeCount + 1]; int attributeCount = 0; Fixed size = FixRatio(emSquare, 1); tags[attributeCount] = kATSUSizeTag; sizes[attributeCount] = sizeof(size); values[attributeCount] = &size; ++attributeCount; Q_ASSERT(attributeCount < maxAttributeCount + 1); OSStatus err = ATSUSetAttributes(unscaledStyle, attributeCount, tags, sizes, values); Q_ASSERT(err == noErr); Q_UNUSED(err); GlyphID atsuGlyph = glyph; ATSGlyphScreenMetrics atsuMetrics; ATSUGlyphGetScreenMetrics(unscaledStyle, 1, &atsuGlyph, 0, /* iForcingAntiAlias =*/ false, /* iAntiAliasSwitch =*/true, &atsuMetrics); metrics->width = int(atsuMetrics.width); metrics->height = int(atsuMetrics.height); metrics->x = QFixed::fromReal(atsuMetrics.topLeft.x); metrics->y = -QFixed::fromReal(atsuMetrics.topLeft.y); metrics->xoff = QFixed::fromReal(atsuMetrics.deviceAdvance.x); metrics->yoff = QFixed::fromReal(atsuMetrics.deviceAdvance.y); QFixedPoint p; ::addGlyphsToPath(unscaledStyle, &glyph, &p, 1, path); ATSUDisposeStyle(unscaledStyle); }
void PsychSetATSUStyleAttributesFromPsychWindowRecord(ATSUStyle atsuStyle, PsychWindowRecordType *winRec) { PsychFontStructType *psychFontRecord; int psychColorSize; //for ATSU attributes ATSUFontID atsuFontID; Fixed atsuFontSize; ATSURGBAlphaColor atsuFontColor; ATSStyleRenderingOptions atsuRenderOptions; GLdouble colorVector[4]; OSStatus callError; ATSUAttributeTag aaTags[] = {kATSUFontTag, kATSUSizeTag, kATSURGBAlphaColorTag, kATSUStyleRenderingOptionsTag }; ByteCount aaSizes[] = {sizeof(ATSUFontID), sizeof(Fixed), sizeof(ATSURGBAlphaColor), sizeof(ATSStyleRenderingOptions) }; ATSUAttributeValuePtr aaValue[] = {&atsuFontID, &atsuFontSize, &atsuFontColor, &atsuRenderOptions}; //set the font index PsychGetFontRecordFromFontNumber(winRec->textAttributes.textFontNumber, &psychFontRecord); if(psychFontRecord==NULL) PsychErrorExitMsg(PsychError_internal, "Failed to lookup the font from the font number"); atsuFontID=psychFontRecord->fontFMRef; //set the font size atsuFontSize=Long2Fix((long)(winRec->textAttributes.textSize)); //set the color PsychCoerceColorMode(&(winRec->textAttributes.textColor)); PsychConvertColorToDoubleVector(&(winRec->textAttributes.textColor), winRec, colorVector); atsuFontColor.red=(float)colorVector[0]; atsuFontColor.green=(float)colorVector[1]; atsuFontColor.blue=(float)colorVector[2]; atsuFontColor.alpha=(float)colorVector[3]; // Set anti-aliasing mode (on/off): Default is to leave it up to the system to decide when // to anti-alias and when not. But this flag allows to always force anti-aliasin on or off: atsuRenderOptions = kATSStyleNoOptions; if(PsychPrefStateGet_TextAntiAliasing()==0) atsuRenderOptions = kATSStyleNoAntiAliasing; if(PsychPrefStateGet_TextAntiAliasing()> 0) atsuRenderOptions = kATSStyleApplyAntiAliasing; //assign attributes to the style object callError=ATSUSetAttributes(atsuStyle, 4, aaTags, aaSizes, aaValue); }
OSStatus CreateATSUIStyleFromFontFullNameAndSize (const char *fontName, int fontSize, ATSUStyle *theStyle) { OSStatus err; ATSUStyle localStyle; ATSUFontID atsuFont; Fixed atsuSize; ATSUAttributeTag theTags[] = { kATSUFontTag, kATSUSizeTag }; ByteCount theSizes[] = { sizeof(ATSUFontID), sizeof(Fixed) }; ATSUAttributeValuePtr theValues[] = { NULL, NULL }; atsuFont = 0; atsuSize = FixRatio(fontSize, 1); localStyle = NULL; theValues[0] = &atsuFont; theValues[1] = &atsuSize; err = ATSUFindFontFromName(fontName, strlen(fontName), kFontFullName, kFontNoPlatform, kFontNoScript, kFontNoLanguage, &atsuFont); if (err == noErr) { err = ATSUCreateStyle(&localStyle); if (err == noErr) { err = ATSUSetAttributes(localStyle, sizeof(theTags) / sizeof(theTags[0]), theTags, theSizes, theValues); if (err == noErr) { *theStyle = localStyle; return (noErr); } } } if (localStyle != NULL) err = ATSUDisposeStyle(localStyle); return (err); }
// This example is almost identical to the helloworld example, except that // in this case, there are two styles instead of just one. ATSUSetRunStyle // is used to apply a style to different parts of the text. // void DrawMultipleStylesContents(WindowRef window) { CFStringRef string; UniChar *text; UniCharCount length; UniCharArrayOffset currentStart, currentEnd; ATSUStyle style1, style2; ATSUTextLayout layout; ATSUFontID font; Fixed pointSize; ATSUAttributeTag tags[2]; ByteCount sizes[2]; ATSUAttributeValuePtr values[2]; Fixed lineWidth, ascent, descent; CGContextRef cgContext; float x, y, cgY, windowHeight; ItemCount numSoftBreaks; UniCharArrayOffset *theSoftBreaks; int i; GrafPtr port, savedPort; Rect portBounds; // Set up the graphics port port = GetWindowPort(window); GetPort(&savedPort); SetPort(port); GetPortBounds(port, &portBounds); EraseRect(&portBounds); // Create a style object. This is one of two objects necessary to draw using ATSUI. // (The layout is the other.) verify_noerr( ATSUCreateStyle(&style1) ); // Look up the font we are going to use, and set it in the style object, using // the aforementioned "triple" (tag, size, value) semantics. This is how almost // all settings in ATSUI are applied. verify_noerr( ATSUFindFontFromName(kMultipleStylesFontName, strlen(kMultipleStylesFontName), kFontFullName, kFontNoPlatform, kFontNoScript, kFontNoLanguage, &font) ); tags[0] = kATSUFontTag; sizes[0] = sizeof(ATSUFontID); values[0] = &font; verify_noerr( ATSUSetAttributes(style1, 1, tags, sizes, values) ); // Set the point size, also using a triple. You can actually set multiple triples at once, // since the tag, size, and value parameters are arrays. Other examples do this, such as // the vertical text example. // pointSize = Long2Fix(kMultipleStylesFontSize); tags[0] = kATSUSizeTag; sizes[0] = sizeof(Fixed); values[0] = &pointSize; verify_noerr( ATSUSetAttributes(style1, 1, tags, sizes, values) ); // Now we create the second of two objects necessary to draw text using ATSUI, the layout. // You can specify a pointer to the text buffer at layout creation time, or later using // the routine ATSUSetTextPointerLocation(). Below, we do it after layout creation time. verify_noerr( ATSUCreateTextLayout(&layout) ); // Before assigning text to the layout, we must first convert the string we plan to draw // from a CFStringRef into an array of UniChar. string = CFStringCreateWithCString(NULL, "In this example, various parts of the text have different styles applied. The same style is used more than once.", kCFStringEncodingASCII); // Extract the raw Unicode from the CFString, then dispose of the CFString length = CFStringGetLength(string); text = (UniChar *)malloc(length * sizeof(UniChar)); CFStringGetCharacters(string, CFRangeMake(0, length), text); CFRelease(string); // Attach the resulting UTF-16 Unicode text to the layout verify_noerr( ATSUSetTextPointerLocation(layout, text, kATSUFromTextBeginning, kATSUToTextEnd, length) ); // Now we tie the two necessary objects, the layout and the style, together verify_noerr( ATSUSetRunStyle(layout, style1, kATSUFromTextBeginning, kATSUToTextEnd) ); // Now, for this example we create a second style, and assign it to various runs within // the text. For our example, the run offsets are hard-coded for simplicity's sake. In // a real application, style runs are often assigned from external sources, such as user // selection. verify_noerr( ATSUCreateAndCopyStyle(style1, &style2) ); // Change the font for the second style verify_noerr( ATSUFindFontFromName(kMultipleStylesFontName2, strlen(kMultipleStylesFontName2), kFontFullName, kFontNoPlatform, kFontNoScript, kFontNoLanguage, &font) ); tags[0] = kATSUFontTag; sizes[0] = sizeof(ATSUFontID); values[0] = &font; verify_noerr( ATSUSetAttributes(style2, 1, tags, sizes, values) ); // Apply the new style to the text in various places verify_noerr( ATSUSetRunStyle(layout, style2, 8, 7) ); // The word "example" verify_noerr( ATSUSetRunStyle(layout, style2, 65, 7) ); // The word "applied" verify_noerr( ATSUSetRunStyle(layout, style2, 83, 5) ); // The word "style" verify_noerr( ATSUSetRunStyle(layout, style2, 107, 4) ); // The word "once" // In this example, we are breaking text into lines. // Therefore, we need to know the width of the line. lineWidth = X2Fix(portBounds.right - portBounds.left - 2.0*kMultipleStylesMargin); tags[0] = kATSULineWidthTag; sizes[0] = sizeof(Fixed); values[0] = &lineWidth; verify_noerr( ATSUSetLayoutControls(layout, 1, tags, sizes, values) ); // Prepare the CGContext for drawing QDBeginCGContext(port, &cgContext); tags[0] = kATSUCGContextTag; sizes[0] = sizeof(CGContextRef); values[0] = &cgContext; verify_noerr( ATSUSetLayoutControls(layout, 1, tags, sizes, values) ); // Prepare the coordinates for drawing. In our example, "x" and "y" are the coordinates // in QD space. "cgY" contains the y coordinate in CG space. // windowHeight = portBounds.bottom - portBounds.top; x = kMultipleStylesMargin; // leave a small left margin y = kMultipleStylesMargin; // leave a small top margin cgY = windowHeight - y; // Subtract the y coordinate from the height of the // window to get the coordinate in CG-aware space. // Break the text into lines verify_noerr( ATSUBatchBreakLines(layout, kATSUFromTextBeginning, length, lineWidth, &numSoftBreaks) ); verify_noerr( ATSUGetSoftLineBreaks(layout, kATSUFromTextBeginning, kATSUToTextEnd, 0, NULL, &numSoftBreaks) ); theSoftBreaks = (UniCharArrayOffset *) malloc(numSoftBreaks * sizeof(UniCharArrayOffset)); verify_noerr( ATSUGetSoftLineBreaks(layout, kATSUFromTextBeginning, kATSUToTextEnd, numSoftBreaks, theSoftBreaks, &numSoftBreaks) ); // Loop over all the lines and draw them currentStart = 0; for (i=0; i <= numSoftBreaks; i++) { currentEnd = ((numSoftBreaks > 0 ) && (numSoftBreaks > i)) ? theSoftBreaks[i] : length; // This is the height of a line, the ascent and descent. Getting the values this way is the preferred method. ATSUGetLineControl(layout, currentStart, kATSULineAscentTag, sizeof(ATSUTextMeasurement), &ascent, NULL); ATSUGetLineControl(layout, currentStart, kATSULineDescentTag, sizeof(ATSUTextMeasurement), &descent, NULL); // Make room for the area above the baseline y += Fix2X(ascent); cgY = windowHeight - y; // Draw the text verify_noerr( ATSUDrawText(layout, currentStart, currentEnd - currentStart, X2Fix(x), X2Fix(cgY)) ); // Make room for the area beloww the baseline y += Fix2X(descent); // Prepare for next line currentStart = currentEnd; } // This is a one-shot window, so we are now ready to dispose of all our objects. // Normally, we would want to keep everything around in case we needed to redraw or change // the text at some point. // Tear down the CGContext CGContextFlush(cgContext); QDEndCGContext(port, &cgContext); // Deallocate string storage free(text); // Layout and styles also need to be disposed verify_noerr( ATSUDisposeStyle(style1) ); verify_noerr( ATSUDisposeStyle(style2) ); verify_noerr( ATSUDisposeTextLayout(layout) ); // Restore the graphics port SetPort(savedPort); }
//------------------------------------------------------------------------------ bool MacCarbFont::create( const char* name, U32 size, U32 charset) { String nameStr = name; nameStr = nameStr.trim(); // create and cache the style and layout. // based on apple sample code at http://developer.apple.com/qa/qa2001/qa1027.html // note: charset is ignored on mac. -- we don't need it to get the right chars. // But do we need it to translate encodings? hmm... CFStringRef cfsName; ATSUFontID atsuFontID; ATSFontRef atsFontRef; Fixed atsuSize; ATSURGBAlphaColor black; ATSFontMetrics fontMetrics; U32 scaledSize; bool isBold = false; bool isItalic = false; bool haveModifier; do { haveModifier = false; if( nameStr.compare( "Bold", 4, String::NoCase | String::Right ) == 0 ) { isBold = true; nameStr = nameStr.substr( 0, nameStr.length() - 4 ).trim(); haveModifier = true; } if( nameStr.compare( "Italic", 6, String::NoCase | String::Right ) == 0 ) { isItalic = true; nameStr = nameStr.substr( 0, nameStr.length() - 6 ).trim(); haveModifier = true; } } while( haveModifier ); // Look up the font. We need it in 2 differnt formats, for differnt Apple APIs. cfsName = CFStringCreateWithCString( kCFAllocatorDefault, nameStr.c_str(), kCFStringEncodingUTF8); if(!cfsName) Con::errorf("Error: could not make a cfstring out of \"%s\" ",nameStr.c_str()); atsFontRef = ATSFontFindFromName( cfsName, kATSOptionFlagsDefault); atsuFontID = FMGetFontFromATSFontRef( atsFontRef); // make sure we found it. ATSFontFindFromName() appears to return 0 if it cant find anything. Apple docs contain no info on error returns. if( !atsFontRef || !atsuFontID ) { Con::errorf("MacCarbFont::create - could not load font -%s-",name); return false; } // adjust the size. win dpi = 96, mac dpi = 72. 72/96 = .75 // Interestingly enough, 0.75 is not what makes things the right size. scaledSize = size - 2 - (int)((float)size * 0.1); mSize = scaledSize; // Set up the size and color. We send these to ATSUSetAttributes(). atsuSize = IntToFixed(scaledSize); black.red = black.green = black.blue = black.alpha = 1.0; // Three parrallel arrays for setting up font, size, and color attributes. ATSUAttributeTag theTags[] = { kATSUFontTag, kATSUSizeTag, kATSURGBAlphaColorTag}; ByteCount theSizes[] = { sizeof(ATSUFontID), sizeof(Fixed), sizeof(ATSURGBAlphaColor) }; ATSUAttributeValuePtr theValues[] = { &atsuFontID, &atsuSize, &black }; // create and configure the style object. ATSUCreateStyle(&mStyle); ATSUSetAttributes( mStyle, 3, theTags, theSizes, theValues ); if( isBold ) { ATSUAttributeTag tag = kATSUQDBoldfaceTag; ByteCount size = sizeof( Boolean ); Boolean value = true; ATSUAttributeValuePtr valuePtr = &value; ATSUSetAttributes( mStyle, 1, &tag, &size, &valuePtr ); } if( isItalic ) { ATSUAttributeTag tag = kATSUQDItalicTag; ByteCount size = sizeof( Boolean ); Boolean value = true; ATSUAttributeValuePtr valuePtr = &value; ATSUSetAttributes( mStyle, 1, &tag, &size, &valuePtr ); } // create the layout object, ATSUCreateTextLayout(&mLayout); // we'll bind the layout to a bitmap context when we actually draw. // ATSUSetTextPointerLocation() - will set the text buffer // ATSUSetLayoutControls() - will set the cg context. // get font metrics, save our baseline and height ATSFontGetHorizontalMetrics(atsFontRef, kATSOptionFlagsDefault, &fontMetrics); mBaseline = scaledSize * fontMetrics.ascent; mHeight = scaledSize * ( fontMetrics.ascent - fontMetrics.descent + fontMetrics.leading ) + 1; // cache our grey color space, so we dont have to re create it every time. mColorSpace = CGColorSpaceCreateDeviceGray(); // and finally cache the font's name. We use this to cheat some antialiasing options below. mName = StringTable->insert(name); return true; }
void QFontDatabase::load(const QFontPrivate *d, int script) { // sanity checks if(!qApp) qWarning("QFont: Must construct a QApplication before a QFont"); Q_ASSERT(script >= 0 && script < QUnicodeTables::ScriptCount); Q_UNUSED(script); QFontDef req = d->request; req.pixelSize = qt_mac_pixelsize(req, d->dpi); // set the point size to 0 to get better caching req.pointSize = 0; QFontCache::Key key = QFontCache::Key(req, QUnicodeTables::Common, d->screen); if(!(d->engineData = QFontCache::instance()->findEngineData(key))) { d->engineData = new QFontEngineData; QFontCache::instance()->insertEngineData(key, d->engineData); } else { d->engineData->ref.ref(); } if(d->engineData->engine) // already loaded return; // set it to the actual pointsize, so QFontInfo will do the right thing req.pointSize = qRound(qt_mac_pointsize(d->request, d->dpi)); QFontEngine *e = QFontCache::instance()->findEngine(key); if(!e && qt_enable_test_font && req.family == QLatin1String("__Qt__Box__Engine__")) { e = new QTestFontEngine(req.pixelSize); e->fontDef = req; } if(e) { e->ref.ref(); d->engineData->engine = e; return; // the font info and fontdef should already be filled } //find the font QStringList family_list = familyList(req); const char *stylehint = styleHint(req); if (stylehint) family_list << QLatin1String(stylehint); // add QFont::defaultFamily() to the list, for compatibility with // previous versions family_list << QApplication::font().defaultFamily(); ATSFontFamilyRef familyRef = 0; ATSFontRef fontRef = 0; QMutexLocker locker(fontDatabaseMutex()); QFontDatabasePrivate *db = privateDb(); if (!db->count) initializeDb(); for(int i = 0; i < family_list.size(); ++i) { for (int k = 0; k < db->count; ++k) { if (db->families[k]->name.compare(family_list.at(i), Qt::CaseInsensitive) == 0) { QByteArray family_name = db->families[k]->name.toUtf8(); familyRef = ATSFontFamilyFindFromName(QCFString(db->families[k]->name), kATSOptionFlagsDefault); if (familyRef) { fontRef = ATSFontFindFromName(QCFString(db->families[k]->name), kATSOptionFlagsDefault); goto FamilyFound; } else { #if defined(QT_MAC_USE_COCOA) // ATS and CT disagrees on what the family name should be, // use CT to look up the font if ATS fails. QCFString familyName = QString::fromAscii(family_name); QCFType<CTFontRef> CTfontRef = CTFontCreateWithName(familyName, 12, NULL); QCFType<CTFontDescriptorRef> fontDescriptor = CTFontCopyFontDescriptor(CTfontRef); QCFString displayName = (CFStringRef)CTFontDescriptorCopyAttribute(fontDescriptor, kCTFontDisplayNameAttribute); familyRef = ATSFontFamilyFindFromName(displayName, kATSOptionFlagsDefault); if (familyRef) { fontRef = ATSFontFindFromName(displayName, kATSOptionFlagsDefault); goto FamilyFound; } #endif } } } } FamilyFound: //fill in the engine's font definition QFontDef fontDef = d->request; //copy.. if(fontDef.pointSize < 0) fontDef.pointSize = qt_mac_pointsize(fontDef, d->dpi); else fontDef.pixelSize = qt_mac_pixelsize(fontDef, d->dpi); #if 0 ItemCount name_count; if(ATSUCountFontNames(fontID, &name_count) == noErr && name_count) { ItemCount actualName_size; if(ATSUGetIndFontName(fontID, 0, 0, 0, &actualName_size, 0, 0, 0, 0) == noErr && actualName_size) { QByteArray actualName(actualName_size); if(ATSUGetIndFontName(fontID, 0, actualName_size, actualName.data(), &actualName_size, 0, 0, 0, 0) == noErr && actualName_size) fontDef.family = QString::fromUtf8(actualName); } } #else { QCFString actualName; if(ATSFontFamilyGetName(familyRef, kATSOptionFlagsDefault, &actualName) == noErr) fontDef.family = actualName; } #endif #ifdef QT_MAC_USE_COCOA QFontEngine *engine = new QCoreTextFontEngineMulti(familyRef, fontRef, fontDef, d->kerning); #elif 1 QFontEngine *engine = new QFontEngineMacMulti(familyRef, fontRef, fontDef, d->kerning); #else ATSFontFamilyRef atsFamily = familyRef; ATSFontFamilyRef atsFontRef = fontRef; FMFont fontID; FMFontFamily fmFamily; FMFontStyle fntStyle = 0; fmFamily = FMGetFontFamilyFromATSFontFamilyRef(atsFamily); if (fmFamily == kInvalidFontFamily) { // Use the ATSFont then... fontID = FMGetFontFromATSFontRef(atsFontRef); } else { if (fontDef.weight >= QFont::Bold) fntStyle |= ::bold; if (fontDef.style != QFont::StyleNormal) fntStyle |= ::italic; FMFontStyle intrinsicStyle; FMFont fnt = 0; if (FMGetFontFromFontFamilyInstance(fmFamily, fntStyle, &fnt, &intrinsicStyle) == noErr) fontID = FMGetATSFontRefFromFont(fnt); } OSStatus status; const int maxAttributeCount = 5; ATSUAttributeTag tags[maxAttributeCount + 1]; ByteCount sizes[maxAttributeCount + 1]; ATSUAttributeValuePtr values[maxAttributeCount + 1]; int attributeCount = 0; Fixed size = FixRatio(fontDef.pixelSize, 1); tags[attributeCount] = kATSUSizeTag; sizes[attributeCount] = sizeof(size); values[attributeCount] = &size; ++attributeCount; tags[attributeCount] = kATSUFontTag; sizes[attributeCount] = sizeof(fontID); values[attributeCount] = &fontID; ++attributeCount; CGAffineTransform transform = CGAffineTransformIdentity; if (fontDef.stretch != 100) { transform = CGAffineTransformMakeScale(float(fontDef.stretch) / float(100), 1); tags[attributeCount] = kATSUFontMatrixTag; sizes[attributeCount] = sizeof(transform); values[attributeCount] = &transform; ++attributeCount; } ATSUStyle style; status = ATSUCreateStyle(&style); Q_ASSERT(status == noErr); Q_ASSERT(attributeCount < maxAttributeCount + 1); status = ATSUSetAttributes(style, attributeCount, tags, sizes, values); Q_ASSERT(status == noErr); QFontEngine *engine = new QFontEngineMac(style, fontID, fontDef, /*multiEngine*/ 0); ATSUDisposeStyle(style); #endif d->engineData->engine = engine; engine->ref.ref(); //a ref for the engineData->engine QFontCache::instance()->insertEngine(key, engine); }
void* loadAATfont(ATSFontRef fontRef, long scaled_size, const char* cp1) { ATSUFontID fontID = FMGetFontFromATSFontRef(fontRef); ATSUStyle style = 0; OSStatus status = ATSUCreateStyle(&style); float extend = 1.0; float slant = 0.0; float embolden = 0.0; float letterspace = 0.0; int i; if (status == noErr) { UInt32 rgbValue; Fixed tracking = 0x80000000; Fixed atsuSize = FixedTeXtoPSPoints(scaled_size); ATSStyleRenderingOptions options = kATSStyleNoHinting; Fract hangInhibit = fract1; ATSUAttributeTag tags[] = { kATSUFontTag, kATSUSizeTag, kATSUStyleRenderingOptionsTag, kATSUHangingInhibitFactorTag }; ByteCount sizes[] = { sizeof(ATSUFontID), sizeof(Fixed), sizeof(ATSStyleRenderingOptions), sizeof(Fract) }; ATSUAttributeValuePtr attrs[] = { &fontID, &atsuSize, &options, &hangInhibit }; ATSUSetAttributes(style, sizeof(tags) / sizeof(ATSUAttributeTag), tags, sizes, attrs); #define FEAT_ALLOC_CHUNK 8 #define VAR_ALLOC_CHUNK 4 if (cp1 != NULL) { int allocFeats = FEAT_ALLOC_CHUNK; UInt16* featureTypes = (UInt16*)xmalloc(allocFeats * sizeof(UInt16)); UInt16* selectorValues = (UInt16*)xmalloc(allocFeats * sizeof(UInt16)); int numFeatures = 0; int allocVars = VAR_ALLOC_CHUNK; UInt32* axes = (UInt32*)xmalloc(allocVars * sizeof(UInt32)); SInt32* values = (SInt32*)xmalloc(allocVars * sizeof(SInt32)); int numVariations = 0; // interpret features & variations following ":" while (*cp1) { // locate beginning of name=value pair if (*cp1 == ':' || *cp1 == ';') // skip over separator ++cp1; while (*cp1 == ' ' || *cp1 == '\t') // skip leading whitespace ++cp1; if (*cp1 == 0) // break if end of string break; // scan to end of pair const char* cp2 = cp1; while (*cp2 && *cp2 != ';' && *cp2 != ':') ++cp2; // look for the '=' separator const char* cp3 = cp1; while (cp3 < cp2 && *cp3 != '=') ++cp3; if (cp3 == cp2) goto bad_option; // now cp1 points to option name, cp3 to '=', cp2 to ';' or null // first try for a feature by this name ATSUFontFeatureType featureType; featureType = find_feature_by_name(fontID, cp1, cp3 - cp1); if (featureType != 0x0000FFFF) { // look past the '=' separator for setting names int featLen = cp3 - cp1; ++cp3; while (cp3 < cp2) { // skip leading whitespace while (*cp3 == ' ' || *cp3 == '\t') ++cp3; // possibly multiple settings... int disable = 0; if (*cp3 == '!') { // check for negation disable = 1; ++cp3; } // scan for end of setting name const char* cp4 = cp3; while (cp4 < cp2 && *cp4 != ',') ++cp4; // now cp3 points to name, cp4 to ',' or ';' or null ATSUFontFeatureSelector selectorValue = find_selector_by_name(fontID, featureType, cp3, cp4 - cp3); if (selectorValue != 0x0000FFFF) { if (numFeatures == allocFeats) { allocFeats += FEAT_ALLOC_CHUNK; featureTypes = xrealloc(featureTypes, allocFeats * sizeof(UInt16)); selectorValues = xrealloc(selectorValues, allocFeats * sizeof(UInt16)); } featureTypes[numFeatures] = featureType; selectorValues[numFeatures] = selectorValue + disable; ++numFeatures; } else { fontfeaturewarning(cp1, featLen, cp3, cp4 - cp3); } // point beyond setting name terminator cp3 = cp4 + 1; } goto next_option; } // try to find a variation by this name ATSUFontVariationAxis axis; axis = find_axis_by_name(fontID, cp1, cp3 - cp1); if (axis != 0) { // look past the '=' separator for the value ++cp3; double value = 0.0, decimal = 1.0; bool negate = false; if (*cp3 == '-') { ++cp3; negate = true; } while (cp3 < cp2) { int v = *cp3 - '0'; if (v >= 0 && v <= 9) { if (decimal != 1.0) { value += v / decimal; decimal *= 10.0; } else value = value * 10.0 + v; } else if (*cp3 == '.') { if (decimal != 1.0) break; decimal = 10.0; } else break; ++cp3; } if (negate) value = -value; if (numVariations == allocVars) { allocVars += VAR_ALLOC_CHUNK; axes = xrealloc(axes, allocVars * sizeof(UInt32)); values = xrealloc(values, allocVars * sizeof(SInt32)); } axes[numVariations] = axis; values[numVariations] = value * 65536.0; // X2Fix(value); ++numVariations; goto next_option; } // didn't find feature or variation, try other options.... i = readCommonFeatures(cp1, cp2, &extend, &slant, &embolden, &letterspace, &rgbValue); if (i == 1) goto next_option; else if (i == -1) goto bad_option; if (strncmp(cp1, "tracking", 8) == 0) { cp3 = cp1 + 8; if (*cp3 != '=') goto bad_option; ++cp3; double val = read_double(&cp3); tracking = X2Fix(val); goto next_option; } bad_option: // not a name=value pair, or not recognized.... // check for plain "vertical" before complaining if (strncmp(cp1, "vertical", 8) == 0) { cp3 = cp2; if (*cp3 == ';' || *cp3 == ':') --cp3; while (*cp3 == '\0' || *cp3 == ' ' || *cp3 == '\t') --cp3; if (*cp3) ++cp3; if (cp3 == cp1 + 8) { ATSUVerticalCharacterType vert = kATSUStronglyVertical; tags[0] = kATSUVerticalCharacterTag; sizes[0] = sizeof(ATSUVerticalCharacterType); attrs[0] = | ATSUSetAttributes(style, 1, tags, sizes, attrs); goto next_option; } } fontfeaturewarning(cp1, cp2 - cp1, 0, 0); next_option: // go to next name=value pair cp1 = cp2; } if (numFeatures > 0) ATSUSetFontFeatures(style, numFeatures, featureTypes, selectorValues); if (numVariations > 0) ATSUSetVariations(style, numVariations, axes, values); if ((loadedfontflags & FONT_FLAGS_COLORED) != 0) { ATSURGBAlphaColor rgba; rgba.red = ((rgbValue & 0xFF000000) >> 24) / 255.0; rgba.green = ((rgbValue & 0x00FF0000) >> 16) / 255.0; rgba.blue = ((rgbValue & 0x0000FF00) >> 8 ) / 255.0; rgba.alpha = ((rgbValue & 0x000000FF) ) / 255.0; tags[0] = kATSURGBAlphaColorTag; sizes[0] = sizeof(ATSURGBAlphaColor); attrs[0] = &rgba; ATSUSetAttributes(style, 1, tags, sizes, attrs); }
bool SetFallbackFont(FreeTypeSettings *settings, const char *language_isocode, int winlangid, MissingGlyphSearcher *callback) { const char *str; bool result = false; callback->FindMissingGlyphs(&str); #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5) if (MacOSVersionIsAtLeast(10, 5, 0)) { /* Determine fallback font using CoreText. This uses the language isocode * to find a suitable font. CoreText is available from 10.5 onwards. */ char lang[16]; if (strcmp(language_isocode, "zh_TW") == 0) { /* Traditional Chinese */ strecpy(lang, "zh-Hant", lastof(lang)); } else if (strcmp(language_isocode, "zh_CN") == 0) { /* Simplified Chinese */ strecpy(lang, "zh-Hans", lastof(lang)); } else if (strncmp(language_isocode, "ur", 2) == 0) { /* The urdu alphabet is variant of persian. As OS X has no default * font that advertises an urdu language code, search for persian * support instead. */ strecpy(lang, "fa", lastof(lang)); } else { /* Just copy the first part of the isocode. */ strecpy(lang, language_isocode, lastof(lang)); char *sep = strchr(lang, '_'); if (sep != NULL) *sep = '\0'; } CFStringRef lang_code; lang_code = CFStringCreateWithCString(kCFAllocatorDefault, lang, kCFStringEncodingUTF8); /* Create a font iterator and iterate over all fonts that * are available to the application. */ ATSFontIterator itr; ATSFontRef font; ATSFontIteratorCreate(kATSFontContextLocal, NULL, NULL, kATSOptionFlagsUnRestrictedScope, &itr); while (!result && ATSFontIteratorNext(itr, &font) == noErr) { /* Get CoreText font handle. */ CTFontRef font_ref = CTFontCreateWithPlatformFont(font, 0.0, NULL, NULL); CFArrayRef langs = CTFontCopySupportedLanguages(font_ref); if (langs != NULL) { /* Font has a list of supported languages. */ for (CFIndex i = 0; i < CFArrayGetCount(langs); i++) { CFStringRef lang = (CFStringRef)CFArrayGetValueAtIndex(langs, i); if (CFStringCompare(lang, lang_code, kCFCompareAnchored) == kCFCompareEqualTo) { /* Lang code is supported by font, get full font name. */ CFStringRef font_name = CTFontCopyFullName(font_ref); char name[128]; CFStringGetCString(font_name, name, lengthof(name), kCFStringEncodingUTF8); CFRelease(font_name); /* Skip some inappropriate or ugly looking fonts that have better alternatives. */ if (strncmp(name, "Courier", 7) == 0 || strncmp(name, "Apple Symbols", 13) == 0 || strncmp(name, ".Aqua", 5) == 0 || strncmp(name, "LastResort", 10) == 0 || strncmp(name, "GB18030 Bitmap", 14) == 0) continue; /* Save result. */ callback->SetFontNames(settings, name); DEBUG(freetype, 2, "CT-Font for %s: %s", language_isocode, name); result = true; break; } } CFRelease(langs); } CFRelease(font_ref); } ATSFontIteratorRelease(&itr); CFRelease(lang_code); } else #endif { #if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5) && !__LP64__ /* Determine fallback font using ATSUI. This uses a string sample with * missing characters. This is not failure-proof, but a better way like * using the isocode as in the CoreText code path is not available. * ATSUI was deprecated with 10.6 and is only partially available in * 64-bit mode. */ /* Remove all control characters in the range from SCC_CONTROL_START to * SCC_CONTROL_END as well as all ASCII < 0x20 from the string as it will * mess with the automatic font detection */ char buff[256]; // This length is enough to find a suitable replacement font strecpy(buff, str, lastof(buff)); str_validate(buff, lastof(buff), SVS_ALLOW_NEWLINE); /* Extract a UniChar representation of the sample string. */ CFStringRef cf_str = CFStringCreateWithCString(kCFAllocatorDefault, buff, kCFStringEncodingUTF8); if (cf_str == NULL) { /* Something went wrong. Corrupt/invalid sample string? */ return false; } CFIndex str_len = CFStringGetLength(cf_str); UniChar string[str_len]; CFStringGetCharacters(cf_str, CFRangeMake(0, str_len), string); /* Create a default text style with the default font. */ ATSUStyle style; ATSUCreateStyle(&style); /* Create a text layout object from the sample string using the text style. */ UniCharCount run_len = kATSUToTextEnd; ATSUTextLayout text_layout; ATSUCreateTextLayoutWithTextPtr(string, kATSUFromTextBeginning, kATSUToTextEnd, str_len, 1, &run_len, &style, &text_layout); /* Try to match a font for the sample text. ATSUMatchFontsToText stops after * it finds the first continuous character run not renderable with the currently * selected font starting at offset. The matching needs to be repeated until * the end of the string is reached to make sure the fallback font matches for * all characters in the string and not only the first run. */ UniCharArrayOffset offset = kATSUFromTextBeginning; OSStatus os_err; do { ATSUFontID font; UniCharCount run_len; os_err = ATSUMatchFontsToText(text_layout, offset, kATSUToTextEnd, &font, &offset, &run_len); if (os_err == kATSUFontsMatched) { /* Found a better fallback font. Update the text layout * object with the new font. */ ATSUAttributeTag tag = kATSUFontTag; ByteCount size = sizeof(font); ATSUAttributeValuePtr val = &font; ATSUSetAttributes(style, 1, &tag, &size, &val); offset += run_len; } /* Exit if the end of the string is reached or some other error occurred. */ } while (os_err == kATSUFontsMatched && offset < (UniCharArrayOffset)str_len); if (os_err == noErr || os_err == kATSUFontsMatched) { /* ATSUMatchFontsToText exited normally. Extract font * out of the text layout object. */ ATSUFontID font; ByteCount act_len; ATSUGetAttribute(style, kATSUFontTag, sizeof(font), &font, &act_len); /* Get unique font name. The result is not a c-string, we have * to leave space for a \0 and terminate it ourselves. */ char name[128]; ATSUFindFontName(font, kFontUniqueName, kFontNoPlatformCode, kFontNoScriptCode, kFontNoLanguageCode, 127, name, &act_len, NULL); name[act_len > 127 ? 127 : act_len] = '\0'; /* Save Result. */ callback->SetFontNames(settings, name); DEBUG(freetype, 2, "ATSUI-Font for %s: %s", language_isocode, name); result = true; } ATSUDisposeTextLayout(text_layout); ATSUDisposeStyle(style); CFRelease(cf_str); #endif } if (result && strncmp(settings->medium.font, "Geeza Pro", 9) == 0) { /* The font 'Geeza Pro' is often found for arabic characters, but * it has the 'tiny' problem of not having any latin characters. * 'Arial Unicode MS' on the other hand has arabic and latin glyphs, * but seems to 'forget' to inform the OS about this fact. Manually * substitute the latter for the former if it is loadable. */ bool ft_init = _library != NULL; FT_Face face; /* Init FreeType if needed. */ if ((ft_init || FT_Init_FreeType(&_library) == FT_Err_Ok) && GetFontByFaceName("Arial Unicode MS", &face) == FT_Err_Ok) { FT_Done_Face(face); callback->SetFontNames(settings, "Arial Unicode MS"); DEBUG(freetype, 1, "Replacing font 'Geeza Pro' with 'Arial Unicode MS'"); } if (!ft_init) { /* Uninit FreeType if we did the init. */ FT_Done_FreeType(_library); _library = NULL; } } callback->FindMissingGlyphs(NULL); return result; }
TextWriter::TextWriter(int in_x, int in_y, Renderer &in_renderer, bool in_centered, int in_size, std::wstring fontname) : x(in_x), y(in_y), size(in_size), original_x(0), last_line_height(0), centered(in_centered), renderer(in_renderer) { x += renderer.m_xoffset; original_x = x; y += renderer.m_yoffset; #ifdef WIN32 Context c = renderer.m_context; point_size = MulDiv(size, GetDeviceCaps(c, LOGPIXELSY), 72); HFONT font = 0; if (font_size_lookup[in_size] == 0) { // Set up the LOGFONT structure LOGFONT logical_font; logical_font.lfHeight = get_point_size(); logical_font.lfWidth = 0; logical_font.lfEscapement = 0; logical_font.lfOrientation = 0; logical_font.lfWeight = FW_NORMAL; logical_font.lfItalic = false; logical_font.lfUnderline = false; logical_font.lfStrikeOut = false; logical_font.lfCharSet = ANSI_CHARSET; logical_font.lfOutPrecision = OUT_DEFAULT_PRECIS; logical_font.lfClipPrecision = CLIP_DEFAULT_PRECIS; logical_font.lfQuality = PROOF_QUALITY; logical_font.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE; lstrcpy(logical_font.lfFaceName, fontname.c_str()); font = CreateFontIndirect(&logical_font); HFONT previous_font = (HFONT)SelectObject(c, font); wglUseFontBitmaps(c, 0, 128, next_call_list_start); font_size_lookup[in_size] = next_call_list_start; font_handle_lookup[in_size] = font; next_call_list_start += 130; SelectObject(c, previous_font); } #else // TODO: is this sufficient? point_size = size; if (font_size_lookup[size] == 0) { int list_start = glGenLists(128); // MACNOTE: Force Trebuchet MS. It's what we mostly use anyway, but // I want to be sure they have it. const CFStringRef font_name = CFSTR("Trebuchet MS"); ATSFontFamilyRef font = ATSFontFamilyFindFromName(font_name, kATSOptionFlagsDefault); if (!font) throw PianoGameError(WSTRING(L"Couldn't get ATSFontFamilyRef for font '" << WideFromMacString(font_name) << L"'.")); AGLContext context = aglGetCurrentContext(); if (!context) throw PianoGameError(L"Couldn't retrieve OpenGL context while creating font."); GLboolean ret = aglUseFont(context, font, normal, size, 0, 128, list_start); if (ret == GL_FALSE) throw PianoGameError(WSTRING(L"aglUseFont() call failed with error code: " << aglGetError())); font_size_lookup[size] = list_start; // Create the ATSU style object that we'll use for calculating text extents and store it for later. ATSUStyle style; OSStatus status = ATSUCreateStyle(&style); if (status != noErr) throw PianoGameError(WSTRING(L"Couldn't create ATSU style. Error code: " << static_cast<int>(status))); Fixed fixed_size = Long2Fix(size); ATSUAttributeTag tags[] = { kATSUSizeTag }; ByteCount sizes[] = { sizeof(Fixed) }; ATSUAttributeValuePtr values[] = { &fixed_size }; status = ATSUSetAttributes(style, sizeof(sizes) / sizeof(ByteCount), tags, sizes, values); if (status != noErr) throw PianoGameError(WSTRING(L"Couldn't set ATSU style attributes. Error code: " << static_cast<int>(status))); atsu_style_lookup[size] = style; } #endif }
static bool osx_prepare_text(const void *p_text, uindex_t p_length, const MCGFont &p_font) { OSStatus t_err; t_err = noErr; if (t_err == noErr) if (s_layout == NULL) t_err = ATSUCreateTextLayout(&s_layout); if (t_err == noErr) if (s_style == NULL) t_err = ATSUCreateStyle(&s_style); if (t_err == noErr) if (s_colour_space == NULL) s_colour_space = CGColorSpaceCreateWithName(kCGColorSpaceGenericGray); if (t_err == noErr) if (s_colour == NULL) { // Components are grey and alpha const float t_colour_components[] = {1.0, 1.0}; s_colour = CGColorCreate(s_colour_space, t_colour_components); } ATSUFontID t_font_id; Fixed t_font_size; Boolean t_font_is_italic; ATSUTextMeasurement t_imposed_width; if (t_err == noErr) { t_font_size = p_font . size << 16; // MM-2013-09-16: [[ Bug 11283 ]] It appears that ATSUI doesn't like italic being passed as a style parameter to ATSUFONDtoFontID. // Instead, set italic as an attribute tag. uint8_t t_style; t_style = p_font . style & ~italic; t_font_is_italic = p_font . style & italic; // MW-2013-12-05: [[ Bug 11535 ]] Set the imposed width to the fixed advance width // if non-zero. Otherwise use the glyph advance. if (p_font . fixed_advance != 0) t_imposed_width = p_font . fixed_advance << 16; else t_imposed_width = kATSUseGlyphAdvance; // if the specified font can't be found, just use the default // MM-2013-09-16: [[ Bug 11283 ]] Do the same for font styles - if the font/style paring cannot be found, try font with no style. t_err = ATSUFONDtoFontID((short)(intptr_t)p_font . fid, t_style, &t_font_id); if (t_err != noErr) t_err = ATSUFONDtoFontID((short)(intptr_t)p_font . fid, 0, &t_font_id); if (t_err != noErr) t_err = ATSUFONDtoFontID(0, t_style, &t_font_id); if (t_err != noErr) t_err = ATSUFONDtoFontID(0, 0, &t_font_id); } ATSUAttributeTag t_tags[] = { kATSUFontTag, kATSUSizeTag, kATSUQDItalicTag, kATSUImposeWidthTag, }; ByteCount t_sizes[] = { sizeof(ATSUFontID), sizeof(Fixed), sizeof(Boolean), sizeof(ATSUTextMeasurement), }; ATSUAttributeValuePtr t_attrs[] = { &t_font_id, &t_font_size, &t_font_is_italic, &t_imposed_width, }; if (t_err == noErr) t_err = ATSUSetAttributes(s_style, sizeof(t_tags) / sizeof(ATSUAttributeTag), t_tags, t_sizes, t_attrs); if (t_err == noErr) t_err = ATSUSetTextPointerLocation(s_layout, (const UniChar *) p_text, 0, p_length / 2, p_length / 2); if (t_err == noErr) t_err = ATSUSetRunStyle(s_layout, s_style, 0, p_length / 2); if (t_err == noErr) t_err = ATSUSetTransientFontMatching(s_layout, true); return t_err == noErr; }
static bool osx_draw_text_to_cgcontext_at_location(const void *p_text, uindex_t p_length, MCGPoint p_location, const MCGFont &p_font, CGContextRef p_cgcontext, MCGIntRectangle &r_bounds) { OSStatus t_err; t_err = noErr; ATSUFontID t_font_id; Fixed t_font_size; t_font_size = p_font . size << 16; ATSUAttributeTag t_tags[] = { kATSUFontTag, kATSUSizeTag, }; ByteCount t_sizes[] = { sizeof(ATSUFontID), sizeof(Fixed), }; ATSUAttributeValuePtr t_attrs[] = { &t_font_id, &t_font_size, }; ATSLineLayoutOptions t_layout_options; ATSUAttributeTag t_layout_tags[] = { kATSULineLayoutOptionsTag, kATSUCGContextTag, }; ByteCount t_layout_sizes[] = { sizeof(ATSLineLayoutOptions), sizeof(CGContextRef), }; ATSUAttributeValuePtr t_layout_attrs[] = { &t_layout_options, &p_cgcontext, }; if (t_err == noErr) { // if the specified fon't can't be found, just use the default if (ATSUFONDtoFontID((short)(intptr_t)p_font . fid, p_font . style, &t_font_id) != noErr) t_err = ATSUFONDtoFontID(0, p_font . style, &t_font_id); } ATSUStyle t_style; t_style = NULL; if (t_err == noErr) t_err = ATSUCreateStyle(&t_style); if (t_err == noErr) t_err = ATSUSetAttributes(t_style, sizeof(t_tags) / sizeof(ATSUAttributeTag), t_tags, t_sizes, t_attrs); ATSUTextLayout t_layout; t_layout = NULL; if (t_err == noErr) { UniCharCount t_run; t_run = p_length / 2; t_err = ATSUCreateTextLayoutWithTextPtr((const UniChar *)p_text, 0, p_length / 2, p_length / 2, 1, &t_run, &t_style, &t_layout); } if (t_err == noErr) t_err = ATSUSetTransientFontMatching(t_layout, true); if (t_err == noErr) { t_layout_options = kATSLineUseDeviceMetrics | kATSLineFractDisable; t_err = ATSUSetLayoutControls(t_layout, sizeof(t_layout_tags) / sizeof(ATSUAttributeTag), t_layout_tags, t_layout_sizes, t_layout_attrs); } MCGIntRectangle t_bounds; if (p_cgcontext == NULL) { ATSUTextMeasurement t_before, t_after, t_ascent, t_descent; if (t_err == noErr) t_err = ATSUGetUnjustifiedBounds(t_layout, 0, p_length / 2, &t_before, &t_after, &t_ascent, &t_descent); if (t_err == noErr) { t_ascent = (t_ascent + 0xffff) >> 16; t_descent = (t_descent + 0xffff) >> 16; t_after = (t_after + 0xffff) >> 16; t_bounds . x = p_location . x; t_bounds . y = p_location . y - p_font . ascent; t_bounds . width = t_after; t_bounds . height = p_font . descent + p_font . ascent; r_bounds = t_bounds; }
static cairo_status_t _cairo_atsui_font_create_toy(cairo_toy_font_face_t *toy_face, const cairo_matrix_t *font_matrix, const cairo_matrix_t *ctm, const cairo_font_options_t *options, cairo_scaled_font_t **font_out) { cairo_atsui_font_t *font = NULL; ATSUStyle style; ATSUFontID fontID; OSStatus err; Boolean isItalic, isBold; cairo_matrix_t scale; const char *family = toy_face->family; err = ATSUCreateStyle(&style); switch (toy_face->weight) { case CAIRO_FONT_WEIGHT_BOLD: isBold = true; break; case CAIRO_FONT_WEIGHT_NORMAL: default: isBold = false; break; } switch (toy_face->slant) { case CAIRO_FONT_SLANT_ITALIC: isItalic = true; break; case CAIRO_FONT_SLANT_OBLIQUE: isItalic = false; break; case CAIRO_FONT_SLANT_NORMAL: default: isItalic = false; break; } err = ATSUFindFontFromName(family, strlen(family), kFontFamilyName, kFontNoPlatformCode, kFontRomanScript, kFontNoLanguageCode, &fontID); if (err != noErr) { // couldn't get the font - remap css names and try again if (!strcmp(family, "serif")) family = "Times"; else if (!strcmp(family, "sans-serif")) family = "Helvetica"; else if (!strcmp(family, "cursive")) family = "Apple Chancery"; else if (!strcmp(family, "fantasy")) family = "Gadget"; else if (!strcmp(family, "monospace")) family = "Courier"; else // anything else - return error instead? family = "Courier"; err = ATSUFindFontFromName(family, strlen(family), kFontFamilyName, kFontNoPlatformCode, kFontRomanScript, kFontNoLanguageCode, &fontID); } ATSUAttributeTag styleTags[] = { kATSUQDItalicTag, kATSUQDBoldfaceTag, kATSUFontTag }; ATSUAttributeValuePtr styleValues[] = { &isItalic, &isBold, &fontID }; ByteCount styleSizes[] = { sizeof(Boolean), sizeof(Boolean), sizeof(ATSUFontID) }; err = ATSUSetAttributes(style, sizeof(styleTags) / sizeof(styleTags[0]), styleTags, styleSizes, styleValues); font = malloc(sizeof(cairo_atsui_font_t)); _cairo_scaled_font_init(&font->base, toy_face, font_matrix, ctm, options, &cairo_atsui_scaled_font_backend); cairo_matrix_multiply(&scale, font_matrix, ctm); font->style = CreateSizedCopyOfStyle(style, &scale); Fixed theSize = FloatToFixed(1.0); const ATSUAttributeTag theFontStyleTags[] = { kATSUSizeTag }; const ByteCount theFontStyleSizes[] = { sizeof(Fixed) }; ATSUAttributeValuePtr theFontStyleValues[] = { &theSize }; err = ATSUSetAttributes(style, sizeof(theFontStyleTags) / sizeof(ATSUAttributeTag), theFontStyleTags, theFontStyleSizes, theFontStyleValues); font->unscaled_style = style; font->fontID = fontID; font->scale = scale; *font_out = &font->base; return CAIRO_STATUS_SUCCESS; }
QFontEngineMac::QFontEngineMac(ATSUStyle baseStyle, ATSUFontID fontID, const QFontDef &def, QFontEngineMacMulti *multiEngine) : fontID(fontID), multiEngine(multiEngine), cmap(0), symbolCMap(false) { fontDef = def; ATSUCreateAndCopyStyle(baseStyle, &style); ATSFontRef atsFont = FMGetATSFontRefFromFont(fontID); cgFont = CGFontCreateWithPlatformFont(&atsFont); const int maxAttributeCount = 4; ATSUAttributeTag tags[maxAttributeCount + 1]; ByteCount sizes[maxAttributeCount + 1]; ATSUAttributeValuePtr values[maxAttributeCount + 1]; int attributeCount = 0; synthesisFlags = 0; quint16 macStyle = 0; { uchar data[4]; ByteCount len = 4; if (ATSFontGetTable(atsFont, MAKE_TAG('h', 'e', 'a', 'd'), 44, 4, &data, &len) == noErr) macStyle = qFromBigEndian<quint16>(data); } Boolean atsuBold = false; Boolean atsuItalic = false; if (fontDef.weight >= QFont::Bold) { if (!(macStyle & 1)) { synthesisFlags |= SynthesizedBold; atsuBold = true; tags[attributeCount] = kATSUQDBoldfaceTag; sizes[attributeCount] = sizeof(atsuBold); values[attributeCount] = &atsuBold; ++attributeCount; } } if (fontDef.style != QFont::StyleNormal) { if (!(macStyle & 2)) { synthesisFlags |= SynthesizedItalic; atsuItalic = true; tags[attributeCount] = kATSUQDItalicTag; sizes[attributeCount] = sizeof(atsuItalic); values[attributeCount] = &atsuItalic; ++attributeCount; } } tags[attributeCount] = kATSUFontTag; values[attributeCount] = &fontID; sizes[attributeCount] = sizeof(fontID); ++attributeCount; Q_ASSERT(attributeCount < maxAttributeCount + 1); OSStatus err = ATSUSetAttributes(style, attributeCount, tags, sizes, values); Q_ASSERT(err == noErr); Q_UNUSED(err); quint16 tmpFsType; if (ATSFontGetTable(atsFont, MAKE_TAG('O', 'S', '/', '2'), 8, 2, &tmpFsType, 0) == noErr) fsType = qFromBigEndian<quint16>(tmpFsType); else fsType = 0; }
QFontEngineMacMulti::QFontEngineMacMulti(const ATSFontFamilyRef &atsFamily, const ATSFontRef &atsFontRef, const QFontDef &fontDef, bool kerning) : QFontEngineMulti(0) { this->fontDef = fontDef; this->kerning = kerning; FMFontFamily fmFamily; FMFontStyle fntStyle = 0; fmFamily = FMGetFontFamilyFromATSFontFamilyRef(atsFamily); if (fmFamily == kInvalidFontFamily) { // Use the ATSFont then... fontID = FMGetFontFromATSFontRef(atsFontRef); } else { if (fontDef.weight >= QFont::Bold) fntStyle |= ::bold; if (fontDef.style != QFont::StyleNormal) fntStyle |= ::italic; FMFontStyle intrinsicStyle; FMFont fnt = 0; if (FMGetFontFromFontFamilyInstance(fmFamily, fntStyle, &fnt, &intrinsicStyle) == noErr) fontID = FMGetATSFontRefFromFont(fnt); } OSStatus status; status = ATSUCreateTextLayout(&textLayout); Q_ASSERT(status == noErr); const int maxAttributeCount = 5; ATSUAttributeTag tags[maxAttributeCount + 1]; ByteCount sizes[maxAttributeCount + 1]; ATSUAttributeValuePtr values[maxAttributeCount + 1]; int attributeCount = 0; Fixed size = FixRatio(fontDef.pixelSize, 1); tags[attributeCount] = kATSUSizeTag; sizes[attributeCount] = sizeof(size); values[attributeCount] = &size; ++attributeCount; tags[attributeCount] = kATSUFontTag; sizes[attributeCount] = sizeof(fontID); values[attributeCount] = &this->fontID; ++attributeCount; transform = CGAffineTransformIdentity; if (fontDef.stretch != 100) { transform = CGAffineTransformMakeScale(float(fontDef.stretch) / float(100), 1); tags[attributeCount] = kATSUFontMatrixTag; sizes[attributeCount] = sizeof(transform); values[attributeCount] = &transform; ++attributeCount; } status = ATSUCreateStyle(&style); Q_ASSERT(status == noErr); Q_ASSERT(attributeCount < maxAttributeCount + 1); status = ATSUSetAttributes(style, attributeCount, tags, sizes, values); Q_ASSERT(status == noErr); QFontEngineMac *fe = new QFontEngineMac(style, fontID, fontDef, this); fe->ref.ref(); engines.append(fe); }