NSBundle+GID3PAdditions.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "GoogleSignIn/Sources/NSBundle+GID3PAdditions.h"
  15. #import <CoreText/CoreText.h>
  16. #if __has_include(<UIKit/UIKit.h>)
  17. #import <UIKit/UIKit.h>
  18. #endif
  19. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h"
  20. NS_ASSUME_NONNULL_BEGIN
  21. #if SWIFT_PACKAGE
  22. NSString *const GoogleSignInBundleName = @"GoogleSignIn_GoogleSignIn";
  23. #else
  24. NSString *const GoogleSignInBundleName = @"GoogleSignIn";
  25. #endif
  26. @implementation NSBundle (GID3PAdditions)
  27. + (nullable NSBundle *)gid_frameworkBundle {
  28. // Look for the resource bundle in the main bundle.
  29. NSString *path = [[NSBundle mainBundle] pathForResource:GoogleSignInBundleName
  30. ofType:@"bundle"];
  31. if (!path) {
  32. // If we can't find the resource bundle in the main bundle, look for it in the framework bundle.
  33. path = [[NSBundle bundleForClass:[GIDSignIn class]] pathForResource:GoogleSignInBundleName
  34. ofType:@"bundle"];
  35. }
  36. return [NSBundle bundleWithPath:path];
  37. }
  38. + (void)gid_registerFonts {
  39. static dispatch_once_t once;
  40. dispatch_once(&once, ^{
  41. NSArray* allFontNames = @[ @"Roboto-Bold" ];
  42. NSBundle* bundle = [self gid_frameworkBundle];
  43. for (NSString *fontName in allFontNames) {
  44. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  45. // Check to see if the font is already here, and skip registration if so.
  46. if ([UIFont fontWithName:fontName size:[UIFont systemFontSize]]) { // size doesn't matter
  47. continue;
  48. }
  49. #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
  50. // Load the font data file from the bundle.
  51. NSString *path = [bundle pathForResource:fontName ofType:@"ttf"];
  52. CGDataProviderRef provider = CGDataProviderCreateWithFilename([path UTF8String]);
  53. CFErrorRef error;
  54. CGFontRef newFont = CGFontCreateWithDataProvider(provider);
  55. if (!newFont || !CTFontManagerRegisterGraphicsFont(newFont, &error)) {
  56. #ifdef DEBUG
  57. NSLog(@"Unable to load font: %@", fontName);
  58. #endif
  59. }
  60. CGFontRelease(newFont);
  61. CGDataProviderRelease(provider);
  62. }
  63. });
  64. }
  65. @end
  66. NS_ASSUME_NONNULL_END