public Object convert(CSSValue value, CSSEngine engine, Object context) throws Exception {
   if (value.getCssValueType() == CSSValue.CSS_PRIMITIVE_VALUE) {
     CSSPrimitiveValue primitiveValue = (CSSPrimitiveValue) value;
     if ("true".equals(primitiveValue.getStringValue())) return Boolean.TRUE;
   }
   return Boolean.FALSE;
 }
Exemplo n.º 2
0
  public PShading(CSSValue value) {

    CTShd shd = Context.getWmlObjectFactory().createCTShd();

    // PrimitiveType 25 -> RGBCOLOR
    short ignored = 1;

    float fRed;
    float fGreen;
    float fBlue;

    CSSPrimitiveValue cssPrimitiveValue = (CSSPrimitiveValue) value;
    try {
      fRed = cssPrimitiveValue.getRGBColorValue().getRed().getFloatValue(ignored);
      fGreen = cssPrimitiveValue.getRGBColorValue().getGreen().getFloatValue(ignored);
      fBlue = cssPrimitiveValue.getRGBColorValue().getBlue().getFloatValue(ignored);
      shd.setFill(UnitsOfMeasurement.rgbTripleToHex(fRed, fGreen, fBlue));

    } catch (UnsupportedOperationException e) {

      try {
        Class<?> xhtmlImporterClass =
            Class.forName("org.docx4j.convert.in.xhtml.FSColorToHexString");
        Method rgbToHexMethod = xhtmlImporterClass.getMethod("rgbToHex", CSSPrimitiveValue.class);
        shd.setFill((String) rgbToHexMethod.invoke(null, cssPrimitiveValue));
      } catch (Exception e2) {
        log.error("docx4j-XHTMLImport jar not found. Please add this to your classpath.");
        log.error(e2.getMessage(), e2);
        throw e; // same as before
      }
    }

    this.setObject(shd);
  }
  public static int getTwip(CSSPrimitiveValue cssPrimitiveValue) {

    short ignored = 1;
    float fVal = cssPrimitiveValue.getFloatValue(ignored); // unit type ignored in cssparser
    log.debug("margin-left: " + fVal);
    if (fVal == 0f) {
      return 0;
    }

    int twip;

    short type = cssPrimitiveValue.getPrimitiveType();

    // TODO
    //		CSSPrimitiveValue.CSS_EMS (Unit 3)
    //		(the 'font-size' of the relevant font)
    //
    //		CSSPrimitiveValue.CSS_EMX (Unit 4)
    //		(the 'x-height' of the relevant font)

    if (CSSPrimitiveValue.CSS_PX == type) {
      // Unit 5
      twip = UnitsOfMeasurement.pxToTwip(fVal);
    } else if (CSSPrimitiveValue.CSS_CM == type) {
      // Unit 6
      twip = UnitsOfMeasurement.mmToTwip(fVal * 10);
    } else if (CSSPrimitiveValue.CSS_MM == type) {
      // Unit 7
      twip = UnitsOfMeasurement.mmToTwip(fVal);
    } else if (CSSPrimitiveValue.CSS_IN == type) {
      // Unit 8
      twip = UnitsOfMeasurement.inchToTwip(fVal);
    } else if (CSSPrimitiveValue.CSS_PT == type) {
      // Unit 9
      twip = UnitsOfMeasurement.pointToTwip(fVal);
    } else if (CSSPrimitiveValue.CSS_NUMBER == type) {
      log.error("Indent: No support for unspecified unit: CSS_NUMBER ");
      // http://stackoverflow.com/questions/11479985/what-is-the-default-unit-for-margin-left
      /*
       * In quirks mode (without a doctype), most browsers will try to correct the code by
       * using the unit px. In standards compliance mode (with a proper doctype), most browsers
       * will ignore the style.
       **/
      twip = 0; // TODO: should throw UnsupportedUnitException?
    } else {
      log.error("Indent: No support for unit " + type);
      twip = 0;
    }

    return twip;
  }
 @Override
 public void applyCSSProperty(Object element, CSSValue value, String pseudo, CSSEngine engine)
     throws Exception {
   if (value.getCssValueType() == CSSValue.CSS_PRIMITIVE_VALUE) {
     CSSPrimitiveValue primitiveValue = (CSSPrimitiveValue) value;
     short type = primitiveValue.getPrimitiveType();
     switch (type) {
       case CSSPrimitiveValue.CSS_IDENT:
       case CSSPrimitiveValue.CSS_RGBCOLOR:
         engine.applyCSSProperty(element, "background-color", value, pseudo);
         break;
       case CSSPrimitiveValue.CSS_URI:
         engine.applyCSSProperty(element, "background-image", value, pseudo);
         break;
     }
   }
 }
