How do I get Lucida Grande italic into my application?

青春壹個敷衍的年華 提交于 2019-12-01 00:25:22

This answer will be similar to my initial one, but updated for what, after more testing, works.

So, first, my method of creating the italic font was deeply flawed. Instead of simply applying a rotation to the text, I needed to apply a skew transform. I ended up finding a good skew transform to apply at WebKit's Font code. It contained the skew transform:

CGAffineTransformMake(1, 0, -tanf(SYNTHETIC_OBLIQUE_ANGLE * acosf(0) / 90), 1, 0, 0)

It does look good.

Simply using a different font is not the correct answer. While the Lucida Sans font is virtually identical to Lucida Grande (which is returned by systemFontOfSize) and has a real italic variant, the italic variant will not draw Japanese Characters in italic.

So, what appears to be the only answer is to obtain the systemFontOfSize, check to see if it has an italic variant, and, if not, add a skew transform.

Here is my final solution:

NSFont              *theFont            = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSMiniControlSize]];
NSFontManager       *sharedFontManager  = [NSFontManager sharedFontManager];

if ( wantItalic ) 
{
    theFont = [sharedFontManager convertFont:theFont toHaveTrait:NSItalicFontMask];

    NSFontTraitMask fontTraits = [sharedFontManager traitsOfFont:theFont];

    if ( !( (fontTraits & NSItalicFontMask) == NSItalicFontMask ) ) 
    {
        const CGFloat kRotationForItalicText = -14.0;

        NSAffineTransform *fontTransform = [NSAffineTransform transform];           

        [fontTransform scaleBy:[NSFont systemFontSizeForControlSize:NSMiniControlSize]];

        NSAffineTransformStruct italicTransformData;

        italicTransformData.m11 = 1;
        italicTransformData.m12 = 0;
        italicTransformData.m21 = -tanf( kRotationForItalicText * acosf(0) / 90 );
        italicTransformData.m22 = 1;
        italicTransformData.tX  = 0;
        italicTransformData.tY  = 0;

        NSAffineTransform   *italicTransform = [NSAffineTransform transform];

        [italicTransform setTransformStruct:italicTransformData];

        [fontTransform appendTransform:italicTransform];

        theFont = [NSFont fontWithDescriptor:[theFont fontDescriptor] textTransform:fontTransform];
    }
}

So, first, my method of creating the italic font was deeply flawed. Instead of simply applying a rotation to the text, I needed to apply a skew transform. I ended up finding a good skew transform to apply at WebKit's Font code. It contained the skew transform:

CGAffineTransformMake(1, 0, -tanf(SYNTHETIC_OBLIQUE_ANGLE * acosf(0) / 90), 1, 0, 0)

It does look good. The cocoa code to set this up is:

const CGFloat kRotationForItalicText = -14.0;

NSAffineTransform *fontTransform = [NSAffineTransform transform];           

[fontTransform scaleBy:[NSFont systemFontSizeForControlSize:NSMiniControlSize]];

NSAffineTransformStruct italicTransformData;

italicTransformData.m11 = 1;
italicTransformData.m12 = 0;
italicTransformData.m21 = -tanf( kRotationForItalicText * acosf(0) / 90 );
italicTransformData.m22 = 1;
italicTransformData.tX  = 0;
italicTransformData.tY  = 0;

NSAffineTransform   *italicTransform = [NSAffineTransform transform];

[italicTransform setTransformStruct:italicTransformData];

However, I was told by another person that the "Lucida Sans" font is virtually identical to Lucida Grande and does have a real italic variant.

So, basically, I am using a different font, but one that should meet with full approval. However, if for some reason the Lucida Sans font cannot be found, I will default back to systemFontOfSize and apply the above transform to it if necessary.

You rock! I made one mod -16.0 instead of -14.0 rotation... so user can find italicized values more easily in a giant spreadsheet. My problem was that using a non-LucidaGrande font causes all kinds of vertical alignment issues throughout my UI.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!