File: gr_Abi_DefaultShaper.cpp

package info (click to toggle)
abiword 3.0.8%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 64,532 kB
  • sloc: cpp: 477,427; ansic: 16,754; makefile: 5,785; xml: 4,014; yacc: 1,818; lex: 1,104; perl: 812; python: 413; php: 183; sh: 169
file content (123 lines) | stat: -rw-r--r-- 4,558 bytes parent folder | download | duplicates (7)
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
/* AbiWord
 * Copyright (C) 2004 Luca Padovani <lpadovan@cs.unibo.it>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 */

#include <stdio.h>

#include "mathview_proxy.h"

#include "gr_Abi_AreaFactory.h"
#include "gr_Abi_DefaultShaper.h"

#define NORMAL_INDEX 0
#define MAPPED_BASE_INDEX 1

GR_Abi_DefaultShaper::GR_Abi_DefaultShaper()
{ }

GR_Abi_DefaultShaper::~GR_Abi_DefaultShaper()
{ }

void
GR_Abi_DefaultShaper::registerShaper(const SmartPtr<ShaperManager>& sm, unsigned shaperId)
{
  // normal characters are not registered because this shaper is supposed to
  // be the default one. It will be called anyway as soon as there's a
  // Unicode char that cannot be shaped otherwise

  // Variant characters however are mapped so that it is possible to
  // recover their "normal" Unicode number in the BMP and the appropriate
  // AbiWord properties can be applied when asking for a font
  for (unsigned i = NORMAL_VARIANT; i <= MONOSPACE_VARIANT; i++)
    {
      for (Char16 ch = 0x21; ch < 0x80; ch++)
	{
	  Char32 vch = mapMathVariant(MathVariant(i), ch);
	  if (vch != ch)
	    sm->registerChar(vch, GlyphSpec(shaperId, MAPPED_BASE_INDEX + i - NORMAL_VARIANT, ch));
	}
    }

}

void
GR_Abi_DefaultShaper::unregisterShaper(const SmartPtr<class ShaperManager>&, unsigned)
{
  // nothing to do
}

#define DEFAULT_SERIF_FAMILY "Times"
#define DEFAULT_SANS_SERIF_FAMILY "Helvetica"
#define DEFAULT_MONOSPACE "Courier"

const GR_Abi_DefaultShaper::AbiTextProperties&
GR_Abi_DefaultShaper::getTextProperties(MathVariant variant)
{
  UT_ASSERT(variant >= NORMAL_VARIANT && variant <= MONOSPACE_VARIANT);
  static const AbiTextProperties variantDesc[] =
    {
      { NORMAL_VARIANT, DEFAULT_SERIF_FAMILY, "normal", "normal" },
      { BOLD_VARIANT, DEFAULT_SERIF_FAMILY, "normal", "bold" },
      { ITALIC_VARIANT, DEFAULT_SERIF_FAMILY, "italic", "normal" },
      { BOLD_ITALIC_VARIANT, DEFAULT_SERIF_FAMILY, "italic", "bold" }, 
      { DOUBLE_STRUCK_VARIANT, DEFAULT_SANS_SERIF_FAMILY, "normal", "bold" },
      { BOLD_FRAKTUR_VARIANT, DEFAULT_SERIF_FAMILY, "normal", "bold" },
      { SCRIPT_VARIANT, DEFAULT_SANS_SERIF_FAMILY, "normal", "normal" },
      { BOLD_SCRIPT_VARIANT, DEFAULT_SANS_SERIF_FAMILY, "normal", "bold" },
      { FRAKTUR_VARIANT, DEFAULT_SERIF_FAMILY, "normal", "bold" },
      { SANS_SERIF_VARIANT, DEFAULT_SANS_SERIF_FAMILY, "normal", "normal" },
      { BOLD_SANS_SERIF_VARIANT, DEFAULT_SANS_SERIF_FAMILY, "normal", "bold" },
      { SANS_SERIF_ITALIC_VARIANT, DEFAULT_SANS_SERIF_FAMILY, "italic", "normal" },
      { SANS_SERIF_BOLD_ITALIC_VARIANT, DEFAULT_SANS_SERIF_FAMILY, "italic", "bold" },
      { MONOSPACE_VARIANT, DEFAULT_MONOSPACE, "normal", "normal" }
    };
  return variantDesc[variant - NORMAL_VARIANT];
}

void
GR_Abi_DefaultShaper::shape(ShapingContext& context) const
{
  const GlyphSpec spec = context.getSpec();
  if (spec.getFontId() == NORMAL_INDEX)
    context.pushArea(1, shapeChar(NORMAL_VARIANT, context, context.thisChar()));
  else
    context.pushArea(1, shapeChar(MathVariant(spec.getFontId() - MAPPED_BASE_INDEX + NORMAL_VARIANT),
				  context, spec.getGlyphId()));
}

AreaRef
GR_Abi_DefaultShaper::shapeChar(MathVariant variant, const ShapingContext& context, UT_UCS4Char ch) const
{
  // the "variant" parameter overrides the variant value in the formatting context

  static char fontSize[128];
  sprintf(fontSize, "%dpt", static_cast<int>(context.getSize().toFloat() + 0.5f));

  const AbiTextProperties& props = getTextProperties(variant);
  GR_Font* font = m_pGraphics->findFont(props.family, props.style, "", props.weight, "", fontSize, "");
  UT_ASSERT(font);

  SmartPtr<GR_Abi_AreaFactory> factory = smart_cast<GR_Abi_AreaFactory>(context.getFactory());
  return factory->charArea(m_pGraphics, font, context.getSize(), ch);
}

void
GR_Abi_DefaultShaper::setGraphics(GR_Graphics* pGr)
{
  m_pGraphics = pGr;
}