Skip to content

Commit

Permalink
Implement CTFontGetVerticalTranslationsForGlyphs
Browse files Browse the repository at this point in the history
  • Loading branch information
aballway committed Feb 27, 2017
1 parent 52d468b commit b31a279
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
22 changes: 18 additions & 4 deletions Frameworks/CoreText/CTFont.mm
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@
const CFStringRef kCTFontDefaultItalicFontName = CFSTR("Arial Italic");
const CFStringRef kCTFontDefaultMonospacedFontName = CFSTR("Courier New");

// Reference platform adds a constant amount of space (scaled by font size) to each glyph's vertical translation
static const unsigned int sc_VerticalTranslationSpace = 102;

using namespace Microsoft::WRL;

struct __CTFont {
Expand Down Expand Up @@ -710,8 +713,8 @@ CGPathRef CTFontCreatePathForGlyph(CTFontRef font, CGGlyph glyph, const CGAffine
}

/**
@Status Stub
@Notes
@Status NotInPlan
@Notes Not much usage, can be easily replaced with CTFontGetGlyphsForCharacters in most common uses
*/
CGGlyph CTFontGetGlyphWithName(CTFontRef font, CFStringRef glyphName) {
UNIMPLEMENTED();
Expand Down Expand Up @@ -796,11 +799,22 @@ CGRect CTFontGetBoundingRectsForGlyphs(
}

/**
@Status Stub
@Status Interoperable
@Notes
*/
void CTFontGetVerticalTranslationsForGlyphs(CTFontRef font, const CGGlyph glyphs[], CGSize translations[], CFIndex count) {
UNIMPLEMENTED();
if (font && glyphs && translations && count > 0L) {
DWRITE_GLYPH_METRICS metrics[count];
if (FAILED(font->_dwriteFontFace->GetDesignGlyphMetrics(glyphs, count, metrics, false))) {
TraceError(g_logTag, L"Unable to get glyph metrics");
return;
}

std::transform(metrics, metrics + count, translations, [font](const DWRITE_GLYPH_METRICS& metrics) {
return CGSize{ -0.5 * __CTFontScaleMetric(font, metrics.advanceWidth),
-__CTFontScaleMetric(font, metrics.verticalOriginY - metrics.topSideBearing + sc_VerticalTranslationSpace) };
});
}
}

/**
Expand Down
5 changes: 2 additions & 3 deletions include/CoreText/CTFont.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,12 @@ CORETEXT_EXPORT CGFloat CTFontGetSlantAngle(CTFontRef font);
CORETEXT_EXPORT CGFloat CTFontGetCapHeight(CTFontRef font);
CORETEXT_EXPORT CGFloat CTFontGetXHeight(CTFontRef font);
CORETEXT_EXPORT CGPathRef CTFontCreatePathForGlyph(CTFontRef font, CGGlyph glyph, const CGAffineTransform* matrix);
CORETEXT_EXPORT CGGlyph CTFontGetGlyphWithName(CTFontRef font, CFStringRef glyphName) STUB_METHOD;
CORETEXT_EXPORT CGGlyph CTFontGetGlyphWithName(CTFontRef font, CFStringRef glyphName) NOTINPLAN_METHOD;
CORETEXT_EXPORT CGRect CTFontGetBoundingRectsForGlyphs(
CTFontRef font, CTFontOrientation orientation, const CGGlyph glyphs[], CGRect* boundingRects, CFIndex count);
CORETEXT_EXPORT double CTFontGetAdvancesForGlyphs(
CTFontRef font, CTFontOrientation orientation, const CGGlyph glyphs[], CGSize* advances, CFIndex count);
CORETEXT_EXPORT void CTFontGetVerticalTranslationsForGlyphs(CTFontRef font, const CGGlyph glyphs[], CGSize translations[], CFIndex count)
STUB_METHOD;
CORETEXT_EXPORT void CTFontGetVerticalTranslationsForGlyphs(CTFontRef font, const CGGlyph glyphs[], CGSize translations[], CFIndex count);
CORETEXT_EXPORT CFArrayRef CTFontCopyVariationAxes(CTFontRef font) STUB_METHOD;
CORETEXT_EXPORT CFDictionaryRef CTFontCopyVariation(CTFontRef font) STUB_METHOD;
CORETEXT_EXPORT CFArrayRef CTFontCopyFeatures(CTFontRef font) STUB_METHOD;
Expand Down
8 changes: 4 additions & 4 deletions scripts/git/common.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function Check-Result() {
function Get-RepoRoot {
$root = & git.exe rev-parse --show-toplevel 2>&1
Check-Result

return $root.Trim()
}

Expand All @@ -25,7 +25,7 @@ function Filter-SourceFiles {

$filter = {$_ -like "*.mm" -or $_ -like "*.m" -or $_ -like "*.c" -or $_ -like "*.cpp" -or $_ -like "*.h"}
[array]$files = $args | Where-Object -FilterScript $filter

return $files
}

Expand Down Expand Up @@ -55,9 +55,9 @@ function Get-StagedFiles {

# Formats the given file using ClangFormat
function ClangFormatFile([string]$repoRoot, [string]$filename, [bool]$isDryRun){
$clangCommand = "$repoRoot/tools/scripts/clang-format.ps1"
$clangCommand = "$repoRoot/scripts/git/clang-format.ps1"
$filename = $filename.Trim()

if ($isDryRun) {
& "$clangCommand" -Directory "" -File $filename -DryRun 2>&1
} else {
Expand Down
22 changes: 20 additions & 2 deletions tests/unittests/CoreText/CTFontTests.mm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//******************************************************************************
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
Expand Down Expand Up @@ -740,4 +740,22 @@ virtual void TearDown() {
});
CGPathApply(pathWithFontSizeAndTransforms, &comparePathContext, comparePathToExpected);
ASSERT_EQ(expectedElements.size(), comparePathContext.count);
}
}

TEST(CTFont, GetVerticalTranslationsForGLyphs) {
auto font = woc::MakeAutoCF<CTFontRef>(CTFontCreateWithName(CFSTR("Arial"), 20, nullptr));
UniChar chars[4] = { 'T', 'e', 's', 't' };
CGGlyph glyphs[4];
CTFontGetGlyphsForCharacters(font, chars, glyphs, 4);

CGSize translations[4];
CTFontGetVerticalTranslationsForGlyphs(font, glyphs, translations, 4);
EXPECT_NEAR(-6.108, translations[0].width, c_errorMargin);
EXPECT_NEAR(-5.562, translations[1].width, c_errorMargin);
EXPECT_NEAR(-5.000, translations[2].width, c_errorMargin);
EXPECT_NEAR(-2.778, translations[3].width, c_errorMargin);
EXPECT_NEAR(-15.3125, translations[0].height, c_errorMargin);
EXPECT_NEAR(-11.6016, translations[1].height, c_errorMargin);
EXPECT_NEAR(-11.6016, translations[2].height, c_errorMargin);
EXPECT_NEAR(-14.9902, translations[3].height, c_errorMargin);
}

0 comments on commit b31a279

Please sign in to comment.