What I did to find the widest character in a font out of those characters I was
using (i.e., not every character in a font) was to use
CTFontGetGlyphsForCharacters to get the glyphs and then to call
CTFontGetBoundingRectsForGlyphs with the glyphs, and then using the returned
value to add the origin.x and size.width. If I recall correctly, I found
CTFontGetBoundingBox gave me a value that was too large for my needs. The
following code works for a range of Unicode characters, and my particular needs
allowed for an American ASCII subset running on iOS on a Retina device, which
is all I tested it with, so this may not work for you—I may be making too many
assumptions—but hopefully it gives you something to try.
CGFloat
WidthOfWidestCharacterInRange
(
CTFontRef font,
UniChar firstCharacter,
UniChar lastCharacter,
UniChar* widestCharacter
)
{
CGFloat maximumLayoutWidth;
UniChar widestCharacterFound;
CGFloat layoutWidthOfThisGlyph;
UniChar thisCharacter;
CGGlyph thisGlyph;
CGRect boundsForThisGlyph;
maximumLayoutWidth = 0;
widestCharacterFound = 0;
for (uint32_t thisCharacterIndex = firstCharacter;
thisCharacterIndex <= lastCharacter;
++thisCharacterIndex)
{
thisCharacter = thisCharacterIndex;
if (CTFontGetGlyphsForCharacters
(font,&thisCharacter,&thisGlyph,1))
{
boundsForThisGlyph = CTFontGetBoundingRectsForGlyphs
(font,
kCTFontOrientationHorizontal,&thisGlyph,NULL,1);
layoutWidthOfThisGlyph = boundsForThisGlyph.origin.x +
boundsForThisGlyph.size.width;
if (maximumLayoutWidth < layoutWidthOfThisGlyph)
{
maximumLayoutWidth = layoutWidthOfThisGlyph;
widestCharacterFound = thisCharacter;
}
}
}
maximumLayoutWidth = ceil (maximumLayoutWidth);
if (NULL != widestCharacter)
{
*widestCharacter = widestCharacterFound;
}
return maximumLayoutWidth;
}
--
Gary L. Wade
http://www.garywade.com/ <http://www.garywade.com/>
> On Nov 14, 2016, at 11:52 AM, [email protected] wrote:
>
> I don’t have any great suggestions myself unfortunately. I took a quick look
> at CoreText APIs (which I would suggest for you over using Cocoa, probably)
> and didn’t see an immediate replacement API for answering “widest character”.
> I’m sure it would be possible to run through the font and look at the width
> of each character to identify the widest one. Or, if you’re always making
> this call for Geneva, and never any other font, you could also just run this
> code on 10.11, make a note of the results, and hardcode those results in your
> app for cases where FetchFontInfo returns an error. The Geneva font certainly
> isn’t going to change its metrics ever again.
>
> -eric
>
>
>> On Nov 11, 2016, at 5:29 PM, Mark Lucas <[email protected]> wrote:
>>
>> Hi Eric,
>>
>> Thanks for the prompt reply.
>>
>> Okay I’m totally out of my depth here — in millions of user-hours logged
>> against this widely-used code just within our app alone, this AFAIK is the
>> first time FetchFontInfo’s ever failed here (and since unfortunately thanks
>> to a decades-old coding error it crashes our app, I think I’d probably have
>> heard).
>>
>> This is down inside WASTE’s WENew code, so I’m not really sure how to go
>> about trying to replace it with ATSUI (which IIRC wants an actual block of
>> text in addition to a font, size, and face to generate metrics, and at this
>> stage there isn’t any….) or whether that’s likely to make any difference. Of
>> course I could guess, but is there a straightforward way to actually
>> identify the widest character in a font so I could pass that? Is there
>> perhaps a Cocoa API I could readily fall back to in these situations? It
>> only has to work in Sierra+...
>>
>> When I file a bug on this, should I include a copy of our user’s
>> Geneva.dfont? Is there anything that would be useful?
>>
>> Thanks!
>> -Mark
>>
>>
>>> On Nov 11, 2016, at 7:41 PM, Eric Schlegel <[email protected]> wrote:
>>>
>>> Doesn’t sound like anything I’ve heard of in Sierra, so I’m afraid I have
>>> no suggestions offhand. Most likely it’s fallout from ongoing changes to
>>> the font system (adding support for San Francisco as the system font, for
>>> example).
>>>
>>> -eric
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Carbon-dev mailing list ([email protected])
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/carbon-dev/archive%40mail-archive.com
This email sent to [email protected]