VmsFormObject.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package com.its.vms.domain;
  2. import com.its.vms.domain.enums.eVmsTextAlign;
  3. import com.its.vms.dto.TbVmsFormObjectDto;
  4. import com.its.vms.service.VmsFontService;
  5. import com.its.vms.service.VmsFormService;
  6. import com.its.vms.xnettcp.vms.protocol.enums.eVmsFormObjectKind;
  7. import com.its.vms.xnettcp.vms.protocol.enums.eVmsImageType;
  8. import lombok.Data;
  9. import lombok.extern.slf4j.Slf4j;
  10. import javax.imageio.ImageIO;
  11. import java.awt.*;
  12. import java.awt.geom.Rectangle2D;
  13. import java.io.ByteArrayInputStream;
  14. import java.io.IOException;
  15. import java.io.Serializable;
  16. /**
  17. * DTO Class
  18. */
  19. @Slf4j
  20. @Data
  21. public class VmsFormObject implements Serializable {
  22. private static final long serialVersionUID = 1L;
  23. private Integer vmsFormId;
  24. private Integer vmsFormObjectId;
  25. private eVmsFormObjectKind objectKind; // 객체종류
  26. private int objectType; // 객체타입
  27. private int objectSize; // 객체크기
  28. private int blinking; // 점멸 여부
  29. private int posX; // 위치 X
  30. private int posY; // 위치 Y
  31. private int dsplWidth; // 객체넓이
  32. private int textWidth; // 객체넓이
  33. private int dsplHeight; // 객체높이
  34. private int bkColor; // 객체배경색상
  35. private int fontNameCd; // 폰트이름
  36. private String fontName;
  37. private int fontColor; // 폰트색상
  38. private int fontSize; // 폰트크기
  39. private int fontBold; // 폰트굵기
  40. private int textAlign; // 문자열정열방식
  41. private String textData; // 문자열데이터
  42. private String memSymbLibNmbr; // 이미지아이디
  43. private Long ifscId;
  44. private int ifscTrafGradCd;
  45. private String trfcFillCd;
  46. private byte[] imageData;
  47. private Color fontClr;
  48. private eVmsImageType bitmapType;
  49. private int bitmapId;
  50. public VmsFormObject(Integer vmsFormId, Integer vmsFormObjectId) {
  51. this.objectKind = eVmsFormObjectKind.OBJECT_TEXT;
  52. this.vmsFormId = vmsFormId;
  53. this.vmsFormObjectId = vmsFormObjectId;
  54. this.ifscId = 0L;
  55. this.imageData = null;
  56. this.fontClr = Color.BLACK;
  57. this.bitmapId = 0;
  58. }
  59. public void setFormObjectInfo(String fontName, TbVmsFormObjectDto obj) {
  60. this.ifscTrafGradCd = 0;
  61. this.ifscId = 0L;
  62. this.trfcFillCd = obj.getTrfcFillCd();
  63. this.fontName = fontName;
  64. this.objectType = obj.getVmsFormObjectTypeCd(); // NUMBER(3) N VMS FORM OBJECT 유형 코드
  65. this.fontNameCd = obj.getVmsFontNameCd(); // NUMBER(3) N VMS 글꼴 유형 코드
  66. this.fontColor = obj.getVmsFontColrCd(); // NUMBER(3) N VMS 글꼴 색상 코드
  67. this.fontBold = obj.getVmsFontBold(); // NUMBER(3) Y VMS 글꼴 방식 코드(0:Normal,1:Bold)
  68. this.fontSize = obj.getVmsFontSize(); // NUMBER(3) Y 0 VMS 글꼴 크기
  69. this.textAlign = obj.getVmsFontAlign(); // NUMBER(1) Y 0 VMS 표출 문자열정열방식(0:LEFT,1:RIGHT,2:CENTER)
  70. this.posX = obj.getVmsDsplXcrdn(); // NUMBER(5) Y VMS 표출 X좌표
  71. this.posY = obj.getVmsDsplYcrdn(); // NUMBER(5) Y VMS 표출 Y좌표
  72. this.dsplWidth = obj.getVmsDsplWidth(); // NUMBER(5) Y 0 VMS 표출 넓이
  73. this.textWidth = obj.getVmsDsplWidth(); // NUMBER(5) Y 0 VMS 표출 넓이
  74. this.dsplHeight = obj.getVmsDsplHeight(); // NUMBER(5) Y 0 VMS 표출 높이
  75. this.blinking = obj.getVmsDsplBlinking(); // NUMBER(1) Y 0 VMS 표출 점멸 여부(0:지속, 1:점멸)
  76. this.bkColor = obj.getVmsDsplBkColor(); // NUMBER(1) Y 0 VMS 표출 배경색상코드
  77. this.objectSize = obj.getVmsDsplSize(); // NUMBER(7) Y 0 VMS 표출 크기(문자:문자길이,이미지:이미지전체크기)
  78. this.textData = obj.getVmsDsplTxt();
  79. this.memSymbLibNmbr = obj.getSymbLibNmbr() + "0";
  80. this.bitmapType = obj.getBitmapType();
  81. this.bitmapId = obj.getBitmapId();
  82. if (VmsFormService.isObjectBitmapId(this.objectType)) {
  83. this.objectKind = eVmsFormObjectKind.OBJECT_BITMAP_ID;
  84. }
  85. else if (obj.getVmsFormObjectTypeCd() == 3) {
  86. // 소통정보배경 이미지
  87. this.objectKind = eVmsFormObjectKind.OBJECT_BITMAP;
  88. }
  89. else {
  90. this.objectKind = eVmsFormObjectKind.OBJECT_TEXT;
  91. }
  92. }
  93. /**
  94. * 문자열 객체의 위치좌표 X 를 조정한다.
  95. */
  96. public void changeTextPosition(float fontSizeRatio) {
  97. if ("".equals(this.textData)) {
  98. // 표출데이터가 없는 경우
  99. return;
  100. }
  101. // 문자열정열방식
  102. // typedef enum en_text_align
  103. // {
  104. // text_align_left, /* 0:LEFT */
  105. // text_align_right, /* 1:RIGHT */
  106. // text_align_center, /* 2:CENTER */
  107. // } EN_TEXT_ALIGN;
  108. int style = this.fontBold == 0 ? Font.PLAIN : Font.BOLD;
  109. Font font = new Font(this.fontName, style, this.fontSize);
  110. font = font.deriveFont(this.fontSize * fontSizeRatio);//1.35f);
  111. // FontRenderContext frc = new FontRenderContext(null, true, true);
  112. // Rectangle2D r2 = font.getStringBounds(this.textData, frc);
  113. // int textWidth = (int)Math.round(r2.getWidth());
  114. FontMetrics metrics = new FontMetrics(font){};
  115. Rectangle2D bounds = metrics.getStringBounds(this.textData, null);
  116. int textWidth = (int) bounds.getWidth();
  117. //int correctWidth = this.dsplWidth - textWidth;
  118. if (this.textAlign == eVmsTextAlign.TEXT_ALIGN_RIGHT.getValue()) {
  119. // 우측정렬: 좌측에서 글자길이를 뺀다음 만약 음수이면 0으로 설정
  120. this.posX = (this.posX + this.dsplWidth) - textWidth;
  121. //this.posX += correctWidth;
  122. }
  123. else if (this.textAlign == eVmsTextAlign.TEXT_ALIGN_CENTER.getValue()) {
  124. // 가운데정렬: 원래 글자의 중앙값을 얻은 다음에 조정된 글자의 1/2 크기를 뺀다음 음수이면 0으로 설정
  125. this.posX = (this.posX+(this.dsplWidth/2)) - (textWidth/2);
  126. //this.posX += (correctWidth / 2);
  127. }
  128. // 좌측정령은 정렬할 필요가 없음, 단 가변문자인 경우 문자길이를 다시 계산해주어야 함....
  129. if (this.posX < 0) {
  130. this.posX = 0;
  131. }
  132. this.textWidth = textWidth;
  133. this.dsplWidth = this.textWidth;
  134. }
  135. /**
  136. * 이미지 바이트배열을 Image 객체로 리턴한다.
  137. * @return
  138. */
  139. private Image getImage() {
  140. if (this.imageData == null || this.imageData.length == 0) {
  141. log.error("VmsFormObject.getImage: Image Data null: {}, {}, [{}]", this.vmsFormId, this.vmsFormObjectId, this.memSymbLibNmbr);
  142. return null;
  143. }
  144. try (
  145. ByteArrayInputStream bis = new ByteArrayInputStream(this.imageData);
  146. ) {
  147. return ImageIO.read(bis);
  148. } catch (IOException e) {
  149. log.error("VmsFormObject.getImage: IOException: {}, {}, {}", this.vmsFormId, this.vmsFormObjectId, e.getMessage());
  150. }
  151. return null;
  152. }
  153. /**
  154. * 폼 이미지에 객체 정보를 그린다.
  155. *
  156. * @param g2d
  157. * @param fontService
  158. */
  159. public void drawObject(Graphics2D g2d, VmsFormService formService, VmsFontService fontService, int vmsFormColrCd, float fontSizeRatio) {
  160. if (VmsFormService.isObjectBitmap(this.objectType)) {
  161. Image imgBmp = getImage();
  162. if (imgBmp != null) {
  163. // g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
  164. // g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  165. // g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_DEFAULT);
  166. g2d.drawImage(imgBmp, this.posX, this.posY, this.dsplWidth, this.dsplHeight, null);
  167. }
  168. }
  169. else {
  170. //기타 문자열
  171. Font txtFont = new Font(this.fontName, (this.fontBold == 1) ? Font.BOLD : Font.PLAIN, this.fontSize);
  172. txtFont = txtFont.deriveFont(this.fontSize * fontSizeRatio);
  173. FontMetrics fm = g2d.getFontMetrics(txtFont);
  174. if (vmsFormColrCd != this.bkColor) {
  175. Rectangle2D rect = fm.getStringBounds(this.textData, g2d);
  176. g2d.setColor(formService.getFormColor(this.bkColor));
  177. g2d.fillRect(this.posX, this.posY, (int) rect.getWidth(), (int) rect.getHeight());
  178. }
  179. g2d.setColor(fontService.getFontColor(this.fontColor));
  180. g2d.setFont(txtFont);
  181. g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
  182. g2d.drawString(this.textData, this.posX, this.posY + fm.getAscent());
  183. }
  184. this.fontClr = fontService.getFontColor(this.fontColor);
  185. }
  186. }