PDFBox Layout: Add Line Break to StyledText - layout

I'm using pdfbox-layout and I want to add line breaks to a StyledText Element but can't manage to do it.
If a styled text contains a line break "\n", I get the following error message:
"StyledText must not contain line breaks, use TextFragment.LINEBREAK for that"
But I can't find an example in the documentation that shows how use TextFragment.LINEBREAK.
Does anybody have an example or a alternative solution for this?
SOLUTION:
I found a solution how to do it. You need to create a NewLine Textfragment and add it to the TextFlow every time you need a linebreak. Here is an example:
NewLine linebreak = new NewLine(fontSize);
TextFlow textflow = new TextFlow();
textflow.add("first line");
textflow.add(linebreak);
textflow.add("second line");
textflow.add(linebreak);
textflow.add("third line");
textflow.drawText(contentStream, posX, posY, Alignment.Left, drawListener);

SOLUTION:
I found a solution how to do it. You need to create a NewLine Textfragment and add it to the TextFlow every time you need a linebreak. Here is an example:
NewLine linebreak = new NewLine(fontSize);
TextFlow textflow = new TextFlow();
textflow.add("first line");
textflow.add(linebreak);
textflow.add("second line");
textflow.add(linebreak);
textflow.add("third line");
textflow.drawText(contentStream, posX, posY, Alignment.Left, drawListener);

Related

insert GlyphProperty.null in shouldGenerateGlyphs causes cursor misplacement problem

I am hiding the character * by inserting GlyphProperty.null then calling setGlyphs(_:properties:characterIndexes:font:forGlyphRange:) inside layoutManager(_:shouldGenerateGlyphs:properties:characterIndexes:font:forGlyphRange:) as described in the answer in https://stackoverflow.com/a/57697139/9568961, it works but after a certain sequence of text editing, the cursor starts to misplace, as shown in the gif https://github.com/dzAtZJU/Demos/blob/master/cursor_misplace.GIF?raw=true
GIF Description: When the second line becomes qq, I try to move the cursor to the end of first line, but it move directly to the beginning. I then try to delete characters, then the cursor move to the end correctly.
The sample project is here https://github.com/dzAtZJU/HideText.
Is it a bug? Have anyone run into this? How can I work around this?
This is a year-old question, but for anyone struggling with the same problem, here's a solution.
My Markdown-like parser knows the ranges for both the line on which the cursor is now, and where it was previously. I'm calling hideAndShowMarkup whenever text view selection changes.
You need to invalidate current glyphs for both of the ranges, and then redraw them synchronously before updating insertion point. The most crucial step is invalidating layout for the range where you want to show Markdown symbols. Without that, your caret gets drawn into the wrong position.
Code is in Objective C, but should be easy to implement in Swift, too.
-(void)hideAndShowMarkup {
// Just for reference
Line* line = currentLine;
Line* prevLine = previouslySelectedLine;
[self.layoutManager invalidateGlyphsForCharacterRange:line.range changeInLength:0 actualCharacterRange:nil];
[self.layoutManager invalidateGlyphsForCharacterRange:prevLine.range changeInLength:0 actualCharacterRange:nil];
[self.layoutManager invalidateLayoutForCharacterRange:line.range actualCharacterRange:nil];
if (NSGraphicsContext.currentContext) {
[self.layoutManager drawGlyphsForGlyphRange:line.range atPoint:self.frame.origin];
[self.layoutManager drawGlyphsForGlyphRange:prevLine.range atPoint:self.frame.origin];
}
[self updateInsertionPointStateAndRestartTimer:NO];
}

How Do I Remove a String that I Added Using svg.appendChild?

I am trying to build a program that will take a user input in a text box and display it. My problem is that when I enter a new input into the text box hat input overlaps the first text. What I want is that original text to be removed and replaced by the new input. The code I am using is below.
function eccentricityChanged() {
let svg = document.getElementById('diagram');
let text = document.createElementNS('http://www.w3.org/2000/svg', 'text');
text.setAttribute('x', 585);
text.setAttribute('y', 275);
text.setAttribute('fill', 'red');
svg.appendChild(text);
svg.selectAll('*').remove();
let txt = document.getElementById("eccentricity").value;
parseFloat('text', txt);
text.innerHTML = txt;

JTextPane, second word must not go to new line when typing and exceeding width

I'm using JTextPane because I need to markup text.
When typing into the JtextPane the second word always goes to new line. Then also when making the second word longer (after it went to a new line) the scroll bar appears and the JTextPane resizes. If I start typing in a new row then second word in that line does not go to a new line (as long the second word does not exceed width)
I tried adding a blank string that is very long in the first line and seems to do the trick, but this blank line is not really what I want to have.
Also tried adding it to after the text but it gives more problems
Is there something else that I can do to make the text only go to new line when I press enter? I can do it without a blank string using jtextarea, but then I can't markup text as I type.
I found the answer:
JTextPane textPane = new JTextPane()
{
public boolean getScrollableTracksViewportWidth()
{
return getUI().getPreferredSize(this).width
<= getParent().getSize().width;
}
};
We can overite the getScrollableTracksViewportWidth method to have a no wrapping text pane.
Website for more info: Here

Whenever I type colon in insert mode it moves my text to the very beginning of line

Whenever I type a : (colon) it moves all the text on the current line to the beginning of the line, ignoring spaces and tabs.
So if I type
var combo = new Ext.form.ComboBox({
typeAhead //I'm about to type a colon, but right now it looks fine
})
Then I type the colon it moves the text and it now looks like
var combo = new Ext.form.ComboBox({
typeAhead: //text is no longer indented
})
This is a javascript file, so that might be causing the problem?
How can I stop my text from being moved to the beginning of the line when I type a colon?
Adding a colon to the end of a token is causing vim to interpret it as a jump label for C-indenting purposes. :set cino+=L0 should cause it to stay in the current column.
Also, doesn't the JSON syntax allow you to quote the thing that precedes the colon? That should prevent vim from thinking it's a label too.
var combo = new Ext.form.ComboBox({
"typeAhead": "foo" // this isn't a jump label
});

create svg text multi-line with one of them is empty line

I am creating the SVG text by using the <text> with <tspan> for each line.
The text has many lines and one of them is empty line, some thing like this:
this is text line 1
this is text line 3
the example above is a text with three lines, one of them is empty.
the problem is the SVG text only displays two lines instead of three lines (the first and the end line, without the middle line).
See here: http://jsfiddle.net/svincoll4/DX4Cn/
Anyone have solution about this to make it display three lines?
Note: I am using the Raphael JS to create these text.
By default whitespace is compressed in html and svg so \n\n\n becomes \n. Also if there's no text at all then the middle line is ignored. xml:space="preserve" stops whitespace compression in SVG and and extra space makes the middle line exist.
var $svg = Raphael('container', 400, 400);
var $text = "this is line 1\n \nthis is line 3";
$svg.canvas.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:space","preserve");
$svg.text(50, 100, $text);

Resources