2012-08-28 26 views
5

我試圖圍繞文本元素使用getBBox(),繪製一個矩形代碼非常簡單,創建文檔,添加文本元素,引導文檔和添加矩形。 問題是,getBBox正在返回一些奇怪的值,我錯過了什麼?使用蠟染的文本元素的邊界框錯誤的值

DOMImplementation impl; 
String svgNS; 
SVGDocument doc; 
Element svgRoot; 

UserAgent userAgent; 
DocumentLoader loader; 
BridgeContext ctx; 
GVTBuilder builder; 
GraphicsNode rootGN; 

... 

// get a DOM and create a document 
impl = SVGDOMImplementation.getDOMImplementation(); 
svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI; 
doc = (SVGDocument)impl.createDocument(svgNS, "svg", null); 

// Get (the 'svg' element). 
svgRoot = doc.getDocumentElement(); 

// Set the width and height attributes on the root 'svg' element. 
svgRoot.setAttributeNS(svgNS, "width", "400"); 
svgRoot.setAttributeNS(svgNS, "height", "450"); 

// Add a text element 
Element txtElem = doc.createElementNS(svgNS, "text"); 
txtElem.setAttributeNS(svgNS, "x", "30"); 
txtElem.setAttributeNS(svgNS, "y", "50"); 
txtElem.setAttributeNS(svgNS, "style", "font-family:Arial;font-size:20;stroke:#000000;#fill:#00ff00;"); 
txtElem.setTextContent("sometext"); 

svgRoot.appendChild(txtElem); 

// boot the document 
userAgent = new UserAgentAdapter(); 
loader = new DocumentLoader(userAgent); 
ctx = new BridgeContext(userAgent, loader); 
ctx.setDynamicState(BridgeContext.DYNAMIC); 
builder = new GVTBuilder(); 
rootGN = builder.build(ctx, doc); 

// add a bounding box to text elements 
NodeList nodelist = doc.getElementsByTagName("text"); 
for (int i=0; i < nodelist.getLength(); i++) { 
    SVGOMTextElement textElem = (SVGOMTextElement)nodelist.item(i); 
    SVGRect bbox = textElem.getBBox(); 

    Element rectangle = doc.createElementNS(svgNS, "rect"); 
    rectangle.setAttributeNS(svgNS, "x", new Float(bbox.getX()).toString()); 
    rectangle.setAttributeNS(svgNS, "y", new Float(bbox.getY()).toString()); 
    rectangle.setAttributeNS(svgNS, "width", new Float(bbox.getWidth()).toString()); 
    rectangle.setAttributeNS(svgNS, "height", new Float(bbox.getHeight()).toString()); 
    rectangle.setAttributeNS(svgNS, "fill", "rgba(0,0,0,0)"); 
    rectangle.setAttributeNS(svgNS, "stroke", "#ff0000"); 
    rectangle.setAttributeNS(svgNS, "stroke-width", "1"); 

    svgRoot.appendChild(rectangle); 
} 

這是我得到的SVG文件,你可以看到長方形有不同的X和Y值(寬度和高度也錯了)。

<?xml version="1.0" encoding="UTF-8"?> 
<svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" contentScriptType="text/ecmascript" zoomAndPan="magnify" width="400" contentStyleType="text/css" preserveAspectRatio="xMidYMid meet" height="450" version="1.0"> 
    <text x="30" style="font-family:Georgia;font-size:20;stroke:#000000;#fill:#00ff00;" y="50">sometext</text> 
    <rect x="0.65234375" fill="rgba(0,0,0,0)" width="55.621094" stroke-width="1" stroke="#ff0000" height="8.597656" y="8.425781"/> 
</svg> 
+0

你還可以發佈添加矩形的代碼嗎? –

+0

矩形添加代碼在for循環中,在文檔啓動之後。不知道這是否是正確的做法。 –

+0

沒關係,我沒有向下滾動,看不到for循環... –

回答

4

的問題來自於事實,你必須使用setAttribute代替setAttributeNS。指定的xy座標被忽略,因爲它們不是Batik在默認名稱空間中預期的xy屬性,它們似乎被識別爲其他未知屬性。只要使用此:

txtElem.setAttribute("x", "30"); 
txtElem.setAttribute("y", "50"); 
txtElem.setAttribute("style", "font-family:Arial;font-size:20;stroke:#000000;fill:#00ff00;"); 
4

試試這個:

rectangle.setAttributeNS(null, 

只有在createElementNS離開svgNS

+1

我不知道爲什麼這個答案收到downvote。在官方的蠟染常見問題解答中,'setAttributeNS(null,...)'甚至被聲明爲更好的方法(而不是'setAttribute(...)'):http://xmlgraphics.apache.org/batik/faq.html (參見問題3.4:「當我通過DOM API創建新的SVG元素或修改某些SVG屬性時,沒有任何反應,所做的更改不會呈現,爲什麼不呢?」) – ComFreek