Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply patch Feature: added color #9

Merged
merged 1 commit into from
Feb 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 71 additions & 0 deletions WpfMath/Data/PredefinedColors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?xml version="1.0"?>
<PredefinedColors xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<color r="251" g="185" b="130" name="apricot" />
<color r="0" g="181" b="190" name="aquamarine" />
<color r="192" g="79" b="23" name="bittersweet" />
<color r="34" g="30" b="31" name="black" />
<color r="45" g="47" b="146" name="blue" />
<color r="0" g="179" b="184" name="bluegreen" />
<color r="71" g="57" b="146" name="blueviolet" />
<color r="182" g="50" b="28" name="brickred" />
<color r="121" g="37" b="0" name="brown" />
<color r="247" g="146" b="29" name="burntorange" />
<color r="116" g="114" b="154" name="cadetblue" />
<color r="242" g="130" b="180" name="carnationpink" />
<color r="0" g="162" b="227" name="cerulean" />
<color r="65" g="176" b="228" name="cornflowerblue" />
<color r="0" g="174" b="239" name="cyan" />
<color r="253" g="188" b="66" name="dandelion" />
<color r="164" g="83" b="138" name="darkorchid" />
<color r="0" g="169" b="157" name="emerald" />
<color r="0" g="155" b="85" name="forestgreen" />
<color r="140" g="54" b="140" name="fuchsia" />
<color r="255" g="223" b="66" name="goldenrod" />
<color r="148" g="150" b="152" name="gray" />
<color r="0" g="166" b="79" name="green" />
<color r="223" g="230" b="116" name="greenyellow" />
<color r="0" g="169" b="154" name="junglegreen" />
<color r="244" g="158" b="196" name="lavender" />
<color r="141" g="199" b="62" name="limegreen" />
<color r="236" g="0" b="140" name="magenta" />
<color r="169" g="52" b="31" name="mahogany" />
<color r="175" g="50" b="53" name="maroon" />
<color r="248" g="158" b="123" name="melon" />
<color r="0" g="103" b="149" name="midnightblue" />
<color r="169" g="60" b="147" name="mulberry" />
<color r="0" g="110" b="184" name="navyblue" />
<color r="60" g="128" b="49" name="olivegreen" />
<color r="245" g="129" b="55" name="orange" />
<color r="237" g="19" b="90" name="orangered" />
<color r="175" g="114" b="176" name="orchid" />
<color r="247" g="150" b="90" name="peach" />
<color r="121" g="119" b="184" name="periwinkle" />
<color r="0" g="139" b="114" name="pinegreen" />
<color r="146" g="38" b="143" name="plum" />
<color r="0" g="176" b="240" name="processblue" />
<color r="153" g="71" b="155" name="purple" />
<color r="151" g="64" b="6" name="rawsienna" />
<color r="237" g="27" b="35" name="red" />
<color r="242" g="96" b="53" name="redorange" />
<color r="161" g="36" b="107" name="redviolet" />
<color r="239" g="85" b="159" name="rhodamine" />
<color r="0" g="113" b="188" name="royalblue" />
<color r="97" g="63" b="153" name="royalpurple" />
<color r="237" g="1" b="125" name="rubinered" />
<color r="246" g="146" b="137" name="salmon" />
<color r="63" g="188" b="157" name="seagreen" />
<color r="103" g="24" b="0" name="sepia" />
<color r="70" g="197" b="221" name="skyblue" />
<color r="198" g="220" b="103" name="springgreen" />
<color r="218" g="157" b="118" name="tan" />
<color r="0" g="174" b="179" name="tealblue" />
<color r="216" g="131" b="183" name="thistle" />
<color r="0" g="180" b="206" name="turquoise" />
<color r="88" g="66" b="155" name="violet" />
<color r="239" g="88" b="160" name="violetred" />
<color r="255" g="255" b="255" name="white" />
<color r="238" g="41" b="103" name="wildstrawberry" />
<color r="255" g="242" b="0" name="yellow" />
<color r="152" g="204" b="112" name="yellowgreen" />
<color r="250" g="162" b="26" name="yelloworange" />
</PredefinedColors>
2 changes: 1 addition & 1 deletion WpfMath/FractionAtom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public override Box CreateBox(TexEnvironment environment)
}