Exemplo n.º 5
0
  public String getCssText() {
    StringBuffer sb = new StringBuffer();

    for (int i = 0; i < NUMBER_OF_STYLE; i++) {
      // we don't return the format in css as the format
      // is a complex object which can't be represented as
      // css string.
      if (i == IStyle.STYLE_DATA_FORMAT) continue;
      CSSValue value = getProperty(i);
      if (value != null) {
        sb.append(engine.getPropertyName(i));
        sb.append(": ");
        short type = value.getCssValueType();
        switch (type) {
          case CSSValue.CSS_PRIMITIVE_VALUE:
            {
              CSSPrimitiveValue pv = (CSSPrimitiveValue) value;
              short unitType = pv.getPrimitiveType();
              switch (unitType) {
                case CSSPrimitiveValue.CSS_STRING:
                  sb.append("'");
                  sb.append(pv.getStringValue());
                  sb.append("'");
                  break;
                case CSSPrimitiveValue.CSS_URI:
                  sb.append("url('");
                  sb.append(pv.getStringValue());
                  sb.append("')");
                  break;
                default:
                  sb.append(value.getCssText());
              }
            }
            break;
          default:
            sb.append(value.getCssText());
        }
        sb.append("; ");
      }
    }
    if (sb.length() > 2) {
      sb.setLength(sb.length() - 2);
    }
    return sb.toString();
  }
 /**
  * Return the key of the CSSPrimitiveValue <code>value</code> which is used to cache Resource into
  * {@link IResourcesRegistry}.
  *
  * @param value
  * @return
  */
 public static String getCSSPrimitiveValueKey(CSSPrimitiveValue value) {
   switch (value.getPrimitiveType()) {
     case CSSPrimitiveValue.CSS_IDENT:
     case CSSPrimitiveValue.CSS_URI:
       String s = value.getStringValue();
       // Test if s is Color Name
       if (CSS2ColorHelper.isColorName(s)) {
         RGBColor rgbColor = CSS2ColorHelper.getRGBColor(s);
         if (rgbColor != null) {
           return getCSSRGBColorKey(rgbColor);
         }
       }
       return value.getStringValue();
     case CSSPrimitiveValue.CSS_RGBCOLOR:
       RGBColor rgbColor = value.getRGBColorValue();
       return getCSSRGBColorKey(rgbColor);
   }
   return null;
 }
  public void testInsertRule() {
    final String RULE =
        "@font-face { font-family: \"Swiss 721\"; src: url(swiss721.pfr); /* The expanded Swiss 721 */ font-stretch: expanded; }";
    CSSStyleSheet sheet = getStyleSheet();
    assertEquals(0, sheet.insertRule(RULE, 0));

    CSSRuleList ruleList = sheet.getCssRules();
    CSSRule rule = ruleList.item(0);
    assertTrue(rule instanceof CSSFontFaceRule);

    CSSStyleDeclaration declaration = ((CSSFontFaceRule) rule).getStyle();
    assertEquals(3, declaration.getLength());

    CSSValue value;
    CSSPrimitiveValue primitiveValue;

    value = declaration.getPropertyCSSValue("font-family");
    assertTrue(value instanceof CSSPrimitiveValue);

    primitiveValue = (CSSPrimitiveValue) value;
    assertEquals(CSSPrimitiveValue.CSS_STRING, primitiveValue.getPrimitiveType());
    assertEquals("Swiss 721", primitiveValue.getStringValue());

    value = declaration.getPropertyCSSValue("src");
    assertTrue(value instanceof CSSPrimitiveValue);

    primitiveValue = (CSSPrimitiveValue) value;
    assertEquals(CSSPrimitiveValue.CSS_URI, primitiveValue.getPrimitiveType());
    assertEquals("swiss721.pfr", primitiveValue.getStringValue());

    value = declaration.getPropertyCSSValue("font-stretch");
    assertTrue(value instanceof CSSPrimitiveValue);

    primitiveValue = (CSSPrimitiveValue) value;
    assertEquals(CSSPrimitiveValue.CSS_IDENT, primitiveValue.getPrimitiveType());
    assertEquals("expanded", primitiveValue.getStringValue());
  }
 /**
  * Gets the float value in the specified unit.
  *
  * @param unitType the type of unit
  * @return the value
  */
 public double jsxFunction_getFloatValue(final int unitType) {
   return wrappedCssPrimitiveValue_.getFloatValue((short) unitType);
 }
 public String toString() {
   return "rgb(" + _red.toString() + ", " + _green.toString() + ", " + _blue.toString() + ")";
 }