Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature request] Multiple text objects in one line without bullets or with align #369

Closed
vpetzel opened this issue Jun 29, 2018 · 16 comments
Assignees
Labels
enhancement-text feature:text Text formatting, multi-line, etc.
Milestone

Comments

@vpetzel
Copy link

vpetzel commented Jun 29, 2018

The current system will not allow for multiple text objects without break if there is no bullet or some kind of align specified. This turns into a Problem if you consider, that text formatting (italic, bold, underlinde, strike, ...) are global to one text object. This means, that even code like this

<html>
  <head>
    <script lang="javascript" src="PptxGenJS/libs/jquery.min.js"></script>
    <script lang="javascript" src="PptxGenJS/libs/jszip.min.js"></script>
    <script lang="javascript" src="PptxGenJS/dist/pptxgen.js"></script>
  </head>
  <body>
    <script>
        pptx = new PptxGenJS();
        var slide = pptx.addNewSlide();

        slide.addText([
            { text: 'abc', options: {}},
            { text: 'def', options: {italic:true}}
        ], {x:'0', y:'0', w:'100%', h:'100%', align:'center', valign:'middle'});

//          Save PPTX
        pptx.save('Titel');
    </script>
  </body>
</html>

is not able to achieve this: abc_def_, but will output two seperate lines for abc and def.
This could be easily mended by adding an additional attribute to the text objects, let’s say, break, and change line 3668 from

else if ( idx > 0 && (typeof textObj.options.bullet !== 'undefined' || typeof textObj.options.align !== 'undefined') ) {
				strSlideXml += '</a:p><a:p>' + paragraphPropXml;
			}

to

else if ( idx > 0 && (typeof textObj.options.break === 'undefined' || textObj.options.break) && (typeof textObj.options.bullet !== 'undefined' || typeof textObj.options.align !== 'undefined') ) {
				strSlideXml += '</a:p><a:p>' + paragraphPropXml;
			}

and using the code

<html>
  <head>
    <script lang="javascript" src="PptxGenJS/libs/jquery.min.js"></script>
    <script lang="javascript" src="PptxGenJS/libs/jszip.min.js"></script>
    <script lang="javascript" src="PptxGenJS/dist/pptxgen.js"></script>
  </head>
  <body>
    <script>
        pptx = new PptxGenJS();
        var slide = pptx.addNewSlide();

        slide.addText([
            { text: 'abc', options: {}},
            { text: 'def', options: {italic:true, break:false}}
        ], {x:'0', y:'0', w:'100%', h:'100%', align:'center', valign:'middle'});

//          Save PPTX
        pptx.save('Titel');
    </script>
  </body>
</html>

which will achieve the desired effect.
See this patch:
pptxgen.js.patch.txt

@vpetzel
Copy link
Author

vpetzel commented Jun 29, 2018

abc_def_ is supposed to be abc and def in italic, but GitHub’s italic specified doesn’t seem to like prepending non-whitespace chars.

@bigianb
Copy link

bigianb commented Aug 3, 2018

+1 ... I've just come across this limitation.
There is already a breakline option. Maybe if that is explicitly set to false then the linebreak could be squashed.

@vpetzel
Copy link
Author

vpetzel commented Aug 3, 2018

@bigianb If you really need this feature now, you can try using my patch above, it’s literally inserting the check

(typeof textObj.options.break === 'undefined' || textObj.options.break)

into

else if ( idx > 0 && (typeof textObj.options.bullet !== 'undefined' || typeof textObj.options.align !== 'undefined') ) {...}

From my experience it works, but I didn’t try anything too special. Hm, maybe one should change this to

NO ONE SHOULDN’T, I JUST DID NOT THINK WHEN WRITING THIS BIT!

(typeof textObj.options.break !== 'undefined' && textObj.options.break)

thus using this option only if it’s explicitely set to false ...

@gitbrent
Copy link
Owner

gitbrent commented Aug 9, 2018

I don't see where the library lacks the ability to format on a word-level basis.

This adds italics and bold without forcing a line-break or requiring bullets:

slide.addText(
  [
    { text:'1st word ', options:{ color:'99ABCC', italic:true } },
    { text:'2nd word ', options:{ color:'FFFF00', bold:true } },
    { text:'3rd word ', options:{ color:'0088CC' } }
  ],
  { x:1.5, y:1.5, w:6, h:2, margin:0.1, fill:'232323' }
);

screen shot 2018-08-08 at 23 28 58

@vpetzel
Copy link
Author

vpetzel commented Aug 9, 2018

