Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Unfortunately i dont know how to suppress the logging.</p> <p>There is a way to obtain the glyphs though and draw the text as vector elements instead text, thus avoiding font issues. (credit: <a href="http://mexircus.com/blog//blog4.php/2009/08/13/messing-with-pdf-files" rel="nofollow">jegeblad</a>). Glyphs are not directly characters. </p> <p>Create a font:</p> <pre><code>//NSString * fontName; CGFont cgfont = CGFontCreateWithFontName ((CFStringRef)fontName); CGContextSetFont(cg, cgfont); CGContextSetFontSize(cg, fontSize); </code></pre> <p>If we wish to draw e.g. an NSString called word..get some info about the glyph:</p> <pre><code>int count = [word.s length]; unichar * buffer = new unichar[count+1]; CGGlyph * glyphs = new CGGlyph[count+1]; int * adv = new int[count+1]; CGRect * rects = new CGRect[count+1]; [word.s getCharacters:buffer]; CGFontGetGlyphsForUnichars(cgfont, buffer, glyphs, [word.s length]); CGFontGetGlyphAdvances(cgfont,glyphs,count,adv); CGFontGetGlyphBBoxes(cgfont, glyphs, count, rects); </code></pre> <p>In other words, we get the glyphs for the letters of the word and then we get the dimensions and advances of the individual glyphs. Note: I could not find any way to determine the number of glyphs for a word. Now we are ready to draw the glyphs:</p> <pre><code>CGContextShowGlyphsAtPoint(outputCG, x, y, glyphs,count); </code></pre> <p>That's neat but it doesn't solve the whole font-embedding problem. However, we can solve that problem if get quartz to draw the text as vector elements instead of ... well... text. We simply change the way text is drawn using the function:</p> <pre><code>CGContextSetTextDrawingMode (outputCG, kCGTextClip); </code></pre> <p>This will use the text as a clipping path instead of simply drawing it. If we first call CGContextShowGlyphsAtPoint and then draw a filled rectangle around the area where the glyphs are shown, that rectangle will be clipped to the glyphs and essentially we are drawing the letters of the word. However, since we are drawing a rectangle and not text, the resulting PDF file will not include the text but instead a set of vector elements that look like text. Therefore Quartz no longer needs to embed the fonts in the PDF file. One way to do this is by the following loop:</p> <pre><code>double scale = ffontSize / double( CGFontGetUnitsPerEm(cgfont) ); for (size_t i = 0; i &lt; count; ++i) { CGContextSaveGState(outputCG); CGContextShowGlyphsAtPoint(outputCG, x, p.y + spaceH, &amp;glyphs[i], 1 ); CGContextBeginPath(outputCG); CGContextAddRect(outputCG, CGRectMake(x+scale* rects[i].origin.x , p.y + spaceH + scale*(rects[i].origin.y), scale*(rects[i].size.width), scale*(rects[i].size.height)) ); CGContextFillPath(outputCG); CGContextRestoreGState(outputCG); x += adv[i] * scale; } </code></pre> <p>draw one glyph at a time by setting the clipping path to the individual glyph and drawing a filled rectangle behind it. I have of course set the current fill color to the color I want the text in. </p> <p>Remember to release the font when you are done:</p> <p><code>CGFontRelease(cgfont)</code>;</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload