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 azimuth
  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  style to place resulting bytcode in
  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_azimuth(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.         uint8_t flags = 0;
  38.         uint16_t value = 0;
  39.         css_fixed length = 0;
  40.         uint32_t unit = 0;
  41.         bool match;
  42.  
  43.         /* angle | [ IDENT(left-side, far-left, left, center-left, center,
  44.          *                 center-right, right, far-right, right-side) ||
  45.          *         IDENT(behind)
  46.          *       ]
  47.          *       | IDENT(leftwards, rightwards, inherit)
  48.          */
  49.         token = parserutils_vector_peek(vector, *ctx);
  50.         if (token == NULL) {
  51.                 *ctx = orig_ctx;
  52.                 return CSS_INVALID;
  53.         }
  54.  
  55.         if (token->type == CSS_TOKEN_IDENT &&
  56.                 (lwc_string_caseless_isequal(token->idata, c->strings[INHERIT],
  57.                         &match) == lwc_error_ok && match)) {
  58.                 parserutils_vector_iterate(vector, ctx);
  59.                 flags = FLAG_INHERIT;
  60.         } else if (token->type == CSS_TOKEN_IDENT &&
  61.                 (lwc_string_caseless_isequal(token->idata, c->strings[LEFTWARDS],
  62.                         &match) == lwc_error_ok && match)) {
  63.                 parserutils_vector_iterate(vector, ctx);
  64.                 value = AZIMUTH_LEFTWARDS;
  65.         } else if (token->type == CSS_TOKEN_IDENT &&
  66.                 (lwc_string_caseless_isequal(token->idata, c->strings[RIGHTWARDS],
  67.                         &match) == lwc_error_ok && match)) {
  68.                 parserutils_vector_iterate(vector, ctx);
  69.                 value = AZIMUTH_RIGHTWARDS;
  70.         } else if (token->type == CSS_TOKEN_IDENT) {
  71.                 parserutils_vector_iterate(vector, ctx);
  72.  
  73.                 /* Now, we may have one of the other keywords or behind,
  74.                  * potentially followed by behind or other keyword,
  75.                  * respectively */
  76.                 if ((lwc_string_caseless_isequal(
  77.                                 token->idata, c->strings[LEFT_SIDE],
  78.                                 &match) == lwc_error_ok && match)) {
  79.                         value = AZIMUTH_LEFT_SIDE;
  80.                 } else if ((lwc_string_caseless_isequal(
  81.                                 token->idata, c->strings[FAR_LEFT],
  82.                                 &match) == lwc_error_ok && match)) {
  83.                         value = AZIMUTH_FAR_LEFT;
  84.                 } else if ((lwc_string_caseless_isequal(
  85.                                 token->idata, c->strings[LEFT],
  86.                                 &match) == lwc_error_ok && match)) {
  87.                         value = AZIMUTH_LEFT;
  88.                 } else if ((lwc_string_caseless_isequal(
  89.                                 token->idata, c->strings[CENTER_LEFT],
  90.                                 &match) == lwc_error_ok && match)) {
  91.                         value = AZIMUTH_CENTER_LEFT;
  92.                 } else if ((lwc_string_caseless_isequal(
  93.                                 token->idata, c->strings[CENTER],
  94.                                 &match) == lwc_error_ok && match)) {
  95.                         value = AZIMUTH_CENTER;
  96.                 } else if ((lwc_string_caseless_isequal(
  97.                                 token->idata, c->strings[CENTER_RIGHT],
  98.                                 &match) == lwc_error_ok && match)) {
  99.                         value = AZIMUTH_CENTER_RIGHT;
  100.                 } else if ((lwc_string_caseless_isequal(
  101.                                 token->idata, c->strings[RIGHT],
  102.                                 &match) == lwc_error_ok && match)) {
  103.                         value = AZIMUTH_RIGHT;
  104.                 } else if ((lwc_string_caseless_isequal(
  105.                                 token->idata, c->strings[FAR_RIGHT],
  106.                                 &match) == lwc_error_ok && match)) {
  107.                         value = AZIMUTH_FAR_RIGHT;
  108.                 } else if ((lwc_string_caseless_isequal(
  109.                                 token->idata, c->strings[RIGHT_SIDE],
  110.                                 &match) == lwc_error_ok && match)) {
  111.                         value = AZIMUTH_RIGHT_SIDE;
  112.                 } else if ((lwc_string_caseless_isequal(
  113.                                 token->idata, c->strings[BEHIND],
  114.                                 &match) == lwc_error_ok && match)) {
  115.                         value = AZIMUTH_BEHIND;
  116.                 } else {
  117.                         *ctx = orig_ctx;
  118.                         return CSS_INVALID;
  119.                 }
  120.  
  121.                 consumeWhitespace(vector, ctx);
  122.  
  123.                 /* Get potential following token */
  124.                 token = parserutils_vector_peek(vector, *ctx);
  125.  
  126.                 if (token != NULL && token->type == CSS_TOKEN_IDENT &&
  127.                                 value == AZIMUTH_BEHIND) {
  128.                         parserutils_vector_iterate(vector, ctx);
  129.  
  130.                         if ((lwc_string_caseless_isequal(
  131.                                         token->idata, c->strings[LEFT_SIDE],
  132.                                         &match) == lwc_error_ok && match)) {
  133.                                 value |= AZIMUTH_LEFT_SIDE;
  134.                         } else if ((lwc_string_caseless_isequal(
  135.                                         token->idata, c->strings[FAR_LEFT],
  136.                                         &match) == lwc_error_ok && match)) {
  137.                                 value |= AZIMUTH_FAR_LEFT;
  138.                         } else if ((lwc_string_caseless_isequal(
  139.                                         token->idata, c->strings[LEFT],
  140.                                         &match) == lwc_error_ok && match)) {
  141.                                 value |= AZIMUTH_LEFT;
  142.                         } else if ((lwc_string_caseless_isequal(
  143.                                         token->idata, c->strings[CENTER_LEFT],
  144.                                         &match) == lwc_error_ok && match)) {
  145.                                 value |= AZIMUTH_CENTER_LEFT;
  146.                         } else if ((lwc_string_caseless_isequal(
  147.                                         token->idata, c->strings[CENTER],
  148.                                         &match) == lwc_error_ok && match)) {
  149.                                 value |= AZIMUTH_CENTER;
  150.                         } else if ((lwc_string_caseless_isequal(
  151.                                         token->idata, c->strings[CENTER_RIGHT],
  152.                                         &match) == lwc_error_ok && match)) {
  153.                                 value |= AZIMUTH_CENTER_RIGHT;
  154.                         } else if ((lwc_string_caseless_isequal(
  155.                                         token->idata, c->strings[RIGHT],
  156.                                         &match) == lwc_error_ok && match)) {
  157.                                 value |= AZIMUTH_RIGHT;
  158.                         } else if ((lwc_string_caseless_isequal(
  159.                                         token->idata, c->strings[FAR_RIGHT],
  160.                                         &match) == lwc_error_ok && match)) {
  161.                                 value |= AZIMUTH_FAR_RIGHT;
  162.                         } else if ((lwc_string_caseless_isequal(
  163.                                         token->idata, c->strings[RIGHT_SIDE],
  164.                                         &match) == lwc_error_ok && match)) {
  165.                                 value |= AZIMUTH_RIGHT_SIDE;
  166.                         } else {
  167.                                 *ctx = orig_ctx;
  168.                                 return CSS_INVALID;
  169.                         }
  170.                 } else if (token != NULL && token->type == CSS_TOKEN_IDENT &&
  171.                                 value != AZIMUTH_BEHIND) {
  172.                         parserutils_vector_iterate(vector, ctx);
  173.  
  174.                         if ((lwc_string_caseless_isequal(
  175.                                         token->idata, c->strings[BEHIND],
  176.                                         &match) == lwc_error_ok && match)) {
  177.                                 value |= AZIMUTH_BEHIND;
  178.                         } else {
  179.                                 *ctx = orig_ctx;
  180.                                 return CSS_INVALID;
  181.                         }
  182.                 } else if ((token == NULL || token->type != CSS_TOKEN_IDENT) &&
  183.                                 value == AZIMUTH_BEHIND) {
  184.                         value |= AZIMUTH_CENTER;
  185.                 }
  186.         } else {
  187.                 error = css__parse_unit_specifier(c, vector, ctx, UNIT_DEG,
  188.                                 &length, &unit);
  189.                 if (error != CSS_OK) {
  190.                         *ctx = orig_ctx;
  191.                         return error;
  192.                 }
  193.  
  194.                 if ((unit & UNIT_ANGLE) == false) {
  195.                         *ctx = orig_ctx;
  196.                         return CSS_INVALID;
  197.                 }
  198.  
  199.                 /* Valid angles lie between -360 and 360 degrees */
  200.                 if (unit == UNIT_DEG) {
  201.                         if ((length < -F_360) || (length > F_360)) {
  202.                                 *ctx = orig_ctx;
  203.                                 return CSS_INVALID;
  204.                         }
  205.                 } else if (unit == UNIT_GRAD) {
  206.                         if ((length < -F_400) || (length > F_400)) {
  207.                                 *ctx = orig_ctx;
  208.                                 return CSS_INVALID;
  209.                         }
  210.                 } else if (unit == UNIT_RAD) {
  211.                         if ((length < -F_2PI) || (length > F_2PI)) {
  212.                                 *ctx = orig_ctx;
  213.                                 return CSS_INVALID;
  214.                         }
  215.                 }
  216.  
  217.                 value = AZIMUTH_ANGLE;
  218.         }
  219.  
  220.         error = css__stylesheet_style_appendOPV(result, CSS_PROP_AZIMUTH, flags, value);
  221.         if (error != CSS_OK) {
  222.                 *ctx = orig_ctx;
  223.                 return error;
  224.         }
  225.  
  226.         if (((flags & FLAG_INHERIT) == false) && (value == AZIMUTH_ANGLE)) {
  227.                 error = css__stylesheet_style_vappend(result, 2, length, unit);
  228.                 if (error != CSS_OK) {
  229.                         *ctx = orig_ctx;
  230.                         return error;
  231.                 }
  232.         }
  233.  
  234.         return CSS_OK;
  235. }
  236.