Calculate the horizontal scale of a glyph

I am writing some text with specific text rectangles to a PDF using the following code:


PTElement *textBegin = [elementBuilder CreateTextBegin];

[elementWriter WriteElement:textBegin];

//**** TEXT RUN

PTElement *textRun = [elementBuilder CreateTextRunWithFont:myTestWord.m_wordContent font:[PTFont Create: [pdfDocument GetSDFDoc] type: e_pttimes_roman embed: NO] font_sz:wordRect.Height ];

[textRun SetTextMatrix:1 b:0 c:0 d:1 h:myTestWord.m_wordX4 v:myTestWord.m_wordY4];

PTGState *gStateTextRun = [textRun GetGState];

[gStateTextRun SetLineWidth:1];

[gStateTextRun SetTextRenderMode:e_ptfill_stroke_clip_text];

[gStateTextRun SetStrokeColorSpace:[PTColorSpace CreateDeviceRGB]];

[gStateTextRun SetFillColorSpace:[PTColorSpace CreateDeviceRGB]];

[gStateTextRun SetStrokeColorWithColorPt:[[PTColorPt alloc] initWithX: 1 y: 0 z: 0 w: 0]];

[gStateTextRun SetFillColorWithColorPt:[[PTColorPt alloc] initWithX: 1 y: 0 z: 0 w: 0]];

[gStateTextRun SetFillOpacity: 1.0];

[elementWriter WriteElement:textRun];

//**** TEXT END

[elementWriter WriteElement:[elementBuilder CreateTextEnd]];

NSLog(@“Written element %@ to PDF.”,myTestWord.m_wordContent);

How do I calculate the horizontal scale factor in the SetTextMatrix to fill the entire text rectangle?

If I understand it correctly the horizontal ScaleFactor 1 should be modified to fill the entire text rectangle .


That is going to possibly cause a lot of horizontal stretching. Is this your intention?

Typically to fit text, you first create the Element without writing it to the content stream, and call Element.GetBBox to find you what size it will be, and then increase/decrease the font size, and maybe break the text up into multiple lines, all depending on what kind of effect you are looking for.

See similar post.