You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Context.java 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.sl.draw.geom;
  20. import java.awt.geom.Rectangle2D;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import java.util.regex.Pattern;
  24. public class Context {
  25. private static final Pattern DOUBLE_PATTERN = Pattern.compile(
  26. "[\\x00-\\x20]*[+-]?(NaN|Infinity|((((\\p{Digit}+)(\\.)?((\\p{Digit}+)?)" +
  27. "([eE][+-]?(\\p{Digit}+))?)|(\\.(\\p{Digit}+)([eE][+-]?(\\p{Digit}+))?)|" +
  28. "(((0[xX](\\p{XDigit}+)(\\.)?)|(0[xX](\\p{XDigit}+)?(\\.)(\\p{XDigit}+)))" +
  29. "[pP][+-]?(\\p{Digit}+)))[fFdD]?))[\\x00-\\x20]*");
  30. private final Map<String, Double> _ctx = new HashMap<>();
  31. private final IAdjustableShape _props;
  32. private final Rectangle2D _anchor;
  33. public Context(CustomGeometry geom, Rectangle2D anchor, IAdjustableShape props){
  34. _props = props;
  35. _anchor = anchor;
  36. for(Guide gd : geom.adjusts) {
  37. evaluate(gd);
  38. }
  39. for(Guide gd : geom.guides) {
  40. evaluate(gd);
  41. }
  42. }
  43. Rectangle2D getShapeAnchor(){
  44. return _anchor;
  45. }
  46. Guide getAdjustValue(String name){
  47. // ignore HSLF props for now ... the results with default value are usually better - see #59004
  48. return (_props.getClass().getName().contains("hslf")) ? null : _props.getAdjustValue(name);
  49. }
  50. public double getValue(String key){
  51. if(DOUBLE_PATTERN.matcher(key).matches()){
  52. return Double.parseDouble(key);
  53. }
  54. // BuiltInGuide throws IllegalArgumentException if key is not defined
  55. return _ctx.containsKey(key) ? _ctx.get(key) : evaluate(BuiltInGuide.valueOf("_"+key));
  56. }
  57. public double evaluate(Formula fmla){
  58. double result = fmla.evaluate(this);
  59. if (fmla instanceof Guide) {
  60. String key = ((Guide)fmla).getName();
  61. if (key != null) {
  62. _ctx.put(key, result);
  63. }
  64. }
  65. return result;
  66. }
  67. }