Skip to content

Commit

Permalink
klighd +.piccolo +.piccolo.freehep +.piccolo.test +.ui: introduced op…
Browse files Browse the repository at this point in the history
…tion to enable the generation of 'textLength' property values for '<text>' elements in SVGs (#44)

 * added flag in 'KlighdPaintContext',
 * added checkbox in 'SaveAsImageDiaglog' and related export data records
 * moved some recently misplaced properties from 'SVGOffscreenRenderer' to 'IOffscreenRenderer'
  • Loading branch information
sailingKieler committed Jul 16, 2020
1 parent d21bf90 commit 8c61a44
Show file tree
Hide file tree
Showing 18 changed files with 235 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ public IStatus export(final Control control, final ExportData data) {
// IMPORTANT
super.setGraphicsDelegate(graphicsDelegate);

final KlighdPaintContext paintContext = KlighdPaintContext.createExportDiagramPaintContext(this, false, true);
final KlighdPaintContext paintContext =
KlighdPaintContext.createExportDiagramPaintContext(this, false, true, false);
paintContext.setRenderQuality(PPaintContext.HIGH_QUALITY_RENDERING);

// perform the painting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ public void endGroup() {
graphicsDelegate.endGroup();
}

/**
* {@inheritDoc}
*/
@Override
public void setNextTextLength(double textLength) {
graphicsDelegate.setNextTextLength(textLength);
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,12 @@ else if (ImageConstants.JPG.equalsIgnoreCase(writeAs)) {
.toString()))));
}

private Double nextTextLength = null;

public void setNextTextLength(double textLength) {
this.nextTextLength = Double.valueOf(textLength);
}

/**
* Copied from {@link AbstractVectorGraphicsIO} to change the handling of text as shapes.
*
Expand Down Expand Up @@ -877,6 +883,16 @@ protected void writeString(String str, double x, double y)
resetSemanticData();
}

protected String textLength() {
if (this.nextTextLength == null)
return "";
else {
final float textLength = this.nextTextLength.floatValue();
this.nextTextLength = null;
return " textLength=\"" + textLength + "px\" lengthAdjust=\"spacingAndGlyphs\"";
}
}

/**
* Insert TSpan elements into a multiline text string.
* @param text string where lines are indicated by "\n"
Expand Down Expand Up @@ -915,6 +931,8 @@ private String insertTSpan(final String text) {
content.append(first ? firstLineHeight : lineHeight);
content.append("\"");
content.append(tSpanAttributes(line, i++));
if (lines.length < 2)
content.append(textLength());
content.append(">");
content.append(line);
content.append("</tspan>" + Klighd.LINE_SEPARATOR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,18 @@ public interface KlighdSWTGraphics {
*/
void drawImage(final ImageData imageData, final double width, final double height);

/**
* If supported, add the passed text length value to the text element that is drawn by the next
* call of {@link #drawText(String)}.
*
* For example for an SVG exporter, create the subsequent {@code <text>} or {@code <tspan>}
* element with the {@code textLength} property set to the given value.
*
* @param textLength
* the textLength value to be set.
*/
void setNextTextLength(final double textLength);

/**
* Draws the provided string while respecting the recently set font & text style settings.<br>
* Its position can be determined by means of {@link #setTransform(AffineTransform)}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public abstract class AbstractDiagramExporter {
protected KlighdPaintContext createPaintContext(final KlighdSWTGraphics graphics,
final DiagramExportConfig exportConfig) {
return KlighdPaintContext.createExportDiagramPaintContext(graphics,
exportConfig.applyCameraZoomLevel, exportConfig.exportSemanticData);
exportConfig.applyCameraZoomLevel, exportConfig.exportSemanticData,
exportConfig.setTextLengths);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,14 @@ public void startGroup(KlighdSemanticDiagramData semanticData) {
public void endGroup() {
}

/**
* {@inheritDoc}
*/
@Override
public void setNextTextLength(double textLength) {
// do nothing, to be overridden by implementations
}

/**
* Set if this exporter can handle multiline strings themself or needs fallback.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public IStatus export(final KlighdMainCamera camera, final ExportData data) {
new DiagramExportConfig(data.viewContext(), bounds, tileBounds)
.setBrandingsAndTrim(brandings, diagramTrim, diagramTileTrim)
.setApplyCameraZoomLevel(data.applyCameraZoomLevel())
.setTextLengths(data.setTextLengths())
.setExportSemanticData(true);

// initialize a graphics object that 'collects' all the drawing instructions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import de.cau.cs.kieler.klighd.IDiagramExporter.ExportData;
import de.cau.cs.kieler.klighd.IDiagramExporter.ExportDataBuilder;
import de.cau.cs.kieler.klighd.KlighdConstants;
import de.cau.cs.kieler.klighd.ViewContext;
import de.cau.cs.kieler.klighd.piccolo.KlighdPiccolo;
import de.cau.cs.kieler.klighd.piccolo.internal.nodes.KlighdMainCamera;
Expand All @@ -49,41 +48,32 @@ public class SVGOffscreenRenderer extends AbstractOffscreenRenderer {

/** Property definition for declaring the desired SVG generator. */
public static final IProperty<String> GENERATOR = new Property<String>(
"de.cau.cs.kieler.klighd.piccolo.svg.generator",
GENERATOR_SVG_FREEHEP_EXTENDED);

