private static void InsertRenderUnit(List <RenderUnit> list, FChar character, Random random, int x, out int newX, Font font) { RenderUnit unit = new RenderUnit(); if (!font.images.ContainsKey(character)) { unit.x = x; unit.image = null; unit.corrCharacter = character; unit.rightBorderX = unit.x + (int)(0.3 * Font.pixelsPerCmH); list.Add(unit); newX = x + (int)(0.3 * Font.pixelsPerCmV); return; } int selectedImageIndex = random.Next(font.images[character].Count()); unit.x = (int)(x - font.leftMargins[character][selectedImageIndex] * Font.pixelsPerCmV); unit.image = font.images[character][selectedImageIndex]; unit.corrCharacter = character; unit.rightBorderX = unit.x + (int)(font.rightMargins[character][selectedImageIndex] * Font.pixelsPerCmH); list.Add(unit); newX = x + (int)((font.rightMargins[character][selectedImageIndex] - font.leftMargins[character][selectedImageIndex] + letterDistance) * Font.pixelsPerCmV); }
public SampleSettings(Font loadedFont, FChar character, int id) { InitializeComponent(); this.character = character; this.id = id; this.loadedFont = loadedFont; Bitmap image = loadedFont.images[character][id]; height = 200; width = (int)(height * (image.Width / (double)image.Height)); ImageCanvas.Width = width; ImageCanvas.Height = height; Preview.Width = width; Preview.Height = height; Preview.Source = BitmapUtils.ImageSourceForBitmap(image); leftBorderPos = loadedFont.leftMargins[character][id]; rightBorderPos = loadedFont.rightMargins[character][id]; LeftBorder.Text = leftBorderPos.ToString("G4", CultureInfo.InvariantCulture); RightBorder.Text = rightBorderPos.ToString("G4", CultureInfo.InvariantCulture); UpdateRectangle(); }
private void OpenSampleSettings(FChar character, int id) { SampleSettings sampleSettings = new SampleSettings(loadedFont, character, id); sampleSettings.ShowDialog(); UpdateCharacterList(); UpdateSampleList(null, null); }
private void UpdateSampleList(object sender, SelectionChangedEventArgs e) { SampleList.Children.Clear(); if (CharacterList.SelectedItem == null) { return; } FChar selectedItem = (FChar)Enum.Parse(typeof(FChar), CharacterList.SelectedItem.ToString()); for (int i = 0; i < loadedFont.images[selectedItem].Count; i++) { Bitmap image = loadedFont.images[selectedItem][i]; int width = 100; int height = (int)(width * (image.Height / (double)image.Width)); var canvas = new Canvas(); canvas.Width = width; canvas.Height = height; int id = i; canvas.MouseLeftButtonDown += (a, b) => OpenSampleSettings(selectedItem, id); var control = new System.Windows.Controls.Image(); control.Source = BitmapUtils.ImageSourceForBitmap(image); control.Width = width; control.Height = height; var rectangle = new System.Windows.Shapes.Rectangle(); rectangle.Stroke = System.Windows.Media.Brushes.Red; rectangle.Fill = System.Windows.Media.Brushes.Transparent; rectangle.StrokeThickness = 2.0; rectangle.Width = (loadedFont.rightMargins[selectedItem][i] - loadedFont.leftMargins[selectedItem][i]) / Font.imageCmW * width; rectangle.Height = height; canvas.Children.Add(control); canvas.Children.Add(rectangle); Canvas.SetLeft(rectangle, loadedFont.leftMargins[selectedItem][i] / Font.imageCmW * width); var border = new Border(); border.BorderBrush = System.Windows.Media.Brushes.Gray; border.BorderThickness = new Thickness(1.0); border.Child = canvas; border.Margin = new Thickness(5.0); SampleList.Children.Add(border); } }
private void TranslateToFChar() { for (int i = 0; i < origText.Length; i++) { //Test if it's a tag int skip; FChar tag = ConvertToTag(i, out skip); if (tag != FChar.empty) { i += skip - 1; convertedText.Add(tag); continue; } //Translate as a regular character convertedText.Add(ConvertToFChar(origText[i])); } }
private void Load(string path) { //Create temp directory if (Directory.Exists("TempFontStorage")) { Directory.Delete("TempFontStorage", true); } Directory.CreateDirectory("TempFontStorage"); //Extract zip try { ZipFile.ExtractToDirectory(path, "TempFontStorage"); } catch (InvalidDataException e) { throw new FontLoadingException("Couldn't unpack zip", e); } //Load images string[] folders = Directory.GetDirectories("TempFontStorage"); foreach (string folder in folders) { FChar key = (FChar)Enum.Parse(typeof(FChar), Path.GetFileName(folder)); List <Bitmap> value = new List <Bitmap>(); string[] imagePaths = Directory.GetFiles(folder); foreach (string imagePath in imagePaths) { Bitmap loadedBitmap; using (Bitmap bitmap = BitmapUtils.LoadBitmap(imagePath)) { loadedBitmap = new Bitmap(bitmap); } value.Add(loadedBitmap); } images.Add(key, value); } //Load margins XElement file = XElement.Load("TempFontStorage/margins.xml"); var leftPairs = file.Descendants().Where((x) => x.Name == "leftMargins").First().Descendants().Where((elem) => elem.Name == "pair"); leftMargins = new Dictionary <FChar, List <double> >(); foreach (XElement pair in leftPairs) { FChar key = (FChar)Enum.Parse(typeof(FChar), pair.Attribute("key").Value); if (!leftMargins.ContainsKey(key)) { leftMargins.Add(key, new List <double>()); } leftMargins[key].AddRange(pair.Descendants().Select((item) => double.Parse(item.Attribute("value").Value, CultureInfo.InvariantCulture))); } var rightPairs = file.Descendants().Where((x) => x.Name == "rightMargins").First().Descendants().Where((elem) => elem.Name == "pair"); rightMargins = new Dictionary <FChar, List <double> >(); foreach (XElement pair in rightPairs) { FChar key = (FChar)Enum.Parse(typeof(FChar), pair.Attribute("key").Value); if (!rightMargins.ContainsKey(key)) { rightMargins.Add(key, new List <double>()); } rightMargins[key].AddRange(pair.Descendants().Select((item) => double.Parse(item.Attribute("value").Value, CultureInfo.InvariantCulture))); } //Delete temp directory if (Directory.Exists("TempFontStorage")) { Directory.Delete("TempFontStorage", true); } }
/// <param name="startFrom">First char to process</param> /// <param name="end">Last processed character</param> /// <param name="lineWidth">in pixels</param> private static List <RenderUnit> MakeLineRenderSequence(Paragraph text, Font font, Random random, int startFrom, out int end, int lineWidth) { int fullWidth = lineWidth; if (text.centered) { lineWidth = lineWidth * 2 / 3; } List <RenderUnit> generatedLine = new List <RenderUnit>(); end = startFrom - 1; int x = 0; for (int i = startFrom; i < text.text.Count; i++) { FChar character = text.text[i]; switch (character) { case FChar.space: if (i == startFrom) { break; } RenderUnit space = new RenderUnit(); space.corrCharacter = FChar.space; space.image = null; space.x = x; x += (int)(spaceSize * Font.pixelsPerCmH); space.rightBorderX = x; generatedLine.Add(space); break; case FChar.tab: x += (int)(tabSize * Font.pixelsPerCmH); break; case FChar.linebreak: RenderUnit linebreak = new RenderUnit(); linebreak.corrCharacter = FChar.linebreak; linebreak.x = x; linebreak.image = null; generatedLine.Add(linebreak); break; default: InsertRenderUnit(generatedLine, character, random, x, out x, font); break; } end = i; if (generatedLine.Count > 0 && generatedLine.Last().rightBorderX >= lineWidth) { bool notFirstIteration = false; while (generatedLine.Last().rightBorderX >= lineWidth) { bool needsWrapping; int breakPos = FindBreakPos(generatedLine, out needsWrapping); //index of the last character on a line if (breakPos == -1) //proper wrapping is not possible { for (int j = generatedLine.Count - 1; j >= 0; j--) { if (generatedLine[j].rightBorderX < lineWidth) { breakPos = j; needsWrapping = false; break; } } } int count = generatedLine.Count - breakPos - 1; generatedLine.RemoveRange(breakPos + 1, count); end -= count; if (notFirstIteration && needsWrapping) { end++;//compensate for prev. inserted minus } int uselessX; if (needsWrapping) { InsertRenderUnit(generatedLine, FChar.minus, random, generatedLine.Last().rightBorderX, out uselessX, font); } notFirstIteration = true; } break; } } if (text.centered && generatedLine.Count > 0) { int rightDist = fullWidth - generatedLine.Last().rightBorderX; foreach (RenderUnit unit in generatedLine) { unit.x += rightDist / 2; } } return(generatedLine); }
public BindCharacter(FChar id) { Id = id; Samples = new ObservableCollection <BindSample>(); }
private void AddFromImage(string path) { FormPreparer generator = new FormPreparer(path); Bitmap prepared; try { prepared = generator.CreatePrepared(); } catch (MarkersNotFoundException e) { throw new FormException("", e); } FormCutter cutter = new FormCutter(prepared); List <List <Bitmap> > letters = cutter.Cut(); int formType; try { formType = GetFormType(prepared); } catch (MarkersNotFoundException e) { throw new FormException("", e); } foreach (List <Bitmap> bitmaps in letters) { for (int i = 0; i < bitmaps.Count; i++) { bitmaps[i] = PrepareLetterImage(bitmaps[i], formType); } } for (int i = 0; i < letters.Count; i++) { //add images of a letter FChar key = formTranslationTables[formType][i]; if (key == FChar.empty) { continue; } if (!images.ContainsKey(key)) { images.Add(key, new List <Bitmap>()); } images[key].AddRange(letters[i]); //add offsets if (!leftMargins.ContainsKey(key)) { leftMargins.Add(key, new List <double>()); } if (!rightMargins.ContainsKey(key)) { rightMargins.Add(key, new List <double>()); } } for (int i = 0; i < letters.Count; i++) { for (int j = 0; j < letters[i].Count; j++) { FChar key = formTranslationTables[formType][i]; if (key == FChar.empty) { continue; } leftMargins[key].Add(0); rightMargins[key].Add(Font.imageCmW); } } }