GVTextLayout.m 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*************************************************************************
  2. * Copyright (c) 2011 AT&T Intellectual Property
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: Details at https://graphviz.org
  9. *************************************************************************/
  10. #include "config.h"
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <common/types.h>
  14. #include <gvc/gvcjob.h>
  15. #include "gvplugin_quartz.h"
  16. #ifdef __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__
  17. #if __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ >= 20000 && __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 30200
  18. #import "GVTextLayout.h"
  19. void *quartz_new_layout(char* fontname, double fontsize, char* text)
  20. {
  21. return [[GVTextLayout alloc] initWithFontName:fontname fontSize:fontsize text:text];
  22. }
  23. void quartz_size_layout(void *layout, double* width, double* height, double* yoffset_layout)
  24. {
  25. [(GVTextLayout*)layout sizeUpWidth:width height:height yoffset:yoffset_layout];
  26. }
  27. void quartz_draw_layout(void *layout, CGContextRef context, CGPoint position)
  28. {
  29. [(GVTextLayout*)layout drawInContext:context atPosition:position];
  30. }
  31. void quartz_free_layout(void *layout)
  32. {
  33. [(GVTextLayout*)layout release];
  34. }
  35. static NSString* _defaultFontName = @"TimesNewRomanPSMT";
  36. @implementation GVTextLayout
  37. - (id)initWithFontName:(char*)fontName fontSize:(CGFloat)fontSize text:(char*)text
  38. {
  39. if (self = [super init])
  40. {
  41. _font = nil;
  42. if (fontName)
  43. _font = [[UIFont fontWithName:[NSString stringWithUTF8String:fontName] size:fontSize] retain];
  44. if (!_font)
  45. _font = [[UIFont fontWithName:_defaultFontName size:fontSize] retain];
  46. _text = text ? [[NSString alloc] initWithUTF8String:text] : nil;
  47. }
  48. return self;
  49. }
  50. - (void)sizeUpWidth:(double*)width height:(double*)height yoffset:(double*)yoffset
  51. {
  52. CGSize size = [_text sizeWithFont:_font];
  53. CGFloat ascender = _font.ascender;
  54. *width = size.width;
  55. *height = size.height;
  56. *yoffset = ascender;
  57. }
  58. - (void)drawInContext:(CGContextRef)context atPosition:(CGPoint)position
  59. {
  60. UIGraphicsPushContext(context);
  61. CGContextSaveGState(context);
  62. CGContextScaleCTM(context, 1.0, -1.0);
  63. [_text drawAtPoint:CGPointMake(position.x, -position.y - _font.ascender) withFont:_font];
  64. CGContextRestoreGState(context);
  65. UIGraphicsPopContext();
  66. }
  67. - (void)dealloc
  68. {
  69. [_font release];
  70. [_text release];
  71. [super dealloc];
  72. }
  73. @end
  74. #endif
  75. #endif