Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * This file is part of LibCSS.
  3.  * Licensed under the MIT License,
  4.  *                http://www.opensource.org/licenses/mit-license.php
  5.  * Copyright 2009 John-Mark Bell <jmb@netsurf-browser.org>
  6.  */
  7.  
  8. #include <assert.h>
  9. #include <string.h>
  10.  
  11. #include "bytecode/bytecode.h"
  12. #include "bytecode/opcodes.h"
  13. #include "parse/properties/properties.h"
  14. #include "parse/properties/utils.h"
  15.  
  16. /**
  17.  * Parse clip
  18.  *
  19.  * \param c       Parsing context
  20.  * \param vector  Vector of tokens to process
  21.  * \param ctx     Pointer to vector iteration context
  22.  * \param result  Pointer to location to receive resulting style
  23.  * \return CSS_OK on success,
  24.  *         CSS_NOMEM on memory exhaustion,
  25.  *         CSS_INVALID if the input is not valid
  26.  *
  27.  * Post condition: \a *ctx is updated with the next token to process
  28.  *                 If the input is invalid, then \a *ctx remains unchanged.
  29.  */
  30. css_error css__parse_clip(css_language *c,
  31.                 const parserutils_vector *vector, int *ctx,
  32.                 css_style *result)
  33. {
  34.         int orig_ctx = *ctx;
  35.         css_error error;
  36.         const css_token *token;
  37.         int num_lengths = 0;
  38.         css_fixed length[4] = { 0 };
  39.         uint32_t unit[4] = { 0 };
  40.         bool match;
  41.  
  42.         /* FUNCTION(rect) [ [ IDENT(auto) | length ] CHAR(,)? ]{3}
  43.          *                [ IDENT(auto) | length ] CHAR{)} |
  44.          * IDENT(auto, inherit) */
  45.         token = parserutils_vector_iterate(vector, ctx);
  46.         if (token == NULL) {
  47.                 *ctx = orig_ctx;
  48.                 return CSS_INVALID;
  49.         }
  50.  
  51.         if ((token->type == CSS_TOKEN_IDENT) &&
  52.             (lwc_string_caseless_isequal(
  53.                     token->idata, c->strings[INHERIT],
  54.                     &match) == lwc_error_ok && match)) {
  55.                 error = css__stylesheet_style_appendOPV(result,
  56.                                                        CSS_PROP_CLIP,
  57.                                                        FLAG_INHERIT,
  58.                                                        0);
  59.         } else if ((token->type == CSS_TOKEN_IDENT) &&
  60.                    (lwc_string_caseless_isequal(
  61.                            token->idata, c->strings[AUTO],
  62.                            &match) == lwc_error_ok && match)) {
  63.                 error = css__stylesheet_style_appendOPV(result,
  64.                                                        CSS_PROP_CLIP,
  65.                                                        0,
  66.                                                        CLIP_AUTO);
  67.         } else if ((token->type == CSS_TOKEN_FUNCTION) &&
  68.                    (lwc_string_caseless_isequal(
  69.                            token->idata, c->strings[RECT],
  70.                            &match) == lwc_error_ok && match)) {
  71.                 int i;
  72.                 uint16_t value = CLIP_SHAPE_RECT;
  73.  
  74.                 for (i = 0; i < 4; i++) {
  75.                         consumeWhitespace(vector, ctx);
  76.  
  77.                         token = parserutils_vector_peek(vector, *ctx);
  78.                         if (token == NULL) {
  79.                                 *ctx = orig_ctx;
  80.                                 return CSS_INVALID;
  81.                         }
  82.  
  83.                         if (token->type == CSS_TOKEN_IDENT) {
  84.                                 /* Slightly magical way of generating the auto
  85.                                  * values. These are bits 3-6 of the value. */
  86.                                 if ((lwc_string_caseless_isequal(
  87.                                                 token->idata, c->strings[AUTO],
  88.                                                 &match) == lwc_error_ok &&
  89.                                                 match))
  90.                                         value |= 1 << (i + 3);
  91.                                 else {
  92.                                         *ctx = orig_ctx;
  93.                                         return CSS_INVALID;
  94.                                 }
  95.  
  96.                                 parserutils_vector_iterate(vector, ctx);
  97.                         } else {
  98.                                 error = css__parse_unit_specifier(c, vector, ctx,
  99.                                                 UNIT_PX,
  100.                                                 &length[num_lengths],
  101.                                                 &unit[num_lengths]);
  102.                                 if (error != CSS_OK) {
  103.                                         *ctx = orig_ctx;
  104.                                         return error;
  105.                                 }
  106.  
  107.                                 if (unit[num_lengths] & UNIT_ANGLE ||
  108.                                                 unit[num_lengths] & UNIT_TIME ||
  109.                                                 unit[num_lengths] & UNIT_FREQ ||
  110.                                                 unit[num_lengths] & UNIT_PCT) {
  111.                                         *ctx = orig_ctx;
  112.                                         return CSS_INVALID;
  113.                                 }
  114.  
  115.                                 num_lengths++;
  116.                         }
  117.  
  118.                         consumeWhitespace(vector, ctx);
  119.  
  120.                         /* Consume optional comma after first 3 parameters */
  121.                         if (i < 3) {
  122.                                 token = parserutils_vector_peek(vector, *ctx);
  123.                                 if (token == NULL) {
  124.                                         *ctx = orig_ctx;
  125.                                         return CSS_INVALID;
  126.                                 }
  127.  
  128.                                 if (tokenIsChar(token, ','))
  129.                                         parserutils_vector_iterate(vector, ctx);
  130.                         }
  131.                 }
  132.  
  133.                 consumeWhitespace(vector, ctx);
  134.  
  135.                 /* Finally, consume closing parenthesis */
  136.                 token = parserutils_vector_iterate(vector, ctx);
  137.                 if (token == NULL || tokenIsChar(token, ')') == false) {
  138.                         *ctx = orig_ctx;
  139.                         return CSS_INVALID;
  140.                 }
  141.  
  142.                 /* output bytecode */
  143.                 error = css__stylesheet_style_appendOPV(result,
  144.                                                        CSS_PROP_CLIP,
  145.                                                        0,
  146.                                                        value);
  147.                 if (error != CSS_OK) {
  148.                         *ctx = orig_ctx;
  149.                         return error;
  150.                 }
  151.  
  152.                 for (i = 0; i < num_lengths; i++) {
  153.                         error = css__stylesheet_style_vappend(result,
  154.                                                              2,
  155.                                                              length[i],
  156.                                                              unit[i]);
  157.                         if (error != CSS_OK)
  158.                                 break;
  159.                 }
  160.  
  161.  
  162.         } else {
  163.                 error = CSS_INVALID;
  164.         }
  165.  
  166.         if (error != CSS_OK) {
  167.                 *ctx = orig_ctx;
  168.         }
  169.  
  170.         return error;
  171. }
  172.