resultBox.Add(new StrutBox(0, kern1, 0, 0));
resultBox.Add(new HorizontalRule(lineHeight, numeratorBox.Width, 0));
resultBox.Add(new HorizontalRule(environment, lineHeight, numeratorBox.Width, 0));
resultBox.Add(new StrutBox(0, kern2, 0, 0));
}
else
Expand Down
6 changes: 4 additions & 2 deletions WpfMath/HorizontalRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ namespace WpfMath
// Box representing horizontal line.
internal class HorizontalRule : Box
{
public HorizontalRule(double thickness, double width, double shift)
public HorizontalRule(TexEnvironment environment, double thickness, double width, double shift)
{
this.Width = width;
this.Height = thickness;
this.Shift = shift;
this.Foreground = environment.Foreground;
this.Background = environment.Background; //Not strictly necessary
}

public override void Draw(DrawingContext drawingContext, double scale, double x, double y)
{
drawingContext.DrawRectangle(Brushes.Black, null, new Rect(
drawingContext.DrawRectangle(this.Foreground ?? Brushes.Black, null, new Rect(
x * scale, (y - this.Height) * scale, this.Width * scale, this.Height * scale));
}

Expand Down
4 changes: 2 additions & 2 deletions WpfMath/OverBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ namespace WpfMath
// Box representing other box with horizontal rule above it.
internal class OverBar : VerticalBox
{
public OverBar(Box box, double kern, double thickness)
public OverBar(TexEnvironment environment, Box box, double kern, double thickness)
: base()
{
Add(new StrutBox(0, thickness, 0, 0));
Add(new HorizontalRule(thickness, box.Width, 0));
Add(new HorizontalRule(environment, thickness, box.Width, 0));
Add(new StrutBox(0, kern, 0, 0));
Add(box);
}
Expand Down
2 changes: 1 addition & 1 deletion WpfMath/OverlinedAtom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public override Box CreateBox(TexEnvironment environment)

// Create result box.
var defaultLineThickness = environment.TexFont.GetDefaultLineThickness(environment.Style);
var resultBox = new OverBar(baseBox, 3 * defaultLineThickness, defaultLineThickness);
var resultBox = new OverBar(environment, baseBox, 3 * defaultLineThickness, defaultLineThickness);

// Adjust height and depth of result box.
resultBox.Height = baseBox.Height + 5 * defaultLineThickness;
Expand Down
34 changes: 34 additions & 0 deletions WpfMath/PredefinedColorParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Xml.Linq;
using System.Reflection;

namespace WpfMath
{
internal class PredefinedColorParser
{
public static readonly string resourceName = TexUtilities.ResourcesDataDirectory + "PredefinedColors.xml";
private XElement rootElement;

public PredefinedColorParser()
{
var doc = XDocument.Load(new System.IO.StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)));
this.rootElement = doc.Root;
}

public void Parse(IDictionary<string, Color> predefinedColors)
{
foreach (var colorElement in rootElement.Elements("color"))
{
var name = colorElement.AttributeValue("name");
var r = colorElement.AttributeValue("r");
var g = colorElement.AttributeValue("g");
var b = colorElement.AttributeValue("b");
predefinedColors.Add(name, Color.FromRgb(Convert.ToByte(r), Convert.ToByte(g), Convert.ToByte(b)));
}
}
}
}
2 changes: 1 addition & 1 deletion WpfMath/Radical.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public override Box CreateBox(TexEnvironment environment)

// Create box for square-root containing base box.
radicalSignBox.Shift = -(baseBox.Height + clearance);
var overBar = new OverBar(baseBox, clearance, radicalSignBox.Height);
var overBar = new OverBar(environment, baseBox, clearance, radicalSignBox.Height);
overBar.Shift = -(baseBox.Height + clearance + defaultRuleThickness);
var radicalContainerBox = new HorizontalBox(radicalSignBox);
radicalContainerBox.Add(overBar);
Expand Down
41 changes: 41 additions & 0 deletions WpfMath/TexFormulaParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Windows;
using System.Reflection;
using System.Windows.Media;

namespace WpfMath
{
Expand All @@ -30,6 +31,7 @@ public class TexFormulaParser
private static IList<string> delimeters;
private static HashSet<string> textStyles;
private static IDictionary<string, TexFormula> predefinedFormulas;
private static IDictionary<string, Color> predefinedColors;

private static readonly string[][] delimiterNames =
{
Expand All @@ -54,6 +56,7 @@ static TexFormulaParser()
isInitialized = false;

predefinedFormulas = new Dictionary<string, TexFormula>();
predefinedColors = new Dictionary<string, Color>();
}

internal static string[][] DelimiterNames
Expand All @@ -77,12 +80,17 @@ public static void Initialize()
commands = new HashSet<string>();
commands.Add("frac");
commands.Add("sqrt");
commands.Add("color");
commands.Add("colorbox");

var formulaSettingsParser = new TexPredefinedFormulaSettingsParser();
symbols = formulaSettingsParser.GetSymbolMappings();
delimeters = formulaSettingsParser.GetDelimiterMappings();
textStyles = formulaSettingsParser.GetTextStyles();

var colorParser = new PredefinedColorParser();
colorParser.Parse(predefinedColors);

isInitialized = true;

var predefinedFormulasParser = new TexPredefinedFormulaParser();
Expand Down Expand Up @@ -269,6 +277,39 @@ private Atom ProcessCommand(TexFormula formula, string value, ref int position,

return new Radical(Parse(ReadGroup(formula, value, ref position, leftGroupChar, rightGroupChar))
.RootAtom, degreeFormula == null ? null : degreeFormula.RootAtom);
case "color":
{
var colorName = ReadGroup(formula, value, ref position, leftGroupChar, rightGroupChar);
string remainingString = value.Substring(position);
var remaining = Parse(remainingString);
position = value.Length;
Color color;
if (predefinedColors.TryGetValue(colorName, out color))
{
return new StyledAtom(remaining.RootAtom, null, new SolidColorBrush(color));
}
else
{
//TODO: Throw exception or default to black?
throw new TexParseException(String.Format("Color {0} not found", colorName));
}
}
case "colorbox":
{
var colorName = ReadGroup(formula, value, ref position, leftGroupChar, rightGroupChar);
string remainingString = ReadGroup(formula, value, ref position, leftGroupChar, rightGroupChar);
var remaining = Parse(remainingString);
Color color;
if (predefinedColors.TryGetValue(colorName, out color))
{
return new StyledAtom(remaining.RootAtom, new SolidColorBrush(color), null);
}
else
{
//TODO: Throw exception or default to black?
throw new TexParseException(String.Format("Color {0} not found", colorName));
}
}
}

throw new TexParseException("Invalid command.");
Expand Down
2 changes: 1 addition & 1 deletion WpfMath/UnderlinedAtom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public override Box CreateBox(TexEnvironment environment)
var resultBox = new VerticalBox();
resultBox.Add(baseBox);
resultBox.Add(new StrutBox(0, 3 * defaultLineThickness, 0, 0));
resultBox.Add(new HorizontalRule(defaultLineThickness, baseBox.Width, 0));
resultBox.Add(new HorizontalRule(environment, defaultLineThickness, baseBox.Width, 0));

resultBox.Depth = baseBox.Depth + 5 * defaultLineThickness;
resultBox.Height = baseBox.Height;
Expand Down
6 changes: 6 additions & 0 deletions WpfMath/WpfMath.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<Compile Include="CharSymbol.cs" />
<Compile Include="BigDelimeterAtom.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="PredefinedColorParser.cs" />
<Compile Include="StyledAtom.cs" />
<Compile Include="DefaultTexFont.cs" />
<Compile Include="DefaultTexFontParser.cs" />
Expand Down Expand Up @@ -142,6 +143,11 @@
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Data\PredefinedColors.xml">
<SubType>Designer</SubType>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down