-
Notifications
You must be signed in to change notification settings - Fork 171
Add font metrics package to FURN #2298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
amgleitman
merged 17 commits into
microsoft:main
from
amgleitman:furn-font-metrics-package
Nov 7, 2022
Merged
Changes from 7 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
708530a
Add NativeFontMetrics package
cbcff5a
Remove unneeded API
f57023d
Fix spacing
98d8e12
Change files
96e09be
Add @types/use-subscription dependency
4b9eabc
Update yarn.lock
37191a3
Add use-subscription as a dependency
4743422
Update yarn.lock again
f6f5b17
NativeFontMetrics.tsx -> NativeFontMetrics.ts
245fd82
Make NativeFontMetrics iOS only, part 1
a4f879f
Stub out NativeFontMetrics for non-iOS platforms
2a25072
Keep use-subscription version consistent with react-native and react-…
0c97508
Remove core-android capability
ef44158
id -> UIFontTextStyle
19638c7
hasListeners -> _hasListeners
794c41c
Revert "Remove core-android capability"
92fd7c9
Revert "Revert "Remove core-android capability""
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
...i-react-native-experimental-native-font-metrics-53961c7c-6784-4f8c-b197-79ec8ba74332.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "minor", | ||
| "comment": "Add NativeFontMetrics package", | ||
| "packageName": "@fluentui-react-native/experimental-native-font-metrics", | ||
| "email": "[email protected]", | ||
| "dependentChangeType": "patch" | ||
| } | ||
Empty file.
Empty file.
22 changes: 22 additions & 0 deletions
22
packages/experimental/NativeFontMetrics/FRNFontMetrics.podspec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| require 'json' | ||
|
|
||
| package = JSON.parse(File.read(File.join(__dir__, 'package.json'))) | ||
|
|
||
| Pod::Spec.new do |s| | ||
| s.name = 'FRNFontMetrics' | ||
| s.version = package['version'] | ||
| s.summary = package['description'] | ||
| s.license = package['license'] | ||
|
|
||
| s.authors = package['author'] | ||
| s.homepage = "https://github.com/microsoft/fluentui-react-native" | ||
|
|
||
| s.source = { :git => "https://github.com/microsoft/fluentui-react-native.git", :tag => "#{s.version}" } | ||
| s.swift_version = "5" | ||
|
|
||
| s.dependency 'React' | ||
|
|
||
| s.ios.deployment_target = "14.0" | ||
| s.ios.source_files = "ios/*.{swift,h,m}" | ||
|
|
||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| module.exports = require('@fluentui-react-native/scripts/babel.config'); |
31 changes: 31 additions & 0 deletions
31
packages/experimental/NativeFontMetrics/ios/FRNFontMetrics.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| #import <React/RCTBridgeModule.h> | ||
| #import <React/RCTEventEmitter.h> | ||
|
|
||
| @interface FRNFontMetrics : RCTEventEmitter <RCTBridgeModule> | ||
| @end | ||
|
|
||
| #import <React/RCTConvert.h> | ||
|
|
||
| typedef NS_ENUM(NSInteger, FRNTextStyle) { | ||
amgleitman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| FRNTextStyleUndefined, | ||
| FRNTextStyleCaption2, | ||
| FRNTextStyleCaption1, | ||
| FRNTextStyleFootnote, | ||
| FRNTextStyleSubheadline, | ||
| FRNTextStyleCallout, | ||
| FRNTextStyleBody, | ||
| FRNTextStyleHeadline, | ||
| FRNTextStyleTitle3, | ||
| FRNTextStyleTitle2, | ||
| FRNTextStyleTitle1, | ||
| FRNTextStyleLargeTitle | ||
| }; | ||
|
|
||
| @interface RCTConvert (FRNFontMetrics) | ||
|
|
||
| + (FRNTextStyle)FRNTextStyle:(nullable id)json; | ||
|
|
||
| @end | ||
|
|
||
| UIFontMetrics *_Nonnull FRNUIFontMetricsForTextStyle(FRNTextStyle textStyle); | ||
| CGFloat FRNBaseSizeForTextStyle(FRNTextStyle textStyle); | ||
amgleitman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
140 changes: 140 additions & 0 deletions
140
packages/experimental/NativeFontMetrics/ios/FRNFontMetrics.m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| #import "FRNFontMetrics.h" | ||
|
|
||
| static NSDictionary<NSString *, NSNumber *> *FRNRecognizedTextStyles() { | ||
| static NSDictionary<NSString *, NSNumber *> *styles; | ||
| static dispatch_once_t onceToken; | ||
| dispatch_once(&onceToken, ^{ | ||
| styles = @{ | ||
| @"caption1": @(FRNTextStyleCaption1), | ||
| @"caption2": @(FRNTextStyleCaption2), | ||
| @"footnote": @(FRNTextStyleFootnote), | ||
| @"subheadline": @(FRNTextStyleSubheadline), | ||
| @"callout": @(FRNTextStyleCallout), | ||
| @"body": @(FRNTextStyleBody), | ||
| @"headline": @(FRNTextStyleHeadline), | ||
| @"title3": @(FRNTextStyleTitle3), | ||
| @"title2": @(FRNTextStyleTitle2), | ||
| @"title1": @(FRNTextStyleTitle1), | ||
| @"largeTitle": @(FRNTextStyleLargeTitle), | ||
| }; | ||
| }); | ||
| return styles; | ||
| } | ||
|
|
||
| @implementation RCTConvert (FRNTextStyle) | ||
|
|
||
| RCT_ENUM_CONVERTER(FRNTextStyle, FRNRecognizedTextStyles(), FRNTextStyleUndefined, integerValue) | ||
|
|
||
| @end | ||
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| UIFontMetrics *FRNUIFontMetricsForTextStyle(FRNTextStyle textStyle) { | ||
| static NSDictionary<NSNumber *, UIFontTextStyle> *mapping; | ||
| static dispatch_once_t onceToken; | ||
| dispatch_once(&onceToken, ^{ | ||
| mapping = @{ | ||
| @(FRNTextStyleCaption2): UIFontTextStyleCaption2, | ||
| @(FRNTextStyleCaption1): UIFontTextStyleCaption1, | ||
| @(FRNTextStyleFootnote): UIFontTextStyleFootnote, | ||
| @(FRNTextStyleSubheadline): UIFontTextStyleSubheadline, | ||
| @(FRNTextStyleCallout): UIFontTextStyleCallout, | ||
| @(FRNTextStyleBody): UIFontTextStyleBody, | ||
| @(FRNTextStyleHeadline): UIFontTextStyleHeadline, | ||
| @(FRNTextStyleTitle3): UIFontTextStyleTitle3, | ||
| @(FRNTextStyleTitle2): UIFontTextStyleTitle2, | ||
| @(FRNTextStyleTitle1): UIFontTextStyleTitle1, | ||
| @(FRNTextStyleLargeTitle): UIFontTextStyleLargeTitle, | ||
| }; | ||
| }); | ||
|
|
||
| id uiFontTextStyle = mapping[@(textStyle)] ?: UIFontTextStyleBody; // Default to body if we don't recognize the specified ramp | ||
amgleitman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return [UIFontMetrics metricsForTextStyle:uiFontTextStyle]; | ||
| } | ||
|
|
||
| CGFloat FRNBaseSizeForTextStyle(FRNTextStyle textStyle) { | ||
| static NSDictionary<NSNumber *, NSNumber *> *mapping; | ||
| static dispatch_once_t onceToken; | ||
| dispatch_once(&onceToken, ^{ | ||
| // Values taken from https://developer.apple.com/design/human-interface-guidelines/foundations/typography/#specifications | ||
| mapping = @{ | ||
| @(FRNTextStyleCaption2): @11, | ||
| @(FRNTextStyleCaption1): @12, | ||
| @(FRNTextStyleFootnote): @13, | ||
| @(FRNTextStyleSubheadline): @15, | ||
| @(FRNTextStyleCallout): @16, | ||
| @(FRNTextStyleBody): @17, | ||
| @(FRNTextStyleHeadline): @17, | ||
| @(FRNTextStyleTitle3): @20, | ||
| @(FRNTextStyleTitle2): @22, | ||
| @(FRNTextStyleTitle1): @28, | ||
| @(FRNTextStyleLargeTitle): @34, | ||
| }; | ||
| }); | ||
|
|
||
| NSNumber *baseSize = mapping[@(textStyle)] ?: @17; // Default to body size if we don't recognize the specified ramp | ||
| return CGFLOAT_IS_DOUBLE ? [baseSize doubleValue] : [baseSize floatValue]; | ||
amgleitman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @implementation FRNFontMetrics { | ||
| BOOL hasListeners; | ||
amgleitman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| + (BOOL)requiresMainQueueSetup | ||
| { | ||
| return YES; | ||
| } | ||
|
|
||
| RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(allScaleFactors) | ||
| { | ||
| NSMutableDictionary *result = [NSMutableDictionary new]; | ||
| [FRNRecognizedTextStyles() enumerateKeysAndObjectsUsingBlock:^(NSString * styleString, __unused NSNumber * boxedTextStyle, __unused BOOL * stop) { | ||
| result[styleString] = [self scaleFactorForStyle:styleString]; | ||
| }]; | ||
| return result; | ||
| } | ||
|
|
||
| RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(scaleFactorForStyle:(NSString *)styleString) | ||
| { | ||
| FRNTextStyle style = [RCTConvert FRNTextStyle:styleString]; | ||
| UIFontMetrics *fontMetrics = FRNUIFontMetricsForTextStyle(style); | ||
| CGFloat baseSize = FRNBaseSizeForTextStyle(style); | ||
| CGFloat scaleFactor = [fontMetrics scaledValueForValue:baseSize] / baseSize; | ||
| return @(scaleFactor); | ||
| } | ||
|
|
||
| #pragma mark - RCTEventEmitter | ||
|
|
||
| - (NSArray<NSString *> *)supportedEvents | ||
| { | ||
| return @[ @"onFontMetricsChanged" ]; | ||
| } | ||
|
|
||
| - (void)startObserving | ||
| { | ||
| hasListeners = YES; | ||
| [[NSNotificationCenter defaultCenter] addObserver:self | ||
| selector:@selector(onFontMetricsChanged:) | ||
| name:UIContentSizeCategoryDidChangeNotification | ||
| object:nil]; | ||
| } | ||
|
|
||
| - (void)stopObserving | ||
| { | ||
| hasListeners = NO; | ||
| [[NSNotificationCenter defaultCenter] removeObserver:self]; | ||
| } | ||
|
|
||
| #pragma mark - Event processing | ||
|
|
||
| - (void)onFontMetricsChanged:(NSNotification *)notification { | ||
| if (hasListeners) { | ||
| [self sendEventWithName:@"onFontMetricsChanged" body:@{@"newScaleFactors": [self allScaleFactors]}]; | ||
| } | ||
| } | ||
|
|
||
| RCT_EXPORT_MODULE(); | ||
|
|
||
| @end | ||
|
|
||
| NS_ASSUME_NONNULL_END | ||
1 change: 1 addition & 0 deletions
1
packages/experimental/NativeFontMetrics/ios/FRNFontMetrics.xcodeproj/project.pbxproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| // This dummy project file is required for @react-native-community/cli <8.0 to recognize this package as a native module |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| const { preset } = require('@fluentui-react-native/scripts'); | ||
|
|
||
| preset(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| { | ||
| "name": "@fluentui-react-native/experimental-native-font-metrics", | ||
| "version": "0.1.0", | ||
| "description": "A temporary partial wrapper for UIFontMetrics.", | ||
| "license": "MIT", | ||
| "author": "Microsoft <[email protected]>", | ||
| "homepage": "https://github.com/microsoft/fluentui-react-native", | ||
| "main": "src/index.ts", | ||
| "module": "src/index.ts", | ||
| "typings": "lib/index.d.ts", | ||
| "onPublish": { | ||
| "main": "lib-commonjs/index.js", | ||
| "module": "lib/index.js" | ||
| }, | ||
| "scripts": { | ||
| "build": "fluentui-scripts build", | ||
| "just": "fluentui-scripts", | ||
| "clean": "fluentui-scripts clean", | ||
| "lint": "fluentui-scripts eslint", | ||
| "depcheck": "fluentui-scripts depcheck", | ||
| "test": "fluentui-scripts jest", | ||
| "update-snapshots": "fluentui-scripts jest -u", | ||
| "prettier": "fluentui-scripts prettier", | ||
| "prettier-fix": "fluentui-scripts prettier --fix true" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/microsoft/fluentui-react-native.git", | ||
| "directory": "packages/experimental/NativeFontMetrics" | ||
| }, | ||
| "devDependencies": { | ||
| "@fluentui-react-native/eslint-config-rules": "^0.1.1", | ||
| "@fluentui-react-native/scripts": "^0.1.1", | ||
| "@types/react-native": "^0.68.0", | ||
| "@types/use-subscription": "1.0.0", | ||
| "react": "17.0.2", | ||
| "react-native": "^0.68.0", | ||
| "use-subscription": "1.8.0" | ||
amgleitman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }, | ||
| "peerDependencies": { | ||
| "react": "17.0.2", | ||
| "react-native": "^0.68.0" | ||
| }, | ||
| "rnx-kit": { | ||
| "reactNativeVersion": "^0.68", | ||
| "reactNativeDevVersion": "^0.68", | ||
| "kitType": "library", | ||
| "capabilities": [ | ||
Saadnajmi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "core", | ||
| "core-android", | ||
| "core-ios", | ||
| "react" | ||
| ] | ||
| } | ||
| } | ||
24 changes: 24 additions & 0 deletions
24
packages/experimental/NativeFontMetrics/src/NativeFontMetrics.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { NativeModules } from 'react-native'; | ||
|
|
||
| export const NativeFontMetrics = NativeModules.FRNFontMetrics; | ||
|
|
||
| export type TextStyle = | ||
| | 'caption2' | ||
| | 'caption1' | ||
| | 'footnote' | ||
| | 'subheadline' | ||
| | 'callout' | ||
| | 'body' | ||
| | 'headline' | ||
| | 'title3' | ||
| | 'title2' | ||
| | 'title1' | ||
| | 'largeTitle'; | ||
|
|
||
| export type ScaleFactors = { [K in TextStyle]: number | undefined }; | ||
|
|
||
| interface NativeFontMetricsInterface { | ||
amgleitman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| allScaleFactors(): ScaleFactors; | ||
| scaleFactorForStyle(style: TextStyle): number; | ||
| } | ||
| export default NativeFontMetrics as NativeFontMetricsInterface; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export * from './NativeFontMetrics'; | ||
| export * from './useFontMetrics'; |
23 changes: 23 additions & 0 deletions
23
packages/experimental/NativeFontMetrics/src/useFontMetrics.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { useMemo } from 'react'; | ||
| import { NativeEventEmitter } from 'react-native'; | ||
| import { useSubscription } from 'use-subscription'; | ||
| import NativeFontMetrics, { ScaleFactors } from './NativeFontMetrics'; | ||
|
|
||
| const eventEmitter = new NativeEventEmitter(NativeFontMetrics as any); | ||
|
|
||
| export function useFontMetrics(): ScaleFactors { | ||
| const subscription = useMemo( | ||
| () => ({ | ||
| getCurrentValue: () => NativeFontMetrics.allScaleFactors(), | ||
| subscribe: (callback) => { | ||
| const appearanceSubscription = eventEmitter.addListener('onFontMetricsChanged', callback); | ||
| return () => { | ||
| appearanceSubscription.remove(); | ||
| }; | ||
| }, | ||
| }), | ||
| [], | ||
| ); | ||
|
|
||
| return useSubscription(subscription); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "extends": "@fluentui-react-native/scripts/tsconfig.json", | ||
| "compilerOptions": { | ||
| "outDir": "lib" | ||
| }, | ||
| "include": ["src"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3556,6 +3556,11 @@ | |
| resolved "https://registry.yarnpkg.com/@types/ua-parser-js/-/ua-parser-js-0.7.36.tgz#9bd0b47f26b5a3151be21ba4ce9f5fa457c5f190" | ||
| integrity sha512-N1rW+njavs70y2cApeIw1vLMYXRwfBy+7trgavGuuTfOd7j1Yh7QTRc/yqsPl6ncokt72ZXuxEU0PiCp9bSwNQ== | ||
|
|
||
| "@types/[email protected]": | ||
| version "1.0.0" | ||
| resolved "https://registry.yarnpkg.com/@types/use-subscription/-/use-subscription-1.0.0.tgz#d146f8d834f70f50d48bd8246a481d096f11db19" | ||
| integrity sha512-0WWZ5GUDKMXUY/1zy4Ur5/zsC0s/B+JjXfHdkvx6JgDNZzZV5eW+KKhDqsTGyqX56uh99gwGwbsKbVwkcVIKQA== | ||
|
|
||
| "@types/[email protected]": | ||
| version "8.3.4" | ||
| resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.4.tgz#bd86a43617df0594787d38b735f55c805becf1bc" | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.