summaryrefslogtreecommitdiffstats
path: root/webkit/port/platform/UniscribeStateTextRun.cpp
blob: 5a3c8f5135d1505e89017242f4b3e39559ec1337 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Copyright (C) 2006, 2007 Apple Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#include "config.h"

#include "UniscribeStateTextRun.h"

#include "Font.h"
#include "SimpleFontData.h"

#include "webkit/glue/webkit_glue.h"

UniscribeStateTextRun::UniscribeStateTextRun(const WebCore::TextRun& run,
                                             const WebCore::Font& font)
    : UniscribeState(run.characters(), run.length(), run.rtl(),
                     font.primaryFont()->platformData().hfont(),
                     font.primaryFont()->platformData().scriptCache(),
                     font.primaryFont()->platformData().scriptFontProperties()),
       font_(&font),
       font_index_(0) {
    set_directional_override(run.directionalOverride());
    set_letter_spacing(font.letterSpacing());
    set_space_width(font.spaceWidth());
    set_word_spacing(font.wordSpacing());
    set_ascent(font.primaryFont()->ascent()); 

    Init();

    // Padding is the amount to add to make justification happen. This
    // should be done after Init() so all the runs are already measured.
    if (run.padding() > 0)
      Justify(run.padding());
}

UniscribeStateTextRun::UniscribeStateTextRun(
    const wchar_t* input,
    int input_length,
    bool is_rtl,
    HFONT hfont,
    SCRIPT_CACHE* script_cache,
    SCRIPT_FONTPROPERTIES* font_properties)
    : UniscribeState(input, input_length, is_rtl, hfont,
                     script_cache, font_properties),
      font_(NULL),
      font_index_(-1) {
}

void UniscribeStateTextRun::TryToPreloadFont(HFONT font) {
  // Ask the browser to get the font metrics for this font.
  // That will preload the font and it should now be accessible
  // from the renderer.
  webkit_glue::EnsureFontLoaded(font);
}

bool UniscribeStateTextRun::NextWinFontData(
    HFONT* hfont,
    SCRIPT_CACHE** script_cache,
    SCRIPT_FONTPROPERTIES** font_properties,
    int* ascent) {
  // This check is necessary because NextWinFontData can be
  // called again after we already ran out of fonts. fontDataAt
  // behaves in a strange manner when the difference between
  // param passed and # of fonts stored in WebKit::Font is
  // larger than one. We can avoid this check by setting 
  // font_index_ to # of elements in hfonts_ when we run out 
  // of font. In that case, we'd have to go through a couple of 
  // more checks before returning false.
  if (font_index_ == -1 || !font_)
    return false;

  // If the font data for a fallback font requested is not
  // yet retrieved, add them to our vectors. Note that '>' rather 
  // than '>=' is used to test that condition. primaryFont is not
  // stored in hfonts_, and friends so that indices for fontDataAt
  // and our vectors for font data are 1 off from each other. 
  // That is, when fully populated, hfonts_ and friends have
  // one font fewer than what's contained in font_. 
  if (static_cast<size_t>(++font_index_) > hfonts_->size()) {
    const WebCore::FontData *font_data;
    font_data = font_->fontDataAt(font_index_); 
    if (!font_data) {
      // run out of fonts
      font_index_ = -1;
      return false;
    }

    // TODO(ericroman): this won't work for SegmentedFontData
    // http://b/issue?id=1007335
    const WebCore::SimpleFontData* simple_font_data =
        font_data->fontDataForCharacter(' ');

    hfonts_->push_back(simple_font_data->platformData().hfont()); 
    script_caches_->push_back(simple_font_data->platformData().scriptCache());
    font_properties_->push_back(simple_font_data->platformData().scriptFontProperties());
    ascents_->push_back(simple_font_data->ascent()); 
  }

  *hfont = hfonts_[font_index_ - 1]; 
  *script_cache = script_caches_[font_index_ - 1];
  *font_properties = font_properties_[font_index_ - 1];
  *ascent = ascents_[font_index_ - 1];
  return true; 
}

void UniscribeStateTextRun::ResetFontIndex() {
  font_index_ = 0;
}