foliojs / pdfkit

A JavaScript PDF generation library for Node and the browser
http://pdfkit.org/
MIT License
9.89k stars 1.15k forks source link

Subscript and superscript support #15

Open devongovett opened 13 years ago

stretch4x4 commented 8 years ago

Is there any way to do this currently or is it not available? We are using pdfmake which makes use of this library and we will need to add support for these soon.

steschi commented 8 years ago

I would be interested in this feature too.

shuaybi commented 8 years ago

We need to add superscript to the text for footnote references. This is an important feature for us. Can support for this feature be added to the library soon?

meganmcmillan commented 8 years ago

Is this a feature that will be implemented soon? We also need to add an important superscript in our pdfs.

fabiancook commented 8 years ago

Just a note, you can achieve superscript by using:

document.font(originalFont, originalSize * 0.5);
document.text(str, options);
document.font(originalFont, originalSize);

I would assume that you could do something along the lines of this for subscript (haven't tested, just an example):

const currentLineHeight = document.currentLineHeight(),
   currentY = document.y,
   originalOptionsY = options.y;
document.font(originalFont, originalSize * 0.5);
options.y = currentY + (currentLineHeight * 0.5);
document.text(str, options);
document.y = currentY;
options.y = originalOptionsY;
document.font(originalFont, originalSize);
moyogo commented 8 years ago

A better way would be to use the OpenType features if available in the font used. Optically designed superscript and subscripts are better than scaled ones. Use {features: ['sups']} on the text that should be superscript and {features: ['subs']} on the text that should be subscript.

For example

doc.text('Some text', {continue: true});
doc.text('a', {features: ['sups'], continue: true});
doc.text(', some more text.', {features: []});
fabiancook commented 8 years ago

@moyogo Had tried that, with the font I was using it didn't appear to work. However I do agree using the OpenType features is the correct way

datenstau commented 6 years ago

The OpenType approach doesn't work for me with Helvetica.

What about Unicode superscripts? They don't work either. I have a lot of text with Unicode superscripts in it and these characters just don't get displayed. Manually cracking up these texts is also no option.

How to add support for Unicode superscripts?

EDIT: Embedding the Liberation font was a solution to this!

rohitverma1986 commented 6 years ago

I want to add a function similar to text that will substitute tags <sup> or <sub> with actual conversion of enclosed characters(s) into respective superscript or subscript. Although I used the linkToPage function, but cannot understand much of it. I want to create something like linkToPage for this as well.

ITSurgeon commented 4 years ago

A better way would be to use the OpenType features if available in the font used. Optically designed superscript and subscripts are better than scaled ones. Use {features: ['sups']} on the text that should be superscript and {features: ['subs']} on the text that should be subscript.

For example

doc.text('Some text', {continue: true});
doc.text('a', {features: ['sups'], continue: true});
doc.text(', some more text.', {features: []});

It works (thank you for idea), but:

  1. Right syntax is continued: true (with d).
  2. Only with fonts, that support "sups" OpenType feature (you can choose it from this list (https://code.thisarmy.com/fontsinfo/) or you may google if your font supports this feature).
  3. Chosen font must be added to the fonts directory and registered in index.js file that uses PdfKit. For example: doc.registerFont('Ubuntu', '__dirname/fonts/Ubuntu-Regular.ttf');
  4. Even if your main font doesn't support "sups" OpenType feature you can additionally register font that does it and use it only for superscripted text. For example:
        doc.text('Total area: ' + totalArea + ' m', {continued: true});
        doc.font('Ubuntu').text('2', {features: ['sups']});
weebao commented 2 months ago

Is there any update on this superscript/subscript feature?