NSBundle+GID3PAdditions.m 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #import <UIKit/UIKit.h>
  17. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h"
  18. NS_ASSUME_NONNULL_BEGIN
  19. #if SWIFT_PACKAGE
  20. NSString *const GoogleSignInBundleName = @"GoogleSignIn_GoogleSignIn";
  21. #else
  22. NSString *const GoogleSignInBundleName = @"GoogleSignIn";
  23. #endif
  24. @implementation NSBundle (GID3PAdditions)
  25. + (nullable NSBundle *)gid_frameworkBundle {
  26. // Look for the resource bundle in the main bundle.
  27. NSString *path = [[NSBundle mainBundle] pathForResource:GoogleSignInBundleName
  28. ofType:@"bundle"];
  29. if (!path) {
  30. // If we can't find the resource bundle in the main bundle, look for it in the framework bundle.
  31. path = [[NSBundle bundleForClass:[GIDSignIn class]] pathForResource:GoogleSignInBundleName
  32. ofType:@"bundle"];
  33. }
  34. return [NSBundle bundleWithPath:path];
  35. }
  36. + (void)gid_registerFonts {
  37. static dispatch_once_t once;
  38. dispatch_once(&once, ^{
  39. NSArray* allFontNames = @[ @"Roboto-Bold" ];
  40. NSBundle* bundle = [self gid_frameworkBundle];
  41. for (NSString *fontName in allFontNames) {
  42. // Check to see if the font is already here, and skip registration if so.
  43. if ([UIFont fontWithName:fontName size:[UIFont systemFontSize]]) { // size doesn't matter
  44. continue;
  45. }
  46. // Load the font data file from the bundle.
  47. NSString *path = [bundle pathForResource:fontName ofType:@"ttf"];
  48. CGDataProviderRef provider = CGDataProviderCreateWithFilename([path UTF8String]);
  49. CFErrorRef error;
  50. CGFontRef newFont = CGFontCreateWithDataProvider(provider);
  51. if (!newFont || !CTFontManagerRegisterGraphicsFont(newFont, &error)) {
  52. #ifdef DEBUG
  53. NSLog(@"Unable to load font: %@", fontName);
  54. #endif
  55. }
  56. CGFontRelease(newFont);
  57. CGDataProviderRelease(provider);
  58. }
  59. });
  60. }
  61. @end
  62. NS_ASSUME_NONNULL_END