splashdust / SVGQuartzRenderer

Simple SVG renderer implemented in Obj-C using CoreGraphics
http://www.splashdust.net/2010/09/svgquartzrenderer-a-basic-iphone-sdk-compatible-svg-renderer/
MIT License
119 stars 14 forks source link

Multibyte characters support #5

Open tsomeq opened 13 years ago

tsomeq commented 13 years ago

Hi,

I found your source does not support multi-byte characters like Japanese. I suggest you to handle text with UIKit API, because some functions (e.g. CGContextSelectFont() ) do not treat several languages well. I wrote a experimental code applyed this like below. This change enables rendering text contains UTF-8 multibyte characters. I hope this helps...


diff --git a/SVGRendererTouch/Classes/SVGStyle.m b/SVGRendererTouch/Classes/SVGStyle.m
index 8221a57..7d6d651 100644
--- a/SVGRendererTouch/Classes/SVGStyle.m
+++ b/SVGRendererTouch/Classes/SVGStyle.m
@@ -42,6 +42,8 @@

 @end

+static NSString* DefaultFontName = @"Helvetica";
+
 @implementation SVGStyle

 @synthesize fillGradientPoints;
@@ -413,7 +415,7 @@
                 break;
          case ';':
                 //parse line
-                [self parseStyle:styleBegin :styleColon-1 :styleColon+1 :styleEnd :defDict];
+                [self parseStyle:styleBegin :styleColon-1 :styleColon+1 :styleEnd-1 :defDict];
                 newLine = true;                
                 break;
         default:
@@ -736,14 +738,18 @@

 - (void)drawText:(char*)text :(CGPoint)location  withContext:(CGContextRef)context
 {
-    if(font)
-        self.font = @"Helvetica";

-    CGContextSetRGBFillColor(context, fillColor.r, fillColor.g, fillColor.b, fillColor.a);
+    UIGraphicsPushContext(context);

-    CGContextSelectFont(context, [font UTF8String], fontSize, kCGEncodingMacRoman);
-    CGContextSetFontSize(context, fontSize);
-    CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, -1.0));
+    CGContextSetRGBFillColor(context, fillColor.r, fillColor.g, fillColor.b, fillColor.a);
+    NSAutoreleasePool* arp = [NSAutoreleasePool new];
+    UIFont* tFont = nil;
+    if (font) {
+        // // TODO: Using alternative font if style(italic/oblique) or weight is specified.
+        // NSLog(@"fontnames for family name (%@) => %@", font, [UIFont fontNamesForFamilyName:font]);
+        tFont = [UIFont fontWithName:font size:fontSize];
+    }
+    if (!tFont) tFont = [UIFont fontWithName:DefaultFontName size:fontSize];

     [self setUpStroke:context];

@@ -757,12 +763,15 @@
         drawingMode = kCGTextStroke;

     CGContextSetTextDrawingMode(context, drawingMode);
-    CGContextShowTextAtPoint(context,
-                             location.x,
-                             location.y,
-                             (const char*)text,
-                             strlen(text));     

+    NSString* str = [NSString stringWithUTF8String:text];
+    CGSize textSize = [str sizeWithFont:tFont]; 
+    CGRect textRect = CGRectMake(location.x, location.y, textSize.width, textSize.height);
+    [str drawInRect:textRect withFont:tFont lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentLeft];
+    [arp release];
+    
+    UIGraphicsPopContext();
+
 }

 // Draw a path based on style information
boxerab commented 13 years ago

Thanks, tsomeq. I will merge this in.