/** Property definition for defining transparent diagram background. */
public static final IProperty<Boolean> SVG_EXPORT_TRANSPARENT_BACKGROUND = new Property<Boolean>(
"de.cau.cs.kieler.klighd.svgExport.transparentBackground", false);

/** Property definition for defining the diagram background color. */
public static final IProperty<RGB> SVG_EXPORT_BACKGROUND_COLOR = new Property<RGB>(
"de.cau.cs.kieler.klighd.svgExport.backgroundColor", KlighdConstants.WHITE);
"de.cau.cs.kieler.klighd.piccolo.svg.generator", GENERATOR_SVG_FREEHEP_EXTENDED);

/**
* {@inheritDoc}
*/
public IStatus render(final ViewContext viewContext, final OutputStream output,
final IPropertyHolder properties) {

final RGB backgroundColor = properties != null
? properties.getProperty(BACKGROUND_COLOR) : BACKGROUND_COLOR.getDefault();
final boolean transparentBackground = properties != null
? properties.getProperty(TRANSPARENT_BACKGROUND) : TRANSPARENT_BACKGROUND.getDefault();
final boolean textAsShapes = properties != null
? properties.getProperty(TEXT_AS_SHAPES) : TEXT_AS_SHAPES.getDefault();
final boolean embedFonts = properties != null
? properties.getProperty(EMBED_FONTS) : EMBED_FONTS.getDefault();
final boolean setTextLengths = properties != null
? properties.getProperty(SET_TEXT_LENGTHS) : SET_TEXT_LENGTHS.getDefault();
final String generator = properties != null
? properties.getProperty(GENERATOR) : GENERATOR.getDefault();
final String description = properties != null
? properties.getProperty(DESCRIPTION) : DESCRIPTION.getDefault();
final String additionalRootData = properties != null
? properties.getProperty(ADDITIONAL_ROOT_DATA) : ADDITIONAL_ROOT_DATA.getDefault();
final String additionalRootData = properties != null
? properties.getProperty(ADDITIONAL_ROOT_DATA) : ADDITIONAL_ROOT_DATA.getDefault();
final String css = properties != null
? properties.getProperty(CSS) : CSS.getDefault();
final boolean transparentBackground =
properties != null ? properties.getProperty(SVG_EXPORT_TRANSPARENT_BACKGROUND)
: SVG_EXPORT_TRANSPARENT_BACKGROUND.getDefault();
final RGB backgroundColor =
properties != null ? properties.getProperty(SVG_EXPORT_BACKGROUND_COLOR)
: SVG_EXPORT_BACKGROUND_COLOR.getDefault();

// Construct a KLighD main camera ...
// (the basic PRoot is sufficient here, as this canvas doesn't rely on any SWT stuff)
Expand All @@ -99,11 +89,12 @@ public IStatus render(final ViewContext viewContext, final OutputStream output,

try {
ExportData data = new ExportDataBuilder(viewContext, generator, output)
.textAsShapes(textAsShapes)
.embedFonts(embedFonts)
.description(description)
.backgroundColor(backgroundColor)
.transparentBackground(transparentBackground)
.textAsShapes(textAsShapes)
.embedFonts(embedFonts)
.setTextLengths(setTextLengths)
.additionalRootData(additionalRootData)
.css(css)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1364,4 +1364,9 @@ public void startGroup(final KlighdSemanticDiagramData semanticData) {

public void endGroup() {
}
/* --------------------- TextLength ---------------------*/

// unsupported for the SWT graphics
public void setNextTextLength(double textLength) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,10 @@ protected void paint(final KlighdPaintContext kpc) {

addSemanticData(kpc);

if (kpc.isSetTextLengths()) {
graphics.setNextTextLength(this.getWidth());
}

graphics.drawText(text);

graphics.setAlpha(currentAlpha);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class KlighdPaintContext extends PPaintContext {
* @return the desired {@link KlighdPaintContext}
*/
public static KlighdPaintContext createDiagramPaintContext(final KlighdSWTGraphics graphics) {
return new KlighdPaintContext(graphics, false, false, false, true, false);
return new KlighdPaintContext(graphics, false, false, false, true, false, false);
}

/**
Expand All @@ -54,7 +54,7 @@ public static KlighdPaintContext createDiagramPaintContext(final KlighdSWTGraphi
* @return the desired {@link KlighdPaintContext}
*/
public static KlighdPaintContext createOutlinePaintContext(final KlighdSWTGraphics graphics) {
return new KlighdPaintContext(graphics, true, false, false, true, false);
return new KlighdPaintContext(graphics, true, false, false, true, false, false);
}

/**
Expand All @@ -68,11 +68,19 @@ public static KlighdPaintContext createOutlinePaintContext(final KlighdSWTGraphi
* {@link KlighdMainCamera}'s view transform will be used while evaluating the
* visibility of the particular diagram elements and diagram element figure parts, if
* <code>false</code> a diagram zoom level of <code>1.0<code> is assumed.
* @param addSemanticData
* flag determining whether semantic data shall be added to the diagram while
* exporting an SVG based image, should be <code>false</code> in other cases (no effect)
* @param setTextLength
* flag determining whether the expected text length shall be added to the diagram while
* exporting an SVG based image, should be <code>false</code> in other cases (no effect)
* @return the desired {@link KlighdPaintContext}
*/
public static KlighdPaintContext createExportDiagramPaintContext(
final KlighdSWTGraphics graphics, boolean applyCameraZoomLevel, boolean addSemanticData) {
return new KlighdPaintContext(graphics, false, true, false, applyCameraZoomLevel, addSemanticData);
final KlighdSWTGraphics graphics, boolean applyCameraZoomLevel, boolean addSemanticData,
boolean setTextLength) {
return new KlighdPaintContext(graphics, false, true, false, applyCameraZoomLevel,
addSemanticData, setTextLength);
}

/**
Expand All @@ -89,7 +97,7 @@ public static KlighdPaintContext createExportDiagramPaintContext(
*/
public static KlighdPaintContext createPrintoutPaintContext(final KlighdSWTGraphics graphics,
boolean applyCameraZoomLevel) {
return new KlighdPaintContext(graphics, false, false, true, applyCameraZoomLevel, false);
return new KlighdPaintContext(graphics, false, false, true, applyCameraZoomLevel, false, false);
}


Expand All @@ -110,19 +118,25 @@ public static KlighdPaintContext createPrintoutPaintContext(final KlighdSWTGraph
* visibility of the particular diagram elements and diagram element figure parts, if
* <code>false</code> a diagram zoom level of <code>1.0<code> is assumed.
* @param addSemanticData
* flag determining whether semantic data shall be added to the diagram, e.g. while
* exporting an SVG based image
* flag determining whether semantic data shall be added to the diagram while
* exporting an SVG based image, should be <code>false</code> in other cases (no
* effect)
* @param setTextLengths
* flag determining whether the expected text length values shall be added to the
* diagram while exporting an SVG based image, should be <code>false</code> in other
* cases (no effect)
*/
protected KlighdPaintContext(final KlighdSWTGraphics graphics, final boolean outline,
final boolean export, final boolean printout, boolean applyCameraZoomLevel,
final boolean addSemanticData) {
final boolean addSemanticData, final boolean setTextLengths) {
super((Graphics2D) graphics);
this.mainDiagram = !(outline || export || printout);
this.outline = outline;
this.export = export;
this.printout = printout;
this.applyCameraZoomLevel = applyCameraZoomLevel;
this.addSemanticData = addSemanticData;
this.setTextLengths = setTextLengths;
}

private double cameraZoomScale = 1d;
Expand All @@ -132,6 +146,7 @@ protected KlighdPaintContext(final KlighdSWTGraphics graphics, final boolean out
private final boolean printout;
private final boolean applyCameraZoomLevel;
private final boolean addSemanticData;
private final boolean setTextLengths;

private final Stack<Double> cameraScales = new Stack<Double>();

Expand Down Expand Up @@ -199,6 +214,17 @@ public boolean isAddSemanticData() {
return this.addSemanticData;
}

/**
* Returns <code>true</code> if the expected text length values shall be added to the diagram
* while exporting an SVG based image, should be <code>false</code> in other cases (no effect).
*
* @return <code>true</code> if the expected text length values shall be added to the diagram
* while exporting an SVG based image, <code>false</code> otherwise.
*/
public boolean isSetTextLengths() {
return this.setTextLengths;
}

/**
* @return the employed {@link KlighdSWTGraphics} (delegates to {@link #getGraphics()} and casts
* accordingly)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ public class Messages extends NLS {
public static String SaveAsImageDialog_title;
public static String SaveAsImageDialog_use_camera_viewport_caption;
public static String SaveAsImageDialog_apply_camera_zoom_level_caption;
public static String SaveAsImageDialog_transparent_background;
public static String SaveAsImageDialog_text_as_shapes;
public static String SaveAsImageDialog_embed_fonts;
public static String SaveAsImageDialog_transparent_background;
public static String SaveAsImageDialog_set_textLength;
public static String SaveAsImageDialog_tiling;
public static String SaveAsImageDialog_tiling_options_caption;

Expand Down
Loading

0 comments on commit 8c61a44

Please sign in to comment.