MTextStyleParse.java 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package com.fdkankan.dxf.parse.utils;
  2. import com.fdkankan.dxf.generate.util.StreamUtil;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.alibaba.fastjson.JSONReader;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.nio.charset.StandardCharsets;
  10. /**
  11. * @Author ytzjj
  12. * @DateTime 2023/11/24 10:05
  13. * @Description 多文本样式解析
  14. */
  15. public class MTextStyleParse {
  16. /**
  17. * cad默认颜色ACI编码
  18. */
  19. private static final String DEFAULT_COLOR_ACI_CODE = "7";
  20. /**
  21. * cad默认颜色与16进制颜色转换文件
  22. */
  23. private static final String DEFAULT_COLOR_JSON_FILE_PATH = "dxf/color.json";
  24. /**
  25. * CAD颜色列表
  26. */
  27. private static JSONArray colorReflection = null;
  28. static {
  29. try (
  30. InputStream resourceStream = StreamUtil.getResourceStream(DEFAULT_COLOR_JSON_FILE_PATH);
  31. JSONReader reader = new JSONReader(new InputStreamReader(resourceStream, StandardCharsets.UTF_8));
  32. ) {
  33. colorReflection = reader.readObject(JSONArray.class);
  34. } catch (IOException e) {
  35. throw new RuntimeException(e);
  36. }
  37. }
  38. /**
  39. * 解析CAD ACI色号为16进制颜色标识
  40. *
  41. * @param cadCode
  42. * @return
  43. */
  44. public static String parseCadColorToHexColorCode(String cadCode) {
  45. String defaultHexColor = "";
  46. for (int i = 0; i < colorReflection.size(); i++) {
  47. JSONObject json = colorReflection.getJSONObject(i);
  48. if (json.getString("code").equals(cadCode)) {
  49. defaultHexColor = json.getString("color");
  50. if (defaultHexColor.startsWith("#")) {
  51. defaultHexColor = defaultHexColor.substring(1);
  52. }
  53. // 添加透明度信息,默认为不透明(FF)
  54. defaultHexColor = (defaultHexColor + "FF").toUpperCase();
  55. } else if (json.getString("code").equals(DEFAULT_COLOR_ACI_CODE)) {
  56. defaultHexColor = json.getString("color");
  57. if (defaultHexColor.startsWith("#")) {
  58. defaultHexColor = defaultHexColor.substring(1);
  59. }
  60. // 添加透明度信息,默认为不透明(FF)
  61. defaultHexColor = (defaultHexColor + "FF").toUpperCase();
  62. }
  63. }
  64. return defaultHexColor;
  65. }
  66. }