/** * Will render this Text shape to RaphJS code * @see currentHeight (private member) will be modified and read by this method. * @see PowerPoint.Shape, PowerPoint.TextFrame, PowerPoint.TextRange for more information about * what this method expects. * @see Settings.Scaler() * @return String representing RaphJS code */ public override String toRaphJS() { this.currentHeight = 0; String js = ""; this.currentHeight = this.findY(); double shapeX = this.findX(); for (int i = 0; i < paragraphs.Count; i++) { js += "idsToAnimate = new Array();"; TextRange2 paragraph = paragraphs.ElementAt(i); TildaAnimation found = null; //find animation foreach (TildaAnimation animation in animations) { try { if (found == null && this.shape.Id.Equals(animation.shape.shape.Id) && i == animation.effect.Paragraph - 1) { found = animation; } } catch { } // this is obviously not the animation we are looking for; however, just throw it away rather than complaining } //add spacing above this line if (this.isBottomOrBaseLine()) { this.currentHeight -= (paragraph.ParagraphFormat.SpaceAfter) * this.scaler; } else { this.currentHeight += (paragraph.ParagraphFormat.SpaceBefore) * this.scaler; } double xAdd = 0; if ((PpBulletType)paragraph.ParagraphFormat.Bullet.Type != PpBulletType.ppBulletNone) { float[] offsets = this.findIndentSpacing(paragraph); float bulletXSpace = offsets[0]; float bulletSize = paragraph.Font.Size / 4 * this.scaler; // seems correct js += this.renderBullet(paragraph, (shapeX + bulletXSpace), (this.currentHeight - bulletSize / 2), found); xAdd += offsets[1] + bulletSize; } foreach (TextRange2 line in this.getLines(paragraph.Lines)) { js += this.renderLine(line, xAdd, found); } if (found != null) { js += "preso.animations.push({'ids':idsToAnimate,'dur':" + found.effect.Timing.Duration * 1000 + ",'delay':" + found.effect.Timing.TriggerDelayTime * 1000 + ",animate:{'fill-opacity':1,'stroke-opacity':1,'opacity':1}});"; } } return(js); }
/** * Renders a Line of Text to RaphJS code * @see currentHeight (private member) will be modified and read by this method. * @param TextRange that represents the line of text * @param double x position offset if needed * @param TildaAnimation object containing a powerpoint animation, or just null if no animation * @return String containing the RaphJS code representing this bullet */ private String renderLine(TextRange2 line, double xOffset, TildaAnimation anim = null) { //push current height position up to write the text in the right place if going bottom up if (this.isBottomOrBaseLine()) { this.currentHeight -= (line.Font.Size * this.scaler); } string font = this.fontStyle(line); string transform = this.transformation(); var fontpos = this.fontPosition((float)(xOffset), (float)(this.currentHeight - this.findY())); String textbox = "preso.shapes.push(preso.paper.text(" + (this.findX() + xOffset) + "," + this.currentHeight + ",'" + line.Text.Replace("\r", "").Replace("\v", "") + "').attr({" + font + "," + transform + "," + fontpos + "}));"; if (anim != null) { textbox += "idsToAnimate.push(preso.shapes.length-1);"; textbox += "preso.shapes[(preso.shapes.length-1)].attr({'fill-opacity':0,'stroke-opacity':0,'opacity':0});"; } //push the current height position if (this.isBottomOrBaseLine()) { this.currentHeight -= (line.ParagraphFormat.SpaceBefore) * this.scaler; } else { this.currentHeight += (line.Font.Size + line.ParagraphFormat.SpaceAfter) * this.scaler; } //add some extra spacing, I tried to be mathematical about it, but I don't know where it is coming from //it looks like 1/4 the line height, but spread above and below it, the other half of this is before the bullet if ((PpBulletType)line.ParagraphFormat.Bullet.Type != PpBulletType.ppBulletNone) { if (this.isBottomOrBaseLine()) { this.currentHeight -= ((line.Font.Size * this.scaler) / 8); } else { this.currentHeight += ((line.Font.Size * this.scaler) / 8); } } return(textbox); }
public void toRaphJSTest() { PowerPoint.Shape shape = new MockShape(); int id = 0; TildaPicture target = new TildaPicture(shape, id); TildaShape[] shapeMap = new TildaShape[2]; shapeMap[0] = target; shapeMap[0].shape.Width = 20f; shapeMap[0].shape.Height = 40f; shapeMap[0].shape.Top = 5f; shapeMap[0].shape.Left = 6f; shapeMap[1] = new TildaShape(new MockShape(), 1); shapeMap[1].shape.Width = 50f; shapeMap[1].shape.Height = 60f; List<TildaAnimation> animationMap = new List<TildaAnimation>(); TildaSlide slide = new TildaSlide(new TildaTests.Mocks.MockSlide()); string expected = @"preso.shapes.push\(preso.paper.image\('assets/[0-9]*-[0-9]*-image.png'," + shapeMap[0].position() + ","+(shapeMap[0].shape.Width*Settings.Scaler())+","+shapeMap[0].shape.Height*Settings.Scaler()+@"\)\);"; string actual; //Assert.AreEqual(slide.shapeCount, 0); actual = target.toRaphJS(); Boolean doesEqual = Regex.IsMatch(actual,expected); Assert.AreEqual(true, doesEqual); //Adding animations TildaAnimation anim = new TildaAnimation(new MockEffect(),shapeMap[0]); anim.effect.Timing.Duration = 5f; anim.effect.Timing.TriggerDelayTime = 15f; shapeMap[0].animations.Add(anim); expected = @"preso.shapes.push\(preso.paper.image\('assets/[0-9]*-[0-9]*-image.png'," + shapeMap[0].position() + "," + (shapeMap[0].shape.Width * Settings.Scaler()) + "," + shapeMap[0].shape.Height * Settings.Scaler() + @"\)\);" + @"preso.shapes\[\(preso.shapes.length-1\)\].attr\(\{'opacity':0\}\);preso.animations.push\(\{'ids':\[\(preso.shapes.length-1\)\],'dur':" + anim.effect.Timing.Duration * 1000 + @",'delay':" + anim.effect.Timing.TriggerDelayTime * 1000 + @",animate:\{'opacity':1\}\}\);"; actual = target.toRaphJS(); doesEqual = Regex.IsMatch(actual, expected); Assert.AreEqual(true, doesEqual); }
/** * Renders a Line of Text to RaphJS code * @see currentHeight (private member) will be modified and read by this method. * @param TextRange that represents the line of text * @param double x position offset if needed * @param TildaAnimation object containing a powerpoint animation, or just null if no animation * @return String containing the RaphJS code representing this bullet */ private String renderLine(TextRange2 line, double xOffset, TildaAnimation anim = null) { //push current height position up to write the text in the right place if going bottom up if(this.isBottomOrBaseLine()) this.currentHeight -= (line.Font.Size * this.scaler); string font = this.fontStyle(line); string transform = this.transformation(); var fontpos = this.fontPosition((float)(xOffset), (float)(this.currentHeight - this.findY())); String textbox = "preso.shapes.push(preso.paper.text(" + (this.findX() + xOffset) + "," + this.currentHeight + ",'" + line.Text.Replace("\r", "").Replace("\v", "") + "').attr({" + font + "," + transform + "," + fontpos + "}));"; if(anim != null) { textbox += "idsToAnimate.push(preso.shapes.length-1);"; textbox += "preso.shapes[(preso.shapes.length-1)].attr({'fill-opacity':0,'stroke-opacity':0,'opacity':0});"; } //push the current height position if(this.isBottomOrBaseLine()) this.currentHeight -= (line.ParagraphFormat.SpaceBefore) * this.scaler; else this.currentHeight += (line.Font.Size + line.ParagraphFormat.SpaceAfter) * this.scaler; //add some extra spacing, I tried to be mathematical about it, but I don't know where it is coming from //it looks like 1/4 the line height, but spread above and below it, the other half of this is before the bullet if((PpBulletType)line.ParagraphFormat.Bullet.Type != PpBulletType.ppBulletNone) if (this.isBottomOrBaseLine()) this.currentHeight -= ((line.Font.Size * this.scaler) / 8); else this.currentHeight += ((line.Font.Size*this.scaler) / 8); return textbox; }
/** * Looks up information about a bullet and creates RaphJS code to render it * @see currentHeight (private member) will be modified and read by this method. * @param TextRange the paragraph that has a bullet * @param double x position of the bullet * @param double y position of the bullet * @param TildaAnimation object containing a powerpoint animation, or just null if no animation * @return String containing the RaphJS code representing this bullet */ private String renderBullet(TextRange2 t, double x, double y, TildaAnimation anim = null) { String js = ""; //no bullet if no text if(t.Text == "" || t.Text == "\r") return js; //add some extra spacing, I tried to be mathematical about it, but I don't know where it is coming from float extraSpacing = ((t.Font.Size*this.scaler) / 8); y += extraSpacing; if(this.isBottomOrBaseLine()) this.currentHeight -= extraSpacing; else this.currentHeight += extraSpacing; //relative size is set by user, this look approximately correct float bulletSize = (t.ParagraphFormat.Bullet.RelativeSize * (t.Font.Size / 4)) * this.scaler; int bullet = t.ParagraphFormat.Bullet.Character; // find the right color of the bullet, first try the color of the bullet itself // fall back to line color otherwise int rgb = t.ParagraphFormat.Bullet.Font.Fill.ForeColor.RGB; if(rgb == 0) rgb = t.Font.Fill.ForeColor.RGB; String color = this.rgbToHex(rgb); if(t.ParagraphFormat.Bullet.Type == MsoBulletType.msoBulletNumbered) { int bulletNumber = (t.ParagraphFormat.Bullet.StartValue - 1 + t.ParagraphFormat.Bullet.Number); String bulletText = this.numberedBullet(bulletNumber, t.ParagraphFormat.Bullet.Style); js += "preso.shapes.push(preso.paper.text(" + (x+bulletSize*2) + "," + this.currentHeight + ",'" + bulletText + "'" + ").attr({" + this.fontStyle(t) + "})"; } else if(bullet == 8226) { double radius = bulletSize / 2; x += radius; y += radius; js += "preso.shapes.push(preso.paper.circle(" + x + "," + y + "," + radius + ")"; } else // if(bullet == 167), the square js += "preso.shapes.push(preso.paper.rect(" + x + "," + y + "," + bulletSize + "," + bulletSize + ")"; js += ".attr({'stroke':'" + color + "','fill':'" + color + "'}));"; if(anim != null) { js += "idsToAnimate.push(preso.shapes.length-1);"; js += "preso.shapes[(preso.shapes.length-1)].attr({'fill-opacity':0,'stroke-opacity':0});"; } return js; }
/** * Looks up information about a bullet and creates RaphJS code to render it * @see currentHeight (private member) will be modified and read by this method. * @param TextRange the paragraph that has a bullet * @param double x position of the bullet * @param double y position of the bullet * @param TildaAnimation object containing a powerpoint animation, or just null if no animation * @return String containing the RaphJS code representing this bullet */ private String renderBullet(TextRange2 t, double x, double y, TildaAnimation anim = null) { String js = ""; //no bullet if no text if (t.Text == "" || t.Text == "\r") { return(js); } //add some extra spacing, I tried to be mathematical about it, but I don't know where it is coming from float extraSpacing = ((t.Font.Size * this.scaler) / 8); y += extraSpacing; if (this.isBottomOrBaseLine()) { this.currentHeight -= extraSpacing; } else { this.currentHeight += extraSpacing; } //relative size is set by user, this look approximately correct float bulletSize = (t.ParagraphFormat.Bullet.RelativeSize * (t.Font.Size / 4)) * this.scaler; int bullet = t.ParagraphFormat.Bullet.Character; // find the right color of the bullet, first try the color of the bullet itself // fall back to line color otherwise int rgb = t.ParagraphFormat.Bullet.Font.Fill.ForeColor.RGB; if (rgb == 0) { rgb = t.Font.Fill.ForeColor.RGB; } String color = this.rgbToHex(rgb); if (t.ParagraphFormat.Bullet.Type == MsoBulletType.msoBulletNumbered) { int bulletNumber = (t.ParagraphFormat.Bullet.StartValue - 1 + t.ParagraphFormat.Bullet.Number); String bulletText = this.numberedBullet(bulletNumber, t.ParagraphFormat.Bullet.Style); js += "preso.shapes.push(preso.paper.text(" + (x + bulletSize * 2) + "," + this.currentHeight + ",'" + bulletText + "'" + ").attr({" + this.fontStyle(t) + "})"; } else if (bullet == 8226) { double radius = bulletSize / 2; x += radius; y += radius; js += "preso.shapes.push(preso.paper.circle(" + x + "," + y + "," + radius + ")"; } else // if(bullet == 167), the square { js += "preso.shapes.push(preso.paper.rect(" + x + "," + y + "," + bulletSize + "," + bulletSize + ")"; } js += ".attr({'stroke':'" + color + "','fill':'" + color + "'}));"; if (anim != null) { js += "idsToAnimate.push(preso.shapes.length-1);"; js += "preso.shapes[(preso.shapes.length-1)].attr({'fill-opacity':0,'stroke-opacity':0});"; } return(js); }