@gitbrent Your code does not use any kind of align, add align:'center' to the options and you end up with

grafik

@vpetzel
Copy link
Author

vpetzel commented Aug 9, 2018

Using the code

slide.addText(
    [
        { text:'1st word ', options:{ color:'99ABCC', italic:true } },
        { text:'2nd word ', options:{ color:'FFFF00', bold:true, break:false } },
        { text:'3rd word ', options:{ color:'0088CC', break: false } }
    ],
    { x:1.5, y:1.5, w:6, h:2, margin:0.1, fill:'232323', align:'center' }
    );

with the patch you get what you want
grafik

@gitbrent
Copy link
Owner

But alignment necessitates a paragraph as you cant align words...

@vpetzel
Copy link
Author

vpetzel commented Aug 10, 2018

But you still have a paragraph, you just don’t add one for each text object. The problem is, that, as soon as an align is specified, you add one
'</a:p><a:p>' + paragraphPropXml
between the different text objects in the array.

@gitbrent
Copy link
Owner

Okay, so what problem are we trying to solve?

Do you have a screencap of the PptxGenJS output versus the result you're looking for.

@vpetzel
Copy link
Author

vpetzel commented Aug 15, 2018

When using the code

slide.addText(
    [
        { text:'1st word ', options:{ color:'99ABCC', italic:true } },
        { text:'2nd word ', options:{ color:'FFFF00', bold:true } },
        { text:'3rd word ', options:{ color:'0088CC' } }
    ],
    { x:1.5, y:1.5, w:6, h:2, margin:0.1, fill:'232323', align:'center' }
    );

I get

Note that the three text objects are not in the same line, so if I want to have it like this

there's no way to do it.

@PaddingtonTheBear
Copy link

@vpetzel, great job on writing this up and providing a fix in the mean time. I'm running into the same issue with this library when I want to style individual words in the PPT.

I couldn't figure this out for the longest time but as you mentioned, it only occurs if you're using the 'align' options, which is a pretty common use case. Adding your patch works great. It should be included in the base library, or if the regular functionality doesn't want to be changed, then add a new config property for the "don't break" scenario.

@gitbrent gitbrent self-assigned this Dec 12, 2018
@gitbrent gitbrent added the feature:text Text formatting, multi-line, etc. label Dec 12, 2018
@lejard-h
Copy link

Any news on this ? I am facing the same issue.

lejard-h added a commit to leftyio/PptxGenJS that referenced this issue Aug 12, 2019
@gitbrent
Copy link
Owner

I'll get this patched in an upcoming release.

var pptx = new PptxGenJS();
pptx.layout = "LAYOUT_WIDE";
var slide = pptx.addSlide();

// Works
slide.addText(
  [
    { text: "1st line", options: { fontSize: 24, color: "99ABCC" } },
    { text: "2nd line", options: { fontSize: 36, color: "FFFF00" } },
    { text: "3rd line", options: { fontSize: 48, color: "0088CC" } }
  ],
  { x: 1.5, y: 1.5, w: 6, h: 2, margin: 0.1, fill: "232323" }
);

// Creates one word per line (bug: `align` is inherited/applied to each text object)
slide.addText(
  [
    { text: "1st word ", options: { color: "99ABCC", italic: true } },
    { text: "2nd word ", options: { color: "FFFF00", bold: true } },
    { text: "3rd word ", options: { color: "0088CC" } }
  ],
  { x: 1.5, y: 1.5, w: 6, h: 2, margin: 0.1, fill: "232323", align: "center" }
);

pptx.save("PptxGenJS-Issue#369");

Screen Shot 2019-08-14 at 22 22 36

@gitbrent gitbrent added this to the 2.6.0 milestone Aug 15, 2019
@gitbrent gitbrent modified the milestones: 2.6.0, 3.0.0 Sep 25, 2019
@kupkama2
Copy link

hey @gitbrent, I just tried this in the 3.0.0-beta.4 and the problem is unfortunately still there.

@gitbrent gitbrent modified the milestones: 3.0.0, 3.1.0 Nov 20, 2019
@gitbrent gitbrent modified the milestones: 3.1.0, 3.2.0 Jan 18, 2020
@gitbrent
Copy link
Owner

Related: Issue #258

lejard-h added a commit to leftyio/PptxGenJS that referenced this issue May 22, 2020
@gitbrent
Copy link
Owner

Fixed via #751

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement-text feature:text Text formatting, multi-line, etc.
Projects
None yet
Development

No branches or pull requests

6 participants