package com.ximple.io.dgn7; //~--- non-JDK imports -------------------------------------------------------- import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.Envelope; import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.GeometryFactory; /** * TextElement * * @author Ulysses * @version 0.1 * @since 2006/5/18 上午 11:45:29 */ public class TextElement extends Element implements GeometryConverter { public static final int ED_CENTERJUSTIFICATION = 0; // Enter data field center justification public static final int ED_LEFTJUSTIFICATION = 0; // Enter data field left justification public static final int ED_RIGHTJUSTIFICATION = 0; // Enter data field right justification public static final int TXTJUST_CB = 0; // Center/bottom text justification. public static final int TXTJUST_CC = 0; // Center/center text justification. public static final int TXTJUST_CT = 0; // Center/top text justification. public static final int TXTJUST_LB = 0; // Left/bottom text justification. public static final int TXTJUST_LC = 0; // Left/center text justification. public static final int TXTJUST_LT = 0; // Left/top text justification. public static final int TXTJUST_RB = 0; // Right/bottom text justification. public static final int TXTJUST_RC = 0; // Right/center text justification. public static final int TXTJUST_RT = 0; public TextElement(byte[] raw) { super(raw); } public Coordinate getOrigin() { int x = (raw[25] << 16 & 0xffff0000); x += raw[26] & 0x0000ffff; double dx = Utility.converUnitToCoord(x); int y = (raw[27] << 16 & 0xffff0000); y += raw[28] & 0x0000ffff; double dy = Utility.converUnitToCoord(y); return new Coordinate(dx, dy); } public Coordinate getUserOrigin() { Coordinate origin = getOrigin(); double x = origin.x; double weight = getUserSetWeight(); double height = getUserSetHeight(); double angle = Utility.converRotationToRadian(getRotationAngle()); x += weight * Math.cos(angle) - height * Math.sin(angle); double y = origin.y; y += weight * Math.cos(angle) - height * Math.sin(angle); return new Coordinate(x, y); } private double getUserSetWeight() { int just = getJustification(); Envelope range = getRange(); double weight = (range.getWidth()); switch (just) { case 0: case 1: case 2: weight = 0; break; case 6: case 7: case 8: weight = weight / 2; break; case 12: case 13: case 14: break; } return weight; } private double getUserSetHeight() { int just = getJustification(); double height = getTextHeight(); switch (just) { case 2: case 8: case 14: // bottom height = 0; break; case 1: case 7: case 13: // center height = height / 2; break; case 0: case 6: case 12: // height break; } return height; } public int getFontIndex() { return (int) raw[18] & 0x00000000ff; } public boolean hasSlant() { return true; } public boolean hasUnderline() { return true; } public boolean isFixedWidthSpacing() { return true; } public boolean isPlanar() { return true; } public boolean isVertical() { return true; } public double getTextHeight() { int height = ((raw[21] << 16) & 0xffff0000); height += raw[22] & 0x0000ffff; return Utility.converIntToDouble(height); } public double getTextWidth() { int length = (raw[19] << 16 & 0xffff0000); length += raw[20] & 0x0000ffff; return Utility.converIntToDouble(length); } public int getJustification() { return ((raw[18] >>> 8) & 0x00000000ff); } public double getRotationAngle() { int totation = ((raw[23] << 16) & 0xffff0000); totation += raw[24] & 0x0000ffff; return Utility.converIntToRotation(totation); } public boolean isChinese() { int isChinese = raw[30] & 0x0000ffff; return (isChinese == 0xfdff); } public int getTextLength() { int num = raw[29]; if (isChinese()) { num = (num / 2) - 1; } return num; } public String getText() { StringBuffer val = new StringBuffer(); char[] temp; int num = getTextLength(); if (!isChinese()) { temp = new char[num]; for (int i = 0; i < temp.length; i++) { if ((i % 2) == 0) { temp[i] = (char) (raw[30 + (int) (i / 2)] & (short) 0x00ff); } else { temp[i] = (char) ((raw[30 + (int) (i / 2)] >> 8) & (short) 0x00ff); } val.append(temp[i]); } } return val.toString(); } public Geometry toGeometry(GeometryFactory factory) { return factory.createPoint(getUserOrigin()); } public static class ElementHandler extends Element.ElementHandler { private static ElementHandler instance = null; public ElementHandler() { super(ElementType.TEXT); } public static IElementHandler getInstance() { if (instance == null) { instance = new ElementHandler(); } return instance; } protected Element createElement(byte[] raw) { return new TextElement(raw); } } }