Home | History | Annotate | Download | only in fpdfdoc
      1 // Copyright 2016 PDFium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
      6 
      7 #include "core/fpdfdoc/cpdf_defaultappearance.h"
      8 
      9 #include <algorithm>
     10 
     11 #include "core/fpdfapi/parser/cpdf_simple_parser.h"
     12 #include "core/fpdfapi/parser/fpdf_parser_decode.h"
     13 #include "core/fpdfdoc/cpdf_formcontrol.h"
     14 
     15 bool CPDF_DefaultAppearance::HasFont() {
     16   if (m_csDA.IsEmpty())
     17     return false;
     18 
     19   CPDF_SimpleParser syntax(m_csDA.AsStringC());
     20   return syntax.FindTagParamFromStart("Tf", 2);
     21 }
     22 
     23 CFX_ByteString CPDF_DefaultAppearance::GetFontString() {
     24   CFX_ByteString csFont;
     25   if (m_csDA.IsEmpty())
     26     return csFont;
     27 
     28   CPDF_SimpleParser syntax(m_csDA.AsStringC());
     29   if (syntax.FindTagParamFromStart("Tf", 2)) {
     30     csFont += syntax.GetWord();
     31     csFont += " ";
     32     csFont += syntax.GetWord();
     33     csFont += " ";
     34     csFont += syntax.GetWord();
     35   }
     36   return csFont;
     37 }
     38 
     39 void CPDF_DefaultAppearance::GetFont(CFX_ByteString& csFontNameTag,
     40                                      FX_FLOAT& fFontSize) {
     41   csFontNameTag = "";
     42   fFontSize = 0;
     43   if (m_csDA.IsEmpty())
     44     return;
     45 
     46   CPDF_SimpleParser syntax(m_csDA.AsStringC());
     47   if (syntax.FindTagParamFromStart("Tf", 2)) {
     48     csFontNameTag = CFX_ByteString(syntax.GetWord());
     49     csFontNameTag.Delete(0, 1);
     50     fFontSize = FX_atof(syntax.GetWord());
     51   }
     52   csFontNameTag = PDF_NameDecode(csFontNameTag);
     53 }
     54 
     55 bool CPDF_DefaultAppearance::HasColor(PaintOperation nOperation) {
     56   if (m_csDA.IsEmpty())
     57     return false;
     58 
     59   CPDF_SimpleParser syntax(m_csDA.AsStringC());
     60   if (syntax.FindTagParamFromStart(
     61           (nOperation == PaintOperation::STROKE ? "G" : "g"), 1)) {
     62     return true;
     63   }
     64   if (syntax.FindTagParamFromStart(
     65           (nOperation == PaintOperation::STROKE ? "RG" : "rg"), 3)) {
     66     return true;
     67   }
     68   return syntax.FindTagParamFromStart(
     69       (nOperation == PaintOperation::STROKE ? "K" : "k"), 4);
     70 }
     71 
     72 CFX_ByteString CPDF_DefaultAppearance::GetColorString(
     73     PaintOperation nOperation) {
     74   CFX_ByteString csColor;
     75   if (m_csDA.IsEmpty())
     76     return csColor;
     77 
     78   CPDF_SimpleParser syntax(m_csDA.AsStringC());
     79   if (syntax.FindTagParamFromStart(
     80           (nOperation == PaintOperation::STROKE ? "G" : "g"), 1)) {
     81     csColor += syntax.GetWord();
     82     csColor += " ";
     83     csColor += syntax.GetWord();
     84     return csColor;
     85   }
     86   if (syntax.FindTagParamFromStart(
     87           (nOperation == PaintOperation::STROKE ? "RG" : "rg"), 3)) {
     88     csColor += syntax.GetWord();
     89     csColor += " ";
     90     csColor += syntax.GetWord();
     91     csColor += " ";
     92     csColor += syntax.GetWord();
     93     csColor += " ";
     94     csColor += syntax.GetWord();
     95     return csColor;
     96   }
     97   if (syntax.FindTagParamFromStart(
     98           (nOperation == PaintOperation::STROKE ? "K" : "k"), 4)) {
     99     csColor += syntax.GetWord();
    100     csColor += " ";
    101     csColor += syntax.GetWord();
    102     csColor += " ";
    103     csColor += syntax.GetWord();
    104     csColor += " ";
    105     csColor += syntax.GetWord();
    106     csColor += " ";
    107     csColor += syntax.GetWord();
    108   }
    109   return csColor;
    110 }
    111 
    112 void CPDF_DefaultAppearance::GetColor(int& iColorType,
    113                                       FX_FLOAT fc[4],
    114                                       PaintOperation nOperation) {
    115   iColorType = COLORTYPE_TRANSPARENT;
    116   for (int c = 0; c < 4; c++)
    117     fc[c] = 0;
    118 
    119   if (m_csDA.IsEmpty())
    120     return;
    121 
    122   CPDF_SimpleParser syntax(m_csDA.AsStringC());
    123   if (syntax.FindTagParamFromStart(
    124           (nOperation == PaintOperation::STROKE ? "G" : "g"), 1)) {
    125     iColorType = COLORTYPE_GRAY;
    126     fc[0] = FX_atof(syntax.GetWord());
    127     return;
    128   }
    129   if (syntax.FindTagParamFromStart(
    130           (nOperation == PaintOperation::STROKE ? "RG" : "rg"), 3)) {
    131     iColorType = COLORTYPE_RGB;
    132     fc[0] = FX_atof(syntax.GetWord());
    133     fc[1] = FX_atof(syntax.GetWord());
    134     fc[2] = FX_atof(syntax.GetWord());
    135     return;
    136   }
    137   if (syntax.FindTagParamFromStart(
    138           (nOperation == PaintOperation::STROKE ? "K" : "k"), 4)) {
    139     iColorType = COLORTYPE_CMYK;
    140     fc[0] = FX_atof(syntax.GetWord());
    141     fc[1] = FX_atof(syntax.GetWord());
    142     fc[2] = FX_atof(syntax.GetWord());
    143     fc[3] = FX_atof(syntax.GetWord());
    144   }
    145 }
    146 
    147 void CPDF_DefaultAppearance::GetColor(FX_ARGB& color,
    148                                       int& iColorType,
    149                                       PaintOperation nOperation) {
    150   color = 0;
    151   iColorType = COLORTYPE_TRANSPARENT;
    152   if (m_csDA.IsEmpty())
    153     return;
    154 
    155   CPDF_SimpleParser syntax(m_csDA.AsStringC());
    156   if (syntax.FindTagParamFromStart(
    157           (nOperation == PaintOperation::STROKE ? "G" : "g"), 1)) {
    158     iColorType = COLORTYPE_GRAY;
    159     FX_FLOAT g = FX_atof(syntax.GetWord()) * 255 + 0.5f;
    160     color = ArgbEncode(255, (int)g, (int)g, (int)g);
    161     return;
    162   }
    163   if (syntax.FindTagParamFromStart(
    164           (nOperation == PaintOperation::STROKE ? "RG" : "rg"), 3)) {
    165     iColorType = COLORTYPE_RGB;
    166     FX_FLOAT r = FX_atof(syntax.GetWord()) * 255 + 0.5f;
    167     FX_FLOAT g = FX_atof(syntax.GetWord()) * 255 + 0.5f;
    168     FX_FLOAT b = FX_atof(syntax.GetWord()) * 255 + 0.5f;
    169     color = ArgbEncode(255, (int)r, (int)g, (int)b);
    170     return;
    171   }
    172   if (syntax.FindTagParamFromStart(
    173           (nOperation == PaintOperation::STROKE ? "K" : "k"), 4)) {
    174     iColorType = COLORTYPE_CMYK;
    175     FX_FLOAT c = FX_atof(syntax.GetWord());
    176     FX_FLOAT m = FX_atof(syntax.GetWord());
    177     FX_FLOAT y = FX_atof(syntax.GetWord());
    178     FX_FLOAT k = FX_atof(syntax.GetWord());
    179     FX_FLOAT r = 1.0f - std::min(1.0f, c + k);
    180     FX_FLOAT g = 1.0f - std::min(1.0f, m + k);
    181     FX_FLOAT b = 1.0f - std::min(1.0f, y + k);
    182     color = ArgbEncode(255, (int)(r * 255 + 0.5f), (int)(g * 255 + 0.5f),
    183                        (int)(b * 255 + 0.5f));
    184   }
    185 }
    186 
    187 bool CPDF_DefaultAppearance::HasTextMatrix() {
    188   if (m_csDA.IsEmpty())
    189     return false;
    190 
    191   CPDF_SimpleParser syntax(m_csDA.AsStringC());
    192   return syntax.FindTagParamFromStart("Tm", 6);
    193 }
    194 
    195 CFX_ByteString CPDF_DefaultAppearance::GetTextMatrixString() {
    196   CFX_ByteString csTM;
    197   if (m_csDA.IsEmpty())
    198     return csTM;
    199 
    200   CPDF_SimpleParser syntax(m_csDA.AsStringC());
    201   if (syntax.FindTagParamFromStart("Tm", 6)) {
    202     for (int i = 0; i < 6; i++) {
    203       csTM += syntax.GetWord();
    204       csTM += " ";
    205     }
    206     csTM += syntax.GetWord();
    207   }
    208   return csTM;
    209 }
    210 
    211 CFX_Matrix CPDF_DefaultAppearance::GetTextMatrix() {
    212   if (m_csDA.IsEmpty())
    213     return CFX_Matrix();
    214 
    215   CPDF_SimpleParser syntax(m_csDA.AsStringC());
    216   if (!syntax.FindTagParamFromStart("Tm", 6))
    217     return CFX_Matrix();
    218 
    219   FX_FLOAT f[6];
    220   for (int i = 0; i < 6; i++)
    221     f[i] = FX_atof(syntax.GetWord());
    222   return CFX_Matrix(f);
    223 }
    224