Skip to content

Commit

Permalink
Add zero width space to long text by export (eclipse-set#1163)
Browse files Browse the repository at this point in the history
* Add zero width space to long text by export

* Fix
  • Loading branch information
TruongQuangSB committed Dec 12, 2024
1 parent 591e5e4 commit 2e9d6da
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.eclipse.set.model.tablemodel.MultiColorCellContent
import org.eclipse.set.model.tablemodel.MultiColorContent
import org.eclipse.set.model.tablemodel.SimpleFootnoteContainer
import org.eclipse.set.model.tablemodel.Table
import org.eclipse.set.model.tablemodel.TableCell
import org.eclipse.set.model.tablemodel.TableContent
import org.eclipse.set.model.tablemodel.TableRow
import org.eclipse.set.model.tablemodel.extensions.TableExtensions.FootnoteInfo
Expand All @@ -35,11 +36,11 @@ import org.w3c.dom.Document
import org.w3c.dom.Element

import static extension org.eclipse.set.model.tablemodel.extensions.CellContentExtensions.*
import static extension org.eclipse.set.model.tablemodel.extensions.ColumnDescriptorExtensions.*
import static extension org.eclipse.set.model.tablemodel.extensions.TableContentExtensions.*
import static extension org.eclipse.set.model.tablemodel.extensions.TableExtensions.*
import static extension org.eclipse.set.model.tablemodel.extensions.TableRowExtensions.*
import static extension org.eclipse.set.utils.StringExtensions.*
import org.eclipse.set.model.tablemodel.extensions.TableExtensions.FootnoteType

/**
* Transformation from {@link Table} to TableDocument {@link Document}.
Expand Down Expand Up @@ -237,8 +238,9 @@ class TableToTableDocument {
var element = doc.createElement("StringContent")
if (isRemarkColumn)
element = doc.createElement("DiffContent")

val stringValue = content.plainStringValue
val columnWidth = content.tableCell.columndescriptor.columnWidth
val stringValue = content.plainStringValue.
intersperseWithZeroSpacesLength(columnWidth.maxCharInCell)
if (isRemarkColumn) {
val child = doc.createElement("UnchangedValue")
element.appendChild(
Expand Down Expand Up @@ -283,14 +285,17 @@ class TableToTableDocument {
private def dispatch Element createContent(CompareCellContent content,
FootnoteContainer fc, int columnNumber, boolean isRemarkColumn) {
val element = doc.createElement("DiffContent")
val columndWidth = content.tableCell.columndescriptor.columnWidth
val maxChar = columndWidth.maxCharInCell
formatCompareContent(
content.oldValue,
content.newValue,
[doc.createElement("OldValue")],
[doc.createElement("UnchangedValue")],
[doc.createElement("NewValue")],
[ text, child |
text.addContentToElement(child, columnNumber, isRemarkColumn)
text.intersperseWithZeroSpacesLength(maxChar).
addContentToElement(child, columnNumber, isRemarkColumn)
]
).forEach[element.appendChild(it)]

Expand Down Expand Up @@ -364,14 +369,20 @@ class TableToTableDocument {

private def Element createMultiColorElement(MultiColorContent content,
int columnNumber, boolean isRemarkColumn) {
val columndWidth = content.tableCell.columndescriptor.columnWidth
val maxChar = columndWidth.maxCharInCell
if (content.multiColorValue === null) {
return content.stringFormat.createContentElement("SimpleValue",
columnNumber, isRemarkColumn)
val cellValue = content.stringFormat.
intersperseWithZeroSpacesLength(maxChar)
return cellValue.createContentElement("SimpleValue", columnNumber,
isRemarkColumn)

}
// IMPROVE: currently the order of multicolor content is static.
// The underlying issue is a limitation in XSL 1.0 and string splitting.
val multiColorElement = content.stringFormat.replace("%s", "").
val multiColorValue = content.stringFormat.
intersperseWithZeroSpacesLength(maxChar)
val multiColorElement = multiColorValue.replace("%s", "").
createContentElement("MultiColorValue", columnNumber,
isRemarkColumn)
multiColorElement.setAttribute("multicolorValue",
Expand Down Expand Up @@ -401,10 +412,8 @@ class TableToTableDocument {
private def Element addContentToElement(String content, Element element,
int columnNumber, boolean isRemarkColumn) {
val checkOutput = content.checkForTestOutput(columnNumber)
element.textContent = if (isRemarkColumn)
checkOutput
else
checkOutput.intersperseWithZeroSpacesSC
element.textContent = isRemarkColumn ? checkOutput : checkOutput.
intersperseWithZeroSpacesSC
return element
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,13 @@ class CellContentExtensions {
*
* @return the table cell of this cell content
*/
static def TableCell getTableCell(CellContent content) {
static def dispatch TableCell getTableCell(CellContent content) {
return content.eContainer as TableCell
}

static def dispatch TableCell getTableCell(MultiColorContent content) {
return content.eContainer.eContainer as TableCell
}

private static def String getTextAlign(CellContent content) {
return content.tableCell.format.textAlignment.literal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,21 @@ class StringExtensions {

static val ENV_PATTERN = Pattern.compile("(?<start>[^$]*)\\$(?<var>[^$]+)\\$(?<end>.*)")
static val ENV_VAR = "var"


/**
* This constant determine manual with OpenSans font
*/
static val int CHARACTER_PRO_CM = 6
static final String ZERO_WIDTH_SPACE = "\u200b"

/**
* @param string this string
*
* @return the string interspersed with "zero spaces"
*/
static def String intersperseWithZeroSpaces(String string) {
return string.replaceAll("(.)", "$1\u200B")
return string.replaceAll("(.)", "$1" + ZERO_WIDTH_SPACE)
}

/**
Expand All @@ -37,7 +44,29 @@ class StringExtensions {
* @return the string interspersed with "zero spaces"
*/
static def String intersperseWithZeroSpacesSC(String string) {
return string.replaceAll("([ /\\-_)}\\]])", "$1\u200B")
return string.replaceAll("([ /\\-_)}\\]])", "$1" + ZERO_WIDTH_SPACE)
}

static def String intersperseWithZeroSpacesLength(String string , int maxChar) {
if (string.length < maxChar) {
return string
}
val result = string.split(" ").map[
if (it.length < maxChar) {
return it
}
val sb = new StringBuilder(it)
val head = sb.subSequence(0, maxChar)
val tail = sb.substring(maxChar)
val newTail = tail.intersperseWithZeroSpacesLength(maxChar)
return head + ZERO_WIDTH_SPACE + newTail

]
return result.join(' ')
}

static def int maxCharInCell(float cellWidth) {
return Math.round(cellWidth * CHARACTER_PRO_CM)
}

/**
Expand Down

0 comments on commit 2e9d6da

Please sign in to comment.